[Schmitzm-commits] r248 - in trunk/src/schmitzm: jfree/chart/style xml

scm-commit@wald.intevation.org scm-commit at wald.intevation.org
Fri Jul 31 12:54:46 CEST 2009


Author: mojays
Date: 2009-07-31 12:54:46 +0200 (Fri, 31 Jul 2009)
New Revision: 248

Modified:
   trunk/src/schmitzm/jfree/chart/style/ChartStyleXMLFactory.java
   trunk/src/schmitzm/xml/XMLUtil.java
Log:
XMLUtil: new useful methods to set an attribute value
ChartStyleXMLFactory.writeStyleToURL(..) renamed to ...ToFile(..)

Modified: trunk/src/schmitzm/jfree/chart/style/ChartStyleXMLFactory.java
===================================================================
--- trunk/src/schmitzm/jfree/chart/style/ChartStyleXMLFactory.java	2009-07-31 10:29:47 UTC (rev 247)
+++ trunk/src/schmitzm/jfree/chart/style/ChartStyleXMLFactory.java	2009-07-31 10:54:46 UTC (rev 248)
@@ -364,7 +364,7 @@
    *                        used)
    * @param filePath        file to store the style to
    */
-  public void writeStyleToURL(E style, String rootElementName, File filePath) throws IOException, URISyntaxException {
+  public void writeStyleToFile(E style, String rootElementName, File filePath) throws IOException {
     final FileWriter out = new FileWriter( filePath );
     // Create XML-Document
     XMLUtil.XML_OUTPUTTER.output(
@@ -422,10 +422,14 @@
     if ( style.getAxisCount() > 2 )
       addAxisChildToElement(root, "rangeAxis2", style.getAxisStyle(2) );
     
-    // Renderer
+    // Renderers
     for (int i=0; i<style.getRendererCount(); i++)
       addRendererChildToElement(root, style.getRendererStyle(i));
     
+    // Plot
+    Element plotElem = addChildToElement(root, "plot");
+
+    
     // TODO: <renderer> + <plot>
     
     // Type specific style properties
@@ -539,17 +543,13 @@
     
     if ( rendererStyle != null ) {
       // Margin attribute
-      if ( rendererStyle.getMargin() != null )
-        rendererElem.setAttribute("margin", String.valueOf( rendererStyle.getMargin() ));
+      XMLUtil.setNotNullAttribute(element, "margin", rendererStyle.getMargin());
       // Create <series> child for all series
       for ( int i=0; i<rendererStyle.getSeriesCount(); i++) {
         Element seriesElem = addChildToElement(rendererElem, "series");
-        if ( rendererStyle.getSeriesPaint(i) != null )
-          seriesElem.setAttribute("paint", Integer.toHexString( rendererStyle.getSeriesPaint(i).getRGB() ));
-        if ( rendererStyle.isSeriesItemLabelsVisible(i) != null )
-          seriesElem.setAttribute("itemLabelsVisible", String.valueOf( rendererStyle.isSeriesItemLabelsVisible(i) ));
-        if ( rendererStyle.isSeriesShapesVisible(i) != null )
-          seriesElem.setAttribute("shapesVisible", String.valueOf( rendererStyle.isSeriesShapesVisible(i) ));
+        XMLUtil.setNotNullAttribute(seriesElem, "paint", rendererStyle.getSeriesPaint(i));
+        XMLUtil.setNotNullAttribute(seriesElem, "itemLabelsVisible", rendererStyle.isSeriesItemLabelsVisible(i));
+        XMLUtil.setNotNullAttribute(seriesElem, "shapesVisible", rendererStyle.isSeriesShapesVisible(i));
       }
     }
     

Modified: trunk/src/schmitzm/xml/XMLUtil.java
===================================================================
--- trunk/src/schmitzm/xml/XMLUtil.java	2009-07-31 10:29:47 UTC (rev 247)
+++ trunk/src/schmitzm/xml/XMLUtil.java	2009-07-31 10:54:46 UTC (rev 248)
@@ -154,7 +154,57 @@
       return defValue[0];
     return null;
   }
-  //  /**
+
+  /**
+   * Sets an attribute value. The {@code .toString()} method is used
+   * to convert the object value to string.<br>
+   * Exceptions:
+   * <ul>
+   *   <li>{@link Color}: the color is converted to hex rgb string</li>
+   * </ul>
+   * @param element    element the attribute is set for
+   * @param attrName   name of the attribute to set
+   * @param value      attribute value
+   * @param ignoreNull if {@code true} and {@code value} is {@code null}
+   *                   the attribute is NOT SET
+   * @return {@code true} if the attribute is set
+   */
+  public static boolean setAttribute(Element element, String attrName, Object value, boolean ignoreNull) {
+    if ( value == null && ignoreNull )
+      return( false );
+    
+    if ( value == null )
+      value = "";
+    
+    // Default: use the internal string conversion
+    String valueStr = value.toString();
+    // Special: convert color to hex rgb string
+    if ( value instanceof Color )
+      valueStr = Integer.toHexString( ((Color)value).getRGB() );
+    
+    element.setAttribute(attrName, valueStr);
+  
+    return true;
+  }
+
+  /**
+   * Sets an attribute value if the value is not {@code null}.
+   * The {@code .toString()} method is used to convert the object
+   * value to string.<br>
+   * Exceptions:
+   * <ul>
+   *   <li>{@link Color}: the color is converted to hex rgb string</li>
+   * </ul>
+   * @param element    element the attribute is set for
+   * @param attrName   name of the attribute to set
+   * @param value      attribute value
+   * @return {@code true} if the attribute is set
+   */
+  public static boolean setNotNullAttribute(Element element, String attrName, Object value) {
+    return setAttribute(element, attrName, value, false);
+  }
+  
+//  /**
 //   * Returns a {@link DocumentBuilder}. If validating is true,
 //   * the contents is validated against the XSD specified in the file.
 //   */



More information about the Schmitzm-commits mailing list