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.w3c.dom.Document;
008: import org.w3c.dom.Element;
009: import java.io.File;
010: import java.io.IOException;
011: import java.util.Iterator;
012: import java.util.Map;
013: import javax.xml.parsers.DocumentBuilderFactory;
014: import javax.xml.transform.OutputKeys;
015: import javax.xml.transform.Transformer;
016: import javax.xml.transform.TransformerFactory;
017: import javax.xml.transform.dom.DOMSource;
018: import javax.xml.transform.stream.StreamResult;
019:
020: /**
021: * Writes the GeoServer catalog.xml file.
022: * <p>
023: * Usage:
024: *
025: * <pre>
026: * <code>
027: *
028: * Map dataStores = ...
029: * Map nameSpaces = ...
030: *
031: * CatalogWriter writer = new CatalogWriter();
032: * writer.dataStores( dataStores );
033: * writer.nameSpaces( nameSpaces );
034: *
035: * File catalog = new File( ".../catalog.xml" );
036: * writer.write( catalog );
037: *
038: *
039: * </code>
040: * </pre>
041: *
042: * </p>
043: *
044: * @author Justin Deoliveira, The Open Planning Project, jdeolive@openplans.org
045: *
046: */
047: public class CatalogWriter {
048: /**
049: * The xml document
050: */
051: Document document;
052:
053: /**
054: * Root catalog element.
055: */
056: Element catalog;
057:
058: public CatalogWriter() {
059: try {
060: DocumentBuilderFactory builderFactory = DocumentBuilderFactory
061: .newInstance();
062: builderFactory.setNamespaceAware(false);
063: builderFactory.setValidating(false);
064:
065: document = builderFactory.newDocumentBuilder()
066: .newDocument();
067: catalog = document.createElement("catalog");
068: document.appendChild(catalog);
069: } catch (Exception e) {
070: throw new RuntimeException(e);
071: }
072: }
073:
074: /**
075: * Writes "datastore" elements to the catalog.xml file.
076: *
077: * @param dataStores map of id to connection parameter map
078: * @param namespaces map of id to namespace prefix map
079: *
080: *
081: */
082: public void dataStores(Map /* <String,Map> */dataStores,
083: Map /*<String,String>*/namespaces) {
084: Element dataStoresElement = document
085: .createElement("datastores");
086: catalog.appendChild(dataStoresElement);
087:
088: for (Iterator d = dataStores.entrySet().iterator(); d.hasNext();) {
089: Map.Entry dataStore = (Map.Entry) d.next();
090: String id = (String) dataStore.getKey();
091: Map params = (Map) dataStore.getValue();
092:
093: Element dataStoreElement = document
094: .createElement("datastore");
095: dataStoresElement.appendChild(dataStoreElement);
096:
097: // set the datastore id
098: dataStoreElement.setAttribute("id", id);
099:
100: //set the namespace
101: dataStoreElement.setAttribute("namespace",
102: (String) namespaces.get(id));
103:
104: // encode hte ocnnection paramters
105: Element connectionParamtersElement = document
106: .createElement("connectionParams");
107: dataStoreElement.appendChild(connectionParamtersElement);
108:
109: for (Iterator p = params.entrySet().iterator(); p.hasNext();) {
110: Map.Entry param = (Map.Entry) p.next();
111: String name = (String) param.getKey();
112: Object value = param.getValue();
113:
114: // skip null values
115: if (value == null) {
116: continue;
117: }
118:
119: Element parameterElement = document
120: .createElement("parameter");
121: connectionParamtersElement
122: .appendChild(parameterElement);
123:
124: parameterElement.setAttribute("name", name);
125: parameterElement
126: .setAttribute("value", value.toString());
127: }
128: }
129: }
130:
131: /**
132: * Writes "namespace" elements to the catalog.xml file.
133: *
134: * @param namespaces
135: * map of <prefix,uri>, default uri is located under the empty
136: * string key.
137: *
138: */
139: public void namespaces(Map namespaces) {
140: Element namespacesElement = document
141: .createElement("namespaces");
142: catalog.appendChild(namespacesElement);
143:
144: for (Iterator n = namespaces.entrySet().iterator(); n.hasNext();) {
145: Map.Entry namespace = (Map.Entry) n.next();
146: String prefix = (String) namespace.getKey();
147: String uri = (String) namespace.getValue();
148:
149: // dont write out default prefix
150: if ("".equals(prefix)) {
151: continue;
152: }
153:
154: Element namespaceElement = document
155: .createElement("namespace");
156: namespacesElement.appendChild(namespaceElement);
157:
158: namespaceElement.setAttribute("uri", uri);
159: namespaceElement.setAttribute("prefix", prefix);
160:
161: // check for default
162: if (uri.equals(namespaces.get(""))) {
163: namespaceElement.setAttribute("default", "true");
164: }
165: }
166: }
167:
168: /**
169: * Writes "style" elements to the catalog.xml file.
170: *
171: * @param styles
172: * map of <id,filename>
173: *
174: */
175: public void styles(Map styles) {
176: Element stylesElement = document.createElement("styles");
177: catalog.appendChild(stylesElement);
178:
179: for (Iterator s = styles.entrySet().iterator(); s.hasNext();) {
180: Map.Entry style = (Map.Entry) s.next();
181: String id = (String) style.getKey();
182: String filename = (String) style.getValue();
183:
184: Element styleElement = document.createElement("style");
185: stylesElement.appendChild(styleElement);
186:
187: styleElement.setAttribute("id", id);
188: styleElement.setAttribute("filename", filename);
189: }
190: }
191:
192: /**
193: * WRites the catalog.xml file.
194: * <p>
195: * This method *must* be called after any other methods.
196: * </p>
197: *
198: * @param file
199: * The catalog.xml file.
200: *
201: * @throws IOException
202: * In event of a writing error.
203: */
204: public void write(File file) throws IOException {
205: try {
206: Transformer tx = TransformerFactory.newInstance()
207: .newTransformer();
208: tx.setOutputProperty(OutputKeys.INDENT, "yes");
209: DOMSource source = new DOMSource(document);
210: StreamResult result = new StreamResult(file);
211:
212: tx.transform(source, result);
213: } catch (Exception e) {
214: String msg = "Could not write catalog to " + file;
215: throw (IOException) new IOException(msg).initCause(e);
216: }
217: }
218: }
|