Example usage for org.jdom2 Content getNamespacesInScope

List of usage examples for org.jdom2 Content getNamespacesInScope

Introduction

In this page you can find the example usage for org.jdom2 Content getNamespacesInScope.

Prototype

@Override
    public List<Namespace> getNamespacesInScope() 

Source Link

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;/* w  w w.  j av  a2 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<>());
}