KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > update > core > model > ModelObject


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 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.update.core.model;
12
13 import java.lang.reflect.Array JavaDoc;
14 import java.net.MalformedURLException JavaDoc;
15 import java.net.URL JavaDoc;
16 import java.net.URLClassLoader JavaDoc;
17 import java.util.HashMap JavaDoc;
18 import java.util.Iterator JavaDoc;
19 import java.util.List JavaDoc;
20 import java.util.Locale JavaDoc;
21 import java.util.Map JavaDoc;
22 import java.util.MissingResourceException JavaDoc;
23 import java.util.ResourceBundle JavaDoc;
24 import java.util.Set JavaDoc;
25
26 import org.eclipse.core.runtime.Assert;
27 import org.eclipse.core.runtime.IPath;
28 import org.eclipse.core.runtime.Path;
29 import org.eclipse.core.runtime.PlatformObject;
30 import org.eclipse.update.core.Feature;
31 import org.eclipse.update.core.SiteManager;
32 import org.eclipse.update.internal.core.Messages;
33 import org.eclipse.update.internal.core.UpdateCore;
34 import org.eclipse.update.internal.core.UpdateManagerUtils;
35
36 /**
37  * Root model object. Extended by all model objects.
38  * <p>
39  * This class cannot be instantiated and must be subclassed.
40  * </p>
41  * <p>
42  * <b>Note:</b> This class/interface is part of an interim API that is still under development and expected to
43  * change significantly before reaching stability. It is being made available at this early stage to solicit feedback
44  * from pioneering adopters on the understanding that any code that uses this API will almost certainly be broken
45  * (repeatedly) as the API evolves.
46  * </p>
47  */

48 public abstract class ModelObject extends PlatformObject {
49
50     private boolean readOnly = false;
51
52     private static final String JavaDoc KEY_PREFIX = "%"; //$NON-NLS-1$
53
private static final String JavaDoc KEY_DOUBLE_PREFIX = KEY_PREFIX + KEY_PREFIX;
54
55     private static Map JavaDoc bundles;
56
57     /**
58      * Creates a base model object.
59      *
60      * @since 2.0
61      */

62     protected ModelObject() {
63     }
64
65     /**
66      * Checks that this model object is writeable. A runtime exception
67      * is thrown if it is not.
68      *
69      * @since 2.0
70      */

71     protected final void assertIsWriteable() {
72         Assert.isTrue(!isReadOnly(), Messages.ModelObject_ModelReadOnly);
73     }
74
75     /**
76      * Sets this model object and all of its descendents to be read-only.
77      * Subclasses may extend this implementation.
78      *
79      * @see #isReadOnly
80      * @since 2.0
81      */

82     public void markReadOnly() {
83         readOnly = true;
84     }
85
86     /**
87      * Returns whether or not this model object is read-only.
88      *
89      * @return <code>true</code> if this model object is read-only,
90      * <code>false</code> otherwise
91      * @see #markReadOnly
92      * @since 2.0
93      */

94     public boolean isReadOnly() {
95         return readOnly;
96     }
97
98     /**
99      * Delegate setting of read-only
100      *
101      * @param o object to delegate to. Must be of type ModelObject.
102      * @see #isReadOnly
103      * @since 2.0
104      */

105     protected void markReferenceReadOnly(ModelObject o) {
106         if (o == null)
107             return;
108         o.markReadOnly();
109     }
110
111     /**
112      * Delegate setting of read-only
113      *
114      * @param o object array to delegate to. Each element must be of type ModelObject.
115      * @see #isReadOnly
116      * @since 2.0
117      */

118     protected void markListReferenceReadOnly(ModelObject[] o) {
119         if (o == null)
120             return;
121         for (int i = 0; i < o.length; i++) {
122             o[i].markReadOnly();
123         }
124     }
125
126     /**
127      * Resolve the model element. This method allows any relative URL strings
128      * to be resolved to actual URL. It also allows any translatable strings
129      * to be localized.
130      *
131      * Subclasses need to override this method to perform the actual resolution.
132      * @param base base URL.
133      * @param bundleURL resource bundle URL.
134      * @exception MalformedURLException
135      * @since 2.0
136      */

137     public void resolve(URL JavaDoc base, URL JavaDoc bundleURL) throws MalformedURLException JavaDoc {
138         return;
139     }
140
141     /**
142      * Delegate resolution to referenced model
143      *
144      * @param o object to delegate to. Must be of type ModelObject.
145      * @param url base URL.
146      * @param bundleURL resource bundle URL.
147      * @exception MalformedURLException
148      * @since 2.0
149      */

150     protected void resolveReference(ModelObject o, URL JavaDoc url, URL JavaDoc bundleURL) throws MalformedURLException JavaDoc {
151         if (o == null)
152             return;
153         o.resolve(url, bundleURL);
154     }
155
156     /**
157      * Delegate resolution to list of referenced models
158      *
159      * @param o object array to delegate to. Each element must be of type ModelObject.
160      * @param url base URL.
161      * @param bundleURL resource bundle URL.
162      * @exception MalformedURLException
163      * @since 2.0
164      */

165     protected void resolveListReference(ModelObject[] o, URL JavaDoc url, URL JavaDoc bundleURL) throws MalformedURLException JavaDoc {
166         if (o == null)
167             return;
168         for (int i = 0; i < o.length; i++) {
169             o[i].resolve(url, bundleURL);
170         }
171     }
172
173     /**
174      * Resolve a URL based on context
175      *
176      * @param context base URL.
177      * @param bundleURL resource bundle URL.
178      * @param urlString url string from model.
179      * @return URL, or <code>null</code>.
180      * @exception MalformedURLException
181      * @since 2.0
182      */

183     protected URL JavaDoc resolveURL(URL JavaDoc context, URL JavaDoc bundleURL, String JavaDoc urlString) throws MalformedURLException JavaDoc {
184
185         // URL string was not specified
186
if (urlString == null || urlString.trim().equals("")) //$NON-NLS-1$
187
return null;
188
189         // check to see if we have NL-sensitive URL
190
String JavaDoc resolvedUrlString = resolveNLString(bundleURL, urlString);
191
192         resolvedUrlString = resolvePlatfromConfiguration(resolvedUrlString);
193
194         // if we don't have a base url, use only the supplied string
195
if (context == null)
196             return new URL JavaDoc(resolvedUrlString);
197
198         // otherwise return new URL in context of base URL
199
return new URL JavaDoc(context, resolvedUrlString);
200     }
201     /**
202      * Resolves the URL based on platfrom Configuration
203      * $os$\$ws$\license.txt will become
204      * win32\win32\license.txt on a system where os=win32 and ws=win32
205      *
206      * @param resolvedUrlString
207      * @return String
208      */

209     private String JavaDoc resolvePlatfromConfiguration(String JavaDoc resolvedUrlString) {
210         int osIndex = resolvedUrlString.indexOf("$os$"); //$NON-NLS-1$
211
if (osIndex != -1)
212             return getExtendedString(resolvedUrlString);
213
214         int wsIndex = resolvedUrlString.indexOf("$ws$"); //$NON-NLS-1$
215
if (wsIndex != -1)
216             return getExtendedString(resolvedUrlString);
217
218         int nlIndex = resolvedUrlString.indexOf("$nl$"); //$NON-NLS-1$
219
if (nlIndex != -1)
220             return getExtendedString(resolvedUrlString);
221
222         int archIndex = resolvedUrlString.indexOf("$arch$"); //$NON-NLS-1$
223
if (archIndex != -1)
224             return getExtendedString(resolvedUrlString);
225
226         return resolvedUrlString;
227     }
228
229     private String JavaDoc getExtendedString(String JavaDoc resolvedUrlString) {
230         IPath path = new Path(resolvedUrlString);
231         path = getExpandedPath(path);
232         if (UpdateCore.DEBUG && UpdateCore.DEBUG_SHOW_WARNINGS) {
233             UpdateCore.warn("Resolved :" + resolvedUrlString + " as:" + path.toOSString()); //$NON-NLS-1$ //$NON-NLS-2$
234
}
235
236         return path.toOSString();
237     }
238
239     private IPath getExpandedPath(IPath path) {
240         String JavaDoc first = path.segment(0);
241         if (first != null) {
242             IPath rest = getExpandedPath(path.removeFirstSegments(1));
243             if (first.equals("$ws$")) { //$NON-NLS-1$
244
path = new Path(SiteManager.getWS()).append(rest);
245             } else if (first.equals("$os$")) { //$NON-NLS-1$
246
path = new Path(SiteManager.getOS()).append(rest);
247             } else if (first.equals("$nl$")) { //$NON-NLS-1$
248
path = new Path(SiteManager.getNL()).append(rest);
249             } else if (first.equals("$arch$")) { //$NON-NLS-1$
250
path = new Path(SiteManager.getOSArch()).append(rest);
251             }
252         }
253         return path;
254     }
255
256     /**
257      * Returns a resource string corresponding to the given argument
258      * value and bundle.
259      * If the argument value specifies a resource key, the string
260      * is looked up in the given resource bundle. If the argument does not
261      * specify a valid key, the argument itself is returned as the
262      * resource string. The key lookup is performed against the
263      * specified resource bundle. If a resource string
264      * corresponding to the key is not found in the resource bundle
265      * the key value, or any default text following the key in the
266      * argument value is returned as the resource string.
267      * A key is identified as a string begining with the "%" character.
268      * Note that the "%" character is stripped off prior to lookup
269      * in the resource bundle.
270      * <p>
271      * For example, assume resource bundle plugin.properties contains
272      * name = Project Name
273      * <pre>
274      * resolveNLString(b,"Hello World") returns "Hello World"</li>
275      * resolveNLString(b,"%name") returns "Project Name"</li>
276      * resolveNLString(b,"%name Hello World") returns "Project Name"</li>
277      * resolveNLString(b,"%abcd Hello World") returns "Hello World"</li>
278      * resolveNLString(b,"%abcd") returns "%abcd"</li>
279      * resolveNLString(b,"%%name") returns "%name"</li>
280      * </pre>
281      * </p>
282      *
283      * @param bundleURL resource bundle url.
284      * @param string translatable string from model
285      * @return string, or <code>null</code>
286      * @since 2.0
287      */

288     protected String JavaDoc resolveNLString(URL JavaDoc bundleURL, String JavaDoc string) {
289
290         if (string == null)
291             return null;
292
293         String JavaDoc s = string.trim();
294
295         if (s.equals("")) //$NON-NLS-1$
296
return string;
297
298         if (!s.startsWith(KEY_PREFIX))
299             return string;
300
301         if (s.startsWith(KEY_DOUBLE_PREFIX))
302             return s.substring(1);
303
304         int ix = s.indexOf(" "); //$NON-NLS-1$
305
String JavaDoc key = ix == -1 ? s : s.substring(0, ix);
306         String JavaDoc dflt = ix == -1 ? s : s.substring(ix + 1);
307
308         ResourceBundle JavaDoc b = getResourceBundle(bundleURL);
309
310         if (b == null)
311             return dflt;
312
313         try {
314             return b.getString(key.substring(1));
315         } catch (MissingResourceException JavaDoc e) {
316             return dflt;
317         }
318     }
319
320     /**
321      * Returns a concrete array type for the elements of the specified
322      * list. The method assumes all the elements of the list are the same
323      * concrete type as the first element in the list.
324      *
325      * @param l list
326      * @return concrete array type, or <code>null</code> if the array type
327      * could not be determined (the list is <code>null</code> or empty)
328      * @since 2.0
329      */

330     protected Object JavaDoc[] arrayTypeFor(List JavaDoc l) {
331         if (l == null || l.size() == 0)
332             return null;
333         return (Object JavaDoc[]) Array.newInstance(l.get(0).getClass(), 0);
334     }
335
336     /**
337      * Returns a concrete array type for the elements of the specified
338      * set. The method assumes all the elements of the set are the same
339      * concrete type as the first element in the set.
340      *
341      * @param s set
342      * @return concrete array type, or <code>null</code> if the array type
343      * could not be determined (the set is <code>null</code> or empty)
344      * @since 2.0
345      */

346     protected Object JavaDoc[] arrayTypeFor(Set JavaDoc s) {
347         if (s == null || s.size() == 0)
348             return null;
349         Iterator JavaDoc i = s.iterator();
350         return (Object JavaDoc[]) Array.newInstance(i.next().getClass(), 0);
351     }
352
353     /**
354         * Helper method to access resouce bundle for feature. The default
355         * implementation attempts to load the appropriately localized
356         * feature.properties file.
357         *
358         * @param url base URL used to load the resource bundle.
359         * @return resource bundle, or <code>null</code>.
360         * @since 2.0
361         */

362     protected ResourceBundle JavaDoc getResourceBundle(URL JavaDoc url) {
363
364         if (url == null)
365             return null;
366
367         if (bundles == null) {
368             bundles = new HashMap JavaDoc();
369         } else {
370             ResourceBundle JavaDoc bundle = (ResourceBundle JavaDoc) bundles.get(url.toExternalForm());
371             if (bundle != null)
372                 return bundle;
373         }
374
375         ResourceBundle JavaDoc bundle = null;
376         try {
377             url = UpdateManagerUtils.asDirectoryURL(url);
378             ClassLoader JavaDoc l = new URLClassLoader JavaDoc(new URL JavaDoc[] { url }, null);
379             bundle = ResourceBundle.getBundle(getPropertyName(), Locale.getDefault(), l);
380             bundles.put(url.toExternalForm(), bundle);
381         } catch (MissingResourceException JavaDoc e) {
382             UpdateCore.warn(e.getLocalizedMessage() + ":" + url.toExternalForm()); //$NON-NLS-1$
383
} catch (MalformedURLException JavaDoc e) {
384             UpdateCore.warn(e.getLocalizedMessage());
385         }
386         return bundle;
387     }
388
389     /**
390      * Method getPropertyName.
391      * @return String
392      */

393     protected String JavaDoc getPropertyName() {
394         return Feature.FEATURE_FILE;
395     }
396
397 }
398
Popular Tags