<artifactId>xmlunit</artifactId>
</dependency>
<dependency>
- <groupId>org.apache.poi</groupId>
- <artifactId>poi</artifactId>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>org.springframework.oxm</artifactId>
- </dependency>
- <dependency>
- <groupId>com.yourkit</groupId>
- <artifactId>yjp-controller-api-redist</artifactId>
- <!-- version>10.0.3</version> -->
- <version>9.0.8</version>
- </dependency>
-<!-- <dependency> -->
-<!-- <groupId>org.springframework.ws</groupId> -->
-<!-- <artifactId>spring-xml</artifactId> -->
-<!-- <version>2.1.2.RELEASE</version> -->
-<!-- <exclusions> -->
-<!-- <exclusion> -->
-<!-- <groupId>org.springframework</groupId> -->
-<!-- <artifactId>spring-core</artifactId> -->
-<!-- </exclusion> -->
-<!-- <exclusion> -->
-<!-- <groupId>org.springframework</groupId> -->
-<!-- <artifactId>spring-context</artifactId> -->
-<!-- </exclusion> -->
-<!-- <exclusion> -->
-<!-- <groupId>org.springframework</groupId> -->
-<!-- <artifactId>spring-beans</artifactId> -->
-<!-- </exclusion> -->
-<!-- </exclusions> -->
-<!-- </dependency> -->
- <dependency>
- <groupId>identificationKeyAPI</groupId>
- <artifactId>identificationKeyAPI</artifactId>
- </dependency>
+ <groupId>org.apache.poi</groupId>
+ <artifactId>poi</artifactId>
+ </dependency>
+ <dependency>
+ <groupId>org.springframework</groupId>
+ <artifactId>org.springframework.oxm</artifactId>
+ </dependency>
+ <dependency>
+ <groupId>com.yourkit</groupId>
+ <artifactId>yjp-controller-api-redist</artifactId>
+ <!-- version>10.0.3</version> -->
+ <version>9.0.8</version>
+ </dependency>
+ <dependency>
+ <groupId>identificationKeyAPI</groupId>
+ <artifactId>identificationKeyAPI</artifactId>
+ </dependency>
</dependencies>
</project>
import java.io.Reader;\r
import java.io.UnsupportedEncodingException;\r
import java.io.Writer;\r
+import java.net.URI;\r
+import java.net.URISyntaxException;\r
\r
import javax.xml.bind.JAXBException;\r
import javax.xml.bind.Marshaller;\r
import javax.xml.parsers.ParserConfigurationException;\r
import javax.xml.parsers.SAXParser;\r
import javax.xml.parsers.SAXParserFactory;\r
+import javax.xml.transform.Source;\r
import javax.xml.transform.sax.SAXResult;\r
import javax.xml.transform.sax.SAXSource;\r
import javax.xml.transform.stream.StreamResult;\r
import javax.xml.validation.Schema;\r
+import javax.xml.validation.SchemaFactory;\r
\r
import org.apache.log4j.Logger;\r
import org.apache.xml.resolver.tools.CatalogResolver;\r
import org.springframework.oxm.UncategorizedMappingException;\r
import org.springframework.oxm.XmlMappingException;\r
import org.springframework.oxm.jaxb.Jaxb2Marshaller;\r
-import org.springframework.xml.validation.SchemaLoaderUtils;\r
import org.xml.sax.InputSource;\r
import org.xml.sax.SAXException;\r
import org.xml.sax.XMLReader;\r
+import org.xml.sax.helpers.XMLReaderFactory;\r
\r
import eu.etaxonomy.cdm.jaxb.CdmNamespacePrefixMapper;\r
import eu.etaxonomy.cdm.jaxb.FormattedText;\r
return CONTEXT_CLASSES;\r
}\r
\r
- public CdmDocumentBuilder() \r
- {\r
+ public CdmDocumentBuilder() {\r
schemas = new Resource[CDM_SCHEMA_FILES.length];\r
\r
for(int i = 0; i < CDM_SCHEMA_FILES.length; i++) {\r
}\r
\r
\r
-// protected void constructUnmarshaller() throws ParserConfigurationException, SAXException, JAXBException {\r
-// unmarshaller = jaxbContext.createUnmarshaller();\r
-// unmarshaller.setSchema(schema);\r
-// \r
-// SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();\r
-// \r
-// saxParserFactory.setNamespaceAware(true);\r
-// saxParserFactory.setXIncludeAware(true);\r
-// saxParserFactory.setValidating(true);\r
-// \r
-// SAXParser saxParser = saxParserFactory.newSAXParser();\r
-// saxParser.setProperty("http://java.sun.com/xml/jaxp/properties/schemaLanguage",\r
-// "http://www.w3.org/2001/XMLSchema");\r
-// xmlReader = saxParser.getXMLReader();\r
-// xmlReader.setEntityResolver(new CatalogResolver());\r
-// xmlReader.setErrorHandler(new DefaultErrorHandler());\r
-// unmarshaller.setEventHandler(new WarningTolerantValidationEventHandler());\r
-// }\r
-// \r
-//\r
-// \r
- \r
@Override\r
protected void initJaxbMarshaller(Marshaller marshaller) throws JAXBException {\r
try {\r
marshaller.setEventHandler(new WarningTolerantValidationEventHandler());\r
} catch(PropertyException pe) {\r
throw new JAXBException(pe.getMessage(),pe);\r
- }\r
+ }\r
} \r
\r
protected <T> T unmarshal(Class<T> clazz, InputSource input) {\r
SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();\r
Schema schema;\r
try {\r
- schema = SchemaLoaderUtils.loadSchema(schemas, "http://www.w3.org/2001/XMLSchema");\r
- \r
- \r
- saxParserFactory.setNamespaceAware(true);\r
- saxParserFactory.setXIncludeAware(true);\r
- saxParserFactory.setValidating(true);\r
- saxParserFactory.setSchema(schema);\r
- \r
- SAXParser saxParser = saxParserFactory.newSAXParser();\r
- XMLReader xmlReader = saxParser.getXMLReader();\r
- xmlReader.setEntityResolver(new CatalogResolver());\r
- xmlReader.setErrorHandler(new DefaultErrorHandler());\r
- SAXSource saxSource = new SAXSource( xmlReader, input);\r
- saxSource.setSystemId(input.getSystemId());\r
- \r
- return (T)super.unmarshal(saxSource);\r
+ schema = createSchema();\r
+ saxParserFactory.setNamespaceAware(true);\r
+ saxParserFactory.setXIncludeAware(true);\r
+ saxParserFactory.setValidating(true);\r
+ saxParserFactory.setSchema(schema);\r
+ \r
+ SAXParser saxParser = saxParserFactory.newSAXParser();\r
+ XMLReader xmlReader = saxParser.getXMLReader();\r
+ xmlReader.setEntityResolver(new CatalogResolver());\r
+ xmlReader.setErrorHandler(new DefaultErrorHandler());\r
+ SAXSource saxSource = new SAXSource( xmlReader, input);\r
+ saxSource.setSystemId(input.getSystemId());\r
+ \r
+ return (T)super.unmarshal(saxSource);\r
} catch (IOException e) {\r
throw new UncategorizedMappingException(e.getMessage(), e);\r
} catch (SAXException e) {\r
}\r
}\r
\r
+ private Schema createSchema() throws SAXException, IOException {\r
+ //method created to avoid dependency of spring-xml like in earlier versions\r
+ //old implementation was schema = SchemaLoaderUtils.loadSchema(schemas, "http://www.w3.org/2001/XMLSchema");\r
+ //maybe we can improve this loading in future\r
+ \r
+ String schemaLanguage = "http://www.w3.org/2001/XMLSchema";\r
+ Source[] schemaSources = new Source[schemas.length];\r
+ XMLReader reader = XMLReaderFactory.createXMLReader();\r
+ reader.setFeature("http://xml.org/sax/features/namespace-prefixes", true);\r
+ for (int i = 0; i < schemas.length; i++) {\r
+ schemaSources[i] = makeSchemaSource(reader, schemas[i]);// new ResourceSource(reader, schemas[i]);\r
+ }\r
+ SchemaFactory schemaFactory = SchemaFactory.newInstance(schemaLanguage);\r
+ return schemaFactory.newSchema(schemaSources);\r
+ }\r
+\r
+ private Source makeSchemaSource(XMLReader reader, Resource resource) throws IOException {\r
+ Source result = new SAXSource(reader, createInputSource(resource));\r
+ return result;\r
+ }\r
+ \r
+ private static InputSource createInputSource(Resource resource) throws IOException {\r
+ InputSource inputSource = new InputSource(resource.getInputStream());\r
+ inputSource.setSystemId(getSystemId(resource));\r
+ return inputSource;\r
+ }\r
+\r
+ /** Retrieves the URL from the given resource as System ID. Returns <code>null</code> if it cannot be opened. */\r
+ private static String getSystemId(Resource resource) {\r
+ try {\r
+ return new URI(resource.getURL().toExternalForm()).toString();\r
+ }\r
+ catch (IOException ex) {\r
+ logger.debug("Could not get System ID from [" + resource + "], ex");\r
+ return null;\r
+ }\r
+ catch (URISyntaxException e) {\r
+ logger.debug("Could not get System ID from [" + resource + "], ex");\r
+ return null;\r
+ }\r
+ }\r
+ \r
+\r
public <T> T unmarshal(Class<T> clazz,Reader reader) throws XmlMappingException {\r
InputSource source = new InputSource(reader);\r
return unmarshal(clazz,source);\r