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=========================================================
20 package org.onap.aai.transforms;
22 import com.bazaarvoice.jolt.Chainr;
23 import com.bazaarvoice.jolt.JsonUtils;
24 import org.json.JSONObject;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
29 import java.util.List;
31 public class XmlFormatTransformer {
33 private static final Logger LOGGER = LoggerFactory.getLogger(XmlFormatTransformer.class);
35 private static final String RESULT_WITH_QUOTES = "\"result\"";
36 private static final String RESULTS_STRING = "results";
38 private Chainr chainr;
40 public XmlFormatTransformer() {
41 List<Object> spec = JsonUtils.classpathToList("/specs/transform-related-to-node.json");
42 this.chainr = Chainr.fromSpec(spec);
45 public String transform(String input) {
47 Object transformedOutput;
49 if(!input.contains(RESULT_WITH_QUOTES)){
50 Object inputMap = JsonUtils.jsonToMap(input);
51 transformedOutput = chainr.transform(inputMap);
53 JSONObject jsonObject;
54 if(transformedOutput == null){
55 LOGGER.debug("For the input {}, unable to transform it so returning null", input);
56 jsonObject = new JSONObject();
58 jsonObject = new JSONObject(JsonUtils.toJsonString(transformedOutput));
61 return XML.toString(jsonObject, RESULTS_STRING);
63 // If the json is already conforming to the following format
64 // {"results":[{"results":"v[2]"}]}
65 // Then no transformation is required
66 return XML.toString(new JSONObject(input));