|
From: <svn...@os...> - 2011-12-19 17:30:18
|
Author: jdeolive
Date: 2011-12-19 09:30:08 -0800 (Mon, 19 Dec 2011)
New Revision: 38434
Modified:
branches/2.7.x/modules/extension/xsd/xsd-core/src/main/java/org/geotools/xml/Configuration.java
branches/2.7.x/modules/extension/xsd/xsd-core/src/main/java/org/geotools/xml/Parser.java
branches/2.7.x/modules/extension/xsd/xsd-core/src/main/java/org/geotools/xml/Schemas.java
branches/2.7.x/modules/extension/xsd/xsd-core/src/main/java/org/geotools/xml/impl/ParserHandler.java
Log:
adding ability to specify URIHandler instances during parsing, GEOT-3987
Modified: branches/2.7.x/modules/extension/xsd/xsd-core/src/main/java/org/geotools/xml/Configuration.java
===================================================================
--- branches/2.7.x/modules/extension/xsd/xsd-core/src/main/java/org/geotools/xml/Configuration.java 2011-12-19 17:29:34 UTC (rev 38433)
+++ branches/2.7.x/modules/extension/xsd/xsd-core/src/main/java/org/geotools/xml/Configuration.java 2011-12-19 17:30:08 UTC (rev 38434)
@@ -32,6 +32,7 @@
import javax.xml.namespace.QName;
+import org.eclipse.emf.ecore.resource.URIHandler;
import org.eclipse.xsd.XSDSchema;
import org.eclipse.xsd.util.XSDSchemaLocationResolver;
import org.eclipse.xsd.util.XSDSchemaLocator;
Modified: branches/2.7.x/modules/extension/xsd/xsd-core/src/main/java/org/geotools/xml/Parser.java
===================================================================
--- branches/2.7.x/modules/extension/xsd/xsd-core/src/main/java/org/geotools/xml/Parser.java 2011-12-19 17:29:34 UTC (rev 38433)
+++ branches/2.7.x/modules/extension/xsd/xsd-core/src/main/java/org/geotools/xml/Parser.java 2011-12-19 17:30:08 UTC (rev 38434)
@@ -38,6 +38,7 @@
import javax.xml.transform.stream.StreamResult;
import org.apache.xerces.parsers.SAXParser;
+import org.eclipse.emf.ecore.resource.URIHandler;
import org.eclipse.xsd.XSDSchema;
import org.geotools.xml.impl.ParserHandler;
import org.geotools.xs.XS;
@@ -436,6 +437,18 @@
return handler.getNamespaceSupport();
}
+ /**
+ * Returns the list of {@link URIHandler} used when parsing schemas.
+ * <p>
+ * URI handlers are invoked to handle external references that occur during parsing.
+ * </p>
+ *
+ * @since 2.7
+ */
+ public List<URIHandler> getURIHandlers() {
+ return handler.getURIHandlers();
+ }
+
protected SAXParser parser() throws ParserConfigurationException, SAXException {
return parser( isValidating() );
}
Modified: branches/2.7.x/modules/extension/xsd/xsd-core/src/main/java/org/geotools/xml/Schemas.java
===================================================================
--- branches/2.7.x/modules/extension/xsd/xsd-core/src/main/java/org/geotools/xml/Schemas.java 2011-12-19 17:29:34 UTC (rev 38433)
+++ branches/2.7.x/modules/extension/xsd/xsd-core/src/main/java/org/geotools/xml/Schemas.java 2011-12-19 17:30:08 UTC (rev 38434)
@@ -18,6 +18,8 @@
import java.io.File;
import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
@@ -44,7 +46,11 @@
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.ResourceSet;
+import org.eclipse.emf.ecore.resource.URIConverter;
+import org.eclipse.emf.ecore.resource.URIHandler;
+import org.eclipse.emf.ecore.resource.impl.ExtensibleURIConverterImpl;
import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
+import org.eclipse.emf.ecore.resource.impl.URIHandlerImpl;
import org.eclipse.xsd.XSDAttributeDeclaration;
import org.eclipse.xsd.XSDAttributeGroupContent;
import org.eclipse.xsd.XSDAttributeGroupDefinition;
@@ -212,6 +218,7 @@
* @return The parsed schema, or null if the schema could not be parsed.
*
* @throws IOException In the event of a schema parsing error.
+ * @deprecated use {@link #parse(String, List, List)}
*/
public static final XSDSchema parse(String location, XSDSchemaLocator[] locators,
XSDSchemaLocationResolver[] resolvers) throws IOException {
@@ -219,9 +226,65 @@
(resolvers != null) ? Arrays.asList(resolvers) : Collections.EMPTY_LIST);
}
+ /**
+ * Parses a schema at the specified location.
+ *
+ * @param location A uri pointing to the location of the schema.
+ * @param locators A list of schema locator objects to be used when
+ * parsing imports/includes of the main schema.
+ * @param resolvers A list of schema location resolvers used to override
+ * schema locations encountered in an instance document or an imported
+ * schema.
+ *
+ * @return The parsed schema, or null if the schema could not be parsed.
+ *
+ * @throws IOException In the event of a schema parsing error.
+ */
public static final XSDSchema parse(String location, List locators, List resolvers)
throws IOException {
+ return parse(location, locators, resolvers, null);
+ }
+
+ /**
+ * Parses a schema at the specified location.
+ *
+ * @param location A uri pointing to the location of the schema.
+ * @param locators A list of schema locator objects to be used when
+ * parsing imports/includes of the main schema.
+ * @param resolvers A list of schema location resolvers used to override
+ * schema locations encountered in an instance document or an imported schema.
+ * @param uriHandlers A list of uri handlers to inject into the parsing chain, to handle
+ * externally referenced resources, like external schemas, etc...
+ *
+ * @return The parsed schema, or null if the schema could not be parsed.
+ *
+ * @throws IOException In the event of a schema parsing error.
+ */
+ public static final XSDSchema parse(String location, List locators, List resolvers,
+ List<URIHandler> uriHandlers) throws IOException {
+ ResourceSet resourceSet = new ResourceSetImpl();
+ //add the specialized schema location resolvers
+ if ((resolvers != null) && !resolvers.isEmpty()) {
+ AdapterFactory adapterFactory = new SchemaLocationResolverAdapterFactory(resolvers);
+ resourceSet.getAdapterFactories().add(adapterFactory);
+ }
+
+ //add the specialized schema locators as adapters
+ if ((locators != null) && !locators.isEmpty()) {
+ AdapterFactory adapterFactory = new SchemaLocatorAdapterFactory(locators);
+ resourceSet.getAdapterFactories().add(adapterFactory);
+ }
+
+ //add the specifialized uri handlers
+ if (uriHandlers != null && !uriHandlers.isEmpty()) {
+ resourceSet.getURIConverter().getURIHandlers().addAll(0, uriHandlers);
+ }
+ return parse(location, resourceSet);
+ }
+
+ public static final XSDSchema parse(String location, ResourceSet resourceSet)
+ throws IOException {
//check for case of file url, make sure it is an absolute reference
File locationFile = null;
try {
@@ -237,24 +300,11 @@
}
URI uri = URI.createURI(location);
- final ResourceSet resourceSet = new ResourceSetImpl();
- //add the specialized schema location resolvers
- if ((resolvers != null) && !resolvers.isEmpty()) {
- AdapterFactory adapterFactory = new SchemaLocationResolverAdapterFactory(resolvers);
- resourceSet.getAdapterFactories().add(adapterFactory);
- }
-
- //add the specialized schema locators as adapters
- if ((locators != null) && !locators.isEmpty()) {
- AdapterFactory adapterFactory = new SchemaLocatorAdapterFactory(locators);
- resourceSet.getAdapterFactories().add(adapterFactory);
- }
-
XSDResourceImpl xsdMainResource = (XSDResourceImpl) resourceSet.createResource(URI.createURI(
".xsd"));
xsdMainResource.setURI(uri);
-
+
// schema building has effects on referenced schemas, it will alter them -> we need
// to synchronize this call so that only one of these operations is active at any time
synchronized(Schemas.class) {
Modified: branches/2.7.x/modules/extension/xsd/xsd-core/src/main/java/org/geotools/xml/impl/ParserHandler.java
===================================================================
--- branches/2.7.x/modules/extension/xsd/xsd-core/src/main/java/org/geotools/xml/impl/ParserHandler.java 2011-12-19 17:29:34 UTC (rev 38433)
+++ branches/2.7.x/modules/extension/xsd/xsd-core/src/main/java/org/geotools/xml/impl/ParserHandler.java 2011-12-19 17:30:08 UTC (rev 38434)
@@ -17,6 +17,8 @@
package org.geotools.xml.impl;
import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
@@ -26,6 +28,7 @@
import javax.xml.namespace.QName;
+import org.eclipse.emf.ecore.resource.URIHandler;
import org.eclipse.xsd.XSDElementDeclaration;
import org.eclipse.xsd.XSDFactory;
import org.eclipse.xsd.XSDImport;
@@ -114,6 +117,9 @@
/** whether the parser should maintain order for elements with mixed content */
boolean handleMixedContent = false;
+ /** uri handlers for handling uri references during parsing */
+ List<URIHandler> uriHandlers = new ArrayList<URIHandler>();
+
public ParserHandler(Configuration config) {
this.config = config;
namespaces = new NamespaceSupport();
@@ -197,6 +203,10 @@
return namespaces;
}
+ public List<URIHandler> getURIHandlers() {
+ return uriHandlers;
+ }
+
public void startPrefixMapping(String prefix, String uri)
throws SAXException {
namespaces.declarePrefix(prefix, uri);
@@ -277,8 +287,8 @@
}
//look up schema overrides
- XSDSchemaLocator[] locators = findSchemaLocators();
- XSDSchemaLocationResolver[] resolvers = findSchemaLocationResolvers();
+ List<XSDSchemaLocator> locators = Arrays.asList(findSchemaLocators());
+ List<XSDSchemaLocationResolver> resolvers = Arrays.asList(findSchemaLocationResolvers());
if ((locations != null) && (locations.length > 0)) {
//parse each namespace location pair into schema objects
@@ -297,8 +307,8 @@
}
//first check for a location override
- for (int j = 0; j < resolvers.length; j++) {
- String override = resolvers[j].resolveSchemaLocation(null, namespace,
+ for (int j = 0; j < resolvers.size(); j++) {
+ String override = resolvers.get(j).resolveSchemaLocation(null, namespace,
location);
if (override != null) {
//ensure that override has no spaces
@@ -312,8 +322,8 @@
}
//next check for schema override
- for (int j = 0; j < locators.length; j++) {
- XSDSchema schema = locators[j].locateSchema(null, namespace, location, null);
+ for (int j = 0; j < locators.size(); j++) {
+ XSDSchema schema = locators.get(j).locateSchema(null, namespace, location, null);
if (schema != null) {
schemas[i / 2] = schema;
@@ -336,7 +346,7 @@
//parse the document
try {
- schemas[i / 2] = Schemas.parse(location, locators, resolvers);
+ schemas[i / 2] = Schemas.parse(location, locators, resolvers, uriHandlers);
} catch (Exception e) {
String msg = "Error parsing: " + location;
logger.warning(msg);
@@ -351,8 +361,8 @@
} else {
//could not find a schemaLocation attribute, use the locators
//look for schema with locators
- for (int i = 0; i < locators.length; i++) {
- XSDSchema schema = locators[i].locateSchema(null, uri, null, null);
+ for (int i = 0; i < locators.size(); i++) {
+ XSDSchema schema = locators.get(i).locateSchema(null, uri, null, null);
if (schema != null) {
schemas = new XSDSchema[] { schema };
|