KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openide > loaders > UniFileLoader


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.openide.loaders;
21
22 import java.io.IOException JavaDoc;
23
24 import org.openide.*;
25 import org.openide.filesystems.*;
26 import org.openide.util.io.SafeException;
27
28 /** Support class for loader handling one file at a time.
29  * This is used for many file types, e.g. HTML, images, etc.
30  * File extensions recognized by the loader may be set.
31 *
32 * @author Petr Hamernik, Jaroslav Tulach
33 */

34 public abstract class UniFileLoader extends MultiFileLoader {
35     /** SUID */
36     static final long serialVersionUID=-6190649471408985837L;
37
38     /** name of property with extensions */
39     public static final String JavaDoc PROP_EXTENSIONS = "extensions"; // NOI18N
40

41     /** Constructor.
42     * @param representationClass class that is produced by this loader
43      * @deprecated Use UniFileLoader#UniFileLoader(String) instead.
44     */

45     @Deprecated JavaDoc
46     protected UniFileLoader(Class JavaDoc<? extends DataObject> representationClass) {
47         super (representationClass);
48     }
49
50     /** Constructor.
51     * @param representationClassName the fully qualified name of the
52     * representation class.
53     *
54     * @since 1.10
55     */

56     protected UniFileLoader (String JavaDoc representationClassName) {
57         super (representationClassName);
58     }
59
60     /** Get the primary file.
61     * By default, only accepts files, not folders.
62     * @param fo the file to find the primary file for
63     *
64     * @return the primary file, or <code>null</code> if its extension is not {@link #getExtensions recognized}
65     */

66     protected FileObject findPrimaryFile (FileObject fo) {
67         // never recognize folders
68
if (fo.isFolder()) return null;
69         
70         return getExtensions().isRegistered(fo) ? fo : null;
71     }
72
73     /* Creates the right data object for given primary file.
74     * It is guaranteed that the provided file is realy primary file
75     * returned from the method findPrimaryFile.
76     *
77     * @param primaryFile the primary file
78     * @return the data object for this file
79     * @exception DataObjectExistsException if the primary file already has data object
80     */

81     protected abstract MultiDataObject createMultiObject (FileObject primaryFile)
82     throws DataObjectExistsException, java.io.IOException JavaDoc;
83
84     /* Creates the right primary entry for given primary file.
85     *
86     * @param obj requesting object
87     * @param primaryFile primary file recognized by this loader
88     * @return primary entry for that file
89     */

90     protected MultiDataObject.Entry createPrimaryEntry (MultiDataObject obj, FileObject primaryFile) {
91         return new FileEntry (obj, primaryFile);
92     }
93
94     /** Do not create a seconday entry.
95     *
96     * @param obj ignored
97     * @param secondaryFile ignored
98     * @return never returns
99     * @exception UnsupportedOperationException because this loader supports only a primary file object
100     */

101     protected MultiDataObject.Entry createSecondaryEntry (MultiDataObject obj, FileObject secondaryFile) {
102
103         // Debug messages for #17014. Please remove, when the bug is fixed.
104
StringBuffer JavaDoc buf = new StringBuffer JavaDoc("Error in data system. Please reopen the bug #17014 with the following message: "); //NOI18N
105
buf.append("\n DataLoader:"); //NOI18N
106
buf.append(getClass().getName());
107         buf.append("\n DataObject:"); //NOI18N
108
buf.append(obj);
109         buf.append("\n PrimaryEntry:"); //NOI18N
110
buf.append(obj.getPrimaryEntry());
111         buf.append("\n PrimaryFile:"); //NOI18N
112
buf.append(obj.getPrimaryFile());
113         buf.append("\n SecondaryFile:"); //NOI18N
114
buf.append(secondaryFile);
115         buf.append("\n");
116         
117         throw new UnsupportedOperationException JavaDoc (buf.toString()); //NOI18N
118
}
119
120     /** Called when there is a collision between a data object that
121     * this loader tries to create and already existing one.
122     *
123     * @param obj existing data object
124     * @param file the original file that has been recognized by this loader
125     * as bellonging to obj data object
126     * @return null
127     */

128     final DataObject checkCollision (DataObject obj, FileObject file) {
129         return null;
130     }
131     
132     /** Does nothing because this loader works only with objects
133     * with one file => primary file so it is not necessary to search
134     * for anything else.
135     *
136     * @param obj the object to test
137     */

138     final void checkConsistency (MultiDataObject obj) {
139     }
140     
141     /** Does nothing because this loader works only with objects
142     * with one file => primary file so it is not necessary to search
143     * for anything else.
144     *
145     * @param obj the object to test
146     */

147     final void checkFiles (MultiDataObject obj) {
148     }
149
150     /** Set the extension list for this data loader.
151     * @param ext new list of extensions
152     */

153     public void setExtensions(ExtensionList ext) {
154         putProperty (PROP_EXTENSIONS, ext, true);
155     }
156
157     /** Get the extension list for this data loader.
158     * @return list of extensions
159     */

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

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

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