KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > javacore > jmiimpl > javamodel > ResourceClassImpl


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 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 JavaDoc;
36 import java.util.Collection JavaDoc;
37 import java.util.Iterator JavaDoc;
38 import java.util.List JavaDoc;
39 import java.util.StringTokenizer JavaDoc;
40
41 /**
42  *
43  * @author Martin Matula
44  */

45 public abstract class ResourceClassImpl extends ClassProxyHandler implements ResourceClass {
46     private static final String JavaDoc NAME_INDEX = "Java.Resource.name"; // NOI18N
47

48     /** Creates a new instance of ResourceClassImpl */
49     public ResourceClassImpl(StorableClass s) {
50         super(s);
51     }
52     
53     public Resource createResource(String JavaDoc name, long timestamp, List JavaDoc classifiers, List JavaDoc imports, String JavaDoc packageName, MultipartId packageIdentifier, List JavaDoc packageAnnotations) {
54         assert !name.startsWith("/") : "Resource name cannot start with /"; // NOI18N
55
if (resolveResource(name, false) == null) {
56             // [TODO] should check whether the file already existed?
57
FileObject f = createFile(name);
58             //this approach does not work
59
//ResourceImpl r = (ResourceImpl) super_createResource(name, timestamp, classifiers, imports, packageName);
60
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."); // NOI18N
69
}
70     }
71     
72     private FileObject createFile(String JavaDoc name) {
73
74         FileObject currentFolder = ((JMManager) JavaMetamodel.getManager()).getCPRoot((JavaModelPackage) refImmediatePackage());
75         StringTokenizer JavaDoc folderNames = new StringTokenizer JavaDoc(name,"/"); // NOI18N
76
String JavaDoc newFolderName;
77         FileObject newFolder;
78         FileObject fileToDelete = null;
79         
80         //iterate through all folders and if they not exist, create them. Finally create a file
81

82         while (folderNames.hasMoreTokens()) {
83             newFolderName = folderNames.nextToken();
84             if (folderNames.hasMoreTokens()) {
85                 //if this is not last token, it must be a folder
86
newFolder = currentFolder.getFileObject(newFolderName);
87                 if (newFolder == null) {
88                     try {
89                         //if the folder does not exist - create it
90
newFolder = currentFolder.createFolder(newFolderName);
91                         if (fileToDelete == null)
92                             fileToDelete = newFolder; //I must delete topmost folder I created
93
} catch (IOException JavaDoc ioe){
94                         ErrorManager.getDefault().notify(ioe);
95                     }
96                     
97                 }
98                 currentFolder = newFolder;
99             } else {
100                 //this is last token - it is a file name
101
try {
102                 FileObject f = currentFolder.createData(newFolderName);
103                 if (fileToDelete == null)
104                     fileToDelete = f;
105                 } catch (IOException JavaDoc ioe) {
106                     ErrorManager.getDefault().notify(ioe);
107                 }
108             }
109         }
110         return fileToDelete;
111     }
112     
113     public Resource resolveResource(String JavaDoc name, boolean create, boolean createFile) {
114         if (name == null || !(name.endsWith(".java") || name.endsWith(".class"))) return null; // NOI18N
115
StorableFeatured storable = (StorableFeatured) _getDelegate();
116         MOFID packageID = storable.getOutermostPackageId();
117         Collection JavaDoc items = storable.getMdrStorage().getObjectsFromAdditionalIndex(
118             packageID,
119             NAME_INDEX,
120             name);
121         Resource res = null;
122         if (items != null) {
123             Iterator JavaDoc it = items.iterator();
124             
125             if (it.hasNext()) {
126                 StorableBaseObject sbo = (StorableBaseObject) it.next();
127                 
128                 if (it.hasNext()) {
129                     // error: there is more than one element with the same descriptor
130
// [TODO] should handle error properly (using error manager/throw exception?)
131
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 /"; // NOI18N
139
if (createFile) {
140                 // [TODO] should check whether the name already existed?
141
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 JavaDoc 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 JavaDoc name, long timestamp, List JavaDoc classifiers, List JavaDoc imports, String JavaDoc packageName, MultipartId packageIdentifier, List JavaDoc packageAnnotations);
161     protected abstract Resource super_createResource();
162 }
163
Popular Tags