1 19 20 package org.netbeans.modules.ant.freeform; 21 22 import java.io.File ; 23 import java.net.URI ; 24 import java.net.URISyntaxException ; 25 import java.util.ArrayList ; 26 import java.util.HashMap ; 27 import java.util.HashSet ; 28 import java.util.Iterator ; 29 import java.util.LinkedHashSet ; 30 import java.util.List ; 31 import java.util.Set ; 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 ; 39 40 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 data = project.getPrimaryConfigurationData(); 54 List <AntArtifact> artifacts = new ArrayList <AntArtifact>(); 55 Set <String > ids = new HashSet <String >(); 56 HashMap <String ,FreeformArtifact> uniqueArtifacts = new HashMap <String ,FreeformArtifact>(); 57 for (Element export : Util.findSubElements(data)) { 58 if (!export.getLocalName().equals("export")) { continue; 60 } 61 FreeformArtifact artifact = new FreeformArtifact(export); 62 63 String 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 id = artifact.preferredId(); 74 if (!ids.add(id)) { 75 int counter = 2; 77 while (true) { 78 String 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 readArtifactLocation(Element export, PropertyEvaluator eval) { 93 Element locEl = Util.findElement(export, "location", FreeformProjectType.NS_GENERAL); assert locEl != null; 95 String loc = Util.findText(locEl); 96 assert loc != null; 97 String locationResolved = eval.evaluate(loc); 98 if (locationResolved == null) { 99 return URI.create("file:/UNDEFINED"); } 101 File locF = new File (locationResolved); 102 if (locF.isAbsolute()) { 103 return locF.toURI(); 104 } else { 105 try { 107 return new URI (null, null, locationResolved.replace(File.separatorChar, '/'), null); 108 } catch (URISyntaxException e) { 109 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e); 110 return URI.create("file:/BROKEN"); } 112 } 113 } 114 115 private final class FreeformArtifact extends AntArtifact { 116 117 private final Element export; 118 private String id = null; 119 private final Set <URI > locations = new LinkedHashSet <URI >(); 120 121 public FreeformArtifact(Element export) { 122 this.export = export; 123 } 124 125 public String preferredId() { 126 return getTargetName(); 127 } 128 129 public void configureId(String id) { 130 assert this.id == null; 131 this.id = id; 132 } 133 134 public String getType() { 135 Element typeEl = Util.findElement(export, "type", FreeformProjectType.NS_GENERAL); assert typeEl != null; 137 String type = Util.findText(typeEl); 138 assert type != null; 139 return type; 140 } 141 142 public String getTargetName() { 143 Element targetEl = Util.findElement(export, "build-target", FreeformProjectType.NS_GENERAL); assert targetEl != null; 145 String target = Util.findText(targetEl); 146 assert target != null; 147 return target; 148 } 149 150 public String getCleanTargetName() { 151 Element targetEl = Util.findElement(export, "clean-target", FreeformProjectType.NS_GENERAL); if (targetEl != null) { 153 String target = Util.findText(targetEl); 154 assert target != null; 155 return target; 156 } else { 157 String target = null; 159 Element genldata = project.getPrimaryConfigurationData(); 160 Element actionsEl = Util.findElement(genldata, "ide-actions", FreeformProjectType.NS_GENERAL); if (actionsEl != null) { 162 for (Element actionEl : Util.findSubElements(actionsEl)) { 163 if (actionEl.getAttribute("name").equals("clean")) { for (Element actionTargetEl : Util.findSubElements(actionEl)) { 165 if (!actionTargetEl.getLocalName().equals("target")) { continue; 167 } 168 String possibleTarget = Util.findText(actionTargetEl); 169 assert possibleTarget != null; 170 if (target == null) { 171 target = possibleTarget; 173 } else { 174 target = null; 176 break; 177 } 178 } 179 break; 181 } 182 } 183 } 184 if (target == null) { 185 target = "clean"; } 188 return target; 189 } 190 } 191 192 public File getScriptLocation() { 193 String loc = null; 194 Element scriptEl = Util.findElement(export, "script", FreeformProjectType.NS_GENERAL); if (scriptEl != null) { 196 String script = Util.findText(scriptEl); 197 assert script != null; 198 loc = project.evaluator().evaluate(script); 199 } 200 if (loc == null) { 201 loc = "build.xml"; } 204 return project.helper().resolveFile(loc); 205 } 206 207 public Project getProject() { 208 return project; 209 } 210 211 public String getID() { 212 assert id != null; 213 return id; 214 } 215 216 public URI [] getArtifactLocations() { 217 return locations.toArray(new URI [locations.size()]); 218 } 219 220 private void addLocation(URI u) { 221 locations.add(u); 222 } 223 224 public String toString() { 225 return "FreeformArtifact[" + project + ":" + id + "]"; } 227 228 } 229 230 } 231 | Popular Tags |