Author: simoneg
Date: Thu Nov 19 22:53:09 2009
New Revision: 882343

URL: http://svn.apache.org/viewvc?rev=882343&view=rev
Log:
LABS-494 : improving i18n matching system to see if we canuse it for more 
generic purposes

Added:
    
labs/magma/trunk/foundation-i18n/src/test/java/org/apache/magma/i18n/LocaleHolderTest.java
Modified:
    
labs/magma/trunk/foundation-i18n/src/main/java/org/apache/magma/i18n/ContextMatrix.java
    
labs/magma/trunk/foundation-i18n/src/main/java/org/apache/magma/i18n/I18nContextElementsInstall.aj
    
labs/magma/trunk/foundation-i18n/src/main/java/org/apache/magma/i18n/LocaleHolder.java
    
labs/magma/trunk/foundation-i18n/src/test/java/org/apache/magma/i18n/MatchingTest.java

Modified: 
labs/magma/trunk/foundation-i18n/src/main/java/org/apache/magma/i18n/ContextMatrix.java
URL: 
http://svn.apache.org/viewvc/labs/magma/trunk/foundation-i18n/src/main/java/org/apache/magma/i18n/ContextMatrix.java?rev=882343&r1=882342&r2=882343&view=diff
==============================================================================
--- 
labs/magma/trunk/foundation-i18n/src/main/java/org/apache/magma/i18n/ContextMatrix.java
 (original)
+++ 
labs/magma/trunk/foundation-i18n/src/main/java/org/apache/magma/i18n/ContextMatrix.java
 Thu Nov 19 22:53:09 2009
@@ -70,33 +70,18 @@
         * @param mc The current context stack, this is usually a {...@link 
SubRunningContext} instance.
         * @return an integer value determining the score.
         */
-       public int getScore(Stack<ContextElement> mc) {
+       public int getScore(String[] mc) {
                //int maxscore = (mc.size() * (mc.size() + 1)) / 2;
                if (parts.length == 1) return 0;
                int total = -1;
                int upto = 0;
                int pos = 1;
-               for (ContextElement ele : mc) {
-                       if (ele instanceof I18nContextElement) {
-                               if (ele instanceof 
I18nSingleSegmentContextElement) {
-                                       String seg = 
((I18nSingleSegmentContextElement)ele).getI18nSegment();
-                                       pos++;
-                                       if (parts[upto].equalsIgnoreCase(seg)) {
-                                               upto++;
-                                               total += pos;
-                                               if (upto == parts.length - 1) 
return total;
-                                       }
-                               } else {
-                                       String[] segs = 
((I18nMultiSegmentContextElement)ele).getI18nSegments();
-                                       for (String seg : segs) {
-                                               pos++;
-                                               if 
(parts[upto].equalsIgnoreCase(seg)) {
-                                                       upto++;
-                                                       total += pos;
-                                                       if (upto == 
parts.length - 1) return total;
-                                               }                               
                
-                                       }
-                               }
+               for (String seg : mc) {
+                       pos++;
+                       if (parts[upto].equalsIgnoreCase(seg)) {
+                               upto++;
+                               total += pos;
+                               if (upto == parts.length - 1) return total;
                        }
                }
                return -1;

Modified: 
labs/magma/trunk/foundation-i18n/src/main/java/org/apache/magma/i18n/I18nContextElementsInstall.aj
URL: 
http://svn.apache.org/viewvc/labs/magma/trunk/foundation-i18n/src/main/java/org/apache/magma/i18n/I18nContextElementsInstall.aj?rev=882343&r1=882342&r2=882343&view=diff
==============================================================================
--- 
labs/magma/trunk/foundation-i18n/src/main/java/org/apache/magma/i18n/I18nContextElementsInstall.aj
 (original)
+++ 
labs/magma/trunk/foundation-i18n/src/main/java/org/apache/magma/i18n/I18nContextElementsInstall.aj
 Thu Nov 19 22:53:09 2009
@@ -29,8 +29,12 @@
  */
 public aspect I18nContextElementsInstall {
 
+       private String I18nSingleSegmentContextElement.cachedTos = null;
+       
        public String I18nSingleSegmentContextElement.getI18nSegment() {
-               return this.toString();
+               if (cachedTos == null) 
+                       cachedTos = this.toString();
+               return cachedTos;
        }
        
        declare parents : StringContextElement implements 
I18nSingleSegmentContextElement;
@@ -38,8 +42,12 @@
 
        declare parents : MethodContextElement implements 
I18nMultiSegmentContextElement;
 
+       private String[] MethodContextElement.cachedTos = null;
+       
        public String[] MethodContextElement.getI18nSegments() {
-               return new String[] { 
this.getMethod().getDeclaringClass().getSimpleName() , 
this.getMethod().getName() };
+               if (cachedTos == null) 
+                       cachedTos = new String[] { 
this.getMethod().getDeclaringClass().getSimpleName() , 
this.getMethod().getName() };
+               return cachedTos;
        }
        
 }

Modified: 
labs/magma/trunk/foundation-i18n/src/main/java/org/apache/magma/i18n/LocaleHolder.java
URL: 
http://svn.apache.org/viewvc/labs/magma/trunk/foundation-i18n/src/main/java/org/apache/magma/i18n/LocaleHolder.java?rev=882343&r1=882342&r2=882343&view=diff
==============================================================================
--- 
labs/magma/trunk/foundation-i18n/src/main/java/org/apache/magma/i18n/LocaleHolder.java
 (original)
+++ 
labs/magma/trunk/foundation-i18n/src/main/java/org/apache/magma/i18n/LocaleHolder.java
 Thu Nov 19 22:53:09 2009
@@ -20,6 +20,7 @@
 import java.io.IOException;
 import java.net.URL;
 import java.util.ArrayList;
+import java.util.Arrays;
 import java.util.Collections;
 import java.util.Enumeration;
 import java.util.HashMap;
@@ -42,10 +43,14 @@
  */
 public class LocaleHolder {
 
-       private Locale locale;
-       private SettingsHolder messages = new SettingsHolder();
+       protected static final ContextMatrix CACHE_NONE = new 
ContextMatrix("none", "none");
        
-       private Map<String, List<ContextMatrix>> firstLookup = new 
HashMap<String, List<ContextMatrix>>();
+       protected Locale locale;
+       protected SettingsHolder messages = new SettingsHolder();
+       protected Map<String, ContextMatrix> cache = new HashMap<String, 
ContextMatrix>();
+       
+       
+       protected Map<String, List<ContextMatrix>> firstLookup = new 
HashMap<String, List<ContextMatrix>>();
        
        /**
         * Creates an instance
@@ -57,7 +62,7 @@
            buildMatrix();
        }
 
-       private void initMessages() {
+       protected void initMessages() {
                messages.initing();
                if (messages.isInited()) return;
                
@@ -90,7 +95,7 @@
                messages.inited();
        }
 
-       private void load(String base, List<Locale> locales) {
+       protected void load(String base, List<Locale> locales) {
                for (Locale locale : locales) {
                        try {
                                String url = base + 
locale.toString().toLowerCase() + ".properties";
@@ -141,20 +146,55 @@
                        }
                }
                //ct.push(deh);
-               int topscore = -1;
-               ContextMatrix winner = null;
-               for (ContextMatrix cm : list) {
-                       int score = cm.getScore(ct);
-                       if (score > topscore) {
-                               topscore = score;
-                               winner = cm;
+               String[] strings = stringify(ct);
+               StringBuilder keysb = new StringBuilder();
+               for (String string : strings) {
+                       keysb.append(string);
+                       keysb.append('.');
+               }
+               String key = keysb.toString();
+               ContextMatrix winner = cache.get(key);
+               if (winner == CACHE_NONE) return original;
+               if (winner == null) {
+                       int topscore = -1;
+                       for (ContextMatrix cm : list) {
+                               int score = cm.getScore(strings);
+                               if (score > topscore) {
+                                       topscore = score;
+                                       winner = cm;
+                               }
+                       }
+                       //ct.pop();
+                       if (winner == null) {
+                               cache.put(key, CACHE_NONE);
+                               return original;
+                       } else {
+                               cache.put(key, winner);
                        }
                }
-               //ct.pop();
-               if (winner == null) return original;
                return winner.getValue();
        }
        
+       protected static String[] stringify(Stack<ContextElement> ct) {
+               String[] ret = new String[ct.size() * 2];
+               int i = 0;
+               for (ContextElement ele : ct) {
+                       if (ele instanceof I18nContextElement) {
+                               if (ele instanceof 
I18nSingleSegmentContextElement) {
+                                       String seg = 
((I18nSingleSegmentContextElement)ele).getI18nSegment();
+                                       ret[i++] = seg;
+                               } else {
+                                       String[] segs = 
((I18nMultiSegmentContextElement)ele).getI18nSegments();
+                                       for (String seg : segs) {
+                                               ret[i++] = seg;
+                                       }
+                               }
+                       }
+               }
+               ret = Arrays.copyOf(ret, i);
+               return ret;
+       }
+       
        /**
         * Transforms a string into a key.
         * 

Added: 
labs/magma/trunk/foundation-i18n/src/test/java/org/apache/magma/i18n/LocaleHolderTest.java
URL: 
http://svn.apache.org/viewvc/labs/magma/trunk/foundation-i18n/src/test/java/org/apache/magma/i18n/LocaleHolderTest.java?rev=882343&view=auto
==============================================================================
--- 
labs/magma/trunk/foundation-i18n/src/test/java/org/apache/magma/i18n/LocaleHolderTest.java
 (added)
+++ 
labs/magma/trunk/foundation-i18n/src/test/java/org/apache/magma/i18n/LocaleHolderTest.java
 Thu Nov 19 22:53:09 2009
@@ -0,0 +1,30 @@
+package org.apache.magma.i18n;
+
+import static org.junit.Assert.*;
+import static org.hamcrest.CoreMatchers.*;
+
+import java.lang.reflect.Method;
+import java.util.Stack;
+
+import org.apache.magma.basics.context.ContextElement;
+import org.apache.magma.basics.context.MethodContextElement;
+import org.apache.magma.basics.context.StringContextElement;
+import org.junit.Test;
+
+
+public class LocaleHolderTest {
+
+       @Test
+       public void stringify() throws Exception {
+               Stack<ContextElement> elements = new Stack<ContextElement>();
+               elements.push(new StringContextElement("text"));
+               Method m = LocaleHolderTest.class.getMethod("stringify");
+               elements.push(new MethodContextElement(m, new Object[0]));
+               
+               String[] strings = LocaleHolder.stringify(elements);
+               assertThat(strings[0], equalTo("text"));
+               assertThat(strings[1], 
equalTo(LocaleHolderTest.class.getSimpleName()));
+               assertThat(strings[2], equalTo("stringify"));
+       }
+       
+}

Modified: 
labs/magma/trunk/foundation-i18n/src/test/java/org/apache/magma/i18n/MatchingTest.java
URL: 
http://svn.apache.org/viewvc/labs/magma/trunk/foundation-i18n/src/test/java/org/apache/magma/i18n/MatchingTest.java?rev=882343&r1=882342&r2=882343&view=diff
==============================================================================
--- 
labs/magma/trunk/foundation-i18n/src/test/java/org/apache/magma/i18n/MatchingTest.java
 (original)
+++ 
labs/magma/trunk/foundation-i18n/src/test/java/org/apache/magma/i18n/MatchingTest.java
 Thu Nov 19 22:53:09 2009
@@ -42,7 +42,7 @@
                ContextMatrix cm = new ContextMatrix(getClass().getSimpleName() 
+ ".test", "test");
                assertThat(cm.getMainDiscriminator(), equalTo("test"));
                
-               int score = cm.getScore(mc);
+               int score = cm.getScore(LocaleHolder.stringify(mc));
                assertThat(score, equalTo(1));
        }
 
@@ -52,7 +52,7 @@
                mc.push(getClass());
                
                ContextMatrix cm = new ContextMatrix("test", "test");
-               int score = cm.getScore(mc);
+               int score = cm.getScore(LocaleHolder.stringify(mc));
                assertTrue("Expected a match", score > -1);             
        }
        
@@ -62,7 +62,7 @@
                
                ContextMatrix cm = new ContextMatrix(getClass().getSimpleName() 
+ ".test", "test");
                
-               int score = cm.getScore(mc);
+               int score = cm.getScore(LocaleHolder.stringify(mc));
                assertThat(score, equalTo(-1));         
        }
        
@@ -74,7 +74,7 @@
                
                ContextMatrix cm = new ContextMatrix("testingthejump.test", 
"test");
                
-               int score = cm.getScore(mc);
+               int score = cm.getScore(LocaleHolder.stringify(mc));
                assertTrue("Expected a match", score > -1);             
        }
        
@@ -85,9 +85,9 @@
                mc.push(getClass());
                
                ContextMatrix cm1 = new ContextMatrix("testingthejump.test", 
"test");
-               int score1 = cm1.getScore(mc);
+               int score1 = cm1.getScore(LocaleHolder.stringify(mc));
                ContextMatrix cm2 = new ContextMatrix("test", "test");
-               int score2 = cm2.getScore(mc);
+               int score2 = cm2.getScore(LocaleHolder.stringify(mc));
                assertTrue(score1 + " is not > than " + score2, score1 > 
score2);               
        }
        
@@ -106,9 +106,9 @@
                mc.push("field2");
                
                ContextMatrix cm1 = new ContextMatrix("UserBean.field2.key", 
"key");
-               int score1 = cm1.getScore(mc);
+               int score1 = cm1.getScore(LocaleHolder.stringify(mc));
                ContextMatrix cm2 = new ContextMatrix("UserBean.field1.key", 
"key");
-               int score2 = cm2.getScore(mc);
+               int score2 = cm2.getScore(LocaleHolder.stringify(mc));
                assertTrue(score1 + " is not > than " + score2, score1 > 
score2);               
        }
        



---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to