AAI-1523 Batch reformat aai-schema-ingest
[aai/aai-common.git] / aai-schema-ingest / src / main / java / org / onap / aai / nodes / CaseFormatStore.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
4  * ================================================================================
5  * Copyright © 2019 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *    http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.aai.nodes;
22
23 import com.google.common.base.CaseFormat;
24
25 import java.util.HashMap;
26 import java.util.Map;
27 import java.util.Optional;
28
29 import org.w3c.dom.Document;
30 import org.w3c.dom.NodeList;
31
32 /**
33  * CaseFormatStore stores the converted strings from
34  * lower hyphen (example-object) to lower camel case (exampleObject)
35  * so it avoids the creation of the object for every single request
36  * and cause an issue with taking too much memory just for the conversion
37  */
38 public class CaseFormatStore {
39
40     private final Map<String, String> lowerHyphenToLowerCamel = new HashMap<>();
41     private final Map<String, String> lowerHyphenToUpperCamel = new HashMap<>();
42     private final Map<String, String> lowerCamelToLowerHyphen = new HashMap<>();
43     private final Map<String, String> upperCamelToLowerHyphen = new HashMap<>();
44
45     CaseFormatStore() {
46     }
47
48     /**
49      * Parses the document and creates a lower camel case string
50      * upper camel string, lower hyphen and lower camel case
51      *
52      * @param doc Takes an xml document and adds it to the hash maps as appropriate
53      */
54     void parse(Document doc) {
55
56         // Get the xml-root-element and add those nodes
57         // with the attribute name and it to the hashmaps
58         // For the attribute with name, it is going to be lower-hyphen
59         // If the attribute is javaAttribute then it will be lower camel case
60         NodeList list = doc.getElementsByTagName("xml-root-element");
61         addCaseFormatForNodesAndProperties(list, "name");
62
63         list = doc.getElementsByTagName("xml-element");
64         addCaseFormatForNodesAndProperties(list, "java-attribute");
65
66         list = doc.getElementsByTagName("xml-any-element");
67         addCaseFormatForNodesAndProperties(list, "java-attribute");
68     }
69
70     private void addCaseFormatForNodesAndProperties(NodeList list, String attributeName) {
71         for (int i = 0; i < list.getLength(); i++) {
72
73             String lowerCamel = null;
74             String lowerHyphen = null;
75
76             if ("java-attribute".equals(attributeName)) {
77                 lowerCamel = list.item(i).getAttributes().getNamedItem(attributeName).getNodeValue();
78                 lowerHyphen = CaseFormat.LOWER_CAMEL.to(CaseFormat.LOWER_HYPHEN, lowerCamel);
79             } else {
80                 lowerHyphen = list.item(i).getAttributes().getNamedItem(attributeName).getNodeValue();
81                 lowerCamel = CaseFormat.LOWER_HYPHEN.to(CaseFormat.LOWER_CAMEL, lowerHyphen);
82             }
83
84             String upperCamel = CaseFormat.LOWER_HYPHEN.to(CaseFormat.UPPER_CAMEL, lowerHyphen);
85             lowerHyphenToLowerCamel.put(lowerHyphen, lowerCamel);
86             lowerHyphenToUpperCamel.put(lowerHyphen, upperCamel);
87             upperCamelToLowerHyphen.put(upperCamel, lowerHyphen);
88             lowerCamelToLowerHyphen.put(lowerCamel, lowerHyphen);
89         }
90     }
91
92     public Optional<String> fromLowerHyphenToLowerCamel(String value) {
93         return Optional.ofNullable(lowerHyphenToLowerCamel.get(value));
94     }
95
96     public Optional<String> fromLowerHyphenToUpperCamel(String value) {
97         return Optional.ofNullable(lowerHyphenToUpperCamel.get(value));
98     }
99
100     public Optional<String> fromUpperCamelToLowerHyphen(String value) {
101         return Optional.ofNullable(upperCamelToLowerHyphen.get(value));
102     }
103
104     public Optional<String> fromLowerCamelToLowerHyphen(String value) {
105         return Optional.ofNullable(lowerCamelToLowerHyphen.get(value));
106     }
107
108 }