001: /*
002: * Geotools2 - OpenSource mapping toolkit
003: * http://geotools.org
004: * (C) 2005-2006, GeoTools Project Managment Committee (PMC)
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation;
009: * version 2.1 of the License.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: */
017: package org.geotools.data.complex.config;
018:
019: import java.io.IOException;
020: import java.io.InputStream;
021: import java.net.URL;
022: import java.util.ArrayList;
023: import java.util.HashMap;
024: import java.util.HashSet;
025: import java.util.logging.Level;
026: import java.util.logging.Logger;
027:
028: import org.apache.commons.digester.Digester;
029: import org.xml.sax.SAXException;
030:
031: /**
032: * DOCUMENT ME!
033: *
034: * @author Gabriel Roldan, Axios Engineering
035: * @version $Id: XMLConfigDigester.java 27862 2007-11-12 19:51:19Z desruisseaux $
036: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/unsupported/community-schemas/community-schema-ds/src/main/java/org/geotools/data/complex/config/XMLConfigDigester.java $
037: * @since 2.4
038: */
039: public class XMLConfigDigester {
040: /** DOCUMENT ME! */
041: private static final Logger LOGGER = org.geotools.util.logging.Logging
042: .getLogger(XMLConfigDigester.class.getPackage().getName());
043:
044: /** Namespace URI for the ComplexDataStore configuration files */
045: private static final String CONFIG_NS_URI = "http://www.geotools.org/complex";
046:
047: /**
048: * Creates a new XMLConfigReader object.
049: */
050: public XMLConfigDigester() {
051: super ();
052: }
053:
054: /**
055: * Parses a complex datastore configuration file in
056: * xml format into a {@link ComplexDataStoreDTO}
057: *
058: * @param dataStoreConfigUrl config file location
059: *
060: * @return a DTO object representing the datastore's
061: * configuration
062: *
063: * @throws IOException if an error occurs parsing the file
064: */
065: public ComplexDataStoreDTO parse(URL dataStoreConfigUrl)
066: throws IOException {
067: ComplexDataStoreDTO config = digest(dataStoreConfigUrl);
068: return config;
069: }
070:
071: /**
072: * DOCUMENT ME!
073: *
074: * @param dataStoreConfigUrl DOCUMENT ME!
075: *
076: * @return DOCUMENT ME!
077: *
078: * @throws IOException DOCUMENT ME!
079: * @throws NullPointerException DOCUMENT ME!
080: */
081: private ComplexDataStoreDTO digest(final URL dataStoreConfigUrl)
082: throws IOException {
083: if (dataStoreConfigUrl == null) {
084: throw new NullPointerException("datastore config url");
085: }
086:
087: InputStream configStream = dataStoreConfigUrl.openStream();
088:
089: if (configStream == null) {
090: throw new IOException("Can't open datastore config file "
091: + dataStoreConfigUrl);
092: }
093:
094: XMLConfigDigester.LOGGER
095: .fine("parsing complex datastore config: "
096: + dataStoreConfigUrl.toExternalForm());
097:
098: Digester digester = new Digester();
099: XMLConfigDigester.LOGGER.fine("digester created");
100: //URL schema = getClass()
101: // .getResource("../test-data/ComplexDataStore.xsd");
102: //digester.setSchema(schema.toExternalForm());
103: digester.setValidating(false);
104: digester.setNamespaceAware(true);
105: digester.setRuleNamespaceURI(XMLConfigDigester.CONFIG_NS_URI);
106:
107: // digester.setRuleNamespaceURI(OGC_NS_URI);
108: ComplexDataStoreDTO configDto = new ComplexDataStoreDTO();
109: configDto
110: .setBaseSchemasUrl(dataStoreConfigUrl.toExternalForm());
111:
112: digester.push(configDto);
113:
114: try {
115: setNamespacesRules(digester);
116:
117: setSourceDataStoresRules(digester);
118:
119: setTargetSchemaUriRules(digester);
120:
121: setTypeMappingsRules(digester);
122:
123: } catch (Exception e) {
124: e.printStackTrace();
125: XMLConfigDigester.LOGGER.log(Level.SEVERE,
126: "setting digester properties: ", e);
127: throw new IOException("Error setting digester properties: "
128: + e.getMessage());
129: }
130:
131: try {
132: digester.parse(configStream);
133: } catch (SAXException e) {
134: e.printStackTrace();
135: XMLConfigDigester.LOGGER.log(Level.SEVERE, "parsing "
136: + dataStoreConfigUrl, e);
137:
138: IOException ioe = new IOException(
139: "Can't parse complex datastore config. ");
140: ioe.initCause(e);
141: throw ioe;
142: }
143:
144: ComplexDataStoreDTO config = (ComplexDataStoreDTO) digester
145: .getRoot();
146:
147: return config;
148: }
149:
150: private void setTypeMappingsRules(Digester digester) {
151: final String mappings = "ComplexDataStore/typeMappings";
152: digester.addObjectCreate(mappings,
153: XMLConfigDigester.CONFIG_NS_URI, HashSet.class);
154:
155: final String typeMapping = mappings + "/FeatureTypeMapping";
156:
157: digester.addObjectCreate(typeMapping,
158: XMLConfigDigester.CONFIG_NS_URI, TypeMapping.class);
159: digester.addCallMethod(typeMapping + "/sourceDataStore",
160: "setSourceDataStore", 1);
161: digester.addCallParam(typeMapping + "/sourceDataStore", 0);
162: digester.addCallMethod(typeMapping + "/sourceType",
163: "setSourceTypeName", 1);
164: digester.addCallParam(typeMapping + "/sourceType", 0);
165: digester.addCallMethod(typeMapping + "/targetElement",
166: "setTargetElementName", 1);
167: digester.addCallParam(typeMapping + "/targetElement", 0);
168:
169: //add grouping source attribute names
170: final String groupBy = typeMapping + "/groupBy";
171: digester.addObjectCreate(groupBy,
172: XMLConfigDigester.CONFIG_NS_URI, ArrayList.class);
173: digester.addCallMethod(groupBy + "/GroupByAttribute", "add", 1);
174: digester.addCallParam(groupBy + "/GroupByAttribute", 0);
175: digester.addSetNext(groupBy, "setGroupbyAttributeNames",
176: "java.util.List");
177:
178: //create attribute mappings
179: final String attMappings = typeMapping + "/attributeMappings";
180: digester.addObjectCreate(attMappings,
181: XMLConfigDigester.CONFIG_NS_URI, ArrayList.class);
182:
183: final String attMap = attMappings + "/AttributeMapping";
184: digester
185: .addObjectCreate(attMap,
186: XMLConfigDigester.CONFIG_NS_URI,
187: AttributeMapping.class);
188:
189: digester
190: .addCallMethod(attMap + "/isMultiple", "setMultiple", 1);
191: digester.addCallParam(attMap + "/isMultiple", 0);
192:
193: digester.addCallMethod(attMap + "/targetAttribute",
194: "setTargetAttributePath", 1);
195: digester.addCallParam(attMap + "/targetAttribute", 0);
196:
197: digester.addCallMethod(attMap + "/targetAttributeNode",
198: "setTargetAttributeSchemaElement", 1);
199: digester.addCallParam(attMap + "/targetAttributeNode", 0);
200:
201: digester.addCallMethod(attMap + "/idExpression/OCQL",
202: "setIdentifierExpression", 1);
203: digester.addCallParam(attMap + "/idExpression/OCQL", 0);
204:
205: digester.addCallMethod(attMap + "/sourceExpression/OCQL",
206: "setSourceExpression", 1);
207: digester.addCallParam(attMap + "/sourceExpression/OCQL", 0);
208:
209: digester.addCallMethod(attMap + "/ClientProperty",
210: "putClientProperty", 2);
211: digester.addCallParam(attMap + "/ClientProperty/name", 0);
212: digester.addCallParam(attMap + "/ClientProperty/value", 1);
213:
214: //add the AttributeMapping to the list
215: digester.addSetNext(attMap, "add");
216:
217: //set attribute mappings
218: digester.addSetNext(attMappings, "setAttributeMappings");
219:
220: //add the TypeMapping to the Set
221: digester.addSetNext(typeMapping, "add");
222:
223: //set the TypeMapping on ComplexDataStoreDTO
224: digester.addSetNext(mappings, "setTypeMappings");
225: }
226:
227: private void setTargetSchemaUriRules(Digester digester) {
228: final String targetSchemas = "ComplexDataStore/targetTypes";
229:
230: digester.addBeanPropertySetter("ComplexDataStore/catalog");
231: // digester.addCallMethod(targetSchemas + "/Catalog", "setCatalog", 1);
232: // digester.addCallParam(targetSchemas + "/Catalog", 0);
233:
234: digester.addObjectCreate(targetSchemas,
235: XMLConfigDigester.CONFIG_NS_URI, ArrayList.class);
236:
237: final String schema = targetSchemas + "/FeatureType/schemaUri";
238: digester.addCallMethod(schema, "add", 1);
239: digester.addCallParam(schema, 0);
240: //set the list of XSD file uris on ComplexDataStoreDTO
241: digester.addSetNext(targetSchemas, "setTargetSchemasUris");
242: }
243:
244: private void setSourceDataStoresRules(Digester digester) {
245: final String dataStores = "ComplexDataStore/sourceDataStores";
246: digester.addObjectCreate(dataStores,
247: XMLConfigDigester.CONFIG_NS_URI, ArrayList.class);
248:
249: //create a SourceDataStore for each DataStore tag
250: digester.addObjectCreate(dataStores + "/DataStore",
251: XMLConfigDigester.CONFIG_NS_URI, SourceDataStore.class);
252: digester
253: .addCallMethod(dataStores + "/DataStore/id", "setId", 1);
254: digester.addCallParam(dataStores + "/DataStore/id", 0);
255:
256: digester.addObjectCreate(dataStores + "/DataStore/parameters",
257: XMLConfigDigester.CONFIG_NS_URI, HashMap.class);
258: digester.addCallMethod(dataStores
259: + "/DataStore/parameters/Parameter", "put", 2);
260: digester.addCallParam(dataStores
261: + "/DataStore/parameters/Parameter/name", 0);
262: digester.addCallParam(dataStores
263: + "/DataStore/parameters/Parameter/value", 1);
264: digester.addSetNext(dataStores + "/DataStore/parameters",
265: "setParams");
266:
267: //add the SourceDataStore to the list
268: digester.addSetNext(dataStores + "/DataStore", "add");
269:
270: //set the list of SourceDataStores for ComlexDataStoreDTO
271: digester.addSetNext(dataStores, "setSourceDataStores");
272: }
273:
274: private void setNamespacesRules(Digester digester) {
275: final String ns = "ComplexDataStore/namespaces";
276: digester.addObjectCreate(ns, XMLConfigDigester.CONFIG_NS_URI,
277: HashMap.class);
278: digester.addCallMethod(ns + "/Namespace", "put", 2);
279: digester.addCallParam(ns + "/Namespace/prefix", 0);
280: digester.addCallParam(ns + "/Namespace/uri", 1);
281: digester.addSetNext(ns, "setNamespaces");
282: }
283: }
|