KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > osgi > framework > internal > core > ManifestLocalization


1 /*******************************************************************************
2  * Copyright (c) 2004, 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.osgi.framework.internal.core;
12
13 import java.io.IOException JavaDoc;
14 import java.io.InputStream JavaDoc;
15 import java.net.URL JavaDoc;
16 import java.util.*;
17 import org.eclipse.osgi.framework.util.Headers;
18 import org.osgi.framework.Bundle;
19 import org.osgi.framework.Constants;
20
21 /**
22  * This class is used by the Bundle Class to localize manifest headers.
23  */

24 public class ManifestLocalization {
25     private AbstractBundle bundle = null;
26     private Dictionary rawHeaders = null;
27     private Dictionary defaultLocaleHeaders = null;
28     private Hashtable cache = new Hashtable(5);
29
30     public ManifestLocalization(AbstractBundle bundle, Dictionary rawHeaders) {
31         this.bundle = bundle;
32         this.rawHeaders = rawHeaders;
33     }
34
35     protected Dictionary getHeaders(String JavaDoc localeString) {
36         if (localeString.length() == 0)
37             return (rawHeaders);
38         boolean isDefaultLocale = false;
39         String JavaDoc defaultLocale = Locale.getDefault().toString();
40         if (localeString.equals(defaultLocale)) {
41             if (defaultLocaleHeaders != null)
42                 return (defaultLocaleHeaders);
43             isDefaultLocale = true;
44         }
45         try {
46             bundle.checkValid();
47         } catch (IllegalStateException JavaDoc ex) {
48             // defaultLocaleHeaders should have been initialized on uninstall
49
if (defaultLocaleHeaders != null)
50                 return defaultLocaleHeaders;
51             return (rawHeaders);
52         }
53         ResourceBundle localeProperties = getResourceBundle(localeString);
54         if (localeProperties == null && !isDefaultLocale)
55             // could not find the requested locale use the default locale
56
localeProperties = getResourceBundle(defaultLocale);
57         Enumeration e = this.rawHeaders.keys();
58         Headers localeHeaders = new Headers(this.rawHeaders.size());
59         while (e.hasMoreElements()) {
60             String JavaDoc key = (String JavaDoc) e.nextElement();
61             String JavaDoc value = (String JavaDoc) this.rawHeaders.get(key);
62             if (value.startsWith("%") && (value.length() > 1)) { //$NON-NLS-1$
63
String JavaDoc propertiesKey = value.substring(1);
64                 try {
65                     value = localeProperties == null ? propertiesKey : (String JavaDoc) localeProperties.getObject(propertiesKey);
66                 } catch (MissingResourceException ex) {
67                     value = propertiesKey;
68                 }
69             }
70             localeHeaders.set(key, value);
71         }
72         localeHeaders.setReadOnly();
73         if (isDefaultLocale) {
74             defaultLocaleHeaders = localeHeaders;
75         }
76         return (localeHeaders);
77     }
78
79     private String JavaDoc[] buildNLVariants(String JavaDoc nl) {
80         ArrayList result = new ArrayList();
81         int lastSeparator;
82         while ((lastSeparator = nl.lastIndexOf('_')) != -1) {
83             result.add(nl);
84             if (lastSeparator != -1) {
85                 nl = nl.substring(0, lastSeparator);
86             }
87         }
88         result.add(nl);
89         // always add the default locale string
90
result.add(""); //$NON-NLS-1$
91
return (String JavaDoc[]) result.toArray(new String JavaDoc[result.size()]);
92     }
93
94     /*
95      * This method find the appropiate Manifest Localization file inside the
96      * bundle. If not found, return null.
97      */

98     protected ResourceBundle getResourceBundle(String JavaDoc localeString) {
99         String JavaDoc propertiesLocation = (String JavaDoc) rawHeaders.get(Constants.BUNDLE_LOCALIZATION);
100         if (propertiesLocation == null) {
101             propertiesLocation = Constants.BUNDLE_LOCALIZATION_DEFAULT_BASENAME;
102         }
103
104         BundleResourceBundle result = (BundleResourceBundle) cache.get(localeString);
105         if (result != null)
106             return (ResourceBundle) (result.isEmpty() ? null : result);
107         String JavaDoc[] nlVarients = buildNLVariants(localeString);
108         BundleResourceBundle parent = null;
109         for (int i = nlVarients.length - 1; i >= 0; i--) {
110             BundleResourceBundle varientBundle = null;
111             URL JavaDoc varientURL = findResource(propertiesLocation + (nlVarients[i].equals("") ? nlVarients[i] : '_' + nlVarients[i]) + ".properties"); //$NON-NLS-1$ //$NON-NLS-2$
112
if (varientURL == null) {
113                 varientBundle = (BundleResourceBundle) cache.get(nlVarients[i]);
114             } else {
115                 InputStream JavaDoc resourceStream = null;
116                 try {
117                     resourceStream = varientURL.openStream();
118                     varientBundle = new LocalizationResourceBundle(resourceStream);
119                 } catch (IOException JavaDoc e) {
120                     // ignore and continue
121
} finally {
122                     if (resourceStream != null) {
123                         try {
124                             resourceStream.close();
125                         } catch (IOException JavaDoc e3) {
126                             //Ignore exception
127
}
128                     }
129                 }
130             }
131
132             if (varientBundle == null) {
133                 varientBundle = new EmptyResouceBundle();
134             }
135             if (parent != null)
136                 varientBundle.setParent((ResourceBundle) parent);
137             cache.put(nlVarients[i], varientBundle);
138             parent = varientBundle;
139         }
140         result = (BundleResourceBundle) cache.get(localeString);
141         return (ResourceBundle) (result.isEmpty() ? null : result);
142     }
143
144     private URL JavaDoc findResource(String JavaDoc resource) {
145         AbstractBundle searchBundle = bundle;
146         if (bundle.isResolved()) {
147             if (bundle.isFragment() && bundle.getHosts() != null) {
148                 //if the bundle is a fragment, look in the host first
149
searchBundle = bundle.getHosts()[0].getBundleHost();
150                 if (searchBundle.getState() == Bundle.UNINSTALLED)
151                     searchBundle = bundle;
152             }
153             return findInResolved(resource, searchBundle);
154         }
155         return findInBundle(resource, searchBundle);
156     }
157
158     private URL JavaDoc findInResolved(String JavaDoc filePath, AbstractBundle bundleHost) {
159
160         URL JavaDoc result = findInBundle(filePath, bundleHost);
161         if (result != null)
162             return result;
163         return findInFragments(filePath, bundleHost);
164     }
165
166     private URL JavaDoc findInBundle(String JavaDoc filePath, AbstractBundle searchBundle) {
167         return searchBundle.getEntry(filePath);
168     }
169
170     private URL JavaDoc findInFragments(String JavaDoc filePath, AbstractBundle searchBundle) {
171         org.osgi.framework.Bundle[] fragments = searchBundle.getFragments();
172         URL JavaDoc fileURL = null;
173         for (int i = 0; fragments != null && i < fragments.length && fileURL == null; i++) {
174             if (fragments[i].getState() != Bundle.UNINSTALLED)
175                 fileURL = fragments[i].getEntry(filePath);
176         }
177         return fileURL;
178     }
179
180     private abstract interface BundleResourceBundle {
181         void setParent(ResourceBundle parent);
182
183         boolean isEmpty();
184     }
185
186     private class LocalizationResourceBundle extends PropertyResourceBundle implements BundleResourceBundle {
187         public LocalizationResourceBundle(InputStream JavaDoc in) throws IOException JavaDoc {
188             super(in);
189         }
190
191         public void setParent(ResourceBundle parent) {
192             super.setParent(parent);
193         }
194
195         public boolean isEmpty() {
196             return false;
197         }
198     }
199
200     private class EmptyResouceBundle extends ResourceBundle implements BundleResourceBundle {
201         public Enumeration getKeys() {
202             return null;
203         }
204
205         protected Object JavaDoc handleGetObject(String JavaDoc arg0) throws MissingResourceException {
206             return null;
207         }
208
209         public void setParent(ResourceBundle parent) {
210             super.setParent(parent);
211         }
212
213         public boolean isEmpty() {
214             if (parent == null)
215                 return true;
216             return ((BundleResourceBundle) parent).isEmpty();
217         }
218     }
219 }
220
Popular Tags