home *** CD-ROM | disk | FTP | other *** search
/ Java 1.2 How-To / JavaHowTo.iso / 3rdParty / jbuilder / unsupported / JDK1.2beta3 / SOURCE / SRC.ZIP / java / text / resources / LocaleData.java < prev    next >
Encoding:
Java Source  |  1998-03-20  |  8.9 KB  |  235 lines

  1. /*
  2.  * @(#)LocaleData.java    1.19 98/03/18
  3.  *
  4.  * (C) Copyright Taligent, Inc. 1996 - All Rights Reserved
  5.  * (C) Copyright IBM Corp. 1996 - All Rights Reserved
  6.  *
  7.  * Portions copyright (c) 1996-1998 Sun Microsystems, Inc. All Rights Reserved.
  8.  *
  9.  *   The original version of this source code and documentation is copyrighted
  10.  * and owned by Taligent, Inc., a wholly-owned subsidiary of IBM. These
  11.  * materials are provided under terms of a License Agreement between Taligent
  12.  * and Sun. This technology is protected by multiple US and International
  13.  * patents. This notice and attribution to Taligent may not be removed.
  14.  *   Taligent is a registered trademark of Taligent, Inc.
  15.  *
  16.  * Permission to use, copy, modify, and distribute this software
  17.  * and its documentation for NON-COMMERCIAL purposes and without
  18.  * fee is hereby granted provided that this copyright notice
  19.  * appears in all copies. Please refer to the file "copyright.html"
  20.  * for further important copyright and licensing information.
  21.  *
  22.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
  23.  * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  24.  * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  25.  * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
  26.  * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  27.  * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
  28.  *
  29.  */
  30.  
  31. package java.text.resources;
  32.  
  33. import java.util.Locale;
  34. import java.util.ResourceBundle;
  35. import java.util.Hashtable;
  36. import java.util.Enumeration;
  37. import java.util.Vector;
  38. import java.io.File;
  39. import java.io.FileInputStream;
  40. import java.io.IOException;
  41. import java.io.FileNotFoundException;
  42. import java.util.zip.ZipInputStream;
  43. import java.util.zip.ZipEntry;
  44.  
  45. /**
  46.  * This class used to be the base class for all of the LocaleElements classes.  This has
  47.  * been changed so that all of the LocaleElements classes descend from ListResourceBundle.
  48.  * This class now exists only to allow a way to get the list of available resources.  Even
  49.  * this will be changing in the future.
  50.  *
  51.  * @author Asmus Freytag
  52.  * @author Mark Davis
  53.  * @version $Revision: 1.15 $ 03/18/98
  54.  */
  55.  
  56. public class LocaleData {
  57.     /**
  58.      * Returns a list of the installed locales.
  59.      * @param key A resource tag.  Currently, this parameter is ignored.  The obvious
  60.      * intent, however,  is for getAvailableLocales() to return a list of only those
  61.      * locales that contain a resource with the specified resource tag.
  62.      *
  63.      * <p>Before we implement this function this way, however, some thought should be
  64.      * given to whether this is really the right thing to do.  Because of the lookup
  65.      * algorithm, a NumberFormat, for example, is "installed" for all locales.  But if
  66.      * we're trying to put up a list of NumberFormats to choose from, we may want to see
  67.      * only a list of those locales that uniquely define a NumberFormat rather than
  68.      * inheriting one from another locale.  Thus, if fr and fr_CA uniquely define
  69.      * NumberFormat data, but fr_BE doesn't, the user wouldn't see "French (Belgium)" in
  70.      * the list and would go for "French (default)" instead.  Of course, this means
  71.      * "English (United States)" would not be in the list, since it is the default locale.
  72.      * This might be okay, but might be confusing to some users.
  73.      *
  74.      * <p>In addition, the other functions that call getAvailableLocales() don't currently
  75.      * all pass the right thing for "key," meaning that all of these functions should be
  76.      * looked at before anything is done to this function.
  77.      *
  78.      * <p>We recommend that someone take some careful consideration of these issues before
  79.      * modifying this function to pay attention to the "key" parameter.  --rtg 1/26/98
  80.      */
  81.     public static Locale[] getAvailableLocales(String key)
  82.     {
  83.         return localeList;  // hard-coded for now
  84.     }
  85.  
  86.     // ========== privates ==========
  87.  
  88.     private static Vector classPathSegments = new Vector();
  89.     private static Locale[] localeList;
  90.     private static final String PACKAGE = "java.text.resources";
  91.     private static final String PREFIX = "LocaleElements_";
  92.  
  93.     static {
  94.     String classPath;
  95.         try {
  96.         java.security.AccessController.beginPrivileged();
  97.  
  98.         // Search combined system and application class path
  99.         classPath = System.getProperty("java.sys.class.path");
  100.         String s = System.getProperty("java.class.path");
  101.         if (s != null && s.length() != 0) {
  102.             classPath += File.pathSeparator + s;
  103.         }  
  104.     } finally {
  105.         java.security.AccessController.endPrivileged();
  106.     }
  107.         while (classPath != null && classPath.length() != 0) {
  108.             int i = classPath.lastIndexOf(java.io.File.pathSeparatorChar);
  109.             String dir = classPath.substring(i + 1);
  110.             if (i == -1) {
  111.                 classPath = null;
  112.             } else {
  113.                 classPath = classPath.substring(0, i);
  114.             }
  115.             classPathSegments.insertElementAt(dir, 0);
  116.         }
  117.  
  118.     String[] classList = null;
  119.         try {
  120.         java.security.AccessController.beginPrivileged();
  121.             classList = getClassList(PACKAGE, PREFIX);
  122.     } finally {
  123.         java.security.AccessController.endPrivileged();
  124.     }
  125.     int    plen = PREFIX.length();
  126.     localeList = new Locale[classList.length];
  127.     for (int i = 0; i < classList.length; i++) {
  128.         int p2 = 0;
  129.         int p1 = classList[i].indexOf('_', plen);
  130.         String lang = "";
  131.         String region = "";
  132.         String var = "";
  133.  
  134.         if (p1 == -1) {
  135.             lang = classList[i].substring(plen);
  136.         } else {
  137.             lang = classList[i].substring(plen, p1);
  138.         p2 = classList[i].indexOf('_', p1 + 1);
  139.             if (p2 == -1) {
  140.                 region = classList[i].substring(p1 + 1);
  141.         } else {
  142.                 region = classList[i].substring(p1 + 1, p2);
  143.             if (p2 < classList[i].length())
  144.                     var = classList[i].substring(p2 + 1);
  145.         }
  146.         }
  147.         localeList[i] = new Locale(lang, region, var);
  148.     }
  149.     }
  150.  
  151.     /**
  152.      * Walk through CLASSPATH and find class list from a package.
  153.      * The class names start with prefix string
  154.      * @param package name, class name prefix
  155.      * @return class list in an array of String
  156.      */
  157.     private static String[] getClassList(String pkgName, String prefix) {
  158.         Vector listBuffer = new Vector();
  159.         String packagePath = pkgName.replace('.', File.separatorChar)
  160.             + File.separatorChar;
  161.     for (int i = 0; i < classPathSegments.size(); i++){
  162.         String onePath = (String) classPathSegments.elementAt(i);
  163.         File f = new File(onePath);
  164.             if (!f.exists())
  165.         continue;
  166.             if (f.isFile())
  167.                 scanFile(f, packagePath, listBuffer, prefix);
  168.             else if (f.isDirectory()) {
  169.         String fullPath;
  170.         if (onePath.endsWith(File.separator))
  171.             fullPath = onePath + packagePath;
  172.         else
  173.             fullPath = onePath + File.separatorChar + packagePath;
  174.                 File dir = new File(fullPath);
  175.                 if (dir.exists() && dir.isDirectory())
  176.                     scanDir(dir, listBuffer, prefix);
  177.             }
  178.         }
  179.     String[] classNames = new String[listBuffer.size()];
  180.     listBuffer.copyInto(classNames);
  181.     return classNames;
  182.     }
  183.  
  184.     private static void addClass (String className, Vector listBuffer,
  185.         String prefix) {
  186.     if (className != null && className.startsWith(prefix)
  187.         && !listBuffer.contains(className))
  188.         listBuffer.addElement(className);
  189.     }
  190.  
  191.     private static String midString(String str, String pre, String suf) {
  192.     String midStr;
  193.     if (str.startsWith(pre) && str.endsWith(suf))
  194.         midStr = str.substring(pre.length(), str.length() - suf.length());
  195.     else
  196.         midStr = null;
  197.     return midStr;
  198.     }
  199.  
  200.     private static void scanDir(File dir, Vector listBuffer, String prefix) {
  201.     String[] fileList = dir.list();
  202.     for (int i = 0; i < fileList.length; i++) {
  203.         addClass(midString(fileList[i], "", ".class"), listBuffer, prefix);
  204.     }
  205.     }
  206.  
  207.     private static void scanFile(File f, String packagePath, Vector listBuffer,
  208.                 String prefix) {
  209.         try {
  210.             ZipInputStream zipFile = new ZipInputStream(new FileInputStream(f));
  211.         boolean gotThere = false;
  212.         ZipEntry entry;
  213.             while ((entry = zipFile.getNextEntry()) != null) {
  214.             String eName = entry.getName();
  215.         if (eName.startsWith(packagePath)) {
  216.             gotThere = true;
  217.             if (eName.endsWith(".class")) {
  218.             addClass(midString(eName, packagePath, ".class"),
  219.                     listBuffer, prefix);
  220.             }
  221.         } else {
  222.             if (gotThere)    // Found the package, now we are leaving
  223.                 break;
  224.         }
  225.             }
  226.         } catch (FileNotFoundException e) {
  227.         System.out.println("file not found:" + e);
  228.         } catch (IOException e) {
  229.         System.out.println("file IO Exception:" + e);
  230.         } catch (Exception e) {
  231.         System.out.println("Exception:" + e);
  232.     }
  233.     }
  234. }
  235.