1 19 20 package org.netbeans.modules.java.freeform.jdkselection; 21 22 import java.io.IOException ; 23 import java.io.OutputStream ; 24 import java.util.ArrayList ; 25 import java.util.HashSet ; 26 import java.util.List ; 27 import java.util.Set ; 28 import org.netbeans.api.java.platform.JavaPlatform; 29 import org.netbeans.api.java.platform.JavaPlatformManager; 30 import org.netbeans.api.project.Project; 31 import org.netbeans.api.project.ProjectManager; 32 import org.netbeans.api.project.ProjectUtils; 33 import org.netbeans.modules.ant.freeform.spi.support.Util; 34 import org.netbeans.modules.java.freeform.JavaProjectGenerator; 35 import org.netbeans.spi.project.support.ant.AntProjectHelper; 36 import org.netbeans.spi.project.support.ant.EditableProperties; 37 import org.netbeans.spi.project.support.ant.GeneratedFilesHelper; 38 import org.netbeans.spi.project.support.ant.PropertyEvaluator; 39 import org.openide.filesystems.FileLock; 40 import org.openide.filesystems.FileObject; 41 import org.openide.filesystems.FileSystem; 42 import org.openide.filesystems.FileUtil; 43 import org.openide.xml.XMLUtil; 44 import org.w3c.dom.Document ; 45 import org.w3c.dom.Element ; 46 import org.w3c.dom.NodeList ; 47 48 53 public class JdkConfiguration { 54 55 private static final String NS_GENERAL = "http://www.netbeans.org/ns/project/1"; private static final String NBJDK_PROPERTIES = "nbproject/nbjdk.properties"; private static final String NBJDK_ACTIVE = "nbjdk.active"; public static final String NBJDK_XML = "nbproject/nbjdk.xml"; public static final String JDK_XML = "nbproject/jdk.xml"; private static final String PLATFORM_ID_DEFAULT = "default_platform"; 62 private final Project project; 63 private final AntProjectHelper helper; 64 private final PropertyEvaluator evaluator; 65 66 public JdkConfiguration(Project project, AntProjectHelper helper, PropertyEvaluator evaluator) { 67 this.project = project; 68 this.helper = helper; 69 this.evaluator = evaluator; 70 } 71 72 80 private void initialize() throws IOException { 81 project.getProjectDirectory().getFileSystem().runAtomicAction(new FileSystem.AtomicAction() { 82 public void run() throws IOException { 83 createJdkXml(); 84 if (project.getProjectDirectory().getFileObject(NBJDK_XML) != null) { 85 return; 86 } 87 Element generalDataE = Util.getPrimaryConfigurationData(helper); 88 Document nbjdkDoc = createNbjdkXmlSkeleton(); 89 rebindAllActions(generalDataE, nbjdkDoc); 90 writeXML(nbjdkDoc, NBJDK_XML); 91 Util.putPrimaryConfigurationData(helper, generalDataE); 92 ProjectManager.getDefault().saveProject(project); 93 } 94 }); 95 } 96 97 private void createJdkXml() throws IOException { 98 ProjectManager.getDefault().saveProject(project); new GeneratedFilesHelper(helper).refreshBuildScript(JDK_XML, JdkConfiguration.class.getResource("jdk.xsl"), true); 100 } 101 102 107 public static void insertJdkXmlImport(Document doc) { 108 NodeList nl = doc.getElementsByTagName("import"); for (int i = 0; i < nl.getLength(); i++) { 110 if (((Element ) nl.item(i)).getAttribute("file").equals("jdk.xml")) { return; 112 } 113 } 114 Element projectE = doc.getDocumentElement(); 115 Element propE = doc.createElement("property"); propE.setAttribute("file", NBJDK_PROPERTIES); projectE.appendChild(propE); 118 propE = doc.createElement("property"); propE.setAttribute("name", "user.properties.file"); propE.setAttribute("location", "${netbeans.user}/build.properties"); projectE.appendChild(propE); 122 propE = doc.createElement("property"); propE.setAttribute("file", "${user.properties.file}"); projectE.appendChild(propE); 125 Element importE = doc.createElement("import"); importE.setAttribute("file", "jdk.xml"); projectE.appendChild(importE); 128 } 129 130 private Document createNbjdkXmlSkeleton() { 131 Document nbjdkDoc = XMLUtil.createDocument("project", null, null, null); Element projectE = nbjdkDoc.getDocumentElement(); 133 projectE.setAttribute("name", ProjectUtils.getInformation(project).getName()); projectE.setAttribute("basedir", ".."); insertJdkXmlImport(nbjdkDoc); 137 return nbjdkDoc; 138 } 139 140 private void rebindAllActions(Element generalDataE, Document nbjdkDoc) { 141 Element projectE = nbjdkDoc.getDocumentElement(); 142 Set <String > targetsCreated = new HashSet <String >(); 143 Element ideActionsE = Util.findElement(generalDataE, "ide-actions", Util.NAMESPACE); 145 if (ideActionsE != null) { 146 for (Element actionE : Util.findSubElements(ideActionsE)) { 147 rebindAction(actionE, projectE, targetsCreated); 148 } 149 } 150 Element viewE = Util.findElement(generalDataE, "ide-actions", Util.NAMESPACE); 151 if (viewE != null) { 152 Element contextMenuE = Util.findElement(viewE, "context-menu", Util.NAMESPACE); 153 if (contextMenuE != null) { 154 for (Element actionE : Util.findSubElements(contextMenuE)) { 155 if (!actionE.getLocalName().equals("action")) { 156 continue; } 158 rebindAction(actionE, projectE, targetsCreated); 159 } 160 } 161 } 162 } 166 167 private void rebindAction(Element actionE, Element projectE, Set <String > targetsCreated) { 168 Element scriptE = Util.findElement(actionE, "script", Util.NAMESPACE); String script; 170 if (scriptE != null) { 171 script = Util.findText(scriptE); 172 actionE.removeChild(scriptE); 173 } else { 174 script = "build.xml"; } 176 scriptE = actionE.getOwnerDocument().createElementNS(Util.NAMESPACE, "script"); scriptE.appendChild(actionE.getOwnerDocument().createTextNode(NBJDK_XML)); 178 actionE.insertBefore(scriptE, actionE.getFirstChild()); 179 List <String > targetNames = new ArrayList <String >(); 180 for (Element targetE : Util.findSubElements(actionE)) { 181 if (!targetE.getLocalName().equals("target")) { continue; 183 } 184 targetNames.add(Util.findText(targetE)); 185 } 186 if (targetNames.isEmpty()) { 187 targetNames.add(null); 188 } 189 String scriptPath = evaluator.evaluate(script); 190 for (String target : targetNames) { 191 if (targetsCreated.add(target)) { 192 createOverride(projectE, target, scriptPath); 193 } 194 } 195 } 196 197 private static void createOverride(Element projectE, String target, String script) { 198 Element targetE = projectE.getOwnerDocument().createElement("target"); if (target != null) { 200 targetE.setAttribute("name", target); } 202 String depends; 203 if (target != null && target.indexOf("debug") != -1) { 204 depends = "-jdk-init,-jdk-presetdef-nbjpdastart"; } else { 206 depends = "-jdk-init"; } 209 targetE.setAttribute("depends", depends); Element antE = projectE.getOwnerDocument().createElement("ant"); if (target != null) { 212 antE.setAttribute("target", target); } 214 if (!script.equals("build.xml")) { antE.setAttribute("antfile", script); } 217 antE.setAttribute("inheritall", "false"); targetE.appendChild(antE); 219 projectE.appendChild(targetE); 220 } 221 222 private void writeXML(Document doc, String path) throws IOException { 223 FileObject fo = FileUtil.createData(project.getProjectDirectory(), path); 224 FileLock lock = fo.lock(); 225 try { 226 OutputStream os = fo.getOutputStream(lock); 227 try { 228 XMLUtil.write(doc, os, "UTF-8"); } finally { 230 os.close(); 231 } 232 } finally { 233 lock.releaseLock(); 234 } 235 } 236 237 241 public JavaPlatform getSelectedPlatform() { 242 EditableProperties ep = helper.getProperties(NBJDK_PROPERTIES); 243 String plaf = ep.getProperty(NBJDK_ACTIVE); 244 if (plaf != null) { 245 for (JavaPlatform p : JavaPlatformManager.getDefault().getInstalledPlatforms()) { 246 if (plaf.equals(getPlatformID(p))) { 247 return p; 248 } 249 } 250 } 251 return null; 252 } 253 254 257 public void setSelectedPlatform(JavaPlatform jdk) throws IOException { 258 assert jdk != null; 259 initialize(); 260 EditableProperties ep = helper.getProperties(NBJDK_PROPERTIES); 261 ep.setProperty(NBJDK_ACTIVE, getPlatformID(jdk)); 262 helper.putProperties(NBJDK_PROPERTIES, ep); 263 ProjectManager.getDefault().saveProject(project); 264 } 265 266 private static String getPlatformID(JavaPlatform platform) { 267 String s = (String ) platform.getProperties().get("platform.ant.name"); if (s != null) { 269 return s; 270 } else { 271 return PLATFORM_ID_DEFAULT; 272 } 273 } 274 275 } 276 | Popular Tags |