001: package org.geotools.data.gml;
002:
003: import java.io.IOException;
004: import java.util.Map;
005:
006: import org.geotools.data.DataStore;
007: import org.geotools.data.DataStoreFactorySpi;
008: import org.geotools.gml3.ApplicationSchemaConfiguration;
009: import org.geotools.xml.Configuration;
010:
011: /**
012: * Datastore factory for gml datastore.
013: * <p>
014: * <br>
015: * <h2>Usage</h2>
016: * <br>
017: * <br>
018: * When creating an instance of the datastore, the {@link #LOCATION} parameter must
019: * be specified.
020: * <pre>
021: * <code>
022: * GMLDataStoreFactory factory = new GMLDataStoreFactory();
023: *
024: * Map params = new HashMap();
025: * params.put( GMLDataStoreFactory.LOCATION, "instanceDocument.xml" );
026: *
027: * GMLDataStore dataStore = factory.createDataStore( params );
028: * </code>
029: * </pre>
030: * Creating a datastore with only the location parameter causes the datastore to deduce
031: * all the information about the schema from the instance document directly. Which means
032: * the schemaLocation must be properly specifed in the instance document. As this is not
033: * always the case, there are ways to specify information about the schema to the datastore.
034: * <br>
035: * <ol>
036: * <li>Specifying the targetNamespace and schemaLocation directly.
037: * <li>Specifying a {@link org.geotools.xml.Configuration}
038: * </ol>
039: * </p>
040: * <br>
041: * <p>
042: * <h3>Target namespace and Schema Location</h3>
043: * <pre>
044: * <code>
045: * GMLDataStoreFactory factory = new GMLDataStoreFactory();
046: *
047: * Map params = new HashMap();
048: * params.put( GMLDataStoreFactory.LOCATION, "instanceDocument.xml" );
049: * params.put( GMLDataStoreFactory.NAMESPACE, "http://myApplicationSchemaNamespaceUri" );
050: * params.put( GMLDataStoreFactory.SCHEMALOCATION, "/path/to/applicationSchema.xsd" );
051: *
052: * GMLDataStore dataStore = factory.createDataStore( params );
053: * </code>
054: * </pre>
055: * </p>
056: * <p>
057: * <h3>Configuration</h3>
058: * <br>
059: * A {@link org.geotools.xml.Configuration} is used to specify information about a schema and
060: * configure the xml parser to parse instances of the schema. The
061: * {@link org.geotools.gml3.ApplicationSchemaConfiguration} is a subclass that can be extented
062: * in order to create a configuration specific to an application schema:
063: * <pre>
064: * <code>
065: * class MyApplictionSchemaConfiguration extends ApplicationSchemaConfiguration {
066: *
067: * public MyApplicationSchema() {
068: * super( "http://myApplicationSchemaNamespaceUri", "/path/to/applicationSchema.xsd" );
069: * }
070: *
071: * }
072: * </pre>
073: * </code>
074: * The class of the application schema can then be supplied with the {@link #CONFIGURATION}
075: * paramter:
076: * <pre>
077: * <code>
078: * GMLDataStoreFactory factory = new GMLDataStoreFactory();
079: *
080: * Map params = new HashMap();
081: * params.put( GMLDataStoreFactory.LOCATION, "instanceDocument.xml" );
082: * params.put( GMLDataStoreFactory.CONFIGURATION, MyApplicationSchemaConfiguration.class );
083: *
084: * GMLDataStore dataStore = factory.createDataStore( params );
085: * </code>
086: * </pre>
087: * It is important to note that when using the CONFIGURATION parameter the configuration class
088: * must have a no-argument constructor.
089: * </p>
090: * <br>
091: * <p>
092: * TODO: document support for application schema defined types
093: *
094: * </p>
095: * @author Justin Deoliveira, The Open Planning Project
096: *
097: */
098: public class GMLDataStoreFactory implements DataStoreFactorySpi {
099:
100: /**
101: * The location of the instance document.
102: */
103: public static Param LOCATION = new Param("location", String.class,
104: "Instance document location", true);
105:
106: /**
107: * The application schema configuration
108: */
109: public static Param CONFIGURATION = new Param("configuration",
110: Class.class, "Application schema configuration", false);
111:
112: /**
113: * Application schema namespace
114: */
115: public static Param NAMESPACE = new Param("namespace",
116: String.class, "Application schema namespace", false);
117: /**
118: * Location of application schema
119: */
120: public static Param SCHEMALOCATION = new Param("schemaLocation",
121: String.class, "Application schema location", false);
122:
123: public DataStore createDataStore(Map params) throws IOException {
124: String location = (String) LOCATION.lookUp(params);
125:
126: if (location != null) {
127: Class configuration = (Class) CONFIGURATION.lookUp(params);
128: if (configuration != null) {
129: try {
130: return new GMLDataStore(location,
131: (Configuration) configuration.newInstance());
132: } catch (Exception e) {
133: throw (IOException) new IOException().initCause(e);
134: }
135: } else {
136: String namespace = (String) NAMESPACE.lookUp(params);
137: String schemaLocation = (String) SCHEMALOCATION
138: .lookUp(params);
139:
140: if (namespace != null && schemaLocation != null) {
141: return new GMLDataStore(location,
142: new ApplicationSchemaConfiguration(
143: namespace, schemaLocation));
144: }
145: }
146:
147: return new GMLDataStore(location);
148: }
149:
150: return null;
151: }
152:
153: public DataStore createNewDataStore(Map params) throws IOException {
154: throw new UnsupportedOperationException();
155: }
156:
157: public String getDisplayName() {
158: return "GML";
159: }
160:
161: public String getDescription() {
162: return "Geographic Markup Language";
163: }
164:
165: public Param[] getParametersInfo() {
166: return new Param[] { LOCATION, CONFIGURATION, NAMESPACE,
167: SCHEMALOCATION };
168: }
169:
170: public boolean canProcess(Map params) {
171: return params.containsKey(LOCATION.key);
172: }
173:
174: public boolean isAvailable() {
175: return true;
176: }
177:
178: public Map getImplementationHints() {
179: return null;
180: }
181:
182: }
|