2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.aai.nodes;
23 import com.google.common.base.CaseFormat;
25 import java.util.HashMap;
27 import java.util.Optional;
29 import org.w3c.dom.Document;
30 import org.w3c.dom.NodeList;
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
38 public class CaseFormatStore {
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<>();
49 * Parses the document and creates a lower camel case string
50 * upper camel string, lower hyphen and lower camel case
52 * @param doc Takes an xml document and adds it to the hash maps as appropriate
54 void parse(Document doc) {
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");
63 list = doc.getElementsByTagName("xml-element");
64 addCaseFormatForNodesAndProperties(list, "java-attribute");
66 list = doc.getElementsByTagName("xml-any-element");
67 addCaseFormatForNodesAndProperties(list, "java-attribute");
70 private void addCaseFormatForNodesAndProperties(NodeList list, String attributeName) {
71 for (int i = 0; i < list.getLength(); i++) {
73 String lowerCamel = null;
74 String lowerHyphen = null;
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);
80 lowerHyphen = list.item(i).getAttributes().getNamedItem(attributeName).getNodeValue();
81 lowerCamel = CaseFormat.LOWER_HYPHEN.to(CaseFormat.LOWER_CAMEL, lowerHyphen);
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);
92 public Optional<String> fromLowerHyphenToLowerCamel(String value) {
93 return Optional.ofNullable(lowerHyphenToLowerCamel.get(value));
96 public Optional<String> fromLowerHyphenToUpperCamel(String value) {
97 return Optional.ofNullable(lowerHyphenToUpperCamel.get(value));
100 public Optional<String> fromUpperCamelToLowerHyphen(String value) {
101 return Optional.ofNullable(upperCamelToLowerHyphen.get(value));
104 public Optional<String> fromLowerCamelToLowerHyphen(String value) {
105 return Optional.ofNullable(lowerCamelToLowerHyphen.get(value));