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