1 package eu
.etaxonomy
.cdm
.jaxb
;
4 import java
.io
.FileOutputStream
;
5 import java
.io
.IOException
;
8 import javax
.xml
.XMLConstants
;
9 import javax
.xml
.bind
.JAXBContext
;
10 import javax
.xml
.bind
.JAXBException
;
11 import javax
.xml
.bind
.Marshaller
;
12 import javax
.xml
.bind
.Unmarshaller
;
13 import javax
.xml
.transform
.Source
;
14 import javax
.xml
.transform
.stream
.StreamSource
;
15 import javax
.xml
.validation
.Schema
;
16 import javax
.xml
.validation
.SchemaFactory
;
18 import org
.apache
.commons
.logging
.Log
;
19 import org
.apache
.commons
.logging
.LogFactory
;
20 import org
.xml
.sax
.SAXException
;
22 import eu
.etaxonomy
.cdm
.model
.DataSet
;
23 import eu
.etaxonomy
.cdm
.strategy
.cache
.reference
.BookDefaultCacheStrategy
;
26 Initializes a JaxbContext with one class (eu.etaxonomy.cdm.model.DataSet)
27 Binds it to XML schemas found in /src/main/resources/schema/cdm (cdm.xsd, common.xsd, name.xsd).
28 There is a bit of magic with a resource resolver in eu.etaxonomy.cdm.jaxb
29 which allows to package the schemas into a jar file.
31 public class CdmDocumentBuilder
{
33 private static Log log
= LogFactory
.getLog(CdmDocumentBuilder
.class);
35 private JAXBContext jaxbContext
;
37 // public static String CDM_NAMESPACE = "eu.etaxonomy.cdm.model";
38 // public static String[] CDM_SCHEMA_FILES = { "/schema/cdm/common.xsd",
39 // "/schema/cdm/name.xsd",
40 // "/schema/cdm/cdm.xsd" };
42 public CdmDocumentBuilder() throws SAXException
, JAXBException
, IOException
{
44 // SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
45 // schemaFactory.setResourceResolver(new CdmResourceResolver());
46 // Source[] sources = new Source[CdmDocumentBuilder.CDM_SCHEMA_FILES.length];
48 // for(int i = 0; i < CdmDocumentBuilder.CDM_SCHEMA_FILES.length; i++) {
49 // String schemaName = CdmDocumentBuilder.CDM_SCHEMA_FILES[i];
50 // sources[i] = new StreamSource(this.getClass().getResourceAsStream(schemaName));
52 // Schema cdmSchema = schemaFactory.newSchema(sources);
54 jaxbContext
= JAXBContext
.newInstance(new Class
[] {DataSet
.class});
58 public void marshal(DataSet dataSet
, Writer writer
) throws JAXBException
{
60 Marshaller marshaller
;
61 marshaller
= jaxbContext
.createMarshaller();
62 marshaller
.setProperty(Marshaller
.JAXB_FORMATTED_OUTPUT
, Boolean
.TRUE
);
64 // validate with explicit schema
65 //marshaller.setSchema(cdmSchema);
67 marshaller
.marshal(dataSet
, writer
);
71 public DataSet
unmarshal(DataSet dataSet
, File file
) throws JAXBException
{
73 Unmarshaller unmarshaller
;
74 unmarshaller
= jaxbContext
.createUnmarshaller();
76 dataSet
= (DataSet
) unmarshaller
.unmarshal(file
);
81 // public void write(DataSet dataSet, Writer writer) throws JAXBException {
82 // marshaller.marshal(dataSet, writer);
85 // can only be used with JAXB 2.1
86 // public void writeFile(DataSet dataSet, File file) throws JAXBException {
87 // marshaller.marshal(dataSet, file);