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=========================================================
20 package org.onap.aai.nodes;
22 import com.google.common.base.CaseFormat;
23 import org.w3c.dom.Document;
24 import org.w3c.dom.NodeList;
26 import java.util.HashMap;
28 import java.util.Optional;
31 * CaseFormatStore stores the converted strings from
32 * lower hyphen (example-object) to lower camel case (exampleObject)
33 * so it avoids the creation of the object for every single request
34 * and cause an issue with taking too much memory just for the conversion
36 public class CaseFormatStore {
38 private final Map<String, String> lowerHyphenToLowerCamel = new HashMap<>();
39 private final Map<String, String> lowerHyphenToUpperCamel = new HashMap<>();
40 private final Map<String, String> lowerCamelToLowerHyphen = new HashMap<>();
41 private final Map<String, String> upperCamelToLowerHyphen = new HashMap<>();
46 * Parses the document and creates a lower camel case string
47 * upper camel string, lower hyphen and lower camel case
49 * @param doc Takes an xml document and adds it to the hash maps as appropriate
51 void parse(Document doc){
53 // Get the xml-root-element and add those nodes
54 // with the attribute name and it to the hashmaps
55 // For the attribute with name, it is going to be lower-hyphen
56 // If the attribute is javaAttribute then it will be lower camel case
57 NodeList list = doc.getElementsByTagName("xml-root-element");
58 addCaseFormatForNodesAndProperties(list, "name");
60 list = doc.getElementsByTagName("xml-element");
61 addCaseFormatForNodesAndProperties(list, "java-attribute");
63 list = doc.getElementsByTagName("xml-any-element");
64 addCaseFormatForNodesAndProperties(list, "java-attribute");
67 private void addCaseFormatForNodesAndProperties(NodeList list, String attributeName) {
68 for (int i = 0; i < list.getLength(); i++) {
70 String lowerCamel = null;
71 String lowerHyphen = null;
73 if ("java-attribute".equals(attributeName)) {
74 lowerCamel = list.item(i).getAttributes().getNamedItem(attributeName).getNodeValue();
75 lowerHyphen = CaseFormat.LOWER_CAMEL.to(CaseFormat.LOWER_HYPHEN, lowerCamel);
77 lowerHyphen = list.item(i).getAttributes().getNamedItem(attributeName).getNodeValue();
78 lowerCamel = CaseFormat.LOWER_HYPHEN.to(CaseFormat.LOWER_CAMEL, lowerHyphen);
81 String upperCamel = CaseFormat.LOWER_HYPHEN.to(CaseFormat.UPPER_CAMEL, lowerHyphen);
82 lowerHyphenToLowerCamel.put(lowerHyphen, lowerCamel);
83 lowerHyphenToUpperCamel.put(lowerHyphen, upperCamel);
84 upperCamelToLowerHyphen.put(upperCamel, lowerHyphen);
85 lowerCamelToLowerHyphen.put(lowerCamel, lowerHyphen);
89 public Optional<String> fromLowerHyphenToLowerCamel(String value){
90 return Optional.ofNullable(lowerHyphenToLowerCamel.get(value));
93 public Optional<String> fromLowerHyphenToUpperCamel(String value){
94 return Optional.ofNullable(lowerHyphenToUpperCamel.get(value));
97 public Optional<String> fromUpperCamelToLowerHyphen(String value){
98 return Optional.ofNullable(upperCamelToLowerHyphen.get(value));
101 public Optional<String> fromLowerCamelToLowerHyphen(String value){
102 return Optional.ofNullable(lowerCamelToLowerHyphen.get(value));