KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > deployment > impl > DefaultSourceMap


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
20 package org.netbeans.modules.j2ee.deployment.impl;
21
22 import java.io.File JavaDoc;
23 import java.util.ArrayList JavaDoc;
24 import java.util.HashSet JavaDoc;
25 import java.util.Iterator JavaDoc;
26 import org.netbeans.modules.j2ee.deployment.common.api.SourceFileMap;
27 import org.netbeans.modules.j2ee.deployment.devmodules.spi.J2eeAppProvider;
28 import org.netbeans.modules.j2ee.deployment.devmodules.spi.J2eeModuleProvider;
29 import org.openide.filesystems.FileObject;
30 import org.openide.filesystems.FileUtil;
31
32 /**
33  *
34  * @author nn136682
35  */

36 public class DefaultSourceMap extends SourceFileMap {
37
38     /**
39      * Straight file mapping service.
40      * Map a distribution path to a file using distribution path as relative path to a mapping root.
41      */

42     private J2eeModuleProvider provider;
43     private HashSet JavaDoc rootFiles = new HashSet JavaDoc();
44     
45     /** Creates a new instance of DefaultFileMapping */
46     public DefaultSourceMap(J2eeModuleProvider provider) {
47         this.provider = provider;
48         FileObject[] roots = provider.getSourceRoots();
49         for (int i=0; i<roots.length; i++) {
50             if (roots[i] != null) {
51                 rootFiles.add(FileUtil.toFile(roots[i]));
52             }
53         }
54     }
55     
56     public String JavaDoc getContextName() {
57         return provider.getDeploymentName();
58     }
59
60     public FileObject[] getSourceRoots() {
61         return provider.getSourceRoots();
62     }
63     
64     public File JavaDoc getEnterpriseResourceDir() {
65         return provider.getEnterpriseResourceDirectory();
66     }
67     
68     public File JavaDoc[] getEnterpriseResourceDirs() {
69         ArrayList JavaDoc result = new ArrayList JavaDoc();
70         result.add(provider.getEnterpriseResourceDirectory());
71         if (provider instanceof J2eeAppProvider) {
72             J2eeAppProvider jap = (J2eeAppProvider) provider;
73             J2eeModuleProvider[] children = jap.getChildModuleProviders();
74             for (int i=0; i<children.length; i++) {
75                 result.add(children[i].getEnterpriseResourceDirectory());
76             }
77         }
78         return (File JavaDoc[]) result.toArray(new File JavaDoc[result.size()]);
79     }
80    
81     public boolean add(String JavaDoc distributionPath, FileObject sourceFile) {
82         return false;
83     }
84     
85     public FileObject remove(String JavaDoc distributionPath) {
86         return null;
87     }
88     
89     public FileObject[] findSourceFile(String JavaDoc distributionPath) {
90         ArrayList JavaDoc ret = new ArrayList JavaDoc();
91         FileObject[] roots = getSourceRoots();
92         String JavaDoc path = distributionPath.startsWith("/") ? distributionPath.substring(1) : distributionPath; //NOI18N
93
for (int i=0; i<roots.length; i++) {
94             FileObject fo = roots[i].getFileObject(path);
95             if (fo != null)
96                 ret.add(fo);
97         }
98         return (FileObject[]) ret.toArray(new FileObject[ret.size()]);
99     }
100     
101     public File JavaDoc getDistributionPath(FileObject sourceFile) {
102         for (Iterator JavaDoc i=rootFiles.iterator(); i.hasNext();) {
103             File JavaDoc rootFile = (File JavaDoc) i.next();
104             FileObject root = FileUtil.toFileObject(rootFile);
105             String JavaDoc relative = FileUtil.getRelativePath(root, sourceFile);
106             if (relative != null && ! relative.trim().equals("")) { //NOI18N
107
return new File JavaDoc(relative);
108             }
109         }
110         return null;
111     }
112 }
113
114
115
Popular Tags