1 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 35 public class ExtensionList extends Object 36 implements Cloneable , java.io.Serializable { 37 38 39 private static final boolean CASE_INSENSITIVE = 40 (Utilities.isWindows () || (Utilities.getOperatingSystem () == Utilities.OS_OS2)) || Utilities.getOperatingSystem() == Utilities.OS_VMS; 41 42 43 private SortedSet<String > list; 44 45 private SortedSet<String > mimeTypes; 46 47 static final long serialVersionUID =8868581349510386291L; 48 50 public ExtensionList () { 51 } 52 53 55 public synchronized Object 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 ex) { 71 throw new InternalError (); 73 } 74 } 75 76 79 public synchronized void addExtension (String ext) { 80 if (list == null) { 81 list = createExtensionSet (); 82 } 83 84 list.add (ext); 85 } 86 87 90 public void removeExtension (String ext) { 91 if (list != null) { 92 list.remove (ext); 93 } 94 } 95 96 99 public synchronized void addMimeType (String mime) { 100 if (mimeTypes == null) { 101 mimeTypes = new TreeSet<String > (); 102 } 103 104 mimeTypes.add (mime); 105 } 106 107 110 public void removeMimeType (String mime) { 111 if (mimeTypes != null) { 112 mimeTypes.remove(mime); 113 } 114 } 115 116 121 public boolean isRegistered (String s) { 122 if (list == null) { 123 return false; 124 } 125 126 try { 127 String ext = s.substring (s.lastIndexOf ('.') + 1); 128 return list.contains (ext); 129 } catch (StringIndexOutOfBoundsException ex) { 130 return false; 131 } 132 } 133 134 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 154 public Enumeration<String > extensions() { 155 return en (list); 156 } 157 158 161 public Enumeration<String > mimeTypes() { 162 return en (mimeTypes); 163 } 164 165 public String toString() { 166 return "ExtensionList[" + list + mimeTypes + "]"; } 168 169 public boolean equals(Object 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 private static boolean equalSets(Set<String > s1, Set<String > s2, boolean flattenCase) { 186 if (s1 == null && s2 == null) return true; Set s1a = normalizeSet(s1, flattenCase); 188 Set s2a = normalizeSet(s2, flattenCase); 189 return s1a.equals(s2a); 190 } 191 private static Set<String > normalizeSet(Set<String > s, boolean flattenCase) { 192 if (s == null || s.isEmpty()) return Collections.emptySet(); 193 if (flattenCase) { 194 Set<String > s2 = new HashSet<String >(s.size() * 4 / 3 + 1); 195 for (String item: s) { 196 s2.add(item.toLowerCase(Locale.US)); 197 } 198 return s2; 199 } else { 200 return s; 201 } 202 } 203 204 207 private static Enumeration<String > en(Collection<String > c) { 208 if (c == null) { 209 return Enumerations.empty(); 210 } else { 211 return Collections.enumeration(c); 212 } 213 } 214 215 218 private static SortedSet<String > createExtensionSet () { 219 if (CASE_INSENSITIVE) { 220 return new TreeSet<String >(String.CASE_INSENSITIVE_ORDER); 221 } else { 222 return new TreeSet<String >(); 223 } 224 } 225 226 228 private void readObject (ObjectInputStream ois) 229 throws IOException, ClassNotFoundException { 230 ObjectInputStream.GetField gf = ois.readFields(); 231 232 Object list = gf.get ("list", null); if (list instanceof Map) { 234 list = ((Map)list).keySet (); 236 } 237 238 if (list != null) { 239 this.list = createExtensionSet (); 243 this.list.addAll ((Set)list); 244 } 245 246 this.mimeTypes = (TreeSet)gf.get ("mimeTypes", null); } 248 } 249 | Popular Tags |