001: /* Copyright (c) 2001 - 2007 TOPP - www.openplans.org. All rights reserved.
002: * This code is licensed under the GPL 2.0 license, availible at the root
003: * application directory.
004: */
005: package org.geoserver.data;
006:
007: import org.geoserver.util.ReaderUtils;
008: import org.w3c.dom.Element;
009: import org.w3c.dom.NodeList;
010: import java.io.File;
011: import java.io.FileReader;
012: import java.io.IOException;
013: import java.util.ArrayList;
014: import java.util.HashMap;
015: import java.util.List;
016: import java.util.Map;
017:
018: /**
019: * Reads the GeoServer catalog.xml file.
020: * <p>
021: * Usage:
022: *
023: * <pre>
024: * <code>
025: * File catalog = new File( ".../catalog.xml" );
026: * CatalogReader reader = new CatalogReader();
027: * reader.read( catalog );
028: * List dataStores = reader.dataStores();
029: * LIst nameSpaces = reader.nameSpaces();
030: * </code>
031: * </pre>
032: * </p>
033: *
034: * @author Justin Deoliveira, The Open Planning Project, jdeolive@openplans.org
035: *
036: */
037: public class CatalogReader {
038: /**
039: * Root catalog element.
040: */
041: Element catalog;
042:
043: /**
044: * Parses the catalog.xml file into a DOM.
045: * <p>
046: * This method *must* be called before any other methods.
047: * </p>
048: *
049: * @param file The catalog.xml file.
050: *
051: * @throws IOException In event of a parser error.
052: */
053: public void read(File file) throws IOException {
054: FileReader reader = new FileReader(file);
055:
056: try {
057: catalog = ReaderUtils.parse(reader);
058: } finally {
059: reader.close();
060: }
061: }
062:
063: /**
064: * Reads "datastore" elements from the catalog.xml file.
065: * <p>
066: * For each datastore element read, a map of the connection parameters is
067: * created.
068: * </p>
069: *
070: * @return A list of Map objects containg the datastore connection parameters.
071: *
072: * @throws Exception If error processing "datastores" element.
073: */
074: public List /*<Map>*/dataStores() throws Exception {
075: Element dataStoresElement = ReaderUtils.getChildElement(
076: catalog, "datastores", true);
077:
078: NodeList dataStoreElements = dataStoresElement
079: .getElementsByTagName("datastore");
080: ArrayList dataStores = new ArrayList();
081:
082: for (int i = 0; i < dataStoreElements.getLength(); i++) {
083: Element dataStoreElement = (Element) dataStoreElements
084: .item(i);
085:
086: try {
087: Map params = dataStoreParams(dataStoreElement);
088: dataStores.add(params);
089: } catch (Exception e) {
090: //TODO: log this
091: continue;
092: }
093: }
094:
095: return dataStores;
096: }
097:
098: /**
099: * Reads "namespace" elements from the catalog.xml file.
100: * <p>
101: * For each namespace element read, an entry of <prefix,uri> is created
102: * in a map. The default uri is located under the empty string key.
103: * </p>
104: *
105: * @return A map containing <prefix,uri> tuples.
106: *
107: * @throws Exception If error processing "namespaces" element.
108: */
109: public Map namespaces() throws Exception {
110: Element namespacesElement = ReaderUtils.getChildElement(
111: catalog, "namespaces", true);
112:
113: NodeList namespaceElements = namespacesElement
114: .getElementsByTagName("namespace");
115: Map namespaces = new HashMap();
116:
117: for (int i = 0; i < namespaceElements.getLength(); i++) {
118: Element namespaceElement = (Element) namespaceElements
119: .item(i);
120:
121: try {
122: Map.Entry tuple = namespaceTuple(namespaceElement);
123: namespaces.put(tuple.getKey(), tuple.getValue());
124:
125: //check for default
126: if ("true".equals(namespaceElement
127: .getAttribute("default"))) {
128: namespaces.put("", tuple.getValue());
129: }
130: } catch (Exception e) {
131: //TODO: log this
132: continue;
133: }
134: }
135:
136: return namespaces;
137: }
138:
139: /**
140: * Convenience method for reading connection parameters from a datastore
141: * element.
142: *
143: * @param dataStoreElement The "datastore" element.
144: *
145: * @return The map of connection paramters.
146: *
147: * @throws Exception If problem parsing any parameters.
148: */
149: protected Map dataStoreParams(Element dataStoreElement)
150: throws Exception {
151: Element paramsElement = ReaderUtils.getChildElement(
152: dataStoreElement, "connectionParameters", true);
153: NodeList paramList = paramsElement
154: .getElementsByTagName("parameter");
155:
156: Map params = new HashMap();
157:
158: for (int i = 0; i < paramList.getLength(); i++) {
159: Element paramElement = (Element) paramList.item(i);
160: String key = ReaderUtils.getAttribute(paramElement, "name",
161: true);
162: String value = ReaderUtils.getAttribute(paramElement,
163: "value", true);
164:
165: params.put(key, value);
166: }
167:
168: return params;
169: }
170:
171: /**
172: * Convenience method for reading namespace prefix and uri from a namespace
173: * element.
174: *
175: * @param namespaceElement The "namespace" element.
176: *
177: * @return A <prefix,uri> tuple.
178: *
179: * @throws Exception If problem parsing any parameters.
180: */
181: protected Map.Entry namespaceTuple(Element namespaceElement)
182: throws Exception {
183: final String pre = namespaceElement.getAttribute("prefix");
184: final String uri = namespaceElement.getAttribute("uri");
185:
186: return new Map.Entry() {
187: public Object getKey() {
188: return pre;
189: }
190:
191: public Object getValue() {
192: return uri;
193: }
194:
195: public Object setValue(Object value) {
196: throw new UnsupportedOperationException();
197: }
198: };
199: }
200: }
|