Example usage for org.jdom2 Element indexOf

List of usage examples for org.jdom2 Element indexOf

Introduction

In this page you can find the example usage for org.jdom2 Element indexOf.

Prototype

@Override
    public int indexOf(final Content child) 

Source Link

Usage

From source file:AIR.Common.xml.XmlElement.java

License:Open Source License

public XmlElement insertBefore(Content node, Content refNode) {
    if (_element instanceof Element) {
        Element castedElement = (Element) _element;
        int existingPosition = castedElement.indexOf(refNode);
        if (existingPosition < 0)
            existingPosition = 0;/* w ww  . ja  v a2  s.com*/
        return new XmlElement(castedElement.addContent(existingPosition, node));
    }
    return null;
}

From source file:com.bc.ceres.nbmgen.NbmGenTool.java

License:Open Source License

@Override
public void process(CeresModuleProject project) throws JDOMException, IOException {

    System.out.println("Project [" + project.projectDir.getName() + "]:");

    File originalPomFile = getFile(project.projectDir, CeresModuleProject.ORIGINAL_POM_XML);
    File pomFile = getFile(project.projectDir, CeresModuleProject.POM_XML);
    File manifestBaseFile = getFile(project.projectDir, "src", "main", "nbm", "manifest.mf");

    Element projectElement = project.pomDocument.getRootElement();
    Namespace ns = projectElement.getNamespace();

    Element moduleElement = project.moduleDocument.getRootElement();
    String moduleName = moduleElement.getChildTextTrim("name");
    String moduleDescription = moduleElement.getChildTextNormalize("description");
    String modulePackaging = moduleElement.getChildTextTrim("packaging");
    String moduleNative = moduleElement.getChildTextTrim("native");
    String moduleActivator = moduleElement.getChildTextTrim("activator");
    String moduleChangelog = moduleElement.getChildTextTrim("changelog");
    String moduleFunding = moduleElement.getChildTextTrim("funding");
    String moduleVendor = moduleElement.getChildTextTrim("vendor");
    String moduleContactAddress = moduleElement.getChildTextTrim("contactAddress");
    String moduleCopyright = moduleElement.getChildTextTrim("copyright");
    String moduleLicenseUrl = moduleElement.getChildTextTrim("licenseUrl");
    // Not used anymore:
    //String moduleUrl = moduleElement.getChildTextTrim("url");
    //String moduleAboutUrl = moduleElement.getChildTextTrim("aboutUrl");

    if (moduleName != null) {
        Element nameElement = getOrAddElement(projectElement, "name", ns);
        nameElement.setText(moduleName);
    }/*from   w w w  .  j av a 2s.  co m*/
    if (moduleDescription != null) {
        int nameIndex = projectElement.indexOf(projectElement.getChild("name"));
        Element descriptionElement = getOrAddElement(projectElement, "description", nameIndex + 1, ns);
        descriptionElement.setText(moduleDescription);
    }
    Element descriptionElement = getOrAddElement(projectElement, "packaging", ns);
    descriptionElement.setText("nbm");

    Element urlElement = getOrAddElement(projectElement, "url", ns);
    urlElement.setText("https://sentinel.esa.int/web/sentinel/toolboxes");

    Element buildElement = getOrAddElement(projectElement, "build", ns);
    Element pluginsElement = getOrAddElement(buildElement, "plugins", ns);

    Map<String, String> nbmConfiguration = new LinkedHashMap<>();
    // moduleType is actually a constant which can be put it into the <pluginManagement-element of the parent
    nbmConfiguration.put("moduleType", "normal");
    // licenseName/File should also be constant
    nbmConfiguration.put("licenseName", "GPL 3");
    nbmConfiguration.put("licenseFile", "../LICENSE.html");

    nbmConfiguration.put("cluster", cluster);
    nbmConfiguration.put("defaultCluster", cluster);
    nbmConfiguration.put("publicPackages", "");
    nbmConfiguration.put("requiresRestart", "true");

    addPluginElement(pluginsElement, "org.codehaus.mojo", "nbm-maven-plugin", nbmConfiguration, ns);

    Map<String, String> jarConfiguration = new LinkedHashMap<>();
    jarConfiguration.put("useDefaultManifestFile", "true");

    addPluginElement(pluginsElement, "org.apache.maven.plugins", "maven-jar-plugin", jarConfiguration, ns);

    StringBuilder longDescription = new StringBuilder();

    longDescription.append(moduleDescription != null ? "<p>" + moduleDescription + "" : "")
            .append(descriptionEntry("Funding", moduleFunding)).append(descriptionEntry("Vendor", moduleVendor))
            .append(descriptionEntry("Contact address", moduleContactAddress))
            .append(descriptionEntry("Copyright", moduleCopyright))
            .append(descriptionEntry("Vendor", moduleVendor))
            .append(descriptionEntry("License", moduleLicenseUrl))
            .append(descriptionEntry("Changelog", moduleChangelog));

    Map<String, String> manifestContent = new LinkedHashMap<>();
    manifestContent.put("Manifest-Version", "1.0");
    manifestContent.put("AutoUpdate-Show-In-Client", "false");
    manifestContent.put("AutoUpdate-Essential-Module", "true");
    manifestContent.put("OpenIDE-Module-Java-Dependencies", "Java > 1.8");
    manifestContent.put("OpenIDE-Module-Display-Category", "SNAP");
    if (longDescription.length() > 0) {
        manifestContent.put("OpenIDE-Module-Long-Description", longDescription.toString());
    }
    if (moduleActivator != null) {
        warnModuleDetail("Activator may be reimplemented for NB: " + moduleActivator + " (--> "
                + "consider using @OnStart, @OnStop, @OnShowing, or a ModuleInstall)");
        manifestContent.put("OpenIDE-Module-Install", moduleActivator);
    }
    if (modulePackaging != null && !"jar".equals(modulePackaging)) {
        warnModuleDetail("Unsupported module packaging: " + modulePackaging + " (--> "
                + "provide a ModuleInstall that does the job on install/uninstall)");
    }
    if (moduleNative != null && "true".equals(moduleNative)) {
        warnModuleDetail("Module contains native code: no auto-conversion possible (--> "
                + "follow NB instructions see http://bits.netbeans.org/dev/javadoc/org-openide-modules/org/openide/modules/doc-files/api.html#how-layer");
    }

    if (!originalPomFile.exists()) {
        if (!dryRun) {
            Files.copy(project.pomFile.toPath(), originalPomFile.toPath());
        }
        infoModuleDetail("Copied " + project.pomFile + " to " + originalPomFile);
    }

    if (!dryRun) {
        writeXml(pomFile, project.pomDocument);
    }
    if (pomFile.equals(project.pomFile)) {
        infoModuleDetail("Updated " + pomFile);
    } else {
        infoModuleDetail("Converted " + project.pomFile + " to " + pomFile);
    }

    //noinspection ResultOfMethodCallIgnored
    if (!dryRun) {
        manifestBaseFile.getParentFile().mkdirs();
        writeManifest(manifestBaseFile, manifestContent);
    }
    infoModuleDetail("Written " + manifestBaseFile);
}

From source file:com.izforge.izpack.util.xmlmerge.action.InsertAction.java

License:Open Source License

@Override
public void perform(Element originalElement, Element patchElement, Element outputParentElement) {

    if (patchElement == null && originalElement != null) {
        outputParentElement.addContent((Element) originalElement.clone());

    } else {/*from  w  w w. j  a  v a  2 s  . co m*/
        List<Content> outputContent = outputParentElement.getContent();

        Iterator<Content> it = outputContent.iterator();

        int lastIndex = outputContent.size();

        while (it.hasNext()) {
            Content content = it.next();

            if (content instanceof Element) {
                Element element = (Element) content;

                if (element.getQualifiedName().equals(patchElement.getQualifiedName())) {
                    lastIndex = outputParentElement.indexOf(element);
                }
            }
        }

        List<Content> toAdd = new ArrayList<Content>();
        toAdd.add(patchElement);
        outputContent.addAll(Math.min(lastIndex + 1, outputContent.size()), toAdd);
    }
}

From source file:cz.muni.fi.mir.mathmlcanonicalization.modules.AbstractModule.java

License:Apache License

protected void replaceElement(final Element toReplace, final String replacementName) {
    assert toReplace != null && replacementName != null;
    assert !replacementName.isEmpty();
    final Element parent = toReplace.getParentElement();
    assert parent != null;
    final Element replacement = new Element(replacementName);
    replacement.addContent(toReplace.removeContent());
    final List<Attribute> attributes = toReplace.getAttributes();
    for (Attribute attribute : attributes) {
        replacement.setAttribute(attribute.detach());
    }/*from  w ww  .  j ava  2s .  com*/
    final int parentIndex = parent.indexOf(toReplace);
    parent.removeContent(parentIndex);
    parent.addContent(parentIndex, replacement);
    LOGGER.log(Level.FINE, "{0} replaced with {1}", new Object[] { toReplace, replacementName });
}

From source file:cz.muni.fi.mir.mathmlcanonicalization.modules.MfencedReplacer.java

License:Apache License

private void replaceMfenced(final Element mfencedElement, final Element insideContent) {
    assert mfencedElement != null; // but insideContent can be null
    final Namespace ns = mfencedElement.getNamespace();
    Element replacement = new Element(ROW, ns);
    String openStr = getProperty(DEFAULT_OPEN);
    String closeStr = getProperty(DEFAULT_CLOSE);
    if (openStr.isEmpty() || closeStr.isEmpty()) {
        LOGGER.warning("Default open or close fence not set");
    }//www .j a  v a 2s  .  co m

    if (!isEnabled(FORCE_DEFAULT_OPEN)) {
        openStr = mfencedElement.getAttributeValue(OPEN_FENCE, openStr);
    }
    if (!isEnabled(FORCE_DEFAULT_CLOSE)) {
        closeStr = mfencedElement.getAttributeValue(CLOSE_FENCE, closeStr);
    }

    replacement.addContent(new Element(OPERATOR, ns).setText(openStr));
    if (insideContent != null) {
        if (isEnabled(ADD_INNER_ROW)) {
            replacement.addContent(insideContent);
        } else {
            replacement.addContent(insideContent.removeContent());
        }
    }
    replacement.addContent(new Element(OPERATOR, ns).setText(closeStr));

    final Element parent = mfencedElement.getParentElement();
    final int index = parent.indexOf(mfencedElement);
    parent.removeContent(index);
    if (isEnabled(ADD_OUTER_ROW)) {
        parent.addContent(index, replacement);
    } else {
        parent.addContent(index, replacement.removeContent());
    }
    LOGGER.fine("Mfenced element converted");
}

From source file:cz.muni.fi.mir.mathmlcanonicalization.modules.MrowNormalizer.java

License:Apache License

private static void removeElement(final Element element, final Element parent) {
    assert element != null && parent != null;
    parent.addContent(parent.indexOf(element), element.cloneContent());
    element.detach();//from  w w w . j ava  2  s  . c  om
}

From source file:cz.muni.fi.mir.mathmlcanonicalization.modules.MrowNormalizer.java

License:Apache License

/**
 * Wrap previously detected fenced expressions in mrow to be same as output
 * of MfencedReplacer//  w ww.  j a  v  a 2 s . c  o  m
 *
 * @param siblings children of parent element
 * @param fenced list of elements inside parentheses, children of parent
 * element
 * @param opening opening parenthesis, child of parent element
 * @param closing closing parenthesis, child of parent element
 */
private void wrapFenced(final List<Element> siblings, final List<Element> fenced, final Element opening,
        final Element closing) {
    assert siblings != null && fenced != null && opening != null;
    final Element parent = opening.getParentElement();
    assert closing != null && closing.getParentElement().equals(parent);
    for (Element e : fenced) {
        e.detach();
    }
    final int openingIndex = parent.indexOf(opening);

    // Element to be placed inside parentheses.
    // If null, the original 'fenced' list will be used.
    final Element innerElement;
    if (fenced.isEmpty() || !isEnabled(WRAP_ISIDE)) {
        innerElement = null; // will not wrap inside in mrow
    } else if (fenced.size() == 1) {
        innerElement = fenced.get(0); // no need to wrap, just one element
    } else {
        innerElement = new Element(ROW);
        innerElement.addContent(fenced);
        LOGGER.fine("Inner mrow added");
    }

    if (((parent.getName().equals(ROW) && siblings.get(0) == opening
            && siblings.get(siblings.size() - 1) == closing)) || !isEnabled(WRAP_OUTSIDE)) {
        // will not wrap outside in mrow
        if (innerElement == null) {
            parent.addContent(openingIndex + 1, fenced);
        } else {
            parent.addContent(openingIndex + 1, innerElement);
        }
        return;
    }
    // wrap outside in mrow
    opening.detach();
    closing.detach();
    final Element outerMrowElement = new Element(ROW);
    outerMrowElement.addContent(opening);
    if (innerElement != null) {
        outerMrowElement.addContent(innerElement);
    } else {
        outerMrowElement.addContent(fenced);
    }
    outerMrowElement.addContent(closing);
    parent.addContent(openingIndex, outerMrowElement);
    LOGGER.fine("Outer mrow added");
}

From source file:cz.muni.fi.mir.mathmlcanonicalization.modules.UnaryOperatorRemover.java

License:Apache License

private void removeUnaryOperator(final Element rootElem) {

    assert rootElem != null;

    /* Presentation MathML */
    final Set<String> pmCharsToRemove = getPropertySet(PM_UNARY_OPERATORS_TO_REMOVE);

    if (!pmCharsToRemove.isEmpty()) {

        // Unary operators
        List<Element> pmElemsToRemove = xpPMUnaryOperators.evaluate(rootElem);
        for (Element toRemove : pmElemsToRemove) {
            if (pmCharsToRemove.contains(toRemove.getValue())) {
                LOGGER.finest("Removing element '" + toRemove.getQualifiedName() + "' with value '"
                        + toRemove.getValue() + "'.");
                toRemove.detach();//from w  w  w  . jav a  2  s.  c om
            } else {
                LOGGER.finest("Skipping element '" + toRemove.getQualifiedName() + "' with value '"
                        + toRemove.getValue() + "'.");
            }
        }

        // Second of the double operators
        pmElemsToRemove = xpPMSecondOperatorInDoubleOperators.evaluate(rootElem);
        for (Element toRemove : pmElemsToRemove) {
            if (pmCharsToRemove.contains(toRemove.getValue())) {
                LOGGER.finest("Removing the second element out of double elements '"
                        + toRemove.getQualifiedName() + "' with value '" + toRemove.getValue() + "'.");
                toRemove.detach();
            } else {
                LOGGER.finest("Skipping the second element out of double elements '"
                        + toRemove.getQualifiedName() + "' with value '" + toRemove.getValue() + "'.");
            }
        }

    }

    LOGGER.finer("RemoveUnaryOperator Presentation MathML finished");

    /* Content MathML */
    List<Element> applyWithTwoChildrens = xpCMApplyWithTwoChildrens.evaluate(rootElem);
    final Set<String> cmOperatorsToRemove = getPropertySet(CM_UNARY_OPERATORS_TO_REMOVE);

    for (Element applyElem : applyWithTwoChildrens) {
        Element operator = applyElem.getChildren().get(0);
        if (cmOperatorsToRemove.contains(operator.getName())) {
            Element operand = applyElem.getChildren().get(1);
            LOGGER.finest("Removing operator '" + operator.getQualifiedName() + "' for operand '"
                    + operand.getQualifiedName() + "'.");
            operand.detach();
            Element parent = applyElem.getParentElement();
            int applyElemIndex = parent.indexOf(applyElem);
            parent.setContent(applyElemIndex, operand);
            applyElem.detach();
        }
    }

    LOGGER.finer("RemoveUnaryOperator Content MathML finished");

}

From source file:de.danielluedecke.zettelkasten.database.DesktopData.java

License:Open Source License

/**
 * This method moves an element (entry-node or bullet-point) one position up- or downwards, depending
 * on the parameter {@code movement}./*  w  w  w.  ja v  a2s . c om*/
 * @param movement indicates whether the element should be moved up or down. use following constants:<br>
 * - {@code CConstants.MOVE_UP}<br>
 * - {@code CConstants.MOVE_DOWN}<br>
 * @param timestamp
 */
public void moveElement(int movement, String timestamp) {
    // get the selected element, independent from whether it's a node or a bullet
    Element e = findEntryElementFromTimestamp(getCurrentDesktopElement(), timestamp);
    if (e != null) {
        // get the element's parent
        Element p = e.getParentElement();
        // get the index of the element that should be moved
        int index = p.indexOf(e);
        // remove the element that should be moved
        Element dummy = (Element) p.removeContent(index);
        try {
            // and insert element one index-position below the previous index-position
            p.addContent((Constants.MOVE_UP == movement) ? index - 1 : index + 1, dummy);
            // change modifed state
            setModified(true);
        } catch (IllegalAddException ex) {
            Constants.zknlogger.log(Level.WARNING, ex.getLocalizedMessage());
        }
    }
}

From source file:de.dfki.iui.mmds.core.emf.persistence.EmfPersistence.java

License:Creative Commons License

/**
 * Update member contents with resolved data
 * //w w  w  . j av  a2s. c  om
 * @param node
 * @param memberReference
 * @param memberDocument
 * @return
 * @throws Exception
 */
private static Element updateMember(Element node, EReference memberReference, int id, EObject memberObject,
        Document memberDocument) throws Exception {
    Namespace namespace = node.getNamespace(modelMetaData.getNamespace(memberReference));

    List<Element> children = null;
    if (namespace == null) {
        children = node.getChildren(modelMetaData.getName(memberReference));
    } else
        throw new IllegalArgumentException();
    if (id < children.size()) {
        // Inserting resolved contents to the member
        Element element = children.get(id);
        Element clone = memberDocument.getRootElement().clone();
        clone.setName(memberReference.getName());
        node.setContent(node.indexOf(element), clone);
        // Setting xsi:type
        clone.setAttribute("type",
                memberObject.eClass().getEPackage().getName() + ":" + memberObject.eClass().getName(),
                node.getNamespace("xsi"));
    }

    return node;
}