comparison flys-artifacts/src/main/java/org/dive4elements/river/artifacts/services/AbstractChartService.java @ 5831:bd047b71ab37

Repaired internal references
author Sascha L. Teichmann <teichmann@intevation.de>
date Thu, 25 Apr 2013 12:06:39 +0200
parents flys-artifacts/src/main/java/de/intevation/flys/artifacts/services/AbstractChartService.java@61f4d4164a30
children
comparison
equal deleted inserted replaced
5830:160f53ee0870 5831:bd047b71ab37
1 package org.dive4elements.river.artifacts.services;
2
3 import java.awt.Dimension;
4 import java.awt.Transparency;
5 import java.awt.image.BufferedImage;
6 import java.io.ByteArrayOutputStream;
7 import java.io.IOException;
8
9 import javax.imageio.ImageIO;
10
11 import org.apache.log4j.Logger;
12 import org.jfree.chart.JFreeChart;
13 import org.w3c.dom.Document;
14 import org.w3c.dom.Element;
15 import org.w3c.dom.NodeList;
16
17 import org.dive4elements.artifactdatabase.DefaultService;
18 import org.dive4elements.artifacts.CallMeta;
19 import org.dive4elements.artifacts.GlobalContext;
20 import org.dive4elements.artifacts.Service;
21
22 /** Serve chart. */
23 public abstract class AbstractChartService extends DefaultService {
24
25 public static final int DEFAULT_WIDTH = 240;
26 public static final int DEFAULT_HEIGHT = 180;
27
28 public static final String DEFAULT_FORMAT = "png";
29
30 private static final Logger log = Logger
31 .getLogger(AbstractChartService.class);
32
33 // TODO: Load fancy image from resources.
34 public static final byte[] EMPTY = { (byte) 0x89, (byte) 0x50, (byte) 0x4e,
35 (byte) 0x47, (byte) 0x0d, (byte) 0x0a, (byte) 0x1a, (byte) 0x0a,
36 (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x0d, (byte) 0x49,
37 (byte) 0x48, (byte) 0x44, (byte) 0x52, (byte) 0x00, (byte) 0x00,
38 (byte) 0x00, (byte) 0x01, (byte) 0x00, (byte) 0x00, (byte) 0x00,
39 (byte) 0x01, (byte) 0x08, (byte) 0x00, (byte) 0x00, (byte) 0x00,
40 (byte) 0x00, (byte) 0x3a, (byte) 0x7e, (byte) 0x9b, (byte) 0x55,
41 (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x01, (byte) 0x73,
42 (byte) 0x52, (byte) 0x47, (byte) 0x42, (byte) 0x00, (byte) 0xae,
43 (byte) 0xce, (byte) 0x1c, (byte) 0xe9, (byte) 0x00, (byte) 0x00,
44 (byte) 0x00, (byte) 0x09, (byte) 0x70, (byte) 0x48, (byte) 0x59,
45 (byte) 0x73, (byte) 0x00, (byte) 0x00, (byte) 0x0b, (byte) 0x13,
46 (byte) 0x00, (byte) 0x00, (byte) 0x0b, (byte) 0x13, (byte) 0x01,
47 (byte) 0x00, (byte) 0x9a, (byte) 0x9c, (byte) 0x18, (byte) 0x00,
48 (byte) 0x00, (byte) 0x00, (byte) 0x07, (byte) 0x74, (byte) 0x49,
49 (byte) 0x4d, (byte) 0x45, (byte) 0x07, (byte) 0xdc, (byte) 0x04,
50 (byte) 0x04, (byte) 0x10, (byte) 0x30, (byte) 0x15, (byte) 0x7d,
51 (byte) 0x77, (byte) 0x36, (byte) 0x0b, (byte) 0x00, (byte) 0x00,
52 (byte) 0x00, (byte) 0x08, (byte) 0x74, (byte) 0x45, (byte) 0x58,
53 (byte) 0x74, (byte) 0x43, (byte) 0x6f, (byte) 0x6d, (byte) 0x6d,
54 (byte) 0x65, (byte) 0x6e, (byte) 0x74, (byte) 0x00, (byte) 0xf6,
55 (byte) 0xcc, (byte) 0x96, (byte) 0xbf, (byte) 0x00, (byte) 0x00,
56 (byte) 0x00, (byte) 0x0a, (byte) 0x49, (byte) 0x44, (byte) 0x41,
57 (byte) 0x54, (byte) 0x08, (byte) 0xd7, (byte) 0x63, (byte) 0xf8,
58 (byte) 0x0f, (byte) 0x00, (byte) 0x01, (byte) 0x01, (byte) 0x01,
59 (byte) 0x00, (byte) 0x1b, (byte) 0xb6, (byte) 0xee, (byte) 0x56,
60 (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x49,
61 (byte) 0x45, (byte) 0x4e, (byte) 0x44, (byte) 0xae, (byte) 0x42,
62 (byte) 0x60, (byte) 0x82 };
63
64 private static final Output empty() {
65 return new Output(EMPTY, "image/png");
66 }
67
68 protected abstract JFreeChart createChart(Document data,
69 GlobalContext globalContext, CallMeta callMeta);
70
71 protected void init() {
72 };
73
74 protected void finish() {
75 };
76
77 @Override
78 public Service.Output process(Document data, GlobalContext globalContext,
79 CallMeta callMeta) {
80 log.debug("process");
81
82 init();
83 try {
84 JFreeChart chart = createChart(data, globalContext, callMeta);
85
86 if (chart == null) {
87 return empty();
88 }
89
90 Dimension extent = getExtent(data);
91 String format = getFormat(data);
92
93 return encode(chart, extent, format);
94 }
95 finally {
96 finish();
97 }
98 }
99
100 protected static Output encode(JFreeChart chart, Dimension extent,
101 String format) {
102 BufferedImage image = chart.createBufferedImage(extent.width,
103 extent.height, Transparency.BITMASK, null);
104
105 ByteArrayOutputStream out = new ByteArrayOutputStream();
106
107 try {
108 ImageIO.write(image, format, out);
109 }
110 catch (IOException ioe) {
111 log.warn("writing image failed", ioe);
112 return empty();
113 }
114
115 return new Output(out.toByteArray(), "image/" + format);
116 }
117
118 protected static Dimension getExtent(Document input) {
119
120 int width = DEFAULT_WIDTH;
121 int height = DEFAULT_HEIGHT;
122
123 NodeList extents = input.getElementsByTagName("extent");
124
125 if (extents.getLength() > 0) {
126 Element element = (Element) extents.item(0);
127 String w = element.getAttribute("width");
128 String h = element.getAttribute("height");
129
130 try {
131 width = Math.max(1, Integer.parseInt(w));
132 }
133 catch (NumberFormatException nfe) {
134 log.warn("width '" + w + "' is not a valid.");
135 }
136
137 try {
138 height = Math.max(1, Integer.parseInt(h));
139 }
140 catch (NumberFormatException nfe) {
141 log.warn("height '" + h + "' is not a valid");
142 }
143 }
144
145 return new Dimension(width, height);
146 }
147
148 protected static String getFormat(Document input) {
149 String format = DEFAULT_FORMAT;
150
151 NodeList formats = input.getElementsByTagName("format");
152
153 if (formats.getLength() > 0) {
154 String type = ((Element) formats.item(0)).getAttribute("type");
155 if (type.length() > 0) {
156 format = type;
157 }
158 }
159
160 return format;
161 }
162 }

http://dive4elements.wald.intevation.org