KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > api > project > ant > AntArtifactQuery


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

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

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

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

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