KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > properties > Util


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.properties;
22
23
24 import java.io.IOException JavaDoc;
25 import java.text.MessageFormat JavaDoc;
26 import java.util.Locale JavaDoc;
27 import org.openide.DialogDisplayer;
28 import org.openide.NotifyDescriptor;
29 import org.openide.cookies.SaveCookie;
30 import org.openide.filesystems.FileObject;
31 import org.openide.filesystems.FileSystem;
32 import org.openide.filesystems.Repository;
33 import org.openide.loaders.FileEntry;
34
35 import org.openide.loaders.MultiDataObject;
36 import org.openide.util.NbBundle;
37
38
39 /**
40  * Miscellaneous utilities for properties(reosurce bundles) module.
41  * @author Petr Jiricka
42  * @author Marian Petras
43  */

44 public final class Util extends Object JavaDoc {
45     
46     /** Help ID for properties module in general. */
47     public static final String JavaDoc HELP_ID_PROPERTIES = "propfiles.prop"; //NOI18N
48
/** Help ID for properties new from template. */
49     public static final String JavaDoc HELP_ID_CREATING = "propfiles.creating"; //NOI18N
50
/** Help ID for new property dialog. */
51     public static final String JavaDoc HELP_ID_ADDING = "propfiles.adding"; //NOI18N
52
/** Help ID for table view of properties. */
53     public static final String JavaDoc HELP_ID_MODIFYING
54                                = "propfiles.modifying"; //NOI18N
55
/** Help ID for new locale dialog. */
56     public static final String JavaDoc HELP_ID_ADDLOCALE
57                                = "propfiles.addlocale"; //NOI18N
58
/** Help ID for source editor of .properties file. */
59     public static final String JavaDoc HELP_ID_EDITLOCALE
60                                = "propfiles.editlocale"; //NOI18N
61

62     /** Character used to separate parts of bundle properties file name */
63     public static final char PRB_SEPARATOR_CHAR
64                              = PropertiesDataLoader.PRB_SEPARATOR_CHAR;
65     /** Default length for the first part of node label */
66     public static final int LABEL_FIRST_PART_LENGTH = 10;
67
68     /** Converts a string to a string suitable for a resource bundle key */
69     public static String JavaDoc stringToKey(String JavaDoc source) {
70         StringBuffer JavaDoc result = new StringBuffer JavaDoc();
71         for (int i = 0; i < source.length(); i++) {
72             char x = source.charAt(i);
73             switch (x) {
74             case '=':
75             case ':':
76             case '\t':
77             case '\r':
78             case '\n':
79             case '\f':
80             case ' ':
81                 result.append('_'); break;
82             default:
83                 result.append(x);
84             }
85         }
86         return result.toString();
87     }
88
89     /**
90      * Assembles a file name for a properties file from its base name and
91      * language.
92      *
93      * @return assembled name
94      */

95     public static String JavaDoc assembleName (String JavaDoc baseName, String JavaDoc lang) {
96         if (lang.length() == 0) {
97             return baseName;
98         } else {
99             if (lang.charAt(0) != PRB_SEPARATOR_CHAR) {
100                 StringBuffer JavaDoc res = new StringBuffer JavaDoc().append(baseName)
101                                                      .append(PRB_SEPARATOR_CHAR)
102                                                      .append(lang);
103                 return res.toString();
104             } else {
105                 return baseName + lang;
106             }
107         }
108     }
109     
110     /**
111      * Returns a locale specification suffix of a given
112      * <code>MultiDataObject</code> entry.
113      * <p>
114      * Examples:<br />
115      * <pre> </pre>Bundle.properties -&gt; &quot;&quot;<br />
116      * <pre> </pre>Bundle_en_CA.properties -&gt; &quot;_en_CA&quot;
117      *
118      * @param fe <code>DataObject</code> entry, representing a single bundle
119      * @return locale specification suffix of a given entry;
120      * or an empty string if the given entry has no locale suffix
121      * @see #getLanguage
122      * @see #getCountry
123      * @see #getVariant
124      */

125     public static String JavaDoc getLocaleSuffix(MultiDataObject.Entry fe) {
126         MultiDataObject.Entry pe = fe.getDataObject().getPrimaryEntry();
127         if (fe == pe) {
128             return ""; //NOI18N
129
}
130         String JavaDoc myName = fe.getFile().getName();
131         String JavaDoc baseName = pe.getFile().getName();
132         assert myName.startsWith(baseName);
133         return myName.substring(baseName.length());
134     }
135
136     /**
137      * Returns a language specification abbreviation of a given
138      * <code>MultiDataObject</code> entry.
139      * For example, if the specified entry represents file
140      * <tt>Bundle_en_UK.properties</tt>, this method returns
141      * <code>&quot;en&quot;</code>.
142      *
143      * @return <ul>
144      * <li>the language specification part of the locale specification,
145      * if present</li>
146      * <li>an empty string (<code>&quot;&quot;</code>)
147      * if the locale specification does not contain
148      * language specification</li>
149      * <li><code>null</code> if the file (entry) has no locale
150      * specification</li>
151      * </ul>
152      * @see #getCountry
153      * @see #getVariant
154      */

155     public static String JavaDoc getLanguage(final String JavaDoc localeSuffix) {
156         return getFirstPart(localeSuffix);
157     }
158
159     /**
160      * Returns a country specification abbreviation of a given
161      * <code>MultiDataObject</code> entry.
162      * For example, if the specified entry represents file
163      * <tt>Bundle_en_UK.properties</tt>, this method returns
164      * <code>&quot;UK&quot;</code>.
165      *
166      * @return <ul>
167      * <li>the country specification part of the locale
168      * specification, if present</li>
169      * <li>an empty string (<code>&quot;&quot;</code>)
170      * if the locale specification does not contain
171      * country specification</li>
172      * <li><code>null</code> if the file (entry) has no locale
173      * specification</li>
174      * </ul>
175      * @see #getLanguage
176      * @see #getVariant
177      */

178     public static String JavaDoc getCountry(final String JavaDoc localeSuffix) {
179         if (localeSuffix.length() == 0) {
180             return null;
181         }
182         int start = localeSuffix.indexOf(PRB_SEPARATOR_CHAR, 1);
183         return (start != -1)
184                ? getFirstPart(localeSuffix.substring(start))
185                : ""; //NOI18N
186
}
187
188     /**
189      * Returns a variant specification abbreviation of a given
190      * <code>MultiDataObject</code> entry.
191      * For example, if the specified entry represents file
192      * <tt>Bundle_en_US_POSIX.properties</tt>, this method returns
193      * <code>&quot;POSIX&quot;</code>.
194      *
195      * @return <ul>
196      * <li>the variant specification part of the locale
197      * specification, if present</li>
198      * <li>an empty string (<code>&quot;&quot;</code>)
199      * if the locale specification does not contain
200      * variant specification</li>
201      * <li><code>null</code> if the file (entry) has no locale
202      * specification</li>
203      * </ul>
204      * @see #getLanguage
205      * @see #getCountry
206      */

207     public static String JavaDoc getVariant(final String JavaDoc localeSuffix) {
208         if (localeSuffix.length() == 0) {
209             return null;
210         }
211         int start = localeSuffix.indexOf(PRB_SEPARATOR_CHAR, 1);
212         if (start == -1) {
213             return ""; //NOI18N
214
}
215         start = localeSuffix.indexOf(PRB_SEPARATOR_CHAR, start + 1);
216         return (start != -1) ? localeSuffix.substring(start + 1) : ""; //NOI18N
217
}
218
219     /**
220      * Returns the first part of a given locale suffix.
221      * The locale suffix must be either empty or start with an underscore.
222      *
223      * @param localeSuffix locale suffix, e.g. &quot;_en_US&quot;
224      * @return first part of the suffix, i.&thinsp;e. the part
225      * between the initial <code>'_'</code> and the
226      * (optional) next <code>'_'</code>; or <code>null</code>
227      * if an empty string was given as an argument
228      */

229     private static String JavaDoc getFirstPart(String JavaDoc localeSuffix) {
230         if (localeSuffix.length() == 0) {
231             return null;
232         }
233
234         assert localeSuffix.charAt(0) == PRB_SEPARATOR_CHAR;
235
236         int end = localeSuffix.indexOf(PRB_SEPARATOR_CHAR, 1);
237         return (end != -1) ? localeSuffix.substring(1, end)
238                            : localeSuffix.substring(1);
239     }
240
241     /** Gets a label for properties nodes for individual locales. */
242     public static String JavaDoc getLocaleLabel(MultiDataObject.Entry fe) {
243         
244         String JavaDoc localeSuffix = getLocaleSuffix(fe);
245         String JavaDoc language;
246         String JavaDoc country;
247         String JavaDoc variant;
248
249         /*
250          * Get the abbreviations for language, country and variant and check
251          * if at least one of them is specified. If none of them is specified,
252          * return the default label:
253          */

254         if (localeSuffix.length() == 0) {
255             language = ""; //NOI18N
256
country = ""; //NOI18N
257
variant = ""; //NOI18N
258
} else {
259             language = getLanguage(localeSuffix);
260             country = getCountry(localeSuffix);
261             variant = getVariant(localeSuffix);
262
263             // intern empty strings so that we can use '==' instead of equals():
264
language = language.length() != 0 ? language : ""; //NOI18N
265
country = country.length() != 0 ? country : ""; //NOI18N
266
variant = variant.length() != 0 ? variant : ""; //NOI18N
267
}
268
269         String JavaDoc defaultLangName = null;
270         if (language == "") { //NOI18N
271
defaultLangName = NbBundle.getMessage(
272                     Util.class,
273                     "LAB_defaultLanguage"); //NOI18N
274
}
275
276         /* Simple case #1 - the default locale */
277         if (language == "" && country == "" && variant == "") { //NOI18N
278
return defaultLangName;
279         }
280
281         String JavaDoc localeSpec = localeSuffix.substring(1);
282         Locale JavaDoc locale = new Locale JavaDoc(language, country, variant);
283
284         /* - language name: */
285         String JavaDoc langName;
286         if (language == "") { //NOI18N
287
langName = defaultLangName;
288         } else {
289             langName = locale.getDisplayLanguage();
290             if (langName.equals(language)) {
291                 langName = NbBundle.getMessage(Util.class,
292                                                "LAB_unknownLanguage", //NOI18N
293
language);
294             }
295         }
296
297         /* Simple case #2 - language specification only */
298         if (country == "" && variant == "") { //NOI18N
299
return NbBundle.getMessage(Util.class,
300                                        "LAB_localeSpecLang", //NOI18N
301
localeSpec,
302                                        langName);
303         }
304
305         /* - country name: */
306         String JavaDoc countryName = ""; //NOI18N
307
if (country != "") { //NOI18N
308
countryName = locale.getDisplayCountry();
309             if (countryName.equals(country)) {
310                 countryName = NbBundle.getMessage(Util.class,
311                                                   "LAB_unknownCountry", //NOI18N
312
country);
313             }
314         }
315
316         /* - variant name: */
317         String JavaDoc variantName = variant == "" ? "" //NOI18N
318
: locale.getDisplayVariant();
319
320         /* Last case - country and/or variant specification */
321         String JavaDoc countryAndVariant;
322         if (variantName == "") { //NOI18N
323
countryAndVariant = countryName;
324         } else if (countryName == "") { //NOI18N
325
countryAndVariant = variantName;
326         } else {
327             countryAndVariant = countryName + ", " + variantName; //NOI18N
328
}
329         return NbBundle.getMessage(Util.class,
330                                    "LAB_localeSpecLangCountry", //NOI18N
331
localeSpec,
332                                    langName,
333                                    countryAndVariant);
334
335     }
336
337     /** Notifies an error happened when attempted to create locale which exists already.
338      * @param locale locale which already exists */

339     private static void notifyError(String JavaDoc locale) {
340         NotifyDescriptor.Message msg = new NotifyDescriptor.Message(
341             MessageFormat.format(
342                 NbBundle.getBundle(PropertiesDataNode.class).getString("MSG_LangExists"),
343                     new Object JavaDoc[] {locale}), NotifyDescriptor.ERROR_MESSAGE);
344             DialogDisplayer.getDefault().notify(msg);
345     }
346     
347     public static void createLocaleFile(PropertiesDataObject propertiesDataObject,
348                                         String JavaDoc locale,
349                                         boolean copyInitialContent)
350     {
351         try {
352             if(locale.length() == 0) {
353                 // It would mean default locale to create again.
354
notifyError(locale);
355                 return;
356             }
357
358             if(propertiesDataObject != null) {
359                 FileObject file = propertiesDataObject.getPrimaryFile();
360                 final String JavaDoc newName = file.getName() + PropertiesDataLoader.PRB_SEPARATOR_CHAR + locale;
361                 final FileObject folder = file.getParent();
362 // final PropertiesEditorSupport editor = (PropertiesEditorSupport)propertiesDataObject.getCookie(PropertiesEditorSupport.class);
363
java.util.Iterator JavaDoc it = propertiesDataObject.secondaryEntries().iterator();
364                 while (it.hasNext()) {
365                     FileObject f = ((FileEntry)it.next()).getFile();
366                     if (newName.startsWith(f.getName()) && f.getName().length() > file.getName().length())
367                         file = f;
368                 }
369                 if (file.getName().equals(newName))
370                     return; // do nothing if the file already exists
371

372                 if (!copyInitialContent) { // we'll create an empty file
373
FileSystem defaultFS = Repository.getDefault().getDefaultFileSystem();
374                     file = defaultFS.findResource("Templates/Other/properties.properties"); // NOI18N
375
}
376
377                 final FileObject templateFile = file;
378
379                 SaveCookie save = (SaveCookie) propertiesDataObject.getCookie(SaveCookie.class);
380                 if (save != null)
381                     save.save();
382
383                 // Actually create new file.
384
// First try to create new file and load it by document content from default(=primary) file.
385
/* if(editor != null && editor.isDocumentLoaded()) {
386                     // Loading from the document in memory.
387                     final Document document = editor.getDocument();
388                     final String[] buffer = new String[1];
389
390                     // Safely take the text from the document.
391                     document.render(new Runnable() {
392                         public void run() {
393                             try {
394                                 buffer[0] = document.getText(0, document.getLength());
395                             } catch(BadLocationException ble) {
396                                 // Should be not possible.
397                                 ble.printStackTrace();
398                             }
399                         }
400                     });
401
402                     if(buffer[0] != null) {
403                         folder.getFileSystem().runAtomicAction(new FileSystem.AtomicAction() {
404                             public void run() throws IOException {
405                                 FileObject newFile = folder.createData(newName, PropertiesDataLoader.PROPERTIES_EXTENSION);
406
407                                 FileLock lock = newFile.lock();
408                                 try {
409                                     Writer writer = new PropertiesEditorSupport.NewLineWriter(newFile.getOutputStream(lock), editor.getNewLineType());
410
411                                     writer.write(buffer[0]);
412                                     writer.flush();
413                                     writer.close();
414                                 } finally {
415                                     lock.releaseLock();
416                                 }
417                             }
418                         });
419                     }
420                 } */

421
422                 // If first attempt failed, copy the default (=primary) file.
423
if(folder.getFileObject(newName, PropertiesDataLoader.PROPERTIES_EXTENSION) == null) {
424                     folder.getFileSystem().runAtomicAction(new FileSystem.AtomicAction() {
425                         public void run() throws IOException JavaDoc {
426                             templateFile.copy(folder, newName, PropertiesDataLoader.PROPERTIES_EXTENSION);
427                         }
428                     }); // End of annonymous inner class extended from FileSystem.AtomicAction.
429
}
430             }
431         } catch(IOException JavaDoc ioe) {
432             if(Boolean.getBoolean("netbeans.debug.exceptions")) // NOI18N
433
ioe.printStackTrace();
434
435             notifyError(locale);
436         }
437     }
438 }
439
Popular Tags