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:tinyequationscoringengine.MathMLParser.java

License:Open Source License

public static boolean applyMSUPFix(Document xml, XmlNamespaceManager nsMgr) {
    boolean docModified = false;
    // MSup issue
    List<Element> matchingNodes = new XmlElement(xml.getRootElement()).selectNodes("//m:msup/m:mrow[m:mo]",
            nsMgr);/*from   ww w  .  j  a  v  a  2 s .  c  o  m*/
    for (Element mRowNode : matchingNodes) {
        XmlElement elementMRowNode = new XmlElement(mRowNode);
        if (!isBogusMROW(elementMRowNode))
            continue; // This is a legit mrow

        // Found a bogus mrow. Remove it and apply the msup to the last child of
        // the mrow.
        // <msup><mrow><mi>x</mi><mo>+</mo><mi>y</mi></mrow><mn>2</mn></msup>

        Element mSupNode = mRowNode.getParentElement();

        XmlElement elementMSupNode = new XmlElement(mSupNode);

        if ((elementMSupNode.getChildNodes().size()) != 2) // If this happens,
                                                           // we should
                                                           // leave. We can't fix this.
        {
            continue;
        }

        if (!(elementMSupNode.getFirstChild().equals(elementMRowNode)))
            continue; // This hack only applies if the mrow is the base node and not
                      // the exponent

        // Get the base node for the msup. This is the last child of the mrow

        XmlElement newBaseNode = elementMRowNode.getLastChild();
        elementMRowNode.removeChild(newBaseNode.getContentNode());

        // Remove all the remaing mrow children and elevent them to be siblings of
        // the msup
        // *** Need to check this
        while (elementMRowNode.hasChildNodes()) {
            XmlElement node = elementMRowNode.getFirstChild();
            elementMRowNode.removeChild(node.getContentNode());
            elementMSupNode.getParentElement().insertBefore(node.getContentNode(),
                    elementMSupNode.getContentNode());
        }

        // remove the mrow all together and splice the new base node in as the
        // first child.
        elementMSupNode.removeChild(elementMRowNode.getContentNode());
        elementMSupNode.insertBefore(newBaseNode.getContentNode(), elementMSupNode.getContentNode());
        docModified = true;
    }
    return docModified;
}

From source file:tinyequationscoringengine.MathMLParser.java

License:Open Source License

public static boolean applySubtractionFix(Document xml, XmlNamespaceManager nsMgr) {
    boolean docModified = false;

    List<Element> matchingNodes = new XmlElement(xml.getRootElement()).selectNodes("//m:mrow[m:mo]", nsMgr);

    for (Element mRowNode : matchingNodes) {
        XmlElement elementmRowNode = new XmlElement(mRowNode);

        if (!isBogusMROW(elementmRowNode))
            continue; // This is a legit mrow

        if (hasComplexParent(mRowNode))
            continue; // we dont want to touch mrows that are within complex mml
                      // elements

        // Found a bogus mrow. Remove it and elevate all the children up 1 level
        // <mrow><mo>&#x2212;<!-- 
        // --></mo><mrow><mo>(</mo><mn>3</mn><mn>7</mn><mo>&#x00D7;<!-- 
        // --></mo><mn>3</mn><mo>)</mo></mrow></mrow>

        Element mRowParentNode = mRowNode.getParentElement();

        // Remove all the remaing mrow children and elevate them to be siblings of
        // the mrow

        while (elementmRowNode.hasChildNodes()) {
            XmlElement node = elementmRowNode.getFirstChild();
            elementmRowNode.removeChild(node.getContentNode());
            new XmlElement(mRowParentNode).insertBefore(node.getContentNode(),
                    elementmRowNode.getContentNode());
        }// w  w  w  .j a  v a2s  .  c om

        // remove the mrow all together and splice the new base node in as the
        // first child.

        new XmlElement(mRowParentNode).removeChild(mRowNode);
        docModified = true;
    }

    return docModified;
}

From source file:tinyequationscoringengine.MathMLParser.java

License:Open Source License

public static boolean applyMROWwithNoOperatorsFix(Document xml, XmlNamespaceManager nsMgr) {
    boolean docModified = false;
    List<Element> matchingNodes = new XmlElement(xml.getRootElement()).selectNodes("//m:mrow", nsMgr);

    for (Element mRowNode : matchingNodes) {
        // We dont want to mess with any mrows that are children of a complex
        // parent
        if (hasComplexParent(mRowNode))
            continue;

        if (new XmlElement(mRowNode).getChildNodes().size() == 0) {
            new XmlElement(mRowNode.getParentElement()).removeChild(mRowNode);
            continue;
        }//from w  ww.  j  a  v a2 s  .c o  m

        // <mn>1</mn><mn>4</mn><mrow><mn>5</mn></mrow>
        boolean containsOnlyMnorMi = true;

        List<Element> filteredList = new ArrayList<Element>();
        CollectionUtils.select(new XmlElement(mRowNode).getChildNodes(), new Predicate() {

            @Override
            public boolean evaluate(Object object) {
                XmlElement child = new XmlElement((Element) object);
                return !("MN".equalsIgnoreCase(child.getLocalName())
                        || "MI".equalsIgnoreCase(child.getLocalName()));
            }
        }, filteredList);

        for (Element child : filteredList) {
            containsOnlyMnorMi = false;
        }

        if (containsOnlyMnorMi) {
            XmlElement mRowParentNode = new XmlElement(mRowNode.getParentElement());

            // Remove all the remaing mrow children and elevate them to be siblings
            // of the mrow
            XmlElement rowNodeElement = new XmlElement(mRowNode);
            while (rowNodeElement.hasChildNodes()) {

                XmlElement node = rowNodeElement.getFirstChild();
                rowNodeElement.removeChild(node.getContentNode());
                mRowParentNode.insertBefore(node.getContentNode(), rowNodeElement.getContentNode());
            }
            // remove the mrow all together and splice the new base node in as the
            // first child.

            mRowParentNode.removeChild(mRowNode);
            docModified = true;
        }
    }

    return docModified;
}

From source file:tinyequationscoringengine.MathMLParser.java

License:Open Source License

private static boolean hasComplexParent(Element node) {
    Element parentNode = node.getParentElement();

    return ("MFRAC".equalsIgnoreCase(parentNode.getName()) || "MSUP".equalsIgnoreCase(parentNode.getName())
            || "MSUB".equalsIgnoreCase(parentNode.getName()) || "MSUPSUB".equalsIgnoreCase(parentNode.getName())
            || "MSQRT".equalsIgnoreCase(parentNode.getName())
            || "MROOT".equalsIgnoreCase(parentNode.getName()));
}

From source file:univ.mlv.GraphBuilder.GraphConstructor.java

private static void process(Element rootElement) {
    //        Element root2=rootElement;
    //        //rcuprer l'id
    //        String nodeID_prop = rootElement.getAttributeValue("nodeID", rdfNamespace);
    //        if (nodeID_prop == null) {
    //            nodeID_prop = rootElement.getAttributeValue("about", rdfNamespace);
    //        }//from   www  .j  a v a  2 s. c  om
    //        if (nodeID_prop == null) {
    //            nodeID_prop = rootElement.getAttributeValue("resource", rdfNamespace);
    //        }
    //        if(nodeID_prop != null && nodeID_prop.contains(".com/")){
    //            nodeID_prop= nodeID_prop.split(".com/")[1].replaceAll("/", "_");
    //        }
    if (!rootElement.getName().equals("RDF") && !rootElement.getName().equals("graph")
            && !rootElement.getName().equals("Text")) {
        String name = rootElement.getName();
        boolean isConcept = false, isProperty = false;
        if (Character.isUpperCase(name.charAt(0))) {
            isConcept = true;
        } else {
            isProperty = true;
        }
        String id = getId(rootElement);
        if (id == null) {
            //c'est une property, si il n'a pas d'enfant alors c'est une proprit object
            //il faut rcuprer son parent et sa valeur
            if (isProperty && rootElement.getChildren().isEmpty()) {
                //c'est une datatype, l'ajouter  la liste des datatype
                if (dataProperty.keySet().contains(getId(rootElement.getParentElement()))) {
                    dataProperty.get(getId(rootElement.getParentElement())).add(rootElement);
                } else {
                    List<Element> newList = new ArrayList<Element>();
                    newList.add(rootElement);
                    dataProperty.put(getId(rootElement.getParentElement()), newList);
                }
            } else if (isProperty) {
                //Si il y a des enfants, c'est que c'est une proprit object, le range n'est pas en id mais est l'enfant qui suit
                if (null != rootElement.getParentElement() && null != getId(rootElement.getParentElement())
                        && objProperty.keySet().contains(getId(rootElement.getParentElement()))) {
                    objProperty.get(getId(rootElement.getParentElement())).add(rootElement);
                } else {
                    List<Element> newList = new ArrayList<Element>();
                    newList.add(rootElement);
                    if (null == getId(rootElement.getParentElement())) {
                        objProperty.put(rootElement.getParentElement().getName(), newList);
                    } else {
                        objProperty.put(getId(rootElement.getParentElement()), newList);
                    }
                }
            }
        } else {
            //Si l'id est prsent mais qu'il n'y a pas d'enfent, alors c'est une proprit objet avec un rdf about dcrit
            if (rootElement.getChildren().isEmpty() && isProperty) {
                if (objProperty.keySet().contains(getId(rootElement.getParentElement()))) {
                    objProperty.get(getId(rootElement.getParentElement())).add(rootElement);
                } else if (isProperty) {
                    List<Element> newList = new ArrayList<Element>();
                    newList.add(rootElement);
                    objProperty.put(getId(rootElement.getParentElement()), newList);
                }
            } else if (isConcept) {
                if (conceptMap.keySet().contains(id)) {
                    //                    conceptMap.get(id).add(rootElement);
                } else {
                    conceptMap.put(id, rootElement);
                    concept.add(rootElement);
                }
            }
        }

    }
    List content = rootElement.getContent();
    Iterator iterator = content.iterator();
    while (iterator.hasNext()) {
        Object o = iterator.next();
        if (o instanceof Element) {
            Element child = (Element) o;
            process(child);
        }
    }
}