KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > ant > freeform > ArtifactProvider


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.ant.freeform;
21
22 import java.io.File JavaDoc;
23 import java.net.URI JavaDoc;
24 import java.net.URISyntaxException JavaDoc;
25 import java.util.ArrayList JavaDoc;
26 import java.util.HashMap JavaDoc;
27 import java.util.HashSet JavaDoc;
28 import java.util.Iterator JavaDoc;
29 import java.util.LinkedHashSet JavaDoc;
30 import java.util.List JavaDoc;
31 import java.util.Set JavaDoc;
32 import org.netbeans.api.project.Project;
33 import org.netbeans.api.project.ant.AntArtifact;
34 import org.netbeans.modules.ant.freeform.spi.support.Util;
35 import org.netbeans.spi.project.ant.AntArtifactProvider;
36 import org.netbeans.spi.project.support.ant.PropertyEvaluator;
37 import org.openide.ErrorManager;
38 import org.w3c.dom.Element JavaDoc;
39
40 /**
41  * Handles <code>&lt;export&gt;</code> elements in project.xml
42  * @author Jesse Glick
43  */

44 final class ArtifactProvider implements AntArtifactProvider {
45     
46     private final FreeformProject project;
47     
48     public ArtifactProvider(FreeformProject project) {
49         this.project = project;
50     }
51
52     public AntArtifact[] getBuildArtifacts() {
53         Element JavaDoc data = project.getPrimaryConfigurationData();
54         List JavaDoc<AntArtifact> artifacts = new ArrayList JavaDoc<AntArtifact>();
55         Set JavaDoc<String JavaDoc> ids = new HashSet JavaDoc<String JavaDoc>();
56         HashMap JavaDoc<String JavaDoc,FreeformArtifact> uniqueArtifacts = new HashMap JavaDoc<String JavaDoc,FreeformArtifact>();
57         for (Element JavaDoc export : Util.findSubElements(data)) {
58             if (!export.getLocalName().equals("export")) { // NOI18N
59
continue;
60             }
61             FreeformArtifact artifact = new FreeformArtifact(export);
62             
63             String JavaDoc artifactKey = artifact.getType() + artifact.getTargetName() + artifact.getScriptLocation().getAbsolutePath();
64             FreeformArtifact alreadyHasArtifact = uniqueArtifacts.get(artifactKey);
65             if (alreadyHasArtifact != null) {
66                 alreadyHasArtifact.addLocation(readArtifactLocation(export, project.evaluator()));
67                 continue;
68             } else {
69                 artifact.addLocation(readArtifactLocation(export, project.evaluator()));
70                 uniqueArtifacts.put(artifactKey, artifact);
71             }
72             
73             String JavaDoc id = artifact.preferredId();
74             if (!ids.add(id)) {
75                 // Need to uniquify it.
76
int counter = 2;
77                 while (true) {
78                     String JavaDoc possibleId = id + counter;
79                     if (ids.add(possibleId)) {
80                         id = possibleId;
81                         break;
82                     }
83                     counter++;
84                 }
85             }
86             artifact.configureId(id);
87             artifacts.add(artifact);
88         }
89         return artifacts.toArray(new AntArtifact[artifacts.size()]);
90     }
91     
92     public static URI JavaDoc readArtifactLocation(Element JavaDoc export, PropertyEvaluator eval) {
93         Element JavaDoc locEl = Util.findElement(export, "location", FreeformProjectType.NS_GENERAL); // NOI18N
94
assert locEl != null;
95         String JavaDoc loc = Util.findText(locEl);
96         assert loc != null;
97         String JavaDoc locationResolved = eval.evaluate(loc);
98         if (locationResolved == null) {
99             return URI.create("file:/UNDEFINED"); // NOI18N
100
}
101         File JavaDoc locF = new File JavaDoc(locationResolved);
102         if (locF.isAbsolute()) {
103             return locF.toURI();
104         } else {
105             // Project-relative path.
106
try {
107                 return new URI JavaDoc(null, null, locationResolved.replace(File.separatorChar, '/'), null);
108             } catch (URISyntaxException JavaDoc e) {
109                 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e);
110                 return URI.create("file:/BROKEN"); // NOI18N
111
}
112         }
113     }
114
115     private final class FreeformArtifact extends AntArtifact {
116         
117         private final Element JavaDoc export;
118         private String JavaDoc id = null;
119         private final Set JavaDoc<URI JavaDoc> locations = new LinkedHashSet JavaDoc<URI JavaDoc>();
120         
121         public FreeformArtifact(Element JavaDoc export) {
122             this.export = export;
123         }
124         
125         public String JavaDoc preferredId() {
126             return getTargetName();
127         }
128         
129         public void configureId(String JavaDoc id) {
130             assert this.id == null;
131             this.id = id;
132         }
133
134         public String JavaDoc getType() {
135             Element JavaDoc typeEl = Util.findElement(export, "type", FreeformProjectType.NS_GENERAL); // NOI18N
136
assert typeEl != null;
137             String JavaDoc type = Util.findText(typeEl);
138             assert type != null;
139             return type;
140         }
141
142         public String JavaDoc getTargetName() {
143             Element JavaDoc targetEl = Util.findElement(export, "build-target", FreeformProjectType.NS_GENERAL); // NOI18N
144
assert targetEl != null;
145             String JavaDoc target = Util.findText(targetEl);
146             assert target != null;
147             return target;
148         }
149
150         public String JavaDoc getCleanTargetName() {
151             Element JavaDoc targetEl = Util.findElement(export, "clean-target", FreeformProjectType.NS_GENERAL); // NOI18N
152
if (targetEl != null) {
153                 String JavaDoc target = Util.findText(targetEl);
154                 assert target != null;
155                 return target;
156             } else {
157                 // Guess based on configured target for 'clean' command, if any.
158
String JavaDoc target = null;
159                 Element JavaDoc genldata = project.getPrimaryConfigurationData();
160                 Element JavaDoc actionsEl = Util.findElement(genldata, "ide-actions", FreeformProjectType.NS_GENERAL); // NOI18N
161
if (actionsEl != null) {
162                     for (Element JavaDoc actionEl : Util.findSubElements(actionsEl)) {
163                         if (actionEl.getAttribute("name").equals("clean")) { // NOI18N
164
for (Element JavaDoc actionTargetEl : Util.findSubElements(actionEl)) {
165                                 if (!actionTargetEl.getLocalName().equals("target")) { // NOI18N
166
continue;
167                                 }
168                                 String JavaDoc possibleTarget = Util.findText(actionTargetEl);
169                                 assert possibleTarget != null;
170                                 if (target == null) {
171                                     // OK, probably use it (unless there is another target for this command).
172
target = possibleTarget;
173                                 } else {
174                                     // Oops! >1 target not supported for AntArtifact.
175
target = null;
176                                     break;
177                                 }
178                             }
179                             // We found the clean command, use that target if we got it.
180
break;
181                         }
182                     }
183                 }
184                 if (target == null) {
185                     // Guess!
186
target = "clean"; // NOI18N
187
}
188                 return target;
189             }
190         }
191
192         public File JavaDoc getScriptLocation() {
193             String JavaDoc loc = null;
194             Element JavaDoc scriptEl = Util.findElement(export, "script", FreeformProjectType.NS_GENERAL); // NOI18N
195
if (scriptEl != null) {
196                 String JavaDoc script = Util.findText(scriptEl);
197                 assert script != null;
198                 loc = project.evaluator().evaluate(script);
199             }
200             if (loc == null) {
201                 // Not configured, or eval failed.
202
loc = "build.xml"; // NOI18N
203
}
204             return project.helper().resolveFile(loc);
205         }
206
207         public Project getProject() {
208             return project;
209         }
210
211         public String JavaDoc getID() {
212             assert id != null;
213             return id;
214         }
215
216         public URI JavaDoc[] getArtifactLocations() {
217             return locations.toArray(new URI JavaDoc[locations.size()]);
218         }
219         
220         private void addLocation(URI JavaDoc u) {
221             locations.add(u);
222         }
223         
224         public String JavaDoc toString() {
225             return "FreeformArtifact[" + project + ":" + id + "]"; // NOI18N
226
}
227
228     }
229
230 }
231
Popular Tags