1 | /* |
---|
2 | * Copyright (C) 2014 CLARIN |
---|
3 | * |
---|
4 | * This program is free software: you can redistribute it and/or modify |
---|
5 | * it under the terms of the GNU General Public License as published by |
---|
6 | * the Free Software Foundation, either version 3 of the License, or |
---|
7 | * (at your option) any later version. |
---|
8 | * |
---|
9 | * This program is distributed in the hope that it will be useful, |
---|
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
12 | * GNU General Public License for more details. |
---|
13 | * |
---|
14 | * You should have received a copy of the GNU General Public License |
---|
15 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
---|
16 | */ |
---|
17 | package eu.clarin.cmdi.vlo.config; |
---|
18 | |
---|
19 | import javax.xml.bind.JAXBContext; |
---|
20 | import javax.xml.bind.JAXBException; |
---|
21 | import javax.xml.bind.Marshaller; |
---|
22 | import javax.xml.bind.Unmarshaller; |
---|
23 | import javax.xml.parsers.ParserConfigurationException; |
---|
24 | import javax.xml.parsers.SAXParserFactory; |
---|
25 | import javax.xml.transform.Result; |
---|
26 | import javax.xml.transform.Source; |
---|
27 | import javax.xml.transform.sax.SAXSource; |
---|
28 | import javax.xml.transform.stream.StreamResult; |
---|
29 | import org.xml.sax.InputSource; |
---|
30 | import org.xml.sax.SAXException; |
---|
31 | import org.xml.sax.XMLReader; |
---|
32 | |
---|
33 | /** |
---|
34 | * Serializes and deserializes {@link VloConfig} objects to/from XML files using |
---|
35 | * the Java Architecture for XML Binding (JAXB) |
---|
36 | * |
---|
37 | * @author twagoo |
---|
38 | */ |
---|
39 | public class VloConfigMarshaller { |
---|
40 | |
---|
41 | private final JAXBContext jc; |
---|
42 | |
---|
43 | public VloConfigMarshaller() throws JAXBException { |
---|
44 | this.jc = JAXBContext.newInstance(VloConfig.class); |
---|
45 | } |
---|
46 | |
---|
47 | /** |
---|
48 | * Marshals (serializes) an existing configuration to some output location |
---|
49 | * |
---|
50 | * @param config configuration to marshal |
---|
51 | * @param result output result for the marshalling, e.g. a |
---|
52 | * {@link StreamResult} for usage with Files, Streams or Writers |
---|
53 | * @throws JAXBException |
---|
54 | */ |
---|
55 | public final void marshal(VloConfig config, Result result) throws JAXBException { |
---|
56 | final Marshaller marshaller = jc.createMarshaller(); |
---|
57 | marshaller.marshal(config, result); |
---|
58 | } |
---|
59 | |
---|
60 | /** |
---|
61 | * Unmarshals (deserializes) a configuration file from some source location |
---|
62 | * |
---|
63 | * @param source the source representing the VLO configuration to unmarshal |
---|
64 | * @return the VLO configuration as described by the source |
---|
65 | * @throws JAXBException if an error occurs while unmarshalling |
---|
66 | */ |
---|
67 | public final VloConfig unmarshal(Source source) throws JAXBException { |
---|
68 | final Unmarshaller unmarshaller = jc.createUnmarshaller(); |
---|
69 | |
---|
70 | // custom parser, so that we can make it XInclude aware |
---|
71 | final SAXParserFactory spf = SAXParserFactory.newInstance(); |
---|
72 | spf.setXIncludeAware(true); |
---|
73 | spf.setNamespaceAware(true); |
---|
74 | |
---|
75 | try { |
---|
76 | final XMLReader xr = spf.newSAXParser().getXMLReader(); |
---|
77 | // XML transformation 'source' needs to be converted to a SAX 'input source' |
---|
78 | final InputSource inputSource = SAXSource.sourceToInputSource(source); |
---|
79 | final SAXSource saxSource = new SAXSource(xr, inputSource); |
---|
80 | |
---|
81 | return (VloConfig) unmarshaller.unmarshal(saxSource); |
---|
82 | } catch (ParserConfigurationException ex) { |
---|
83 | throw new JAXBException(ex); |
---|
84 | } catch (SAXException ex) { |
---|
85 | throw new JAXBException(ex); |
---|
86 | } |
---|
87 | } |
---|
88 | |
---|
89 | } |
---|