2 * ============LICENSE_START=======================================================
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
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.transforms;
23 import com.bazaarvoice.jolt.Chainr;
24 import com.bazaarvoice.jolt.JsonUtils;
26 import java.util.List;
28 import org.json.JSONObject;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
33 public class XmlFormatTransformer {
35 private static final Logger LOGGER = LoggerFactory.getLogger(XmlFormatTransformer.class);
37 private static final String RESULT_WITH_QUOTES = "\"result\"";
38 private static final String RESULTS_STRING = "results";
40 private Chainr chainr;
42 public XmlFormatTransformer() {
43 List<Object> spec = JsonUtils.classpathToList("/specs/transform-related-to-node.json");
44 this.chainr = Chainr.fromSpec(spec);
47 public String transform(String input) {
49 Object transformedOutput;
51 if (!input.contains(RESULT_WITH_QUOTES)) {
52 Object inputMap = JsonUtils.jsonToMap(input);
53 transformedOutput = chainr.transform(inputMap);
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();
60 jsonObject = new JSONObject(JsonUtils.toJsonString(transformedOutput));
63 return XML.toString(jsonObject, RESULTS_STRING);
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));