2 * Copyright (C) 2008 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
6 * The contents of this file are subject to the Mozilla Public License Version 1.1
7 * See LICENSE.TXT at the top of this package for the full license terms.
10 package eu
.etaxonomy
.cdm
.io
.jaxb
;
13 import java
.io
.IOException
;
14 import java
.io
.Writer
;
16 import javax
.xml
.bind
.JAXBContext
;
17 import javax
.xml
.bind
.JAXBException
;
18 import javax
.xml
.bind
.Marshaller
;
19 import javax
.xml
.bind
.Unmarshaller
;
20 import javax
.xml
.bind
.helpers
.DefaultValidationEventHandler
;
22 import org
.apache
.log4j
.Logger
;
23 import org
.xml
.sax
.SAXException
;
26 * Initializes a JaxbContext with one class (eu.etaxonomy.cdm.model.DataSet).
28 * @author a.babadshanjan
30 //Binds it to XML schemas found in /src/main/resources/schema/cdm (cdm.xsd, common.xsd, name.xsd).
31 //There is a bit of magic with a resource resolver in eu.etaxonomy.cdm.io.jaxb
32 //which allows to package the schemas into a jar file.
33 public class CdmDocumentBuilder
{
35 private static final Logger logger
= Logger
.getLogger(CdmDocumentBuilder
.class);
37 private JAXBContext jaxbContext
;
38 private boolean formattedOutput
= Boolean
.TRUE
;
39 private String encoding
= "UTF-8";
41 // public static String CDM_NAMESPACE = "eu.etaxonomy.cdm.model";
42 // public static String[] CDM_SCHEMA_FILES = { "/schema/cdm/common.xsd",
43 // "/schema/cdm/name.xsd",
44 // "/schema/cdm/cdm.xsd" };
46 public CdmDocumentBuilder() throws SAXException
, JAXBException
, IOException
{
48 // SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
49 // schemaFactory.setResourceResolver(new CdmResourceResolver());
50 // Source[] sources = new Source[CdmDocumentBuilder.CDM_SCHEMA_FILES.length];
52 // for(int i = 0; i < CdmDocumentBuilder.CDM_SCHEMA_FILES.length; i++) {
53 // String schemaName = CdmDocumentBuilder.CDM_SCHEMA_FILES[i];
54 // sources[i] = new StreamSource(this.getClass().getResourceAsStream(schemaName));
56 // Schema cdmSchema = schemaFactory.newSchema(sources);
58 jaxbContext
= JAXBContext
.newInstance(new Class
[] {DataSet
.class});
59 if (logger
.isDebugEnabled()) { logger
.debug(jaxbContext
.toString()); }
63 public CdmDocumentBuilder(boolean formattedOutput
, String encoding
)
64 throws SAXException
, JAXBException
, IOException
{
67 this.formattedOutput
= formattedOutput
;
68 this.encoding
= encoding
;
71 public void marshal(DataSet dataSet
, Writer writer
) throws JAXBException
{
73 Marshaller marshaller
;
74 marshaller
= jaxbContext
.createMarshaller();
76 // For test purposes insert newlines to make the XML output readable
77 marshaller
.setProperty(Marshaller
.JAXB_FORMATTED_OUTPUT
, formattedOutput
);
79 marshaller
.setProperty(Marshaller
.JAXB_ENCODING
, encoding
);
81 CdmMarshallerListener marshallerListener
= new CdmMarshallerListener();
82 marshaller
.setListener(marshallerListener
);
84 // validate with explicit schema
85 //marshaller.setSchema(cdmSchema);
87 marshaller
.setEventHandler(new DefaultValidationEventHandler());
89 logger
.info("Start marshalling");
90 marshaller
.marshal(dataSet
, writer
);
94 public DataSet
unmarshal(DataSet dataSet
, File file
) throws JAXBException
{
96 Unmarshaller unmarshaller
;
97 unmarshaller
= jaxbContext
.createUnmarshaller();
99 // DefaultValidationEventHandler implementation is part of the API and convenient for trouble-shooting.
100 // It prints errors to System.out.
101 //unmarshaller.setEventHandler(new DefaultValidationEventHandler());
103 logger
.info("Start unmarshalling");
104 dataSet
= (DataSet
) unmarshaller
.unmarshal(file
);
109 // can only be used with JAXB 2.1
110 // public void writeFile(DataSet dataSet, File file) throws JAXBException {
111 // marshaller.marshal(dataSet, file);