You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@myfaces.apache.org by lo...@apache.org on 2016/03/16 17:50:33 UTC

svn commit: r1735263 - in /myfaces/tobago/branches/tobago-3.0.x: tobago-core/src/main/java/org/apache/myfaces/tobago/util/ tobago-example/tobago-example-demo/src/main/webapp/ tobago-example/tobago-example-demo/src/main/webapp/content/30-concept/20-form...

Author: lofwyr
Date: Wed Mar 16 16:50:33 2016
New Revision: 1735263

URL: http://svn.apache.org/viewvc?rev=1735263&view=rev
Log:
TOBAGO-1541: Use of ResourceBundles should support parameters

Added:
    myfaces/tobago/branches/tobago-3.0.x/tobago-core/src/main/java/org/apache/myfaces/tobago/util/MessageFormat.java
    myfaces/tobago/branches/tobago-3.0.x/tobago-example/tobago-example-demo/src/main/webapp/content/30-concept/20-format/
    myfaces/tobago/branches/tobago-3.0.x/tobago-example/tobago-example-demo/src/main/webapp/content/30-concept/20-format/format.xhtml
Modified:
    myfaces/tobago/branches/tobago-3.0.x/tobago-example/tobago-example-demo/src/main/webapp/main.xhtml
    myfaces/tobago/branches/tobago-3.0.x/tobago-example/tobago-example-demo/src/main/webapp/tobago-resource/html/standard/standard/property/overview.properties.xml
    myfaces/tobago/branches/tobago-3.0.x/tobago-tool/tobago-tool-apt/src/main/java/org/apache/myfaces/tobago/apt/processor/TaglibGenerator.java

Added: myfaces/tobago/branches/tobago-3.0.x/tobago-core/src/main/java/org/apache/myfaces/tobago/util/MessageFormat.java
URL: http://svn.apache.org/viewvc/myfaces/tobago/branches/tobago-3.0.x/tobago-core/src/main/java/org/apache/myfaces/tobago/util/MessageFormat.java?rev=1735263&view=auto
==============================================================================
--- myfaces/tobago/branches/tobago-3.0.x/tobago-core/src/main/java/org/apache/myfaces/tobago/util/MessageFormat.java (added)
+++ myfaces/tobago/branches/tobago-3.0.x/tobago-core/src/main/java/org/apache/myfaces/tobago/util/MessageFormat.java Wed Mar 16 16:50:33 2016
@@ -0,0 +1,55 @@
+package org.apache.myfaces.tobago.util;
+
+import javax.faces.context.FacesContext;
+
+/**
+ * Helper class for the EL functions <code>tc:format1()</code> ... <code>tc:format9()</code>
+ */
+public class MessageFormat {
+
+  public static String format(String pattern, Object param0) {
+    return createMessageFormat(pattern).format(new Object[] {param0});
+  }
+
+  public static String format(String pattern, Object param0, Object param1) {
+    return createMessageFormat(pattern).format(new Object[] {param0, param1});
+  }
+
+  public static String format(String pattern, Object param0, Object param1, Object param2) {
+    return createMessageFormat(pattern).format(new Object[] {param0, param1, param2});
+  }
+
+  public static String format(String pattern, Object param0, Object param1, Object param2, Object param3) {
+    return createMessageFormat(pattern).format(new Object[] {param0, param1, param2, param3});
+  }
+
+  public static String format(String pattern, Object param0, Object param1, Object param2, Object param3,
+                               Object param4) {
+    return createMessageFormat(pattern).format(new Object[] {param0, param1, param2, param3, param4});
+  }
+
+  public static String format(String pattern, Object param0, Object param1, Object param2, Object param3,
+                               Object param4, Object param5) {
+    return createMessageFormat(pattern).format(new Object[] {param0, param1, param2, param3, param4, param5});
+  }
+
+  public static String format(String pattern, Object param0, Object param1, Object param2, Object param3,
+                               Object param4, Object param5, Object param6) {
+    return createMessageFormat(pattern).format(new Object[] {param0, param1, param2, param3, param4, param5, param6});
+  }
+
+  public static String format(String pattern, Object param0, Object param1, Object param2, Object param3,
+                               Object param4, Object param5, Object param6, Object param7) {
+    return createMessageFormat(pattern).format(new Object[] {param0, param1, param2, param3, param4, param5, param6, param7});
+  }
+
+  public static String format(String pattern, Object param0, Object param1, Object param2, Object param3,
+                               Object param4, Object param5, Object param6, Object param7, Object param8) {
+    return createMessageFormat(pattern).format(new Object[] {param0, param1, param2, param3, param4, param5, param6, param7, param8});
+  }
+
+  private static java.text.MessageFormat createMessageFormat(String pattern) {
+    return new java.text.MessageFormat(pattern, FacesContext.getCurrentInstance().getViewRoot().getLocale());
+  }
+
+}

Added: myfaces/tobago/branches/tobago-3.0.x/tobago-example/tobago-example-demo/src/main/webapp/content/30-concept/20-format/format.xhtml
URL: http://svn.apache.org/viewvc/myfaces/tobago/branches/tobago-3.0.x/tobago-example/tobago-example-demo/src/main/webapp/content/30-concept/20-format/format.xhtml?rev=1735263&view=auto
==============================================================================
--- myfaces/tobago/branches/tobago-3.0.x/tobago-example/tobago-example-demo/src/main/webapp/content/30-concept/20-format/format.xhtml (added)
+++ myfaces/tobago/branches/tobago-3.0.x/tobago-example/tobago-example-demo/src/main/webapp/content/30-concept/20-format/format.xhtml Wed Mar 16 16:50:33 2016
@@ -0,0 +1,53 @@
+<?xml version="1.0" encoding="UTF-8"?>
+
+<!--
+ * 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.
+-->
+
+<ui:composition template="/main.xhtml"
+                xmlns:tc="http://myfaces.apache.org/tobago/component"
+                xmlns:ui="http://java.sun.com/jsf/facelets"
+                xmlns:fn="http://java.sun.com/jsp/jstl/functions">
+
+  <ui:param name="title" value="Formatting"/>
+
+  <p>
+    Use the EL functions <code>tc:format1</code> ...
+    <code>tc:format9</code> to replace up to 9 variables in a given internationalized text.
+    This work like in [TODO ref java.text.MessageFormat].
+  </p>
+  <p>
+    This is also analog to the <code>&lt;h:outputFormat></code> tag, but can be applied to generally each attribute
+    in each tag and also in content.
+  </p>
+  <ul>
+    <li><tc:out value="#{tc:format1(overviewBundle.format1, 695700)}" id="format1"/></li>
+    <li><tc:out value="#{tc:format3(overviewBundle.format3, 5800, 15700000, 5000000)}" id="format3"/></li>
+    <li><tc:out value="#{tc:format9(overviewBundle.format9, 0, 1, 2, 3, 4, 5, 6, 7, 8)}" id="format9"/></li>
+  </ul>
+  The message format is the bundles are:
+  <ul>
+    <li><tc:out value="#{overviewBundle.format1}" id="mformat1"/></li>
+    <li><tc:out value="#{overviewBundle.format3}" id="mformat3"/></li>
+    <li><tc:out value="#{overviewBundle.format9}" id="mformat9"/></li>
+  </ul>
+
+  <p>
+    You may also use the <code>fn:</code> function library for string manipulation.
+    E.g. uppercase: #{fn:toUpperCase("uppercase")}.
+  </p>
+
+</ui:composition>

Modified: myfaces/tobago/branches/tobago-3.0.x/tobago-example/tobago-example-demo/src/main/webapp/main.xhtml
URL: http://svn.apache.org/viewvc/myfaces/tobago/branches/tobago-3.0.x/tobago-example/tobago-example-demo/src/main/webapp/main.xhtml?rev=1735263&r1=1735262&r2=1735263&view=diff
==============================================================================
--- myfaces/tobago/branches/tobago-3.0.x/tobago-example/tobago-example-demo/src/main/webapp/main.xhtml (original)
+++ myfaces/tobago/branches/tobago-3.0.x/tobago-example/tobago-example-demo/src/main/webapp/main.xhtml Wed Mar 16 16:50:33 2016
@@ -37,8 +37,6 @@
 
       <ui:include src="/menu.xhtml"/>
 
-      <!--<tc:segmentLayout large="6;6">-->
-      <!--<tc:segmentLayout large="#{navigationState.viewSource ? tc:columnPartition('8;4') : tc:columnPartition('6;6')}">-->
       <tc:segmentLayout large="#{tc:columnPartition(navigationState.viewSource ? '8;4' : '12')}">
 
         <tc:flexLayout columns="*;4*">

Modified: myfaces/tobago/branches/tobago-3.0.x/tobago-example/tobago-example-demo/src/main/webapp/tobago-resource/html/standard/standard/property/overview.properties.xml
URL: http://svn.apache.org/viewvc/myfaces/tobago/branches/tobago-3.0.x/tobago-example/tobago-example-demo/src/main/webapp/tobago-resource/html/standard/standard/property/overview.properties.xml?rev=1735263&r1=1735262&r2=1735263&view=diff
==============================================================================
--- myfaces/tobago/branches/tobago-3.0.x/tobago-example/tobago-example-demo/src/main/webapp/tobago-resource/html/standard/standard/property/overview.properties.xml (original)
+++ myfaces/tobago/branches/tobago-3.0.x/tobago-example/tobago-example-demo/src/main/webapp/tobago-resource/html/standard/standard/property/overview.properties.xml Wed Mar 16 16:50:33 2016
@@ -396,4 +396,9 @@
 
     The content has to reside in the /META-INF/resources directory.</entry>
 
+  <entry key="format1">The equatorial radius of the sun is {0} km.</entry>
+  <entry key="format3">The temperature of the sun is {0} K on the surface, but {1} K in the center and {2} K in the corona.</entry>
+  <entry key="format9">Just reordering: {4} - {1} - {6} - {8} - {5} - {3} - {0} - {7} - {2}</entry>
+
+
 </properties>

Modified: myfaces/tobago/branches/tobago-3.0.x/tobago-tool/tobago-tool-apt/src/main/java/org/apache/myfaces/tobago/apt/processor/TaglibGenerator.java
URL: http://svn.apache.org/viewvc/myfaces/tobago/branches/tobago-3.0.x/tobago-tool/tobago-tool-apt/src/main/java/org/apache/myfaces/tobago/apt/processor/TaglibGenerator.java?rev=1735263&r1=1735262&r2=1735263&view=diff
==============================================================================
--- myfaces/tobago/branches/tobago-3.0.x/tobago-tool/tobago-tool-apt/src/main/java/org/apache/myfaces/tobago/apt/processor/TaglibGenerator.java (original)
+++ myfaces/tobago/branches/tobago-3.0.x/tobago-tool/tobago-tool-apt/src/main/java/org/apache/myfaces/tobago/apt/processor/TaglibGenerator.java Wed Mar 16 16:50:33 2016
@@ -130,12 +130,16 @@ public class TaglibGenerator extends Abs
 
     addLeafTextElement(taglibAnnotation.uri(), "namespace", taglib, document);
 
-    final Element function = document.createElement("function");
-    taglib.appendChild(function);
-    addLeafTextElement("columnPartition", "function-name", function, document);
-    addLeafTextElement("org.apache.myfaces.tobago.layout.ColumnPartition", "function-class", function, document);
-    addLeafTextElement("org.apache.myfaces.tobago.layout.ColumnPartition valueOf(java.lang.String)",
-        "function-signature", function, document);
+    // XXX hack: should be configurable or generated from annotations.
+    if ("http://myfaces.apache.org/tobago/component".equals(taglibAnnotation.uri())) {
+      addFunction(document, taglib, "columnPartition", "org.apache.myfaces.tobago.layout.ColumnPartition",
+          "org.apache.myfaces.tobago.layout.ColumnPartition valueOf(java.lang.String)");
+
+      for (int i = 1; i < 10; i++) {
+        addFunction(document, taglib, "format" + i, "org.apache.myfaces.tobago.util.MessageFormat",
+            "java.lang.String format(java.lang.String"+ StringUtils.repeat(", java.lang.Object", i) +")");
+      }
+    }
 
     for (final TypeElement typeElement : getTypes()) {
       if (processingEnv.getElementUtils().getPackageOf(typeElement).equals(packageElement)) {
@@ -165,6 +169,15 @@ public class TaglibGenerator extends Abs
     }
   }
 
+  private void addFunction(Document document, Element taglib, String functionName, String functionClass,
+                           String functionSignature) {
+    final Element function = document.createElement("function");
+    taglib.appendChild(function);
+    addLeafTextElement(functionName, "function-name", function, document);
+    addLeafTextElement(functionClass, "function-class", function, document);
+    addLeafTextElement(functionSignature, "function-signature", function, document);
+  }
+
   protected void appendTag(
       final TypeElement typeElement, final Element parent, final Document document)
       throws ClassNotFoundException {