2 * Copyright (C) 2008 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
7 package eu
.etaxonomy
.cdm
.io
.jaxb
;
10 import java
.io
.IOException
;
11 import java
.io
.Writer
;
13 import javax
.xml
.bind
.JAXBContext
;
14 import javax
.xml
.bind
.JAXBException
;
15 import javax
.xml
.bind
.Marshaller
;
16 import javax
.xml
.bind
.Unmarshaller
;
17 import javax
.xml
.bind
.helpers
.DefaultValidationEventHandler
;
19 import org
.apache
.log4j
.Logger
;
20 import org
.xml
.sax
.SAXException
;
23 * Initializes a JaxbContext with one class (eu.etaxonomy.cdm.model.DataSet).
25 * @author a.babadshanjan
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.io.jaxb
29 //which allows to package the schemas into a jar file.
30 public class CdmDocumentBuilder
{
32 private static final Logger logger
= Logger
.getLogger(CdmDocumentBuilder
.class);
34 private JAXBContext jaxbContext
;
35 private boolean formattedOutput
= Boolean
.TRUE
;
36 private String encoding
= "UTF-8";
38 // public static String CDM_NAMESPACE = "eu.etaxonomy.cdm.model";
39 // public static String[] CDM_SCHEMA_FILES = { "/schema/cdm/common.xsd",
40 // "/schema/cdm/name.xsd",
41 // "/schema/cdm/cdm.xsd" };
43 public CdmDocumentBuilder() throws SAXException
, JAXBException
, IOException
{
45 // SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
46 // schemaFactory.setResourceResolver(new CdmResourceResolver());
47 // Source[] sources = new Source[CdmDocumentBuilder.CDM_SCHEMA_FILES.length];
49 // for(int i = 0; i < CdmDocumentBuilder.CDM_SCHEMA_FILES.length; i++) {
50 // String schemaName = CdmDocumentBuilder.CDM_SCHEMA_FILES[i];
51 // sources[i] = new StreamSource(this.getClass().getResourceAsStream(schemaName));
53 // Schema cdmSchema = schemaFactory.newSchema(sources);
55 jaxbContext
= JAXBContext
.newInstance(new Class
[] {DataSet
.class});
56 if (logger
.isDebugEnabled()) { logger
.debug(jaxbContext
.toString()); }
60 public CdmDocumentBuilder(boolean formattedOutput
, String encoding
)
61 throws SAXException
, JAXBException
, IOException
{
64 this.formattedOutput
= formattedOutput
;
65 this.encoding
= encoding
;
68 public void marshal(DataSet dataSet
, Writer writer
) throws JAXBException
{
70 Marshaller marshaller
;
71 marshaller
= jaxbContext
.createMarshaller();
73 // For test purposes insert newlines to make the XML output readable
74 marshaller
.setProperty(Marshaller
.JAXB_FORMATTED_OUTPUT
, formattedOutput
);
76 marshaller
.setProperty(Marshaller
.JAXB_ENCODING
, encoding
);
78 CdmMarshallerListener marshallerListener
= new CdmMarshallerListener();
79 marshaller
.setListener(marshallerListener
);
81 // validate with explicit schema
82 //marshaller.setSchema(cdmSchema);
84 marshaller
.setEventHandler(new DefaultValidationEventHandler());
86 logger
.info("Start marshalling");
87 marshaller
.marshal(dataSet
, writer
);
91 public DataSet
unmarshal(DataSet dataSet
, File file
) throws JAXBException
{
93 Unmarshaller unmarshaller
;
94 unmarshaller
= jaxbContext
.createUnmarshaller();
96 // DefaultValidationEventHandler implementation is part of the API and convenient for trouble-shooting.
97 // It prints errors to System.out.
98 //unmarshaller.setEventHandler(new DefaultValidationEventHandler());
100 logger
.info("Start unmarshalling");
101 dataSet
= (DataSet
) unmarshaller
.unmarshal(file
);
106 // can only be used with JAXB 2.1
107 // public void writeFile(DataSet dataSet, File file) throws JAXBException {
108 // marshaller.marshal(dataSet, file);