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.wfs.response;
006:
007: import org.geoserver.ows.util.ResponseUtils;
008: import org.geoserver.ows.xml.v1_0.OWS;
009: import org.geoserver.wfs.WFS;
010: import org.geotools.filter.v1_0.OGC;
011: import org.geotools.gml2.bindings.GML;
012: import org.geotools.xs.bindings.XS;
013: import org.xml.sax.helpers.NamespaceSupport;
014: import java.io.BufferedWriter;
015: import java.io.IOException;
016: import java.io.OutputStream;
017: import java.io.OutputStreamWriter;
018: import java.util.Enumeration;
019: import java.util.HashMap;
020: import java.util.Iterator;
021: import java.util.Map;
022: import java.util.Map.Entry;
023:
024: /**
025: * Utility class for writing out wfs xml documents.
026: * <p>
027: * Usage:
028: * <pre>
029: * WFS wfs = ...;
030: * OutputStream output = ...;
031: *
032: * WfsXmlWriter writer = new WfsXmlWriter.WFS1_0( wfs, output );
033: *
034: * //declare application schema namespaces
035: * writer.getNamespaceSupport().declareNamespace( "cdf", "http://cite.opengeospatial.org/cite" );
036: *
037: * //write the document
038: * writer.openTag( "wfs", "GetCapabilities" );
039: * ...
040: * //write the response
041: * writer.closeTag( "wfs", "GetCapabilities" );
042: * ....
043: *
044: * //close the writer
045: * writer.close();
046: * </pre>
047: * </p>
048: * @author Justin Deoliveira, The Open Planning Project
049: *
050: */
051: public abstract class WfsXmlWriter {
052: /**
053: * wfs configuration
054: */
055: WFS wfs;
056:
057: /**
058: * The output stream
059: */
060: OutputStream output;
061:
062: /**
063: * The character encoding.
064: */
065: String charSetEncoding;
066:
067: /**
068: * Declared namespaces
069: */
070: NamespaceSupport namespaceSupport;
071:
072: /**
073: * HashMap schemaLocations
074: */
075: HashMap schemaLocations;
076:
077: /**
078: * The wfs version
079: */
080: String version;
081:
082: /**
083: * writer
084: */
085: BufferedWriter writer;
086:
087: public WfsXmlWriter(WFS wfs, OutputStream output) {
088: this .wfs = wfs;
089: this .output = output;
090:
091: //default to wfs configured charset
092: charSetEncoding = wfs.getCharSet().name();
093:
094: //schema locations
095: schemaLocations = new HashMap();
096:
097: //declare the namespaces ( wfs is default )
098: namespaceSupport = new NamespaceSupport();
099: namespaceSupport.declarePrefix("xs", XS.NAMESPACE);
100: namespaceSupport.declarePrefix("ogc", OGC.NAMESPACE);
101: namespaceSupport.declarePrefix("gml", GML.NAMESPACE);
102:
103: namespaceSupport.declarePrefix("wfs",
104: org.geoserver.wfs.xml.v1_0_0.WFS.NAMESPACE);
105: namespaceSupport.declarePrefix("",
106: org.geoserver.wfs.xml.v1_0_0.WFS.NAMESPACE);
107: }
108:
109: public void setCharSetEncoding(String charSetEncoding) {
110: this .charSetEncoding = charSetEncoding;
111: }
112:
113: public NamespaceSupport getNamespaceSupport() {
114: return namespaceSupport;
115: }
116:
117: private void init() throws IOException {
118: //namespace declarations
119: for (Enumeration e = namespaceSupport.getDeclaredPrefixes(); e
120: .hasMoreElements();) {
121: String pre = (String) e.nextElement();
122: String uri = namespaceSupport.getURI(pre);
123:
124: if ("".equals(pre)) {
125: attribute("xmlns" + pre, uri);
126: } else {
127: attribute("xmlns:" + pre, uri);
128: }
129: }
130:
131: //schema locations
132: if (!schemaLocations.isEmpty()) {
133: StringBuffer buffer = new StringBuffer();
134:
135: for (Iterator e = schemaLocations.entrySet().iterator(); e
136: .hasNext();) {
137: Map.Entry entry = (Entry) e.next();
138: String uri = (String) entry.getKey();
139: String location = (String) entry.getValue();
140:
141: buffer.append(uri + " " + location);
142:
143: if (e.hasNext()) {
144: buffer.append(" ");
145: }
146: }
147:
148: attribute("xs:schemaLocation", buffer.toString());
149: }
150: }
151:
152: private void attribute(String name, String value)
153: throws IOException {
154: writer.write(" " + name + "=\"" + value + "\"");
155: }
156:
157: public void openTag(String prefix, String name) throws IOException {
158: openTag(prefix, name, null);
159: }
160:
161: public void openTag(String prefix, String name, String[] attributes)
162: throws IOException {
163: boolean root = writer == null;
164:
165: if (root) {
166: writer = new BufferedWriter(new OutputStreamWriter(output));
167:
168: //write the processing instruction
169: writer.write("<?xml version=\"1.0\" encoding=\""
170: + charSetEncoding + "\"?>");
171: }
172:
173: //write the element
174: if (prefix != null) {
175: writer.write("<" + prefix + ":" + name);
176: } else {
177: writer.write("<" + name);
178: }
179:
180: if (root) {
181: init();
182: }
183:
184: if (attributes != null) {
185: for (int i = 0; i < attributes.length; i += 2) {
186: attribute(attributes[i], attributes[i + 1]);
187: }
188: }
189:
190: writer.write(">");
191: }
192:
193: public void text(String text) throws IOException {
194: writer.write(text);
195: }
196:
197: public void closeTag(String prefix, String name) throws IOException {
198: //write the element
199: if (prefix != null) {
200: writer.write("</" + prefix + ":" + name + ">");
201: } else {
202: writer.write("</" + name + ">");
203: }
204: }
205:
206: public void close() throws IOException {
207: //close the writer
208: writer.flush();
209: writer.close();
210:
211: writer = null;
212: }
213:
214: public static class WFS1_0 extends WfsXmlWriter {
215: public WFS1_0(WFS wfs, OutputStream output) {
216: super (wfs, output);
217:
218: //set the schema location
219: schemaLocations.put(
220: org.geoserver.wfs.xml.v1_0_0.WFS.NAMESPACE,
221: ResponseUtils.appendPath(wfs.getSchemaBaseURL(),
222: "wfs/1.0.0/WFS-transaction.xsd"));
223:
224: version = "1.0.0";
225: }
226: }
227:
228: public static class WFS1_1 extends WfsXmlWriter {
229: public WFS1_1(WFS wfs, OutputStream output) {
230: super (wfs, output);
231:
232: //add the ows namespace
233: namespaceSupport.declarePrefix("ows", OWS.NAMESPACE);
234:
235: //set the schema location
236: schemaLocations.put(
237: org.geoserver.wfs.xml.v1_1_0.WFS.NAMESPACE,
238: ResponseUtils.appendPath(wfs.getSchemaBaseURL(),
239: "wfs/1.1.0/wfs.xsd"));
240:
241: version = "1.1.0";
242: }
243: }
244: }
|