Example usage for org.jdom2 Element detach

List of usage examples for org.jdom2 Element detach

Introduction

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

Prototype

@Override
    public Element detach() 

Source Link

Usage

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 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//from w  w w  .  j a  va  2s .  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.OperatorNormalizer.java

License:Apache License

private void removeSpareOperators(final Element element, final Collection<String> spareOperators) {
    assert element != null && spareOperators != null && !spareOperators.isEmpty();
    final List<Element> children = element.getChildren();
    for (int i = 0; i < children.size(); i++) {
        final Element actual = children.get(i); // actual element
        if (isOperator(actual)) {
            if (isSpareOperator(actual, spareOperators)) {
                actual.detach();
                i--; // move iterator back after detaching so it points to next element
                LOGGER.log(Level.FINE, "Operator {0} removed", actual);
            }//  w  w w .  ja  va 2  s . c o m
        } else {
            removeSpareOperators(actual, spareOperators);
        }
    }
}

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();
            } else {
                LOGGER.finest("Skipping element '" + toRemove.getQualifiedName() + "' with value '"
                        + toRemove.getValue() + "'.");
            }/*from  w ww . j  a v  a  2 s. c o  m*/
        }

        // 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.smartics.maven.plugin.jboss.modules.xml.XmlFragmentParser.java

License:Apache License

/**
 * Parses the given XML fragment.//from w  ww. j a  v a2  s.c o  m
 *
 * @param xmlFragment the fragment to be parsed in UTF-8 encoding.
 * @return the parsed root element.
 * @throws IllegalArgumentException on any parsing problem.
 */
public Element parse(final String xmlFragment) throws IllegalArgumentException {
    try {
        final InputStream input = IOUtils.toInputStream(xmlFragment, "UTF-8");
        final Document document = builder.build(input);
        final Element root = document.getRootElement();
        return root.detach();
    } catch (final IOException e) {
        throw new IllegalStateException("UTF-8 encoding not supported on this platform.");
    } catch (final JDOMException e) {
        throw new IllegalArgumentException("Cannot parse XML fragment: " + xmlFragment, e);
    }
}

From source file:io.wcm.maven.plugins.contentpackage.unpacker.ContentUnpacker.java

License:Apache License

private void applyXmlExcludes(Element element, String parentPath) {
    String path = parentPath + "/" + element.getName();
    if (exclude(path, this.excludeNodes)) {
        element.detach();
        return;//from   www. j a v  a 2s  .c o  m
    }
    List<Attribute> attributes = new ArrayList<>(element.getAttributes());
    for (Attribute attribute : attributes) {
        if (exclude(attribute.getQualifiedName(), this.excludeProperties)) {
            attribute.detach();
        }
    }
    List<Element> children = new ArrayList<>(element.getChildren());
    for (Element child : children) {
        applyXmlExcludes(child, path);
    }
}

From source file:jmri.jmrit.roster.CopyRosterItemAction.java

License:Open Source License

@Override
boolean doTransfer() {

    // read the from file, change the ID, and write it out
    log.debug("doTransfer starts");

    // ensure preferences will be found
    FileUtil.createDirectory(LocoFile.getFileLocation());

    // locate the file
    //File f = new File(mFullFromFilename);
    // read it//from   ww  w  .  ja v  a  2  s  . c  o  m
    LocoFile lf = new LocoFile(); // used as a temporary
    Element lroot;
    try {
        lroot = lf.rootFromName(mFullFromFilename);
    } catch (Exception e) {
        log.error("Exception while loading loco XML file: " + mFullFromFilename + " exception: " + e);
        return false;
    }

    // create a new entry
    mToEntry = new RosterEntry(mFromEntry, mToID);

    // set the filename from the ID
    mToEntry.ensureFilenameExists();

    // detach the content element from it's existing file so 
    // it can be reused
    lroot.detach();

    // transfer the contents to a new file
    LocoFile newLocoFile = new LocoFile();
    File fout = new File(LocoFile.getFileLocation() + mToEntry.getFileName());
    newLocoFile.writeFile(fout, lroot, mToEntry);

    return true;
}

From source file:lu.list.itis.dkd.aig.resolution.Template.java

License:Apache License

/**
 * Method used for extracting the QTI item husk from an item template.
 *
 * @throws TemplateParseException//from  w w  w.j a v a  2s .c o  m
 *         TODO
 */
private void extractItem(final Document document) throws TemplateParseException {
    Element qtiRoot;

    try {
        final XPathExpression<Element> xpath = XPathFactory.instance().compile(Externalization.QTI_XPATH,
                Filters.element());
        qtiRoot = xpath.evaluateFirst(document);
    } catch (final NullPointerException | IllegalArgumentException | IllegalStateException e) {
        throw new TemplateParseException("Compiling the Xpath expression to resolve the QTI root node failed!", //$NON-NLS-1$
                e);
    }

    itemLayer = DocumentConverter.convertDocumentToString(new Document(qtiRoot.detach()));
    itemLayer = itemLayer.replace("<?xml version=\"1.0\" encoding=\"UTF-8\"?>", ""); //$NON-NLS-1$ //$NON-NLS-2$
}

From source file:model.data.RendezVousIp.java

License:Open Source License

public void removeIp(String ip) {
    for (Element e : ips.getChildren()) {
        if (e.getValue().equals(ip)) {
            e.detach();
        }/*from w ww.  ja v a 2 s.c  o  m*/
    }
}

From source file:neon.editor.DataStore.java

License:Open Source License

private Element loadInfo(String... file) {
    Element info;
    try {/*from  ww w.  j a  va2  s .c o m*/
        info = Editor.files.getFile(new XMLTranslator(), file).getRootElement();
        info.detach();
    } catch (NullPointerException e) { // file bestaat niet
        info = new Element("master");
        info.setAttribute("id", file[0]);
        info.addContent(new Element("title"));
        info.addContent(new Element("currency"));
    }
    return info;
}