Reduce the number of problems in aai-common by removing unused imports
[aai/aai-common.git] / aai-core / src / main / java / org / onap / aai / transforms / XmlFormatTransformer.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
4  * ================================================================================
5  * Copyright © 2017-2018 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.transforms;
22
23 import com.bazaarvoice.jolt.Chainr;
24 import com.bazaarvoice.jolt.JsonUtils;
25
26 import java.util.List;
27
28 import org.json.JSONObject;
29 import org.json.XML;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32
33 public class XmlFormatTransformer {
34
35     private static final Logger LOGGER = LoggerFactory.getLogger(XmlFormatTransformer.class);
36
37     private static final String RESULT_WITH_QUOTES = "\"result\"";
38     private static final String RESULTS_STRING = "results";
39
40     private Chainr chainr;
41
42     public XmlFormatTransformer() {
43         List<Object> spec = JsonUtils.classpathToList("/specs/transform-related-to-node.json");
44         this.chainr = Chainr.fromSpec(spec);
45     }
46
47     public String transform(String input) {
48
49         Object transformedOutput;
50
51         if (!input.contains(RESULT_WITH_QUOTES)) {
52             Object inputMap = JsonUtils.jsonToMap(input);
53             transformedOutput = chainr.transform(inputMap);
54
55             JSONObject jsonObject;
56             if (transformedOutput == null) {
57                 LOGGER.debug("For the input {}, unable to transform it so returning null", input);
58                 jsonObject = new JSONObject();
59             } else {
60                 jsonObject = new JSONObject(JsonUtils.toJsonString(transformedOutput));
61             }
62
63             return XML.toString(jsonObject, RESULTS_STRING);
64         } else {
65             // If the json is already conforming to the following format
66             // {"results":[{"results":"v[2]"}]}
67             // Then no transformation is required
68             return XML.toString(new JSONObject(input));
69         }
70
71     }
72 }