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.featureInfo;
006:
007: import java.io.IOException;
008: import java.io.OutputStream;
009: import java.io.OutputStreamWriter;
010: import java.nio.charset.Charset;
011: import java.util.Collections;
012: import java.util.HashMap;
013: import java.util.Iterator;
014:
015: import org.geoserver.template.FeatureWrapper;
016: import org.geoserver.template.GeoServerTemplateLoader;
017: import org.geotools.feature.FeatureCollection;
018: import org.geotools.feature.FeatureType;
019:
020: import freemarker.template.Configuration;
021: import freemarker.template.Template;
022: import freemarker.template.TemplateException;
023:
024: /**
025: * Produces a FeatureInfo response in HTML. Relies on {@link AbstractFeatureInfoResponse} and
026: * the feature delegate to do most of the work, just implements an HTML based
027: * writeTo method.
028: *
029: * <p>
030: * In the future James suggested that we allow some sort of template system, so
031: * that one can control the formatting of the html output, since now we just
032: * hard code some minimal header stuff. See
033: * http://jira.codehaus.org/browse/GEOS-196
034: * </p>
035: *
036: * @author James Macgill, PSU
037: * @author Andrea Aime, TOPP
038: * @version $Id: HTMLTableFeatureInfoResponse.java 7589 2007-10-11 08:02:59Z aaime $
039: */
040: public class HTMLTableFeatureInfoResponse extends
041: AbstractFeatureInfoResponse {
042: private static Configuration templateConfig;
043:
044: static {
045: // initialize the template engine, this is static to maintain a cache
046: // over instantiations of kml writer
047: templateConfig = new Configuration();
048: templateConfig.setObjectWrapper(new FeatureWrapper());
049: }
050:
051: GeoServerTemplateLoader templateLoader;
052:
053: /**
054: *
055: */
056: public HTMLTableFeatureInfoResponse() {
057: format = "text/html";
058: supportedFormats = Collections.singletonList(format);
059: }
060:
061: /**
062: * Returns any extra headers that this service might want to set in the HTTP response object.
063: * @see org.vfny.geoserver.Response#getResponseHeaders()
064: */
065: public HashMap getResponseHeaders() {
066: return new HashMap();
067: }
068:
069: /**
070: * Writes the image to the client.
071: *
072: * @param out The output stream to write to.
073: *
074: * @throws org.vfny.geoserver.ServiceException For problems with geoserver
075: * @throws java.io.IOException For problems writing the output.
076: */
077: public void writeTo(OutputStream out)
078: throws org.vfny.geoserver.ServiceException,
079: java.io.IOException {
080: // setup the writer
081: Charset charSet = getRequest().getGeoServer().getCharSet();
082: OutputStreamWriter osw = new OutputStreamWriter(out, charSet);
083:
084: // if there is only one feature type loaded, we allow for header/footer customization,
085: // otherwise we stick with the generic ones
086: FeatureType templateFeatureType = null;
087: if (results.size() == 1) {
088: templateFeatureType = ((FeatureCollection) results.get(0))
089: .getSchema();
090: }
091: Template header = getTemplate(templateFeatureType,
092: "header.ftl", charSet);
093: Template footer = getTemplate(templateFeatureType,
094: "footer.ftl", charSet);
095:
096: try {
097: header.process(null, osw);
098:
099: for (Iterator it = results.iterator(); it.hasNext();) {
100: FeatureCollection fc = (FeatureCollection) it.next();
101: if (fc.size() > 0) {
102: FeatureType ft = fc.getSchema();
103: Template content = getTemplate(ft, "content.ftl",
104: charSet);
105: content.process(fc, osw);
106: }
107: }
108:
109: footer.process(null, osw);
110: } catch (TemplateException e) {
111: String msg = "Error occured processing template.";
112: throw (IOException) new IOException(msg).initCause(e);
113: }
114: osw.flush();
115: }
116:
117: public String getContentDisposition() {
118: return null;
119: }
120:
121: Template getTemplate(FeatureType featureType,
122: String templateFileName, Charset charset)
123: throws IOException {
124: // setup template subsystem
125: if (templateLoader == null) {
126: templateLoader = new GeoServerTemplateLoader(getClass());
127: }
128: templateLoader.setFeatureType(featureType);
129:
130: synchronized (templateConfig) {
131: templateConfig.setTemplateLoader(templateLoader);
132: Template t = templateConfig.getTemplate(templateFileName);
133: t.setEncoding(charset.name());
134: return t;
135: }
136: }
137: }
|