[Schmitzm-commits] r1192 - in trunk: src/schmitzm/io src/schmitzm/jfree/chart/style src_junit/schmitzm/io
scm-commit@wald.intevation.org
scm-commit at wald.intevation.org
Mon Nov 1 17:04:57 CET 2010
Author: alfonx
Date: 2010-11-01 17:04:55 +0100 (Mon, 01 Nov 2010)
New Revision: 1192
Modified:
trunk/src/schmitzm/io/IOUtil.java
trunk/src/schmitzm/jfree/chart/style/ChartStyleUtil.java
trunk/src/schmitzm/jfree/chart/style/ChartStyleXMLFactory.java
trunk/src_junit/schmitzm/io/IOUtilTest.java
Log:
Fixed
java.lang.NullPointerException
at schmitzm.jfree.chart.style.ChartStyleUtil.readStyleFromXML(ChartStyleUtil.java:75)
when parsing a ChartStyle from a URL tht does not point to a File.
Modified: trunk/src/schmitzm/io/IOUtil.java
===================================================================
--- trunk/src/schmitzm/io/IOUtil.java 2010-10-30 00:02:44 UTC (rev 1191)
+++ trunk/src/schmitzm/io/IOUtil.java 2010-11-01 16:04:55 UTC (rev 1192)
@@ -1153,4 +1153,15 @@
}
+ /**
+ * @return the "Filename" of an {@link URL}. e.g.
+ * <code>http://www.bahn.de/exterme/index.html?k=v</code> returns
+ * <code>index.html</code><br/>
+ * <code>http://www.bahn.de/exterme/</code> and
+ * <code>http://www.bahn.de/exterme<code> both return <code>exterme</code>
+ */
+ public static String getFilename(URL url) {
+ return new File(url.getPath()).getName();
+ }
+
}
Modified: trunk/src/schmitzm/jfree/chart/style/ChartStyleUtil.java
===================================================================
--- trunk/src/schmitzm/jfree/chart/style/ChartStyleUtil.java 2010-10-30 00:02:44 UTC (rev 1191)
+++ trunk/src/schmitzm/jfree/chart/style/ChartStyleUtil.java 2010-11-01 16:04:55 UTC (rev 1192)
@@ -48,7 +48,7 @@
private static final SAXBuilder SAX_BUILDER = new SAXBuilder();
/** Instance of {@link ChartStyleXMLFactory}. */
- public static final ChartStyleXMLFactory CHART_STYLE_FACTORY = new ChartStyleXMLFactory();
+ public static final ChartStyleXMLFactory<ChartStyle> CHART_STYLE_FACTORY = new ChartStyleXMLFactory<ChartStyle>();
/**
* Returns the chart style for a given string.
@@ -65,14 +65,19 @@
/**
* Reads a chart definition from {@link URL}. The URL must refers to a
* resource which contains exactly one chart definition as root element.<br>
- * <b>The filename is taken as chart ID</b>
+ * <b>The filename of the {@link URL} is taken as the chart ID</b>
*
* @param url
* refers to XML resource
*/
public static ChartStyle readStyleFromXML(URL url,
- ChartStyleXMLFactory factory) throws IOException {
- String id = IOUtil.urlToFile(url).getName();
+ ChartStyleXMLFactory<? extends ChartStyle> factory)
+ throws IOException {
+
+ // Makes!!!! NPE when URL not points to a File: String id =
+ // IOUtil.urlToFile(url).getName();
+ String id = IOUtil.getFilename(url);
+
return readStyleFromXML(url, id, factory);
}
@@ -89,7 +94,8 @@
* factory to create the style with
*/
public static ChartStyle readStyleFromXML(URL url, String id,
- ChartStyleXMLFactory factory) throws IOException {
+ ChartStyleXMLFactory<? extends ChartStyle> factory)
+ throws IOException {
try {
Document document = SAX_BUILDER.build(url);
return (factory.createStyleFromXML(document.getRootElement(), null));
@@ -98,5 +104,4 @@
}
}
-
}
Modified: trunk/src/schmitzm/jfree/chart/style/ChartStyleXMLFactory.java
===================================================================
--- trunk/src/schmitzm/jfree/chart/style/ChartStyleXMLFactory.java 2010-10-30 00:02:44 UTC (rev 1191)
+++ trunk/src/schmitzm/jfree/chart/style/ChartStyleXMLFactory.java 2010-11-01 16:04:55 UTC (rev 1192)
@@ -43,6 +43,7 @@
import java.util.List;
import java.util.Random;
+import org.geotools.data.DataUtilities;
import org.jdom.Document;
import org.jdom.Element;
import org.jfree.chart.plot.PlotOrientation;
@@ -52,685 +53,817 @@
/**
* This class defines a factory to create a chart style from XML.
+ *
* @author <a href="mailto:Martin.Schmitz at koeln.de">Martin Schmitz</a>
* @version 1.0
*/
public class ChartStyleXMLFactory<E extends ChartStyle> {
- /**
- * Reads a chart definition from XML element. The chart style ID
- * is taken from "id" attribute.
- * @param element the XML element
- * @param factory factory to create the style with
- */
- public E createStyleFromXML(Element element) {
- return( createStyleFromXML(element,null) );
- }
+ /**
+ * Reads a chart definition from XML element. The chart style ID is taken
+ * from "id" attribute.
+ *
+ * @param element
+ * the XML element
+ * @param factory
+ * factory to create the style with
+ */
+ public E createStyleFromXML(Element element) {
+ return (createStyleFromXML(element, null));
+ }
- /**
- * Reads a chart definition from XML element.
- * @param element the XML element
- * @param id the ID for the style (if {@code null} the
- * ID is taken from "id" attribute)
- */
- public E createStyleFromXML(Element element, String id) {
- if ( id == null )
- id = XMLUtil.getAttribute(element, "id", String.valueOf(new Random().nextInt()));
-
- // Determine the chart type from XML
- String typeStr = XMLUtil.getAttribute(element, "type");
- ChartType chartType = ChartStyleUtil.getChartType(typeStr);
+ /**
+ * Reads a chart definition from XML element.
+ *
+ * @param element
+ * the XML element
+ * @param id
+ * the ID for the style (if {@code null} the ID is taken from
+ * "id" attribute)
+ */
+ public E createStyleFromXML(Element element, String id) {
+ if (id == null)
+ id = XMLUtil.getAttribute(element, "id",
+ String.valueOf(new Random().nextInt()));
- // create default style for given type
- E chartStyle = createDefaultChartStyle(id, chartType);
- // apply the "rest" of the XML definition to style
- applyStyleFromXML(chartStyle, element);
-
- return chartStyle;
- }
-
- /**
- * Creates a default style for a chart type.
- * @param id a (unique) ID for the style
- * @param type a chart type
- */
- public E createDefaultChartStyle(String id, ChartType type) {
- switch ( type ) {
- case AREA:
- case LINE:
- case BAR:
- case POINT: return (E) new BasicChartStyle(id, type);
- case SCATTER: return (E) new ScatterChartStyle(id);
- }
- throw new UnsupportedOperationException("Style for this chart type not yet supported: "+type);
- }
-
- /**
- * Applies the chart style definition from XML (<b>except the type and id!!</b>)
- * to an existing {@link ChartStyle} object.
- * @param chartStyle an existing chart style ({@code null} not permitted!)
- * @param element element to read the properties from
- * @exception UnsupportedOperationException if {@code chartStyle} or {@code element}
- * is {@code null}
- */
- public void applyStyleFromXML(E chartStyle, Element element) {
- if ( chartStyle == null || element == null )
- throw new UnsupportedOperationException("Chart style and element must be given to apply the chart style from XML!");
-
- // Legend visible
- chartStyle.setLegend( XMLUtil.getBooleanAttribute(element,"legend",true) );
- chartStyle.setTooltips( XMLUtil.getBooleanAttribute(element,"tooltips",true) );
- chartStyle.setURLs( XMLUtil.getBooleanAttribute(element,"urls",true) );
-
- // Background color
- Color color = XMLUtil.getColorAttribute(element.getChild("background"),"paint");
- if ( color != null )
- chartStyle.setBackground( color );
-
- // Border around chart visible
- Boolean borderVisible = XMLUtil.getBooleanAttribute(element.getChild("border"),"visible");
- if ( borderVisible != null )
- chartStyle.setBorderVisible( borderVisible );
+ // Determine the chart type from XML
+ String typeStr = XMLUtil.getAttribute(element, "type");
+ ChartType chartType = ChartStyleUtil.getChartType(typeStr);
- // Plot orientation
- String orientationStr = XMLUtil.getAttribute(element,"orientation","vertical");
- if ("horizontal".equalsIgnoreCase(orientationStr))
- chartStyle.setOrientation( PlotOrientation.HORIZONTAL );
- else if ("vertical".equalsIgnoreCase(orientationStr))
- chartStyle.setOrientation( PlotOrientation.VERTICAL );
- else
- throw new UnsupportedOperationException("Unknown orientation definition: "+orientationStr);
+ // create default style for given type
+ E chartStyle = createDefaultChartStyle(id, chartType);
+ // apply the "rest" of the XML definition to style
+ applyStyleFromXML(chartStyle, element);
- // Title style
- if ( element.getChild("title") != null )
- chartStyle.setTitleStyle( createLabelStyleFromXML(element.getChild("title") ) );
- // Description style
- if ( element.getChild("desc") != null )
- chartStyle.setDescStyle( createLabelStyleFromXML(element.getChild("desc") ) );
- // Domain axis
- if ( element.getChild("domainAxis") != null )
- chartStyle.setAxisStyle( ChartStyle.DOMAIN_AXIS,createAxisStyleFromXML(element.getChild("domainAxis") ) );
- // Range axis
- if ( element.getChild("rangeAxis") != null )
- chartStyle.setAxisStyle( ChartStyle.RANGE_AXIS,createAxisStyleFromXML(element.getChild("rangeAxis") ) );
- // Second range axis
- if ( element.getChild("rangeAxis2") != null )
- chartStyle.setAxisStyle( ChartStyle.RANGE_AXIS2,createAxisStyleFromXML(element.getChild("rangeAxis2") ) );
+ return chartStyle;
+ }
- // Rendering of series
- List<Element> rendererElements = element.getChildren("renderer");
- for (int i=0; i<rendererElements.size(); i++)
- chartStyle.setRendererStyle(i,createRendererStyleFromXML(rendererElements.get(i)));
-
- // Plot
- if ( element.getChild("plot") != null )
- chartStyle.setPlotStyle( createPlotStyleFromXML(element.getChild("plot") ) );
+ /**
+ * Creates a default style for a chart type.
+ *
+ * @param id
+ * a (unique) ID for the style
+ * @param type
+ * a chart type
+ */
+ public E createDefaultChartStyle(String id, ChartType type) {
+ switch (type) {
+ case AREA:
+ case LINE:
+ case BAR:
+ case POINT:
+ return (E) new BasicChartStyle(id, type);
+ case SCATTER:
+ return (E) new ScatterChartStyle(id);
+ }
+ throw new UnsupportedOperationException(
+ "Style for this chart type not yet supported: " + type);
+ }
- // apply type specific properties
- if ( chartStyle instanceof BasicChartStyle )
- applyStyleFromXML((BasicChartStyle)chartStyle, element);
-
- }
-
- /**
- * Applies the {@link BasicChartStyle} properties from XML (<b>not the {@link ChartStyle}
- * properties!)</b>) to an existing {@link BasicChartStyle} object.
- * @param chartStyle an existing chart style ({@code null} not permitted!)
- * @param element element to read the properties from
- */
- private void applyStyleFromXML(BasicChartStyle chartStyle, Element element) {
- // stepped property
- Boolean stepped = XMLUtil.getBooleanAttribute(element, "stepped");
- if ( stepped != null )
- chartStyle.setStepped(stepped);
- // stacked property
- Boolean stacked = XMLUtil.getBooleanAttribute(element, "stacked");
- if ( stacked != null )
- chartStyle.setStacked(stacked);
- }
-
- /**
- * Reads the {@link ChartLabelStyle} properties from an XML element.
- * <ul>
- * <li>Attribute "paint": color of the label</li>
- * <li>Element content: the label title</li>
- * </ul>
- * @param element
- * @param style
- */
- private void applyLabelStyleFromXML(Element element, ChartLabelStyle style) {
- // color in attribute
- Color color = XMLUtil.getColorAttribute(element, "paint");
- if ( color != null )
- style.setPaint( color );
-
- // label in element data
- String label = element.getTextTrim();
- style.setLabel( label != null ? label : "" );
- }
-
- /**
- * Creates a {@link ChartLabelStyle} from XML element.
- * @param element an element
- */
- public ChartLabelStyle createLabelStyleFromXML(Element element) {
- ChartLabelStyle style = new ChartLabelStyle();
-
- // read the common ChartLabelStyle properties
- applyLabelStyleFromXML(element,style);
-
- return style;
- }
-
- /**
- * Creates a {@link ChartLabelStyle} from XML element.
- * @param element an element
- */
- public ChartAxisStyle createAxisStyleFromXML(Element element) {
- ChartAxisStyle style = new ChartAxisStyle();
-
- // read "visible" property from attribute
- Boolean visible = XMLUtil.getBooleanAttribute(element, "visible");
- if ( visible != null )
- style.setVisible( visible );
- // read "unit" property from attribute
- String unit = XMLUtil.getAttribute(element, "unit");
- if ( unit != null )
- style.setUnitString(unit);
+ /**
+ * Applies the chart style definition from XML (<b>except the type and
+ * id!!</b>) to an existing {@link ChartStyle} object.
+ *
+ * @param chartStyle
+ * an existing chart style ({@code null} not permitted!)
+ * @param element
+ * element to read the properties from
+ * @exception UnsupportedOperationException
+ * if {@code chartStyle} or {@code element} is {@code null}
+ */
+ public void applyStyleFromXML(E chartStyle, Element element) {
+ if (chartStyle == null || element == null)
+ throw new UnsupportedOperationException(
+ "Chart style and element must be given to apply the chart style from XML!");
- // read the axis title from child <title>
- Element axisTitleElem = element.getChild("title");
- applyLabelStyleFromXML(axisTitleElem,style);
- // read the axis title angle and font from child <title>
- Double labelAngle = XMLUtil.getDoubleAttribute(axisTitleElem, "angle");
- if ( labelAngle != null )
- style.setLabelAngle( labelAngle );
- Font labelFont = XMLUtil.getFontAttribute(axisTitleElem, "font");
- if ( labelFont != null )
- style.setLabelFont( labelFont );
+ // Legend visible
+ chartStyle.setLegend(XMLUtil.getBooleanAttribute(element, "legend",
+ true));
+ chartStyle.setTooltips(XMLUtil.getBooleanAttribute(element, "tooltips",
+ true));
+ chartStyle.setURLs(XMLUtil.getBooleanAttribute(element, "urls", true));
- // values font, color, format and angle from child element <valueLabels>
- Element valuesElem = element.getChild("valueLabels");
- if ( valuesElem != null ) {
- Color valuesColor = XMLUtil.getColorAttribute(valuesElem, "paint");
- if ( valuesColor != null )
- style.setValuesPaint( valuesColor );
- Font valuesFont = XMLUtil.getFontAttribute(valuesElem, "font");
- if ( valuesFont != null )
- style.setValuesFont( valuesFont );
- Double valuesAngle = XMLUtil.getDoubleAttribute(valuesElem, "valueAngle");
- if ( valuesAngle != null )
- style.setValuesAngle( valuesAngle );
- String formatStr = XMLUtil.getAttribute(valuesElem, "numberFormat");
- if ( formatStr != null )
- style.setValuesFormat(new DecimalFormat(formatStr) );
- else {
- formatStr = XMLUtil.getAttribute(valuesElem, "dateFormat");
- if ( formatStr != null )
- style.setValuesFormat(new SimpleDateFormat(formatStr) );
- }
- }
-
- return style;
- }
-
- /**
- * Creates a {@link ChartRendererStyle} from XML element.
- * @param element an element
- */
- public ChartRendererStyle createRendererStyleFromXML(Element element) {
- ChartRendererStyle style = new ChartRendererStyle();
-
- // read "margin" property from attribute
- Double margin = XMLUtil.getDoubleAttribute(element, "margin");
- if ( margin != null )
- style.setMargin( margin );
+ // Background color
+ Color color = XMLUtil.getColorAttribute(element.getChild("background"),
+ "paint");
+ if (color != null)
+ chartStyle.setBackground(color);
- //##### read default series properties ######
- // series paint
- Color defaultPaint = XMLUtil.getColorAttribute(element, "paint");
- if ( defaultPaint != null )
- style.setDefaultPaint(defaultPaint);
+ // Border around chart visible
+ Boolean borderVisible = XMLUtil.getBooleanAttribute(
+ element.getChild("border"), "visible");
+ if (borderVisible != null)
+ chartStyle.setBorderVisible(borderVisible);
- // series line width
- Integer defaultLineWidth = XMLUtil.getIntegerAttribute(element, "lineWidth");
- if ( defaultLineWidth != null )
- style.setDefaultLineWidth(defaultLineWidth);
+ // Plot orientation
+ String orientationStr = XMLUtil.getAttribute(element, "orientation",
+ "vertical");
+ if ("horizontal".equalsIgnoreCase(orientationStr))
+ chartStyle.setOrientation(PlotOrientation.HORIZONTAL);
+ else if ("vertical".equalsIgnoreCase(orientationStr))
+ chartStyle.setOrientation(PlotOrientation.VERTICAL);
+ else
+ throw new UnsupportedOperationException(
+ "Unknown orientation definition: " + orientationStr);
- // series line dash attributes
- Object[] defaultLineDashAttrObj = XMLUtil.getArrayAttribute(element, "lineDash");
- Float[] defaultLineDashAttr = XMLUtil.convertObjectToFloat(defaultLineDashAttrObj);
- if ( defaultLineDashAttr != null )
- style.setDefaultLineDashAttributes(defaultLineDashAttr);
-
- // item label visibility
- Boolean defaultLabelsVisible = XMLUtil.getBooleanAttribute(element, "itemLabelsVisible");
- if ( defaultLabelsVisible != null )
- style.setDefaultItemLabelsVisible(defaultLabelsVisible);
+ // Title style
+ if (element.getChild("title") != null)
+ chartStyle.setTitleStyle(createLabelStyleFromXML(element
+ .getChild("title")));
+ // Description style
+ if (element.getChild("desc") != null)
+ chartStyle.setDescStyle(createLabelStyleFromXML(element
+ .getChild("desc")));
+ // Domain axis
+ if (element.getChild("domainAxis") != null)
+ chartStyle.setAxisStyle(ChartStyle.DOMAIN_AXIS,
+ createAxisStyleFromXML(element.getChild("domainAxis")));
+ // Range axis
+ if (element.getChild("rangeAxis") != null)
+ chartStyle.setAxisStyle(ChartStyle.RANGE_AXIS,
+ createAxisStyleFromXML(element.getChild("rangeAxis")));
+ // Second range axis
+ if (element.getChild("rangeAxis2") != null)
+ chartStyle.setAxisStyle(ChartStyle.RANGE_AXIS2,
+ createAxisStyleFromXML(element.getChild("rangeAxis2")));
- // item shapes visibility
- Boolean defaultShapesVisible = XMLUtil.getBooleanAttribute(element, "shapesVisible");
- if ( defaultShapesVisible != null )
- style.setDefaultShapesVisible(defaultShapesVisible);
-
- // legend visibility
- Boolean defaultLegendVisible = XMLUtil.getBooleanAttribute(element, "legendVisible");
- if ( defaultLegendVisible != null )
- style.setDefaultLegendVisible(defaultLegendVisible);
-
- // legend label
- String defaultLegendLabel = XMLUtil.getAttribute(element, "legendLabel");
- if ( defaultLegendLabel != null )
- style.setDefaultLegendLabel(new ChartLabelStyle(defaultLegendLabel));
-
- // legend tooltip
- String defaultLegendTooltip = XMLUtil.getAttribute(element, "legendTooltip");
- if ( defaultLegendTooltip != null )
- style.setDefaultLegendTooltip(new ChartLabelStyle(defaultLegendTooltip));
-
+ // Rendering of series
+ List<Element> rendererElements = element.getChildren("renderer");
+ for (int i = 0; i < rendererElements.size(); i++)
+ chartStyle.setRendererStyle(i,
+ createRendererStyleFromXML(rendererElements.get(i)));
- //##### read explicit series properties ######
- List<Element> seriesElements = element.getChildren("series");
- for (int i=0; i<seriesElements.size(); i++) {
- Element seriesElement = seriesElements.get(i);
- // series paint
- String seriesKey = XMLUtil.getAttribute(seriesElement, "seriesKey");
- if ( seriesKey != null )
- style.setSeriesKey(i, seriesKey);
+ // Plot
+ if (element.getChild("plot") != null)
+ chartStyle.setPlotStyle(createPlotStyleFromXML(element
+ .getChild("plot")));
- // series paint
- Color seriesPaint = XMLUtil.getColorAttribute(seriesElement, "paint");
- if ( seriesPaint != null )
- style.setSeriesPaint(i, seriesPaint);
+ // apply type specific properties
+ if (chartStyle instanceof BasicChartStyle)
+ applyStyleFromXML((BasicChartStyle) chartStyle, element);
- // series line width
- Integer seriesLineWidth = XMLUtil.getIntegerAttribute(seriesElement, "lineWidth");
- if ( seriesLineWidth != null )
- style.setSeriesLineWidth(i, seriesLineWidth);
+ }
- // series line dash attributes
- Object[] seriesLineDashAttrObj = XMLUtil.getArrayAttribute(seriesElement, "lineDash");
- Float[] seriesLineDashAttr = XMLUtil.convertObjectToFloat(seriesLineDashAttrObj);
- if ( seriesLineDashAttr != null )
- style.setSeriesLineDashAttributes(i, seriesLineDashAttr);
+ /**
+ * Applies the {@link BasicChartStyle} properties from XML (<b>not the
+ * {@link ChartStyle} properties!)</b>) to an existing
+ * {@link BasicChartStyle} object.
+ *
+ * @param chartStyle
+ * an existing chart style ({@code null} not permitted!)
+ * @param element
+ * element to read the properties from
+ */
+ private void applyStyleFromXML(BasicChartStyle chartStyle, Element element) {
+ // stepped property
+ Boolean stepped = XMLUtil.getBooleanAttribute(element, "stepped");
+ if (stepped != null)
+ chartStyle.setStepped(stepped);
+ // stacked property
+ Boolean stacked = XMLUtil.getBooleanAttribute(element, "stacked");
+ if (stacked != null)
+ chartStyle.setStacked(stacked);
+ }
- // item label visibility
- Boolean labelsVisible = XMLUtil.getBooleanAttribute(seriesElement, "itemLabelsVisible");
- if ( labelsVisible != null )
- style.setSeriesItemLabelsVisible(i, labelsVisible);
+ /**
+ * Reads the {@link ChartLabelStyle} properties from an XML element.
+ * <ul>
+ * <li>Attribute "paint": color of the label</li>
+ * <li>Element content: the label title</li>
+ * </ul>
+ *
+ * @param element
+ * @param style
+ */
+ private void applyLabelStyleFromXML(Element element, ChartLabelStyle style) {
+ // color in attribute
+ Color color = XMLUtil.getColorAttribute(element, "paint");
+ if (color != null)
+ style.setPaint(color);
- // item shapes visibility
- Boolean shapesVisible = XMLUtil.getBooleanAttribute(seriesElement, "shapesVisible");
- if ( shapesVisible != null )
- style.setSeriesShapesVisible(i, shapesVisible);
-
- // legend visibility
- Boolean legendVisible = XMLUtil.getBooleanAttribute(seriesElement, "legendVisible");
- if ( legendVisible != null )
- style.setSeriesLegendVisible(i, legendVisible);
-
- // legend label
- String legendLabel = XMLUtil.getAttribute(seriesElement, "legendLabel");
- if ( legendLabel != null )
- style.setSeriesLegendLabel(i, new ChartLabelStyle(legendLabel));
-
- // legend tooltip
- String legendTooltip = XMLUtil.getAttribute(seriesElement, "legendTooltip");
- if ( legendTooltip != null )
- style.setSeriesLegendTooltip(i, new ChartLabelStyle(legendTooltip));
- }
- return style;
- }
+ // label in element data
+ String label = element.getTextTrim();
+ style.setLabel(label != null ? label : "");
+ }
- /**
- * Creates a {@link ChartPlotStyle} from XML element.
- * @param element an element
- */
- public ChartPlotStyle createPlotStyleFromXML(Element element) {
- ChartPlotStyle style = new ChartPlotStyle();
+ /**
+ * Creates a {@link ChartLabelStyle} from XML element.
+ *
+ * @param element
+ * an element
+ */
+ public ChartLabelStyle createLabelStyleFromXML(Element element) {
+ ChartLabelStyle style = new ChartLabelStyle();
- // Axis origin centration
- style.setCenterOriginSymetrically( XMLUtil.getBooleanAttribute(element, "centerAxisOrigin", false) );
-
- // foreground transparency
- Element foregroundElem = element.getChild("foreground");
- if ( foregroundElem != null ) {
- Float alpha = XMLUtil.getFloatAttribute(foregroundElem, "alpha");
- if ( alpha != null )
- style.setForegroundAlpha(alpha);
- }
-
- // background color and transparency
- Element backegroundElem = element.getChild("background");
- if ( backegroundElem != null ) {
- Float alpha = XMLUtil.getFloatAttribute(backegroundElem, "alpha");
- if ( alpha != null )
- style.setBackgroundAlpha(alpha);
- Color color = XMLUtil.getColorAttribute(backegroundElem, "paint");
- if ( color != null )
- style.setBackgroundPaint(color);
- }
-
- // Insets
- Element insetsElem = element.getChild("insets");
- if ( insetsElem != null ) {
- double top = XMLUtil.getDoubleAttribute(insetsElem, "top", 0.0);
- double bottom = XMLUtil.getDoubleAttribute(insetsElem, "bottom", 0.0);
- double left = XMLUtil.getDoubleAttribute(insetsElem, "left", 0.0);
- double right = XMLUtil.getDoubleAttribute(insetsElem, "right", 0.0);
- style.setInsets( new RectangleInsets(top,left,bottom,right) );
- }
-
- // domain grid line color and visibility
- Element domainGridElem = element.getChild("domainGridline");
- if ( domainGridElem != null ) {
- Boolean visible = XMLUtil.getBooleanAttribute(domainGridElem, "visible");
- if ( visible != null )
- style.setDomainGridlineVisible(visible);
- Color color = XMLUtil.getColorAttribute(domainGridElem, "paint");
- if ( color != null )
- style.setDomainGridlinePaint(color);
- }
+ // read the common ChartLabelStyle properties
+ applyLabelStyleFromXML(element, style);
- // range grid line color and visibility
- Element rangeGridElem = element.getChild("rangeGridline");
- if ( rangeGridElem != null ) {
- Boolean visible = XMLUtil.getBooleanAttribute(rangeGridElem, "visible");
- if ( visible != null )
- style.setRangeGridlineVisible(visible);
- Color color = XMLUtil.getColorAttribute(rangeGridElem, "paint");
- if ( color != null )
- style.setRangeGridlinePaint(color);
- }
-
- // crosshair lines color and visiblilty
- Element crosshairElem = element.getChild("crosshairLines");
- if ( crosshairElem != null ) {
- Boolean visible = XMLUtil.getBooleanAttribute(crosshairElem, "visible");
- if ( visible != null )
- style.setCrosshairVisible(visible);
- Color color = XMLUtil.getColorAttribute(crosshairElem, "paint");
- if ( color != null )
- style.setCrosshairPaint(color);
- }
+ return style;
+ }
- return style;
- }
-
- /**
- * Stores a {@link ChartStyle} to an XML file.
- * @param style style to store
- * @param rootElementName name for the root element (if {@code null} "ChartType" is
- * used)
- * @param filePath file to store the style to
- */
- public void writeStyleToFile(E style, String rootElementName, File filePath) throws IOException {
- OutputStreamWriter out = new OutputStreamWriter(
- new FileOutputStream(filePath), "utf-8");
- // Create XML-Document
- Element rootElem = createElementFromStyle(style, rootElementName);
- Document document = new Document(rootElem);
- // Write document
- XMLUtil.XML_OUTPUTTER.output(document,out);
- out.flush();
- out.close();
+ /**
+ * Creates a {@link ChartLabelStyle} from XML element.
+ *
+ * @param element
+ * an element
+ */
+ public ChartAxisStyle createAxisStyleFromXML(Element element) {
+ ChartAxisStyle style = new ChartAxisStyle();
- }
+ // read "visible" property from attribute
+ Boolean visible = XMLUtil.getBooleanAttribute(element, "visible");
+ if (visible != null)
+ style.setVisible(visible);
+ // read "unit" property from attribute
+ String unit = XMLUtil.getAttribute(element, "unit");
+ if (unit != null)
+ style.setUnitString(unit);
- /**
- * Reads a {@link ChartStyle} from an XML file.
- * @param filePath file to read the style from
- */
- public E readStyleFromFile(File filePath) throws IOException {
- return (E)ChartStyleUtil.readStyleFromXML(filePath.toURI().toURL(), this);
- }
-
- /**
- * Creates a XML {@link Element} representing the {@link ChartStyle}.
- * @param style style to create an element for
- * @param rootElementName name for the root element (if {@code null} "ChartStyle" is
- * used)
- */
- public Element createElementFromStyle(E style, String rootElementName) {
- if ( rootElementName == null )
- rootElementName = "ChartStyle";
- Element root = new Element(rootElementName);
- // write common attributes
- root.setAttribute("id", style.getID());
- root.setAttribute("type", style.getType().toString());
- root.setAttribute("legend", String.valueOf( style.hasLegend() ));
- root.setAttribute("tooltips", String.valueOf( style.hasTooltips() ));
- root.setAttribute("urls", String.valueOf( style.hasURLs() ));
-
- // Attribute for plot orientation
- if ( PlotOrientation.HORIZONTAL.equals( style.getOrientation() ) )
- root.setAttribute("orientation", "horizontal" );
- else if ( PlotOrientation.VERTICAL.equals( style.getOrientation() ) )
- root.setAttribute("orientation", "vertical" );
-
- // Element for background
- addChildToElement(root, "background", false, "paint", style.getBackground() );
-
- // Element for border
- addChildToElement(root, "border", false, "visible", style.isBorderVisible() );
-
- // Chart title
- addLabelChildToElement(root, "title", style.getTitleStyle() );
+ // read the axis title from child <title>
+ Element axisTitleElem = element.getChild("title");
+ applyLabelStyleFromXML(axisTitleElem, style);
+ // read the axis title angle and font from child <title>
+ Double labelAngle = XMLUtil.getDoubleAttribute(axisTitleElem, "angle");
+ if (labelAngle != null)
+ style.setLabelAngle(labelAngle);
+ Font labelFont = XMLUtil.getFontAttribute(axisTitleElem, "font");
+ if (labelFont != null)
+ style.setLabelFont(labelFont);
- // Chart description
- addLabelChildToElement(root, "desc", style.getDescStyle() );
-
- // Axis
- if ( style.getAxisStyle(0) != null )
- addAxisChildToElement(root, "domainAxis", style.getAxisStyle(0) );
- if ( style.getAxisStyle(1) != null )
- addAxisChildToElement(root, "rangeAxis", style.getAxisStyle(1) );
- if ( style.getAxisStyle(2) != null )
- addAxisChildToElement(root, "rangeAxis2", style.getAxisStyle(2) );
-
- // Renderers
- for (int i=0; i<style.getRendererCount(); i++)
- addRendererChildToElement(root, style.getRendererStyle(i));
-
- // Plot
- if ( style.getPlotStyle() != null ) {
- Element plotElem = addChildToElement(root, "plot");
- ChartPlotStyle plotStyle = style.getPlotStyle();
-
- XMLUtil.setAttribute(plotElem, "centerAxisOrigin", plotStyle.isCenterOriginSymetrically(), false);
-
- addChildToElement(plotElem, "foreground", false,
- "alpha", plotStyle.getForegroundAlpha()
- );
- addChildToElement(plotElem, "background", false,
- "alpha", plotStyle.getBackgroundAlpha(),
- "paint", plotStyle.getBackgroundPaint()
- );
- if ( plotStyle.getInsets() != null )
- addChildToElement(plotElem, "insets", false,
- "top", plotStyle.getInsets().getTop(),
- "bottom", plotStyle.getInsets().getBottom(),
- "left", plotStyle.getInsets().getLeft(),
- "right", plotStyle.getInsets().getRight()
- );
- addChildToElement(plotElem, "domainGridline", false,
- "visible", plotStyle.isDomainGridlineVisible(),
- "paint", plotStyle.getDomainGridlinePaint()
- );
- addChildToElement(plotElem, "rangeGridline", false,
- "visible", plotStyle.isRangeGridlineVisible(),
- "paint", plotStyle.getRangeGridlinePaint()
- );
- addChildToElement(plotElem, "crosshairLines", false,
- "visible", plotStyle.isCrosshairVisible(),
- "paint", plotStyle.getCrosshairPaint()
- );
- }
-
- // Type specific style properties
- if ( style instanceof BasicChartStyle ) {
- root.setAttribute("stepped", String.valueOf( ((BasicChartStyle)style).isStepped() ));
- root.setAttribute("stacked", String.valueOf( ((BasicChartStyle)style).isStacked() ));
- }
- if ( style instanceof ScatterChartStyle )
- root.setAttribute("regrLineVisible", String.valueOf( ((ScatterChartStyle)style).isRegressionLineVisible() ));
-
-
- return root;
- }
-
- /**
- * Creates an child element with no attributes. If an element is given,
- * the new child is automatically added.
- * @param element if not {@code null} the new element is added as child to
- * this element
- * @param childName name of the new child
- * @return the new child element
- */
- public static Element addChildToElement(Element element, String childName) {
- return addChildToElement(element,childName,true);
- }
-
- /**
- * Creates an child element and sets some attributes. If an element is given,
- * the new child is automatically added.
- * @param element if not {@code null} the new element is added as child to
- * this element
- * @param childName name of the new child
- * @param inclNullAttr if {@code false} attributes with value {@code null}
- * are not set; if ALL attributes are {@code null} also
- * the child is not created!
- * @param attributes attribute/value pairs for the new child element
- * @return the new child element
- */
- public static Element addChildToElement(Element element, String childName, boolean inclNullAttr, Object... attributes) {
- // Create child
- Element child = new Element(childName);
- // Set child attributes
- boolean anyAttrSet = false;
- for (int i=0; i<attributes.length; i+=2) {
- String attrName = attributes[i].toString();
- Object attrValue = attributes[i+1];
- anyAttrSet |= XMLUtil.setAttribute(child, attrName, attrValue, inclNullAttr);
- }
- // if null attributes should be ignored and NO attribute is
- // actually set, also the child is not created!
- if ( !anyAttrSet && !inclNullAttr )
- child = null;
-
- // If element is given, the child is added
- if ( element != null && child != null )
- element.addContent(child);
+ // values font, color, format and angle from child element <valueLabels>
+ Element valuesElem = element.getChild("valueLabels");
+ if (valuesElem != null) {
+ Color valuesColor = XMLUtil.getColorAttribute(valuesElem, "paint");
+ if (valuesColor != null)
+ style.setValuesPaint(valuesColor);
+ Font valuesFont = XMLUtil.getFontAttribute(valuesElem, "font");
+ if (valuesFont != null)
+ style.setValuesFont(valuesFont);
+ Double valuesAngle = XMLUtil.getDoubleAttribute(valuesElem,
+ "valueAngle");
+ if (valuesAngle != null)
+ style.setValuesAngle(valuesAngle);
+ String formatStr = XMLUtil.getAttribute(valuesElem, "numberFormat");
+ if (formatStr != null)
+ style.setValuesFormat(new DecimalFormat(formatStr));
+ else {
+ formatStr = XMLUtil.getAttribute(valuesElem, "dateFormat");
+ if (formatStr != null)
+ style.setValuesFormat(new SimpleDateFormat(formatStr));
+ }
+ }
- return child;
- }
+ return style;
+ }
- /**
- * Creates an child element for a {@link ChartLabelStyle}. Besides the attributes
- * the label title is set as element content. If an element is given, the new child
- * is automatically added.
- * @param element if not {@code null} the new element is added as child to
- * this element
- * @param titleChildName name of the new child
- * @param labelStyle the label style to create the element for
- * @return the new child element; {@code null} if {@code labelStyle} is {@code null}
- */
- private static Element addLabelChildToElement(Element element, String titleChildName, ChartLabelStyle labelStyle) {
- if ( labelStyle == null )
- return null;
- // Create and add child with paint attribute
- Element labelElem = addChildToElement(element, titleChildName);
- XMLUtil.setNotNullAttribute(labelElem, "paint", labelStyle.getPaint());
- // Set title to element content
- labelElem.setText( labelStyle.getLabelTranslation().toOneLine() );
+ /**
+ * Creates a {@link ChartRendererStyle} from XML element.
+ *
+ * @param element
+ * an element
+ */
+ public ChartRendererStyle createRendererStyleFromXML(Element element) {
+ ChartRendererStyle style = new ChartRendererStyle();
- return labelElem;
- }
+ // read "margin" property from attribute
+ Double margin = XMLUtil.getDoubleAttribute(element, "margin");
+ if (margin != null)
+ style.setMargin(margin);
- /**
- * Creates an child element for a {@link ChartAxisStyle}.
- * If an element is given, the new child is automatically added.
- * @param element if not {@code null} the new element is added as child to
- * this element
- * @param axisChildName name of the new child
- * @param axisStyle the axis style to create the element for
- * @return the new child element; {@code null} if {@code axisStyle} is {@code null}
- */
- private static Element addAxisChildToElement(Element element, String axisChildName, ChartAxisStyle axisStyle) {
- if ( axisStyle == null )
- return null;
-
- // Create and add child with visible and unit attribute
- Element axisElem = addChildToElement(element, axisChildName);
- XMLUtil.setNotNullAttribute(axisElem, "visible", axisStyle.isVisible());
- XMLUtil.setNotNullAttribute(axisElem, "unit", axisStyle.getUnitString());
-
- // Create and add child <title>
- Element titleElem = addLabelChildToElement(axisElem, "title", axisStyle);
- // Add angle and font attribute to <title> element
- XMLUtil.setNotNullAttribute(titleElem, "font", axisStyle.getLabelFont());
- XMLUtil.setNotNullAttribute(titleElem, "angle", axisStyle.getLabelAngle());
-
- // Create and add child <valueLabels>
- Element valueLabelsElem = addChildToElement(axisElem, "valueLabels");
- XMLUtil.setNotNullAttribute(valueLabelsElem, "paint", axisStyle.getValuesPaint());
- XMLUtil.setNotNullAttribute(valueLabelsElem, "font", axisStyle.getValuesFont());
- XMLUtil.setNotNullAttribute(valueLabelsElem, "valueAngle", axisStyle.getValuesAngle());
+ // ##### read default series properties ######
+ // series paint
+ Color defaultPaint = XMLUtil.getColorAttribute(element, "paint");
+ if (defaultPaint != null)
+ style.setDefaultPaint(defaultPaint);
- Format format = axisStyle.getValuesFormat();
- if ( format instanceof DateFormat )
- valueLabelsElem.setAttribute("dateFormat", format.toString());
- if ( format instanceof NumberFormat )
- valueLabelsElem.setAttribute("numberFormat", format.toString());
-
- return axisElem;
- }
+ // series line width
+ Integer defaultLineWidth = XMLUtil.getIntegerAttribute(element,
+ "lineWidth");
+ if (defaultLineWidth != null)
+ style.setDefaultLineWidth(defaultLineWidth);
- /**
- * Creates an child element for a {@link ChartRendererStyle}.
- * If an element is given, the new child is automatically added.
- * @param element if not {@code null} the new element is added as child to
- * this element
- * @param rendererStyle the renderer style to create the element for
- * @return the new child element; if {@code rendererStyle} is {@code null} an empty
- * element is returned and added (so that the order of the renderers according
- * to the dataset is preserved)
- */
- private static Element addRendererChildToElement(Element element, ChartRendererStyle rendererStyle) {
- // Create and add child with visible attribute
- Element rendererElem = addChildToElement(element, "renderer");
-
- if ( rendererStyle != null ) {
- // Margin attribute
- XMLUtil.setAttribute(rendererElem, "margin", rendererStyle.getMargin(),true);
- // Attributes for default series properties
- XMLUtil.setNotNullAttribute(rendererElem, "paint", rendererStyle.getDefaultPaint());
- XMLUtil.setNotNullAttribute(rendererElem, "lineWidth", rendererStyle.getDefaultLineWidth());
- XMLUtil.setNotNullAttribute(rendererElem, "lineDash", XMLUtil.convertArrayToSeparatedString(rendererStyle.getDefaultLineDashAttibutes()));
- XMLUtil.setNotNullAttribute(rendererElem, "itemLabelsVisible", rendererStyle.isDefaultItemLabelsVisible());
- XMLUtil.setNotNullAttribute(rendererElem, "shapesVisible", rendererStyle.isDefaultShapesVisible());
- XMLUtil.setNotNullAttribute(rendererElem, "legendVisible", rendererStyle.isDefaultLegendVisible());
- if ( rendererStyle.getDefaultLegendLabel() != null )
- XMLUtil.setNotNullAttribute(rendererElem, "legendLabel", rendererStyle.getDefaultLegendLabel().getLabelTranslation());
- if ( rendererStyle.getDefaultLegendTooltip() != null )
- XMLUtil.setNotNullAttribute(rendererElem, "legendTooltip", rendererStyle.getDefaultLegendTooltip().getLabelTranslation());
-
- // Create <series> child for all series
- for ( int i=0; i<rendererStyle.getSeriesCount(); i++) {
- Element seriesElem = addChildToElement(rendererElem, "series");
- XMLUtil.setNotNullAttribute(seriesElem, "seriesKey", rendererStyle.getSeriesKey(i));
- XMLUtil.setNotNullAttribute(seriesElem, "paint", rendererStyle.getSeriesPaint(i));
- XMLUtil.setNotNullAttribute(seriesElem, "lineWidth", rendererStyle.getSeriesLineWidth(i));
- XMLUtil.setNotNullAttribute(seriesElem, "lineDash", XMLUtil.convertArrayToSeparatedString(rendererStyle.getSeriesLineDashAttibutes(i)));
- XMLUtil.setNotNullAttribute(seriesElem, "itemLabelsVisible", rendererStyle.isSeriesItemLabelsVisible(i));
- XMLUtil.setNotNullAttribute(seriesElem, "shapesVisible", rendererStyle.isSeriesShapesVisible(i));
- XMLUtil.setNotNullAttribute(seriesElem, "legendVisible", rendererStyle.isSeriesLegendVisible(i));
- if ( rendererStyle.getSeriesLegendLabel(i) != null )
- XMLUtil.setNotNullAttribute(seriesElem, "legendLabel", rendererStyle.getSeriesLegendLabel(i).getLabelTranslation());
- if ( rendererStyle.getSeriesLegendTooltip(i) != null )
- XMLUtil.setNotNullAttribute(seriesElem, "legendTooltip", rendererStyle.getSeriesLegendTooltip(i).getLabelTranslation());
- }
- }
-
- return rendererElem;
- }
+ // series line dash attributes
+ Object[] defaultLineDashAttrObj = XMLUtil.getArrayAttribute(element,
+ "lineDash");
+ Float[] defaultLineDashAttr = XMLUtil
+ .convertObjectToFloat(defaultLineDashAttrObj);
+ if (defaultLineDashAttr != null)
+ style.setDefaultLineDashAttributes(defaultLineDashAttr);
+
+ // item label visibility
+ Boolean defaultLabelsVisible = XMLUtil.getBooleanAttribute(element,
+ "itemLabelsVisible");
+ if (defaultLabelsVisible != null)
+ style.setDefaultItemLabelsVisible(defaultLabelsVisible);
+
+ // item shapes visibility
+ Boolean defaultShapesVisible = XMLUtil.getBooleanAttribute(element,
+ "shapesVisible");
+ if (defaultShapesVisible != null)
+ style.setDefaultShapesVisible(defaultShapesVisible);
+
+ // legend visibility
+ Boolean defaultLegendVisible = XMLUtil.getBooleanAttribute(element,
+ "legendVisible");
+ if (defaultLegendVisible != null)
+ style.setDefaultLegendVisible(defaultLegendVisible);
+
+ // legend label
+ String defaultLegendLabel = XMLUtil
+ .getAttribute(element, "legendLabel");
+ if (defaultLegendLabel != null)
+ style.setDefaultLegendLabel(new ChartLabelStyle(defaultLegendLabel));
+
+ // legend tooltip
+ String defaultLegendTooltip = XMLUtil.getAttribute(element,
+ "legendTooltip");
+ if (defaultLegendTooltip != null)
+ style.setDefaultLegendTooltip(new ChartLabelStyle(
+ defaultLegendTooltip));
+
+ // ##### read explicit series properties ######
+ List<Element> seriesElements = element.getChildren("series");
+ for (int i = 0; i < seriesElements.size(); i++) {
+ Element seriesElement = seriesElements.get(i);
+ // series paint
+ String seriesKey = XMLUtil.getAttribute(seriesElement, "seriesKey");
+ if (seriesKey != null)
+ style.setSeriesKey(i, seriesKey);
+
+ // series paint
+ Color seriesPaint = XMLUtil.getColorAttribute(seriesElement,
+ "paint");
+ if (seriesPaint != null)
+ style.setSeriesPaint(i, seriesPaint);
+
+ // series line width
+ Integer seriesLineWidth = XMLUtil.getIntegerAttribute(
+ seriesElement, "lineWidth");
+ if (seriesLineWidth != null)
+ style.setSeriesLineWidth(i, seriesLineWidth);
+
+ // series line dash attributes
+ Object[] seriesLineDashAttrObj = XMLUtil.getArrayAttribute(
+ seriesElement, "lineDash");
+ Float[] seriesLineDashAttr = XMLUtil
+ .convertObjectToFloat(seriesLineDashAttrObj);
+ if (seriesLineDashAttr != null)
+ style.setSeriesLineDashAttributes(i, seriesLineDashAttr);
+
+ // item label visibility
+ Boolean labelsVisible = XMLUtil.getBooleanAttribute(seriesElement,
+ "itemLabelsVisible");
+ if (labelsVisible != null)
+ style.setSeriesItemLabelsVisible(i, labelsVisible);
+
+ // item shapes visibility
+ Boolean shapesVisible = XMLUtil.getBooleanAttribute(seriesElement,
+ "shapesVisible");
+ if (shapesVisible != null)
+ style.setSeriesShapesVisible(i, shapesVisible);
+
+ // legend visibility
+ Boolean legendVisible = XMLUtil.getBooleanAttribute(seriesElement,
+ "legendVisible");
+ if (legendVisible != null)
+ style.setSeriesLegendVisible(i, legendVisible);
+
+ // legend label
+ String legendLabel = XMLUtil.getAttribute(seriesElement,
+ "legendLabel");
+ if (legendLabel != null)
+ style.setSeriesLegendLabel(i, new ChartLabelStyle(legendLabel));
+
+ // legend tooltip
+ String legendTooltip = XMLUtil.getAttribute(seriesElement,
+ "legendTooltip");
+ if (legendTooltip != null)
+ style.setSeriesLegendTooltip(i, new ChartLabelStyle(
+ legendTooltip));
+ }
+ return style;
+ }
+
+ /**
+ * Creates a {@link ChartPlotStyle} from XML element.
+ *
+ * @param element
+ * an element
+ */
+ public ChartPlotStyle createPlotStyleFromXML(Element element) {
+ ChartPlotStyle style = new ChartPlotStyle();
+
+ // Axis origin centration
+ style.setCenterOriginSymetrically(XMLUtil.getBooleanAttribute(element,
+ "centerAxisOrigin", false));
+
+ // foreground transparency
+ Element foregroundElem = element.getChild("foreground");
+ if (foregroundElem != null) {
+ Float alpha = XMLUtil.getFloatAttribute(foregroundElem, "alpha");
+ if (alpha != null)
+ style.setForegroundAlpha(alpha);
+ }
+
+ // background color and transparency
+ Element backegroundElem = element.getChild("background");
+ if (backegroundElem != null) {
+ Float alpha = XMLUtil.getFloatAttribute(backegroundElem, "alpha");
+ if (alpha != null)
+ style.setBackgroundAlpha(alpha);
+ Color color = XMLUtil.getColorAttribute(backegroundElem, "paint");
+ if (color != null)
+ style.setBackgroundPaint(color);
+ }
+
+ // Insets
+ Element insetsElem = element.getChild("insets");
+ if (insetsElem != null) {
+ double top = XMLUtil.getDoubleAttribute(insetsElem, "top", 0.0);
+ double bottom = XMLUtil.getDoubleAttribute(insetsElem, "bottom",
+ 0.0);
+ double left = XMLUtil.getDoubleAttribute(insetsElem, "left", 0.0);
+ double right = XMLUtil.getDoubleAttribute(insetsElem, "right", 0.0);
+ style.setInsets(new RectangleInsets(top, left, bottom, right));
+ }
+
+ // domain grid line color and visibility
+ Element domainGridElem = element.getChild("domainGridline");
+ if (domainGridElem != null) {
+ Boolean visible = XMLUtil.getBooleanAttribute(domainGridElem,
+ "visible");
+ if (visible != null)
+ style.setDomainGridlineVisible(visible);
+ Color color = XMLUtil.getColorAttribute(domainGridElem, "paint");
+ if (color != null)
+ style.setDomainGridlinePaint(color);
+ }
+
+ // range grid line color and visibility
+ Element rangeGridElem = element.getChild("rangeGridline");
+ if (rangeGridElem != null) {
+ Boolean visible = XMLUtil.getBooleanAttribute(rangeGridElem,
+ "visible");
+ if (visible != null)
+ style.setRangeGridlineVisible(visible);
+ Color color = XMLUtil.getColorAttribute(rangeGridElem, "paint");
+ if (color != null)
+ style.setRangeGridlinePaint(color);
+ }
+
+ // crosshair lines color and visiblilty
+ Element crosshairElem = element.getChild("crosshairLines");
+ if (crosshairElem != null) {
+ Boolean visible = XMLUtil.getBooleanAttribute(crosshairElem,
+ "visible");
+ if (visible != null)
+ style.setCrosshairVisible(visible);
+ Color color = XMLUtil.getColorAttribute(crosshairElem, "paint");
+ if (color != null)
+ style.setCrosshairPaint(color);
+ }
+
+ return style;
+ }
+
+ /**
+ * Stores a {@link ChartStyle} to an XML file.
+ *
+ * @param style
+ * style to store
+ * @param rootElementName
+ * name for the root element (if {@code null} "ChartType" is
+ * used)
+ * @param filePath
+ * file to store the style to
+ */
+ public void writeStyleToFile(E style, String rootElementName, File filePath)
+ throws IOException {
+ OutputStreamWriter out = new OutputStreamWriter(new FileOutputStream(
+ filePath), "utf-8");
+ // Create XML-Document
+ Element rootElem = createElementFromStyle(style, rootElementName);
+ Document document = new Document(rootElem);
+ // Write document
+ XMLUtil.XML_OUTPUTTER.output(document, out);
+ out.flush();
+ out.close();
+
+ }
+
+ /**
+ * Reads a {@link ChartStyle} from an XML file.
+ *
+ * @param file
+ * file to read the style from
+ */
+ public E readStyleFromFile(File file) throws IOException {
+ return (E) ChartStyleUtil.readStyleFromXML(
+ DataUtilities.fileToURL(file), this);
+ }
+
+ /**
+ * Creates a XML {@link Element} representing the {@link ChartStyle}.
+ *
+ * @param style
+ * style to create an element for
+ * @param rootElementName
+ * name for the root element (if {@code null} "ChartStyle" is
+ * used)
+ */
+ public Element createElementFromStyle(E style, String rootElementName) {
+ if (rootElementName == null)
+ rootElementName = "ChartStyle";
+ Element root = new Element(rootElementName);
+ // write common attributes
+ root.setAttribute("id", style.getID());
+ root.setAttribute("type", style.getType().toString());
+ root.setAttribute("legend", String.valueOf(style.hasLegend()));
+ root.setAttribute("tooltips", String.valueOf(style.hasTooltips()));
+ root.setAttribute("urls", String.valueOf(style.hasURLs()));
+
+ // Attribute for plot orientation
+ if (PlotOrientation.HORIZONTAL.equals(style.getOrientation()))
+ root.setAttribute("orientation", "horizontal");
+ else if (PlotOrientation.VERTICAL.equals(style.getOrientation()))
+ root.setAttribute("orientation", "vertical");
+
+ // Element for background
+ addChildToElement(root, "background", false, "paint",
+ style.getBackground());
+
+ // Element for border
+ addChildToElement(root, "border", false, "visible",
+ style.isBorderVisible());
+
+ // Chart title
+ addLabelChildToElement(root, "title", style.getTitleStyle());
+
+ // Chart description
+ addLabelChildToElement(root, "desc", style.getDescStyle());
+
+ // Axis
+ if (style.getAxisStyle(0) != null)
+ addAxisChildToElement(root, "domainAxis", style.getAxisStyle(0));
+ if (style.getAxisStyle(1) != null)
+ addAxisChildToElement(root, "rangeAxis", style.getAxisStyle(1));
+ if (style.getAxisStyle(2) != null)
+ addAxisChildToElement(root, "rangeAxis2", style.getAxisStyle(2));
+
+ // Renderers
+ for (int i = 0; i < style.getRendererCount(); i++)
+ addRendererChildToElement(root, style.getRendererStyle(i));
+
+ // Plot
+ if (style.getPlotStyle() != null) {
+ Element plotElem = addChildToElement(root, "plot");
+ ChartPlotStyle plotStyle = style.getPlotStyle();
+
+ XMLUtil.setAttribute(plotElem, "centerAxisOrigin",
+ plotStyle.isCenterOriginSymetrically(), false);
+
+ addChildToElement(plotElem, "foreground", false, "alpha",
+ plotStyle.getForegroundAlpha());
+ addChildToElement(plotElem, "background", false, "alpha",
+ plotStyle.getBackgroundAlpha(), "paint",
+ plotStyle.getBackgroundPaint());
+ if (plotStyle.getInsets() != null)
+ addChildToElement(plotElem, "insets", false, "top", plotStyle
+ .getInsets().getTop(), "bottom", plotStyle.getInsets()
+ .getBottom(), "left", plotStyle.getInsets().getLeft(),
+ "right", plotStyle.getInsets().getRight());
+ addChildToElement(plotElem, "domainGridline", false, "visible",
+ plotStyle.isDomainGridlineVisible(), "paint",
+ plotStyle.getDomainGridlinePaint());
+ addChildToElement(plotElem, "rangeGridline", false, "visible",
+ plotStyle.isRangeGridlineVisible(), "paint",
+ plotStyle.getRangeGridlinePaint());
+ addChildToElement(plotElem, "crosshairLines", false, "visible",
+ plotStyle.isCrosshairVisible(), "paint",
+ plotStyle.getCrosshairPaint());
+ }
+
+ // Type specific style properties
+ if (style instanceof BasicChartStyle) {
+ root.setAttribute("stepped",
+ String.valueOf(((BasicChartStyle) style).isStepped()));
+ root.setAttribute("stacked",
+ String.valueOf(((BasicChartStyle) style).isStacked()));
+ }
+ if (style instanceof ScatterChartStyle)
+ root.setAttribute("regrLineVisible", String
+ .valueOf(((ScatterChartStyle) style)
+ .isRegressionLineVisible()));
+
+ return root;
+ }
+
+ /**
+ * Creates an child element with no attributes. If an element is given, the
+ * new child is automatically added.
+ *
+ * @param element
+ * if not {@code null} the new element is added as child to this
+ * element
+ * @param childName
+ * name of the new child
+ * @return the new child element
+ */
+ public static Element addChildToElement(Element element, String childName) {
+ return addChildToElement(element, childName, true);
+ }
+
+ /**
+ * Creates an child element and sets some attributes. If an element is
+ * given, the new child is automatically added.
+ *
+ * @param element
+ * if not {@code null} the new element is added as child to this
+ * element
+ * @param childName
+ * name of the new child
+ * @param inclNullAttr
+ * if {@code false} attributes with value {@code null} are not
+ * set; if ALL attributes are {@code null} also the child is not
+ * created!
+ * @param attributes
+ * attribute/value pairs for the new child element
+ * @return the new child element
+ */
+ public static Element addChildToElement(Element element, String childName,
+ boolean inclNullAttr, Object... attributes) {
+ // Create child
+ Element child = new Element(childName);
+ // Set child attributes
+ boolean anyAttrSet = false;
+ for (int i = 0; i < attributes.length; i += 2) {
+ String attrName = attributes[i].toString();
+ Object attrValue = attributes[i + 1];
+ anyAttrSet |= XMLUtil.setAttribute(child, attrName, attrValue,
+ inclNullAttr);
+ }
+ // if null attributes should be ignored and NO attribute is
+ // actually set, also the child is not created!
+ if (!anyAttrSet && !inclNullAttr)
+ child = null;
+
+ // If element is given, the child is added
+ if (element != null && child != null)
+ element.addContent(child);
+
+ return child;
+ }
+
+ /**
+ * Creates an child element for a {@link ChartLabelStyle}. Besides the
+ * attributes the label title is set as element content. If an element is
+ * given, the new child is automatically added.
+ *
+ * @param element
+ * if not {@code null} the new element is added as child to this
+ * element
+ * @param titleChildName
+ * name of the new child
+ * @param labelStyle
+ * the label style to create the element for
+ * @return the new child element; {@code null} if {@code labelStyle} is
+ * {@code null}
+ */
+ private static Element addLabelChildToElement(Element element,
+ String titleChildName, ChartLabelStyle labelStyle) {
+ if (labelStyle == null)
+ return null;
+ // Create and add child with paint attribute
+ Element labelElem = addChildToElement(element, titleChildName);
+ XMLUtil.setNotNullAttribute(labelElem, "paint", labelStyle.getPaint());
+ // Set title to element content
+ labelElem.setText(labelStyle.getLabelTranslation().toOneLine());
+
+ return labelElem;
+ }
+
+ /**
+ * Creates an child element for a {@link ChartAxisStyle}. If an element is
+ * given, the new child is automatically added.
+ *
+ * @param element
+ * if not {@code null} the new element is added as child to this
+ * element
+ * @param axisChildName
+ * name of the new child
+ * @param axisStyle
+ * the axis style to create the element for
+ * @return the new child element; {@code null} if {@code axisStyle} is
+ * {@code null}
+ */
+ private static Element addAxisChildToElement(Element element,
+ String axisChildName, ChartAxisStyle axisStyle) {
+ if (axisStyle == null)
+ return null;
+
+ // Create and add child with visible and unit attribute
+ Element axisElem = addChildToElement(element, axisChildName);
+ XMLUtil.setNotNullAttribute(axisElem, "visible", axisStyle.isVisible());
+ XMLUtil.setNotNullAttribute(axisElem, "unit", axisStyle.getUnitString());
+
+ // Create and add child <title>
+ Element titleElem = addLabelChildToElement(axisElem, "title", axisStyle);
+ // Add angle and font attribute to <title> element
+ XMLUtil.setNotNullAttribute(titleElem, "font", axisStyle.getLabelFont());
+ XMLUtil.setNotNullAttribute(titleElem, "angle",
+ axisStyle.getLabelAngle());
+
+ // Create and add child <valueLabels>
+ Element valueLabelsElem = addChildToElement(axisElem, "valueLabels");
+ XMLUtil.setNotNullAttribute(valueLabelsElem, "paint",
+ axisStyle.getValuesPaint());
+ XMLUtil.setNotNullAttribute(valueLabelsElem, "font",
+ axisStyle.getValuesFont());
+ XMLUtil.setNotNullAttribute(valueLabelsElem, "valueAngle",
+ axisStyle.getValuesAngle());
+
+ Format format = axisStyle.getValuesFormat();
+ if (format instanceof DateFormat)
+ valueLabelsElem.setAttribute("dateFormat", format.toString());
+ if (format instanceof NumberFormat)
+ valueLabelsElem.setAttribute("numberFormat", format.toString());
+
+ return axisElem;
+ }
+
+ /**
+ * Creates an child element for a {@link ChartRendererStyle}. If an element
+ * is given, the new child is automatically added.
+ *
+ * @param element
+ * if not {@code null} the new element is added as child to this
+ * element
+ * @param rendererStyle
+ * the renderer style to create the element for
+ * @return the new child element; if {@code rendererStyle} is {@code null}
+ * an empty element is returned and added (so that the order of the
+ * renderers according to the dataset is preserved)
+ */
+ private static Element addRendererChildToElement(Element element,
+ ChartRendererStyle rendererStyle) {
+ // Create and add child with visible attribute
+ Element rendererElem = addChildToElement(element, "renderer");
+
+ if (rendererStyle != null) {
+ // Margin attribute
+ XMLUtil.setAttribute(rendererElem, "margin",
+ rendererStyle.getMargin(), true);
+ // Attributes for default series properties
+ XMLUtil.setNotNullAttribute(rendererElem, "paint",
+ rendererStyle.getDefaultPaint());
+ XMLUtil.setNotNullAttribute(rendererElem, "lineWidth",
+ rendererStyle.getDefaultLineWidth());
+ XMLUtil.setNotNullAttribute(rendererElem, "lineDash", XMLUtil
+ .convertArrayToSeparatedString(rendererStyle
+ .getDefaultLineDashAttibutes()));
+ XMLUtil.setNotNullAttribute(rendererElem, "itemLabelsVisible",
+ rendererStyle.isDefaultItemLabelsVisible());
+ XMLUtil.setNotNullAttribute(rendererElem, "shapesVisible",
+ rendererStyle.isDefaultShapesVisible());
+ XMLUtil.setNotNullAttribute(rendererElem, "legendVisible",
+ rendererStyle.isDefaultLegendVisible());
+ if (rendererStyle.getDefaultLegendLabel() != null)
+ XMLUtil.setNotNullAttribute(rendererElem, "legendLabel",
+ rendererStyle.getDefaultLegendLabel()
+ .getLabelTranslation());
+ if (rendererStyle.getDefaultLegendTooltip() != null)
+ XMLUtil.setNotNullAttribute(rendererElem, "legendTooltip",
+ rendererStyle.getDefaultLegendTooltip()
+ .getLabelTranslation());
+
+ // Create <series> child for all series
+ for (int i = 0; i < rendererStyle.getSeriesCount(); i++) {
+ Element seriesElem = addChildToElement(rendererElem, "series");
+ XMLUtil.setNotNullAttribute(seriesElem, "seriesKey",
+ rendererStyle.getSeriesKey(i));
+ XMLUtil.setNotNullAttribute(seriesElem, "paint",
+ rendererStyle.getSeriesPaint(i));
+ XMLUtil.setNotNullAttribute(seriesElem, "lineWidth",
+ rendererStyle.getSeriesLineWidth(i));
+ XMLUtil.setNotNullAttribute(seriesElem, "lineDash", XMLUtil
+ .convertArrayToSeparatedString(rendererStyle
+ .getSeriesLineDashAttibutes(i)));
+ XMLUtil.setNotNullAttribute(seriesElem, "itemLabelsVisible",
+ rendererStyle.isSeriesItemLabelsVisible(i));
+ XMLUtil.setNotNullAttribute(seriesElem, "shapesVisible",
+ rendererStyle.isSeriesShapesVisible(i));
+ XMLUtil.setNotNullAttribute(seriesElem, "legendVisible",
+ rendererStyle.isSeriesLegendVisible(i));
+ if (rendererStyle.getSeriesLegendLabel(i) != null)
+ XMLUtil.setNotNullAttribute(seriesElem, "legendLabel",
+ rendererStyle.getSeriesLegendLabel(i)
+ .getLabelTranslation());
+ if (rendererStyle.getSeriesLegendTooltip(i) != null)
+ XMLUtil.setNotNullAttribute(seriesElem, "legendTooltip",
+ rendererStyle.getSeriesLegendTooltip(i)
+ .getLabelTranslation());
+ }
+ }
+
+ return rendererElem;
+ }
}
Modified: trunk/src_junit/schmitzm/io/IOUtilTest.java
===================================================================
--- trunk/src_junit/schmitzm/io/IOUtilTest.java 2010-10-30 00:02:44 UTC (rev 1191)
+++ trunk/src_junit/schmitzm/io/IOUtilTest.java 2010-11-01 16:04:55 UTC (rev 1192)
@@ -1,5 +1,6 @@
package schmitzm.io;
+import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
@@ -7,6 +8,7 @@
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
+import java.net.MalformedURLException;
import java.net.URISyntaxException;
import java.net.URL;
@@ -57,4 +59,19 @@
"The destination file must not exist, because the original name was applied during copying",
dest.exists());
}
+
+ @Test
+ public void testGetFilename() throws MalformedURLException {
+ assertEquals("index.html", IOUtil.getFilename(new URL(
+ "http://www.bahn.de/exterme/index.html?k=v")));
+
+ assertEquals("index.html", IOUtil.getFilename(new URL(
+ "http://www.bahn.de/exterme/index.html")));
+
+ assertEquals("exterme",
+ IOUtil.getFilename(new URL("http://www.bahn.de/exterme/")));
+
+ assertEquals("exterme",
+ IOUtil.getFilename(new URL("http://www.bahn.de/exterme")));
+ }
}
More information about the Schmitzm-commits
mailing list