View Javadoc

1   /*
2    * Copyright (C) 2007 ETH Zurich
3    *
4    * This file is part of Fosstrak (www.fosstrak.org).
5    *
6    * Fosstrak is free software; you can redistribute it and/or
7    * modify it under the terms of the GNU Lesser General Public
8    * License version 2.1, as published by the Free Software Foundation.
9    *
10   * Fosstrak is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13   * Lesser General Public License for more details.
14   *
15   * You should have received a copy of the GNU Lesser General Public
16   * License along with Fosstrak; if not, write to the Free
17   * Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18   * Boston, MA  02110-1301  USA
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   * Parses the XML representation of an EPCIS query results into a QueryResults
45   * object for use with axis.
46   * 
47   * @author Marco Steybe
48   */
49  public final class QueryResultsParser {
50  
51      private static ObjectFactory factory = new ObjectFactory();
52  
53      /**
54       * Empty default constructor. Utility classes should not have public
55       * constructors.
56       */
57      private QueryResultsParser() {
58      }
59  
60      /**
61       * A helper method to parse and convert the XML representation of an EPCIS
62       * query results into a QueryResults object.
63       * 
64       * @param xml
65       *            The InputStream containing the XML representation of a
66       *            QueryResults object.
67       * @return The parsed QueryResults object.
68       * @throws IOException
69       *             If an error de-serializing the InputStream occurred.
70       */
71      public static QueryResults parseResults(final InputStream xml) throws IOException {
72          // de-serialize the XML
73          try {
74              JAXBContext context = JAXBContext.newInstance(QueryResults.class);
75              Unmarshaller unmarshaller = context.createUnmarshaller();
76              // setting schema to null will turn XML validation off
77              // unmarshaller.setSchema(null);
78              JAXBElement<?> results = (JAXBElement<?>) unmarshaller.unmarshal(xml);
79              return (QueryResults) results.getValue();
80          } catch (JAXBException e) {
81              // wrap JAXBException into IOException to keep the interface
82              // JAXB-free
83              IOException ioe = new IOException(e.getMessage());
84              ioe.setStackTrace(e.getStackTrace());
85              throw ioe;
86          }
87      }
88  
89      /**
90       * A helper method to parse and convert the XML representation of an EPCIS
91       * query document into an QueryResults object.
92       * 
93       * @param xml
94       *            The Reader containing the XML representation of an
95       *            EPCISQueryDocumentType object.
96       * @return The parsed QueryResults object.
97       * @throws IOException
98       *             If an error de-serializing the InputStream occurred.
99       */
100     public static QueryResults parseQueryDocResults(final Reader r) throws IOException {
101         // de-serialize the XML
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             // wrap JAXBException into IOException to keep the interface
110             // JAXB-free
111             IOException ioe = new IOException(e.getMessage());
112             ioe.setStackTrace(e.getStackTrace());
113             throw ioe;
114         }
115     }
116 
117     /**
118      * Marshals the given QueryResults object to its XML representations and
119      * serializes it to the given OutputStream. Use as follows for printing a
120      * QueryResults instance to standard output:
121      * 
122      * <pre>
123      * QueryResults results = ...
124      * QueryResultsParser.queryResultsToXml(results, System.out);
125      * </pre>
126      * 
127      * @param results
128      *            The QueryResults object to marshal into XML.
129      * @param out
130      *            The OutputStream to which the XML representation will be
131      *            written to.
132      * @throws IOException
133      *             If an error marshaling the QueryResults object occurred.
134      */
135     public static void queryResultsToXml(final QueryResults results, OutputStream out) throws IOException {
136         // serialize the response
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      * Marshals the given QueryResults object to its XML representations and
154      * returns it as a String.
155      * 
156      * @param results
157      *            The QueryResults object to marshal into XML.
158      * @param out
159      *            The OutputStream to which the XML representation will be
160      *            written to.
161      * @throws IOException
162      *             If an error marshaling the QueryResults object occurred.
163      */
164     public static String queryResultsToXml(final QueryResults results) throws IOException {
165         // serialize the response
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      * Marshals the given QueryResults object to its XML representation and
185      * returns a DOM Document of it.
186      * 
187      * @param results
188      *            The QueryResults object to marshal into XML.
189      * @param out
190      *            The OutputStream to which the XML representation will be
191      *            written to.
192      * @throws IOException
193      *             If an error marshaling the QueryResults object occurred.
194      */
195     public static Document queryResultsToDocument(final QueryResults results) throws IOException {
196         // serialize the response
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 }