You are viewing a plain text version of this content. The canonical link for it is here.
Posted to fop-commits@xmlgraphics.apache.org by je...@apache.org on 2008/03/18 11:09:37 UTC

svn commit: r638299 - in /xmlgraphics/fop/branches/Temp_ProcessingFeedback: src/java/META-INF/services/ src/java/org/apache/fop/util/text/ test/java/org/apache/fop/util/

Author: jeremias
Date: Tue Mar 18 03:09:30 2008
New Revision: 638299

URL: http://svn.apache.org/viewvc?rev=638299&view=rev
Log:
Added support for java.util.text's ChoiceFormat to AdvancedMessageFormat.
Reuse the regexes as constants.

Added:
    xmlgraphics/fop/branches/Temp_ProcessingFeedback/src/java/org/apache/fop/util/text/ChoiceFieldPart.java   (with props)
Modified:
    xmlgraphics/fop/branches/Temp_ProcessingFeedback/src/java/META-INF/services/org.apache.fop.util.text.AdvancedMessageFormat$PartFactory
    xmlgraphics/fop/branches/Temp_ProcessingFeedback/src/java/org/apache/fop/util/text/AdvancedMessageFormat.java
    xmlgraphics/fop/branches/Temp_ProcessingFeedback/src/java/org/apache/fop/util/text/EqualsFieldPart.java
    xmlgraphics/fop/branches/Temp_ProcessingFeedback/src/java/org/apache/fop/util/text/IfFieldPart.java
    xmlgraphics/fop/branches/Temp_ProcessingFeedback/test/java/org/apache/fop/util/AdvancedMessageFormatTestCase.java

Modified: xmlgraphics/fop/branches/Temp_ProcessingFeedback/src/java/META-INF/services/org.apache.fop.util.text.AdvancedMessageFormat$PartFactory
URL: http://svn.apache.org/viewvc/xmlgraphics/fop/branches/Temp_ProcessingFeedback/src/java/META-INF/services/org.apache.fop.util.text.AdvancedMessageFormat%24PartFactory?rev=638299&r1=638298&r2=638299&view=diff
==============================================================================
--- xmlgraphics/fop/branches/Temp_ProcessingFeedback/src/java/META-INF/services/org.apache.fop.util.text.AdvancedMessageFormat$PartFactory (original)
+++ xmlgraphics/fop/branches/Temp_ProcessingFeedback/src/java/META-INF/services/org.apache.fop.util.text.AdvancedMessageFormat$PartFactory Tue Mar 18 03:09:30 2008
@@ -1,3 +1,4 @@
 org.apache.fop.util.text.IfFieldPart$Factory
 org.apache.fop.util.text.EqualsFieldPart$Factory
+org.apache.fop.util.text.ChoiceFieldPart$Factory
 org.apache.fop.events.EventFormatter$LookupFieldPartFactory

Modified: xmlgraphics/fop/branches/Temp_ProcessingFeedback/src/java/org/apache/fop/util/text/AdvancedMessageFormat.java
URL: http://svn.apache.org/viewvc/xmlgraphics/fop/branches/Temp_ProcessingFeedback/src/java/org/apache/fop/util/text/AdvancedMessageFormat.java?rev=638299&r1=638298&r2=638299&view=diff
==============================================================================
--- xmlgraphics/fop/branches/Temp_ProcessingFeedback/src/java/org/apache/fop/util/text/AdvancedMessageFormat.java (original)
+++ xmlgraphics/fop/branches/Temp_ProcessingFeedback/src/java/org/apache/fop/util/text/AdvancedMessageFormat.java Tue Mar 18 03:09:30 2008
@@ -22,6 +22,7 @@
 import java.util.Iterator;
 import java.util.List;
 import java.util.Map;
+import java.util.regex.Pattern;
 
 import org.apache.xmlgraphics.util.Service;
 
@@ -43,7 +44,7 @@
 public class AdvancedMessageFormat {
 
     /** Regex that matches "," but not "\," (escaped comma) */
-    static final String COMMA_SEPARATOR_REGEX = "(?<!\\\\),";
+    static final Pattern COMMA_SEPARATOR_REGEX = Pattern.compile("(?<!\\\\),");
     
     private static final Map PART_FACTORIES = new java.util.HashMap();
     private static final List OBJECT_FORMATTERS = new java.util.ArrayList();
@@ -98,11 +99,17 @@
                     sb.setLength(0);
                 }
                 i++;
+                int nesting = 1;
                 while (i < len) {
                     ch = pattern.charAt(i);
-                    if (ch == '}') {
-                        i++;
-                        break;
+                    if (ch == '{') {
+                        nesting++;
+                    } else if (ch == '}') {
+                        nesting--;
+                        if (nesting == 0) {
+                            i++;
+                            break;
+                        }
                     }
                     sb.append(ch);
                     i++;
@@ -150,7 +157,7 @@
     }
     
     private Part parseField(String field) {
-        String[] parts = field.split(COMMA_SEPARATOR_REGEX, 3);
+        String[] parts = COMMA_SEPARATOR_REGEX.split(field, 3);
         String fieldName = parts[0];
         if (parts.length == 1) {
             if (fieldName.startsWith("#")) {
@@ -184,10 +191,19 @@
      */
     public String format(Map params) {
         StringBuffer sb = new StringBuffer();
-        rootPart.write(sb, params);
+        format(params, sb);
         return sb.toString();
     }
 
+    /**
+     * Formats a message with the given parameters.
+     * @param params a Map of named parameters (Contents: <String, Object>)
+     * @param target the target StringBuffer to write the formatted message to
+     */
+    public void format(Map params, StringBuffer target) {
+        rootPart.write(target, params);
+    }
+    
     public interface Part {
         void write(StringBuffer sb, Map params);
         boolean isGenerated(Map params);

Added: xmlgraphics/fop/branches/Temp_ProcessingFeedback/src/java/org/apache/fop/util/text/ChoiceFieldPart.java
URL: http://svn.apache.org/viewvc/xmlgraphics/fop/branches/Temp_ProcessingFeedback/src/java/org/apache/fop/util/text/ChoiceFieldPart.java?rev=638299&view=auto
==============================================================================
--- xmlgraphics/fop/branches/Temp_ProcessingFeedback/src/java/org/apache/fop/util/text/ChoiceFieldPart.java (added)
+++ xmlgraphics/fop/branches/Temp_ProcessingFeedback/src/java/org/apache/fop/util/text/ChoiceFieldPart.java Tue Mar 18 03:09:30 2008
@@ -0,0 +1,91 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.
+ */
+
+/* $Id$ */
+
+package org.apache.fop.util.text;
+
+import java.text.ChoiceFormat;
+import java.util.Map;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+import org.apache.fop.util.text.AdvancedMessageFormat.Part;
+import org.apache.fop.util.text.AdvancedMessageFormat.PartFactory;
+
+/**
+ * Defines a "choice" field part that works like {@link ChoiceFormat}.
+ */
+public class ChoiceFieldPart implements Part {
+    
+    private static final Pattern VARIABLE_REGEX = Pattern.compile("\\{([^\\}]+)\\}");
+    
+    private String fieldName;
+    private ChoiceFormat choiceFormat;
+    
+    /**
+     * Creates a new choice part.
+     * @param fieldName the field name to work on
+     * @param choicesPattern the choices pattern (as used by {@link ChoiceFormat})
+     */
+    public ChoiceFieldPart(String fieldName, String choicesPattern) {
+        this.fieldName = fieldName;
+        this.choiceFormat = new ChoiceFormat(choicesPattern);
+    }
+
+    /** {@inheritDoc} */
+    public boolean isGenerated(Map params) {
+        Object obj = params.get(fieldName);
+        return obj != null;
+    }
+
+    /** {@inheritDoc} */
+    public void write(StringBuffer sb, Map params) {
+        Object obj = params.get(fieldName);
+        Number num = (Number)obj;
+        String result = this.choiceFormat.format(num.doubleValue());
+        Matcher m = VARIABLE_REGEX.matcher(result);
+        if (m.find()) {
+            //Resolve inner variables
+            AdvancedMessageFormat f = new AdvancedMessageFormat(result);
+            f.format(params, sb);
+        } else {
+            sb.append(result);
+        }
+    }
+
+    /** {@inheritDoc} */
+    public String toString() {
+        return "{" + this.fieldName + ",choice, ....}";
+    }
+    
+    /** Factory for ChoiceFieldPart. */
+    public static class Factory implements PartFactory {
+
+        /** {@inheritDoc} */
+        public Part newPart(String fieldName, String values) {
+            return new ChoiceFieldPart(fieldName, values);
+        }
+
+        /** {@inheritDoc} */
+        public String getFormat() {
+            return "choice";
+        }
+        
+    }
+
+}
\ No newline at end of file

Propchange: xmlgraphics/fop/branches/Temp_ProcessingFeedback/src/java/org/apache/fop/util/text/ChoiceFieldPart.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: xmlgraphics/fop/branches/Temp_ProcessingFeedback/src/java/org/apache/fop/util/text/ChoiceFieldPart.java
------------------------------------------------------------------------------
    svn:keywords = Id

Modified: xmlgraphics/fop/branches/Temp_ProcessingFeedback/src/java/org/apache/fop/util/text/EqualsFieldPart.java
URL: http://svn.apache.org/viewvc/xmlgraphics/fop/branches/Temp_ProcessingFeedback/src/java/org/apache/fop/util/text/EqualsFieldPart.java?rev=638299&r1=638298&r2=638299&view=diff
==============================================================================
--- xmlgraphics/fop/branches/Temp_ProcessingFeedback/src/java/org/apache/fop/util/text/EqualsFieldPart.java (original)
+++ xmlgraphics/fop/branches/Temp_ProcessingFeedback/src/java/org/apache/fop/util/text/EqualsFieldPart.java Tue Mar 18 03:09:30 2008
@@ -34,7 +34,7 @@
 
     /** {@inheritDoc} */
     protected void parseValues(String values) {
-        String[] parts = values.split(AdvancedMessageFormat.COMMA_SEPARATOR_REGEX, 3);
+        String[] parts = AdvancedMessageFormat.COMMA_SEPARATOR_REGEX.split(values, 3);
         this.equalsValue = parts[0];
         if (parts.length == 1) {
             throw new IllegalArgumentException(

Modified: xmlgraphics/fop/branches/Temp_ProcessingFeedback/src/java/org/apache/fop/util/text/IfFieldPart.java
URL: http://svn.apache.org/viewvc/xmlgraphics/fop/branches/Temp_ProcessingFeedback/src/java/org/apache/fop/util/text/IfFieldPart.java?rev=638299&r1=638298&r2=638299&view=diff
==============================================================================
--- xmlgraphics/fop/branches/Temp_ProcessingFeedback/src/java/org/apache/fop/util/text/IfFieldPart.java (original)
+++ xmlgraphics/fop/branches/Temp_ProcessingFeedback/src/java/org/apache/fop/util/text/IfFieldPart.java Tue Mar 18 03:09:30 2008
@@ -36,7 +36,7 @@
     }
 
     protected void parseValues(String values) {
-        String[] parts = values.split(AdvancedMessageFormat.COMMA_SEPARATOR_REGEX, 2);
+        String[] parts = AdvancedMessageFormat.COMMA_SEPARATOR_REGEX.split(values, 2);
         if (parts.length == 2) {
             ifValue = AdvancedMessageFormat.unescapeComma(parts[0]);
             elseValue = AdvancedMessageFormat.unescapeComma(parts[1]);

Modified: xmlgraphics/fop/branches/Temp_ProcessingFeedback/test/java/org/apache/fop/util/AdvancedMessageFormatTestCase.java
URL: http://svn.apache.org/viewvc/xmlgraphics/fop/branches/Temp_ProcessingFeedback/test/java/org/apache/fop/util/AdvancedMessageFormatTestCase.java?rev=638299&r1=638298&r2=638299&view=diff
==============================================================================
--- xmlgraphics/fop/branches/Temp_ProcessingFeedback/test/java/org/apache/fop/util/AdvancedMessageFormatTestCase.java (original)
+++ xmlgraphics/fop/branches/Temp_ProcessingFeedback/test/java/org/apache/fop/util/AdvancedMessageFormatTestCase.java Tue Mar 18 03:09:30 2008
@@ -156,4 +156,28 @@
         msg = format.format(params);
         assertEquals("Error\nSome explanation!", msg);
     }
+    
+    public void testChoiceFormatting() throws Exception {
+        String msg;
+        AdvancedMessageFormat format;
+        
+        format = new AdvancedMessageFormat(
+                "You have {amount,choice,0#nothing|0<{amount} bucks|100<more than enough}.");
+
+        Map params = new java.util.HashMap();
+
+        params.put("amount", new Integer(0));
+        msg = format.format(params);
+        assertEquals("You have nothing.", msg);
+
+        params.put("amount", new Integer(7));
+        msg = format.format(params);
+        assertEquals("You have 7 bucks.", msg);
+
+        params.put("amount", new Integer(140));
+        msg = format.format(params);
+        assertEquals("You have more than enough.", msg);
+
+    }
+    
 }



---------------------------------------------------------------------
To unsubscribe, e-mail: fop-commits-unsubscribe@xmlgraphics.apache.org
For additional commands, e-mail: fop-commits-help@xmlgraphics.apache.org