001: package org.geotools.data.complex.config;
002:
003: import java.io.File;
004: import java.io.IOException;
005: import java.net.MalformedURLException;
006: import java.net.URI;
007: import java.net.URISyntaxException;
008: import java.util.Iterator;
009: import java.util.logging.Level;
010: import java.util.logging.Logger;
011:
012: import org.apache.xml.resolver.Catalog;
013: import org.eclipse.xsd.XSDSchema;
014: import org.eclipse.xsd.util.XSDSchemaLocationResolver;
015: import org.geotools.xml.BindingConfiguration;
016: import org.geotools.xml.Configuration;
017:
018: public class OasisCatalogConfigurationWrapper extends Configuration {
019:
020: private static final Logger LOGGER = org.geotools.util.logging.Logging
021: .getLogger(OasisCatalogConfigurationWrapper.class
022: .getPackage().getName());
023:
024: private Catalog catalog;
025:
026: private Configuration configuration;
027:
028: public OasisCatalogConfigurationWrapper(final Catalog catalog,
029: final Configuration configuration) {
030: this .catalog = catalog;
031: this .configuration = configuration;
032: addWrappedDepencencies();
033: }
034:
035: private void addWrappedDepencencies() {
036: Configuration dependency;
037: for (Iterator it = configuration.allDependencies().iterator(); it
038: .hasNext();) {
039: dependency = (Configuration) it.next();
040: addDependency(dependency);
041: }
042: }
043:
044: public BindingConfiguration getBindingConfiguration() {
045: return configuration.getBindingConfiguration();
046: }
047:
048: public String getNamespaceURI() {
049: return configuration.getNamespaceURI();
050: }
051:
052: public String getSchemaFileURL() {
053: String schemaFileURL = configuration.getSchemaFileURL();
054: try {
055: String resolvedLocation = catalog
056: .resolveSystem(schemaFileURL);
057: if (resolvedLocation != null) {
058: schemaFileURL = resolvedLocation;
059: }
060: } catch (MalformedURLException e) {
061: LOGGER.log(Level.SEVERE, "Illegal schema URL: "
062: + schemaFileURL, e);
063: throw new RuntimeException(e);
064: } catch (IOException e) {
065: LOGGER.log(Level.SEVERE, "Error reading Oasis Catalog: "
066: + e.getMessage(), e);
067: throw new RuntimeException(e);
068: }
069: return schemaFileURL;
070: }
071:
072: public XSDSchemaLocationResolver getSchemaLocationResolver() {
073: final XSDSchemaLocationResolver resolver = configuration
074: .getSchemaLocationResolver();
075: final XSDSchemaLocationResolver lastResortCatalogResolver;
076: lastResortCatalogResolver = new CatalogSchemaLocationResolverWrapper(
077: catalog, resolver);
078: return lastResortCatalogResolver;
079: }
080:
081: private static class CatalogSchemaLocationResolverWrapper implements
082: XSDSchemaLocationResolver {
083:
084: private Catalog catalog;
085:
086: private XSDSchemaLocationResolver resolver;
087:
088: public CatalogSchemaLocationResolverWrapper(
089: final Catalog catalog,
090: final XSDSchemaLocationResolver resolver) {
091: this .catalog = catalog;
092: this .resolver = resolver;
093: }
094:
095: /**
096: * @param schema
097: * the schema being resolved
098: * @param uri
099: * the namespace being resolved. If its an empty string (i.e.
100: * the location refers to an include, and thus the uri to the
101: * same one than the schema), the schema one is used.
102: * @param location
103: * the xsd location, either of <code>schema</code>, an
104: * import or an include, for which to try resolving it as a
105: * relative path of the <code>schema</code> location.
106: * @return
107: *
108: */
109: public String resolveSchemaLocation(final XSDSchema schema,
110: final String url, final String location) {
111: String schemaLocation = resolver.resolveSchemaLocation(
112: schema, url, location);
113: if (schemaLocation == null) {
114: try {
115: LOGGER.finest("resolving " + location);
116: schemaLocation = catalog.resolveSystem(location);
117: if (schemaLocation != null) {
118: LOGGER
119: .finer("Verifying existence of catalog resolved location "
120: + schemaLocation);
121: File f;
122: try {
123: f = new File(new URI(schemaLocation));
124: if (!f.exists()) {
125: LOGGER.info("Cannot locate "
126: + schemaLocation);
127: schemaLocation = null;
128: }
129: } catch (URISyntaxException e) {
130: schemaLocation = null;
131: LOGGER.log(Level.WARNING,
132: "Exception resolving "
133: + schemaLocation, e);
134: }
135: }
136: } catch (MalformedURLException e) {
137: e.printStackTrace();
138: } catch (IOException e) {
139: e.printStackTrace();
140: }
141: }
142: return schemaLocation;
143: }
144: }
145: }
|