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.BufferedInputStream;
045: import java.io.File;
046: import java.io.FileInputStream;
047: import java.io.IOException;
048: import java.io.InputStream;
049: import java.util.Properties;
050: import org.openide.filesystems.FileUtil;
051:
052: /**
053: * Serveral helpers for parsing, managing, loading Eclipse projects and
054: * workspace metadata.
055: *
056: * @author mkrauskopf
057: */
058: public class EclipseUtils {
059:
060: /**
061: * Returns whether there is a valid project in the given
062: * <code>projectDir</code>.
063: */
064: public static boolean isRegularProject(String projectDir) {
065: return projectDir != null
066: && isRegularProject(new File(projectDir.trim()));
067: }
068:
069: /**
070: * Returns whether there is a valid project in the given
071: * <code>projectDir</code>.
072: */
073: public static boolean isRegularProject(File projectDir) {
074: return projectDir != null
075: && FileUtil.toFileObject(FileUtil
076: .normalizeFile(projectDir)) != null
077: && projectDir.isDirectory()
078: && new File(projectDir, EclipseProject.PROJECT_FILE)
079: .isFile();
080: }
081:
082: /**
083: * Returns whether there is a valid project in the given
084: * <code>projectDir</code> and if the project has java nature.
085: */
086: public static boolean isRegularJavaProject(File projectDir) {
087: return isRegularProject(projectDir)
088: && new File(projectDir, EclipseProject.CLASSPATH_FILE)
089: .isFile();
090: }
091:
092: /**
093: * Returns whether there is a valid project in the given
094: * <code>projectDir</code> and if the project has java nature.
095: */
096: public static boolean isRegularJavaProject(String projectDir) {
097: return projectDir != null
098: && isRegularJavaProject(new File(projectDir.trim()));
099: }
100:
101: /**
102: * Returns whether there is a valid workspace in the given
103: * <code>workspaceDir</code>.
104: */
105: public static boolean isRegularWorkSpace(String workspaceDir) {
106: return workspaceDir != null
107: && isRegularWorkSpace(new File(workspaceDir.trim()));
108: }
109:
110: /**
111: * Returns whether there is a valid workspace in the given
112: * <code>workspaceDir</code>.
113: */
114: public static boolean isRegularWorkSpace(File workspaceDir) {
115: FileUtil.toFileObject(FileUtil.normalizeFile(workspaceDir));
116: return workspaceDir != null
117: && FileUtil.toFileObject(FileUtil
118: .normalizeFile(workspaceDir)) != null
119: && workspaceDir.isDirectory()
120: && new File(workspaceDir, Workspace.CORE_PREFERENCE)
121: .isFile()
122: && new File(workspaceDir,
123: Workspace.LAUNCHING_PREFERENCES).isFile()
124: && new File(workspaceDir,
125: Workspace.RESOURCE_PROJECTS_DIR).isDirectory();
126: }
127:
128: private static final String TMP_NAME = "NB___TMP___ENOUGH___UNIQUE___CONSTANT___"; // NOI18N
129:
130: public static boolean isWritable(String projectDestination) {
131: File tmpDir = new File(projectDestination.trim(),
132: (TMP_NAME + System.currentTimeMillis()));
133: if (tmpDir.mkdirs()) {
134: tmpDir.delete();
135: return true;
136: }
137: return false;
138: }
139:
140: /**
141: * Load properties from a given <code>file</code>.
142: * <p>
143: * <strong>Note: package private for unit tests only.</strong>
144: *
145: * @throws IOException when reading file failed
146: */
147: static Properties loadProperties(File file) throws IOException {
148: InputStream propsIS = new BufferedInputStream(
149: new FileInputStream(file));
150: Properties properties = new Properties();
151: try {
152: properties.load(propsIS);
153: } finally {
154: propsIS.close();
155: }
156: return properties;
157: }
158:
159: }
|