1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package org.fosstrak.epcis.utils;
22
23 import java.io.IOException;
24 import java.io.InputStream;
25 import java.io.OutputStream;
26 import java.io.Reader;
27 import java.io.StringWriter;
28
29 import javax.xml.bind.JAXBContext;
30 import javax.xml.bind.JAXBElement;
31 import javax.xml.bind.JAXBException;
32 import javax.xml.bind.Marshaller;
33 import javax.xml.bind.Unmarshaller;
34 import javax.xml.parsers.DocumentBuilder;
35 import javax.xml.parsers.DocumentBuilderFactory;
36 import javax.xml.parsers.ParserConfigurationException;
37
38 import org.fosstrak.epcis.model.EPCISQueryDocumentType;
39 import org.fosstrak.epcis.model.ObjectFactory;
40 import org.fosstrak.epcis.model.QueryResults;
41 import org.w3c.dom.Document;
42
43
44
45
46
47
48
49 public final class QueryResultsParser {
50
51 private static ObjectFactory factory = new ObjectFactory();
52
53
54
55
56
57 private QueryResultsParser() {
58 }
59
60
61
62
63
64
65
66
67
68
69
70
71 public static QueryResults parseResults(final InputStream xml) throws IOException {
72
73 try {
74 JAXBContext context = JAXBContext.newInstance(QueryResults.class);
75 Unmarshaller unmarshaller = context.createUnmarshaller();
76
77
78 JAXBElement<?> results = (JAXBElement<?>) unmarshaller.unmarshal(xml);
79 return (QueryResults) results.getValue();
80 } catch (JAXBException e) {
81
82
83 IOException ioe = new IOException(e.getMessage());
84 ioe.setStackTrace(e.getStackTrace());
85 throw ioe;
86 }
87 }
88
89
90
91
92
93
94
95
96
97
98
99
100 public static QueryResults parseQueryDocResults(final Reader r) throws IOException {
101
102 try {
103 JAXBContext context = JAXBContext.newInstance(EPCISQueryDocumentType.class);
104 Unmarshaller unmarshaller = context.createUnmarshaller();
105 JAXBElement<?> results = (JAXBElement<?>) unmarshaller.unmarshal(r);
106 EPCISQueryDocumentType doc = (EPCISQueryDocumentType) results.getValue();
107 return doc.getEPCISBody().getQueryResults();
108 } catch (JAXBException e) {
109
110
111 IOException ioe = new IOException(e.getMessage());
112 ioe.setStackTrace(e.getStackTrace());
113 throw ioe;
114 }
115 }
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135 public static void queryResultsToXml(final QueryResults results, OutputStream out) throws IOException {
136
137 try {
138 JAXBElement<QueryResults> item = factory.createQueryResults(results);
139 JAXBContext context = JAXBContext.newInstance(QueryResults.class);
140 Marshaller marshaller = context.createMarshaller();
141 marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
142 marshaller.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.TRUE);
143 marshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
144 marshaller.marshal(item, out);
145 } catch (JAXBException e) {
146 IOException ioe = new IOException(e.getMessage());
147 ioe.setStackTrace(e.getStackTrace());
148 throw ioe;
149 }
150 }
151
152
153
154
155
156
157
158
159
160
161
162
163
164 public static String queryResultsToXml(final QueryResults results) throws IOException {
165
166 try {
167 StringWriter writer = new StringWriter();
168 JAXBElement<QueryResults> item = factory.createQueryResults(results);
169 JAXBContext context = JAXBContext.newInstance(QueryResults.class);
170 Marshaller marshaller = context.createMarshaller();
171 marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
172 marshaller.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.TRUE);
173 marshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
174 marshaller.marshal(item, writer);
175 return writer.toString();
176 } catch (JAXBException e) {
177 IOException ioe = new IOException(e.getMessage());
178 ioe.setStackTrace(e.getStackTrace());
179 throw ioe;
180 }
181 }
182
183
184
185
186
187
188
189
190
191
192
193
194
195 public static Document queryResultsToDocument(final QueryResults results) throws IOException {
196
197 try {
198 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
199 dbf.setNamespaceAware(true);
200 DocumentBuilder db = dbf.newDocumentBuilder();
201 Document doc = db.newDocument();
202 JAXBElement<QueryResults> item = factory.createQueryResults(results);
203 JAXBContext context = JAXBContext.newInstance(QueryResults.class);
204 Marshaller marshaller = context.createMarshaller();
205 marshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
206 marshaller.marshal(item, doc);
207 return doc;
208 } catch (JAXBException e) {
209 IOException ioe = new IOException(e.getMessage());
210 ioe.setStackTrace(e.getStackTrace());
211 throw ioe;
212 } catch (ParserConfigurationException e) {
213 IOException ioe = new IOException(e.getMessage());
214 ioe.setStackTrace(e.getStackTrace());
215 throw ioe;
216 }
217 }
218 }