Add wso2bpel-ext code
[vfc/nfvo/wfengine.git] / wso2bpel-ext / wso2bpel-core / wso2bpel-mgr / src / main / java / org / openo / carbon / bpel / util / Xml2JsonUtil.java
1 /**
2  * Copyright 2016 [ZTE] and others.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package org.openo.carbon.bpel.util;
17
18 import java.io.ByteArrayInputStream;
19 import java.io.File;
20 import java.io.InputStream;
21 import java.util.HashMap;
22 import java.util.LinkedList;
23 import java.util.List;
24 import java.util.Map;
25
26 import net.sf.json.JSONObject;
27
28 import org.jdom.Document;
29 import org.jdom.Element;
30 import org.jdom.input.SAXBuilder;
31
32 public class Xml2JsonUtil {
33   /**
34    * transform xml to json
35    * 
36    * @param xml xml format string
37    * @return return json string when success; otherwise return null
38    */
39   public static String xml2JSON(String xml) {
40     JSONObject obj = new JSONObject();
41     try {
42       InputStream is = new ByteArrayInputStream(xml.getBytes("utf-8"));
43       SAXBuilder sb = new SAXBuilder();
44       Document doc = sb.build(is);
45       Element root = doc.getRootElement();
46       obj.put(root.getName(), iterateElement(root));
47       return obj.toString();
48     } catch (Exception e) {
49       e.printStackTrace();
50       return null;
51     }
52   }
53
54   /**
55    * transform xml file to json string
56    * 
57    * @param file java.io.File is an effective xml file
58    * @return return json string when success; otherwise return null
59    */
60   public static String xml2JSON(File file) {
61     JSONObject obj = new JSONObject();
62     try {
63       SAXBuilder sb = new SAXBuilder();
64       Document doc = sb.build(file);
65       Element root = doc.getRootElement();
66       obj.put(root.getName(), iterateElement(root));
67       return obj.toString();
68     } catch (Exception e) {
69       e.printStackTrace();
70       return null;
71     }
72   }
73
74   /**
75    * an iteration function
76    * 
77    * @param parentElement : org.jdom.Element
78    * @return java.util.Map
79    */
80   @SuppressWarnings({"unchecked", "rawtypes"})
81   private static Map iterateElement(Element parentElement) {
82     List node = parentElement.getChildren();
83     Element element = null;
84     Map map = new HashMap();
85     List list = null;
86     for (int i = 0; i < node.size(); i++) {
87       element = (Element) node.get(i);
88       if (element.getTextTrim().equals("")) {
89         if (element.getChildren().size() == 0)
90           continue;
91         if (map.containsKey(element.getName())) {
92           Object obj = map.get(element.getName());
93           if (obj instanceof Map) {
94             list = new LinkedList();
95             list.add(obj);
96             list.add(iterateElement(element));
97             map.remove(element.getName());
98             map.put(element.getName(), list);
99           } else if (obj instanceof List) {
100             list = (List) obj;
101             list.add(iterateElement(element));
102           }
103         } else {
104           map.put(element.getName(), iterateElement(element));
105         }
106       } else {
107         map.put(element.getName(), element.getTextTrim());
108       }
109     }
110     return map;
111   }
112
113   public static void main(String[] args) {
114     System.out.println(Xml2JsonUtil.xml2JSON("<MapSet>" + "<MapGroup id='Sheboygan'>" + "<Map>"
115         + "<Type>MapGuideddddddd</Type>" + "<SingleTile>true</SingleTile>" + "<Extension>"
116         + "<ResourceId>ddd</ResourceId>" + "</Extension>" + "</Map>" + "<Map>" + "<Type>ccc</Type>"
117         + "<SingleTile>ggg</SingleTile>" + "<Extension>" + "<ResourceId>aaa</ResourceId>"
118         + "</Extension>" + "</Map>" + "<Extension />" + "</MapGroup>" + "<ddd>" + "33333333"
119         + "</ddd>" + "<ddd>" + "444" + "</ddd>" + "</MapSet>"));
120
121     String xml =
122         "<body>   <p:helloXsl xmlns:p=\"http://ode/bpel/unit-test.wsdl\">      <!--Exactly 1 occurrence-->      <TestPart>         <!--Exactly 1 occurrence-->         <content>fdsafasdfasdf</content>      </TestPart>   </p:helloXsl></body>";
123
124     System.out.println(Xml2JsonUtil.xml2JSON(xml));
125
126     xml =
127         "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:unit=\"http://ode/bpel/unit-test.wsdl\">   <soapenv:Header/>   <soapenv:Body>      <unit:helloXsl>         <TestPart>            <content>?</content>         </TestPart>      </unit:helloXsl>   </soapenv:Body></soapenv:Envelope>";
128
129     System.out.println(Xml2JsonUtil.xml2JSON(xml));
130
131     xml =
132         "<ns:uploadServiceResponse xmlns:ns=\"http://services.deployer.bpel.carbon.wso2.org\"><ns:return xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:nil=\"true\"/></ns:uploadServiceResponse>";
133
134     System.out.println(Xml2JsonUtil.xml2JSON(xml));
135
136     xml =
137         "<body>   <p:planInput xmlns:p=\"http://www.open-o.org/tosca/nfv/2015/12\">      <!--Exactly 1 occurrence-->      <p:sfc_count>2</p:sfc_count>      <!--Exactly 1 occurrence-->      <p:iaUrl></p:iaUrl>      <!--Exactly 1 occurrence-->      <p:vnfmId>112</p:vnfmId>      <!--Exactly 1 occurrence-->      <p:object_context>{\"e\":{\"f\":\"4\"}}</p:object_context>      <!--Exactly 1 occurrence-->      <p:statusUrl></p:statusUrl>      <!--Exactly 1 occurrence-->      <p:serviceTemplateId>?</p:serviceTemplateId>      <!--Exactly 1 occurrence-->      <p:roUrl></p:roUrl>      <!--Exactly 1 occurrence-->      <p:vl_count>2</p:vl_count>      <!--Exactly 1 occurrence-->      <p:containerapiUrl>?</p:containerapiUrl>      <!--Exactly 1 occurrence-->      <p:flavor></p:flavor>      <!--Exactly 1 occurrence-->      <p:nsInstanceId>223</p:nsInstanceId>      <!--Exactly 1 occurrence-->      <p:instanceId>334</p:instanceId>      <!--Exactly 1 occurrence-->      <p:resourceUrl></p:resourceUrl>      <!--Exactly 1 occurrence-->      <p:vnf_count>2</p:vnf_count>      <!--Exactly 1 occurrence-->      <p:callbackId></p:callbackId>      <!--Exactly 1 occurrence-->      <p:object_additionalParamForVnf>[{\"b\":1},{\"c\":{\"d\":\"2\"}}}]</p:object_additionalParamForVnf>      <!--Exactly 1 occurrence-->      <p:object_additionalParamForNs>[{\"a\":3},{\"e\":{\"f\":\"4\"}}}]</p:object_additionalParamForNs>      <!--Exactly 1 occurrence-->      <p:flavorParams></p:flavorParams>   </p:planInput></body>";
138
139     System.out.println(Xml2JsonUtil.xml2JSON(xml));
140   }
141 }