1 19 package org.netbeans.modules.javacore.jmiimpl.javamodel; 20 21 import org.netbeans.jmi.javamodel.JavaModelPackage; 22 import org.netbeans.jmi.javamodel.MultipartId; 23 import org.netbeans.jmi.javamodel.Resource; 24 import org.netbeans.jmi.javamodel.ResourceClass; 25 import org.netbeans.mdr.handlers.ClassProxyHandler; 26 import org.netbeans.mdr.persistence.MOFID; 27 import org.netbeans.mdr.storagemodel.StorableBaseObject; 28 import org.netbeans.mdr.storagemodel.StorableClass; 29 import org.netbeans.mdr.storagemodel.StorableFeatured; 30 import org.netbeans.modules.javacore.JMManager; 31 import org.netbeans.modules.javacore.internalapi.JavaMetamodel; 32 import org.openide.ErrorManager; 33 import org.openide.filesystems.FileObject; 34 import javax.jmi.reflect.ConstraintViolationException; 35 import java.io.IOException ; 36 import java.util.Collection ; 37 import java.util.Iterator ; 38 import java.util.List ; 39 import java.util.StringTokenizer ; 40 41 45 public abstract class ResourceClassImpl extends ClassProxyHandler implements ResourceClass { 46 private static final String NAME_INDEX = "Java.Resource.name"; 48 49 public ResourceClassImpl(StorableClass s) { 50 super(s); 51 } 52 53 public Resource createResource(String name, long timestamp, List classifiers, List imports, String packageName, MultipartId packageIdentifier, List packageAnnotations) { 54 assert !name.startsWith("/") : "Resource name cannot start with /"; if (resolveResource(name, false) == null) { 56 FileObject f = createFile(name); 58 ResourceImpl r = (ResourceImpl) super_createResource(name, timestamp, null, null, null, null, null); 61 r.setFileToDeleteOnRollback(f); 62 r._setPackageName(packageName); 63 r.objectChanged(MetadataElement.CHANGED_PACKAGE_NAME); 64 r.getClassifiers().addAll(classifiers); 65 r.getImports().addAll(imports); 66 return r; 67 } else { 68 throw new ConstraintViolationException(this, refMetaObject(), "Resource named " + name + " already exists."); } 70 } 71 72 private FileObject createFile(String name) { 73 74 FileObject currentFolder = ((JMManager) JavaMetamodel.getManager()).getCPRoot((JavaModelPackage) refImmediatePackage()); 75 StringTokenizer folderNames = new StringTokenizer (name,"/"); String newFolderName; 77 FileObject newFolder; 78 FileObject fileToDelete = null; 79 80 82 while (folderNames.hasMoreTokens()) { 83 newFolderName = folderNames.nextToken(); 84 if (folderNames.hasMoreTokens()) { 85 newFolder = currentFolder.getFileObject(newFolderName); 87 if (newFolder == null) { 88 try { 89 newFolder = currentFolder.createFolder(newFolderName); 91 if (fileToDelete == null) 92 fileToDelete = newFolder; } catch (IOException ioe){ 94 ErrorManager.getDefault().notify(ioe); 95 } 96 97 } 98 currentFolder = newFolder; 99 } else { 100 try { 102 FileObject f = currentFolder.createData(newFolderName); 103 if (fileToDelete == null) 104 fileToDelete = f; 105 } catch (IOException ioe) { 106 ErrorManager.getDefault().notify(ioe); 107 } 108 } 109 } 110 return fileToDelete; 111 } 112 113 public Resource resolveResource(String name, boolean create, boolean createFile) { 114 if (name == null || !(name.endsWith(".java") || name.endsWith(".class"))) return null; StorableFeatured storable = (StorableFeatured) _getDelegate(); 116 MOFID packageID = storable.getOutermostPackageId(); 117 Collection items = storable.getMdrStorage().getObjectsFromAdditionalIndex( 118 packageID, 119 NAME_INDEX, 120 name); 121 Resource res = null; 122 if (items != null) { 123 Iterator it = items.iterator(); 124 125 if (it.hasNext()) { 126 StorableBaseObject sbo = (StorableBaseObject) it.next(); 127 128 if (it.hasNext()) { 129 ErrorManager.getDefault().log("Multiple Resource instances for " + name); 132 } 133 res = (Resource) _getRepository().getHandler(sbo); 134 res = res != null && res.isValid() ? res : null; 135 } 136 } 137 if (res == null && create) { 138 assert !name.startsWith("/") : "Resource name cannot start with /"; if (createFile) { 140 createFile(name); 142 } 143 return super_createResource(name, 0, null, null, null, null, null); 144 } else { 145 return res; 146 } 147 } 148 149 public Resource resolveResource(String name, boolean create) { 150 return resolveResource(name, create, true); 151 } 152 153 public Resource createResource() { 154 ResourceImpl ret=(ResourceImpl)super_createResource(); 155 ret.setNew(); 156 ret.setData(); 157 return ret; 158 } 159 160 protected abstract Resource super_createResource(String name, long timestamp, List classifiers, List imports, String packageName, MultipartId packageIdentifier, List packageAnnotations); 161 protected abstract Resource super_createResource(); 162 } 163 | Popular Tags |