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.projectimport.eclipse;
043:
044: import java.util.ArrayList;
045: import java.util.Collection;
046: import java.util.Collections;
047: import java.util.HashSet;
048: import java.util.Iterator;
049: import java.util.logging.Logger;
050: import org.netbeans.modules.projectimport.LoggerFactory;
051:
052: /**
053: * Represents classpath for an Eclipse project (.classpath file content)
054: *
055: * @author mkrauskopf
056: */
057: final class ClassPath {
058:
059: /**
060: * Logger for this class
061: */
062: private static final Logger logger = LoggerFactory.getDefault()
063: .createLogger(ClassPath.class);
064:
065: /** Represents link in Eclipse project's classpath. */
066: static class Link {
067: public static final int TYPE_INVALID = 0;
068: public static final int TYPE_FILE = 1;
069: public static final int TYPE_FOLDER = 2;
070:
071: private String name;
072: private int type = TYPE_INVALID;
073: private String location;
074:
075: String getName() {
076: return name;
077: }
078:
079: void setName(String name) {
080: this .name = name;
081: }
082:
083: int getType() {
084: return type;
085: }
086:
087: void setType(int type) {
088: this .type = type;
089: }
090:
091: String getLocation() {
092: return location;
093: }
094:
095: void setLocation(String location) {
096: this .location = location;
097: }
098:
099: public String toString() {
100: return name + " = " + location + " (type: " + type + ")"; // NOI18N
101: }
102:
103: public boolean equals(Object obj) {
104: if (this == obj)
105: return true;
106: if (!(obj instanceof Link))
107: return false;
108: final Link link = (Link) obj;
109: if (type != link.type)
110: return false;
111: if (name != null ? !name.equals(link.name)
112: : link.name != null)
113: return false;
114: if (location != null ? !location.equals(link.location)
115: : link.location != null)
116: return false;
117: return true;
118: }
119:
120: public int hashCode() {
121: int result = 17;
122: result = 37 * result + type;
123: result = 37 * result + System.identityHashCode(name);
124: result = 37 * result + System.identityHashCode(location);
125: return result;
126: }
127: }
128:
129: private static final String USER_LIBRARY_PREFIX = "org.eclipse.jdt.USER_LIBRARY/"; // NOI18N
130: private static final int USER_LIBRARY_PREFIX_LENGTH = USER_LIBRARY_PREFIX
131: .length();
132:
133: private ClassPathEntry output;
134: private Collection/*<ClassPathEntry>*/pathEntries = Collections.EMPTY_LIST;
135:
136: private String jreContainer;
137: private Collection/*<ClassPathEntry>*/sourceRoots;
138: private Collection/*<ClassPathEntry>*/externalSourceRoots;
139: private Collection/*<ClassPathEntry>*/libraries;
140: private Collection/*<ClassPathEntry>*/externalLibraries;
141: private Collection/*<ClassPathEntry>*/projects;
142: private Collection/*<ClassPathEntry>*/variables;
143: private Collection/*<ClassPathEntry>*/userLibraries;
144:
145: /**
146: * Adds a given entry to entries list. If entry is output output member is
147: * set.
148: */
149: void addEntry(ClassPathEntry entry) {
150: if (entry != null) {
151: if (entry.getType() == ClassPathEntry.TYPE_OUTPUT) {
152: output = entry;
153: } else {
154: addSource(entry);
155: }
156: }
157: }
158:
159: private void addSource(ClassPathEntry path) {
160: if (pathEntries == Collections.EMPTY_LIST) {
161: pathEntries = new ArrayList();
162: }
163: pathEntries.add(path);
164: }
165:
166: ClassPathEntry getOutput() {
167: return output;
168: }
169:
170: Collection/*<ClassPathEntry>*/getEntries() {
171: return pathEntries;
172: }
173:
174: private Collection/*<ClassPathEntry>*/getEntriesByType(
175: ClassPathEntry.Type type) {
176: Collection/*<ClassPathEntry>*/entries = new ArrayList();
177: for (Iterator it = pathEntries.iterator(); it.hasNext();) {
178: ClassPathEntry entry = (ClassPathEntry) it.next();
179: if (entry.getType() == type) {
180: entries.add(entry);
181: }
182: }
183: return entries;
184: }
185:
186: /**
187: * Just provides more convenient access to source entries.
188: *
189: * @see #getEntries()
190: */
191: Collection/*<ClassPathEntry>*/getSourceRoots() {
192: // lazy initialization
193: if (sourceRoots == null) {
194: sourceRoots = getEntriesByType(ClassPathEntry.TYPE_SOURCE);
195: }
196: return sourceRoots;
197: }
198:
199: /**
200: * Returns container classpath entry for JRE.
201: *
202: * @see #getEntries()
203: */
204: String getJREContainer() {
205: // lazy initialization
206: if (jreContainer == null) {
207: Collection col = getEntriesByType(ClassPathEntry.TYPE_CONTAINER);
208: for (Iterator it = col.iterator(); it.hasNext();) {
209: ClassPathEntry cpe = (ClassPathEntry) it.next();
210: if (cpe.getRawPath().startsWith(
211: Workspace.DEFAULT_JRE_CONTAINER)) {
212: jreContainer = cpe.getRawPath();
213: logger
214: .finest("jreContainer found: "
215: + jreContainer); // NOI18N
216: break;
217: }
218: }
219: if (jreContainer == null) {
220: logger
221: .fine("jreContainer wasn't found in classpath entries!"); // NOI18N
222: logger.fine("Classpath entries: " + this .getEntries()); // NOI18N
223: }
224: }
225: return jreContainer;
226: }
227:
228: /**
229: * Just provides more convenient access to external source entries.
230: *
231: * @see #getEntries()
232: */
233: Collection/*<ClassPathEntry>*/getExternalSourceRoots() {
234: // lazy initialization
235: if (externalSourceRoots == null) {
236: externalSourceRoots = getEntriesByType(ClassPathEntry.TYPE_LINK);
237: }
238: return externalSourceRoots;
239: }
240:
241: /**
242: * Just provides more convenient access to library entries.
243: *
244: * @see #getEntries()
245: */
246: Collection/*<ClassPathEntry>*/getLibraries() {
247: // lazy initialization
248: if (libraries == null) {
249: libraries = getEntriesByType(ClassPathEntry.TYPE_LIBRARY);
250: }
251: return libraries;
252: }
253:
254: /**
255: * Just provides more convenient access to external library entries.
256: *
257: * @see #getEntries()
258: */
259: Collection/*<ClassPathEntry>*/getExternalLibraries() {
260: // lazy initialization
261: if (externalLibraries == null) {
262: externalLibraries = getEntriesByType(ClassPathEntry.TYPE_EXTERNAL_LIBRARY);
263: }
264: return externalLibraries;
265: }
266:
267: /**
268: * Returns collection of names of user defined libraries.
269: */
270: Collection/*<String>*/getUserLibraries() {
271: // lazy initialization
272: if (userLibraries == null) {
273: Collection col = getEntriesByType(ClassPathEntry.TYPE_CONTAINER);
274: userLibraries = new HashSet();
275: for (Iterator it = col.iterator(); it.hasNext();) {
276: ClassPathEntry cpe = (ClassPathEntry) it.next();
277: String rawPath = cpe.getRawPath();
278: if (rawPath.startsWith(USER_LIBRARY_PREFIX)) {
279: userLibraries.add(rawPath
280: .substring(USER_LIBRARY_PREFIX_LENGTH));
281: }
282: }
283: }
284: return userLibraries;
285: }
286:
287: /**
288: * Just provides more convenient access to project entries.
289: *
290: * @see #getEntries()
291: */
292: Collection/*<ClassPathEntry>*/getProjects() {
293: // lazy initialization
294: if (projects == null) {
295: projects = getEntriesByType(ClassPathEntry.TYPE_PROJECT);
296: }
297: return projects;
298: }
299:
300: /**
301: * Just provides more convenient access to project entries.
302: *
303: * @see #getEntries()
304: */
305: Collection/*<ClassPathEntry>*/getVariables() {
306: // lazy initialization
307: if (variables == null) {
308: variables = getEntriesByType(ClassPathEntry.TYPE_VARIABLE);
309: }
310: return variables;
311: }
312: }
|