Update license files, sonar plugin and fix tests
[aai/aai-common.git] / aai-core / src / main / java / org / openecomp / aai / ingestModel / ConvertXmlToJsonMoxyOxm.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * org.openecomp.aai
4  * ================================================================================
5  * Copyright (C) 2017 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.openecomp.aai.ingestModel;
22
23 import java.io.File;
24 import java.io.StringWriter;
25 import java.util.ArrayList;
26
27 import javax.xml.transform.stream.StreamSource;
28
29 import org.eclipse.persistence.dynamic.DynamicEntity;
30 import org.eclipse.persistence.jaxb.JAXBMarshaller;
31 import org.eclipse.persistence.jaxb.JAXBUnmarshaller;
32 import org.eclipse.persistence.jaxb.MarshallerProperties;
33 import org.eclipse.persistence.jaxb.dynamic.DynamicJAXBContext;
34
35 import org.openecomp.aai.util.AAIConfig;
36 import org.openecomp.aai.util.AAIConstants;
37
38 /**
39  * The Class ConvertXmlToJsonMoxyOxm.
40  */
41 public class ConvertXmlToJsonMoxyOxm
42 {
43         /**
44          * The main method.
45          *
46          * @param args the arguments
47          * @throws Exception the exception
48          */
49         public static void main(String[] args) throws Exception {
50
51                 String _apiVersion = AAIConfig.get(AAIConstants.AAI_DEFAULT_API_VERSION_PROP);
52                 String fileName = null;
53                 String dynamicType = null;
54                 if (args.length > 0) { 
55                         if (args[0] != null) {
56                                 _apiVersion = args[0];
57                         }
58                         if (args[1] != null) { 
59                                 fileName = args[1];
60                         }
61                         if (args[2] != null) { 
62                                 dynamicType = args[2];
63                         }
64                 }
65                 
66                 if (fileName == null) { 
67                         System.err.println("You must specify a fileName");
68                         System.exit(0);
69                 }
70                 if (dynamicType == null) { 
71                         System.err.println("You must specify a dynamic Type");
72                         System.exit(0);
73                 }
74                 
75                 ArrayList<String> apiVersions = new ArrayList<String>();
76                 apiVersions.add(_apiVersion);
77                 final IngestModelMoxyOxm m = new IngestModelMoxyOxm();
78                 m.init(apiVersions, false);
79
80                 DynamicJAXBContext jaxbContext = IngestModelMoxyOxm.aaiResourceContainer.get(_apiVersion).getJaxbContext();
81
82                 JAXBUnmarshaller unmarshaller = jaxbContext.createUnmarshaller();
83
84                 Class<? extends DynamicEntity> resultClass = jaxbContext.newDynamicEntity(dynamicType).getClass();
85
86                 DynamicEntity meObject = (DynamicEntity) unmarshaller.unmarshal(new StreamSource(new File(fileName)), resultClass).getValue();
87
88                 // put it out as JSON
89                 
90                 JAXBMarshaller marshaller = jaxbContext.createMarshaller();
91                 marshaller.setProperty(JAXBMarshaller.JAXB_FORMATTED_OUTPUT, true);
92
93                 marshaller.setProperty("eclipselink.media-type", "application/json");
94                 marshaller.setProperty("eclipselink.json.include-root", false);
95                 marshaller.setProperty(MarshallerProperties.JSON_MARSHAL_EMPTY_COLLECTIONS, Boolean.FALSE) ;
96
97                 StringWriter writer = new StringWriter();
98                 marshaller.marshal(meObject, writer);
99                 
100                 System.out.println(writer.toString());
101                 
102                 System.exit(0);
103         }
104
105 }