[Dive4elements-commits] [PATCH] Datacage: Added dc:date-format to function resolver. Args: dc:date-format(pattern, date)

Wald Commits scm-commit at wald.intevation.org
Tue Apr 9 12:00:57 CEST 2013


# HG changeset patch
# User Sascha L. Teichmann <teichmann at intevation.de>
# Date 1365501652 -7200
# Node ID 62513c9183ba2f595d55e7b566431d17eb6140b3
# Parent  6e0d122904f91addbb1322f5c9c5ef836872b692
Datacage: Added dc:date-format to function resolver. Args: dc:date-format(pattern, date)
pattern obeys to java.text.SimpleDateFormat.

diff -r 6e0d122904f9 -r 62513c9183ba flys-artifacts/src/main/java/de/intevation/flys/artifacts/datacage/templating/FunctionResolver.java
--- a/flys-artifacts/src/main/java/de/intevation/flys/artifacts/datacage/templating/FunctionResolver.java	Tue Apr 09 11:53:47 2013 +0200
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/datacage/templating/FunctionResolver.java	Tue Apr 09 12:00:52 2013 +0200
@@ -1,7 +1,9 @@
 package de.intevation.flys.artifacts.datacage.templating;
 
+import java.text.SimpleDateFormat;
 import java.util.ArrayList;
 import java.util.Collection;
+import java.util.Date;
 import java.util.List;
 import java.util.Map;
 
@@ -90,6 +92,13 @@
                 return FunctionResolver.this.buildHelper.hasResult();
             }
         });
+
+        addFunction("date-format", 2, new XPathFunction() {
+            @Override
+            public Object evaluate(List args) throws XPathFunctionException {
+                return dateFormat(args);
+            }
+        });
     }
 
     /**
@@ -278,5 +287,20 @@
             return haystack;
         }
     }
+
+    public Object dateFormat(List args) throws XPathFunctionException {
+        Object pattern = args.get(0);
+        Object date    = args.get(1);
+        if (pattern instanceof String && date instanceof Date) {
+            try {
+                // TODO: Take locale into account.
+                return new SimpleDateFormat((String)pattern).format((Date)date);
+            }
+            catch (IllegalArgumentException iae) {
+                throw new XPathFunctionException(iae);
+            }
+        }
+        return "";
+    }
 }
 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :


More information about the Dive4elements-commits mailing list