Example usage for org.jdom2 Element getDescendants

List of usage examples for org.jdom2 Element getDescendants

Introduction

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

Prototype

@Override
public IteratorIterable<Content> getDescendants() 

Source Link

Document

Returns an iterator that walks over all descendants in document order.

Usage

From source file:com.kixeye.kixmpp.KixmppWebSocketCodec.java

License:Apache License

@Override
protected void encode(ChannelHandlerContext ctx, Object msg, List<Object> out) throws Exception {
    WebSocketFrame frame = null;/*from  w  w w . j  ava 2s .  c o m*/

    if (msg instanceof Element) {
        Element element = (Element) msg;

        if (element.getNamespace() == null || element.getNamespace() == Namespace.NO_NAMESPACE) {
            if ("stream".equals(element.getNamespacePrefix())) {
                element.setNamespace(Namespace.getNamespace("http://etherx.jabber.org/streams"));
            } else {
                element.setNamespace(Namespace.getNamespace("jabber:client"));

                IteratorIterable<Content> descendants = element.getDescendants();

                while (descendants.hasNext()) {
                    Content content = descendants.next();

                    if (content instanceof Element) {
                        Element descendantElement = (Element) content;
                        if (descendantElement.getNamespace() == null
                                || descendantElement.getNamespace() == Namespace.NO_NAMESPACE) {
                            descendantElement.setNamespace(element.getNamespace());
                        }
                    }
                }
            }
        }

        ByteBuf binaryData = ctx.alloc().buffer();
        new XMLOutputter().output((Element) msg, new ByteBufOutputStream(binaryData));

        frame = new TextWebSocketFrame(binaryData);
    } else if (msg instanceof KixmppStreamStart) {
        KixmppStreamStart streamStart = (KixmppStreamStart) msg;

        StringWriter writer = new StringWriter();

        if (streamStart.doesIncludeXmlHeader()) {
            writer.append("<?xml version='1.0' encoding='UTF-8'?>");
        }
        writer.append("<stream:stream ");
        if (streamStart.getId() != null) {
            writer.append(String.format("id=\"%s\" ", streamStart.getId()));
        }
        if (streamStart.getFrom() != null) {
            writer.append(String.format("from=\"%s\" ", streamStart.getFrom().getFullJid()));
        }
        if (streamStart.getTo() != null) {
            writer.append(String.format("to=\"%s\" ", streamStart.getTo()));
        }
        writer.append(
                "version=\"1.0\" xmlns=\"jabber:client\" xmlns:stream=\"http://etherx.jabber.org/streams\">");

        frame = new TextWebSocketFrame(writer.toString());
    } else if (msg instanceof KixmppStreamEnd) {
        frame = new TextWebSocketFrame("</stream:stream>");
    } else if (msg instanceof String) {
        frame = new TextWebSocketFrame((String) msg);
    } else if (msg instanceof ByteBuf) {
        frame = new TextWebSocketFrame((ByteBuf) msg);
    }

    if (frame != null) {
        if (logger.isDebugEnabled()) {
            logger.debug("Sending: [{}]", frame.content().toString(StandardCharsets.UTF_8));
        }

        out.add(frame);
    }
}

From source file:eu.himeros.hocr.HocrInfoAggregator.java

License:Open Source License

private void makeNearGtHm() {
    List<Element> words = nearGt.getChildren();
    for (Element word : words) {
        nearGtHm.put(word.getAttributeValue("uc"), word);
        nearGtIdHm.put(Integer.parseInt(word.getAttributeValue("id")), word);
        IteratorIterable<Content> iterator = word.getDescendants();
        while (iterator.hasNext()) {
            Element nestedWord = (Element) iterator.next();
            nearGtIdHm.put(Integer.parseInt(nestedWord.getAttributeValue("id")), nestedWord);
        }/*from  w w w .  j a  va  2s .c  o  m*/
    }
}

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;/*from   w  w  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:utils.ParserXML.java

License:Apache License

public String getStringedSubTree(String element) throws JDOMException, IOException {
    String s = "";
    Element elem = rootElement.getChild(element);
    Iterator lst = elem.getDescendants();
    int i;/*from w ww.j a v a  2s . co m*/
    Element e = document.detachRootElement();
    e.removeNamespaceDeclaration(Namespace.NO_NAMESPACE);
    Element e2 = e.getChild(element);
    XMLOutputter xout = new XMLOutputter();
    Format f = Format.getPrettyFormat();
    xout.setFormat(f);
    return ((xout.outputString(e2).replaceAll("<" + element + ">", "")).replaceAll("</" + element + ">", ""));
}