KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > ruby > api > project > rake > RakeArtifactQuery


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.api.project.rake;
21
22 import java.io.File JavaDoc;
23 import java.util.ArrayList JavaDoc;
24 import java.util.Iterator JavaDoc;
25 import java.util.List JavaDoc;
26 import org.netbeans.api.project.Project;
27 import org.netbeans.modules.ruby.spi.project.rake.RakeArtifactProvider;
28 import org.netbeans.modules.ruby.spi.project.rake.RakeArtifactQueryImplementation;
29 import org.openide.filesystems.FileUtil;
30 import org.openide.util.Lookup;
31
32 /**
33  * Find out how to create a certain build artifact by calling some Ant script.
34  * @see RakeArtifactQueryImplementation
35  * @author Jesse Glick
36  */

37 public class RakeArtifactQuery {
38     
39     private RakeArtifactQuery() {}
40     
41     /**
42      * Try to find an Ant artifact object corresponding to a given file on disk.
43      * The file need not currently exist for the query to succeed.
44      * All registered {@link RakeArtifactQueryImplementation} providers are asked.
45      * @param file a file which might be built by some Ant target
46      * @return an Ant artifact object describing it, or null if it is not recognized
47      */

48     public static RakeArtifact findArtifactFromFile(File JavaDoc file) {
49         if (!file.equals(FileUtil.normalizeFile(file))) {
50             throw new IllegalArgumentException JavaDoc("Parameter file was not "+ // NOI18N
51
"normalized. Was "+file+" instead of "+FileUtil.normalizeFile(file)); // NOI18N
52
}
53         Iterator JavaDoc it = Lookup.getDefault().lookupAll(RakeArtifactQueryImplementation.class).iterator();
54         while (it.hasNext()) {
55             RakeArtifactQueryImplementation aaqi = (RakeArtifactQueryImplementation)it.next();
56             RakeArtifact artifact = aaqi.findArtifact(file);
57             if (artifact != null) {
58                 return artifact;
59             }
60         }
61         return null;
62     }
63     
64     /**
65      * Try to find a particular build artifact according to the Ant target producing it.
66      * @param p a project (should have {@link RakeArtifactProvider} in lookup
67      * in order for this query to work)
68      * @param id a desired {@link RakeArtifact#getID ID}
69      * @return an artifact produced by that project with the specified target,
70      * or null if none such can be found
71      */

72     public static RakeArtifact findArtifactByID(Project p, String JavaDoc id) {
73         RakeArtifactProvider prov = p.getLookup().lookup(RakeArtifactProvider.class);
74         if (prov == null) {
75             return null;
76         }
77         RakeArtifact[] artifacts = prov.getBuildArtifacts();
78         for (int i = 0; i < artifacts.length; i++) {
79             if (artifacts[i].getID().equals(id)) {
80                 return artifacts[i];
81             }
82         }
83         return null;
84     }
85     
86     /**
87      * Try to find build artifacts of a certain type in a project.
88      * @param p a project (should have {@link RakeArtifactProvider} in lookup
89      * in order for this query to work)
90      * @param type a desired {@link RakeArtifact#getType artifact type}
91      * @return all artifacts of the specified type produced by that project
92      * (may be an empty list)
93      */

94     public static RakeArtifact[] findArtifactsByType(Project p, String JavaDoc type) {
95         RakeArtifactProvider prov = p.getLookup().lookup(RakeArtifactProvider.class);
96         if (prov == null) {
97             return new RakeArtifact[0];
98         }
99         RakeArtifact[] artifacts = prov.getBuildArtifacts();
100         List JavaDoc<RakeArtifact> l = new ArrayList JavaDoc<RakeArtifact>(artifacts.length);
101         for (RakeArtifact aa : artifacts) {
102             if (aa.getType().equals(type)) {
103                 l.add(aa);
104             }
105         }
106         return l.toArray(new RakeArtifact[l.size()]);
107     }
108     
109 }
110
Popular Tags