KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > i18n > ResourceHolder


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20
21 package org.netbeans.modules.i18n;
22
23
24 import java.io.IOException JavaDoc;
25 import java.util.Arrays JavaDoc;
26
27 import org.openide.loaders.DataObject;
28
29 /**
30  * Abstract class which implementation's are wrappers for holders of properties resources.
31  * Typically such object is <code>PropertiesDataObject</code> which represents bundle
32  * of .properties files. But it can be also some object representing other type of resources
33  * e.g. subclass of <code>ListResourceBundle</code> class or in case of i18n-ing JSP pages
34  * object representing tag library etc.
35  *
36  * @author Peter Zavadsky
37  */

38 public abstract class ResourceHolder {
39     
40     /** Instance of resource bundle object. */
41     protected DataObject resource;
42     
43     /** Classes which instances as resources can be hold by this <code>ResourceHolder</code>. */
44     protected final Class JavaDoc[] resourceClasses;
45
46     
47     /** Construct resource holder. */
48     public ResourceHolder(Class JavaDoc[] resourceClasses) {
49         if(resourceClasses == null || resourceClasses.length == 0)
50             throw new IllegalArgumentException JavaDoc();
51         
52         this.resourceClasses = resourceClasses;
53     }
54     
55     
56     /** Setter for </code>resource</code>. */
57     public void setResource(DataObject resource) {
58         if (resource == null) {
59             this.resource = null;
60             return;
61         }
62
63         Class JavaDoc clazz = resource.getClass();
64
65         // Check if the class of parameter is valid for this ResourceHolder.
66
if(!Arrays.asList(resourceClasses).contains(clazz))
67             throw new IllegalArgumentException JavaDoc();
68
69         if(!resource.equals(this.resource))
70             this.resource = resource;
71     }
72     
73     /** Getter for </code>resource</code>. */
74     public DataObject getResource() {
75         return resource;
76     }
77
78     /** Getter for supported <code>resourceClasses</code>. */
79     public Class JavaDoc[] getResourceClasses() {
80         return resourceClasses;
81     }
82     
83     /** Gets all keys which are stored in underlying resource object. */
84     public abstract String JavaDoc[] getAllKeys();
85     
86     /** Gets value for specified key.
87      * @return value for key or null if such key os not stored in resource */

88     public abstract String JavaDoc getValueForKey(String JavaDoc key);
89     
90     /** Gets comment for specified key.
91      * @return value for key or null if such key os not stored in resource */

92     public abstract String JavaDoc getCommentForKey(String JavaDoc key);
93     
94     /**
95      * Adds new property (key-value pair) to resource object.
96      * Behave according to settings.
97      */

98     public void addProperty(Object JavaDoc key, Object JavaDoc value, String JavaDoc comment) {
99         boolean overwriteValues = I18nUtil.getOptions().isReplaceResourceValue();
100         addProperty(key, value, comment, overwriteValues);
101     }
102     
103     /** Adds new property (key-value pair) to resource object, with forcing
104      * of reset the value for existing key in all locales. */

105     public abstract void addProperty(Object JavaDoc key, Object JavaDoc value, String JavaDoc comment, boolean forceNewValue);
106     
107     /** Gets template for reosurce data object. Used by instatianing.
108      * @param clazz <code>Class</code> of object to instantaniate. Have to be one of supported classes. */

109     public final DataObject getTemplate(Class JavaDoc clazz) throws IOException JavaDoc {
110         if(!Arrays.asList(resourceClasses).contains(clazz))
111             throw new IllegalArgumentException JavaDoc();
112         
113         return createTemplate(clazz);
114     }
115     
116     /** Creates template of type clazz. */
117     protected abstract DataObject createTemplate(Class JavaDoc clazz) throws IOException JavaDoc;
118
119 }
120
121
Popular Tags