1 /*******************************************************************************
\r
2 * ============LICENSE_START====================================================
\r
4 * * ===========================================================================
\r
5 * * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
\r
6 * * ===========================================================================
\r
7 * * Licensed under the Apache License, Version 2.0 (the "License");
\r
8 * * you may not use this file except in compliance with the License.
\r
9 * * You may obtain a copy of the License at
\r
11 * * http://www.apache.org/licenses/LICENSE-2.0
\r
13 * * Unless required by applicable law or agreed to in writing, software
\r
14 * * distributed under the License is distributed on an "AS IS" BASIS,
\r
15 * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
16 * * See the License for the specific language governing permissions and
\r
17 * * limitations under the License.
\r
18 * * ============LICENSE_END====================================================
\r
20 * * ECOMP is a trademark and service mark of AT&T Intellectual Property.
\r
22 ******************************************************************************/
\r
23 package com.data.test;
\r
25 import java.io.IOException;
\r
26 import java.io.Reader;
\r
27 import java.io.StringReader;
\r
28 import java.io.StringWriter;
\r
30 import org.junit.Test;
\r
31 import org.onap.aaf.inno.env.APIException;
\r
32 import org.onap.aaf.inno.env.Data;
\r
33 import org.onap.aaf.inno.env.DataFactory;
\r
34 import org.onap.aaf.inno.env.EnvJAXB;
\r
35 import org.onap.aaf.inno.env.impl.BasicEnv;
\r
36 import org.onap.aaf.rosetta.InJson;
\r
37 import org.onap.aaf.rosetta.InXML;
\r
38 import org.onap.aaf.rosetta.Out;
\r
39 import org.onap.aaf.rosetta.OutJson;
\r
40 import org.onap.aaf.rosetta.OutRaw;
\r
41 import org.onap.aaf.rosetta.OutXML;
\r
42 import org.onap.aaf.rosetta.Parse;
\r
43 import org.onap.aaf.rosetta.ParseException;
\r
45 import inherit.DerivedA;
\r
46 import inherit.Root;
\r
48 public class JU_Stream2Obj {
\r
51 <?xml version="1.0" encoding=Config.UTF-8 standalone="yes"?>
\r
52 <root xmlns="urn:inherit">
\r
53 <base xsi:type="derivedA" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
\r
54 <name>myDerivedA_1</name>
\r
56 <shortName>mda_1</shortName>
\r
57 <value>value_1</value>
\r
58 <value>value_2</value>
\r
62 {"base":[{"__extension":"derivedA","name":"myDerivedA_1","num":1432,"shortName":"mda_1","value":["value_1","value_2"]}]}
\r
66 public void json2Obj() throws APIException, SecurityException, NoSuchFieldException, ClassNotFoundException, ParseException, IOException {
\r
67 DerivedA da = new DerivedA();
\r
68 da.setName("myDerivedA_1");
\r
69 da.setNum((short)1432);
\r
70 da.setShortName("mda_1");
\r
71 da.getValue().add("value_1");
\r
72 da.getValue().add("value_2");
\r
74 Root root = new Root();
\r
75 root.getBase().add(da);
\r
77 da = new DerivedA();
\r
78 da.setName("myDerivedA_2");
\r
79 da.setNum((short)1432);
\r
80 da.setShortName("mda_2");
\r
81 da.getValue().add("value_2.1");
\r
82 da.getValue().add("value_2.2");
\r
83 root.getBase().add(da);
\r
85 EnvJAXB env = new BasicEnv();
\r
86 DataFactory<Root> rootDF = env.newDataFactory(Root.class);
\r
88 String xml = rootDF.newData(env).out(Data.TYPE.XML).load(root).option(Data.PRETTY).asString();
\r
89 System.out.println(xml);
\r
92 Parse<Reader,?> in = inXML = new InXML(Root.class);
\r
93 Out out = new OutRaw();
\r
95 StringWriter sw = new StringWriter();
\r
96 out.extract(new StringReader(xml), sw, in);
\r
97 System.out.println(sw.toString());
\r
100 out = new OutJson();
\r
102 sw = new StringWriter();
\r
103 out.extract(new StringReader(xml), sw, in);
\r
105 System.out.println(json = sw.toString());
\r
108 out = new OutRaw();
\r
110 sw = new StringWriter();
\r
111 out.extract(new StringReader(json), sw, in);
\r
112 System.out.println(sw.toString());
\r
114 out = new OutXML(inXML);
\r
116 sw = new StringWriter();
\r
117 out.extract(new StringReader(json), sw, in, true);
\r
118 System.out.println(sw.toString());
\r
120 System.out.flush();
\r