Java Code Examples for org.openide.xml.XMLUtil#findElement()

The following examples show how to use org.openide.xml.XMLUtil#findElement() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: WebFreeFormActionProvider.java    From netbeans with Apache License 2.0 6 votes vote down vote up
private String findSourceFolders(String type) {
    StringBuffer result = new StringBuffer();
    Element data = Util.getPrimaryConfigurationData(helper);
    Element foldersEl = XMLUtil.findElement(data, "folders", Util.NAMESPACE); // NOI18N
    if (foldersEl != null) {
        for (Iterator i = XMLUtil.findSubElements(foldersEl).iterator(); i.hasNext();) {
            Element sourceFolderEl = (Element)i.next();
            Element typeEl = XMLUtil.findElement(sourceFolderEl , "type", Util.NAMESPACE); // NOI18N
            if (typeEl == null || !XMLUtil.findText(typeEl).equals(type))
                continue;
            Element locationEl = XMLUtil.findElement(sourceFolderEl , "location", Util.NAMESPACE); // NOI18N
            if (locationEl == null)
                continue;
            String location = XMLUtil.findText(locationEl);
            if (result.length() > 0)
                result.append(":"); // NOI18N
            result.append(location);
        }
    }
    return result.toString();
}
 
Example 2
Source File: FolderNodeFactory.java    From netbeans with Apache License 2.0 6 votes vote down vote up
private void updateKeys(boolean fromListener) {
    Element genldata = p.getPrimaryConfigurationData();
    Element viewEl = XMLUtil.findElement(genldata, "view", FreeformProjectType.NS_GENERAL); // NOI18N
    if (viewEl != null) {
        Element itemsEl = XMLUtil.findElement(viewEl, "items", FreeformProjectType.NS_GENERAL); // NOI18N
        keys = XMLUtil.findSubElements(itemsEl);
    } else {
        keys = Collections.<Element>emptyList();
    }
    if (fromListener && !synchronous) {
        // #50328, #58491 - post setKeys to different thread to prevent deadlocks
        RP.post(new Runnable() {
            public void run() {
                cs.fireChange();
            }
        });
    } else {
        cs.fireChange();
    }
}
 
Example 3
Source File: SourceLevelQueryImpl.java    From netbeans with Apache License 2.0 5 votes vote down vote up
/**
 * Get the source level indicated in a compilation unit (or null if none is indicated).
 */
private String getLevel(Element compilationUnitEl) {
    Element sourceLevelEl = XMLUtil.findElement(compilationUnitEl, "source-level", JavaProjectNature.NS_JAVA_LASTEST);
    if (sourceLevelEl != null) {
        return XMLUtil.findText(sourceLevelEl);
    } else {
        return null;
    }
}
 
Example 4
Source File: NbModuleProject.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public String getCodeNameBase() {
    Element config = getPrimaryConfigurationData();
    Element cnb = XMLUtil.findElement(config, "code-name-base", NbModuleProject.NAMESPACE_SHARED); // NOI18N
    if (cnb != null) {
        return XMLUtil.findText(cnb);
    } else {
        return null;
    }
}
 
Example 5
Source File: ProjectXMLManagerTest.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public void testNonstandardDependencies() throws Exception { // #180717
    NbModuleProject p = generateStandaloneModule("m");
    Element data = p.getPrimaryConfigurationData();
    Element mds = XMLUtil.findElement(data, ProjectXMLManager.MODULE_DEPENDENCIES, NbModuleProject.NAMESPACE_SHARED);
    Document doc = mds.getOwnerDocument();
    boolean[] Z2 = {false, true};
    int i = 0;
    for (boolean buildPrerequisite : Z2) {
        for (boolean compileDependency : Z2) {
            for (boolean runDependency : Z2) {
                Element md = (Element) mds.appendChild(doc.createElementNS(NbModuleProject.NAMESPACE_SHARED, ProjectXMLManager.DEPENDENCY));
                Element cnb = (Element) md.appendChild(doc.createElementNS(NbModuleProject.NAMESPACE_SHARED, ProjectXMLManager.CODE_NAME_BASE));
                cnb.appendChild(doc.createTextNode("dep" + i++));
                if (buildPrerequisite) {
                    md.appendChild(doc.createElementNS(NbModuleProject.NAMESPACE_SHARED, ProjectXMLManager.BUILD_PREREQUISITE));
                }
                if (compileDependency) {
                    md.appendChild(doc.createElementNS(NbModuleProject.NAMESPACE_SHARED, ProjectXMLManager.COMPILE_DEPENDENCY));
                }
                if (runDependency) {
                    md.appendChild(doc.createElementNS(NbModuleProject.NAMESPACE_SHARED, ProjectXMLManager.RUN_DEPENDENCY));
                }
            }
        }
    }
    String orig = elementToString(mds);
    p.putPrimaryConfigurationData(data);
    ProjectXMLManager pxm = new ProjectXMLManager(p);
    ModuleDependency extra = new ModuleDependency(new NonexistentModuleEntry(("other")), null, null, true, false);
    pxm.addDependency(extra);
    pxm.removeDependency("other");
    mds = XMLUtil.findElement(p.getPrimaryConfigurationData(), ProjectXMLManager.MODULE_DEPENDENCIES, NbModuleProject.NAMESPACE_SHARED);
    assertEquals(orig, elementToString(mds));
}
 
Example 6
Source File: AntBasedTestUtil.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private String getText(String elementName) {
    Element data = helper.getPrimaryConfigurationData(true);
    Element el = XMLUtil.findElement(data, elementName, "urn:test:shared");
    if (el != null) {
        String text = XMLUtil.findText(el);
        if (text != null) {
            return text;
        }
    }
    // Some kind of fallback here.
    return getProjectDirectory().getNameExt();
}
 
Example 7
Source File: JavaActionsTest.java    From netbeans with Apache License 2.0 5 votes vote down vote up
protected void setUp() throws Exception {
    super.setUp();
    prj = copyProject(simple);
    // Remove existing context-sensitive bindings to make a clean slate.
    Element data = prj.getPrimaryConfigurationData();
    Element ideActions = XMLUtil.findElement(data, "ide-actions", Util.NAMESPACE);
    assertNotNull(ideActions);
    Iterator<Element> actionsIt = XMLUtil.findSubElements(ideActions).iterator();
    while (actionsIt.hasNext()) {
        Element action = actionsIt.next();
        assertEquals("action", action.getLocalName());
        if (XMLUtil.findElement(action, "context", Util.NAMESPACE) != null) {
            ideActions.removeChild(action);
        }
    }
    prj.putPrimaryConfigurationData(data);
    ProjectManager.getDefault().saveProject(prj);
    AuxiliaryConfiguration origAux = prj.getLookup().lookup(AuxiliaryConfiguration.class);
    AuxiliaryConfiguration aux = new LookupProviderImpl.UpgradingAuxiliaryConfiguration(origAux);
    ja = new JavaActions(prj, prj.helper(), prj.evaluator(), aux);
    src = prj.getProjectDirectory().getFileObject("src");
    assertNotNull(src);
    myAppJava = src.getFileObject("org/foo/myapp/MyApp.java");
    assertNotNull(myAppJava);
    someFileJava = src.getFileObject("org/foo/myapp/SomeFile.java");
    assertNotNull(someFileJava);
    someResourceTxt = src.getFileObject("org/foo/myapp/some-resource.txt");
    assertNotNull(someResourceTxt);
    antsrc = prj.getProjectDirectory().getFileObject("antsrc");
    assertNotNull(antsrc);
    specialTaskJava = antsrc.getFileObject("org/foo/ant/SpecialTask.java");
    assertNotNull(specialTaskJava);
    buildProperties = prj.getProjectDirectory().getFileObject("build.properties");
    assertNotNull(buildProperties);
}
 
Example 8
Source File: JavaActions.java    From netbeans with Apache License 2.0 5 votes vote down vote up
/**
 * Try to find the source level corresponding to a source root.
 * @param sources a source root in the project (as a virtual Ant name)
 * @return the source level, or null if none was specified or there was no such source root
 */
String findSourceLevel(String sources) {
    Element compilationUnitEl = findCompilationUnit(sources);
    if (compilationUnitEl != null) {
        Element sourceLevel = XMLUtil.findElement(compilationUnitEl, "source-level", JavaProjectNature.NS_JAVA_LASTEST);
        if (sourceLevel != null) {
            return XMLUtil.findText(sourceLevel);
        }
    }
    return null;
}
 
Example 9
Source File: JdkConfiguration.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private void rebindAction(Element actionE, Element projectE, Set<String> targetsCreated) {
    Element scriptE = XMLUtil.findElement(actionE, "script", Util.NAMESPACE); // NOI18N
    String script;
    if (scriptE != null) {
        script = XMLUtil.findText(scriptE);
        actionE.removeChild(scriptE);
    } else {
        script = "build.xml"; // NOI18N
    }
    scriptE = actionE.getOwnerDocument().createElementNS(Util.NAMESPACE, "script"); // NOI18N
    scriptE.appendChild(actionE.getOwnerDocument().createTextNode(NBJDK_XML));
    actionE.insertBefore(scriptE, actionE.getFirstChild());
    List<String> targetNames = new ArrayList<String>();
    for (Element targetE : XMLUtil.findSubElements(actionE)) {
        if (!targetE.getLocalName().equals("target")) { // NOI18N
            continue;
        }
        targetNames.add(XMLUtil.findText(targetE));
    }
    if (targetNames.isEmpty()) {
        targetNames.add(null);
    }
    String scriptPath = evaluator.evaluate(script);
    for (String target : targetNames) {
        if (targetsCreated.add(target)) {
            createOverride(projectE, target, scriptPath);
        }
    }
}
 
Example 10
Source File: Classpaths.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private List<URL> createProcessorClasspath(Element compilationUnitEl) {
    final Element ap = XMLUtil.findElement(compilationUnitEl, AnnotationProcessingQueryImpl.EL_ANNOTATION_PROCESSING, JavaProjectNature.NS_JAVA_LASTEST);
    if (ap != null) {
        final Element path = XMLUtil.findElement(ap, AnnotationProcessingQueryImpl.EL_PROCESSOR_PATH, JavaProjectNature.NS_JAVA_LASTEST);
        if (path != null) {
            return createClasspath(path, new RemoveSources(helper, sfbqImpl));
        }
    }
    // None specified; assume it is the same as the compile classpath.
    return createCompileClasspath(compilationUnitEl);
}
 
Example 11
Source File: Classpaths.java    From netbeans with Apache License 2.0 4 votes vote down vote up
private boolean computeMatcher() {
    String incl = null;
    String excl = null;
    URI rootURI = URI.create(root.toExternalForm());
    // Annoying to duplicate logic from FreeformSources.
    // But using SourceGroup.contains is not an option since that requires FileObject creation.
    File rootFolder;
    try {
        rootFolder = Utilities.toFile(rootURI);
    } catch (IllegalArgumentException x) {
        Logger.getLogger(Classpaths.class.getName()).warning("Illegal source root: " + rootURI);
        rootFolder = null;
    }
    Element genldata = Util.getPrimaryConfigurationData(helper);
    Element foldersE = XMLUtil.findElement(genldata, "folders", Util.NAMESPACE); // NOI18N
    if (foldersE != null) {
        for (Element folderE : XMLUtil.findSubElements(foldersE)) {
            if (folderE.getLocalName().equals("source-folder")) {
                Element typeE = XMLUtil.findElement(folderE, "type", Util.NAMESPACE); // NOI18N
                if (typeE != null) {
                    String type = XMLUtil.findText(typeE);
                    if (type.equals(JavaProjectConstants.SOURCES_TYPE_JAVA)) {
                        Element locationE = XMLUtil.findElement(folderE, "location", Util.NAMESPACE); // NOI18N
                        String location = evaluator.evaluate(XMLUtil.findText(locationE));
                        if (location != null && helper.resolveFile(location).equals(rootFolder)) {
                            Element includesE = XMLUtil.findElement(folderE, "includes", Util.NAMESPACE); // NOI18N
                            if (includesE != null) {
                                incl = evaluator.evaluate(XMLUtil.findText(includesE));
                                if (incl != null && incl.matches("\\$\\{[^}]+\\}")) { // NOI18N
                                    // Clearly intended to mean "include everything".
                                    incl = null;
                                }
                            }
                            Element excludesE = XMLUtil.findElement(folderE, "excludes", Util.NAMESPACE); // NOI18N
                            if (excludesE != null) {
                                excl = evaluator.evaluate(XMLUtil.findText(excludesE));
                            }
                        }
                    }
                }
            }
        }
    }
    if (!Utilities.compareObjects(incl, includes) || !Utilities.compareObjects(excl, excludes)) {
        includes = incl;
        excludes = excl;
        matcher = new PathMatcher(incl, excl, rootFolder);
        return true;
    } else {
        if (matcher == null) {
            matcher = new PathMatcher(incl, excl, rootFolder);
        }
        return false;
    }
}
 
Example 12
Source File: SuiteBrandingModel.java    From netbeans with Apache License 2.0 4 votes vote down vote up
@Override protected String getSimpleName() {
    Element nameEl = XMLUtil.findElement(suiteProps.getProject().getHelper().getPrimaryConfigurationData(true), "name", SuiteProjectType.NAMESPACE_SHARED); // NOI18N
    String text = (nameEl != null) ? XMLUtil.findText(nameEl) : null;
    return (text != null) ? text : "???"; // NOI18N
}
 
Example 13
Source File: AntBasedProjectFactorySingleton.java    From netbeans with Apache License 2.0 4 votes vote down vote up
public @Override Project loadProject(FileObject projectDirectory, ProjectState state) throws IOException {
    if (FileUtil.toFile(projectDirectory) == null) {
        LOG.log(Level.FINER, "no disk dir {0}", projectDirectory);
        return null;
    }
    FileObject projectFile = projectDirectory.getFileObject(PROJECT_XML_PATH);
    if (projectFile == null) {
        LOG.log(Level.FINER, "no {0}/nbproject/project.xml", projectDirectory);
        return null;
    }
    //#54488: Added check for virtual
    if (!projectFile.isData() || projectFile.isVirtual()) {
        LOG.log(Level.FINE, "not concrete data file {0}/nbproject/project.xml", projectDirectory);
        return null;
    }
    File projectDiskFile = FileUtil.toFile(projectFile);
    //#63834: if projectFile exists and projectDiskFile does not, do nothing:
    if (projectDiskFile == null) {
        LOG.log(Level.FINE, "{0} not mappable to file", projectFile);
        return null;
    }
    Document projectXml = loadProjectXml(projectDiskFile);
    if (projectXml == null) {
        LOG.log(Level.FINE, "could not load {0}", projectDiskFile);
        return null;
    }
    Element typeEl = XMLUtil.findElement(projectXml.getDocumentElement(), "type", PROJECT_NS); // NOI18N
    if (typeEl == null) {
        LOG.log(Level.FINE, "no <type> in {0}", projectDiskFile);
        return null;
    }
    String type = XMLUtil.findText(typeEl);
    if (type == null) {
        LOG.log(Level.FINE, "no <type> text in {0}", projectDiskFile);
        return null;
    }
    AntBasedProjectType provider = findAntBasedProjectType(type);
    if (provider == null) {
        LOG.log(Level.FINE, "no provider for {0}", type);
        return null;
    }
    AntProjectHelper helper = HELPER_CALLBACK.createHelper(projectDirectory, projectXml, state, provider);
    Project project = provider.createProject(helper);
    synchronized (helper2Project) {
        project2Helper.put(project, new WeakReference<AntProjectHelper>(helper));
        helper2Project.put(helper, new WeakReference<Project>(project));
    }
    synchronized (AntBasedProjectFactorySingleton.class) {
        List<Reference<AntProjectHelper>> l = type2Projects.get(provider);
        if (l == null) {
            type2Projects.put(provider, l = new ArrayList<Reference<AntProjectHelper>>());
        }
        l.add(new WeakReference<AntProjectHelper>(helper));
    }
    return project;
}
 
Example 14
Source File: JavaProjectGenerator.java    From netbeans with Apache License 2.0 4 votes vote down vote up
/**
 * Read Java compilation units from the project.
 * @param helper AntProjectHelper instance
 * @param aux AuxiliaryConfiguration instance
 * @return list of JavaCompilationUnit instances; never null;
 */
public static List<JavaCompilationUnit> getJavaCompilationUnits(
        AntProjectHelper helper, AuxiliaryConfiguration aux) {
    //assert ProjectManager.mutex().isReadAccess() || ProjectManager.mutex().isWriteAccess();
    List<JavaCompilationUnit> list = new ArrayList<JavaCompilationUnit>();
    final Element data = getJavaCompilationUnits(aux);
    if (data == null) {
        return list;
    }
    for (Element cuEl : XMLUtil.findSubElements(data)) {
        JavaCompilationUnit cu = new JavaCompilationUnit();
        List<String> outputs = new ArrayList<String>();
        List<String> javadoc = new ArrayList<String>();
        List<JavaCompilationUnit.CP> cps = new ArrayList<JavaCompilationUnit.CP>();
        List<String> packageRoots = new ArrayList<String>();
        for (Element el : XMLUtil.findSubElements(cuEl)) {
            if (el.getLocalName().equals("package-root")) { // NOI18N
                packageRoots.add(XMLUtil.findText(el));
                continue;
            }
            if (el.getLocalName().equals("classpath")) { // NOI18N
                JavaCompilationUnit.CP cp = new JavaCompilationUnit.CP();
                cp.classpath = XMLUtil.findText(el);
                cp.mode = el.getAttribute("mode"); // NOI18N
                if (cp.mode != null && cp.classpath != null) {
                    cps.add(cp);
                }
                continue;
            }
            if (el.getLocalName().equals("built-to")) { // NOI18N
                outputs.add(XMLUtil.findText(el));
                continue;
            }
            if (el.getLocalName().equals("javadoc-built-to")) { // NOI18N
                javadoc.add(XMLUtil.findText(el));
                continue;
            }
            if (el.getLocalName().equals("source-level")) { // NOI18N
                cu.sourceLevel = XMLUtil.findText(el);
                continue;
            }
            if (el.getLocalName().equals("unit-tests")) { // NOI18N
                cu.isTests = true;
                continue;
            }
            if ("annotation-processing".equals(el.getLocalName())&&         //NOI18N
                JavaProjectNature.namespaceAtLeast(el.getNamespaceURI(), JavaProjectNature.NS_JAVA_3)) {
                cu.annotationPorocessing = new JavaCompilationUnit.AnnotationProcessing();
                cu.annotationPorocessing.trigger = EnumSet.<AnnotationProcessingQuery.Trigger>noneOf(AnnotationProcessingQuery.Trigger.class);
                cu.annotationPorocessing.processors = new ArrayList<String>();
                cu.annotationPorocessing.processorParams = new LinkedHashMap<String, String>();
                for (Element apEl : XMLUtil.findSubElements(el)) {
                    final String localName = apEl.getLocalName();
                    if ("scan-trigger".equals(localName)) { //NOI18N
                        cu.annotationPorocessing.trigger.add(AnnotationProcessingQuery.Trigger.ON_SCAN);
                    } else if ("editor-trigger".equals(localName)) {   //NOI18N
                        cu.annotationPorocessing.trigger.add(AnnotationProcessingQuery.Trigger.IN_EDITOR);
                    } else if ("source-output".equals(localName)) {
                        cu.annotationPorocessing.sourceOutput = XMLUtil.findText(apEl);
                    } else if ("processor-path".equals(localName)) {    //NOI18N
                        cu.annotationPorocessing.processorPath = XMLUtil.findText(apEl);
                    } else if ("processor".equals(localName)) { //NOI18N
                        cu.annotationPorocessing.processors.add(XMLUtil.findText(apEl));
                    } else if ("processor-option".equals(localName)) {  //NOI18N
                        final Element keyEl = XMLUtil.findElement(apEl, "key", el.getNamespaceURI());    //NOI18N
                        final Element valueEl = XMLUtil.findElement(apEl, "value", el.getNamespaceURI());     //NOI18N
                        if (keyEl != null && valueEl != null) {
                            final String key = XMLUtil.findText(keyEl);
                            final String value = XMLUtil.findText(valueEl);
                            if (key != null) {
                                cu.annotationPorocessing.processorParams.put(key, value);
                            }
                        }
                    }
                }
            }
        }
        cu.output = outputs.size() > 0 ? outputs : null;
        cu.javadoc = javadoc.size() > 0 ? javadoc : null;
        cu.classpath = cps.size() > 0 ? cps: null;
        cu.packageRoots = packageRoots.size() > 0 ? packageRoots: null;
        list.add(cu);
    }
    return list;
}
 
Example 15
Source File: Actions.java    From netbeans with Apache License 2.0 4 votes vote down vote up
public void invokeAction(String command, Lookup context) throws IllegalArgumentException {
    if (COMMAND_DELETE.equals(command)) {
        DefaultProjectOperations.performDefaultDeleteOperation(project);
        return ;
    }
    if (COMMAND_COPY.equals(command)) {
        DefaultProjectOperations.performDefaultCopyOperation(project);
        return ;
    }
    if (COMMAND_RENAME.equals(command)) {
        DefaultProjectOperations.performDefaultRenameOperation(project, null);
        return ;
    }
    if (COMMAND_MOVE.equals(command)) {
        DefaultProjectOperations.performDefaultMoveOperation(project);
        return ;
    }
    
    Element genldata = project.getPrimaryConfigurationData();
    Element actionsEl = XMLUtil.findElement(genldata, "ide-actions", FreeformProjectType.NS_GENERAL); // NOI18N
    if (actionsEl == null) {
        throw new IllegalArgumentException("No commands supported"); // NOI18N
    }
    boolean foundAction = false;
    for (Element actionEl : XMLUtil.findSubElements(actionsEl)) {
        if (actionEl.getAttribute("name").equals(command)) { // NOI18N
            foundAction = true;
            runConfiguredAction(command, project, actionEl, context);
        }
    }
    if (!foundAction) {
        if (COMMON_NON_IDE_GLOBAL_ACTIONS.contains(command)) {
            // #46886: try to bind it.
            if (addGlobalBinding(command)) {
                // If bound, run it immediately.
                invokeAction(command, context);
            }
        } else {
            throw new IllegalArgumentException("Unrecognized command: " + command); // NOI18N
        }
    }
}
 
Example 16
Source File: JavaActionsTest.java    From netbeans with Apache License 2.0 4 votes vote down vote up
public void testEnsureImports() throws Exception {
    // Start with the simple case:
    Element root = XMLUtil.createDocument("project", null, null, null).getDocumentElement();
    ja.ensureImports(root, "build.xml");
    String expectedXml =
        "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
        "<project basedir=\"..\">\n" +
        "    <import file=\"../build.xml\"/>\n" +
        "</project>\n";
    assertEquals("Correct code generated", expectedXml, xmlToString(root));
    ja.ensureImports(root, "build.xml");
    assertEquals("Idempotent", expectedXml, xmlToString(root));
    // Test strange locations too. Make a script somewhere different.
    File testdir = getWorkDir();
    File subtestdir = new File(testdir, "sub");
    subtestdir.mkdir();
    File script = new File(subtestdir, "external.xml");
    Document doc = XMLUtil.createDocument("project", null, null, null);
    doc.getDocumentElement().setAttribute("basedir", "..");
    OutputStream os = new FileOutputStream(script);
    try {
        XMLUtil.write(doc, os, "UTF-8");
    } finally {
        os.close();
    }
    root = XMLUtil.createDocument("project", null, null, null).getDocumentElement();
    String scriptPath = script.getAbsolutePath();
    ja.ensureImports(root, scriptPath);
    expectedXml =
        "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
        "<project basedir=\"" + testdir.getAbsolutePath() + "\">\n" +
        "    <import file=\"" + scriptPath + "\"/>\n" +
        "</project>\n";
    assertEquals("Correct code generated for external script", expectedXml, xmlToString(root));
    // And also with locations defined as special properties in various ways...
    Element data = prj.getPrimaryConfigurationData();
    Element properties = XMLUtil.findElement(data, "properties", Util.NAMESPACE);
    assertNotNull(properties);
    Element property = data.getOwnerDocument().createElementNS(Util.NAMESPACE, "property");
    property.setAttribute("name", "external.xml");
    property.appendChild(data.getOwnerDocument().createTextNode(scriptPath));
    properties.appendChild(property);
    property = data.getOwnerDocument().createElementNS(Util.NAMESPACE, "property");
    property.setAttribute("name", "subtestdir");
    property.appendChild(data.getOwnerDocument().createTextNode(subtestdir.getAbsolutePath()));
    properties.appendChild(property);
    property = data.getOwnerDocument().createElementNS(Util.NAMESPACE, "property");
    property.setAttribute("name", "testdir");
    property.appendChild(data.getOwnerDocument().createTextNode(testdir.getAbsolutePath()));
    properties.appendChild(property);
    prj.putPrimaryConfigurationData(data);
    ProjectManager.getDefault().saveProject(prj); // ease of debugging
    root = XMLUtil.createDocument("project", null, null, null).getDocumentElement();
    ja.ensureImports(root, "${external.xml}");
    expectedXml =
        "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
        "<project basedir=\"" + testdir.getAbsolutePath() + "\">\n" +
        "    <import file=\"" + scriptPath +  "\"/>\n" +
        "</project>\n";
    assertEquals("Correct code generated for ${external.xml}", expectedXml, xmlToString(root));
    root = XMLUtil.createDocument("project", null, null, null).getDocumentElement();
    ja.ensureImports(root, "${subtestdir}" + File.separator + "external.xml");
    expectedXml =
        "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
        "<project basedir=\"" + testdir.getAbsolutePath() + "\">\n" +
        "    <import file=\"" + scriptPath +  "\"/>\n" +
        "</project>\n";
    assertEquals("Correct code generated for ${subtestdir}/external.xml", expectedXml, xmlToString(root));
    root = XMLUtil.createDocument("project", null, null, null).getDocumentElement();
    ja.ensureImports(root, "${testdir}" + File.separator + "sub" +File.separator + "external.xml");
    expectedXml =
        "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
        "<project basedir=\"" + testdir.getAbsolutePath() + "\">\n" +
        "    <import file=\"" + scriptPath +  "\"/>\n" +
        "</project>\n";
    assertEquals("Correct code generated for ${testdir}/sub/external.xml", expectedXml, xmlToString(root));
    // XXX try also <import file="somewhere-relative/build.xml"/>
}
 
Example 17
Source File: WebFreeFormActionProvider.java    From netbeans with Apache License 2.0 4 votes vote down vote up
/**
 * Add an action binding to project.xml.
 * If there is no required context, the action is also added to the context menu of the project node.
 * @param command the command name
 * @param scriptPath the path to the generated script
 * @param target the name of the target (in scriptPath)
 * @param propertyName a property name to hold the selection (or null for no context, in which case remainder should be null)
 * @param dir the raw text to use for the directory name
 * @param pattern the regular expression to match, or null
 * @param format the format to use
 * @param separator the separator to use for multiple files, or null for single file only
 */
void addBinding(String command, String scriptPath, String target, String propertyName, String dir, String pattern, String format, String separator) throws IOException {
    // XXX cannot use FreeformProjectGenerator since that is currently not a public support SPI from ant/freeform
    // XXX should this try to find an existing binding? probably not, since it is assumed that if there was one, we would never get here to begin with
    Element data = Util.getPrimaryConfigurationData(helper);
    Element ideActions = XMLUtil.findElement(data, "ide-actions", Util.NAMESPACE); // NOI18N
    if (ideActions == null) {
        // Probably won't happen, since generator produces it always.
        // Not trivial to just add it now, since order is significant in the schema. (FPG deals with these things.)
        return;
    }
    Document doc = data.getOwnerDocument();
    Element action = doc.createElementNS(Util.NAMESPACE, "action"); // NOI18N
    action.setAttribute("name", command); // NOI18N
    Element script = doc.createElementNS(Util.NAMESPACE, "script"); // NOI18N
    script.appendChild(doc.createTextNode(scriptPath));
    action.appendChild(script);
    Element targetEl = doc.createElementNS(Util.NAMESPACE, "target"); // NOI18N
    targetEl.appendChild(doc.createTextNode(target));
    action.appendChild(targetEl);
    ideActions.appendChild(action);
    
    if (propertyName != null) {
        Element context = doc.createElementNS(Util.NAMESPACE, "context"); // NOI18N
        Element property = doc.createElementNS(Util.NAMESPACE, "property"); // NOI18N
        property.appendChild(doc.createTextNode(propertyName));
        context.appendChild(property);
        Element folder = doc.createElementNS(Util.NAMESPACE, "folder"); // NOI18N
        folder.appendChild(doc.createTextNode(dir));
        context.appendChild(folder);
        if (pattern != null) {
            Element patternEl = doc.createElementNS(Util.NAMESPACE, "pattern"); // NOI18N
            patternEl.appendChild(doc.createTextNode(pattern));
            context.appendChild(patternEl);
        }
        Element formatEl = doc.createElementNS(Util.NAMESPACE, "format"); // NOI18N
        formatEl.appendChild(doc.createTextNode(format));
        context.appendChild(formatEl);
        Element arity = doc.createElementNS(Util.NAMESPACE, "arity"); // NOI18N
        if (separator != null) {
            Element separatorEl = doc.createElementNS(Util.NAMESPACE, "separated-files"); // NOI18N
            separatorEl.appendChild(doc.createTextNode(separator));
            arity.appendChild(separatorEl);
        } else {
            arity.appendChild(doc.createElementNS(Util.NAMESPACE, "one-file-only")); // NOI18N
        }
        context.appendChild(arity);
        action.appendChild(context);
    } else {
        // Add a context menu item, since it applies to the project as a whole.
        // Assume there is already a <context-menu> defined, which is quite likely.
        Element view = XMLUtil.findElement(data, "view", Util.NAMESPACE); // NOI18N
        if (view != null) {
            Element contextMenu = XMLUtil.findElement(view, "context-menu", Util.NAMESPACE); // NOI18N
            if (contextMenu != null) {
                Element ideAction = doc.createElementNS(Util.NAMESPACE, "ide-action"); // NOI18N
                ideAction.setAttribute("name", command); // NOI18N
                contextMenu.appendChild(ideAction);
            }
        }
    }

    Util.putPrimaryConfigurationData(helper, data);
    ProjectManager.getDefault().saveProject(project);
}
 
Example 18
Source File: ProjectXMLManager.java    From netbeans with Apache License 2.0 4 votes vote down vote up
private static Element findElement(Element parentEl, String elementName) {
    return XMLUtil.findElement(parentEl, elementName, NbModuleProject.NAMESPACE_SHARED);
}
 
Example 19
Source File: AnnotationProcessingQueryImpl.java    From netbeans with Apache License 2.0 4 votes vote down vote up
private Element findAP(final Element cu) {
    return cu == null ? null : XMLUtil.findElement(cu, AnnotationProcessingQueryImpl.EL_ANNOTATION_PROCESSING, JavaProjectNature.NS_JAVA_LASTEST);
}
 
Example 20
Source File: AntProjectHelperTest.java    From netbeans with Apache License 2.0 4 votes vote down vote up
@RandomlyFails // in last assertion in NB-Core-Build #2426 & #2431; maybe due to #2bbacd6640a5?
public void test68872() throws Exception {
    AuxiliaryConfiguration aux = p.getLookup().lookup(AuxiliaryConfiguration.class);
    assertNotNull("AuxiliaryConfiguration present", aux);

    Element data = aux.getConfigurationFragment("data", "urn:test:private-aux", false);
    Element stuff = XMLUtil.findElement(data, "aux-private-stuff", "urn:test:private-aux");
    assertNotNull("found <aux-private-stuff/>", stuff);
    // Check write of private data.
    stuff.setAttribute("attr", "val");
    aux.putConfigurationFragment(data, false);
    assertTrue("now project is modified", pm.isModified(p));
    
    FileObject privateXMLFO = p.getProjectDirectory().getFileObject("nbproject/private/private.xml");
    
    assertNotNull(privateXMLFO);
    
    File privateXML = FileUtil.toFile(privateXMLFO);
    
    privateXML.delete();
    
    privateXMLFO.refresh();
    privateXMLFO.getParent().refresh();
    
    pm.saveProject(p);
    
    //the file should be renewed with new data:
    assertTrue(privateXML.exists());
    
    //check the data are written:
    Document doc = AntBasedTestUtil.slurpXml(h, AntProjectHelper.PRIVATE_XML_PATH);
    Element config = doc.getDocumentElement();
    data = XMLUtil.findElement(config, "data", "urn:test:private-aux");
    assertNotNull("<data> still exists", data);
    stuff = XMLUtil.findElement(data, "aux-private-stuff", "urn:test:private-aux");
    assertNotNull("still have <aux-private-stuff/>", stuff);
    assertEquals("attr written correctly", "val", stuff.getAttribute("attr"));
    
    //check that on-disk changes are not ignored if the project is saved:
    privateXML.delete();
    
    privateXMLFO.refresh();
    privateXMLFO.getParent().refresh();
    
    assertNull(aux.getConfigurationFragment("data", "urn:test:private-aux", false));
}