Revision 892efc69
Added by Andreas Kohlbecker almost 14 years ago
cdmlib-io/src/main/java/eu/etaxonomy/cdm/io/jaxb/CdmDocumentBuilder.java | ||
---|---|---|
18 | 18 |
import java.io.UnsupportedEncodingException; |
19 | 19 |
import java.io.Writer; |
20 | 20 |
|
21 |
import javax.xml.XMLConstants; |
|
22 |
import javax.xml.bind.JAXBContext; |
|
23 | 21 |
import javax.xml.bind.JAXBException; |
24 | 22 |
import javax.xml.bind.Marshaller; |
25 |
import javax.xml.bind.Unmarshaller;
|
|
23 |
import javax.xml.bind.PropertyException;
|
|
26 | 24 |
import javax.xml.parsers.ParserConfigurationException; |
27 | 25 |
import javax.xml.parsers.SAXParser; |
28 | 26 |
import javax.xml.parsers.SAXParserFactory; |
29 |
import javax.xml.transform.Source; |
|
30 | 27 |
import javax.xml.transform.sax.SAXResult; |
31 | 28 |
import javax.xml.transform.sax.SAXSource; |
32 |
import javax.xml.transform.stream.StreamSource;
|
|
29 |
import javax.xml.transform.stream.StreamResult;
|
|
33 | 30 |
import javax.xml.validation.Schema; |
34 |
import javax.xml.validation.SchemaFactory; |
|
35 | 31 |
|
36 | 32 |
import org.apache.log4j.Logger; |
37 | 33 |
import org.apache.xml.resolver.tools.CatalogResolver; |
34 |
import org.springframework.core.io.ClassPathResource; |
|
35 |
import org.springframework.core.io.Resource; |
|
36 |
import org.springframework.oxm.UncategorizedMappingException; |
|
37 |
import org.springframework.oxm.XmlMappingException; |
|
38 |
import org.springframework.oxm.jaxb.Jaxb2Marshaller; |
|
39 |
import org.springframework.xml.validation.SchemaLoaderUtils; |
|
38 | 40 |
import org.xml.sax.InputSource; |
39 | 41 |
import org.xml.sax.SAXException; |
40 | 42 |
import org.xml.sax.XMLReader; |
... | ... | |
51 | 53 |
//Binds it to XML schemas found in /src/main/resources/schema/cdm (cdm.xsd, common.xsd, name.xsd). |
52 | 54 |
//There is a bit of magic with a resource resolver in eu.etaxonomy.cdm.io.jaxb |
53 | 55 |
//which allows to package the schemas into a jar file. |
54 |
public class CdmDocumentBuilder { |
|
56 |
public class CdmDocumentBuilder extends Jaxb2Marshaller {
|
|
55 | 57 |
|
56 | 58 |
private static final Logger logger = Logger.getLogger(CdmDocumentBuilder.class); |
57 |
|
|
58 |
private JAXBContext jaxbContext; |
|
59 | 59 |
private boolean formattedOutput = Boolean.TRUE; |
60 | 60 |
private String encoding = "UTF-8"; |
61 |
private Schema schema; |
|
62 |
private Marshaller marshaller; |
|
63 |
private Unmarshaller unmarshaller; |
|
64 |
private XMLReader xmlReader; |
|
65 | 61 |
|
66 | 62 |
public static String CDM_NAMESPACE = "eu.etaxonomy.cdm.model"; |
67 | 63 |
public static String[] CDM_SCHEMA_FILES = { "/schema/cdm/agent.xsd", |
... | ... | |
77 | 73 |
"/schema/cdm/taxon.xsd"}; |
78 | 74 |
public static Class[] CONTEXT_CLASSES = {DataSet.class,FormattedText.class,MultilanguageTextElement.class}; |
79 | 75 |
|
76 |
private Resource schemas[]; |
|
77 |
|
|
80 | 78 |
protected String[] getSchemaFiles() { |
81 | 79 |
return CDM_SCHEMA_FILES; |
82 | 80 |
} |
... | ... | |
85 | 83 |
return CONTEXT_CLASSES; |
86 | 84 |
} |
87 | 85 |
|
88 |
protected void constructSchema() throws IOException, SAXException { |
|
89 |
SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); |
|
90 |
schemaFactory.setResourceResolver(new CdmResourceResolver()); |
|
91 |
String[] schemaFiles = getSchemaFiles(); |
|
86 |
public CdmDocumentBuilder() |
|
87 |
{ |
|
88 |
schemas = new Resource[CDM_SCHEMA_FILES.length]; |
|
92 | 89 |
|
93 |
Source[] sources = new Source[schemaFiles.length]; |
|
90 |
for(int i = 0; i < CDM_SCHEMA_FILES.length; i++) { |
|
91 |
schemas[i] = new ClassPathResource(CDM_SCHEMA_FILES[i]); |
|
92 |
} |
|
94 | 93 |
|
95 |
for(int i = 0; i < schemaFiles.length; i++) {
|
|
96 |
String schemaName = schemaFiles[i];
|
|
97 |
sources[i] = new StreamSource(this.getClass().getResourceAsStream(schemaName));
|
|
94 |
super.setSchemas(schemas);
|
|
95 |
super.setClassesToBeBound(CONTEXT_CLASSES);
|
|
96 |
super.setSchemaLanguage("http://www.w3.org/2001/XMLSchema");
|
|
98 | 97 |
} |
99 |
|
|
100 |
schema = schemaFactory.newSchema(sources); |
|
101 |
} |
|
102 |
|
|
103 |
protected void constructUnmarshaller() throws ParserConfigurationException, SAXException, JAXBException { |
|
104 |
unmarshaller = jaxbContext.createUnmarshaller(); |
|
105 |
unmarshaller.setSchema(schema); |
|
106 |
|
|
107 |
SAXParserFactory saxParserFactory = SAXParserFactory.newInstance(); |
|
108 | 98 |
|
109 |
saxParserFactory.setNamespaceAware(true); |
|
110 |
saxParserFactory.setXIncludeAware(true); |
|
111 |
saxParserFactory.setValidating(true); |
|
112 |
|
|
113 |
SAXParser saxParser = saxParserFactory.newSAXParser(); |
|
114 |
saxParser.setProperty("http://java.sun.com/xml/jaxp/properties/schemaLanguage", |
|
115 |
"http://www.w3.org/2001/XMLSchema"); |
|
116 |
xmlReader = saxParser.getXMLReader(); |
|
117 |
xmlReader.setEntityResolver(new CatalogResolver()); |
|
118 |
xmlReader.setErrorHandler(new DefaultErrorHandler()); |
|
119 |
unmarshaller.setEventHandler(new WarningTolerantValidationEventHandler()); |
|
99 |
public CdmDocumentBuilder(boolean formattedOutput, String encoding) { |
|
100 |
this.formattedOutput = formattedOutput; |
|
101 |
this.encoding = encoding; |
|
120 | 102 |
} |
121 | 103 |
|
122 |
public CdmDocumentBuilder() throws SAXException, JAXBException, IOException, ParserConfigurationException { |
|
123 |
constructSchema(); |
|
124 | 104 |
|
125 |
jaxbContext = JAXBContext.newInstance(getContextClasses()); |
|
105 |
// protected void constructUnmarshaller() throws ParserConfigurationException, SAXException, JAXBException { |
|
106 |
// unmarshaller = jaxbContext.createUnmarshaller(); |
|
107 |
// unmarshaller.setSchema(schema); |
|
108 |
// |
|
109 |
// SAXParserFactory saxParserFactory = SAXParserFactory.newInstance(); |
|
110 |
// |
|
111 |
// saxParserFactory.setNamespaceAware(true); |
|
112 |
// saxParserFactory.setXIncludeAware(true); |
|
113 |
// saxParserFactory.setValidating(true); |
|
114 |
// |
|
115 |
// SAXParser saxParser = saxParserFactory.newSAXParser(); |
|
116 |
// saxParser.setProperty("http://java.sun.com/xml/jaxp/properties/schemaLanguage", |
|
117 |
// "http://www.w3.org/2001/XMLSchema"); |
|
118 |
// xmlReader = saxParser.getXMLReader(); |
|
119 |
// xmlReader.setEntityResolver(new CatalogResolver()); |
|
120 |
// xmlReader.setErrorHandler(new DefaultErrorHandler()); |
|
121 |
// unmarshaller.setEventHandler(new WarningTolerantValidationEventHandler()); |
|
122 |
// } |
|
123 |
// |
|
124 |
// |
|
125 |
// |
|
126 | 126 |
|
127 |
constructUnmarshaller(); |
|
128 |
constructMarshaller(); |
|
129 |
} |
|
130 |
|
|
131 |
protected void constructMarshaller() throws JAXBException { |
|
132 |
marshaller = jaxbContext.createMarshaller(); |
|
127 |
@Override |
|
128 |
protected void initJaxbMarshaller(Marshaller marshaller) throws JAXBException { |
|
129 |
try { |
|
133 | 130 |
marshaller.setProperty("com.sun.xml.bind.namespacePrefixMapper", new CdmNamespacePrefixMapper() ); |
134 |
marshaller.setSchema(schema); |
|
135 | 131 |
|
136 | 132 |
// For test purposes insert newlines to make the XML output readable |
137 | 133 |
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, formattedOutput); |
138 |
// marshaller.setProperty(Marshaller.JAXB_SCHEMA_LOCATION,"http://etaxonomy.eu/cdm/model/1.0 schema/cdm/cdm.xsd");
|
|
134 |
//marshaller.setProperty(Marshaller.JAXB_SCHEMA_LOCATION,"http://etaxonomy.eu/cdm/model/1.0 schema/cdm/cdm.xsd");
|
|
139 | 135 |
//marshaller.setProperty(Marshaller.JAXB_SCHEMA_LOCATION,"http://etaxonomy.eu/cdm/model/1.0 cdm.xsd"); |
140 | 136 |
marshaller.setProperty(Marshaller.JAXB_ENCODING, encoding); |
141 | 137 |
|
142 | 138 |
CdmMarshallerListener marshallerListener = new CdmMarshallerListener(); |
143 | 139 |
marshaller.setListener(marshallerListener); |
144 | 140 |
marshaller.setEventHandler(new WarningTolerantValidationEventHandler()); |
145 |
System.out.println(marshaller.toString());
|
|
146 |
|
|
141 |
} catch(PropertyException pe) {
|
|
142 |
throw new JAXBException(pe.getMessage(),pe); |
|
147 | 143 |
} |
148 |
|
|
149 |
public CdmDocumentBuilder(boolean formattedOutput, String encoding) throws SAXException, JAXBException, IOException, ParserConfigurationException { |
|
150 |
this.formattedOutput = formattedOutput; |
|
151 |
this.encoding = encoding; |
|
152 |
constructSchema(); |
|
144 |
} |
|
153 | 145 |
|
154 |
jaxbContext = JAXBContext.newInstance(getContextClasses()); |
|
146 |
protected <T> T unmarshal(Class<T> clazz, InputSource input) { |
|
147 |
SAXParserFactory saxParserFactory = SAXParserFactory.newInstance(); |
|
148 |
Schema schema; |
|
149 |
try { |
|
150 |
schema = SchemaLoaderUtils.loadSchema(schemas, "http://www.w3.org/2001/XMLSchema"); |
|
155 | 151 |
|
156 |
constructUnmarshaller(); |
|
157 |
constructMarshaller(); |
|
158 |
} |
|
159 | 152 |
|
160 |
public void marshal(DataSet dataSet, Writer writer) throws JAXBException { |
|
153 |
saxParserFactory.setNamespaceAware(true); |
|
154 |
saxParserFactory.setXIncludeAware(true); |
|
155 |
saxParserFactory.setValidating(true); |
|
156 |
saxParserFactory.setSchema(schema); |
|
161 | 157 |
|
162 |
logger.info("Start marshalling"); |
|
163 |
marshaller.marshal(dataSet, writer); |
|
158 |
SAXParser saxParser = saxParserFactory.newSAXParser(); |
|
159 |
XMLReader xmlReader = saxParser.getXMLReader(); |
|
160 |
xmlReader.setEntityResolver(new CatalogResolver()); |
|
161 |
xmlReader.setErrorHandler(new DefaultErrorHandler()); |
|
162 |
SAXSource saxSource = new SAXSource( xmlReader, input); |
|
163 |
saxSource.setSystemId(input.getSystemId()); |
|
164 | 164 |
|
165 |
return (T)super.unmarshal(saxSource); |
|
166 |
} catch (IOException e) { |
|
167 |
throw new UncategorizedMappingException(e.getMessage(), e); |
|
168 |
} catch (SAXException e) { |
|
169 |
throw new UncategorizedMappingException(e.getMessage(), e); |
|
170 |
} catch (ParserConfigurationException e) { |
|
171 |
throw new UncategorizedMappingException(e.getMessage(), e); |
|
172 |
} |
|
165 | 173 |
} |
166 | 174 |
|
167 |
public <T> T unmarshal(Class<T> clazz,Reader reader) throws JAXBException { |
|
168 |
InputSource input = new InputSource(reader); |
|
169 |
SAXSource saxSource = new SAXSource( xmlReader, input); |
|
170 |
logger.info("Start unmarshalling"); |
|
171 |
T t = (T) unmarshaller.unmarshal(saxSource); |
|
172 |
return t; |
|
175 |
public <T> T unmarshal(Class<T> clazz,Reader reader) throws XmlMappingException { |
|
176 |
InputSource source = new InputSource(reader); |
|
177 |
return unmarshal(clazz,source); |
|
173 | 178 |
} |
174 | 179 |
|
175 |
public <T> T unmarshal(Class<T> clazz,Reader reader, String systemId) throws JAXBException {
|
|
180 |
public <T> T unmarshal(Class<T> clazz,Reader reader, String systemId) throws XmlMappingException {
|
|
176 | 181 |
InputSource input = new InputSource(reader); |
177 | 182 |
input.setSystemId(systemId); |
178 |
SAXSource saxSource = new SAXSource( xmlReader, input); |
|
179 |
logger.info("Start unmarshalling"); |
|
180 |
T t = (T) unmarshaller.unmarshal(saxSource); |
|
181 |
return t; |
|
183 |
return unmarshal(clazz,input); |
|
182 | 184 |
} |
183 | 185 |
|
184 |
public <T> T unmarshal(Class<T> clazz, File file) throws JAXBException, UnsupportedEncodingException, FileNotFoundException {
|
|
186 |
public <T> T unmarshal(Class<T> clazz, File file) throws XmlMappingException {
|
|
185 | 187 |
|
186 |
InputSource input = new InputSource(new InputStreamReader(new FileInputStream(file),encoding)); |
|
187 |
input.setSystemId(file.toURI().toString()); |
|
188 |
SAXSource saxSource = new SAXSource( xmlReader, input); |
|
189 |
logger.info("Start unmarshalling"); |
|
190 |
T t = (T) unmarshaller.unmarshal(saxSource); |
|
191 |
return t; |
|
188 |
InputSource input; |
|
189 |
try { |
|
190 |
input = new InputSource(new InputStreamReader(new FileInputStream(file),encoding)); |
|
191 |
return unmarshal(clazz,input); |
|
192 |
} catch (UnsupportedEncodingException e) { |
|
193 |
throw new UncategorizedMappingException(e.getMessage(), e); |
|
194 |
} catch (FileNotFoundException e) { |
|
195 |
throw new UncategorizedMappingException(e.getMessage(), e); |
|
196 |
} |
|
192 | 197 |
|
193 | 198 |
} |
194 | 199 |
|
195 |
public void marshal(DataSet dataSet, SAXResult result) throws JAXBException {
|
|
200 |
public void marshal(DataSet dataSet, Writer writer) throws XmlMappingException {
|
|
196 | 201 |
logger.info("Start marshalling"); |
197 |
marshaller.marshal(dataSet, result); |
|
202 |
super.marshal(dataSet, new StreamResult(writer)); |
|
203 |
} |
|
198 | 204 |
|
205 |
public void marshal(DataSet dataSet, SAXResult result) throws XmlMappingException { |
|
206 |
logger.info("Start marshalling"); |
|
207 |
super.marshal(dataSet, result); |
|
199 | 208 |
} |
200 | 209 |
|
201 |
// public void marshal(DataSet dataSet, File file) throws JAXBException { |
|
202 |
// marshaller.marshal(dataSet, file); |
|
203 |
// } |
|
204 |
|
|
205 | 210 |
} |
206 | 211 |
|
207 | 212 |
|
Also available in: Unified diff
merging /branches/cdmlib/SPRINT-Chichorieae1/ to trunk