2 * ============LICENSE_START=======================================================
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
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.openecomp.aai.ingestModel;
24 import java.io.StringWriter;
25 import java.util.ArrayList;
27 import javax.xml.transform.stream.StreamSource;
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;
35 import org.openecomp.aai.util.AAIConfig;
36 import org.openecomp.aai.util.AAIConstants;
39 * The Class ConvertXmlToJsonMoxyOxm.
41 public class ConvertXmlToJsonMoxyOxm
46 * @param args the arguments
47 * @throws Exception the exception
49 public static void main(String[] args) throws Exception {
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];
58 if (args[1] != null) {
61 if (args[2] != null) {
62 dynamicType = args[2];
66 if (fileName == null) {
67 System.err.println("You must specify a fileName");
70 if (dynamicType == null) {
71 System.err.println("You must specify a dynamic Type");
75 ArrayList<String> apiVersions = new ArrayList<String>();
76 apiVersions.add(_apiVersion);
77 final IngestModelMoxyOxm m = new IngestModelMoxyOxm();
78 m.init(apiVersions, false);
80 DynamicJAXBContext jaxbContext = IngestModelMoxyOxm.aaiResourceContainer.get(_apiVersion).getJaxbContext();
82 JAXBUnmarshaller unmarshaller = jaxbContext.createUnmarshaller();
84 Class<? extends DynamicEntity> resultClass = jaxbContext.newDynamicEntity(dynamicType).getClass();
86 DynamicEntity meObject = (DynamicEntity) unmarshaller.unmarshal(new StreamSource(new File(fileName)), resultClass).getValue();
90 JAXBMarshaller marshaller = jaxbContext.createMarshaller();
91 marshaller.setProperty(JAXBMarshaller.JAXB_FORMATTED_OUTPUT, true);
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) ;
97 StringWriter writer = new StringWriter();
98 marshaller.marshal(meObject, writer);
100 System.out.println(writer.toString());