Author: simoneg
Date: Thu Nov 19 23:48:28 2009
New Revision: 882365

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

Modified:
    
labs/magma/trunk/foundation-i18n/src/main/java/org/apache/magma/i18n/LocaleHolder.java

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=882365&r1=882364&r2=882365&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 23:48:28 2009
@@ -43,14 +43,14 @@
  */
 public class LocaleHolder {
 
-       protected static final ContextMatrix CACHE_NONE = new 
ContextMatrix("none", "none");
+       protected static final String CACHE_NONE = "___NONE___";
        
        protected Locale locale;
        protected SettingsHolder messages = new SettingsHolder();
-       protected Map<String, ContextMatrix> cache = new HashMap<String, 
ContextMatrix>();
+       protected Map<String, String> cache = new HashMap<String, String>();
        
        
-       protected Map<String, List<ContextMatrix>> firstLookup = new 
HashMap<String, List<ContextMatrix>>();
+       protected Map<String, List<String[]>> firstLookup = new HashMap<String, 
List<String[]>>();
        
        /**
         * Creates an instance
@@ -62,6 +62,9 @@
            buildMatrix();
        }
 
+       /**
+        * Loads correct messages cnsidering the locale.
+        */
        protected void initMessages() {
                messages.initing();
                if (messages.isInited()) return;
@@ -116,14 +119,16 @@
        public void buildMatrix() {
                Map<String, String> all = messages.getAll();
                for (Map.Entry<String, String> entry : all.entrySet()) {
-                       ContextMatrix cm = new ContextMatrix(entry.getKey(), 
entry.getValue());
-                       String discriminator = cm.getMainDiscriminator();
-                       List<ContextMatrix> list = 
firstLookup.get(discriminator);
+                       String[] parts = entry.getKey().split("\\.");
+                       parts = Arrays.copyOf(parts, parts.length + 1);
+                       parts[parts.length - 1] = entry.getValue();
+                       String discriminator = parts[parts.length - 2];
+                       List<String[]> list = firstLookup.get(discriminator);
                        if (list == null) {
-                               list = new ArrayList<ContextMatrix>();
+                               list = new ArrayList<String[]>();
                                firstLookup.put(discriminator, list);
                        }
-                       list.add(cm);
+                       list.add(parts);
                }
        }
        
@@ -138,14 +143,17 @@
         */
        public String getMessage(Stack<ContextElement> ct, String original) {
                String deh = normalize(original);
-               List<ContextMatrix> list = firstLookup.get(deh);
+               List<String[]> list = firstLookup.get(deh);
+               // Fast not found case
                if (list == null || list.size() == 0) return original;
+               
+               // Fast found xxxx=yyyyy case
                if (list.size() == 1) {
-                       if (list.get(0).getParts().length == 1) {
-                               return list.get(0).getValue();
+                       if (list.get(0).length == 2) {
+                               return list.get(0)[1];
                        }
                }
-               //ct.push(deh);
+               
                String[] strings = stringify(ct);
                StringBuilder keysb = new StringBuilder();
                for (String string : strings) {
@@ -153,15 +161,15 @@
                        keysb.append('.');
                }
                String key = keysb.toString();
-               ContextMatrix winner = cache.get(key);
+               String 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);
+                       for (String[] cm : list) {
+                               int score = getScore(cm, strings);
                                if (score > topscore) {
                                        topscore = score;
-                                       winner = cm;
+                                       winner = cm[cm.length - 1];
                                }
                        }
                        //ct.pop();
@@ -172,9 +180,36 @@
                                cache.put(key, winner);
                        }
                }
-               return winner.getValue();
+               return winner;
        }
        
+       /**
+        * Checks the score of two string arrays.
+        * @param parts The keys in the property file
+        * @param mc The keys of the context
+        * @return -1 for no match, 0 for one step match (x=y), increasingly 
higher score for more in depth match
+        */
+       protected static int getScore(String[] parts, String[] mc) {
+               if (parts.length == 1) return 0;
+               int total = -1;
+               int upto = 0;
+               int pos = 1;
+               for (String seg : mc) {
+                       pos++;
+                       if (parts[upto].equalsIgnoreCase(seg)) {
+                               upto++;
+                               total += pos;
+                               if (upto == parts.length - 2) return total;
+                       }
+               }
+               return -1;              
+       }
+
+       /**
+        * Converts a stack of context elements to an array of strings.
+        * @param ct The stack of the current context
+        * @return A string array, interpreting {...@link 
I18nMultiSegmentContextElement} with correct elements
+        */
        protected static String[] stringify(Stack<ContextElement> ct) {
                String[] ret = new String[ct.size() * 2];
                int i = 0;



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

Reply via email to