Coverage Report - com.sun.javafx.runtime.util.StringLocalization
 
Classes in this File Line Coverage Branch Coverage Complexity
StringLocalization
90%
52/58
93%
26/28
0
 
 1  
 /*
 2  
  * Copyright 2008 Sun Microsystems, Inc.  All Rights Reserved.
 3  
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 4  
  *
 5  
  * This code is free software; you can redistribute it and/or modify it
 6  
  * under the terms of the GNU General Public License version 2 only, as
 7  
  * published by the Free Software Foundation.  Sun designates this
 8  
  * particular file as subject to the "Classpath" exception as provided
 9  
  * by Sun in the LICENSE file that accompanied this code.
 10  
  *
 11  
  * This code is distributed in the hope that it will be useful, but WITHOUT
 12  
  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 13  
  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 14  
  * version 2 for more details (a copy is included in the LICENSE file that
 15  
  * accompanied this code).
 16  
  *
 17  
  * You should have received a copy of the GNU General Public License version
 18  
  * 2 along with this work; if not, write to the Free Software Foundation,
 19  
  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 20  
  *
 21  
  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
 22  
  * CA 95054 USA or visit www.sun.com if you need additional information or
 23  
  * have any questions.
 24  
  */
 25  
 
 26  
 package com.sun.javafx.runtime.util;
 27  
 
 28  
 import java.util.Collections;
 29  
 import java.util.Map;
 30  
 import java.util.Locale;
 31  
 import java.util.WeakHashMap;
 32  
 import java.util.concurrent.ConcurrentHashMap;
 33  
 import java.util.regex.Pattern;
 34  
 import com.sun.javafx.runtime.util.backport.ResourceBundle;
 35  
 
 36  0
 public class StringLocalization {
 37  
 
 38  8
     private static final Map<ThreadGroup, Map<String, String>> map = 
 39  
         Collections.synchronizedMap(
 40  
             new WeakHashMap<ThreadGroup, Map<String, String>>());
 41  
 
 42  
     public static String getLocalizedString(String scriptName, String explicitKey,
 43  
                                             String literal, Object... embeddedExpr) {
 44  34
         String key = scriptName.replaceAll("/", "\\.");
 45  34
         int lastDot = key.lastIndexOf('.');
 46  34
         if (lastDot != -1) {
 47  8
             key = key.substring(0, lastDot) + "/" + key.substring(lastDot + 1);
 48  
         } else {
 49  26
             key = "/" + key;
 50  
         }
 51  34
         return getLocalizedString(
 52  
             getPropertiesName(key), explicitKey, literal, Locale.getDefault(), embeddedExpr);
 53  
     }
 54  
 
 55  
     public static String getLocalizedString(String propertiesName, String explicitKey, 
 56  
                         String literal, Locale locale, Object... embeddedExpr) {
 57  53
         String localization = literal;
 58  
 
 59  
         try {
 60  53
             ResourceBundle rb = ResourceBundle.getBundle(propertiesName,
 61  
                     locale, getCallerLoader(), FXPropertyResourceBundle.FXPropertiesControl.INSTANCE);
 62  53
             if (explicitKey != null) {
 63  23
                 localization = rb.getString(explicitKey);
 64  23
                 if (explicitKey.equals(localization) && 
 65  
                     !rb.keySet().contains(explicitKey)) {
 66  7
                     localization = literal;
 67  
                 }
 68  
             } else {
 69  30
                 localization = rb.getString(literal.replaceAll("\r\n|\r|\n", "\n"));
 70  
             }
 71  
 
 72  53
             if (embeddedExpr.length != 0) {
 73  3
                 localization = FXFormatter.sprintf(locale, localization, embeddedExpr);
 74  
             }
 75  0
         } catch (Exception e) {
 76  0
             e.printStackTrace();
 77  53
         }
 78  
 
 79  53
         return localization;
 80  
     }
 81  
 
 82  
     public static void associate(String source, String properties) {
 83  5
         getAssociation().put(source, properties);
 84  5
     }
 85  
 
 86  
     public static void dissociate(String source) {
 87  3
         Map<String, String> assoc = getAssociation();
 88  
 
 89  
         // remove itself first
 90  3
         assoc.remove(source);
 91  
 
 92  
         // remove all associationis for source files in that package
 93  3
         if (source.indexOf('/') == -1) {
 94  1
             String toRemove = source + "/";
 95  1
             for (String key : assoc.keySet()) {
 96  1
                 if (key.startsWith(toRemove)) {
 97  1
                     assoc.remove(key);
 98  
                 }
 99  
             }
 100  
         }
 101  3
     }
 102  
 
 103  
     /**
 104  
      * Get the properties file name for the given key, which consists of
 105  
      * 'packageName(+scriptFileName)'. E.g., 'Example.fx' in 'foo.bar' package would have
 106  
      * a key as 'foo.bar+Example', while 'foo.bar' can represent the package itself.
 107  
      * A script file in the unnamed package can be denoted as '+Example'.
 108  
      */
 109  
     public static String getPropertiesName(String key) {
 110  48
         String propertiesName = key.replaceAll("^/", "").replaceAll("/", ".");
 111  48
         Map<String, String> assoc = getAssociation();
 112  48
         Pattern chopoff = Pattern.compile("[\\./][^\\./]*\\z");
 113  
         
 114  
         while (true) {
 115  100
             if (assoc.containsKey(key)) {
 116  6
                 propertiesName = assoc.get(key);
 117  6
                 break;
 118  
             } else {
 119  94
                 if ("".equals(key)) {
 120  42
                     break;
 121  52
                 } else if (chopoff.matcher(key).find()) {
 122  48
                     key = chopoff.matcher(key).replaceAll("");
 123  
                 } else {
 124  4
                     key = "";
 125  
                 }
 126  
             }
 127  
         }
 128  
 
 129  48
         return propertiesName;
 130  
     }
 131  
 
 132  
     private static Map<String, String> getAssociation() {
 133  56
         ThreadGroup tg = Thread.currentThread().getThreadGroup();
 134  56
         Map<String, String> assoc = map.get(tg);
 135  
 
 136  56
         if (assoc == null) {
 137  8
             assoc = new ConcurrentHashMap<String, String>();
 138  8
             map.put(tg, assoc);
 139  
         }
 140  
 
 141  56
         return assoc;
 142  
     }
 143  
 
 144  8
     private static final ClassContext CC = new ClassContext();
 145  8
     private static final String PKGNAME = StringLocalization.class.getPackage().getName();
 146  
     private static ClassLoader getCallerLoader() {
 147  53
         Class[] callers = CC.getClassContext();
 148  
         try {
 149  246
             for (Class c : callers) {
 150  246
                 if (!c.getName().startsWith(PKGNAME)) {
 151  53
                     return c.getClassLoader();
 152  
                 }
 153  
             }
 154  0
         } catch (SecurityException se) {
 155  0
         }
 156  0
         return StringLocalization.class.getClassLoader();
 157  
     }
 158  
 }