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
;
36 // public static String CDM_NAMESPACE = "eu.etaxonomy.cdm.model";
37 // public static String[] CDM_SCHEMA_FILES = { "/schema/cdm/common.xsd",
38 // "/schema/cdm/name.xsd",
39 // "/schema/cdm/cdm.xsd" };
41 public CdmDocumentBuilder() throws SAXException
, JAXBException
, IOException
{
43 // SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
44 // schemaFactory.setResourceResolver(new CdmResourceResolver());
45 // Source[] sources = new Source[CdmDocumentBuilder.CDM_SCHEMA_FILES.length];
47 // for(int i = 0; i < CdmDocumentBuilder.CDM_SCHEMA_FILES.length; i++) {
48 // String schemaName = CdmDocumentBuilder.CDM_SCHEMA_FILES[i];
49 // sources[i] = new StreamSource(this.getClass().getResourceAsStream(schemaName));
51 // Schema cdmSchema = schemaFactory.newSchema(sources);
53 jaxbContext
= JAXBContext
.newInstance(new Class
[] {DataSet
.class});
54 logger
.debug(jaxbContext
.toString());
58 public void marshal(DataSet dataSet
, Writer writer
) throws JAXBException
{
60 Marshaller marshaller
;
61 marshaller
= jaxbContext
.createMarshaller();
63 // For test purposes insert newlines to make the XML output readable
64 marshaller
.setProperty(Marshaller
.JAXB_FORMATTED_OUTPUT
, Boolean
.TRUE
);
66 marshaller
.setProperty(Marshaller
.JAXB_ENCODING
, "UTF-8");
68 CdmMarshallerListener marshallerListener
= new CdmMarshallerListener();
69 marshaller
.setListener(marshallerListener
);
71 // validate with explicit schema
72 //marshaller.setSchema(cdmSchema);
74 marshaller
.setEventHandler(new DefaultValidationEventHandler());
76 logger
.info("Start marshalling");
77 marshaller
.marshal(dataSet
, writer
);
81 public DataSet
unmarshal(DataSet dataSet
, File file
) throws JAXBException
{
83 Unmarshaller unmarshaller
;
84 unmarshaller
= jaxbContext
.createUnmarshaller();
86 // DefaultValidationEventHandler implementation is part of the API and convenient for trouble-shooting.
87 // It prints errors to System.out.
88 //unmarshaller.setEventHandler(new DefaultValidationEventHandler());
90 logger
.info("Start unmarshalling");
91 dataSet
= (DataSet
) unmarshaller
.unmarshal(file
);
96 // can only be used with JAXB 2.1
97 // public void writeFile(DataSet dataSet, File file) throws JAXBException {
98 // marshaller.marshal(dataSet, file);