1 package org.cateproject.marshal;
2
3 import java.io.IOException;
4
5 import javax.xml.bind.JAXBException;
6 import javax.xml.bind.Marshaller;
7 import javax.xml.bind.Unmarshaller;
8 import javax.xml.parsers.ParserConfigurationException;
9 import javax.xml.parsers.SAXParser;
10 import javax.xml.parsers.SAXParserFactory;
11 import javax.xml.transform.Source;
12 import javax.xml.transform.sax.SAXSource;
13 import javax.xml.validation.Schema;
14
15 import org.apache.xml.resolver.tools.CatalogResolver;
16 import org.cateproject.model.DataSet;
17 import org.springframework.core.io.ClassPathResource;
18 import org.springframework.core.io.Resource;
19 import org.springframework.oxm.UncategorizedXmlMappingException;
20 import org.springframework.oxm.XmlMappingException;
21 import org.springframework.oxm.jaxb.Jaxb2Marshaller;
22 import org.springframework.oxm.jaxb.JaxbSystemException;
23 import org.springframework.xml.validation.SchemaLoaderUtils;
24 import org.xml.sax.SAXException;
25 import org.xml.sax.SAXNotRecognizedException;
26 import org.xml.sax.XMLReader;
27
28 import eu.etaxonomy.cdm.io.jaxb.CdmDocumentBuilder;
29 import eu.etaxonomy.cdm.io.jaxb.CdmMarshallerListener;
30 import eu.etaxonomy.cdm.io.jaxb.DefaultErrorHandler;
31 import eu.etaxonomy.cdm.io.jaxb.WarningTolerantValidationEventHandler;
32 import eu.etaxonomy.cdm.jaxb.CdmNamespacePrefixMapper;
33 import eu.etaxonomy.cdm.jaxb.FormattedText;
34 import eu.etaxonomy.cdm.jaxb.MultilanguageTextElement;
35
36
37
38
39
40
41
42 public class CateDocumentBuilder extends Jaxb2Marshaller {
43 private boolean formattedOutput = Boolean.TRUE;
44 private String encoding = "UTF-8";
45
46 public static String[] CATE_SCHEMA_FILES = {
47 "/schema/xml/xinclude.xsd",
48 "/schema/xml/xml.xsd",
49 "/schema/xml/XMLSchema.xsd",
50 "/schema/cate/cate.xsd",
51 "/schema/cdm/agent.xsd",
52 "/schema/cdm/cdm.xsd",
53 "/schema/cdm/common.xsd",
54 "/schema/cdm/description.xsd",
55 "/schema/cdm/location.xsd",
56 "/schema/cdm/media.xsd",
57 "/schema/cdm/molecular.xsd",
58 "/schema/cdm/name.xsd",
59 "/schema/cdm/occurrence.xsd",
60 "/schema/cdm/reference.xsd",
61 "/schema/cdm/taxon.xsd"};
62 private Resource schemas[];
63
64 public static Class[] CONTEXT_CLASSES = {DataSet.class,eu.etaxonomy.cdm.io.jaxb.DataSet.class,FormattedText.class,MultilanguageTextElement.class};
65
66 public CateDocumentBuilder() {
67 schemas = new Resource[CATE_SCHEMA_FILES.length];
68
69 for(int i = 0; i < CATE_SCHEMA_FILES.length; i++) {
70 schemas[i] = new ClassPathResource(CATE_SCHEMA_FILES[i]);
71 }
72
73 super.setSchemas(schemas);
74 super.setClassesToBeBound(CONTEXT_CLASSES);
75 super.setSchemaLanguage("http://www.w3.org/2001/XMLSchema");
76 }
77 @Override
78 protected void initJaxbUnmarshaller(Unmarshaller unmarshaller) throws JAXBException {
79 unmarshaller.setEventHandler(new WarningTolerantValidationEventHandler());
80 }
81
82 @Override
83 public Object unmarshal(Source source) throws XmlMappingException {
84 try {
85 SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();
86 Schema schema = SchemaLoaderUtils.loadSchema(schemas, "http://www.w3.org/2001/XMLSchema");
87
88 saxParserFactory.setNamespaceAware(true);
89 saxParserFactory.setXIncludeAware(true);
90 saxParserFactory.setValidating(true);
91 saxParserFactory.setSchema(schema);
92
93 SAXParser saxParser = saxParserFactory.newSAXParser();
94 XMLReader xmlReader = saxParser.getXMLReader();
95 xmlReader.setEntityResolver(new CatalogResolver());
96 xmlReader.setErrorHandler(new DefaultErrorHandler());
97 SAXSource saxSource = new SAXSource( xmlReader, SAXSource.sourceToInputSource(source));
98 saxSource.setSystemId(source.getSystemId());
99
100 return super.unmarshal(saxSource);
101 } catch (ParserConfigurationException pce) {
102 throw new JaxbSystemException(new JAXBException(pce));
103 } catch (SAXNotRecognizedException saxnre) {
104 throw new JaxbSystemException(new JAXBException(saxnre));
105 } catch (SAXException saxe) {
106 throw new JaxbSystemException(new JAXBException(saxe));
107 } catch (IOException ioe) {
108 throw new JaxbSystemException(new JAXBException(ioe));
109 }
110 }
111
112 protected void initJaxbMarshaller(Marshaller marshaller) throws JAXBException {
113
114
115
116
117
118
119
120
121
122
123 }
124 }
125
126