[Schmitzm-commits] r1307 - trunk/src/schmitzm/xml

scm-commit@wald.intevation.org scm-commit at wald.intevation.org
Wed Nov 24 19:04:47 CET 2010


Author: mojays
Date: 2010-11-24 19:04:47 +0100 (Wed, 24 Nov 2010)
New Revision: 1307

Modified:
   trunk/src/schmitzm/xml/XMLUtil.java
Log:
XMLUtil: new method getDateAttribute(..)

Modified: trunk/src/schmitzm/xml/XMLUtil.java
===================================================================
--- trunk/src/schmitzm/xml/XMLUtil.java	2010-11-24 15:15:08 UTC (rev 1306)
+++ trunk/src/schmitzm/xml/XMLUtil.java	2010-11-24 18:04:47 UTC (rev 1307)
@@ -31,6 +31,10 @@
 
 import java.awt.Color;
 import java.awt.Font;
+import java.text.DateFormat;
+import java.text.ParseException;
+import java.text.SimpleDateFormat;
+import java.util.Date;
 import java.util.HashSet;
 import java.util.Set;
 import java.util.StringTokenizer;
@@ -215,6 +219,27 @@
   }
 
   /**
+   * Gets the attribute value from element as {@link Date}.
+   * @param element  element where the attribute is determined from
+   * @param attrName name of the attribute
+   * @param dateFormat format (according to {@link SimpleDateFormat}) the
+   *                   date string is given in
+   * @param defValue optional default value returned if attribute is not found (or empty)
+   * @return {@code null} if {@code element} is {@code null} or attribute is not
+   *         specified in element
+   */
+  public static Date getDateAttribute(Element element, String attrName, String dateFormat, Date... defValue) throws ParseException {
+    String value = getAttribute(element, attrName);
+    if ( value != null ) {
+      DateFormat dateFormatter = new SimpleDateFormat(dateFormat);
+      return dateFormatter.parse(value);
+    }
+    if ( defValue.length > 0 )
+      return defValue[0];
+    return null;
+  }
+
+  /**
    * Gets the attribute value from element as {@link Color}.
    * @param element  element where the attribute is determined from
    * @param attrName name of the attribute



More information about the Schmitzm-commits mailing list