1 19 20 package org.netbeans.modules.ant.freeform; 21 22 import java.io.File ; 23 import java.io.IOException ; 24 import java.util.ArrayList ; 25 import java.util.HashMap ; 26 import java.util.List ; 27 import java.util.Map ; 28 import org.netbeans.api.project.Project; 29 import org.netbeans.api.project.ProjectManager; 30 import org.netbeans.api.queries.CollocationQuery; 31 import org.netbeans.modules.ant.freeform.spi.ProjectConstants; 32 import org.netbeans.modules.ant.freeform.spi.support.Util; 33 import org.netbeans.spi.project.AuxiliaryConfiguration; 34 import org.netbeans.spi.project.support.ant.AntProjectHelper; 35 import org.netbeans.spi.project.support.ant.EditableProperties; 36 import org.netbeans.spi.project.support.ant.ProjectGenerator; 37 import org.netbeans.spi.project.support.ant.PropertyEvaluator; 38 import org.netbeans.spi.project.support.ant.PropertyUtils; 39 import org.openide.filesystems.FileObject; 40 import org.openide.filesystems.FileStateInvalidException; 41 import org.openide.filesystems.FileUtil; 42 import org.openide.util.NbBundle; 43 import org.w3c.dom.Document ; 44 import org.w3c.dom.Element ; 45 import org.w3c.dom.Node ; 46 47 52 public class FreeformProjectGenerator { 53 54 55 private static final String [] rootElementsOrder = new String []{"name", "properties", "folders", "ide-actions", "export", "view", "subprojects"}; private static final String [] viewElementsOrder = new String []{"items", "context-menu"}; 58 private static final String [] viewItemElementsOrder = new String []{"source-folder", "source-file"}; private static final String [] contextMenuElementsOrder = new String []{"ide-action", "separator", "action"}; 63 private FreeformProjectGenerator() {} 64 65 public static AntProjectHelper createProject(File location, File dir, String name, File antScript) throws IOException { 66 FileObject dirFO = createProjectDir(dir); 67 FileObject locationFO = FileUtil.toFileObject(location); 68 AntProjectHelper h = createProject(locationFO, dirFO, name, antScript); 69 Project p = ProjectManager.getDefault().findProject(dirFO); 70 ProjectManager.getDefault().saveProject(p); 71 return h; 72 } 73 74 private static AntProjectHelper createProject(final FileObject locationFO, final FileObject dirFO, final String name, final File antScript) throws IOException { 75 final AntProjectHelper[] h = new AntProjectHelper[1]; 76 final IOException [] ioe = new IOException [1]; 77 ProjectManager.mutex().writeAccess(new Runnable () { 78 public void run() { 79 Project p; 80 try { 81 h[0] = ProjectGenerator.createProject(dirFO, FreeformProjectType.TYPE); 82 p = ProjectManager.getDefault().findProject(dirFO); 83 } catch (IOException e) { 84 ioe[0] = e; 85 return; 86 } 87 AuxiliaryConfiguration aux = p.getLookup().lookup(AuxiliaryConfiguration.class); 88 assert aux != null; 89 90 Element data = Util.getPrimaryConfigurationData(h[0]); 91 Document doc = data.getOwnerDocument(); 92 93 Node comment = doc.createComment(" " + NbBundle.getMessage(FreeformProjectGenerator.class, "LBL_Manual_Editing_Warning") + " "); 94 data.appendChild(comment); 95 96 Element nm = doc.createElementNS(FreeformProjectType.NS_GENERAL, "name"); nm.appendChild(doc.createTextNode(name)); data.appendChild(nm); 99 Element props = doc.createElementNS(FreeformProjectType.NS_GENERAL, "properties"); File locationF = FileUtil.toFile(locationFO); 101 File dirF = FileUtil.toFile(dirFO); 102 Map <String ,String > properties = new HashMap <String ,String >(); 103 if (!locationFO.equals(dirFO)) { 104 Element property = doc.createElementNS(FreeformProjectType.NS_GENERAL, "property"); property.setAttribute("name", ProjectConstants.PROP_PROJECT_LOCATION); String path; 107 if (CollocationQuery.areCollocated(dirF, locationF)) { 108 path = PropertyUtils.relativizeFile(dirF, locationF); } else { 110 path = locationF.getAbsolutePath(); 111 } 112 property.appendChild(doc.createTextNode(path)); 113 props.appendChild(property); 114 properties.put(ProjectConstants.PROP_PROJECT_LOCATION, path); 115 } 116 String antPath = "build.xml"; if (antScript != null) { 118 Element property = doc.createElementNS(FreeformProjectType.NS_GENERAL, "property"); property.setAttribute("name", ProjectConstants.PROP_ANT_SCRIPT); antPath = Util.relativizeLocation(locationF, dirF, antScript); 121 property.appendChild(doc.createTextNode(antPath)); 122 properties.put(ProjectConstants.PROP_ANT_SCRIPT, antPath); 123 antPath = "${"+ProjectConstants.PROP_ANT_SCRIPT+"}"; props.appendChild(property); 125 } 126 data.appendChild(props); 129 Util.putPrimaryConfigurationData(h[0], data); 131 putBuildXMLSourceFile(h[0], antPath); 132 } 133 } 134 ); 135 136 if (ioe[0] != null) { 137 throw ioe[0]; 138 } 139 return h[0]; 140 } 141 142 private static FileObject createProjectDir(File dir) throws IOException { 143 FileObject dirFO; 144 if(!dir.exists()) { 145 refreshFileSystem (dir); 147 dir.mkdirs(); 148 refreshFileSystem (dir); 149 } 150 dirFO = FileUtil.toFileObject(dir); 151 assert dirFO != null : "No such dir on disk: " + dir; assert dirFO.isFolder() : "Not really a dir: " + dir; return dirFO; 154 } 155 156 157 private static void refreshFileSystem (final File dir) throws FileStateInvalidException { 158 File rootF = dir; 159 while (rootF.getParentFile() != null) { 160 rootF = rootF.getParentFile(); 161 } 162 FileObject dirFO = FileUtil.toFileObject(rootF); 163 assert dirFO != null : "At least disk roots must be mounted! " + rootF; dirFO.getFileSystem().refresh(false); 165 } 166 167 172 public static List <TargetMapping> getTargetMappings(AntProjectHelper helper) { 173 List <TargetMapping> list = new ArrayList <TargetMapping>(); 175 Element genldata = Util.getPrimaryConfigurationData(helper); 176 Element actionsEl = Util.findElement(genldata, "ide-actions", FreeformProjectType.NS_GENERAL); if (actionsEl == null) { 178 return list; 179 } 180 for (Element actionEl : Util.findSubElements(actionsEl)) { 181 TargetMapping tm = new TargetMapping(); 182 tm.name = actionEl.getAttribute("name"); List <String > targetNames = new ArrayList <String >(); 184 EditableProperties props = new EditableProperties(false); 185 for (Element subEl : Util.findSubElements(actionEl)) { 186 if (subEl.getLocalName().equals("target")) { targetNames.add(Util.findText(subEl)); 188 continue; 189 } 190 if (subEl.getLocalName().equals("script")) { tm.script = Util.findText(subEl); 192 continue; 193 } 194 if (subEl.getLocalName().equals("context")) { TargetMapping.Context ctx = new TargetMapping.Context(); 196 for (Element contextSubEl : Util.findSubElements(subEl)) { 197 if (contextSubEl.getLocalName().equals("property")) { ctx.property = Util.findText(contextSubEl); 199 continue; 200 } 201 if (contextSubEl.getLocalName().equals("format")) { ctx.format = Util.findText(contextSubEl); 203 continue; 204 } 205 if (contextSubEl.getLocalName().equals("folder")) { ctx.folder = Util.findText(contextSubEl); 207 continue; 208 } 209 if (contextSubEl.getLocalName().equals("pattern")) { ctx.pattern = Util.findText(contextSubEl); 211 continue; 212 } 213 if (contextSubEl.getLocalName().equals("arity")) { Element sepFilesEl = Util.findElement(contextSubEl, "separated-files", FreeformProjectType.NS_GENERAL); if (sepFilesEl != null) { 216 ctx.separator = Util.findText(sepFilesEl); 217 } 218 continue; 219 } 220 } 221 tm.context = ctx; 222 } 223 if (subEl.getLocalName().equals("property")) { readProperty(subEl, props); 225 continue; 226 } 227 } 228 tm.targets = targetNames; 229 if (props.keySet().size() > 0) { 230 tm.properties = props; 231 } 232 list.add(tm); 233 } 234 return list; 235 } 236 237 private static void readProperty(Element propertyElement, EditableProperties props) { 238 String key = propertyElement.getAttribute("name"); String value = Util.findText(propertyElement); 240 props.setProperty(key, value); 241 } 242 243 249 public static void putTargetMappings(AntProjectHelper helper, List <TargetMapping> mappings) { 250 Element data = Util.getPrimaryConfigurationData(helper); 252 Document doc = data.getOwnerDocument(); 253 Element actions = Util.findElement(data, "ide-actions", FreeformProjectType.NS_GENERAL); if (actions != null) { 255 data.removeChild(actions); 256 } 257 258 actions = doc.createElementNS(FreeformProjectType.NS_GENERAL, "ide-actions"); for (TargetMapping tm : mappings) { 260 Element action = doc.createElementNS(FreeformProjectType.NS_GENERAL, "action"); assert tm.name != null && tm.name.length() > 0; 262 action.setAttribute("name", tm.name); if (tm.script != null) { 264 Element script = doc.createElementNS(FreeformProjectType.NS_GENERAL, "script"); script.appendChild(doc.createTextNode(tm.script)); 266 action.appendChild(script); 267 } 268 if (tm.targets != null) { 269 for (String targetName : tm.targets) { 270 Element target = doc.createElementNS(FreeformProjectType.NS_GENERAL, "target"); target.appendChild(doc.createTextNode(targetName)); 272 action.appendChild(target); 273 } 274 } 275 if (tm.properties != null) { 276 writeProperties(tm.properties, doc, action); 277 } 278 if (tm.context != null) { 279 Element context = doc.createElementNS(FreeformProjectType.NS_GENERAL, "context"); TargetMapping.Context ctx = tm.context; 281 assert ctx.property != null; 282 Element property = doc.createElementNS(FreeformProjectType.NS_GENERAL, "property"); property.appendChild(doc.createTextNode(ctx.property)); 284 context.appendChild(property); 285 assert ctx.folder != null; 286 Element folder = doc.createElementNS(FreeformProjectType.NS_GENERAL, "folder"); folder.appendChild(doc.createTextNode(ctx.folder)); 288 context.appendChild(folder); 289 if (ctx.pattern != null) { 290 Element pattern = doc.createElementNS(FreeformProjectType.NS_GENERAL, "pattern"); pattern.appendChild(doc.createTextNode(ctx.pattern)); 292 context.appendChild(pattern); 293 } 294 assert ctx.format != null; 295 Element format = doc.createElementNS(FreeformProjectType.NS_GENERAL, "format"); format.appendChild(doc.createTextNode(ctx.format)); 297 context.appendChild(format); 298 Element arity = doc.createElementNS(FreeformProjectType.NS_GENERAL, "arity"); if (ctx.separator != null) { 300 Element sepFilesEl = doc.createElementNS(FreeformProjectType.NS_GENERAL, "separated-files"); sepFilesEl.appendChild(doc.createTextNode(ctx.separator)); 302 arity.appendChild(sepFilesEl); 303 } else { 304 arity.appendChild(doc.createElementNS(FreeformProjectType.NS_GENERAL, "one-file-only")); } 306 context.appendChild(arity); 307 action.appendChild(context); 308 } 309 actions.appendChild(action); 310 } 311 Util.appendChildElement(data, actions, rootElementsOrder); 312 Util.putPrimaryConfigurationData(helper, data); 313 } 314 315 private static void writeProperties(EditableProperties props, Document doc, Element element) { 316 for (Map.Entry <String ,String > entry : props.entrySet()) { 317 Element property = doc.createElementNS(FreeformProjectType.NS_GENERAL, "property"); property.setAttribute("name", entry.getKey()); property.appendChild(doc.createTextNode(entry.getValue())); 320 element.appendChild(property); 321 } 322 } 323 324 333 public static void putContextMenuAction(AntProjectHelper helper, List <TargetMapping> mappings) { 334 Element data = Util.getPrimaryConfigurationData(helper); 336 Document doc = data.getOwnerDocument(); 337 Element viewEl = Util.findElement(data, "view", FreeformProjectType.NS_GENERAL); if (viewEl == null) { 339 viewEl = doc.createElementNS(FreeformProjectType.NS_GENERAL, "view"); Util.appendChildElement(data, viewEl, rootElementsOrder); 341 } 342 Element contextMenuEl = Util.findElement(viewEl, "context-menu", FreeformProjectType.NS_GENERAL); if (contextMenuEl == null) { 344 contextMenuEl = doc.createElementNS(FreeformProjectType.NS_GENERAL, "context-menu"); Util.appendChildElement(viewEl, contextMenuEl, viewElementsOrder); 346 } 347 for (Element ideActionEl : Util.findSubElements(contextMenuEl)) { 348 if (!ideActionEl.getLocalName().equals("ide-action")) { continue; 350 } 351 contextMenuEl.removeChild(ideActionEl); 352 } 353 for (TargetMapping tm : mappings) { 354 if (tm.context != null) { 355 continue; 357 } 358 Element ideAction = doc.createElementNS(FreeformProjectType.NS_GENERAL, "ide-action"); ideAction.setAttribute("name", tm.name); Util.appendChildElement(contextMenuEl, ideAction, contextMenuElementsOrder); 361 } 362 Util.putPrimaryConfigurationData(helper, data); 363 } 364 365 370 public static List <CustomTarget> getCustomContextMenuActions(AntProjectHelper helper) { 371 List <CustomTarget> list = new ArrayList <CustomTarget>(); 373 Element genldata = Util.getPrimaryConfigurationData(helper); 374 Element viewEl = Util.findElement(genldata, "view", FreeformProjectType.NS_GENERAL); if (viewEl == null) { 376 return list; 377 } 378 Element contextMenuEl = Util.findElement(viewEl, "context-menu", FreeformProjectType.NS_GENERAL); if (contextMenuEl == null) { 380 return list; 381 } 382 for (Element actionEl : Util.findSubElements(contextMenuEl)) { 383 if (!actionEl.getLocalName().equals("action")) { continue; 385 } 386 CustomTarget ct = new CustomTarget(); 387 List <String > targetNames = new ArrayList <String >(); 388 EditableProperties props = new EditableProperties(false); 389 for (Element subEl : Util.findSubElements(actionEl)) { 390 if (subEl.getLocalName().equals("target")) { targetNames.add(Util.findText(subEl)); 392 continue; 393 } 394 if (subEl.getLocalName().equals("script")) { ct.script = Util.findText(subEl); 396 continue; 397 } 398 if (subEl.getLocalName().equals("label")) { ct.label = Util.findText(subEl); 400 continue; 401 } 402 if (subEl.getLocalName().equals("property")) { readProperty(subEl, props); 404 continue; 405 } 406 } 407 ct.targets = targetNames; 408 if (props.keySet().size() > 0) { 409 ct.properties = props; 410 } 411 list.add(ct); 412 } 413 return list; 414 } 415 416 424 public static void putCustomContextMenuActions(AntProjectHelper helper, List <CustomTarget> customTargets) { 425 Element data = Util.getPrimaryConfigurationData(helper); 427 Document doc = data.getOwnerDocument(); 428 Element viewEl = Util.findElement(data, "view", FreeformProjectType.NS_GENERAL); if (viewEl == null) { 430 viewEl = doc.createElementNS(FreeformProjectType.NS_GENERAL, "view"); Util.appendChildElement(data, viewEl, rootElementsOrder); 432 } 433 Element contextMenuEl = Util.findElement(viewEl, "context-menu", FreeformProjectType.NS_GENERAL); if (contextMenuEl == null) { 435 contextMenuEl = doc.createElementNS(FreeformProjectType.NS_GENERAL, "context-menu"); Util.appendChildElement(viewEl, contextMenuEl, viewElementsOrder); 437 } 438 for (Element actionEl : Util.findSubElements(contextMenuEl)) { 439 if (!actionEl.getLocalName().equals("action")) { continue; 441 } 442 contextMenuEl.removeChild(actionEl); 443 } 444 for (CustomTarget ct : customTargets) { 445 Element action = doc.createElementNS(FreeformProjectType.NS_GENERAL, "action"); if (ct.script != null) { 447 Element script = doc.createElementNS(FreeformProjectType.NS_GENERAL, "script"); script.appendChild(doc.createTextNode(ct.script)); action.appendChild(script); 450 } 451 Element label = doc.createElementNS(FreeformProjectType.NS_GENERAL, "label"); label.appendChild(doc.createTextNode(ct.label)); action.appendChild(label); 454 if (ct.targets != null) { 455 for (String targetName : ct.targets) { 456 Element target = doc.createElementNS(FreeformProjectType.NS_GENERAL, "target"); target.appendChild(doc.createTextNode(targetName)); action.appendChild(target); 459 } 460 } 461 if (ct.properties != null) { 462 writeProperties(ct.properties, doc, action); 463 } 464 Util.appendChildElement(contextMenuEl, action, contextMenuElementsOrder); 465 } 466 Util.putPrimaryConfigurationData(helper, data); 467 } 468 469 473 public static final class CustomTarget { 474 public List <String > targets; 475 public String label; 476 public String script; 477 public EditableProperties properties; 478 } 479 480 484 public static final class TargetMapping { 485 public String script; 486 public List <String > targets; 487 public String name; 488 public EditableProperties properties; 489 public Context context; 491 public static final class Context { 492 public String property; 493 public String format; 494 public String folder; 495 public String pattern; public String separator; } 498 } 499 500 private static void putBuildXMLSourceFile(AntProjectHelper helper, String antPath) { 501 Element data = Util.getPrimaryConfigurationData(helper); 502 Document doc = data.getOwnerDocument(); 503 Element viewEl = Util.findElement(data, "view", FreeformProjectType.NS_GENERAL); if (viewEl == null) { 505 viewEl = doc.createElementNS(FreeformProjectType.NS_GENERAL, "view"); Util.appendChildElement(data, viewEl, rootElementsOrder); 507 } 508 Element itemsEl = Util.findElement(viewEl, "items", FreeformProjectType.NS_GENERAL); if (itemsEl == null) { 510 itemsEl = doc.createElementNS(FreeformProjectType.NS_GENERAL, "items"); Util.appendChildElement(viewEl, itemsEl, viewElementsOrder); 512 } 513 Element fileEl = doc.createElementNS(FreeformProjectType.NS_GENERAL, "source-file"); Element el = doc.createElementNS(FreeformProjectType.NS_GENERAL, "location"); el.appendChild(doc.createTextNode(antPath)); fileEl.appendChild(el); 517 Util.appendChildElement(itemsEl, fileEl, viewItemElementsOrder); 518 Util.putPrimaryConfigurationData(helper, data); 519 } 520 521 528 public static FileObject getAntScript(AntProjectHelper helper, PropertyEvaluator ev) { 529 String antScript = ev.getProperty(ProjectConstants.PROP_ANT_SCRIPT); 531 if (antScript != null) { 532 File f= helper.resolveFile(antScript); 533 if (!f.exists()) { 534 return null; 535 } 536 FileObject fo = FileUtil.toFileObject(f); 537 return fo; 538 } else { 539 FileObject fo = helper.getProjectDirectory().getFileObject("build.xml"); return fo; 541 } 542 } 543 544 } 545 | Popular Tags |