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.api.project.ant;
043:
044: import java.io.File;
045: import java.util.ArrayList;
046: import java.util.List;
047: import org.netbeans.api.project.Project;
048: import org.netbeans.spi.project.ant.AntArtifactProvider;
049: import org.netbeans.spi.project.ant.AntArtifactQueryImplementation;
050: import org.openide.filesystems.FileUtil;
051: import org.openide.util.Lookup;
052:
053: /**
054: * Find out how to create a certain build artifact by calling some Ant script.
055: * @see AntArtifactQueryImplementation
056: * @author Jesse Glick
057: */
058: public class AntArtifactQuery {
059:
060: private AntArtifactQuery() {
061: }
062:
063: /**
064: * Try to find an Ant artifact object corresponding to a given file on disk.
065: * The file need not currently exist for the query to succeed.
066: * All registered {@link AntArtifactQueryImplementation} providers are asked.
067: * @param file a file which might be built by some Ant target
068: * @return an Ant artifact object describing it, or null if it is not recognized
069: */
070: public static AntArtifact findArtifactFromFile(File file) {
071: if (!file.equals(FileUtil.normalizeFile(file))) {
072: throw new IllegalArgumentException(
073: "Parameter file was not "
074: + // NOI18N
075: "normalized. Was " + file + " instead of "
076: + FileUtil.normalizeFile(file)); // NOI18N
077: }
078: for (AntArtifactQueryImplementation aaqi : Lookup.getDefault()
079: .lookupAll(AntArtifactQueryImplementation.class)) {
080: AntArtifact artifact = aaqi.findArtifact(file);
081: if (artifact != null) {
082: return artifact;
083: }
084: }
085: return null;
086: }
087:
088: /**
089: * Try to find a particular build artifact according to the Ant target producing it.
090: * @param p a project (should have {@link AntArtifactProvider} in lookup
091: * in order for this query to work)
092: * @param id a desired {@link AntArtifact#getID ID}
093: * @return an artifact produced by that project with the specified target,
094: * or null if none such can be found
095: */
096: public static AntArtifact findArtifactByID(Project p, String id) {
097: AntArtifactProvider prov = p.getLookup().lookup(
098: AntArtifactProvider.class);
099: if (prov == null) {
100: return null;
101: }
102: for (AntArtifact artifact : prov.getBuildArtifacts()) {
103: if (artifact.getID().equals(id)) {
104: return artifact;
105: }
106: }
107: return null;
108: }
109:
110: /**
111: * Try to find build artifacts of a certain type in a project.
112: * @param p a project (should have {@link AntArtifactProvider} in lookup
113: * in order for this query to work)
114: * @param type a desired {@link AntArtifact#getType artifact type}
115: * @return all artifacts of the specified type produced by that project
116: * (may be an empty list)
117: */
118: public static AntArtifact[] findArtifactsByType(Project p,
119: String type) {
120: AntArtifactProvider prov = p.getLookup().lookup(
121: AntArtifactProvider.class);
122: if (prov == null) {
123: return new AntArtifact[0];
124: }
125: AntArtifact[] artifacts = prov.getBuildArtifacts();
126: List<AntArtifact> l = new ArrayList<AntArtifact>(
127: artifacts.length);
128: for (AntArtifact aa : artifacts) {
129: if (aa.getType().equals(type)) {
130: l.add(aa);
131: }
132: }
133: return l.toArray(new AntArtifact[l.size()]);
134: }
135:
136: }
|