Example usage for org.jdom2 Element getChildren

List of usage examples for org.jdom2 Element getChildren

Introduction

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

Prototype

public List<Element> getChildren() 

Source Link

Document

This returns a List of all the child elements nested directly (one level deep) within this element, as Element objects.

Usage

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

License:Apache License

private void replaceMfenced(final Element mfencedElement) {
    assert mfencedElement != null;
    final char[] separators = getSeparators(mfencedElement);
    final Namespace ns = mfencedElement.getNamespace();
    final List<Element> children = mfencedElement.getChildren();
    final int nChildren = children.size();
    final int last = Math.min(separators.length - 1, nChildren - 2);

    Element insideFence = null;/*from  w w w  .  j  a  v a  2 s .c o  m*/
    if (nChildren == 1 && children.get(0).getName().equals(ROW)) {
        // we do not want to add another mrow
        insideFence = children.get(0).detach();
    } else if (nChildren != 0) {
        insideFence = new Element(ROW, ns);
        for (int i = 0; i < nChildren; i++) {
            // add separator
            if (i > 0 && last >= 0) { // not before first or when blank separators
                char separatorChar = separators[(i - 1 > last) ? last : i - 1];
                String separatorStr = Character.toString(separatorChar);
                insideFence.addContent(new Element(OPERATOR, ns).setText(separatorStr));
            }
            // add original child
            insideFence.addContent(children.get(0).detach());
        }
    }
    replaceMfenced(mfencedElement, insideFence);
}

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

License:Apache License

/**
 * Recursively searches element content to possibly remove or add mrow where
 * needed.//from  w  w w  .j  a v a 2 s.  c o m
 *
 * @param element element to start at
 */
private void traverseChildrenElements(final Element element) {
    assert element != null;
    final List<Element> children = new ArrayList<Element>(element.getChildren());
    for (Element child : children) {
        traverseChildrenElements(child);
    }
    if (element.getName().equals(ROW)) {
        checkRemoval(element);
    } else {
        checkAddition(element);
    }
}

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

License:Apache License

/**
 * Removes a mrow element if possible./*ww w .  j  av a 2s  . c om*/
 *
 * @param mrowElement the mrow element
 */
private void checkRemoval(final Element mrowElement) {
    assert mrowElement != null && mrowElement.getName().equals(ROW);
    final Parent parent = mrowElement.getParent();
    if (!(parent instanceof Element)) {
        return; // no parent element
    }
    final Element parentElement = (Element) parent;
    final List<Element> children = mrowElement.getChildren();

    if (children.size() <= 1) {
        removeElement(mrowElement, parentElement);
        LOGGER.log(Level.FINE, "Element {0} removed", mrowElement);
        return;
    }

    final String childCountPropertyName = CHILD_COUNT_PREFIX + parentElement.getName();
    if (!isProperty(childCountPropertyName)) {
        return; // unknown parent element
    }
    final String childCountProperty = getProperty(childCountPropertyName);
    final int childCount;
    try {
        childCount = Integer.parseInt(childCountProperty);
    } catch (NumberFormatException e) {
        LOGGER.log(Level.WARNING, "{0} must be an integer, property ignored", childCountProperty);
        return;
    }

    if (childCount == 1 || // parent can accept any number of elements so we can remove mrow
            children.size() + parentElement.getChildren().size() - 1 == childCount) {
        removeElement(mrowElement, parentElement);
    }
}

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

License:Apache License

/**
 * Add mrow if necessary/*from  w ww. j a  v  a2s . c om*/
 */
private void checkAddition(final Element element) {
    assert element != null;
    final Parent parent = element.getParent();
    if (!(parent instanceof Element)) {
        return;
    }
    final Element parentElement = (Element) parent;
    final List<Element> siblings = parentElement.getChildren();

    if (isParenthesis(element, OPENING)) {
        // Need to find matching closing par and register the elements between
        int nesting = 0;

        // list of elements inside parentheses
        final List<Element> fenced = new ArrayList<Element>();

        for (int i = siblings.indexOf(element) + 1; i < siblings.size(); i++) {
            final Element current = siblings.get(i);

            if (isParenthesis(current, OPENING)) {
                nesting++; // opening parenthase reached
            } else if (isParenthesis(current, CLOSING)) { // closing parenthase reached
                if (nesting == 0) {
                    // matching closing parenthase
                    wrapFenced(siblings, fenced, element, current);
                    break;
                } else {
                    nesting--;
                }
            }
            fenced.add(current);
        }
    }
}

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();/*from  ww w .  j a v a  2 s  .c  o m*/
                i--; // move iterator back after detaching so it points to next element
                LOGGER.log(Level.FINE, "Operator {0} removed", actual);
            }
        } else {
            removeSpareOperators(actual, spareOperators);
        }
    }
}

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

License:Apache License

private void normalizeSupInSub(final Element element) {
    assert element != null;
    final List<Element> children = element.getChildren();
    for (int i = 0; i < children.size(); i++) {
        final Element actual = children.get(i);
        normalizeSupInSub(actual);//from   www.j  ava 2 s. c o  m
        if (!actual.getName().equals(SUBSCRIPT)) {
            continue;
        }
        List<Element> subscriptChildren = actual.getChildren();
        if (subscriptChildren.size() != 2) {
            LOGGER.info("Invalid msub, skipped");
            continue;
        }
        if (!subscriptChildren.get(0).getName().equals(SUPERSCRIPT)) {
            continue;
        }
        final List<Element> superscriptChildren = subscriptChildren.get(0).getChildren();
        if (superscriptChildren.size() != 2) {
            LOGGER.info("Invalid msup, skipped");
            continue;
        }
        final Element newMsub = new Element(SUBSCRIPT);
        newMsub.addContent(superscriptChildren.get(0).detach());
        newMsub.addContent(subscriptChildren.get(1).detach());
        final Element newMsup = new Element(SUPERSCRIPT);
        newMsup.addContent(newMsub);
        newMsup.addContent(superscriptChildren.get(0).detach());
        children.set(i, newMsup);
        LOGGER.fine("Sub/sup scripts swapped");
    }
}

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

License:Apache License

private void normalizeMsubsup(final Element element, Collection<String> firstChildren) {
    assert element != null && firstChildren != null;
    final List<Element> children = element.getChildren();
    for (int i = 0; i < children.size(); i++) {
        final Element actual = children.get(i);
        if (actual.getName().equals(SUBSUP)) {
            final List<Element> actualChildren = actual.getChildren();
            if (actualChildren.size() != 3) {
                LOGGER.info("Invalid msubsup, skipped");
                continue;
            }/* www .j  a v  a 2  s .com*/
            if (!firstChildren.contains(actualChildren.get(0).getName())) {
                continue;
            }
            final Element newMsub = new Element(SUBSCRIPT);
            newMsub.addContent(actualChildren.get(0).detach());
            newMsub.addContent(actualChildren.get(0).detach());
            final Element newMsup = new Element(SUPERSCRIPT);
            newMsup.addContent(newMsub);
            newMsup.addContent(actualChildren.get(0).detach());
            children.set(i, newMsup);
            i--; // move back to check the children of the new transformation
            LOGGER.fine("Msubsup converted to nested msub and msup");
        } else {
            normalizeMsubsup(actual, firstChildren);
        }
    }
}

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   ww  w  . j a  v a2 s.com*/
            } 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:cz.natur.cuni.mirai.math.algebra.MiraiParser.java

License:Open Source License

public static void load(MathModel model, String filename) throws Exception {

    System.out.println("MiraiParser.load(" + filename + ")");

    Element root = new SAXBuilder().build(new FileInputStream(filename), "UTF-8").getRootElement();
    // keyboard input, characters and operators
    for (Object element : root.getChildren()) {
        String name = ((Element) element).getName();

        if (name.equals(FORMULA)) {
            String text = ((Element) element).getText();
            model.addElement(parse(model, text));

        } else if (name.equals(LABEL)) {
            String label = ((Element) element).getText();
            model.addElement(label);//from   www  .ja v a 2 s  . co m
        }
    }
    model.setModified(false);
    // validate model
    model.firstFormula();
}

From source file:cz.natur.cuni.mirai.math.meta.MetaModel.java

License:Open Source License

private void parseComponents(Element parent) throws Exception {
    for (Object elements : parent.getChildren()) {
        String groupName = ((Element) elements).getName(), group = groupName;
        int columns = 0;
        try {/* w  w w.  jav  a 2s  . c  o m*/
            groupName = getStringAttribute(NAME, (Element) elements);
        } catch (Exception e) {
        }

        try {
            group = getStringAttribute(GROUP, (Element) elements);
        } catch (Exception e) {
        }

        try {
            columns = getIntAttribute(COLUMNS, (Element) elements);
        } catch (Exception e) {
        }

        ArrayList<MetaComponent> arrayList = new ArrayList<MetaComponent>();
        for (Object element : ((Element) elements).getChildren()) {
            String name = ((Element) element).getName();
            if (name.equals(OPEN) || name.equals(CLOSE) || name.equals(FIELD) || name.equals(ROW)) {
                MetaComponent metaComponent = parseArrayElement((Element) element);
                arrayList.add(metaComponent);

            } else if (name.equals(CHARACTER)) {
                MetaCharacter metaCharacter = parseCharacter((Element) element);
                arrayList.add(metaCharacter);

            } else if (name.equals(OPERATOR)) {
                MetaSymbol metaOperator = parseSymbol((Element) element);
                arrayList.add(metaOperator);

            } else if (name.equals(SYMBOL)) {
                MetaSymbol metaSymbol = parseSymbol((Element) element);
                arrayList.add(metaSymbol);

            } else if (name.equals(FUNCTION)) {
                MetaFunction metaFunction = parseFunction((Element) element);
                arrayList.add(metaFunction);
            }
        }
        MetaComponent metas[] = arrayList.toArray(new MetaComponent[arrayList.size()]);

        if (groupName.equals(ARRAY) || groupName.equals(MATRIX)) {
            groups.add(new MetaArray(groupName, group, metas));
        } else {
            groups.add(new MetaGroup(groupName, group, metas, columns));
        }
    }
}