KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > ruby > railsprojects > RailsProjectUtil


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.ruby.railsprojects;
21
22 import java.io.File JavaDoc;
23 import java.net.MalformedURLException JavaDoc;
24 import java.net.URL JavaDoc;
25 import java.util.ArrayList JavaDoc;
26 import java.util.List JavaDoc;
27 import org.netbeans.modules.ruby.rubyproject.api.RubyInstallation;
28 import org.netbeans.api.project.Project;
29 import org.openide.filesystems.FileObject;
30 import org.openide.filesystems.FileUtil;
31
32 /**
33  * Miscellaneous utilities for the j2seproject module.
34  * @author Jiri Rechtacek
35  */

36 public class RailsProjectUtil {
37     private RailsProjectUtil () {}
38     
39     /**
40      * Returns the property value evaluated by RailsProject's PropertyEvaluator.
41      *
42      * @param p project
43      * @param value of property
44      * @return evaluated value of given property or null if the property not set or
45      * if the project doesn't provide RakeProjectHelper
46      */

47     public static Object JavaDoc getEvaluatedProperty(Project p, String JavaDoc value) {
48         if (value == null) {
49             return null;
50         }
51         RailsProject j2seprj = (RailsProject) p.getLookup().lookup(RailsProject.class);
52         if (j2seprj != null) {
53             return j2seprj.evaluator().evaluate(value);
54         } else {
55             return null;
56         }
57     }
58     
59     public static void getAllScripts(String JavaDoc prefix, FileObject sourcesRoot, List JavaDoc/*<String>*/ result) {
60         FileObject children[] = sourcesRoot.getChildren();
61         if (!"".equals(prefix)) {
62             prefix += "/";
63             //prefix += ".";
64
}
65         for (int i = 0; i < children.length; i++) {
66             if (children[i].isData()) {
67                 if (children[i].getMIMEType().equals(RubyInstallation.RUBY_MIME_TYPE)) {
68                     result.add(prefix + children[i].getNameExt());
69                 }
70             }
71             if (children[i].isFolder()) {
72                 getAllScripts(prefix + children[i].getNameExt(), children[i], result);
73             }
74         }
75     }
76     
77     
78     /**
79      * Creates an URL of a classpath or sourcepath root
80      * For the existing directory it returns the URL obtained from {@link File#toUri()}
81      * For archive file it returns an URL of the root of the archive file
82      * For non existing directory it fixes the ending '/'
83      * @param root the file of a root
84      * @param offset a path relative to the root file or null (eg. src/ for jar:file:///lib.jar!/src/)"
85      * @return an URL of the root
86      * @throws MalformedURLException if the URL cannot be created
87      */

88     public static URL JavaDoc getRootURL (File JavaDoc root, String JavaDoc offset) throws MalformedURLException JavaDoc {
89         URL JavaDoc url = root.toURI().toURL();
90         if (FileUtil.isArchiveFile(url)) {
91             url = FileUtil.getArchiveRoot(url);
92         } else if (!root.exists()) {
93             url = new URL JavaDoc(url.toExternalForm() + "/"); // NOI18N
94
}
95         if (offset != null) {
96             assert offset.endsWith("/"); //NOI18N
97
url = new URL JavaDoc(url.toExternalForm() + offset); // NOI18N
98
}
99         return url;
100     }
101 }
102
Popular Tags