KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > i18n > java > JavaResourceHolder


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.java;
22
23
24 import java.io.IOException JavaDoc;
25
26 import org.netbeans.modules.i18n.ResourceHolder;
27
28 import org.netbeans.modules.properties.BundleStructure;
29 import org.netbeans.modules.properties.Element;
30 import org.netbeans.modules.properties.PropertiesDataObject;
31 import org.netbeans.modules.properties.PropertiesStructure;
32
33 import org.openide.filesystems.FileObject;
34 import org.openide.filesystems.FileSystem;
35 import org.openide.filesystems.Repository;
36 import org.openide.loaders.DataObject;
37 import org.openide.loaders.DataObjectNotFoundException;
38
39 /**
40  * Resource holder java sources. Now supports .properties bundle files only.
41  *
42  * @author Peter Zavadsky
43  */

44 public class JavaResourceHolder extends ResourceHolder {
45
46     private String JavaDoc selectedLocale;
47
48     /** Constructor. */
49     public JavaResourceHolder() {
50         super(new Class JavaDoc[] {PropertiesDataObject.class});
51     }
52
53     /**
54      * Sets design time localization - i.e. where to look for values
55      * preferentially (methods getValueForKey and getCommentForKey). If not
56      * found in the given file entry directly, then parent entries are tried
57      * (just like ResourceBundle would do at runtime).
58      * @param locale including initial underscore (e.g. _cs_CZ)
59      */

60     public void setLocalization(String JavaDoc locale) {
61         selectedLocale = locale;
62     }
63
64     private String JavaDoc getLocalizationFileName() {
65         return selectedLocale != null && !selectedLocale.equals("") ? // NOI18N
66
resource.getName() + selectedLocale : resource.getName();
67     }
68
69     /** Implements superclass abstract method.
70     /* Gets all keys which are stored in underlying resource object. */

71     public String JavaDoc[] getAllKeys() {
72         if(resource == null)
73             return new String JavaDoc[0];
74
75         return ((PropertiesDataObject)resource).getBundleStructure().getKeys();
76     }
77
78     /**
79      * Finds a free key in the bundle for given suggested key name.
80      */

81     public String JavaDoc findFreeKey(String JavaDoc keySpec) {
82         BundleStructure bundleStructure = ((PropertiesDataObject)resource).getBundleStructure();
83         return bundleStructure != null ? bundleStructure.findFreeKey(keySpec) : null;
84     }
85
86     /** Implements superclass abstract method. Gets value for specified key.
87      * @return value for key or null if such key os not stored in resource */

88     public String JavaDoc getValueForKey(String JavaDoc key) {
89         if(resource == null)
90             return null;
91
92         Element.ItemElem item = getItem(key);
93         return item == null ? null : item.getValue();
94     }
95
96     /** Implemenst superclass abstract method. Gets comment for specified key.
97      * @return value for key or null if such key os not stored in resource */

98     public String JavaDoc getCommentForKey(String JavaDoc key) {
99         if(resource == null)
100             return null;
101
102         Element.ItemElem item = getItem(key);
103         return item == null ? null : item.getComment();
104     }
105
106     /** Helper method. */
107     private Element.ItemElem getItem(String JavaDoc key) {
108         BundleStructure bundleStructure = ((PropertiesDataObject)resource).getBundleStructure();
109         if (bundleStructure == null)
110             return null;
111
112         return bundleStructure.getItem(getLocalizationFileName(), key);
113     }
114
115     /**
116      * Gets all data (values, comments) for given key across all locales.
117      */

118     public Object JavaDoc getAllData(String JavaDoc key) {
119         BundleStructure bundleStructure = ((PropertiesDataObject)resource).getBundleStructure();
120         return bundleStructure != null ? bundleStructure.getAllData(key) : null;
121     }
122
123     /**
124      * Restores data for given key (obtained sooner from getAllData method).
125      */

126     public void setAllData(String JavaDoc key, Object JavaDoc data) {
127         BundleStructure bundleStructure = ((PropertiesDataObject)resource).getBundleStructure();
128         if (bundleStructure != null)
129             bundleStructure.setAllData(key, (String JavaDoc[])data);
130     }
131
132     /** Implements superclass abstract method. Adds new property (key-valkue pair) to resource object.
133      * @param key key value, if it is <code>null</code> nothing is done
134      * @param value 'value' value, can be <code>null</code>
135      * @param comment comment, can be <code>null</code>
136      * @param forceNewValue if there already exists a key forces to reset its value
137      */

138     public void addProperty(Object JavaDoc key, Object JavaDoc value, String JavaDoc comment, boolean forceNewValue) {
139         if(resource == null || key == null) return;
140
141         String JavaDoc keyValue = key.toString();
142         String JavaDoc valueValue = value == null ? "" : value.toString(); // NOI18N
143
String JavaDoc commentValue = comment;
144         
145         // write to bundle file(s)
146
BundleStructure bundleStructure = ((PropertiesDataObject)resource).getBundleStructure();
147         if (bundleStructure != null) {
148             bundleStructure.addItem(getLocalizationFileName(),
149                                     keyValue, valueValue, commentValue,
150                                     forceNewValue);
151         }
152     }
153
154     /**
155      * Removes property of given key from all locale files of the bundle.
156      */

157     public void removeProperty(Object JavaDoc key) {
158         BundleStructure bundleStructure = ((PropertiesDataObject)resource).getBundleStructure();
159         if (bundleStructure != null)
160             bundleStructure.removeItem(key.toString());
161     }
162
163     /** Implements superclass abstract method. Creates template of type clazz
164      * which have to be of <code>PropertiesDataObject</code> type in our case. */

165     protected DataObject createTemplate(Class JavaDoc clazz) throws IOException JavaDoc {
166         return getTemplate();
167     }
168
169     /**
170      * Returns template data object for properties file.
171      */

172     public static DataObject getTemplate() throws IOException JavaDoc {
173         FileSystem defaultFS = Repository.getDefault().getDefaultFileSystem();
174
175         FileObject fileObject = defaultFS.findResource("Templates/Other/properties.properties"); // NOI18N
176

177         if(fileObject == null)
178             throw new IOException JavaDoc(Util.getString("EXC_TemplateNotFound"));
179
180         try {
181             return DataObject.find(fileObject);
182         } catch(DataObjectNotFoundException e) {
183             throw new IOException JavaDoc(Util.getString("EXC_TemplateNotFound"));
184         }
185     }
186 }
187
Popular Tags