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_1_0;
06:
07: import freemarker.template.Configuration;
08: import freemarker.template.Template;
09: import freemarker.template.TemplateException;
10: import org.geoserver.ows.Response;
11: import org.geoserver.platform.Operation;
12: import org.geoserver.platform.ServiceException;
13: import org.geoserver.template.GeoServerTemplateLoader;
14: import org.geotools.data.postgis.FeatureDiffReader;
15: import java.io.IOException;
16: import java.io.OutputStream;
17: import java.io.OutputStreamWriter;
18: import java.nio.charset.Charset;
19:
20: /**
21: * WFS output format for a GetDiff operation whose output format is a WFS 1.1
22: * transaction
23: *
24: * @author Andrea Aime, TOPP
25: *
26: */
27: public class GetDiffHtmlOutputFormat extends Response {
28: private static Configuration templateConfig;
29:
30: static {
31: // initialize the template engine, this is static to maintain a cache
32: // over instantiations of kml writer
33: templateConfig = new Configuration();
34: templateConfig.setObjectWrapper(new FeatureDiffWrapper());
35: }
36:
37: public GetDiffHtmlOutputFormat() {
38: super (FeatureDiffReader[].class, "HTML");
39: }
40:
41: public String getMimeType(Object value, Operation operation)
42: throws ServiceException {
43: return "text/html";
44: }
45:
46: public boolean canHandle(Operation operation) {
47: return "GetDiff".equalsIgnoreCase(operation.getId());
48: }
49:
50: public void write(Object value, OutputStream output,
51: Operation operation) throws IOException, ServiceException {
52: FeatureDiffReader[] diffReaders = (FeatureDiffReader[]) value;
53:
54: // setup template subsystem
55: GeoServerTemplateLoader templateLoader = new GeoServerTemplateLoader(
56: getClass());
57: templateLoader.setFeatureType(diffReaders[0].getSchema());
58:
59: Template template = null;
60:
61: synchronized (templateConfig) {
62: templateConfig.setTemplateLoader(templateLoader);
63: template = templateConfig.getTemplate("wfsvGetDiff.ftl");
64: }
65:
66: try {
67: template.setOutputEncoding("UTF-8");
68: template.process(diffReaders, new OutputStreamWriter(
69: output, Charset.forName("UTF-8")));
70: } catch (TemplateException e) {
71: String msg = "Error occured processing template.";
72: throw (IOException) new IOException(msg).initCause(e);
73: } finally {
74: for (int i = 0; i < diffReaders.length; i++) {
75: diffReaders[i].close();
76: }
77: }
78: }
79: }
|