Example usage for org.jdom2 Content getParentElement

List of usage examples for org.jdom2 Content getParentElement

Introduction

In this page you can find the example usage for org.jdom2 Content 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:org.esa.s2tbx.dataio.s2.gml.GmlFilter.java

License:Open Source License

public Pair<String, List<EopPolygon>> parse(InputStream stream) {
    SAXBuilder builder = new SAXBuilder();
    Document jdomDoc = null;// ww  w  .  j a v a 2  s  . c  om

    try {
        jdomDoc = builder.build(stream);

        //get the root element
        Element web_app = jdomDoc.getRootElement();
        String maskEpsg = "";

        Namespace gml = Namespace.getNamespace("http://www.opengis.net/gml/3.2");
        Namespace eop = Namespace.getNamespace("http://www.opengis.net/eop/2.0");

        List<Element> targeted = web_app.getChildren("boundedBy", gml);
        if (!targeted.isEmpty()) {
            Element aEnvelope = targeted.get(0).getChild("Envelope", gml);
            if (aEnvelope != null) {
                maskEpsg = aEnvelope.getAttribute("srsName").getValue();
            }
        }

        List<EopPolygon> recoveredGeometries = new ArrayList<>();

        IteratorIterable<Content> contents = web_app.getDescendants();
        while (contents.hasNext()) {
            Content web_app_content = contents.next();
            if (!web_app_content.getCType().equals(CType.Text)
                    && !web_app_content.getCType().equals(CType.Comment)) {
                boolean withGml = (web_app_content.getNamespacesInScope().get(0).getPrefix().contains("gml"));
                if (withGml) {
                    boolean parentNotGml = !(web_app_content.getParentElement().getNamespace().getPrefix()
                            .contains("gml"));
                    if (parentNotGml) {
                        Element capturedElement = (Element) web_app_content;
                        Attribute attr = null;
                        String polygonId = "";
                        String typeId = "";
                        if (capturedElement.getName().contains("Polygon")) {
                            attr = capturedElement.getAttribute("id", gml);
                            if (attr != null) {
                                polygonId = attr.getValue();
                                if (polygonId.indexOf('.') != -1) {
                                    typeId = polygonId.substring(0, polygonId.indexOf('.'));
                                }
                            }
                        }

                        Document newDoc = new Document(capturedElement.clone().detach());

                        ByteArrayOutputStream baos = new ByteArrayOutputStream();
                        xmlOutput.output(newDoc, baos);
                        String replacedContent = baos.toString().replace("/www.opengis.net/gml/3.2",
                                "/www.opengis.net/gml");

                        InputStream ois = new ByteArrayInputStream(replacedContent.getBytes());

                        List<Polygon> pols = streamParseGML3(ois);
                        for (Polygon pol : pols) {
                            recoveredGeometries.add(new EopPolygon(polygonId, typeId, pol));
                        }
                    }
                }

            }
        }

        return new Pair<String, List<EopPolygon>>(maskEpsg, recoveredGeometries);
    } catch (JDOMException e) {
        // {@report "parse xml problem !"}
    } catch (IOException e) {
        // {@report "IO problem !"}
    }

    return new Pair<String, List<EopPolygon>>("", new ArrayList<>());
}

From source file:org.xflatdb.xflat.query.XPathUpdate.java

License:Apache License

/**
 * Applies the update operations to the given DOM Element representing
 * the data in a selected row.//from  w  w  w . j a  v a2  s. c o m
 * @param rowData The DOM Element representing the data in a selected row.
 * @return true if any updates were applied.
 */
public int apply(Element rowData) {
    int updateCount = 0;

    for (Update update : this.updates) {
        //the update's value will be one or the other, don't know which
        Content asContent = null;
        String asString = null;

        if (update.value instanceof String) {
            asString = (String) update.value;
        }

        if (update.value instanceof Content) {
            asContent = (Content) update.value;
        }

        for (Object node : update.path.evaluate(rowData)) {
            if (node == null)
                continue;

            Parent parent;
            Element parentElement;

            if (update.getUpdateType() == UpdateType.UNSET) {
                if (node instanceof Attribute) {
                    parentElement = ((Attribute) node).getParent();
                    if (parentElement != null) {
                        parentElement.removeAttribute((Attribute) node);
                        updateCount++;
                    }
                } else if (node instanceof Content) {
                    parent = ((Content) node).getParent();
                    //remove this node from its parent element
                    if (parent != null) {
                        parent.removeContent((Content) node);
                        updateCount++;
                    }
                }

                continue;
            }

            //it's a set

            if (node instanceof Attribute) {
                //for attributes we set the value to empty string
                //this way it can still be selected by xpath for future updates
                if (update.value == null) {
                    ((Attribute) node).setValue("");
                    updateCount++;
                } else {
                    if (asString == null) {
                        asString = getStringValue(update.value);
                    }

                    //if we fail conversion then do nothing.
                    if (asString != null) {
                        ((Attribute) node).setValue(asString);
                        updateCount++;
                    }
                }

                continue;
            } else if (!(node instanceof Content)) {
                //can't do anything
                continue;
            }

            Content contentNode = (Content) node;

            //need to convert
            if (update.value != null && asContent == null) {
                asContent = getContentValue(update.value);
                if (asContent == null) {
                    //failed conversion, try text
                    asString = getStringValue(update.value);
                    if (asString != null) {
                        //success!
                        asContent = new Text(asString);
                    }
                }
            }

            if (node instanceof Element) {
                //for elements we also set the value, but the value could be Content
                if (update.value == null) {
                    ((Element) node).removeContent();
                    updateCount++;
                } else if (asContent != null) {
                    if (asContent.getParent() != null) {
                        //we used the content before, need to clone it
                        asContent = asContent.clone();
                    }

                    ((Element) node).setContent(asContent);
                    updateCount++;
                }
                continue;
            }

            //at this point the node is Text, CDATA or something else.
            //The strategy now is to replace the value in its parent.
            parentElement = contentNode.getParentElement();
            if (parentElement == null) {
                //can't do anything
                continue;
            }

            if (update.value == null || asContent != null) {
                //replace this content in the parent element
                int index = parentElement.indexOf(contentNode);
                parentElement.removeContent(index);
                if (update.value != null) {
                    //if it was null then act like an unset, otherwise
                    //its a replace

                    if (asContent.getParent() != null) {
                        //we used the content before, need to clone it
                        asContent = asContent.clone();
                    }

                    parentElement.addContent(index, asContent);
                }
                updateCount++;
            }
        }
    }

    return updateCount;
}