KickJava   Java API By Example, From Geeks To Geeks.

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


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.*;
23 import java.util.*;
24
25 import org.openide.filesystems.FileObject;
26 import org.openide.util.Enumerations;
27 import org.openide.util.Utilities;
28
29 /** Property class that collects a modifiable list of file extensions
30 * and permits checking of whether a name or a file object has a given extension.
31 * It comes with a property editor to allow the user to modify the extensions.
32 *
33 * @author Jaroslav Tulach
34 */

35 public class ExtensionList extends Object JavaDoc
36     implements Cloneable JavaDoc, java.io.Serializable JavaDoc {
37
38     /** if true, ignore case of file extensions (not MIME types tho!) */
39     private static final boolean CASE_INSENSITIVE =
40             (Utilities.isWindows () || (Utilities.getOperatingSystem () == Utilities.OS_OS2)) || Utilities.getOperatingSystem() == Utilities.OS_VMS;
41
42     /** set of extensions to recognize */
43     private SortedSet<String JavaDoc> list;
44     /** set of mime types to recognize */
45     private SortedSet<String JavaDoc> mimeTypes;
46
47     static final long serialVersionUID =8868581349510386291L;
48     /** Default constructor.
49     */

50     public ExtensionList () {
51     }
52
53     /** Clone new object.
54     */

55     public synchronized Object JavaDoc clone () {
56         try {
57             ExtensionList l = (ExtensionList)super.clone ();
58             
59             if (list != null) {
60                 l.list = createExtensionSet ();
61                 l.list.addAll (list);
62             }
63             
64             if (mimeTypes != null) {
65                 l.mimeTypes = createExtensionSet();
66                 l.mimeTypes.addAll(mimeTypes);
67             }
68             
69             return l;
70         } catch (CloneNotSupportedException JavaDoc ex) {
71             // has to be supported we implement the right interface
72
throw new InternalError JavaDoc ();
73         }
74     }
75
76     /** Add a new extension.
77     * @param ext the extension
78     */

79     public synchronized void addExtension (String JavaDoc ext) {
80         if (list == null) {
81             list = createExtensionSet ();
82         }
83         
84         list.add (ext);
85     }
86
87     /** Remove an extension.
88     * @param ext the extension
89     */

90     public void removeExtension (String JavaDoc ext) {
91         if (list != null) {
92             list.remove (ext);
93         }
94     }
95     
96     /** Adds new mime type.
97     * @param mime the mime type
98     */

99     public synchronized void addMimeType (String JavaDoc mime) {
100         if (mimeTypes == null) {
101             mimeTypes = new TreeSet<String JavaDoc> ();
102         }
103
104         mimeTypes.add (mime);
105     }
106     
107     /** Removes a mime type.
108      * @param mime the name of the type
109      */

110     public void removeMimeType (String JavaDoc mime) {
111         if (mimeTypes != null) {
112             mimeTypes.remove(mime);
113         }
114     }
115
116     /** Test whether the name in the string is acceptable.
117     * It should end with a dot and be one of the registered extenstions.
118     * @param s the name
119     * @return <CODE>true</CODE> if the name is acceptable
120     */

121     public boolean isRegistered (String JavaDoc s) {
122         if (list == null) {
123             return false;
124         }
125       
126         try {
127             String JavaDoc ext = s.substring (s.lastIndexOf ('.') + 1);
128             return list.contains (ext);
129         } catch (StringIndexOutOfBoundsException JavaDoc ex) {
130             return false;
131         }
132     }
133
134     /** Tests whether the file object is acceptable.
135     * Its extension should be registered.
136     * @param fo the file object to test
137     * @return <CODE>true</CODE> if the file object is acceptable
138     */

139     public boolean isRegistered (FileObject fo) {
140         if (list != null && list.contains (fo.getExt ())) {
141             return true;
142         }
143
144         if (mimeTypes != null && mimeTypes.contains(fo.getMIMEType())) {
145             return true;
146         }
147         
148         return false;
149     }
150
151     /** Get all extensions.
152     * @return enumeration of extensions
153     */

154     public Enumeration<String JavaDoc> extensions() {
155         return en (list);
156     }
157     
158     /** Get all mime types.
159      * @return enumeration of MIME types
160      */

161     public Enumeration<String JavaDoc> mimeTypes() {
162         return en (mimeTypes);
163     }
164     
165     public String JavaDoc toString() {
166         return "ExtensionList[" + list + mimeTypes + "]"; // NOI18N
167
}
168     
169     public boolean equals(Object JavaDoc o) {
170         if (!(o instanceof ExtensionList)) return false;
171         ExtensionList e = (ExtensionList)o;
172         return equalSets(list, e.list, CASE_INSENSITIVE) &&
173                equalSets(mimeTypes, e.mimeTypes, false);
174     }
175     
176     public int hashCode() {
177         int x = 0;
178         if (list != null) x = normalizeSet(list, CASE_INSENSITIVE).hashCode();
179         if (mimeTypes != null) x += normalizeSet(mimeTypes, false).hashCode();
180         return x;
181     }
182     
183     // Helper methods for equals/hashCode.
184
// Note that these are unsorted sets; we don't care about order.
185
private static boolean equalSets(Set<String JavaDoc> s1, Set<String JavaDoc> s2, boolean flattenCase) {
186         if (s1 == null && s2 == null) return true; // quick return
187
Set s1a = normalizeSet(s1, flattenCase);
188         Set s2a = normalizeSet(s2, flattenCase);
189         return s1a.equals(s2a);
190     }
191     private static Set<String JavaDoc> normalizeSet(Set<String JavaDoc> s, boolean flattenCase) {
192         if (s == null || s.isEmpty()) return Collections.emptySet();
193         if (flattenCase) {
194             Set<String JavaDoc> s2 = new HashSet<String JavaDoc>(s.size() * 4 / 3 + 1);
195             for (String JavaDoc item: s) {
196                 s2.add(item.toLowerCase(Locale.US));
197             }
198             return s2;
199         } else {
200             return s;
201         }
202     }
203     
204     /** Enumeration from set
205      * @param set set or null
206      */

207     private static Enumeration<String JavaDoc> en(Collection<String JavaDoc> c) {
208         if (c == null) {
209             return Enumerations.empty();
210         } else {
211             return Collections.enumeration(c);
212         }
213     }
214     
215     /** Creates a set for holding the extensions. It is platform
216     * dependent whether case sensitive or insensitive.
217     */

218     private static SortedSet<String JavaDoc> createExtensionSet () {
219         if (CASE_INSENSITIVE) {
220             return new TreeSet<String JavaDoc>(String.CASE_INSENSITIVE_ORDER);
221         } else {
222             return new TreeSet<String JavaDoc>();
223         }
224     }
225     
226     /** Backward compatibility settings read.
227     */

228     private void readObject (ObjectInputStream ois)
229     throws IOException, ClassNotFoundException JavaDoc {
230         ObjectInputStream.GetField gf = ois.readFields();
231         
232         Object JavaDoc list = gf.get ("list", null); // NOI18N
233
if (list instanceof Map) {
234             // backward compatible serialization
235
list = ((Map)list).keySet ();
236         }
237         
238         if (list != null) {
239             // have to reinsert everything because we could migrate from
240
// different operating system and we might need to change
241
// case-sensitivity
242
this.list = createExtensionSet ();
243             this.list.addAll ((Set)list);
244         }
245         
246         this.mimeTypes = (TreeSet)gf.get ("mimeTypes", null); // NOI18N
247
}
248 }
249
Popular Tags