Example usage for org.w3c.dom Node getTextContent

List of usage examples for org.w3c.dom Node getTextContent

Introduction

In this page you can find the example usage for org.w3c.dom Node getTextContent.

Prototype

public String getTextContent() throws DOMException;

Source Link

Document

This attribute returns the text content of this node and its descendants.

Usage

From source file:com.msopentech.odatajclient.engine.data.json.DOMTreeUtils.java

/**
 * Serializes DOM content as JSON./*from  w  ww  . j  a va2  s  .  co  m*/
 *
 * @param jgen JSON generator.
 * @param content content.
 * @param propType whether to output type information in the way needed for property values or not.
 * @throws IOException in case of write error.
 */
public static void writeSubtree(final JsonGenerator jgen, final Node content, final boolean propType)
        throws IOException {

    for (Node child : XMLUtils.getChildNodes(content, Node.ELEMENT_NODE)) {
        final String childName = XMLUtils.getSimpleName(child);

        final Node typeAttr = child.getAttributes().getNamedItem(ODataConstants.ATTR_M_TYPE);
        if (typeAttr != null && EdmSimpleType.isGeospatial(typeAttr.getTextContent())) {
            jgen.writeStringField(
                    propType ? ODataConstants.JSON_TYPE : childName + "@" + ODataConstants.JSON_TYPE,
                    typeAttr.getTextContent());

            jgen.writeObjectFieldStart(childName);
            GeospatialJSONHandler.serialize(jgen, (Element) child, typeAttr.getTextContent());
            jgen.writeEndObject();
        } else if (XMLUtils.hasOnlyTextChildNodes(child)) {
            if (child.hasChildNodes()) {
                final String out;
                if (typeAttr == null) {
                    out = child.getChildNodes().item(0).getNodeValue();
                } else {
                    final EdmSimpleType type = EdmSimpleType.fromValue(typeAttr.getTextContent());
                    final ODataPrimitiveValue value = new ODataPrimitiveValue.Builder().setType(type)
                            .setText(child.getChildNodes().item(0).getNodeValue()).build();
                    out = value.toString();

                    jgen.writeStringField(childName + "@" + ODataConstants.JSON_TYPE, type.toString());
                }
                jgen.writeStringField(childName, out);
            } else {
                if (child.getAttributes().getNamedItem(ODataConstants.ATTR_NULL) == null) {
                    if (typeAttr != null && EdmSimpleType.String.toString().equals(typeAttr.getTextContent())) {
                        jgen.writeStringField(childName + "@" + ODataConstants.JSON_TYPE,
                                typeAttr.getTextContent());
                        jgen.writeStringField(childName, StringUtils.EMPTY);
                    } else {
                        jgen.writeArrayFieldStart(childName);
                        jgen.writeEndArray();
                    }
                } else {
                    jgen.writeNullField(childName);
                }
            }
        } else {
            if (XMLUtils.hasElementsChildNode(child)) {
                jgen.writeArrayFieldStart(childName);

                for (Node nephew : XMLUtils.getChildNodes(child, Node.ELEMENT_NODE)) {
                    if (XMLUtils.hasOnlyTextChildNodes(nephew)) {
                        jgen.writeString(nephew.getChildNodes().item(0).getNodeValue());
                    } else {
                        jgen.writeStartObject();
                        writeSubtree(jgen, nephew);
                        jgen.writeEndObject();
                    }
                }

                jgen.writeEndArray();
            } else {
                jgen.writeObjectFieldStart(childName);
                if (typeAttr != null) {
                    jgen.writeStringField(ODataConstants.JSON_TYPE, typeAttr.getTextContent());
                }

                writeSubtree(jgen, child);

                jgen.writeEndObject();
            }
        }
    }
}

From source file:Main.java

public static String nodeListToString(NodeList nodeList) throws TransformerException {
    StringWriter stringWriter = new StringWriter();
    for (int i = 0; i < nodeList.getLength(); ++i) {
        Node node = nodeList.item(i);
        if (node instanceof Element) {
            Transformer transformer = TransformerFactory.newInstance().newTransformer();
            transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
            transformer.transform(new DOMSource(node), new StreamResult(stringWriter));
        } else {//from   www. j a  va 2  s  . c o m
            stringWriter.append(node.getTextContent());
        }
    }
    return stringWriter.toString();
}

From source file:com.msopentech.odatajclient.engine.data.json.GeospatialJSONHandler.java

private static void serializePoint(final JsonGenerator jgen, final Node node) throws IOException {
    for (String coord : node.getTextContent().split(" ")) {
        jgen.writeNumber(coord);//from   www  . jav  a  2  s  .  co  m
    }
}

From source file:jp.sf.fess.solr.plugin.suggest.util.SolrConfigUtil.java

private static Class<?> getMethodArgClass(final Node typeNode) {
    if (typeNode != null) {
        final String type = typeNode.getTextContent();
        if ("Long".equals(type)) {
            return Long.class;
        } else if ("long".equals(type)) {
            return long.class;
        } else if ("Integer".equals(type)) {
            return Integer.class;
        } else if ("int".equals(type)) {
            return int.class;
        }/*  w ww. j ava  2s  .  c om*/
    }
    return String.class;
}

From source file:com.msopentech.odatajclient.engine.data.impl.JSONDOMTreeUtils.java

/**
 * Serializes DOM content as JSON.//from   w w w. j  av  a2 s  .c  o m
 *
 * @param client OData client.
 * @param jgen JSON generator.
 * @param content content.
 * @param propType whether to output type information in the way needed for property values or not.
 * @throws IOException in case of write error.
 */
public static void writeSubtree(final ODataClient client, final JsonGenerator jgen, final Node content,
        final boolean propType) throws IOException {

    for (Node child : XMLUtils.getChildNodes(content, Node.ELEMENT_NODE)) {
        final String childName = XMLUtils.getSimpleName(child);

        final Node typeAttr = child.getAttributes().getNamedItem(ODataConstants.ATTR_M_TYPE);
        if (typeAttr != null && EdmSimpleType.isGeospatial(typeAttr.getTextContent())) {
            jgen.writeStringField(
                    propType ? ODataConstants.JSON_TYPE : childName + "@" + ODataConstants.JSON_TYPE,
                    typeAttr.getTextContent());

            jgen.writeObjectFieldStart(childName);
            GeospatialJSONHandler.serialize(client, jgen, (Element) child, typeAttr.getTextContent());
            jgen.writeEndObject();
        } else if (XMLUtils.hasOnlyTextChildNodes(child)) {
            if (child.hasChildNodes()) {
                final String out;
                if (typeAttr == null) {
                    out = child.getChildNodes().item(0).getNodeValue();
                } else {
                    final EdmSimpleType type = EdmSimpleType.fromValue(typeAttr.getTextContent());
                    final ODataPrimitiveValue value = client.getPrimitiveValueBuilder().setType(type)
                            .setText(child.getChildNodes().item(0).getNodeValue()).build();
                    out = value.toString();

                    jgen.writeStringField(childName + "@" + ODataConstants.JSON_TYPE, type.toString());
                }
                jgen.writeStringField(childName, out);
            } else {
                if (child.getAttributes().getNamedItem(ODataConstants.ATTR_NULL) == null) {
                    if (typeAttr != null && EdmSimpleType.String.toString().equals(typeAttr.getTextContent())) {
                        jgen.writeStringField(childName + "@" + ODataConstants.JSON_TYPE,
                                typeAttr.getTextContent());
                        jgen.writeStringField(childName, StringUtils.EMPTY);
                    } else {
                        jgen.writeArrayFieldStart(childName);
                        jgen.writeEndArray();
                    }
                } else {
                    jgen.writeNullField(childName);
                }
            }
        } else {
            if (XMLUtils.hasElementsChildNode(child)) {
                jgen.writeArrayFieldStart(childName);

                for (Node nephew : XMLUtils.getChildNodes(child, Node.ELEMENT_NODE)) {
                    if (XMLUtils.hasOnlyTextChildNodes(nephew)) {
                        jgen.writeString(nephew.getChildNodes().item(0).getNodeValue());
                    } else {
                        jgen.writeStartObject();
                        writeSubtree(client, jgen, nephew);
                        jgen.writeEndObject();
                    }
                }

                jgen.writeEndArray();
            } else {
                jgen.writeObjectFieldStart(childName);
                if (typeAttr != null) {
                    jgen.writeStringField(ODataConstants.JSON_TYPE, typeAttr.getTextContent());
                }

                writeSubtree(client, jgen, child);

                jgen.writeEndObject();
            }
        }
    }
}

From source file:com.msopentech.odatajclient.engine.data.ODataReader.java

/**
 * Parses a stream taking care to de-serialize the first OData entity property found.
 *
 * @param input stream to de-serialize./*from w  w  w  .  j  av a  2s  . c  o  m*/
 * @param format de-serialize as XML or JSON
 * @return OData entity property de-serialized.
 */
public static ODataProperty readProperty(final InputStream input, final ODataFormat format) {
    final Element property = Deserializer.toPropertyDOM(input, format);

    // The ODataProperty object is used either for actual entity properties and for invoke result (when return type
    // is neither an entity nor a collection of entities).
    // Such formats are mostly the same except for collections: an entity property looks like
    //     <aproperty m:type="Collection(AType)">
    //       <element>....</element>
    //     </aproperty>
    //
    // while an invoke result with returnType="Collection(AnotherType)" looks like
    //     <functionImportName>
    //       <element m:type="AnotherType">...</element>
    //     <functionImportName>
    //
    // The code below is meant for "normalizing" the latter into
    //     <functionImportName m:type="Collection(AnotherType)">
    //       <element m:type="AnotherType">...</element>
    //     <functionImportName>
    final String type = property.getAttribute(ODataConstants.ATTR_M_TYPE);
    final NodeList elements = property.getElementsByTagName(ODataConstants.ELEM_ELEMENT);
    if (StringUtils.isBlank(type) && elements != null && elements.getLength() > 0) {
        final Node elementType = elements.item(0).getAttributes().getNamedItem(ODataConstants.ATTR_M_TYPE);
        if (elementType != null) {
            property.setAttribute(ODataConstants.ATTR_M_TYPE,
                    "Collection(" + elementType.getTextContent() + ")");
        }
    }

    return ODataBinder.getProperty(property);
}

From source file:jp.sf.fess.solr.plugin.suggest.util.SolrConfigUtil.java

private static Object getMethodArgValue(final Node typeNode, final String value) {
    if (typeNode != null) {
        final String type = typeNode.getTextContent();
        if ("Long".equals(type) || "long".equals(type)) {
            return Long.parseLong(value);
        } else if ("Integer".equals(type) || "int".equals(type)) {
            return Integer.parseInt(value);
        }/*from  w w w .j a v a2s  .  co m*/
    }
    return value;
}

From source file:com.msopentech.odatajclient.engine.data.atom.AtomDeserializer.java

public static AtomEntry entry(final Element input) {
    if (!ODataConstants.ATOM_ELEM_ENTRY.equals(input.getNodeName())) {
        return null;
    }/*www  .  j a  va2 s.c om*/

    final AtomEntry entry = new AtomEntry();

    common(input, entry);

    final String etag = input.getAttribute(ODataConstants.ATOM_ATTR_ETAG);
    if (StringUtils.isNotBlank(etag)) {
        entry.setETag(etag);
    }

    final List<Element> categories = XMLUtils.getChildElements(input, ODataConstants.ATOM_ELEM_CATEGORY);
    if (!categories.isEmpty()) {
        entry.setType(categories.get(0).getAttribute(ODataConstants.ATOM_ATTR_TERM));
    }

    final List<Element> links = XMLUtils.getChildElements(input, ODataConstants.ATOM_ELEM_LINK);
    for (Element linkElem : links) {
        final AtomLink link = new AtomLink();
        link.setRel(linkElem.getAttribute(ODataConstants.ATTR_REL));
        link.setTitle(linkElem.getAttribute(ODataConstants.ATTR_TITLE));
        link.setHref(linkElem.getAttribute(ODataConstants.ATTR_HREF));

        if (ODataConstants.SELF_LINK_REL.equals(link.getRel())) {
            entry.setSelfLink(link);
        } else if (ODataConstants.EDIT_LINK_REL.equals(link.getRel())) {
            entry.setEditLink(link);
        } else if (link.getRel().startsWith(ODataConstants.NAVIGATION_LINK_REL)) {
            link.setType(linkElem.getAttribute(ODataConstants.ATTR_TYPE));
            entry.addNavigationLink(link);

            final List<Element> inlines = XMLUtils.getChildElements(linkElem, ODataConstants.ATOM_ELEM_INLINE);
            if (!inlines.isEmpty()) {
                final List<Element> entries = XMLUtils.getChildElements(inlines.get(0),
                        ODataConstants.ATOM_ELEM_ENTRY);
                if (!entries.isEmpty()) {
                    link.setInlineEntry(entry(entries.get(0)));
                }

                final List<Element> feeds = XMLUtils.getChildElements(inlines.get(0),
                        ODataConstants.ATOM_ELEM_FEED);
                if (!feeds.isEmpty()) {
                    link.setInlineFeed(feed(feeds.get(0)));
                }
            }
        } else if (link.getRel().startsWith(ODataConstants.ASSOCIATION_LINK_REL)) {
            entry.addAssociationLink(link);
        } else if (link.getRel().startsWith(ODataConstants.MEDIA_EDIT_LINK_REL)) {
            entry.addMediaEditLink(link);
        }
    }

    final List<Element> authors = XMLUtils.getChildElements(input, ODataConstants.ATOM_ELEM_AUTHOR);
    if (!authors.isEmpty()) {
        final AtomEntry.Author author = new AtomEntry.Author();
        for (Node child : XMLUtils.getChildNodes(input, Node.ELEMENT_NODE)) {
            if (ODataConstants.ATOM_ELEM_AUTHOR_NAME.equals(XMLUtils.getSimpleName(child))) {
                author.setName(child.getTextContent());
            } else if (ODataConstants.ATOM_ELEM_AUTHOR_URI.equals(XMLUtils.getSimpleName(child))) {
                author.setUri(child.getTextContent());
            } else if (ODataConstants.ATOM_ELEM_AUTHOR_EMAIL.equals(XMLUtils.getSimpleName(child))) {
                author.setEmail(child.getTextContent());
            }
        }
        if (!author.isEmpty()) {
            entry.setAuthor(author);
        }
    }

    final List<Element> actions = XMLUtils.getChildElements(input, ODataConstants.ATOM_ELEM_ACTION);
    for (Element action : actions) {
        final ODataOperation operation = new ODataOperation();
        operation.setMetadataAnchor(action.getAttribute(ODataConstants.ATTR_METADATA));
        operation.setTitle(action.getAttribute(ODataConstants.ATTR_TITLE));
        operation.setTarget(URI.create(action.getAttribute(ODataConstants.ATTR_TARGET)));

        entry.addOperation(operation);
    }

    final List<Element> contents = XMLUtils.getChildElements(input, ODataConstants.ATOM_ELEM_CONTENT);
    if (!contents.isEmpty()) {
        final Element content = contents.get(0);

        List<Element> props = XMLUtils.getChildElements(content, ODataConstants.ELEM_PROPERTIES);
        if (props.isEmpty()) {
            entry.setMediaContentSource(content.getAttribute(ODataConstants.ATOM_ATTR_SRC));
            entry.setMediaContentType(content.getAttribute(ODataConstants.ATTR_TYPE));

            props = XMLUtils.getChildElements(input, ODataConstants.ELEM_PROPERTIES);
            if (!props.isEmpty()) {
                entry.setMediaEntryProperties(props.get(0));
            }
        } else {
            entry.setContent(props.get(0));
        }
    }

    return entry;
}

From source file:de.codesourcery.eve.apiclient.utils.XMLParseHelper.java

public static final String getNodeValue(Node n) {
    final String value = n.getTextContent();
    if (StringUtils.isBlank(value)) {
        throw new UnparseableResponseException(
                "Child element " + " '" + n.getNodeName() + "' " + " has no/blank value ?");
    }/*from  w  w w  . ja v a  2  s  .co  m*/
    return value;
}

From source file:importer.handler.post.stages.Discriminator.java

/**
 * Get the next sibling that is an element
 * @param elem the element/*from   w  w w .  j a  v  a 2s  .  c  o  m*/
 * @param skipText if true skip text nodes to next element node
 * @return its next sibling of elem or null
 */
static Element nextSibling(Element elem, boolean skipText) {
    Node n = elem.getNextSibling();
    while (n != null) {
        if (!skipText && n.getNodeType() == Node.TEXT_NODE && !isWhitespace(n.getTextContent()))
            return null;
        else if (n.getNodeType() == Node.ELEMENT_NODE)
            return (Element) n;
        n = n.getNextSibling();
    }
    return null;
}