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.xml;
006:
007: import net.opengis.wfs.FeatureCollectionType;
008: import net.opengis.wfs.GetFeatureType;
009:
010: import net.opengis.wfs.BaseRequestType;
011: import org.geoserver.ows.util.RequestUtils;
012: import org.geoserver.ows.util.ResponseUtils;
013: import org.geoserver.platform.Operation;
014: import org.geoserver.platform.ServiceException;
015: import org.geoserver.wfs.WFS;
016: import org.geoserver.wfs.WFSException;
017: import org.geoserver.wfs.WFSGetFeatureOutputFormat;
018: import org.geoserver.wfs.xml.v1_1_0.WFSConfiguration;
019: import org.geotools.feature.FeatureCollection;
020: import org.geotools.feature.FeatureType;
021: import org.geotools.xml.Encoder;
022: import org.vfny.geoserver.global.Data;
023: import org.vfny.geoserver.global.FeatureTypeInfo;
024: import org.xml.sax.SAXException;
025: import java.io.IOException;
026: import java.io.OutputStream;
027: import java.util.Arrays;
028: import java.util.HashMap;
029: import java.util.HashSet;
030: import java.util.Iterator;
031: import java.util.List;
032: import java.util.Map;
033: import java.util.Set;
034:
035: public class GML3OutputFormat extends WFSGetFeatureOutputFormat {
036: WFS wfs;
037: Data catalog;
038: WFSConfiguration configuration;
039:
040: public GML3OutputFormat(WFS wfs, Data catalog,
041: WFSConfiguration configuration) {
042: super (new HashSet(Arrays.asList(new Object[] { "gml3",
043: "text/xml; subtype=gml/3.1.1" })));
044:
045: this .wfs = wfs;
046: this .catalog = catalog;
047: this .configuration = configuration;
048: }
049:
050: public String getMimeType(Object value, Operation operation) {
051: return "text/xml; subtype=gml/3.1.1";
052: }
053:
054: public String getCapabilitiesElementName() {
055: return "GML3";
056: }
057:
058: protected void write(FeatureCollectionType results,
059: OutputStream output, Operation getFeature)
060: throws ServiceException, IOException {
061: List featureCollections = results.getFeature();
062:
063: //round up the info objects for each feature collection
064: HashMap /*<String,Set>*/ns2metas = new HashMap();
065:
066: for (Iterator fc = featureCollections.iterator(); fc.hasNext();) {
067: FeatureCollection features = (FeatureCollection) fc.next();
068: FeatureType featureType = features.getSchema();
069:
070: //load the metadata for the feature type
071: String namespaceURI = featureType.getNamespace().toString();
072: FeatureTypeInfo meta = catalog.getFeatureTypeInfo(
073: featureType.getTypeName(), namespaceURI);
074:
075: if (meta == null)
076: throw new WFSException("Could not find feature type "
077: + namespaceURI + ":"
078: + featureType.getTypeName()
079: + " in the GeoServer catalog");
080:
081: //add it to the map
082: Set metas = (Set) ns2metas.get(namespaceURI);
083:
084: if (metas == null) {
085: metas = new HashSet();
086: ns2metas.put(namespaceURI, metas);
087: }
088:
089: metas.add(meta);
090: }
091:
092: Encoder encoder = new Encoder(configuration, configuration
093: .schema());
094:
095: //declare wfs schema location
096: BaseRequestType gft = (BaseRequestType) getFeature
097: .getParameters()[0];
098:
099: String proxifiedBaseUrl = RequestUtils.proxifiedBaseURL(gft
100: .getBaseUrl(), wfs.getGeoServer().getProxyBaseUrl());
101: encoder.setSchemaLocation(
102: org.geoserver.wfs.xml.v1_1_0.WFS.NAMESPACE,
103: ResponseUtils.appendPath(proxifiedBaseUrl,
104: "schemas/wfs/1.1.0/wfs.xsd"));
105:
106: //declare application schema namespaces
107: for (Iterator i = ns2metas.entrySet().iterator(); i.hasNext();) {
108: Map.Entry entry = (Map.Entry) i.next();
109:
110: String namespaceURI = (String) entry.getKey();
111: Set metas = (Set) entry.getValue();
112:
113: StringBuffer typeNames = new StringBuffer();
114:
115: for (Iterator m = metas.iterator(); m.hasNext();) {
116: FeatureTypeInfo meta = (FeatureTypeInfo) m.next();
117: typeNames.append(meta.getName());
118:
119: if (m.hasNext()) {
120: typeNames.append(",");
121: }
122: }
123:
124: //set the schema location
125: encoder.setSchemaLocation(namespaceURI, ResponseUtils
126: .appendQueryString(proxifiedBaseUrl + "wfs",
127: "service=WFS&version=1.1.0&request=DescribeFeatureType&typeName="
128: + typeNames.toString()));
129: }
130:
131: try {
132: encoder.encode(results,
133: org.geoserver.wfs.xml.v1_1_0.WFS.FEATURECOLLECTION,
134: output);
135: } catch (SAXException e) {
136: String msg = "Error occurred encoding features";
137: //SAXException does not sets initCause(). Instead, it holds its own "exception" field.
138: if (e.getException() != null && e.getCause() == null) {
139: e.initCause(e.getException());
140: }
141: throw (IOException) new IOException(msg).initCause(e);
142: }
143: }
144: }
|