001 //$$Header: $$
002 /*----------------------------------------------------------------------------
003 This file is part of deegree, http://deegree.org/
004 Copyright (C) 2001-2009 by:
005 Department of Geography, University of Bonn
006 and
007 lat/lon GmbH
008
009 This library is free software; you can redistribute it and/or modify it under
010 the terms of the GNU Lesser General Public License as published by the Free
011 Software Foundation; either version 2.1 of the License, or (at your option)
012 any later version.
013 This library is distributed in the hope that it will be useful, but WITHOUT
014 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
015 FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
016 details.
017 You should have received a copy of the GNU Lesser General Public License
018 along with this library; if not, write to the Free Software Foundation, Inc.,
019 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
020
021 Contact information:
022
023 lat/lon GmbH
024 Aennchenstr. 19, 53177 Bonn
025 Germany
026 http://lat-lon.de/
027
028 Department of Geography, University of Bonn
029 Prof. Dr. Klaus Greve
030 Postfach 1147, 53001 Bonn
031 Germany
032 http://www.geographie.uni-bonn.de/deegree/
033
034 e-mail: info@deegree.org
035 ----------------------------------------------------------------------------*/
036
037 package org.deegree.graphics.charts;
038
039 import java.awt.Color;
040 import java.io.IOException;
041 import java.net.URI;
042 import java.net.URL;
043
044 import javax.xml.parsers.DocumentBuilder;
045 import javax.xml.parsers.DocumentBuilderFactory;
046 import javax.xml.parsers.ParserConfigurationException;
047
048 import org.deegree.framework.util.StringTools;
049 import org.deegree.framework.xml.NamespaceContext;
050 import org.deegree.framework.xml.XMLFragment;
051 import org.deegree.framework.xml.XMLParsingException;
052 import org.deegree.framework.xml.XMLTools;
053 import org.deegree.ogcbase.CommonNamespaces;
054 import org.w3c.dom.Document;
055 import org.w3c.dom.Element;
056 import org.xml.sax.SAXException;
057
058 /**
059 * This class reads the configurations file for the charts and acts as a data class for the configurations It parses the
060 * configurations file according to a given schema and throws an exception if the parsing failed It also assigns default
061 * values to variables that are not mandatory and don't exist in the configs file
062 *
063 * @author <a href="mailto:elmasry@lat-lon.de">Moataz Elmasry</a>
064 * @author last edited by: $Author: elmasri$
065 *
066 * @version $Revision: $, $Date: 25 Apr 2008 13:50:16$
067 */
068 public class ChartConfig {
069
070 private static NamespaceContext cnxt = CommonNamespaces.getNamespaceContext();
071
072 private boolean genAntiAliasing;
073
074 private boolean genTextAntiAlias;
075
076 private boolean genBorderVisible;
077
078 private boolean plotOutlineVisible;
079
080 private String genRectangleInsets;
081
082 private Color genBackgroundColor = null;
083
084 private String genFontFamily;
085
086 private String genFontType;
087
088 private double genFontSize;
089
090 private double plotForegroundOpacity;
091
092 private Color plotBackgroundColor = null;
093
094 private double plotBackgroundOpacity;
095
096 private double pieInteriorGap;
097
098 private double pieLabelGap;
099
100 private boolean pieCircular;
101
102 private Color pieBaseSectionColor = null;
103
104 private Color pieShadowColor = null;
105
106 private boolean lineRenderLines;
107
108 private boolean lineRenderShapes;
109
110 /**
111 * Takes in the path to the configurations file, parses it and holds its values
112 *
113 * @param configsPath
114 * XML file that contains the charts configurations
115 * @throws SAXException
116 * @throws IOException
117 * @throws XMLParsingException
118 */
119 public ChartConfig( URL configsPath ) throws SAXException, IOException, XMLParsingException {
120
121 Document doc = instantiateParser().parse( configsPath.openStream(), XMLFragment.DEFAULT_URL );
122 Element root = doc.getDocumentElement();
123 String prefix = root.getPrefix();
124 String namespace = root.getNamespaceURI();
125 URI uri = CommonNamespaces.buildNSURI( namespace );
126 cnxt.addNamespace( prefix, uri );
127 parseConfigurations( root );
128 }
129
130 /**
131 * Creates a new instance of DocumentBuilder
132 *
133 * @return DocumentBuilder
134 * @throws IOException
135 */
136 private static DocumentBuilder instantiateParser()
137 throws IOException {
138
139 DocumentBuilder parser = null;
140
141 try {
142 DocumentBuilderFactory fac = DocumentBuilderFactory.newInstance();
143 fac.setNamespaceAware( true );
144 fac.setValidating( false );
145 fac.setIgnoringElementContentWhitespace( false );
146 parser = fac.newDocumentBuilder();
147 return parser;
148 } catch ( ParserConfigurationException e ) {
149 throw new IOException( Messages.getMessage( "GRA_CHART_ERROR_INIT_DOCBUILDER", e.getLocalizedMessage() ) );
150 }
151 }
152
153 /**
154 * This is the main parsing method. It parses the four parts of the xml configurations file: GeneralSettings,
155 * PlotSettings, PiePlotSettings and LinePlotSettings
156 *
157 * @param root
158 * @throws XMLParsingException
159 */
160 protected void parseConfigurations( Element root )
161 throws XMLParsingException {
162 String prefix = root.getPrefix() + ":";
163 parseGeneralChartSettings( XMLTools.getElement( root, StringTools.concat( 100, "./", prefix,
164 "GeneralChartSettings" ), cnxt ) );
165 parsePlotSettings( XMLTools.getElement( root, StringTools.concat( 100, "./", prefix, "GeneralPlotSettings" ),
166 cnxt ) );
167 parsePiePlotSettings( XMLTools.getElement( root, StringTools.concat( 100, "./", prefix, "PiePlotSettings" ),
168 cnxt ) );
169 parseLinePlotSettings( XMLTools.getElement( root, StringTools.concat( 100, "./", prefix, "LinePlotSettings" ),
170 cnxt ) );
171 }
172
173 /**
174 * Parses the general settings of the configurations file. Refer to Resources for an example file and/or schema
175 *
176 * @param chartElem
177 * @throws XMLParsingException
178 */
179 protected void parseGeneralChartSettings( Element chartElem )
180 throws XMLParsingException {
181 String prefix = chartElem.getPrefix() + ":";
182 genAntiAliasing = XMLTools.getRequiredNodeAsBoolean( chartElem, StringTools.concat( 100, "./", prefix,
183 "Antialiasing" ), cnxt );
184 genTextAntiAlias = XMLTools.getRequiredNodeAsBoolean( chartElem, StringTools.concat( 100, "./", prefix,
185 "TextAntialiasing" ), cnxt );
186 genBorderVisible = XMLTools.getRequiredNodeAsBoolean( chartElem, StringTools.concat( 100, "./", prefix,
187 "BorderVisibility" ), cnxt );
188 genRectangleInsets = XMLTools.getRequiredNodeAsString( chartElem, StringTools.concat( 100, "./", prefix,
189 "RectangleInsets" ), cnxt );
190 double genBackgroundOpacity = XMLTools.getRequiredNodeAsDouble( chartElem,
191 StringTools.concat( 100, "./", prefix,
192 "BackgroundOpacity" ), cnxt );
193 String color = XMLTools.getRequiredNodeAsString( chartElem, StringTools.concat( 100, "./", prefix,
194 "BackgroundColor" ), cnxt );
195 genBackgroundColor = hexToColor( color, (float) genBackgroundOpacity );
196
197 genFontFamily = XMLTools.getRequiredNodeAsString( chartElem, StringTools.concat( 100, "./", prefix,
198 "FontFamily" ), cnxt );
199
200 genFontType = XMLTools.getRequiredNodeAsString( chartElem, StringTools.concat( 100, "./", prefix, "FontType" ),
201 cnxt );
202 genFontSize = XMLTools.getRequiredNodeAsDouble( chartElem, StringTools.concat( 100, "./", prefix, "FontSize" ),
203 cnxt );
204
205 }
206
207 /**
208 * Parses the plot settings of the configurations file. Refer to Resources for an example file and/or schema The
209 * plot is the actual chart, while everything outside it is the general settings
210 *
211 * @param plotElem
212 * @throws XMLParsingException
213 */
214 protected void parsePlotSettings( Element plotElem )
215 throws XMLParsingException {
216 String prefix = plotElem.getPrefix() + ":";
217 plotForegroundOpacity = XMLTools.getRequiredNodeAsDouble( plotElem, StringTools.concat( 100, "./", prefix,
218 "ForegroundOpacity" ),
219 cnxt );
220 plotBackgroundOpacity = XMLTools.getRequiredNodeAsDouble( plotElem, StringTools.concat( 100, "./", prefix,
221 "BackgroundOpacity" ),
222 cnxt );
223 plotBackgroundColor = hexToColor( XMLTools.getRequiredNodeAsString( plotElem,
224 StringTools.concat( 100, "./", prefix,
225 "BackgroundColor" ),
226 cnxt ), (float) plotBackgroundOpacity );
227 plotOutlineVisible = XMLTools.getRequiredNodeAsBoolean( plotElem, StringTools.concat( 100, "./", prefix,
228 "OutlineVisibility" ),
229 cnxt );
230
231 }
232
233 /**
234 * Parses the Pie plot settings of the configurations file. Refer to Resources for an example file and/or schema
235 *
236 * @param pieplotElem
237 * @throws XMLParsingException
238 */
239 protected void parsePiePlotSettings( Element pieplotElem )
240 throws XMLParsingException {
241
242 String prefix = pieplotElem.getPrefix() + ":";
243 pieInteriorGap = XMLTools.getRequiredNodeAsDouble( pieplotElem, StringTools.concat( 100, "./", prefix,
244 "InteriorGap" ), cnxt );
245 pieLabelGap = XMLTools.getRequiredNodeAsDouble( pieplotElem,
246 StringTools.concat( 100, "./", prefix, "LabelGap" ), cnxt );
247
248 pieCircular = XMLTools.getRequiredNodeAsBoolean( pieplotElem,
249 StringTools.concat( 100, "./", prefix, "Circular" ), cnxt );
250 double pieBaseSectionOpacity = XMLTools.getRequiredNodeAsDouble( pieplotElem,
251 StringTools.concat( 100, "./", prefix,
252 "BaseSectionOpacity" ),
253 cnxt );
254 pieBaseSectionColor = hexToColor( XMLTools.getRequiredNodeAsString( pieplotElem,
255 StringTools.concat( 100, "./", prefix,
256 "BaseSectionColor" ),
257 cnxt ), (float) pieBaseSectionOpacity );
258
259 double pieShadowOpacity = XMLTools.getRequiredNodeAsDouble( pieplotElem, StringTools.concat( 100, "./", prefix,
260 "ShadowOpacity" ),
261 cnxt );
262 pieShadowColor = hexToColor( XMLTools.getRequiredNodeAsString( pieplotElem,
263 StringTools.concat( 100, "./", prefix,
264 "ShadowColor" ), cnxt ),
265 (float) pieShadowOpacity );
266 }
267
268 /**
269 * Parses the Line plot settings of the configurations file. Refer to Resources for an example file and/or schema
270 *
271 * @param lineplotElem
272 * @throws XMLParsingException
273 */
274 protected void parseLinePlotSettings( Element lineplotElem )
275 throws XMLParsingException {
276 String prefix = lineplotElem.getPrefix() + ":";
277 lineRenderLines = XMLTools.getRequiredNodeAsBoolean( lineplotElem, StringTools.concat( 100, "./", prefix,
278 "RenderLines" ), cnxt );
279 lineRenderShapes = XMLTools.getRequiredNodeAsBoolean( lineplotElem, StringTools.concat( 100, "./", prefix,
280 "RenderShapes" ), cnxt );
281 }
282
283 /**
284 * It converts a hexdecimal to a awt.color instance
285 *
286 * @param hexDecimal
287 * it can start with # or not
288 * @param alpha
289 * between 0..1
290 * @return generated color
291 * @throws NumberFormatException
292 * if the given hex decimal could not be parsed.
293 */
294 protected Color hexToColor( String hexDecimal, float alpha )
295 throws NumberFormatException {
296 if ( hexDecimal.startsWith( "0x" ) ) {
297 hexDecimal = hexDecimal.substring( 2, hexDecimal.length() );
298 }
299 if ( hexDecimal.length() == 6 ) {
300 float r = Integer.parseInt( hexDecimal.substring( 0, 2 ), 16 );
301 float g = Integer.parseInt( hexDecimal.substring( 2, 4 ), 16 );
302 float b = Integer.parseInt( hexDecimal.substring( 4, 6 ), 16 );
303 return new Color( r / 255f, g / 255f, b / 255f, alpha );
304 }
305 throw new NumberFormatException( Messages.getMessage( "GRA_CHART_BAD_FORMAT_HEX", hexDecimal ) );
306 }
307
308 /**
309 * @return is enabled Antialiasing for the chart
310 */
311 public boolean isGenAntiAliasing() {
312 return genAntiAliasing;
313 }
314
315 /**
316 * @return Background color of the chart
317 */
318 public Color getGenBackgroundColor() {
319 return genBackgroundColor;
320 }
321
322 /**
323 * @return is cart borders visible
324 */
325 public boolean isGenBorderVisible() {
326 return genBorderVisible;
327 }
328
329 /**
330 * @return font family of the chart
331 */
332 public String getGenFontFamily() {
333 return genFontFamily;
334 }
335
336 /**
337 * @return font size of the chart
338 */
339 public double getGenFontSize() {
340 return genFontSize;
341 }
342
343 /**
344 * @return font type of the chart
345 */
346 public String getGenFontType() {
347 return genFontType;
348 }
349
350 /**
351 * @return is chart outline visible
352 */
353 public boolean isPlotOutlineVisible() {
354 return plotOutlineVisible;
355 }
356
357 /**
358 * @return RectangleInsets of the chart
359 */
360 public String getGenRectangleInsets() {
361 return genRectangleInsets;
362 }
363
364 /**
365 * @return is enabled Text AntiAliasing
366 */
367 public boolean isGenTextAntiAlias() {
368 return genTextAntiAlias;
369 }
370
371 /**
372 * @return is enabled line rendering in Line Chart
373 */
374 public boolean isLineRenderLines() {
375 return lineRenderLines;
376 }
377
378 /**
379 * @return is enabled shape rendering in Line Chart
380 */
381 public boolean isLineRenderShapes() {
382 return lineRenderShapes;
383 }
384
385 /**
386 * @return BaseSectionColor of Pie chart
387 */
388 public Color getPieBaseSectionColor() {
389 return pieBaseSectionColor;
390 }
391
392 /**
393 * @return is the Pie Chart circular
394 */
395 public boolean isPieCircular() {
396 return pieCircular;
397 }
398
399 /**
400 * @return interior gap of the Pie chart
401 */
402 public double getPieInteriorGap() {
403 return pieInteriorGap;
404 }
405
406 /**
407 * @return label gap of the Pie Chart
408 */
409 public double getPieLabelGap() {
410 return pieLabelGap;
411 }
412
413 /**
414 * @return Background color of the general plot
415 */
416 public Color getPlotBackgroundColor() {
417 return plotBackgroundColor;
418 }
419
420 /**
421 * @return Background opacity of the general plot
422 */
423 public double getPlotBackgroundOpacity() {
424 return plotBackgroundOpacity;
425 }
426
427 /**
428 * @return Foreground opacity of the plot
429 */
430 public double getPlotForegroundOpacity() {
431 return plotForegroundOpacity;
432 }
433
434 /**
435 * @return Shadow Color of the Pie Chart
436 */
437 public Color getPieShadowColor() {
438 return pieShadowColor;
439 }
440 }