KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > ibm > icu > impl > ICUData


1 /*
2  *******************************************************************************
3  * Copyright (C) 2004-2005, International Business Machines Corporation and *
4  * others. All Rights Reserved. *
5  *******************************************************************************
6  *
7  * Created on Feb 4, 2004
8  *
9  */

10 package com.ibm.icu.impl;
11
12 import java.io.InputStream JavaDoc;
13 import java.net.URL JavaDoc;
14 import java.security.AccessController JavaDoc;
15 import java.security.PrivilegedAction JavaDoc;
16 import java.util.MissingResourceException JavaDoc;
17
18 /**
19  * Provides access to ICU data files as InputStreams. Implements security checking.
20  */

21 public final class ICUData {
22     /*
23      * Return a URL to the ICU resource names resourceName. The
24      * resource name should either be an absolute path, or a path relative to
25      * com.ibm.icu.impl (e.g., most likely it is 'data/foo'). If required
26      * is true, throw an MissingResourceException instead of returning a null result.
27      */

28     public static boolean exists(final String JavaDoc resourceName) {
29         URL JavaDoc i = null;
30         if (System.getSecurityManager() != null) {
31             i = (URL JavaDoc)AccessController.doPrivileged(new PrivilegedAction JavaDoc() {
32                     public Object JavaDoc run() {
33                         return ICUData.class.getResource(resourceName);
34                     }
35                 });
36         } else {
37             i = ICUData.class.getResource(resourceName);
38         }
39         return i != null;
40     }
41         
42     private static InputStream JavaDoc getStream(final Class JavaDoc root, final String JavaDoc resourceName, boolean required) {
43         InputStream JavaDoc i = null;
44         
45         if (System.getSecurityManager() != null) {
46             i = (InputStream JavaDoc)AccessController.doPrivileged(new PrivilegedAction JavaDoc() {
47                     public Object JavaDoc run() {
48                         return root.getResourceAsStream(resourceName);
49                     }
50                 });
51         } else {
52             i = root.getResourceAsStream(resourceName);
53         }
54
55         if (i == null && required) {
56             throw new MissingResourceException JavaDoc("could not locate data " +resourceName, root.getPackage().getName(), resourceName);
57         }
58         return i;
59     }
60
61     private static InputStream JavaDoc getStream(final ClassLoader JavaDoc loader, final String JavaDoc resourceName, boolean required) {
62         InputStream JavaDoc i = null;
63         if (System.getSecurityManager() != null) {
64             i = (InputStream JavaDoc)AccessController.doPrivileged(new PrivilegedAction JavaDoc() {
65                     public Object JavaDoc run() {
66                         return loader.getResourceAsStream(resourceName);
67                     }
68                 });
69         } else {
70             i = loader.getResourceAsStream(resourceName);
71         }
72         if (i == null && required) {
73             throw new MissingResourceException JavaDoc("could not locate data", loader.toString(), resourceName);
74         }
75         return i;
76     }
77     
78     public static InputStream JavaDoc getStream(ClassLoader JavaDoc loader, String JavaDoc resourceName){
79         return getStream(loader,resourceName, false);
80     }
81
82     public static InputStream JavaDoc getRequiredStream(ClassLoader JavaDoc loader, String JavaDoc resourceName){
83         return getStream(loader,resourceName, true);
84     }
85
86     /*
87      * Convenience override that calls getStream(ICUData.class, resourceName, false);
88      */

89     public static InputStream JavaDoc getStream(String JavaDoc resourceName) {
90         return getStream(ICUData.class, resourceName, false);
91     }
92         
93     /*
94      * Convenience method that calls getStream(ICUData.class, resourceName, true).
95      */

96     public static InputStream JavaDoc getRequiredStream(String JavaDoc resourceName) {
97         return getStream(ICUData.class, resourceName, true);
98     }
99
100     /*
101      * Convenience override that calls getStream(root, resourceName, false);
102      */

103     public static InputStream JavaDoc getStream(Class JavaDoc root, String JavaDoc resourceName) {
104         return getStream(root, resourceName, false);
105     }
106         
107     /*
108      * Convenience method that calls getStream(root, resourceName, true).
109      */

110     public static InputStream JavaDoc getRequiredStream(Class JavaDoc root, String JavaDoc resourceName) {
111         return getStream(root, resourceName, true);
112     }
113 }
114
115
Popular Tags