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

The following examples show how to use org.openide.xml.XMLUtil#findSubElements() . 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: ProjectXMLManager.java    From netbeans with Apache License 2.0 6 votes vote down vote up
public void editDependency(ModuleDependency origDep, ModuleDependency newDep) {
    Element _confData = getConfData();
    Element moduleDependencies = findModuleDependencies(_confData);
    List<Element> currentDeps = XMLUtil.findSubElements(moduleDependencies);
    for (Iterator<Element> it = currentDeps.iterator(); it.hasNext();) {
        Element dep = it.next();
        Element cnbEl = findElement(dep, ProjectXMLManager.CODE_NAME_BASE);
        String _cnb = XMLUtil.findText(cnbEl);
        if (_cnb.equals(origDep.getModuleEntry().getCodeNameBase())) {
            moduleDependencies.removeChild(dep);
            Element nextDep = it.hasNext() ? it.next() : null;
            createModuleDependencyElement(moduleDependencies, newDep, nextDep);
            break;
        }
    }
    project.putPrimaryConfigurationData(_confData);
    directDeps = null;
}
 
Example 2
Source File: ProjectXMLManager.java    From netbeans with Apache License 2.0 6 votes vote down vote up
/**
 * Use this for removing more than one dependencies. It's faster then
 * iterating and using <code>removeDependency</code> for every entry.
 */
public void removeDependenciesByCNB(Collection<String> cnbsToDelete) {
    Element _confData = getConfData();
    Element moduleDependencies = findModuleDependencies(_confData);
    for (Element dep : XMLUtil.findSubElements(moduleDependencies)) {
        Element cnbEl = findElement(dep, ProjectXMLManager.CODE_NAME_BASE);
        String _cnb = XMLUtil.findText(cnbEl);
        if (cnbsToDelete.remove(_cnb)) {
            moduleDependencies.removeChild(dep);
        }
        if (cnbsToDelete.size() == 0) {
            break; // everything was deleted
        }
    }
    if (cnbsToDelete.size() != 0) {
        Util.err.log(ErrorManager.WARNING,
                "Some modules weren't deleted: " + cnbsToDelete); // NOI18N
    }
    project.putPrimaryConfigurationData(_confData);
    directDeps = null;
}
 
Example 3
Source File: Actions.java    From netbeans with Apache License 2.0 6 votes vote down vote up
public String[] getSupportedActions() {
    final Element genldata = project.getPrimaryConfigurationData();
    final Element actionsEl = XMLUtil.findElement(genldata, "ide-actions", FreeformProjectType.NS_GENERAL); // NOI18N
    // Use a set, not a list, since when using context you can define one action several times:
    final Set<String> names = new LinkedHashSet<String>();
    if (actionsEl != null) {                            
        for (Element actionEl : XMLUtil.findSubElements(actionsEl)) {
            names.add(actionEl.getAttribute("name")); // NOI18N
        }
        // #46886: also always enable all common global actions, in case they should be selected:
        names.addAll(COMMON_NON_IDE_GLOBAL_ACTIONS);
    }
    names.add(COMMAND_RENAME);
    names.add(COMMAND_MOVE);
    names.add(COMMAND_COPY);
    names.add(COMMAND_DELETE);
    return names.toArray(new String[names.size()]);
}
 
Example 4
Source File: SourceForBinaryQueryImpl.java    From netbeans with Apache License 2.0 6 votes vote down vote up
/**
 * Find a list of URLs of binaries which will be produced from a compilation unit.
 * Result may be empty.
 */
private List<URL> findBinaries(Element compilationUnitEl) {
    List<URL> binaries = new ArrayList<URL>();
    for (Element builtToEl : XMLUtil.findSubElements(compilationUnitEl)) {
        if (!builtToEl.getLocalName().equals("built-to")) { // NOI18N
            continue;
        }
        String text = XMLUtil.findText(builtToEl);
        String textEval = evaluator.evaluate(text);
        if (textEval == null) {
            continue;
        }
        File buildProduct = helper.resolveFile(textEval);
        URL buildProductURL = FileUtil.urlForArchiveOrDir(buildProduct);
        binaries.add(buildProductURL);
    }
    return binaries;
}
 
Example 5
Source File: JavaActions.java    From netbeans with Apache License 2.0 5 votes vote down vote up
/**
 * Try to find the compile-time classpath corresponding to a source root.
 * @param sources a source root in the project (as a virtual Ant name)
 * @return the classpath (in Ant form), or null if none was specified or there was no such source root
 */
String findCUClasspath(String sources, String moud) {
    Element compilationUnitEl = findCompilationUnit(sources);
    if (compilationUnitEl != null) {
        for (Element classpath : XMLUtil.findSubElements(compilationUnitEl)) {
            if (classpath.getLocalName().equals("classpath")) { // NOI18N
                String mode = classpath.getAttribute("mode"); // NOI18N
                if (mode.equals(moud)) {
                    return XMLUtil.findText(classpath);
                }
            }
        }
    }
    return null;
}
 
Example 6
Source File: ReferenceHelper.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private static RawReference[] getRawReferences(Element references) throws IllegalArgumentException {
    List<Element> subEls = XMLUtil.findSubElements(references);
    List<RawReference> refs = new ArrayList<RawReference>(subEls.size());
    for (Element subEl : subEls) {
        refs.add(RawReference.create(subEl));
    }
    return refs.toArray(new RawReference[refs.size()]);
}
 
Example 7
Source File: ReferenceHelper.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private static RawReference getRawReference(String foreignProjectName, String id, Element references, boolean escaped) throws IllegalArgumentException {
    for (Element subEl : XMLUtil.findSubElements(references)) {
        RawReference ref = RawReference.create(subEl);
        String refID = ref.getID();
        String refName = ref.getForeignProjectName();
        if (escaped) {
            refID = getUsableReferenceID(ref.getID());
            refName = getUsableReferenceID(ref.getForeignProjectName());
        }
        if (refName.equals(foreignProjectName) && refID.equals(id)) {
            return ref;
        }
    }
    return null;
}
 
Example 8
Source File: AnnotationProcessingQueryImpl.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public @Override Map<? extends String, ? extends String> processorOptions() {
    Map<String,String> result = optionsCache;
    if (result != null) {
        return result;
    }
    result = new HashMap<String, String>();
    if (ap != null) {
        for (Element e : XMLUtil.findSubElements(ap)) {
            if (e.getLocalName().equals(EL_PROCESSOR_OPTION)) {
                final Element keyElement = XMLUtil.findElement(e, "key", JavaProjectNature.NS_JAVA_LASTEST);  //NOI18N
                final Element valueElement = XMLUtil.findElement(e, "value", JavaProjectNature.NS_JAVA_LASTEST);  //NOI18N
                if (keyElement == null || valueElement == null) {
                    continue;
                }
                final String key = XMLUtil.findText(keyElement);
                final String value = XMLUtil.findText(valueElement);
                result.put(
                    key,
                    value == null ? null : eval.evaluate(value));
            }
        }
    }
    synchronized (LCK) {
        if (optionsCache == null) {
            optionsCache = result;
        }
    }
    return result;
}
 
Example 9
Source File: ProjectXMLManager.java    From netbeans with Apache License 2.0 5 votes vote down vote up
/**
 * Removes test dependency under type <code>testType</code>, indentified
 * by <code>cnbToRemove</code>. Does not remove whole test type even if
 * removed test dependency was the last one.
 */
public boolean removeTestDependency(String testType, String cnbToRemove) {
    boolean wasRemoved = false;
    Element _confData = getConfData();
    Element testModuleDependenciesEl = findTestDependenciesElement(_confData);
    Element testTypeRemoveEl = null;
    for (Element type : XMLUtil.findSubElements(testModuleDependenciesEl)) {
        Element nameEl = findElement(type, TEST_TYPE_NAME);
        String nameOfType = XMLUtil.findText(nameEl);
        if (testType.equals(nameOfType)) {
            testTypeRemoveEl = type;
        }
    }
    //found such a test type
    if (testTypeRemoveEl != null) {
        for (Element el : XMLUtil.findSubElements(testTypeRemoveEl)) {
            Element cnbEl = findElement(el, TEST_DEPENDENCY_CNB);
            if (cnbEl == null) {
                continue;   //name node, continue
            }
            String _cnb = XMLUtil.findText(cnbEl);
            if (cnbToRemove.equals(_cnb)) {
                // found test dependency with desired CNB
                testTypeRemoveEl.removeChild(el);
                wasRemoved = true;
                project.putPrimaryConfigurationData(_confData);
            }
        }
    }
    return wasRemoved;
}
 
Example 10
Source File: ProjectXMLManager.java    From netbeans with Apache License 2.0 5 votes vote down vote up
/** Utility method for finding friend. */
public static String[] findFriends(final Element confData) {
    Element friendsEl = findFriendsElement(confData);
    if (friendsEl != null) {
        Set<String> friends = new TreeSet<String>();
        for (Element friendEl : XMLUtil.findSubElements(friendsEl)) {
            if (FRIEND.equals(friendEl.getTagName())) {
                friends.add(XMLUtil.findText(friendEl));
            }
        }
        return friends.toArray(new String[friends.size()]);
    }
    return null;
}
 
Example 11
Source File: JavaActions.java    From netbeans with Apache License 2.0 5 votes vote down vote up
/**
 * Try to find the compilation unit containing a source root.
 * @param sources a source root in the project (as a virtual Ant name)
 * @return the compilation unit owning it, or null if not found
 */
private Element findCompilationUnit(String sources) {
    for (Element compilationUnitEl : compilationUnits()) {
        for (Element packageRoot : XMLUtil.findSubElements(compilationUnitEl)) {
            if (packageRoot.getLocalName().equals("package-root")) { // NOI18N
                if (XMLUtil.findText(packageRoot).equals(sources)) {
                    return compilationUnitEl;
                }
            }
        }
    }
    return null;
}
 
Example 12
Source File: Classpaths.java    From netbeans with Apache License 2.0 5 votes vote down vote up
static List<String> findPackageRootNames(Element compilationUnitEl) {
    List<String> names = new ArrayList<String>();
    for (Element e : XMLUtil.findSubElements(compilationUnitEl)) {
        if (!e.getLocalName().equals("package-root")) { // NOI18N
            continue;
        }
        String location = XMLUtil.findText(e);
        names.add(location);
    }
    return names;
}
 
Example 13
Source File: ReferenceHelperTest.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private void assertReferenceXmlFragment(String namespace, String... scriptLocations) {
    Element refs = p.getLookup().lookup(AuxiliaryConfiguration.class).getConfigurationFragment(ReferenceHelper.REFS_NAME, namespace, true);
    assertNotNull(refs);
    List<String> actualScriptLocations = new ArrayList<String>();
    for (Element ref : XMLUtil.findSubElements(refs)) {
        Element script = XMLUtil.findElement(ref, "script", namespace);
        actualScriptLocations.add(XMLUtil.findText(script));
    }
    assertEquals(Arrays.asList(scriptLocations), actualScriptLocations);
}
 
Example 14
Source File: AntBasedProjectFactorySingletonTest.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private static String namesOfChildren(Element e) {
    StringBuilder b = new StringBuilder();
    for (Element kid : XMLUtil.findSubElements(e)) {
        if (b.length() > 0) {
            b.append(' ');
        }
        b.append(kid.getLocalName());
        String ns = kid.getNamespaceURI();
        b.append(ns.charAt(ns.length() - 1));
    }
    return b.toString();
}
 
Example 15
Source File: JavaProjectGenerator.java    From netbeans with Apache License 2.0 4 votes vote down vote up
/**
 * Read target mappings from project.
 * @param helper AntProjectHelper instance
 * @return list of TargetMapping instances
 */
public static List<TargetMapping> getTargetMappings(AntProjectHelper helper) {
    //assert ProjectManager.mutex().isReadAccess() || ProjectManager.mutex().isWriteAccess();
    List<TargetMapping> list = new ArrayList<TargetMapping>();
    Element genldata = Util.getPrimaryConfigurationData(helper);
    Element actionsEl = XMLUtil.findElement(genldata, "ide-actions", Util.NAMESPACE); // NOI18N
    if (actionsEl == null) {
        return list;
    }
    for (Element actionEl : XMLUtil.findSubElements(actionsEl)) {
        TargetMapping tm = new TargetMapping();
        tm.name = actionEl.getAttribute("name"); // NOI18N
        List<String> targetNames = new ArrayList<String>();
        EditableProperties props = new EditableProperties(false);
        for (Element subEl : XMLUtil.findSubElements(actionEl)) {
            if (subEl.getLocalName().equals("target")) { // NOI18N
                targetNames.add(XMLUtil.findText(subEl));
                continue;
            }
            if (subEl.getLocalName().equals("script")) { // NOI18N
                tm.script = XMLUtil.findText(subEl);
                continue;
            }
            if (subEl.getLocalName().equals("context")) { // NOI18N
                TargetMapping.Context ctx = new TargetMapping.Context();
                for (Element contextSubEl : XMLUtil.findSubElements(subEl)) {
                    if (contextSubEl.getLocalName().equals("property")) { // NOI18N
                        ctx.property = XMLUtil.findText(contextSubEl);
                        continue;
                    }
                    if (contextSubEl.getLocalName().equals("format")) { // NOI18N
                        ctx.format = XMLUtil.findText(contextSubEl);
                        continue;
                    }
                    if (contextSubEl.getLocalName().equals("folder")) { // NOI18N
                        ctx.folder = XMLUtil.findText(contextSubEl);
                        continue;
                    }
                    if (contextSubEl.getLocalName().equals("pattern")) { // NOI18N
                        ctx.pattern = XMLUtil.findText(contextSubEl);
                        continue;
                    }
                    if (contextSubEl.getLocalName().equals("arity")) { // NOI18N
                        Element sepFilesEl = XMLUtil.findElement(contextSubEl, "separated-files", Util.NAMESPACE); // NOI18N
                        if (sepFilesEl != null) {
                            ctx.separator = XMLUtil.findText(sepFilesEl);
                        }
                        continue;
                    }
                }
                tm.context = ctx;
            }
            if (subEl.getLocalName().equals("property")) { // NOI18N
                readProperty(subEl, props);
                continue;
            }
        }
        tm.targets = targetNames;
        if (props.keySet().size() > 0) {
            tm.properties = props;
        }
        list.add(tm);
    }
    return list;
}
 
Example 16
Source File: FreeformProjectGeneratorTest.java    From netbeans with Apache License 2.0 4 votes vote down vote up
public void testRawContextMenuActions() throws Exception {
        AntProjectHelper helper = createEmptyProject("proj", "proj");
        FileObject base = helper.getProjectDirectory();
        Project p = ProjectManager.getDefault().findProject(base);
        assertNotNull("Project was not created", p);
        assertEquals("Project folder is incorrect", base, p.getProjectDirectory());
        
        // check that all data are correctly persisted
        
        List<FreeformProjectGenerator.TargetMapping> mappings = new ArrayList<FreeformProjectGenerator.TargetMapping>();
        FreeformProjectGenerator.TargetMapping tm = new FreeformProjectGenerator.TargetMapping();
        tm.name = "first-targetName";
        mappings.add(tm);
        tm = new FreeformProjectGenerator.TargetMapping();
        tm.name = "second-targetName";
        mappings.add(tm);
        tm = new FreeformProjectGenerator.TargetMapping();
        tm.name = "context-sensitive";
        tm.context = new FreeformProjectGenerator.TargetMapping.Context();
        mappings.add(tm);
        FreeformProjectGenerator.putContextMenuAction(helper, mappings);
//        ProjectManager.getDefault().saveAllProjects();
        Element el = Util.getPrimaryConfigurationData(helper);
        el = XMLUtil.findElement(el, "view", FreeformProjectType.NS_GENERAL);
        assertNotNull("Target mapping were not saved correctly",  el);
        el = XMLUtil.findElement(el, "context-menu", FreeformProjectType.NS_GENERAL);
        assertNotNull("Target mapping were not saved correctly",  el);
        List<Element> subElements = XMLUtil.findSubElements(el);
        assertEquals(2, subElements.size());
        assertElementArray(subElements, 
            new String[]{"ide-action", "ide-action"}, 
            new String[]{null, null},
            new String[]{"name", "name"}, 
            new String[]{"first-targetName", "second-targetName"}
            );
        ProjectManager.getDefault().saveAllProjects();
            
        // test updating
            
        tm = new FreeformProjectGenerator.TargetMapping();
        tm.name = "foo";
        mappings.add(tm);
        tm = new FreeformProjectGenerator.TargetMapping();
        tm.name = "bar";
        mappings.add(tm);
        FreeformProjectGenerator.putContextMenuAction(helper, mappings);
//        ProjectManager.getDefault().saveAllProjects();
        el = Util.getPrimaryConfigurationData(helper);
        el = XMLUtil.findElement(el, "view", FreeformProjectType.NS_GENERAL);
        assertNotNull("Target mapping were not saved correctly",  el);
        el = XMLUtil.findElement(el, "context-menu", FreeformProjectType.NS_GENERAL);
        assertNotNull("Target mapping were not saved correctly",  el);
        subElements = XMLUtil.findSubElements(el);
        assertEquals(4, subElements.size());
        assertElementArray(subElements, 
            new String[]{"ide-action", "ide-action", "ide-action", "ide-action"},
            new String[]{null, null, null, null},
            new String[]{"name", "name", "name", "name"}, 
            new String[]{"first-targetName", "second-targetName", "foo", "bar"}
            );
        ProjectManager.getDefault().saveAllProjects();
    }
 
Example 17
Source File: PluginIndexManager.java    From netbeans with Apache License 2.0 4 votes vote down vote up
private static Map<String,List<String>> parsePhases(String u, String packaging) throws Exception {
    Document doc = XMLUtil.parse(new InputSource(u), false, false, XMLUtil.defaultErrorHandler(), null);
    for (Element componentsEl : XMLUtil.findSubElements(doc.getDocumentElement())) {
        for (Element componentEl : XMLUtil.findSubElements(componentsEl)) {
            if (XMLUtil.findText(XMLUtil.findElement(componentEl, "role", null)).trim().equals("org.apache.maven.lifecycle.mapping.LifecycleMapping")
                    && XMLUtil.findText(XMLUtil.findElement(componentEl, "implementation", null)).trim().equals("org.apache.maven.lifecycle.mapping.DefaultLifecycleMapping")
                    && XMLUtil.findText(XMLUtil.findElement(componentEl, "role-hint", null)).trim().equals(packaging)) {
                for (Element configurationEl : XMLUtil.findSubElements(componentEl)) {
                    if (!configurationEl.getTagName().equals("configuration")) {
                        continue;
                    }
                    Element phases = XMLUtil.findElement(configurationEl, "phases", null);
                    if (phases == null) {
                        for (Element lifecyclesEl : XMLUtil.findSubElements(configurationEl)) {
                            if (!lifecyclesEl.getTagName().equals("lifecycles")) {
                                continue;
                            }
                            for (Element lifecycleEl : XMLUtil.findSubElements(lifecyclesEl)) {
                                if (XMLUtil.findText(XMLUtil.findElement(lifecycleEl, "id", null)).trim().equals("default")) {
                                    phases = XMLUtil.findElement(lifecycleEl, "phases", null);
                                    break;
                                }
                            }
                        }
                    }
                    if (phases != null) {
                        Map<String,List<String>> result = new LinkedHashMap<String,List<String>>();
                        for (Element phase : XMLUtil.findSubElements(phases)) {
                            List<String> plugins = new ArrayList<String>();
                            for (String plugin : XMLUtil.findText(phase).split(",")) {
                                String[] gavMojo = plugin.trim().split(":", 4);
                                plugins.add(gavMojo[0] + ':' + gavMojo[1] + ':' + (gavMojo.length == 4 ? gavMojo[3] : gavMojo[2])); // version is not used here
                            }
                            result.put(phase.getTagName(), plugins);
                        }
                        LOG.log(Level.FINE, "for {0} found in {1}: {2}", new Object[] {packaging, u, result});
                        return result;
                    }
                }
            }
        }
    }
    return null;
}
 
Example 18
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 19
Source File: JavaProjectGenerator.java    From netbeans with Apache License 2.0 4 votes vote down vote up
/**
 * Update source views of the project. 
 * This method should be called always after the putSourceFolders method
 * to keep views and folders in sync.
 * Project is left modified and you must save it explicitely.
 * @param helper AntProjectHelper instance
 * @param sources list of SourceFolder instances
 * @param style style of source views to update. 
 *    Can be null in which case all styles will be overriden.
 *    Useful for overriding just one style of source view.
 */
public static void putSourceViews(AntProjectHelper helper, List<SourceFolder> sources, String style) {
    //assert ProjectManager.mutex().isWriteAccess();
    Element data = Util.getPrimaryConfigurationData(helper);
    Document doc = data.getOwnerDocument();
    Element viewEl = XMLUtil.findElement(data, "view", Util.NAMESPACE); // NOI18N
    if (viewEl == null) {
        viewEl = doc.createElementNS(Util.NAMESPACE, "view"); // NOI18N
        XMLUtil.appendChildElement(data, viewEl, rootElementsOrder);
    }
    Element itemsEl = XMLUtil.findElement(viewEl, "items", Util.NAMESPACE); // NOI18N
    if (itemsEl == null) {
        itemsEl = doc.createElementNS(Util.NAMESPACE, "items"); // NOI18N
        XMLUtil.appendChildElement(viewEl, itemsEl, viewElementsOrder);
    }
    List<Element> sourceViews = XMLUtil.findSubElements(itemsEl);
    for (Element sourceViewEl : sourceViews) {
        if (!sourceViewEl.getLocalName().equals("source-folder")) { // NOI18N
            continue;
        }
        String sourceStyle = sourceViewEl.getAttribute("style"); // NOI18N
        if (style == null || style.equals(sourceStyle)) {
            itemsEl.removeChild(sourceViewEl);
        }
    }
    
    for (SourceFolder sf : sources) {
        if (sf.style == null || sf.style.length() == 0) {
            // perhaps this is principal source folder?
            continue;
        }
        Element sourceFolderEl = doc.createElementNS(Util.NAMESPACE, "source-folder"); // NOI18N
        sourceFolderEl.setAttribute("style", sf.style); // NOI18N
        Element el;
        if (sf.label != null) {
            el = doc.createElementNS(Util.NAMESPACE, "label"); // NOI18N
            el.appendChild(doc.createTextNode(sf.label)); // NOI18N
            sourceFolderEl.appendChild(el);
        }
        if (sf.location != null) {
            el = doc.createElementNS(Util.NAMESPACE, "location"); // NOI18N
            el.appendChild(doc.createTextNode(sf.location)); // NOI18N
            sourceFolderEl.appendChild(el);
        }
        if (sf.includes != null) {
            el = doc.createElementNS(Util.NAMESPACE, "includes"); // NOI18N
            el.appendChild(doc.createTextNode(sf.includes)); // NOI18N
            sourceFolderEl.appendChild(el);
        }
        if (sf.excludes != null) {
            el = doc.createElementNS(Util.NAMESPACE, "excludes"); // NOI18N
            el.appendChild(doc.createTextNode(sf.excludes)); // NOI18N
            sourceFolderEl.appendChild(el);
        }
        XMLUtil.appendChildElement(itemsEl, sourceFolderEl, viewItemElementsOrder);
    }
    Util.putPrimaryConfigurationData(helper, data);
}
 
Example 20
Source File: UsageLogger.java    From netbeans with Apache License 2.0 4 votes vote down vote up
private static Object[] data(Project p) throws Exception {
    ProjectAccessor accessor = p.getLookup().lookup(ProjectAccessor.class);
    if (accessor == null) {
        throw new IllegalArgumentException("no ProjectAccessor");
    }
    AntProjectHelper helper = accessor.getHelper();
    PropertyEvaluator eval = accessor.getEvaluator();
    AuxiliaryConfiguration aux = ProjectUtils.getAuxiliaryConfiguration(p);
    int compilationUnits = 0;
    int compilationUnitsMissingBuiltTo = 0;
    int compilationUnitsMultipleRoots = 0;
    Set<String> classpathEntries = new HashSet<String>();
    Element java = JavaProjectGenerator.getJavaCompilationUnits(aux);
    if (java != null) {
        for (Element compilationUnitEl : XMLUtil.findSubElements(java)) {
            compilationUnits++;
            int builtTos = 0;
            int roots = 0;
            for (Element other : XMLUtil.findSubElements(compilationUnitEl)) {
                String name = other.getLocalName();
                if (name.equals("package-root")) { // NOI18N
                    roots++;
                } else if (name.equals("built-to")) { // NOI18N
                    builtTos++;
                } else if (name.equals("classpath")) { // NOI18N
                    String text = XMLUtil.findText(other);
                    if (text != null) {
                        String textEval = eval.evaluate(text);
                        if (textEval != null) {
                            for (String entry : textEval.split("[:;]")) {
                                if (entry.length() > 0) {
                                    classpathEntries.add(entry);
                                }
                            }
                        }
                    }
                }
            }
            if (builtTos == 0) {
                compilationUnitsMissingBuiltTo++;
            }
            if (roots > 1) {
                compilationUnitsMultipleRoots++;
            }
        }
    }
    int targets = 0;
    {
        String antScriptS = eval.getProperty(ProjectConstants.PROP_ANT_SCRIPT);
        if (antScriptS == null) {
            antScriptS = "build.xml"; // NOI18N
        }
        FileObject antScript = FileUtil.toFileObject(helper.resolveFile(antScriptS));
        if (antScript != null) {
            AntProjectCookie apc = DataObject.find(antScript).getLookup().lookup(AntProjectCookie.class);
            if (apc != null) {
                try {
                    targets = TargetLister.getTargets(apc).size();
                } catch (IOException ioe) {
                    //pass - Broken build.xml which may happen for freeform, targets = 0 and log usage
                }
            }
        }
    }
    boolean webData = aux.getConfigurationFragment("web-data", "http://www.netbeans.org/ns/freeform-project-web/2", true) != null || // NOI18N
            aux.getConfigurationFragment("web-data", "http://www.netbeans.org/ns/freeform-project-web/1", true) != null; // NOI18N
    /* XXX takes about 1msec per source file to count them, even with a warm disk cache:
    int sourceFiles = 0;
    for (SourceGroup g : ProjectUtils.getSources(p).getSourceGroups(JavaProjectConstants.SOURCES_TYPE_JAVA)) {
        for (FileObject kid : NbCollections.iterable(g.getRootFolder().getChildren(true))) {
            if (kid.hasExt("java")) { // NOI18N
                sourceFiles++;
            }
        }
    }
     */
    // XXX other things which could be reported:
    // number of <properties>s (other than the original project location sometimes inserted by the New Project wizard) or <property-file>s defined
    // number of <view-item>s (other than those inserted by the GUI) defined
    // whether a custom Java platform is configured for the project
    // number of subprojects (i.e. classpath entries corresponding to project-owned sources)
    // number of context-sensitive actions defined
    // number of targets bound to non-context-sensitive actions
    return new Object[] { // Bundle.properties#USG_FREEFORM_PROJECT must match these fields
        someOrMany(compilationUnits),
        someOrMany(compilationUnitsMissingBuiltTo),
        someOrMany(compilationUnitsMultipleRoots),
        someOrMany(classpathEntries.size()),
        someOrMany(targets),
        webData,
    };
}