01: package org.geotools.xml;
02:
03: import java.io.File;
04: import java.net.URL;
05:
06: import org.eclipse.xsd.XSDSchema;
07: import org.eclipse.xsd.util.XSDSchemaLocationResolver;
08:
09: /**
10: * Resolves a physical schema location from a namespace uri.
11: * <p>
12: * This class works from a {@link org.geotools.xml.Configuration} which defines information about
13: * the schema.
14: * </p>
15: * <p>
16: * Example usage:
17: *
18: * <code>
19: * <pre>
20: * Configuration myConfig = ...
21: * String namespaceURI = myConfig.getNamesapceURI();
22: *
23: * SchemaLocationResolver resolver = new SchemaLocationResolver( myConfig );
24: * String schemaLocation = locator.resolveSchemaLocation( null, namespaceURI, "mySchema.xsd" );
25: * </pre>
26: * </code>
27: *
28: * </p>
29: * @author Justin Deoliveira, The Open Planning Project
30: *
31: */
32: public class SchemaLocationResolver implements
33: XSDSchemaLocationResolver {
34:
35: /**
36: * The Configuration
37: */
38: Configuration configuration;
39:
40: /**
41: * Creates the new schema location resolver.
42: *
43: * @param configuration The schema configuration
44: */
45: public SchemaLocationResolver(Configuration configuration) {
46: this .configuration = configuration;
47: }
48:
49: /**
50: * Resolves <param>location<param> to a physical location.
51: * <p>
52: * Resolution is performed by stripping the filename off of <param>location</param>
53: * and looking up a resource located in the same package as the class of the configuration.
54: * </p>
55: */
56: public String resolveSchemaLocation(XSDSchema schema, String uri,
57: String location) {
58:
59: if (location == null) {
60: return null;
61: }
62:
63: //if no namespace given, assume default for the current schema
64: if (((uri == null) || "".equals(uri)) && (schema != null)) {
65: uri = schema.getTargetNamespace();
66: }
67:
68: //namespace match?
69: if (configuration.getNamespaceURI().equals(uri)) {
70: //strip off the filename and do a resource lookup
71: String fileName = new File(location).getName();
72: URL xsd = configuration.getClass().getResource(fileName);
73: if (xsd != null) {
74: return xsd.toString();
75: }
76: }
77:
78: return null;
79: }
80:
81: }
|