(no commit message)
[cdmlib.git] / cdmlib-model / src / main / java / eu / etaxonomy / cdm / jaxb / CdmDocumentBuilder.java
1 package eu.etaxonomy.cdm.jaxb;
2
3 import java.io.File;
4 import java.io.FileOutputStream;
5 import java.io.IOException;
6 import java.io.Writer;
7
8 import javax.xml.XMLConstants;
9 import javax.xml.bind.JAXBContext;
10 import javax.xml.bind.JAXBException;
11 import javax.xml.bind.Marshaller;
12 import javax.xml.bind.Unmarshaller;
13 import javax.xml.transform.Source;
14 import javax.xml.transform.stream.StreamSource;
15 import javax.xml.validation.Schema;
16 import javax.xml.validation.SchemaFactory;
17
18 import org.apache.commons.logging.Log;
19 import org.apache.commons.logging.LogFactory;
20 import org.xml.sax.SAXException;
21
22 import eu.etaxonomy.cdm.model.DataSet;
23 import eu.etaxonomy.cdm.strategy.cache.reference.BookDefaultCacheStrategy;
24
25 /*
26 Initializes a JaxbContext with one class (eu.etaxonomy.cdm.model.DataSet)
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.jaxb
29 which allows to package the schemas into a jar file.
30 */
31 public class CdmDocumentBuilder {
32
33 private static Log log = LogFactory.getLog(CdmDocumentBuilder.class);
34
35 private JAXBContext jaxbContext;
36
37 // public static String CDM_NAMESPACE = "eu.etaxonomy.cdm.model";
38 // public static String[] CDM_SCHEMA_FILES = { "/schema/cdm/common.xsd",
39 // "/schema/cdm/name.xsd",
40 // "/schema/cdm/cdm.xsd" };
41
42 public CdmDocumentBuilder() throws SAXException, JAXBException, IOException {
43
44 // SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
45 // schemaFactory.setResourceResolver(new CdmResourceResolver());
46 // Source[] sources = new Source[CdmDocumentBuilder.CDM_SCHEMA_FILES.length];
47 //
48 // for(int i = 0; i < CdmDocumentBuilder.CDM_SCHEMA_FILES.length; i++) {
49 // String schemaName = CdmDocumentBuilder.CDM_SCHEMA_FILES[i];
50 // sources[i] = new StreamSource(this.getClass().getResourceAsStream(schemaName));
51 // }
52 // Schema cdmSchema = schemaFactory.newSchema(sources);
53
54 jaxbContext = JAXBContext.newInstance(new Class[] {DataSet.class});
55
56 }
57
58 public void marshal(DataSet dataSet, Writer writer) throws JAXBException {
59
60 Marshaller marshaller;
61 marshaller = jaxbContext.createMarshaller();
62 marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
63
64 // validate with explicit schema
65 //marshaller.setSchema(cdmSchema);
66
67 marshaller.marshal(dataSet, writer);
68
69 }
70
71 public DataSet unmarshal(DataSet dataSet, File file) throws JAXBException {
72
73 Unmarshaller unmarshaller;
74 unmarshaller = jaxbContext.createUnmarshaller();
75
76 dataSet = (DataSet) unmarshaller.unmarshal(file);
77 return dataSet;
78
79 }
80
81 // public void write(DataSet dataSet, Writer writer) throws JAXBException {
82 // marshaller.marshal(dataSet, writer);
83 // }
84
85 // can only be used with JAXB 2.1
86 // public void writeFile(DataSet dataSet, File file) throws JAXBException {
87 // marshaller.marshal(dataSet, file);
88 // }
89
90 }
91
92