01: /* Copyright (c) 2001 - 2007 TOPP - www.openplans.org. All rights reserved.
02: * This code is licensed under the GPL 2.0 license, availible at the root
03: * application directory.
04: */
05: package org.geoserver.wfsv.response.v1_0_0;
06:
07: import java.io.ByteArrayOutputStream;
08: import java.io.IOException;
09: import java.io.OutputStream;
10:
11: import org.geoserver.ows.Response;
12: import org.geoserver.platform.Operation;
13: import org.geoserver.wfs.xml.v1_0_0.XmlSchemaEncoder;
14: import org.geoserver.wfsv.VersionedDescribeResults;
15:
16: /**
17: *
18: * @author Andrea Aime
19: *
20: */
21: public class VersionedXmlSchemaEncoder extends Response {
22:
23: private static String[][] REPLACEMENTS;
24:
25: static {
26: REPLACEMENTS = new String[4][2];
27: REPLACEMENTS[0][0] = "\"http://www.opengis.net/gml\"";
28: REPLACEMENTS[0][1] = "\"http://www.opengis.net/wfsv\"";
29: REPLACEMENTS[1][0] = "schemas/gml/2.1.2.1/feature.xsd\"";
30: REPLACEMENTS[1][1] = "schemas/wfs/1.0.0/WFS-versioning.xsd\"";
31: REPLACEMENTS[2][0] = "base=\"gml:AbstractFeatureType\"";
32: REPLACEMENTS[2][1] = "base=\"wfsv:AbstractVersionedFeatureType\"";
33: REPLACEMENTS[3][0] = "substitutionGroup=\"gml:_Feature\"";
34: REPLACEMENTS[3][1] = "substitutionGroup=\"wfsv:_VersionedFeature\"";
35: }
36:
37: private XmlSchemaEncoder delegate;
38:
39: public VersionedXmlSchemaEncoder(XmlSchemaEncoder delegate) {
40: super (VersionedDescribeResults.class, delegate
41: .getOutputFormats());
42: this .delegate = delegate;
43: }
44:
45: public String getMimeType(Object value, Operation operation)
46: throws org.geoserver.platform.ServiceException {
47: return delegate.getMimeType(value, operation);
48: }
49:
50: public void write(Object value, OutputStream output,
51: Operation operation) throws IOException,
52: org.geoserver.platform.ServiceException {
53: VersionedDescribeResults results = (VersionedDescribeResults) value;
54:
55: if (!results.isVersioned()) {
56: delegate.write(results.getFeatureTypeInfo(), output,
57: operation);
58: } else {
59: ByteArrayOutputStream bos = new ByteArrayOutputStream();
60: delegate
61: .write(results.getFeatureTypeInfo(), bos, operation);
62: String describe = bos.toString();
63:
64: // now let's do the transformation magic, sigh...
65: for (int i = 0; i < REPLACEMENTS.length; i++) {
66: describe = describe.replaceAll(REPLACEMENTS[i][0],
67: REPLACEMENTS[i][1]);
68: }
69:
70: // back on the output stream
71: output.write(describe.getBytes());
72: }
73: }
74:
75: public boolean canHandle(Operation operation) {
76: return "DescribeVersionedFeatureType"
77: .equalsIgnoreCase(operation.getId())
78: && operation.getService().getId().equalsIgnoreCase(
79: "wfsv");
80: }
81:
82: }
|