Refactored JAXB package from cdmlib-model to cdmlib-io. Activator added to app-import.
[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
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" };
40
41 public CdmDocumentBuilder() throws SAXException, JAXBException, IOException {
42
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];
46 //
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));
50 // }
51 // Schema cdmSchema = schemaFactory.newSchema(sources);
52
53 jaxbContext = JAXBContext.newInstance(new Class[] {DataSet.class});
54 logger.debug(jaxbContext.toString());
55
56 }
57
58 public void marshal(DataSet dataSet, Writer writer) throws JAXBException {
59
60 Marshaller marshaller;
61 marshaller = jaxbContext.createMarshaller();
62
63 // For test purposes insert newlines to make the XML output readable
64 marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
65
66 marshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
67
68 CdmMarshallerListener marshallerListener = new CdmMarshallerListener();
69 marshaller.setListener(marshallerListener);
70
71 // validate with explicit schema
72 //marshaller.setSchema(cdmSchema);
73
74 marshaller.setEventHandler(new DefaultValidationEventHandler());
75
76 logger.info("Start marshalling");
77 marshaller.marshal(dataSet, writer);
78
79 }
80
81 public DataSet unmarshal(DataSet dataSet, File file) throws JAXBException {
82
83 Unmarshaller unmarshaller;
84 unmarshaller = jaxbContext.createUnmarshaller();
85
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());
89
90 logger.info("Start unmarshalling");
91 dataSet = (DataSet) unmarshaller.unmarshal(file);
92 return dataSet;
93
94 }
95
96 // can only be used with JAXB 2.1
97 // public void writeFile(DataSet dataSet, File file) throws JAXBException {
98 // marshaller.marshal(dataSet, file);
99 // }
100
101 }
102
103