1 19 20 package org.openide.loaders; 21 22 import java.io.IOException ; 23 import java.util.*; 24 import java.util.logging.Level ; 25 26 import org.openide.*; 27 import org.openide.filesystems.*; 28 29 35 public abstract class MultiFileLoader extends DataLoader { 36 static final long serialVersionUID=1521919955690157343L; 37 38 39 43 @Deprecated 44 protected MultiFileLoader(Class <? extends DataObject> representationClass) { 45 super(representationClass); 46 } 47 48 54 protected MultiFileLoader (String representationClassName) { 55 super (representationClassName); 56 } 57 58 71 protected final DataObject handleFindDataObject ( 72 FileObject fo, RecognizedFiles recognized ) throws IOException { 73 FileObject primary = findPrimaryFileImpl (fo); 75 76 if (primary == null) return null; 78 79 80 boolean willLog = ERR.isLoggable(Level.FINE); 81 82 if (willLog) { 83 ERR.fine(getClass().getName() + " is accepting: " + fo); } 85 86 if (primary != fo) { 87 if (willLog) { 88 ERR.fine("checking correctness: primary is different than provided file: " + primary + " fo: " + fo); } 90 Enumeration en = DataLoaderPool.getDefault().allLoaders(); 91 for (;;) { 92 DataLoader l = (DataLoader)en.nextElement(); 93 if (l == this) { 94 ERR.fine("ok, consistent"); break; 96 } 97 if (l instanceof MultiFileLoader) { 98 MultiFileLoader ml = (MultiFileLoader)l; 99 if (ml.findPrimaryFile(primary) == primary) { 100 if (willLog) { 101 ERR.fine("loader seems to also take care of the file: " + ml); 102 } 103 DataObject snd; 104 try { 105 snd = ml.findDataObject(primary, recognized); 106 } catch (DataObjectExistsException ex) { 107 snd = ex.getDataObject(); 108 } 109 if (snd != null) { 110 return null; 111 } 112 } 113 } 114 } 115 } 116 117 MultiDataObject obj; 118 try { 119 obj = createMultiObject (primary); 121 if (willLog) { 122 ERR.fine(getClass().getName() + " created object for: " + fo + " obj: " + obj); } 124 } catch (DataObjectExistsException ex) { 125 DataObject dataObject = ex.getDataObject (); 127 if (willLog) { 128 ERR.fine(getClass().getName() + " object already exists for: " + fo + " obj: " + dataObject); } 130 131 if (dataObject.getLoader () != this) { 132 if (willLog) { 133 ERR.fine(getClass().getName() + " loader is wrong: " + dataObject.getLoader().getClass().getName()); } 135 136 if (dataObject.getLoader() instanceof MultiFileLoader) { 137 MultiFileLoader mfl = (MultiFileLoader)dataObject.getLoader(); 138 FileObject loaderPrimary = mfl.findPrimaryFileImpl(fo); 139 ERR.log(Level.FINE, "Its primary file is {0}", loaderPrimary); if (loaderPrimary != null && dataObject.getPrimaryFile() != loaderPrimary) { 141 ERR.log(Level.FINE, "Which is different than primary of found: {0}", dataObject); 143 Enumeration before = DataLoaderPool.getDefault().allLoaders(); 144 while (before.hasMoreElements()) { 145 Object o = before.nextElement(); 146 if (o == mfl) { 147 ERR.log(Level.FINE, "Returning null"); return null; 149 } 150 if (o == this) { 151 ERR.log(Level.FINE, "The loader" + mfl + " is after " + this + ". So do break."); break; 153 } 154 } 155 } 156 } 157 158 dataObject = checkCollision (dataObject, fo); 161 } 162 163 if (!(dataObject instanceof MultiDataObject)) { 164 if (willLog) { 166 ERR.fine(getClass().getName() + " object is not MultiDataObject: " + dataObject); } 168 throw ex; 169 } 170 obj = (MultiDataObject)dataObject; 171 } catch (IOException ex) { 172 ERR.log(Level.FINE, null, ex); 173 throw ex; 174 } 175 176 if (obj.getLoader () != this) { 177 if (willLog) { 178 ERR.fine(getClass().getName() + " wrong loader: " + obj.getLoader().getClass().getName()); } 180 return obj; 183 } 184 185 if (willLog) { 187 ERR.fine(getClass().getName() + " marking secondary entries"); } 189 obj.markSecondaryEntriesRecognized (recognized); 190 191 if (willLog) { 193 ERR.fine(getClass().getName() + " register entry: " + fo); } 195 org.openide.loaders.MultiDataObject.Entry e = obj.registerEntry (fo); 196 if (willLog) { 197 ERR.fine(getClass().getName() + " success: " + e); } 199 200 return obj; 201 } 202 203 204 210 protected abstract FileObject findPrimaryFile (FileObject fo); 211 212 220 protected abstract MultiDataObject createMultiObject (FileObject primaryFile) 221 throws DataObjectExistsException, IOException ; 222 223 229 protected abstract MultiDataObject.Entry createPrimaryEntry (MultiDataObject obj, FileObject primaryFile); 230 231 239 protected abstract MultiDataObject.Entry createSecondaryEntry (MultiDataObject obj, FileObject secondaryFile); 240 241 249 DataObject checkCollision (DataObject obj, FileObject file) { 250 253 FileObject primary = obj.getPrimaryFile (); 254 255 DataObjectPool.getPOOL().revalidate ( 256 new HashSet<FileObject> (Collections.singleton(primary)) 257 ); 258 DataObject result = DataObjectPool.getPOOL().find (primary); 260 return result; 261 } 262 263 267 void checkConsistency (MultiDataObject obj) { 268 271 FileObject primary = obj.getPrimaryFile (); 272 if (primary.equals (findPrimaryFileImpl (primary))) { 273 return; 275 } 276 277 try { 280 obj.setValid (false); 281 } catch (java.beans.PropertyVetoException ex) { 282 } 284 } 285 286 287 288 297 void checkFiles (MultiDataObject obj) { 298 301 302 303 FileObject primary = obj.getPrimaryFile (); 304 assert primary != null : "Object " + obj + " cannot have null primary file"; FileObject parent = primary.getParent (); 306 assert parent != null : "Object " + obj + " cannot have null parent file"; 308 FileObject[] arr = parent.getChildren (); 309 for (int i = 0; i < arr.length; i++) { 310 FileObject pf = findPrimaryFileImpl (arr[i]); 311 312 if (pf == primary) { 313 try { 315 DataObject.find (arr[i]); 319 } catch (DataObjectNotFoundException ex) { 320 } 322 } 323 } 324 } 325 326 MultiDataObject.Entry createSecondaryEntryImpl (MultiDataObject obj, FileObject secondaryFile) { 327 return createSecondaryEntry (obj, secondaryFile); 328 } 329 330 FileObject findPrimaryFileImpl (FileObject fo) { 331 return findPrimaryFile (fo); 332 } 333 } 334 | Popular Tags |