001 /*
002 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003 *
004 * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005 *
006 * The contents of this file are subject to the terms of either the GNU
007 * General Public License Version 2 only ("GPL") or the Common
008 * Development and Distribution License("CDDL") (collectively, the
009 * "License"). You may not use this file except in compliance with the
010 * License. You can obtain a copy of the License at
011 * http://www.netbeans.org/cddl-gplv2.html
012 * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013 * specific language governing permissions and limitations under the
014 * License. When distributing the software, include this License Header
015 * Notice in each file and include the License file at
016 * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017 * particular file as subject to the "Classpath" exception as provided
018 * by Sun in the GPL Version 2 section of the License file that
019 * accompanied this code. If applicable, add the following below the
020 * License Header, with the fields enclosed by brackets [] replaced by
021 * your own identifying information:
022 * "Portions Copyrighted [year] [name of copyright owner]"
023 *
024 * Contributor(s):
025 *
026 * The Original Software is NetBeans. The Initial Developer of the Original
027 * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028 * Microsystems, Inc. All Rights Reserved.
029 *
030 * If you wish your version of this file to be governed by only the CDDL
031 * or only the GPL Version 2, indicate your decision by adding
032 * "[Contributor] elects to include this software in this distribution
033 * under the [CDDL or GPL Version 2] license." If you do not indicate a
034 * single choice of license, a recipient has the option to distribute
035 * your version of this file under either the CDDL, the GPL Version 2 or
036 * to extend the choice of license to its licensees as provided above.
037 * However, if you add GPL Version 2 code and therefore, elected the GPL
038 * Version 2 license, then the option applies only if the new code is
039 * made subject to such option by the copyright holder.
040 */
041
042 package org.netbeans.modules.j2ee.deployment.impl;
043
044 import java.io.File;
045 import java.util.ArrayList;
046 import java.util.HashSet;
047 import java.util.Iterator;
048 import org.netbeans.modules.j2ee.deployment.common.api.SourceFileMap;
049 import org.netbeans.modules.j2ee.deployment.devmodules.spi.J2eeApplicationProvider;
050 import org.netbeans.modules.j2ee.deployment.devmodules.spi.J2eeModuleProvider;
051 import org.openide.filesystems.FileObject;
052 import org.openide.filesystems.FileUtil;
053
054 /**
055 *
056 * @author nn136682
057 */
058 public class DefaultSourceMap extends SourceFileMap {
059
060 /**
061 * Straight file mapping service.
062 * Map a distribution path to a file using distribution path as relative path to a mapping root.
063 */
064 private J2eeModuleProvider provider;
065 private HashSet rootFiles = new HashSet();
066
067 /** Creates a new instance of DefaultFileMapping */
068 public DefaultSourceMap(J2eeModuleProvider provider) {
069 this .provider = provider;
070 FileObject[] roots = provider.getSourceRoots();
071 for (int i = 0; i < roots.length; i++) {
072 if (roots[i] != null) {
073 rootFiles.add(FileUtil.toFile(roots[i]));
074 }
075 }
076 }
077
078 public String getContextName() {
079 return provider.getDeploymentName();
080 }
081
082 public FileObject[] getSourceRoots() {
083 return provider.getSourceRoots();
084 }
085
086 public File getEnterpriseResourceDir() {
087 return provider.getJ2eeModule().getResourceDirectory();
088 }
089
090 public File[] getEnterpriseResourceDirs() {
091 ArrayList result = new ArrayList();
092 result.add(provider.getJ2eeModule().getResourceDirectory());
093 if (provider instanceof J2eeApplicationProvider) {
094 J2eeApplicationProvider jap = (J2eeApplicationProvider) provider;
095 J2eeModuleProvider[] children = jap
096 .getChildModuleProviders();
097 for (int i = 0; i < children.length; i++) {
098 result.add(children[i].getJ2eeModule()
099 .getResourceDirectory());
100 }
101 }
102 return (File[]) result.toArray(new File[result.size()]);
103 }
104
105 public boolean add(String distributionPath, FileObject sourceFile) {
106 return false;
107 }
108
109 public FileObject remove(String distributionPath) {
110 return null;
111 }
112
113 public FileObject[] findSourceFile(String distributionPath) {
114 ArrayList ret = new ArrayList();
115 FileObject[] roots = getSourceRoots();
116 String path = distributionPath.startsWith("/") ? distributionPath
117 .substring(1)
118 : distributionPath; //NOI18N
119 for (int i = 0; i < roots.length; i++) {
120 FileObject fo = roots[i].getFileObject(path);
121 if (fo != null)
122 ret.add(fo);
123 }
124 return (FileObject[]) ret.toArray(new FileObject[ret.size()]);
125 }
126
127 public File getDistributionPath(FileObject sourceFile) {
128 for (Iterator i = rootFiles.iterator(); i.hasNext();) {
129 File rootFile = (File) i.next();
130 FileObject root = FileUtil.toFileObject(rootFile);
131 String relative = FileUtil
132 .getRelativePath(root, sourceFile);
133 if (relative != null && !relative.trim().equals("")) { //NOI18N
134 return new File(relative);
135 }
136 }
137 return null;
138 }
139 }
|