KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > catalogsupport > DefaultProjectCatalogSupport


1 /*
2  * DefaultProjectCatalogSupport.java
3  *
4  * Created on December 18, 2006, 3:55 PM
5  *
6  * To change this template, choose Tools | Template Manager
7  * and open the template in the editor.
8  */

9
10 package org.netbeans.modules.xml.catalogsupport;
11
12 import java.io.File JavaDoc;
13 import java.io.IOException JavaDoc;
14 import java.net.URI JavaDoc;
15 import java.net.URISyntaxException JavaDoc;
16 import java.util.ArrayList JavaDoc;
17 import java.util.Arrays JavaDoc;
18 import java.util.Set JavaDoc;
19 import java.util.StringTokenizer JavaDoc;
20 import org.netbeans.api.java.project.JavaProjectConstants;
21 import org.netbeans.api.project.FileOwnerQuery;
22 import org.netbeans.api.project.Project;
23 import org.netbeans.api.project.ProjectUtils;
24 import org.netbeans.api.project.SourceGroup;
25 import org.netbeans.api.project.Sources;
26 import org.netbeans.modules.web.api.webmodule.WebProjectConstants;
27 import org.netbeans.modules.xml.catalogsupport.util.ProjectReferenceUtility;
28 import org.netbeans.modules.xml.retriever.catalog.CatalogEntry;
29 import org.netbeans.modules.xml.retriever.catalog.CatalogWriteModel;
30 import org.netbeans.modules.xml.retriever.catalog.CatalogWriteModelFactory;
31 import org.netbeans.modules.xml.xam.locator.CatalogModelException;
32 import org.netbeans.spi.project.SubprojectProvider;
33 import org.netbeans.spi.project.support.ant.AntProjectHelper;
34 import org.netbeans.spi.project.support.ant.PropertyUtils;
35 import org.netbeans.spi.project.support.ant.ReferenceHelper;
36 import org.openide.filesystems.FileObject;
37 import org.netbeans.modules.xml.retriever.catalog.ProjectCatalogSupport;
38 import org.openide.filesystems.FileUtil;
39
40 /**
41  *
42  * @author Ajit
43  */

44 public class DefaultProjectCatalogSupport extends ProjectCatalogSupport {
45     
46     public static final String JavaDoc NBURI_SCHEME = "nb-uri";
47     
48     public Project project;
49     public AntProjectHelper helper;
50     public ReferenceHelper refHelper;
51     /**
52      * Creates a new instance of DefaultProjectCatalogSupport
53      */

54     public DefaultProjectCatalogSupport(Project project) {
55         this(project,null,null);
56     }
57     
58     public DefaultProjectCatalogSupport(Project project, AntProjectHelper helper,
59             ReferenceHelper refHelper) {
60         this.project = project;
61         this.helper = helper;
62         this.refHelper = refHelper;
63     }
64     
65     public static DefaultProjectCatalogSupport getInstance(FileObject source) {
66         Project owner = FileOwnerQuery.getOwner(source);
67         if(owner!=null) {
68             DefaultProjectCatalogSupport support = (DefaultProjectCatalogSupport)owner.
69                     getLookup().lookup(DefaultProjectCatalogSupport.class);
70             if(support!=null) return support;
71         }
72         return new DefaultProjectCatalogSupport(owner);
73     }
74     
75     public boolean supportsCrossProject() {
76         return helper!=null;
77     }
78     
79     public URI JavaDoc constructProjectProtocol(FileObject foTobeAddedInCat) {
80         Project owner = FileOwnerQuery.getOwner(foTobeAddedInCat);
81         if(owner!=null) {
82             String JavaDoc ssp = getUsableProjectName(owner);
83             String JavaDoc fragment = getRelativePath(owner.getProjectDirectory(),foTobeAddedInCat);
84             try {
85                 return new URI JavaDoc(NBURI_SCHEME,ssp,fragment);
86             } catch (URISyntaxException JavaDoc ex) {
87             }
88         }
89         return null;
90     }
91     
92     public boolean isProjectProtocol(URI JavaDoc uriStoredInCatFile) {
93         return NBURI_SCHEME.equals(uriStoredInCatFile.getScheme());
94     }
95     
96     public FileObject resolveProjectProtocol(URI JavaDoc uriToBeResolved) {
97         if(supportsCrossProject() && isProjectProtocol(uriToBeResolved)) {
98             String JavaDoc ssp = uriToBeResolved.getSchemeSpecificPart();
99             String JavaDoc targetPrjRelativeRoot = helper.getProperties(
100                     AntProjectHelper.PROJECT_PROPERTIES_PATH).
101                     getProperty("project.".concat(ssp));
102             if(targetPrjRelativeRoot!=null){
103                 File JavaDoc myPrjRoot = FileUtil.toFile(project.getProjectDirectory());
104                 File JavaDoc tgtPrjRoot = new File JavaDoc(myPrjRoot.toURI().resolve(targetPrjRelativeRoot));
105                 FileObject tgtPrjFobj = FileUtil.toFileObject(FileUtil.normalizeFile(tgtPrjRoot));
106                 return tgtPrjFobj.getFileObject(uriToBeResolved.getFragment());
107             }
108         }
109         return null;
110     }
111     
112     public boolean needsCatalogEntry(FileObject source, FileObject target) {
113         assert source !=null && target !=null;
114         // check if target belongs to different project or different source root
115
if(project!=FileOwnerQuery.getOwner(target)) {
116             return false;
117         }
118
119         FileObject folder = getSourceFolder(source);
120         if (folder != null && !FileUtil.isParentOf(folder,target)) {
121             return true;
122         }
123         
124         return false;
125     }
126     
127     public URI JavaDoc createCatalogEntry(FileObject source, FileObject target) throws
128             IOException JavaDoc, CatalogModelException {
129         assert source !=null && target !=null;
130         CatalogWriteModel cwm = CatalogWriteModelFactory.getInstance().
131                 getCatalogWriteModelForProject(project.getProjectDirectory());
132         assert cwm!= null;
133         Project targetProject = FileOwnerQuery.getOwner(target);
134         URI JavaDoc targetURI = null;
135         URI JavaDoc sourceURI = null;
136         try {
137             sourceURI = getReferenceURI(source, target);
138         } catch (URISyntaxException JavaDoc ex) {
139             return null;
140         }
141         if(project!=targetProject) {
142             if(!getProjectReferences().contains(targetProject) &&
143                     supportsCrossProject()) {
144                 ProjectReferenceUtility.addProjectReference(refHelper,targetProject);
145             };
146             targetURI = constructProjectProtocol(target);
147         } else {
148             try {
149                 targetURI = new URI JavaDoc(getRelativePath(cwm.getCatalogFileObject(),target));
150             } catch (URISyntaxException JavaDoc ex) {
151                 return null;
152             }
153         }
154         cwm.addURI(sourceURI,targetURI);
155         return sourceURI;
156     }
157     
158     public URI JavaDoc getReferenceURI(FileObject source, FileObject target) throws URISyntaxException JavaDoc {
159         Project targetProject = FileOwnerQuery.getOwner(target);
160         FileObject sourceFolder = getSourceFolder(source);
161         if (sourceFolder == null) {
162             throw new IllegalArgumentException JavaDoc(source.getPath()+" is not in project source"); //NOI18N
163
}
164         String JavaDoc relPathToSrcGroup = getRelativePath(source.getParent(), sourceFolder);
165         String JavaDoc relPathToSrcGroupWithSlash = relPathToSrcGroup.trim().equals("")? "" :
166             relPathToSrcGroup.concat("/");
167         if(project!=targetProject) {
168             FileObject folder = getSourceFolder(targetProject,target);
169             if (folder == null) {
170                 throw new IllegalArgumentException JavaDoc(target.getPath()+" is not in target project source"); //NOI18N
171
}
172             String JavaDoc relPathFromTgtGroup = getRelativePath(folder,target);
173             return new URI JavaDoc(relPathToSrcGroupWithSlash.concat(
174                     getUsableProjectName(targetProject)).
175                     concat("/").concat(relPathFromTgtGroup));
176         } else {
177             FileObject targetSourceFolder = getSourceFolder(target);
178             if (targetSourceFolder == null) {
179                 throw new IllegalArgumentException JavaDoc(target.getPath()+" is not in project source"); //NOI18N
180
}
181             String JavaDoc relPathFromTgtGroup =
182                     getRelativePath(targetSourceFolder,target);
183             return new URI JavaDoc(relPathToSrcGroupWithSlash.concat(relPathFromTgtGroup));
184         }
185     }
186     
187     public Set JavaDoc getProjectReferences() {
188         SubprojectProvider provider = (SubprojectProvider)project.getLookup().
189                 lookup(SubprojectProvider.class);
190         return provider.getSubprojects();
191     }
192     
193     private FileObject getSourceFolder(FileObject source) {
194         return getSourceFolder(project,source);
195     }
196     
197     private static String JavaDoc[] sourceTypes = new String JavaDoc[] {
198         JavaProjectConstants.SOURCES_TYPE_JAVA,
199         WebProjectConstants.TYPE_DOC_ROOT,
200         WebProjectConstants.TYPE_WEB_INF
201     };
202     
203     private static FileObject getSourceFolder(Project project, FileObject source) {
204         Sources sources = ProjectUtils.getSources(project);
205         assert sources !=null;
206         ArrayList JavaDoc<SourceGroup> sourceGroups = new ArrayList JavaDoc<SourceGroup>();
207         for (String JavaDoc type : sourceTypes) {
208             SourceGroup[] groups = sources.getSourceGroups(type);
209             if (groups != null) {
210                 sourceGroups.addAll(Arrays.asList(groups));
211             }
212         }
213             
214         assert sourceGroups.size()>0;
215         for(SourceGroup sourceGroup:sourceGroups) {
216             if(FileUtil.isParentOf(sourceGroup.getRootFolder(),source))
217                 return sourceGroup.getRootFolder();
218         }
219         return null;
220     }
221     
222     private static String JavaDoc getRelativePath(FileObject source, FileObject target) {
223         File JavaDoc sourceLocationFile = FileUtil.toFile(source);
224         File JavaDoc targetLocationFile = FileUtil.toFile(target);
225         String JavaDoc sourceLocation = sourceLocationFile.toURI().toString();
226         String JavaDoc targetLocation = targetLocationFile.toURI().toString();
227         StringTokenizer JavaDoc st1 = new StringTokenizer JavaDoc(sourceLocation,"/");
228         StringTokenizer JavaDoc st2 = new StringTokenizer JavaDoc(targetLocation,"/");
229         String JavaDoc relativeLoc = "";
230         while (st1.hasMoreTokens() && st2.hasMoreTokens()) {
231             relativeLoc = st2.nextToken();
232             if (!st1.nextToken().equals(relativeLoc)) {
233                 break;
234             }
235             if(!st1.hasMoreTokens() || !st2.hasMoreTokens()) {
236                 // seems like one of the file is parent directory of other file
237
if(st1.hasMoreElements()) {
238                     // seems like target is parent of source
239
relativeLoc = "..";
240                     st1.nextToken();
241                 } else if(st2.hasMoreTokens()) {
242                     // seems like source is parent of target
243
relativeLoc = st2.nextToken();
244                 } else {
245                     // both represent same file
246
relativeLoc = "";
247                 }
248             }
249         }
250         while (st1.hasMoreTokens()) {
251             relativeLoc = "../".concat(relativeLoc);
252             st1.nextToken();
253         }
254         while(st2.hasMoreTokens()) {
255             relativeLoc = relativeLoc.concat("/").concat(st2.nextToken());
256         }
257         return relativeLoc;
258     }
259     
260     
261     private static String JavaDoc getUsableProjectName(Project project) {
262         return PropertyUtils.getUsablePropertyName(ProjectUtils.getInformation
263                 (project).getName()).replace('.','_');
264     }
265     
266     public boolean removeCatalogEntry(URI JavaDoc uri) throws IOException JavaDoc {
267         CatalogWriteModel cwm = null;
268         try {
269             cwm = CatalogWriteModelFactory.getInstance().
270                     getCatalogWriteModelForProject(project.getProjectDirectory());
271         } catch (CatalogModelException ex) {
272             return false;
273         }
274         boolean entryFound = false;
275         for(CatalogEntry ce : cwm.getCatalogEntries()){
276             URI JavaDoc src = null;
277             try {
278                 src = new URI JavaDoc(ce.getSource());
279             } catch (URISyntaxException JavaDoc ex) {
280                 continue;
281             }
282             if(src.equals(uri)){
283                 entryFound = true;
284                 break;
285             }
286         }
287         if(entryFound){
288             cwm.removeURI(uri);
289             return true;
290         }
291         return false;
292     }
293     
294 }
295
Popular Tags