642285bbfd25877c3f26db428eae13ab8efb58cd
[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 * 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.
8 */
9
10 package eu.etaxonomy.cdm.io.jaxb;
11
12 import java.io.File;
13 import java.io.IOException;
14 import java.io.Writer;
15
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;
21
22 import org.apache.log4j.Logger;
23 import org.xml.sax.SAXException;
24
25 /**
26 * Initializes a JaxbContext with one class (eu.etaxonomy.cdm.model.DataSet).
27 *
28 * @author a.babadshanjan
29 */
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 {
34
35 private static final Logger logger = Logger.getLogger(CdmDocumentBuilder.class);
36
37 private JAXBContext jaxbContext;
38 private boolean formattedOutput = Boolean.TRUE;
39 private String encoding = "UTF-8";
40
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" };
45
46 public CdmDocumentBuilder() throws SAXException, JAXBException, IOException {
47
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];
51 //
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));
55 // }
56 // Schema cdmSchema = schemaFactory.newSchema(sources);
57
58 jaxbContext = JAXBContext.newInstance(new Class[] {DataSet.class});
59 if (logger.isDebugEnabled()) { logger.debug(jaxbContext.toString()); }
60
61 }
62
63 public CdmDocumentBuilder(boolean formattedOutput, String encoding)
64 throws SAXException, JAXBException, IOException {
65
66 this();
67 this.formattedOutput = formattedOutput;
68 this.encoding = encoding;
69 }
70
71 public void marshal(DataSet dataSet, Writer writer) throws JAXBException {
72
73 Marshaller marshaller;
74 marshaller = jaxbContext.createMarshaller();
75
76 // For test purposes insert newlines to make the XML output readable
77 marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, formattedOutput);
78
79 marshaller.setProperty(Marshaller.JAXB_ENCODING, encoding);
80
81 CdmMarshallerListener marshallerListener = new CdmMarshallerListener();
82 marshaller.setListener(marshallerListener);
83
84 // validate with explicit schema
85 //marshaller.setSchema(cdmSchema);
86
87 marshaller.setEventHandler(new DefaultValidationEventHandler());
88
89 logger.info("Start marshalling");
90 marshaller.marshal(dataSet, writer);
91
92 }
93
94 public DataSet unmarshal(DataSet dataSet, File file) throws JAXBException {
95
96 Unmarshaller unmarshaller;
97 unmarshaller = jaxbContext.createUnmarshaller();
98
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());
102
103 logger.info("Start unmarshalling");
104 dataSet = (DataSet) unmarshaller.unmarshal(file);
105 return dataSet;
106
107 }
108
109 // can only be used with JAXB 2.1
110 // public void writeFile(DataSet dataSet, File file) throws JAXBException {
111 // marshaller.marshal(dataSet, file);
112 // }
113
114 }
115
116