Example usage for org.jdom2 Element getParentElement

List of usage examples for org.jdom2 Element getParentElement

Introduction

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

Prototype

final public Element getParentElement() 

Source Link

Document

A convenience method that returns any parent element for this element, or null if the element is unattached or is a root element.

Usage

From source file:com.rometools.modules.cc.io.CCModuleGenerator.java

License:Open Source License

@Override
public void generate(final Module module, final Element element) {
    Element root = element;
    while (root.getParentElement() != null) {
        root = root.getParentElement();/*from   w w  w  .  jav a2 s .  c o  m*/
    }
    if (root.getNamespace().equals(RDF) || root.getNamespace().equals(RSS)) {
        generateRSS1((CreativeCommons) module, element);
    } else {
        generateRSS2((CreativeCommons) module, element);
    }
}

From source file:com.rometools.modules.cc.io.CCModuleGenerator.java

License:Open Source License

private void generateRSS1(final CreativeCommons module, final Element element) {
    // throw new RuntimeException( "Generating RSS1 Feeds not currently Supported.");

    LOG.debug(element.getName());/*from  ww  w. j  a  va 2 s . c  o  m*/
    if (element.getName().equals("channel")) {
        // Do all licenses list.
        final License[] all = module.getAllLicenses();
        for (final License element2 : all) {
            final Element license = new Element("License", RSS1);
            license.setAttribute("about", element2.getValue(), RDF);
            final License.Behaviour[] permits = element2.getPermits();
            for (int j = 0; permits != null && j < permits.length; j++) {
                final Element permit = new Element("permits", RSS1);
                permit.setAttribute("resource", permits[j].toString(), RDF);
                license.addContent(permit);
            }
            final License.Behaviour[] requires = element2.getPermits();
            for (int j = 0; requires != null && j < requires.length; j++) {
                final Element permit = new Element("requires", RSS1);
                permit.setAttribute("resource", permits[j].toString(), RDF);
                license.addContent(permit);
            }
            LOG.debug("Is Root? {}", element.getParentElement());
            element.getParentElement().addContent(license);
        }
    }

    // Do local licenses
    final License[] licenses = module.getLicenses();
    for (final License license2 : licenses) {
        final Element license = new Element("license", RSS1);
        license.setAttribute("resource", license2.getValue(), RDF);
        element.addContent(license);
    }

}

From source file:com.rometools.modules.cc.io.ModuleParserRSS1.java

License:Open Source License

@Override
public Module parse(final Element element, final Locale locale) {
    final CreativeCommonsImpl module = new CreativeCommonsImpl();
    {/*from   w w w .  j a  v a  2  s. c  om*/
        // Parsing Channel level.
        Element root = element;
        while (root.getParentElement() != null) {
            root = root.getParentElement();
        }
        final List<Element> licenseList = root.getChildren("License", NS);
        final ArrayList<License> licenses = new ArrayList<License>();
        final Iterator<Element> it = licenseList.iterator();
        while (it.hasNext()) {
            final Element licenseTag = it.next();
            final String licenseURI = licenseTag.getAttributeValue("about", RDF);
            if (licenseURI == null) {
                continue;
            }
            License license = License.findByValue(licenseURI);
            {
                final ArrayList<Behaviour> permitsValues = new ArrayList<Behaviour>();
                final ArrayList<Behaviour> requiresValues = new ArrayList<Behaviour>();
                final List<Element> permitsTags = licenseTag.getChildren("permits", NS);
                Iterator<Element> sit = permitsTags.iterator();
                while (sit.hasNext()) {
                    final Element permitTag = sit.next();
                    permitsValues
                            .add(License.Behaviour.findByValue(permitTag.getAttributeValue("resource", RDF)));
                }
                final List<Element> requiresTags = licenseTag.getChildren("requires", NS);
                sit = requiresTags.iterator();
                while (sit.hasNext()) {
                    final Element requireTag = sit.next();
                    requiresValues
                            .add(License.Behaviour.findByValue(requireTag.getAttributeValue("resource", RDF)));
                }
                license = new License(licenseURI,
                        requiresValues.toArray(new License.Behaviour[requiresValues.size()]),
                        permitsValues.toArray(new License.Behaviour[permitsValues.size()]));

            }

            licenses.add(license);
        }
        module.setAllLicenses(licenses.toArray(new License[0]));
    }
    final ArrayList<License> licenses = new ArrayList<License>();
    final List<Element> licenseTags = element.getChildren("license", NS);
    final Iterator<Element> lit = licenseTags.iterator();
    while (lit.hasNext()) {
        final Element licenseTag = lit.next();
        licenses.add(License.findByValue(licenseTag.getAttributeValue("resource", RDF)));
    }

    if (!licenses.isEmpty()) {
        module.setLicenses(licenses.toArray(new License[licenses.size()]));
    }

    if (module.getLicenses() != null || module.getAllLicenses() != null) {
        return module;
    } else {
        return null;
    }
}

From source file:com.rometools.modules.cc.io.ModuleParserRSS2.java

License:Open Source License

@Override
public Module parse(final Element element, final Locale locale) {
    final CreativeCommonsImpl module = new CreativeCommonsImpl();
    // Do channel global
    {//  w  ww . j a  v a  2s  . c  om
        Element root = element;
        while (!root.getName().equals("channel") && !root.getName().equals("feed")) {
            root = root.getParentElement();
        }
        final ArrayList<License> licenses = new ArrayList<License>();
        List<Element> items = null;
        if (root.getName().equals("channel")) {
            items = root.getChildren("item");
        } else {
            items = root.getChildren("entry");
        }

        final Iterator<Element> iit = items.iterator();
        while (iit.hasNext()) {
            final Element item = iit.next();
            final List<Element> licenseTags = item.getChildren("license", NS);
            final Iterator<Element> lit = licenseTags.iterator();
            while (lit.hasNext()) {
                final Element licenseTag = lit.next();
                final License license = License.findByValue(licenseTag.getTextTrim());
                if (!licenses.contains(license)) {
                    ;
                }
                licenses.add(license);
            }
        }
        if (!licenses.isEmpty()) {
            module.setAllLicenses(licenses.toArray(new License[0]));
        }
    }
    // do element local
    final ArrayList<License> licenses = new ArrayList<License>();
    final List<Element> licenseTags = element.getChildren("license", NS);
    final Iterator<Element> it = licenseTags.iterator();
    while (it.hasNext()) {
        final Element licenseTag = it.next();
        licenses.add(License.findByValue(licenseTag.getTextTrim()));
    }
    if (!licenses.isEmpty()) {
        module.setLicenses(licenses.toArray(new License[0]));
    }

    if (module.getLicenses() != null && module.getAllLicenses() != null) {
        return module;
    } else {
        return null;
    }
}

From source file:com.xebialabs.overcast.support.libvirt.jdom.DomainXml.java

License:Apache License

/** remove elements that need to be unique per clone. */
public static Document prepareForCloning(Document domainXml) {
    XPathFactory xpf = XPathFactory.instance();

    // remove uuid so it will be generated
    domainXml.getRootElement().removeChild("uuid");

    // remove mac address, so it will be generated
    XPathExpression<Element> macExpr = xpf.compile("/domain/devices/interface/mac", Filters.element());
    for (Element mac : macExpr.evaluate(domainXml)) {
        mac.getParentElement().removeChild("mac");
    }//from www.j a  v a2s .com
    return domainXml;
}

From source file:com.xebialabs.overcast.support.libvirt.jdom.FilesystemXml.java

License:Apache License

public static void removeFilesystemsWithTarget(Document domainXml, String targetDir) {
    XPathFactory xpf = XPathFactory.instance();
    XPathExpression<Element> fsExpr = xpf.compile(
            String.format("/domain/devices/filesystem[@type='mount']/target[@dir='%s']", targetDir),
            Filters.element());/* w  ww.j a  va 2s.  co  m*/
    List<Element> tfs = fsExpr.evaluate(domainXml);
    for (Element e : tfs) {
        e.getParentElement().getParentElement().removeContent(e.getParentElement());
    }
}

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   www  .  j a v  a 2  s.c o m*/
    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");
    }/*from   w w  w . ja va2 s.  c o 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

/**
 * Wrap previously detected fenced expressions in mrow to be same as output
 * of MfencedReplacer/*  w w w. j  av a  2s  .  c om*/
 *
 * @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();//  w  w  w . j a v a 2s  . 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");

}