KickJava   Java API By Example, From Geeks To Geeks.

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


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 package org.netbeans.modules.properties;
21
22 import java.io.IOException JavaDoc;
23 import java.io.ObjectInput JavaDoc;
24 import java.io.ObjectOutput JavaDoc;
25 import org.openide.filesystems.FileObject;
26 import org.openide.loaders.ExtensionList;
27 import org.openide.loaders.MultiDataObject;
28 import org.openide.loaders.MultiFileLoader;
29 import org.openide.util.NbBundle;
30 import org.openide.util.io.SafeException;
31
32 /**
33  * Data loader which recognizes properties files.
34  * This class is final only for performance reasons,
35  * can be unfinaled if desired.
36  *
37  * @author Ian Formanek, Petr Jiricka
38  * @author Marian Petras
39  */

40 public final class PropertiesDataLoader extends MultiFileLoader {
41
42     /** Extension for properties files. */
43     static final String JavaDoc PROPERTIES_EXTENSION = "properties"; // NOI18N
44

45     /** Character used to separate parts of bundle properties file name */
46     public static final char PRB_SEPARATOR_CHAR = '_';
47
48     /** Generated serial version UID. */
49     static final long serialVersionUID =4384899552891479449L;
50     
51     /** name of property with extensions */
52     public static final String JavaDoc PROP_EXTENSIONS = "extensions"; // NOI18N
53

54     /** Creates new PropertiesDataLoader. */
55     public PropertiesDataLoader() {
56         super("org.netbeans.modules.properties.PropertiesDataObject"); // NOI18N
57

58         // Set extentions. Due performance reasons do it here instead in initialize method.
59
// During startup it's in findPrimaryFile method called getExtensions method. If the
60
// extentions list was not set in constructor the initialize method would be called
61
// during startup, but we want to avoid the initialize call since we don't need
62
// actions and display name initialized during startup time.
63
ExtensionList extList = new ExtensionList();
64         extList.addExtension(PROPERTIES_EXTENSION);
65         // Add .impl for CORBA module.
66
extList.addExtension("impl"); // NOI18N
67
setExtensions(extList);
68     }
69
70     
71     /** */
72     protected String JavaDoc defaultDisplayName() {
73         return NbBundle.getMessage(PropertiesDataLoader.class,
74                                    "PROP_PropertiesLoader_Name"); //NOI18N
75
}
76     
77     /**
78      * This methods uses the layer action context so it returns
79      * a non-<code>null</code> value.
80      *
81      * @return name of the context on layer files to read/write actions to
82      */

83     protected String JavaDoc actionsContext () {
84         return "Loaders/text/x-properties/Actions/"; //NOI18N
85
}
86     
87     /**
88      * @return <code>PropertiesDataObject</code> for the specified
89      * <code>FileObject</code>
90      */

91     protected MultiDataObject createMultiObject(final FileObject fo)
92             throws IOException JavaDoc {
93         return new PropertiesDataObject(fo, this);
94     }
95
96     /** */
97     protected FileObject findPrimaryFile (FileObject fo) {
98         if (fo.isFolder()) {
99             return null;
100         }
101         if (fo.getExt().equalsIgnoreCase(PROPERTIES_EXTENSION)) {
102             
103             /*
104              * returns a file whose name is the shortest valid prefix
105              * corresponding to an existing file
106              */

107             String JavaDoc fName = fo.getName();
108             int index = fName.indexOf(PRB_SEPARATOR_CHAR);
109             while (index != -1) {
110                 FileObject candidate = fo.getParent().getFileObject(
111                         fName.substring(0, index), fo.getExt());
112                 if (candidate != null) {
113                     return candidate;
114                 }
115                 index = fName.indexOf(PRB_SEPARATOR_CHAR, index + 1);
116             }
117             return fo;
118         } else {
119             return getExtensions().isRegistered(fo) ? fo : null;
120         }
121     }
122
123     /**
124      * @return <code>PropertiesFileEntry</code> for the given file
125      */

126     protected MultiDataObject.Entry createPrimaryEntry(MultiDataObject obj,
127                                                        FileObject primaryFile) {
128         return new PropertiesFileEntry(obj, primaryFile);
129     }
130
131     /**
132      * @return <code>PropertiesFileEntry</code> for the given file
133      */

134     protected MultiDataObject.Entry createSecondaryEntry(
135             MultiDataObject obj,
136             FileObject secondaryFile) {
137         return new PropertiesFileEntry(obj, secondaryFile);
138     }
139     
140
141     /**
142      * Sets the extension list for this data loader.
143      * This data loader will then recognize all files having any extension
144      * of the given list.
145      *
146      * @param extList list of extensions
147      */

148     public void setExtensions(ExtensionList ext) {
149         putProperty(PROP_EXTENSIONS, ext, true);
150     }
151
152     /**
153      * Get the extension list for this data loader.
154      *
155      * @return list of extensions
156      * @see #setExtensions
157      */

158     public ExtensionList getExtensions() {
159         ExtensionList l = (ExtensionList) getProperty(PROP_EXTENSIONS);
160         if (l == null) {
161             l = new ExtensionList();
162             putProperty(PROP_EXTENSIONS, l, false);
163         }
164         return l;
165     }
166
167     /** Writes extensions to the stream.
168     * @param oo ignored
169     */

170     public void writeExternal (ObjectOutput JavaDoc oo) throws IOException JavaDoc {
171         super.writeExternal (oo);
172
173         oo.writeObject (getProperty (PROP_EXTENSIONS));
174     }
175
176     /** Reads nothing from the stream.
177     * @param oi ignored
178     */

179     public void readExternal (ObjectInput JavaDoc oi)
180     throws IOException JavaDoc, ClassNotFoundException JavaDoc {
181         SafeException se;
182         try {
183             super.readExternal (oi);
184             se = null;
185         } catch (SafeException se2) {
186             se = se2;
187         }
188
189         setExtensions ((ExtensionList)oi.readObject ());
190         if (se != null) throw se;
191     }
192     
193 }
194
Popular Tags