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.io.File;
045: import java.util.Iterator;
046: import java.util.logging.Logger;
047: import org.netbeans.modules.projectimport.LoggerFactory;
048: import org.netbeans.modules.projectimport.ProjectImporterException;
049:
050: /**
051: * Able to load and fill up an <code>EclipseProject</code> from Eclipse project
052: * directory using a .project and .classpath file and eventually passed
053: * workspace. It is also able to load the basic information from workspace.
054: *
055: * @author mkrauskopf
056: */
057: public final class ProjectFactory {
058:
059: /** Logger for this class. */
060: private static final Logger logger = LoggerFactory.getDefault()
061: .createLogger(ProjectFactory.class);
062:
063: /** singleton */
064: private static ProjectFactory instance = new ProjectFactory();
065:
066: private ProjectFactory() {/*empty constructor*/
067: }
068:
069: /** Returns ProjectFactory instance. */
070: public static ProjectFactory getInstance() {
071: return instance;
072: }
073:
074: /**
075: * Loads a project contained in the given <code>projectDir</code> and tries
076: * if there is workspace in the parent directory (which works only for
077: * eclipse internal projects)
078: *
079: * @throws ProjectImporterException if project in the given
080: * <code>projectDir</code> is not a valid Eclipse project.
081: */
082: public EclipseProject load(File projectDir)
083: throws ProjectImporterException {
084: Workspace workspace = Workspace.createWorkspace(projectDir
085: .getParentFile());
086: if (workspace != null) {
087: WorkspaceParser parser = new WorkspaceParser(workspace);
088: parser.parse();
089: }
090: return load(projectDir, workspace);
091: }
092:
093: /**
094: * Loads a project contained in the given <code>projectDir</code>.
095: *
096: * @throws ProjectImporterException if project in the given
097: * <code>projectDir</code> is not a valid Eclipse project.
098: */
099: EclipseProject load(File projectDir, Workspace workspace)
100: throws ProjectImporterException {
101:
102: EclipseProject project = EclipseProject
103: .createProject(projectDir);
104: if (project != null) {
105: project.setWorkspace(workspace);
106: load(project);
107: }
108: return project;
109: }
110:
111: /**
112: * Fullfill given <code>project</code> with all needed information.
113: *
114: * @throws ProjectImporterException if project in the given
115: * <code>projectDir</code> is not a valid Eclipse project.
116: */
117: void load(EclipseProject project) throws ProjectImporterException {
118: logger.finest("Loading project: "
119: + project.getDirectory().getAbsolutePath()); // NOI18N
120: ProjectParser.parse(project);
121: File cpFile = project.getClassPathFile();
122: // non-java project doesn't need to have a classpath file
123: if (cpFile != null && cpFile.exists()) {
124: project.setClassPath(ClassPathParser.parse(cpFile));
125: for (Iterator it = project.getClassPath().getEntries()
126: .iterator(); it.hasNext();) {
127: project.setAbsolutePathForEntry((ClassPathEntry) it
128: .next());
129: }
130: } else {
131: logger.finer("Project " + project.getName() + // NOI18N
132: " doesn't have java nature."); // NOI18N
133: project.setJavaNature(false);
134: }
135: }
136: }
|