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.vfny.geoserver.wms.responses.helpers;
006:
007: import org.geoserver.ows.util.RequestUtils;
008: import org.geoserver.platform.GeoServerExtensions;
009: import org.geotools.xml.transform.TransformerBase;
010: import org.geotools.xml.transform.Translator;
011: import org.springframework.context.ApplicationContext;
012: import org.vfny.geoserver.global.GeoServer;
013: import org.vfny.geoserver.global.MapLayerInfo;
014: import org.vfny.geoserver.wms.requests.DescribeLayerRequest;
015: import org.xml.sax.ContentHandler;
016: import org.xml.sax.helpers.AttributesImpl;
017: import java.util.Iterator;
018: import javax.xml.transform.OutputKeys;
019: import javax.xml.transform.Transformer;
020: import javax.xml.transform.TransformerException;
021:
022: /**
023: * <code>org.geotools.xml.transform.TransformerBase</code> specialized in
024: * producing a WMS DescribeLayer response.
025: *
026: * @author Gabriel Roldan, Axios Engineering
027: * @version $Id: DescribeLayerTransformer.java 7522 2007-09-12 22:00:10Z saul.farber $
028: */
029: public class DescribeLayerTransformer extends TransformerBase {
030: /** The base url upon URLs which point to 'me' should be based. */
031: private String baseUrl;
032:
033: private GeoServer geoserver;
034:
035: /**
036: * Creates a new DescribeLayerTransformer object.
037: *
038: * @param baseUrl the url string wich holds the validation
039: * schemas and DTD's on this server instance.
040: */
041: public DescribeLayerTransformer(String baseUrl, GeoServer gs) {
042: super ();
043:
044: if (baseUrl == null) {
045: throw new NullPointerException();
046: }
047:
048: this .baseUrl = baseUrl;
049: this .geoserver = gs;
050: }
051:
052: /**
053: * Creates and returns a Translator specialized in producing
054: * a DescribeLayer response document.
055: *
056: * @param handler the content handler to send sax events to.
057: *
058: * @return a new <code>DescribeLayerTranslator</code>
059: */
060: public Translator createTranslator(ContentHandler handler) {
061: return new DescribeLayerTranslator(handler);
062: }
063:
064: /**
065: * Gets the <code>Transformer</code> created by the overriden method in
066: * the superclass and adds it the DOCTYPE token pointing to the
067: * DescribeLayer DTD on this server instance.
068: *
069: * <p>
070: * The DTD is set at the fixed location given by the <code>schemaBaseUrl</code>
071: * passed to the constructor <code>+ "wms/1.1.1/WMS_DescribeLayerResponse.dtd</code>.
072: * </p>
073: *
074: * @return a Transformer propoerly configured to produce DescribeLayer responses.
075: *
076: * @throws TransformerException if it is thrown by <code>super.createTransformer()</code>
077: */
078: public Transformer createTransformer() throws TransformerException {
079: Transformer transformer = super .createTransformer();
080: String dtdUrl = RequestUtils.proxifiedBaseURL(baseUrl,
081: geoserver.getProxyBaseUrl())
082: + "schemas/wms/1.1.1/WMS_DescribeLayerResponse.dtd";
083: transformer
084: .setOutputProperty(OutputKeys.DOCTYPE_SYSTEM, dtdUrl);
085:
086: return transformer;
087: }
088:
089: /**
090: * Sends SAX events to produce a DescribeLayer response document.
091: *
092: * @author Gabriel Roldan, Axios Engineering
093: * @version $Id: DescribeLayerTransformer.java 7522 2007-09-12 22:00:10Z saul.farber $
094: */
095: private static class DescribeLayerTranslator extends
096: TranslatorSupport {
097: /**
098: * Creates a new DescribeLayerTranslator object.
099: *
100: * @param handler DOCUMENT ME!
101: */
102: public DescribeLayerTranslator(ContentHandler handler) {
103: super (handler, null, null);
104: }
105:
106: /**
107: * Encode the object.
108: *
109: * @param o The Object to encode.
110: *
111: * @throws IllegalArgumentException if the Object is not encodeable.
112: */
113: public void encode(Object o) throws IllegalArgumentException {
114: if (!(o instanceof DescribeLayerRequest)) {
115: throw new IllegalArgumentException();
116: }
117:
118: DescribeLayerRequest req = (DescribeLayerRequest) o;
119:
120: AttributesImpl version = new AttributesImpl();
121: version.addAttribute("", "version", "version", "", "1.0.0");
122:
123: start("WMS_DescribeLayerResponse", version);
124:
125: handleLayers(req);
126:
127: end("WMS_DescribeLayerResponse");
128: }
129:
130: /**
131: * As currently GeoServer does not have support for nested layers, this
132: * method declares a <code>LayerDescription</code> element for each
133: * featuretype requested.
134: *
135: * @param req
136: */
137: private void handleLayers(DescribeLayerRequest req) {
138: MapLayerInfo layer;
139:
140: String url = RequestUtils.proxifiedBaseURL(
141: req.getBaseUrl(), req.getServiceRef()
142: .getGeoServer().getProxyBaseUrl())
143: + "wfs/WfsDispatcher?";
144:
145: AttributesImpl layerAtts = new AttributesImpl();
146: layerAtts.addAttribute("", "name", "name", "", "");
147: layerAtts.addAttribute("", "wfs", "wfs", "", url);
148:
149: AttributesImpl queryAtts = new AttributesImpl();
150: queryAtts.addAttribute("", "typeName", "typeName", "", "");
151:
152: for (Iterator it = req.getLayers().iterator(); it.hasNext();) {
153: layer = (MapLayerInfo) it.next();
154: layerAtts.setAttribute(0, "", "name", "name", "", layer
155: .getName());
156: start("LayerDescription", layerAtts);
157:
158: queryAtts.setAttribute(0, "", "typeName", "typeName",
159: "", layer.getName());
160: element("Query", null, queryAtts);
161:
162: end("LayerDescription");
163: }
164: }
165: }
166: }
|