Make marshaller properties configurable
[cdmlib.git] / cdmlib-io / src / main / java / eu / etaxonomy / cdm / io / jaxb / CdmDocumentBuilder.java
1 /**
2 * Copyright (C) 2008 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
5 */
6
7 package eu.etaxonomy.cdm.io.jaxb;
8
9 import java.io.File;
10 import java.io.IOException;
11 import java.io.Writer;
12
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;
18
19 import org.apache.log4j.Logger;
20 import org.xml.sax.SAXException;
21
22 /**
23 * Initializes a JaxbContext with one class (eu.etaxonomy.cdm.model.DataSet).
24 *
25 * @author a.babadshanjan
26 */
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 {
31
32 private static final Logger logger = Logger.getLogger(CdmDocumentBuilder.class);
33
34 private JAXBContext jaxbContext;
35 private boolean formattedOutput = Boolean.TRUE;
36 private String encoding = "UTF-8";
37
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" };
42
43 public CdmDocumentBuilder() throws SAXException, JAXBException, IOException {
44
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];
48 //
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));
52 // }
53 // Schema cdmSchema = schemaFactory.newSchema(sources);
54
55 jaxbContext = JAXBContext.newInstance(new Class[] {DataSet.class});
56 if (logger.isDebugEnabled()) { logger.debug(jaxbContext.toString()); }
57
58 }
59
60 public CdmDocumentBuilder(boolean formattedOutput, String encoding)
61 throws SAXException, JAXBException, IOException {
62
63 this();
64 this.formattedOutput = formattedOutput;
65 this.encoding = encoding;
66 }
67
68 public void marshal(DataSet dataSet, Writer writer) throws JAXBException {
69
70 Marshaller marshaller;
71 marshaller = jaxbContext.createMarshaller();
72
73 // For test purposes insert newlines to make the XML output readable
74 marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, formattedOutput);
75
76 marshaller.setProperty(Marshaller.JAXB_ENCODING, encoding);
77
78 CdmMarshallerListener marshallerListener = new CdmMarshallerListener();
79 marshaller.setListener(marshallerListener);
80
81 // validate with explicit schema
82 //marshaller.setSchema(cdmSchema);
83
84 marshaller.setEventHandler(new DefaultValidationEventHandler());
85
86 logger.info("Start marshalling");
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 DefaultValidationEventHandler());
99
100 logger.info("Start unmarshalling");
101 dataSet = (DataSet) unmarshaller.unmarshal(file);
102 return dataSet;
103
104 }
105
106 // can only be used with JAXB 2.1
107 // public void writeFile(DataSet dataSet, File file) throws JAXBException {
108 // marshaller.marshal(dataSet, file);
109 // }
110
111 }
112
113