You are viewing a plain text version of this content. The canonical link for it is here.
Posted to cvs@cocoon.apache.org by lg...@apache.org on 2005/09/02 10:27:19 UTC

svn commit: r266732 - in /cocoon/blocks/template/trunk: java/org/apache/cocoon/template/instruction/ test/org/apache/cocoon/template/jxtg/

Author: lgawron
Date: Fri Sep  2 01:27:01 2005
New Revision: 266732

URL: http://svn.apache.org/viewcvs?rev=266732&view=rev
Log:
introduce LocaleAwareInstruction which unifies and fixes locale handling among instructions.

Added:
    cocoon/blocks/template/trunk/java/org/apache/cocoon/template/instruction/LocaleAwareInstruction.java   (with props)
    cocoon/blocks/template/trunk/test/org/apache/cocoon/template/jxtg/formatDate-output.xml   (with props)
    cocoon/blocks/template/trunk/test/org/apache/cocoon/template/jxtg/formatDate.xml   (with props)
Modified:
    cocoon/blocks/template/trunk/java/org/apache/cocoon/template/instruction/FormatDate.java
    cocoon/blocks/template/trunk/java/org/apache/cocoon/template/instruction/FormatNumber.java
    cocoon/blocks/template/trunk/test/org/apache/cocoon/template/jxtg/JXTemplateGeneratorTestCase.java

Modified: cocoon/blocks/template/trunk/java/org/apache/cocoon/template/instruction/FormatDate.java
URL: http://svn.apache.org/viewcvs/cocoon/blocks/template/trunk/java/org/apache/cocoon/template/instruction/FormatDate.java?rev=266732&r1=266731&r2=266732&view=diff
==============================================================================
--- cocoon/blocks/template/trunk/java/org/apache/cocoon/template/instruction/FormatDate.java (original)
+++ cocoon/blocks/template/trunk/java/org/apache/cocoon/template/instruction/FormatDate.java Fri Sep  2 01:27:01 2005
@@ -24,7 +24,6 @@
 import org.apache.cocoon.components.expression.ExpressionContext;
 import org.apache.cocoon.template.environment.ErrorHolder;
 import org.apache.cocoon.template.environment.ExecutionContext;
-import org.apache.cocoon.template.environment.ValueHelper;
 import org.apache.cocoon.template.expression.JXTExpression;
 import org.apache.cocoon.template.script.event.Event;
 import org.apache.cocoon.template.script.event.StartElement;
@@ -37,7 +36,7 @@
 /**
  * @version SVN $Id$
  */
-public class FormatDate extends Instruction {
+public class FormatDate extends LocaleAwareInstruction {
     private static final String DATE = "date";
     private static final String TIME = "time";
     private static final String DATETIME = "both";
@@ -49,11 +48,9 @@
     private JXTExpression timeZone;
     private JXTExpression dateStyle;
     private JXTExpression timeStyle;
-    private JXTExpression locale;
-
     public FormatDate(StartElement raw, Attributes attrs, Stack stack) 
         throws SAXException{
-        super(raw);
+        super(raw, attrs, stack);
 
         Locator locator = getLocation();
 
@@ -64,7 +61,6 @@
         this.timeZone = JXTExpression.compileExpr(attrs.getValue("timeZone"), null, locator);
         this.dateStyle = JXTExpression.compileExpr(attrs.getValue("dateStyle"), null, locator);
         this.timeStyle = JXTExpression.compileExpr(attrs.getValue("timeStyle"), null, locator);
-        this.locale = JXTExpression.compileExpr(attrs.getValue("locale"), null, locator);
     }
 
     public Event execute(final XMLConsumer consumer,
@@ -88,7 +84,7 @@
     private String format(ExpressionContext expressionContext) throws Exception {
         String var = this.var == null ? null : this.var.getStringValue(expressionContext);
         Object value = this.value == null ? null : this.value.getValue(expressionContext);
-        Object locVal = this.locale == null ? null : this.locale.getValue(expressionContext);
+
         String pattern = this.pattern == null ? null : this.pattern.getStringValue(expressionContext);
         Object timeZone = this.timeZone == null ? null : this.timeZone.getValue(expressionContext);
 
@@ -99,13 +95,7 @@
         String formatted = null;
 
         // Create formatter
-        Locale locale;
-        if (locVal != null) {
-            locale = locVal instanceof Locale ? (Locale) locVal
-                : ValueHelper.parseLocale(locVal.toString(), null);
-        } else {
-            locale = Locale.getDefault();
-        }
+        Locale locale = getLocale( expressionContext );
         DateFormat formatter = createFormatter(locale, type, dateStyle,
                 timeStyle);
         // Apply pattern, if present

Modified: cocoon/blocks/template/trunk/java/org/apache/cocoon/template/instruction/FormatNumber.java
URL: http://svn.apache.org/viewcvs/cocoon/blocks/template/trunk/java/org/apache/cocoon/template/instruction/FormatNumber.java?rev=266732&r1=266731&r2=266732&view=diff
==============================================================================
--- cocoon/blocks/template/trunk/java/org/apache/cocoon/template/instruction/FormatNumber.java (original)
+++ cocoon/blocks/template/trunk/java/org/apache/cocoon/template/instruction/FormatNumber.java Fri Sep  2 01:27:01 2005
@@ -25,7 +25,6 @@
 import org.apache.cocoon.components.expression.ExpressionContext;
 import org.apache.cocoon.template.environment.ErrorHolder;
 import org.apache.cocoon.template.environment.ExecutionContext;
-import org.apache.cocoon.template.environment.ValueHelper;
 import org.apache.cocoon.template.expression.JXTExpression;
 import org.apache.cocoon.template.script.event.Event;
 import org.apache.cocoon.template.script.event.StartElement;
@@ -39,7 +38,7 @@
 /**
  * @version SVN $Id$
  */
-public class FormatNumber extends Instruction {
+public class FormatNumber extends LocaleAwareInstruction {
 
     private JXTExpression value;
     private JXTExpression type;
@@ -71,8 +70,7 @@
 
     public FormatNumber(StartElement raw, Attributes attrs, Stack stack) 
         throws SAXException {
-
-        super(raw);
+        super(raw, attrs, stack);
 
         Locator locator = getLocation();
 
@@ -93,7 +91,6 @@
             JXTExpression.compileInt(attrs.getValue("maxFractionDigits"), null, locator);
         this.minFractionDigits =
             JXTExpression.compileInt(attrs.getValue("minFractionDigits"), null, locator);
-        this.locale = JXTExpression.compileExpr(attrs.getValue("locale"), null, locator);
         this.var = JXTExpression.compileExpr(attrs.getValue("var"), null, locator);
     }
 
@@ -129,10 +126,8 @@
         Number minIntegerDigits = this.minIntegerDigits.getNumberValue(expressionContext);
         Number maxFractionDigits = this.maxFractionDigits.getNumberValue(expressionContext);
         Number minFractionDigits = this.minFractionDigits.getNumberValue(expressionContext);
-        String localeStr = this.locale.getStringValue(expressionContext);
-        Locale loc = localeStr != null
-            ? ValueHelper.parseLocale(localeStr, null)
-            : Locale.getDefault();
+        
+        Locale loc = getLocale(expressionContext);
         String formatted;
         if (loc != null) {
             // Create formatter
@@ -153,6 +148,7 @@
             formatted = formatter.format(input);
         } else {
             // no formatting locale available, use toString()
+            //TODO: locale is never null, default in worst case. What's this code for? 
             formatted = input.toString();
         }
         if (var != null) {

Added: cocoon/blocks/template/trunk/java/org/apache/cocoon/template/instruction/LocaleAwareInstruction.java
URL: http://svn.apache.org/viewcvs/cocoon/blocks/template/trunk/java/org/apache/cocoon/template/instruction/LocaleAwareInstruction.java?rev=266732&view=auto
==============================================================================
--- cocoon/blocks/template/trunk/java/org/apache/cocoon/template/instruction/LocaleAwareInstruction.java (added)
+++ cocoon/blocks/template/trunk/java/org/apache/cocoon/template/instruction/LocaleAwareInstruction.java Fri Sep  2 01:27:01 2005
@@ -0,0 +1,52 @@
+/*
+ * Copyright 1999-2005 The Apache Software Foundation.
+ * 
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ * 
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.cocoon.template.instruction;
+
+import java.util.Locale;
+import java.util.Stack;
+
+import org.apache.cocoon.components.expression.ExpressionContext;
+import org.apache.cocoon.template.environment.ValueHelper;
+import org.apache.cocoon.template.expression.JXTExpression;
+import org.apache.cocoon.template.script.event.StartElement;
+import org.xml.sax.Attributes;
+import org.xml.sax.SAXException;
+
+/**
+ * @version SVN $Id$
+ */
+public class LocaleAwareInstruction extends Instruction {
+    private JXTExpression locale;
+
+    public LocaleAwareInstruction(StartElement raw, Attributes attrs, Stack stack) throws SAXException {
+        super(raw);
+        this.locale = JXTExpression.compileExpr(attrs.getValue("locale"), null, getLocation());
+    }
+
+    protected Locale getLocale(ExpressionContext expressionContext) throws Exception {
+        Object locVal = null;
+        if (this.locale != null) {
+            locVal = this.locale.getValue(expressionContext);
+            if (locVal == null)
+                locVal = this.locale.getStringValue(expressionContext);
+        }
+
+        if (locVal != null)
+            return (locVal instanceof Locale ? (Locale) locVal : ValueHelper.parseLocale(locVal.toString(), null));
+        else
+            return Locale.getDefault();
+    }
+}

Propchange: cocoon/blocks/template/trunk/java/org/apache/cocoon/template/instruction/LocaleAwareInstruction.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: cocoon/blocks/template/trunk/java/org/apache/cocoon/template/instruction/LocaleAwareInstruction.java
------------------------------------------------------------------------------
    svn:keywords = Id

Modified: cocoon/blocks/template/trunk/test/org/apache/cocoon/template/jxtg/JXTemplateGeneratorTestCase.java
URL: http://svn.apache.org/viewcvs/cocoon/blocks/template/trunk/test/org/apache/cocoon/template/jxtg/JXTemplateGeneratorTestCase.java?rev=266732&r1=266731&r2=266732&view=diff
==============================================================================
--- cocoon/blocks/template/trunk/test/org/apache/cocoon/template/jxtg/JXTemplateGeneratorTestCase.java (original)
+++ cocoon/blocks/template/trunk/test/org/apache/cocoon/template/jxtg/JXTemplateGeneratorTestCase.java Fri Sep  2 01:27:01 2005
@@ -15,6 +15,8 @@
  */
 package org.apache.cocoon.template.jxtg;
 
+import java.util.Calendar;
+import java.util.GregorianCalendar;
 import java.util.HashMap;
 import java.util.Map;
 
@@ -127,6 +129,15 @@
         String inputURI = docBase + "jxAttribute.xml";
         String outputURI = docBase + "jxAttribute-output.xml";
 
+        assertEqual(load(outputURI), generate(JX, inputURI, EMPTY_PARAMS));
+    }
+    
+    public void testFormatDate() throws Exception {
+        String inputURI = docBase + "formatDate.xml";
+        String outputURI = docBase + "formatDate-output.xml";
+
+        Calendar cal = new GregorianCalendar(1979, 0, 1, 10, 21, 33);
+        getFlowContext().put("date", cal.getTime());
         assertEqual(load(outputURI), generate(JX, inputURI, EMPTY_PARAMS));
     }
 }

Added: cocoon/blocks/template/trunk/test/org/apache/cocoon/template/jxtg/formatDate-output.xml
URL: http://svn.apache.org/viewcvs/cocoon/blocks/template/trunk/test/org/apache/cocoon/template/jxtg/formatDate-output.xml?rev=266732&view=auto
==============================================================================
--- cocoon/blocks/template/trunk/test/org/apache/cocoon/template/jxtg/formatDate-output.xml (added)
+++ cocoon/blocks/template/trunk/test/org/apache/cocoon/template/jxtg/formatDate-output.xml Fri Sep  2 01:27:01 2005
@@ -0,0 +1,5 @@
+<?xml version="1.0" encoding="utf-8"?>
+<root xmlns:jx="http://apache.org/cocoon/templates/jx/1.0">
+	<date locale="pl_PL">01 styczeń 1979</date>
+	<date locale="sv_SE">01 januari 1979</date>
+</root>

Propchange: cocoon/blocks/template/trunk/test/org/apache/cocoon/template/jxtg/formatDate-output.xml
------------------------------------------------------------------------------
    svn:eol-style = native

Added: cocoon/blocks/template/trunk/test/org/apache/cocoon/template/jxtg/formatDate.xml
URL: http://svn.apache.org/viewcvs/cocoon/blocks/template/trunk/test/org/apache/cocoon/template/jxtg/formatDate.xml?rev=266732&view=auto
==============================================================================
--- cocoon/blocks/template/trunk/test/org/apache/cocoon/template/jxtg/formatDate.xml (added)
+++ cocoon/blocks/template/trunk/test/org/apache/cocoon/template/jxtg/formatDate.xml Fri Sep  2 01:27:01 2005
@@ -0,0 +1,5 @@
+<?xml version="1.0"?>
+<root xmlns:jx="http://apache.org/cocoon/templates/jx/1.0">
+	<date locale="pl_PL"><jx:formatDate value="${date}" pattern="dd MMMM yyyy" locale="pl_PL"/></date>
+	<date locale="sv_SE"><jx:formatDate value="${date}" pattern="dd MMMM yyyy" locale="sv_SE"/></date>
+</root>

Propchange: cocoon/blocks/template/trunk/test/org/apache/cocoon/template/jxtg/formatDate.xml
------------------------------------------------------------------------------
    svn:eol-style = native