1
|
package eu.etaxonomy.cdm.jaxb;
|
2
|
|
3
|
import java.io.File;
|
4
|
import java.io.IOException;
|
5
|
import java.io.Writer;
|
6
|
|
7
|
import javax.xml.bind.JAXBContext;
|
8
|
import javax.xml.bind.JAXBException;
|
9
|
import javax.xml.bind.Marshaller;
|
10
|
import javax.xml.bind.Unmarshaller;
|
11
|
import org.apache.commons.logging.Log;
|
12
|
import org.apache.commons.logging.LogFactory;
|
13
|
import org.xml.sax.SAXException;
|
14
|
|
15
|
|
16
|
/*
|
17
|
Initializes a JaxbContext with one class (eu.etaxonomy.cdm.model.DataSet)
|
18
|
Binds it to XML schemas found in /src/main/resources/schema/cdm (cdm.xsd, common.xsd, name.xsd).
|
19
|
There is a bit of magic with a resource resolver in eu.etaxonomy.cdm.jaxb
|
20
|
which allows to package the schemas into a jar file.
|
21
|
*/
|
22
|
public class CdmDocumentBuilder {
|
23
|
|
24
|
private static Log log = LogFactory.getLog(CdmDocumentBuilder.class);
|
25
|
|
26
|
private JAXBContext jaxbContext;
|
27
|
|
28
|
// public static String CDM_NAMESPACE = "eu.etaxonomy.cdm.model";
|
29
|
// public static String[] CDM_SCHEMA_FILES = { "/schema/cdm/common.xsd",
|
30
|
// "/schema/cdm/name.xsd",
|
31
|
// "/schema/cdm/cdm.xsd" };
|
32
|
|
33
|
public CdmDocumentBuilder() throws SAXException, JAXBException, IOException {
|
34
|
|
35
|
// SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
|
36
|
// schemaFactory.setResourceResolver(new CdmResourceResolver());
|
37
|
// Source[] sources = new Source[CdmDocumentBuilder.CDM_SCHEMA_FILES.length];
|
38
|
//
|
39
|
// for(int i = 0; i < CdmDocumentBuilder.CDM_SCHEMA_FILES.length; i++) {
|
40
|
// String schemaName = CdmDocumentBuilder.CDM_SCHEMA_FILES[i];
|
41
|
// sources[i] = new StreamSource(this.getClass().getResourceAsStream(schemaName));
|
42
|
// }
|
43
|
// Schema cdmSchema = schemaFactory.newSchema(sources);
|
44
|
|
45
|
jaxbContext = JAXBContext.newInstance(new Class[] {DataSet.class});
|
46
|
log.debug(jaxbContext.toString());
|
47
|
|
48
|
}
|
49
|
|
50
|
public void marshal(Object object, Writer writer) throws JAXBException {
|
51
|
|
52
|
Marshaller marshaller;
|
53
|
marshaller = jaxbContext.createMarshaller();
|
54
|
|
55
|
// For test purposes insert newlines to make the XML output readable
|
56
|
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
|
57
|
|
58
|
// UTF-8 encoding delivers error when unmarshalling
|
59
|
//marshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
|
60
|
marshaller.setProperty(Marshaller.JAXB_ENCODING, "ISO-8859-1");
|
61
|
|
62
|
// validate with explicit schema
|
63
|
//marshaller.setSchema(cdmSchema);
|
64
|
|
65
|
marshaller.marshal(object, writer);
|
66
|
|
67
|
}
|
68
|
|
69
|
public void marshal(DataSet dataSet, Writer writer) throws JAXBException {
|
70
|
|
71
|
Marshaller marshaller;
|
72
|
marshaller = jaxbContext.createMarshaller();
|
73
|
|
74
|
// For test purposes insert newlines to make the XML output readable
|
75
|
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
|
76
|
|
77
|
// UTF-8 encoding delivers error when unmarshalling
|
78
|
//marshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
|
79
|
marshaller.setProperty(Marshaller.JAXB_ENCODING, "ISO-8859-1");
|
80
|
|
81
|
CdmMarshallerListener marshallerListener = new CdmMarshallerListener();
|
82
|
marshaller.setListener(marshallerListener);
|
83
|
|
84
|
// validate with explicit schema
|
85
|
//marshaller.setSchema(cdmSchema);
|
86
|
|
87
|
marshaller.marshal(dataSet, writer);
|
88
|
|
89
|
}
|
90
|
|
91
|
public DataSet unmarshal(DataSet dataSet, File file) throws JAXBException {
|
92
|
|
93
|
Unmarshaller unmarshaller;
|
94
|
unmarshaller = jaxbContext.createUnmarshaller();
|
95
|
|
96
|
// DefaultValidationEventHandler implementation is part of the API and convenient for trouble-shooting.
|
97
|
// It prints errors to System.out.
|
98
|
//unmarshaller.setEventHandler(new javax.xml.bind.helpers.DefaultValidationEventHandler());
|
99
|
|
100
|
dataSet = (DataSet) unmarshaller.unmarshal(file);
|
101
|
return dataSet;
|
102
|
|
103
|
}
|
104
|
|
105
|
// public void write(DataSet dataSet, Writer writer) throws JAXBException {
|
106
|
// marshaller.marshal(dataSet, writer);
|
107
|
// }
|
108
|
|
109
|
// can only be used with JAXB 2.1
|
110
|
// public void writeFile(DataSet dataSet, File file) throws JAXBException {
|
111
|
// marshaller.marshal(dataSet, file);
|
112
|
// }
|
113
|
|
114
|
}
|
115
|
|
116
|
|