KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > projectimport > eclipse > EclipseUtils


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.projectimport.eclipse;
21
22 import java.io.BufferedInputStream JavaDoc;
23 import java.io.File JavaDoc;
24 import java.io.FileInputStream JavaDoc;
25 import java.io.IOException JavaDoc;
26 import java.io.InputStream JavaDoc;
27 import java.util.Properties JavaDoc;
28 import org.openide.filesystems.FileUtil;
29
30 /**
31  * Serveral helpers for parsing, managing, loading Eclipse projects and
32  * workspace metadata.
33  *
34  * @author mkrauskopf
35  */

36 public class EclipseUtils {
37
38     /**
39      * Returns whether there is a valid project in the given
40      * <code>projectDir</code>.
41      */

42     public static boolean isRegularProject(String JavaDoc projectDir) {
43         return projectDir != null &&
44                 isRegularProject(new File JavaDoc(projectDir.trim()));
45     }
46     
47     /**
48      * Returns whether there is a valid project in the given
49      * <code>projectDir</code>.
50      */

51     public static boolean isRegularProject(File JavaDoc projectDir) {
52         return projectDir != null
53                 && FileUtil.toFileObject(FileUtil.normalizeFile(projectDir)) != null
54                 && projectDir.isDirectory()
55                 && new File JavaDoc(projectDir, EclipseProject.PROJECT_FILE).isFile();
56     }
57     
58     /**
59      * Returns whether there is a valid project in the given
60      * <code>projectDir</code> and if the project has java nature.
61      */

62     public static boolean isRegularJavaProject(File JavaDoc projectDir) {
63         return isRegularProject(projectDir) &&
64                 new File JavaDoc(projectDir, EclipseProject.CLASSPATH_FILE).isFile();
65     }
66     
67     /**
68      * Returns whether there is a valid project in the given
69      * <code>projectDir</code> and if the project has java nature.
70      */

71     public static boolean isRegularJavaProject(String JavaDoc projectDir) {
72         return projectDir != null &&
73                 isRegularJavaProject(new File JavaDoc(projectDir.trim()));
74     }
75     
76     /**
77      * Returns whether there is a valid workspace in the given
78      * <code>workspaceDir</code>.
79      */

80     public static boolean isRegularWorkSpace(String JavaDoc workspaceDir) {
81         return workspaceDir != null &&
82                 isRegularWorkSpace(new File JavaDoc(workspaceDir.trim()));
83     }
84     
85     /**
86      * Returns whether there is a valid workspace in the given
87      * <code>workspaceDir</code>.
88      */

89     public static boolean isRegularWorkSpace(File JavaDoc workspaceDir) {
90         FileUtil.toFileObject(FileUtil.normalizeFile(workspaceDir));
91         return workspaceDir != null
92                 && FileUtil.toFileObject(FileUtil.normalizeFile(workspaceDir)) != null
93                 && workspaceDir.isDirectory()
94                 && new File JavaDoc(workspaceDir, Workspace.CORE_PREFERENCE).isFile()
95                 && new File JavaDoc(workspaceDir, Workspace.LAUNCHING_PREFERENCES).isFile()
96                 && new File JavaDoc(workspaceDir, Workspace.RESOURCE_PROJECTS_DIR).isDirectory();
97     }
98     
99     private static final String JavaDoc TMP_NAME =
100             "NB___TMP___ENOUGH___UNIQUE___CONSTANT___"; // NOI18N
101

102     public static boolean isWritable(String JavaDoc projectDestination) {
103         File JavaDoc tmpDir = new File JavaDoc(projectDestination.trim(),
104                 (TMP_NAME + System.currentTimeMillis()));
105         if (tmpDir.mkdirs()) {
106             tmpDir.delete();
107             return true;
108         }
109         return false;
110     }
111     
112     /**
113      * Load properties from a given <code>file</code>.
114      * <p>
115      * <strong>Note: package private for unit tests only.</strong>
116      *
117      * @throws IOException when reading file failed
118      */

119     static Properties JavaDoc loadProperties(File JavaDoc file) throws IOException JavaDoc {
120         InputStream JavaDoc propsIS = new BufferedInputStream JavaDoc(new FileInputStream JavaDoc(file));
121         Properties JavaDoc properties = new Properties JavaDoc();
122         try {
123             properties.load(propsIS);
124         } finally {
125             propsIS.close();
126         }
127         return properties;
128     }
129     
130 }
131
Popular Tags