Project

General

Profile

Download (8.09 KB) Statistics
| Branch: | Tag: | Revision:
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.FileInputStream;
14
import java.io.FileNotFoundException;
15
import java.io.IOException;
16
import java.io.InputStreamReader;
17
import java.io.Reader;
18
import java.io.UnsupportedEncodingException;
19
import java.io.Writer;
20

    
21
import javax.xml.bind.JAXBException;
22
import javax.xml.bind.Marshaller;
23
import javax.xml.bind.PropertyException;
24
import javax.xml.parsers.ParserConfigurationException;
25
import javax.xml.parsers.SAXParser;
26
import javax.xml.parsers.SAXParserFactory;
27
import javax.xml.transform.sax.SAXResult;
28
import javax.xml.transform.sax.SAXSource;
29
import javax.xml.transform.stream.StreamResult;
30
import javax.xml.validation.Schema;
31

    
32
import org.apache.log4j.Logger;
33
import org.apache.xml.resolver.tools.CatalogResolver;
34
import org.springframework.core.io.ClassPathResource;
35
import org.springframework.core.io.Resource;
36
import org.springframework.oxm.UncategorizedMappingException;
37
import org.springframework.oxm.XmlMappingException;
38
import org.springframework.oxm.jaxb.Jaxb2Marshaller;
39
import org.springframework.xml.validation.SchemaLoaderUtils;
40
import org.xml.sax.InputSource;
41
import org.xml.sax.SAXException;
42
import org.xml.sax.XMLReader;
43

    
44
import eu.etaxonomy.cdm.jaxb.CdmNamespacePrefixMapper;
45
import eu.etaxonomy.cdm.jaxb.FormattedText;
46
import eu.etaxonomy.cdm.jaxb.MultilanguageTextElement;
47

    
48
/**
49
 * Initializes a JaxbContext with one class (eu.etaxonomy.cdm.model.DataSet). 
50
 * 
51
 * @author a.babadshanjan, ben.clark
52
 */
53
//Binds it to XML schemas found in /src/main/resources/schema/cdm (cdm.xsd, common.xsd, name.xsd).
54
//There is a bit of magic with a resource resolver in eu.etaxonomy.cdm.io.jaxb
55
//which allows to package the schemas into a jar file.
56
public class CdmDocumentBuilder extends Jaxb2Marshaller  {
57
	
58
	private static final Logger logger = Logger.getLogger(CdmDocumentBuilder.class);
59
	private boolean formattedOutput = Boolean.TRUE;
60
	private String encoding = "UTF-8";
61
	
62
	public static String CDM_NAMESPACE = "eu.etaxonomy.cdm.model";
63
	public static String[] CDM_SCHEMA_FILES = { "/schema/cdm/agent.xsd",
64
		                                        "/schema/cdm/cdm.xsd",
65
		                                        "/schema/cdm/common.xsd",
66
		                                        "/schema/cdm/description.xsd",
67
		                                        "/schema/cdm/location.xsd",
68
		                                        "/schema/cdm/media.xsd",
69
		                                        "/schema/cdm/molecular.xsd",
70
		                                        "/schema/cdm/name.xsd",
71
		                                        "/schema/cdm/occurrence.xsd",
72
		                                        "/schema/cdm/reference.xsd",
73
		                                        "/schema/cdm/taxon.xsd"};
74
	public static Class[] CONTEXT_CLASSES = {DataSet.class,FormattedText.class,MultilanguageTextElement.class};
75
	
76
	private Resource schemas[];
77
	
78
	protected String[] getSchemaFiles() {
79
		return CDM_SCHEMA_FILES;
80
	}
81
	
82
	protected Class[] getContextClasses() {
83
		return CONTEXT_CLASSES;
84
	}
85
	
86
	public CdmDocumentBuilder() 
87
	{
88
		 schemas = new Resource[CDM_SCHEMA_FILES.length];
89
		
90
		for(int i = 0; i < CDM_SCHEMA_FILES.length; i++) {
91
			schemas[i] = new ClassPathResource(CDM_SCHEMA_FILES[i]);
92
		}
93
		
94
		super.setSchemas(schemas);
95
		super.setClassesToBeBound(CONTEXT_CLASSES);
96
		super.setSchemaLanguage("http://www.w3.org/2001/XMLSchema");
97
		}
98
	
99
	public CdmDocumentBuilder(boolean formattedOutput, String encoding) {
100
		this.formattedOutput = formattedOutput;
101
		this.encoding = encoding;
102
	}
103
		                                        
104
		
105
//	protected void constructUnmarshaller() throws ParserConfigurationException, SAXException, JAXBException {
106
//		unmarshaller = jaxbContext.createUnmarshaller();
107
//        unmarshaller.setSchema(schema);
108
//	    
109
//        SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();
110
//	
111
//        saxParserFactory.setNamespaceAware(true);
112
//        saxParserFactory.setXIncludeAware(true);
113
//        saxParserFactory.setValidating(true);
114
//        
115
//        SAXParser saxParser = saxParserFactory.newSAXParser();
116
//        saxParser.setProperty("http://java.sun.com/xml/jaxp/properties/schemaLanguage",
117
//    			              "http://www.w3.org/2001/XMLSchema");
118
//	    xmlReader = saxParser.getXMLReader();
119
//	    xmlReader.setEntityResolver(new CatalogResolver());
120
//	    xmlReader.setErrorHandler(new DefaultErrorHandler());
121
//        unmarshaller.setEventHandler(new WarningTolerantValidationEventHandler());
122
//	}
123
//		                                        
124
//
125
//	
126
		
127
	@Override
128
	protected void initJaxbMarshaller(Marshaller marshaller) throws JAXBException {
129
		try {
130
        marshaller.setProperty("com.sun.xml.bind.namespacePrefixMapper", new CdmNamespacePrefixMapper() );
131
        
132
		// For test purposes insert newlines to make the XML output readable
133
		marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, formattedOutput);
134
		    //marshaller.setProperty(Marshaller.JAXB_SCHEMA_LOCATION,"http://etaxonomy.eu/cdm/model/1.0 schema/cdm/cdm.xsd");
135
		//marshaller.setProperty(Marshaller.JAXB_SCHEMA_LOCATION,"http://etaxonomy.eu/cdm/model/1.0 cdm.xsd");
136
		marshaller.setProperty(Marshaller.JAXB_ENCODING, encoding);
137
		
138
		CdmMarshallerListener marshallerListener = new CdmMarshallerListener();
139
		marshaller.setListener(marshallerListener);		
140
		marshaller.setEventHandler(new WarningTolerantValidationEventHandler());
141
		} catch(PropertyException pe) {
142
			throw new JAXBException(pe.getMessage(),pe);
143
	}
144
	}	
145
		
146
	protected <T> T unmarshal(Class<T> clazz, InputSource input) {
147
		SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();
148
		Schema schema;
149
		try {
150
			schema = SchemaLoaderUtils.loadSchema(schemas, "http://www.w3.org/2001/XMLSchema");
151
		
152
	
153
	    saxParserFactory.setNamespaceAware(true);
154
	    saxParserFactory.setXIncludeAware(true);
155
	    saxParserFactory.setValidating(true);
156
	    saxParserFactory.setSchema(schema);
157
		
158
	    SAXParser saxParser = saxParserFactory.newSAXParser();
159
	    XMLReader xmlReader = saxParser.getXMLReader();
160
		xmlReader.setEntityResolver(new CatalogResolver());
161
		xmlReader.setErrorHandler(new DefaultErrorHandler());
162
		SAXSource saxSource = new SAXSource( xmlReader, input);
163
	    saxSource.setSystemId(input.getSystemId());
164
		
165
		return (T)super.unmarshal(saxSource);
166
		} catch (IOException e) {
167
			throw new UncategorizedMappingException(e.getMessage(), e);
168
		} catch (SAXException e) {
169
			throw new UncategorizedMappingException(e.getMessage(), e);
170
		} catch (ParserConfigurationException e) {
171
			throw new UncategorizedMappingException(e.getMessage(), e);
172
	}
173
	}
174
	
175
	public <T> T unmarshal(Class<T> clazz,Reader reader) throws XmlMappingException {
176
		InputSource source = new InputSource(reader);
177
		return unmarshal(clazz,source);
178
	} 
179
	
180
	public <T> T unmarshal(Class<T> clazz,Reader reader, String systemId) throws XmlMappingException {
181
		InputSource input = new InputSource(reader);
182
		input.setSystemId(systemId);
183
		return unmarshal(clazz,input);
184
	} 
185

    
186
	public <T> T unmarshal(Class<T> clazz, File file) throws XmlMappingException {
187

    
188
		InputSource input;
189
		try {
190
			input = new InputSource(new InputStreamReader(new FileInputStream(file),encoding));
191
			return unmarshal(clazz,input);
192
		} catch (UnsupportedEncodingException e) {
193
			throw new UncategorizedMappingException(e.getMessage(), e);
194
		} catch (FileNotFoundException e) {
195
			throw new UncategorizedMappingException(e.getMessage(), e);
196
		}
197
		
198
	}
199

    
200
	public void marshal(DataSet dataSet, Writer writer) throws XmlMappingException {
201
		logger.info("Start marshalling");
202
		super.marshal(dataSet, new StreamResult(writer));	
203
	}
204
		
205
	public void marshal(DataSet dataSet, SAXResult result) throws XmlMappingException {
206
		logger.info("Start marshalling");
207
		super.marshal(dataSet, result);	
208
	}
209

    
210
}
211

    
212
 
(1-1/17)