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: * If you wish your version of this file to be governed by only the CDDL
025: * or only the GPL Version 2, indicate your decision by adding
026: * "[Contributor] elects to include this software in this distribution
027: * under the [CDDL or GPL Version 2] license." If you do not indicate a
028: * single choice of license, a recipient has the option to distribute
029: * your version of this file under either the CDDL, the GPL Version 2 or
030: * to extend the choice of license to its licensees as provided above.
031: * However, if you add GPL Version 2 code and therefore, elected the GPL
032: * Version 2 license, then the option applies only if the new code is
033: * made subject to such option by the copyright holder.
034: *
035: * Contributor(s):
036: *
037: * Portions Copyrighted 2008 Sun Microsystems, Inc.
038: */
039:
040: package org.netbeans.modules.java.api.common.classpath.j2ee;
041:
042: import java.io.File;
043: import java.net.URI;
044: import java.net.URL;
045: import java.util.Iterator;
046: import java.util.List;
047: import org.netbeans.api.project.ant.AntArtifact;
048: import org.netbeans.api.project.libraries.Library;
049: import org.netbeans.modules.java.api.common.classpath.ClassPathItem;
050: import org.netbeans.modules.java.api.common.classpath.ClassPathSupport;
051: import org.netbeans.spi.project.support.ant.AntProjectHelper;
052: import org.netbeans.spi.project.support.ant.EditableProperties;
053: import org.netbeans.spi.project.support.ant.PropertyEvaluator;
054: import org.netbeans.spi.project.support.ant.ReferenceHelper;
055: import org.openide.filesystems.FileObject;
056: import org.openide.filesystems.FileUtil;
057: import org.openide.filesystems.URLMapper;
058: import org.w3c.dom.Node;
059: import org.w3c.dom.NodeList;
060: import org.w3c.dom.Text;
061:
062: // will be moved to j2ee.common
063: /**
064: *
065: * @author Tomas Mysik
066: * @since 1.21
067: */
068: abstract class BaseClassPathSupport<T extends BaseClassPathSupport.Item> {
069:
070: static final String ATTR_FILES = "files"; // NOI18N
071: static final String ATTR_DIRS = "dirs"; // NOI18N
072:
073: final AntProjectHelper antProjectHelper;
074: final ClassPathSupport delegate;
075: final String projectConfigurationNameSpace;
076:
077: BaseClassPathSupport(PropertyEvaluator evaluator,
078: ReferenceHelper referenceHelper,
079: AntProjectHelper antProjectHelper, String[] wellKnownPaths,
080: String libraryPrefix, String librarySuffix,
081: String antArtifactPrefix,
082: String projectConfigurationNameSpace) {
083: // XXX call Parameters here as well?
084: assert projectConfigurationNameSpace != null;
085:
086: this .antProjectHelper = antProjectHelper;
087: this .projectConfigurationNameSpace = projectConfigurationNameSpace;
088: delegate = ClassPathSupport.create(evaluator, referenceHelper,
089: antProjectHelper, wellKnownPaths, libraryPrefix,
090: librarySuffix, antArtifactPrefix);
091: }
092:
093: /**
094: * Creates list of <CODE>Items</CODE> from given property.
095: */
096: public Iterator<T> itemsIterator(String propertyValue,
097: String libraryElementName) {
098: // XXX More performance frendly impl. would retrun a lazzy iterator.
099: return itemsList(propertyValue, libraryElementName).iterator();
100: }
101:
102: public abstract List<T> itemsList(String propertyValue,
103: String libraryElementName);
104:
105: public abstract String[] encodeToStrings(final List<T> items,
106: String libraryElementName);
107:
108: // XXX move into utilities module
109: public static void getFilesForItem(Item item, List<File> files,
110: List<File> dirs) {
111: if (item.isBroken()) {
112: return;
113: }
114: switch (item.getType()) {
115: case LIBRARY:
116: List<URL> roots = item.getLibrary().getContent("classpath"); // NOI18N
117: for (URL rootUrl : roots) {
118: FileObject root = URLMapper.findFileObject(rootUrl);
119: if ("jar".equals(rootUrl.getProtocol())) { // NOI18N
120: root = FileUtil.getArchiveFile(root);
121: }
122: File f = FileUtil.toFile(root);
123: if (f != null) {
124: if (f.isFile()) {
125: files.add(f);
126: } else {
127: dirs.add(f);
128: }
129: }
130: }
131: break;
132: case JAR:
133: File root = item.getFile();
134: if (root != null) {
135: if (root.isFile()) {
136: files.add(root);
137: } else {
138: dirs.add(root);
139: }
140: }
141: break;
142: case ARTIFACT:
143: String artifactFolder = item.getArtifact()
144: .getScriptLocation().getParent();
145: URI[] locations = item.getArtifact().getArtifactLocations();
146: for (URI location : locations) {
147: String fullLocation = artifactFolder + File.separator
148: + location;
149: if (fullLocation.endsWith(File.separator)) {
150: dirs.add(new File(fullLocation));
151: } else {
152: files.add(new File(fullLocation));
153: }
154: }
155: break;
156: default:
157: // noop
158: break;
159: }
160: }
161:
162: public String getLibraryReference(T item) {
163: return delegate.getLibraryReference(item.delegate);
164: }
165:
166: /**
167: * Tokenize library classpath and try to relativize all the jars.
168: * @param ep the editable properties in which the result should be stored
169: * @param aph AntProjectHelper used to resolve files
170: * @param libCpProperty the library classpath property
171: */
172: public static boolean relativizeLibraryClassPath(
173: final EditableProperties ep, final AntProjectHelper aph,
174: final String libCpProperty) {
175: return ClassPathSupport.relativizeLibraryClassPath(ep, aph,
176: libCpProperty);
177: }
178:
179: /**
180: * Extracts <b>the first</b> nested text from an element.
181: * Currently does not handle coalescing text nodes, CDATA sections, etc.
182: * @param parent a parent element
183: * @return the nested text, or null if none was found
184: */
185: static String findText(Node parent) {
186: NodeList l = parent.getChildNodes();
187: for (int i = 0; i < l.getLength(); i++) {
188: if (l.item(i).getNodeType() == Node.TEXT_NODE) {
189: Text text = (Text) l.item(i);
190: return text.getNodeValue();
191: }
192: }
193: return null;
194: }
195:
196: abstract static class Item implements ClassPathItem {
197:
198: final ClassPathSupport.Item delegate;
199: private String property;
200: private String raw;
201:
202: Item(ClassPathSupport.Item delegate, String property, String raw) {
203: this .delegate = delegate;
204: this .property = property;
205: this .raw = raw;
206: }
207:
208: void setReference(String property) {
209: this .property = property;
210: }
211:
212: public ClassPathItem.Type getType() {
213: return delegate.getType();
214: }
215:
216: public Library getLibrary() {
217: return delegate.getLibrary();
218: }
219:
220: public File getFile() {
221: return delegate.getFile();
222: }
223:
224: public AntArtifact getArtifact() {
225: return delegate.getArtifact();
226: }
227:
228: public URI getArtifactURI() {
229: return delegate.getArtifactURI();
230: }
231:
232: public String getReference() {
233: return delegate.getReference();
234: }
235:
236: public boolean isBroken() {
237: return delegate.isBroken();
238: }
239:
240: public String getRaw() {
241: return raw;
242: }
243:
244: @Override
245: public int hashCode() {
246: return delegate.hashCode();
247: }
248:
249: @Override
250: public boolean equals(Object itemObject) {
251: if (itemObject instanceof Item) {
252: Item item = (Item) itemObject;
253: return delegate.equals(item);
254: }
255: return false;
256: }
257: }
258: }
|