Example usage for org.w3c.dom Attr setTextContent

List of usage examples for org.w3c.dom Attr setTextContent

Introduction

In this page you can find the example usage for org.w3c.dom Attr setTextContent.

Prototype

public void setTextContent(String textContent) throws DOMException;

Source Link

Document

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

Usage

From source file:sf.net.experimaestro.utils.JSUtils.java

public static Object toDOM(Scriptable scope, Object object, OptionalDocument document) {
    // Unwrap if needed (if this is not a JSBaseObject)
    if (object instanceof Wrapper && !(object instanceof JSBaseObject))
        object = ((Wrapper) object).unwrap();

    // It is already a DOM node
    if (object instanceof Node)
        return object;

    if (object instanceof XMLObject) {
        final XMLObject xmlObject = (XMLObject) object;
        String className = xmlObject.getClassName();

        if (className.equals("XMLList")) {
            LOGGER.debug("Transforming from XMLList [%s]", object);
            final Object[] ids = xmlObject.getIds();
            if (ids.length == 1)
                return toDOM(scope, xmlObject.get((Integer) ids[0], xmlObject), document);

            Document doc = XMLUtils.newDocument();
            DocumentFragment fragment = doc.createDocumentFragment();

            for (int i = 0; i < ids.length; i++) {
                Node node = (Node) toDOM(scope, xmlObject.get((Integer) ids[i], xmlObject), document);
                if (node instanceof Document)
                    node = ((Document) node).getDocumentElement();
                fragment.appendChild(doc.adoptNode(node));
            }/*from ww  w  . j  av a  2  s  .co m*/

            return fragment;
        }

        // XML node
        if (className.equals("XML")) {
            // FIXME: this strips all whitespaces!
            Node node = XMLLibImpl.toDomNode(object);
            LOGGER.debug("Got node from JavaScript [%s / %s] from [%s]", node.getClass(),
                    XMLUtils.toStringObject(node), object.toString());

            if (node instanceof Document)
                node = ((Document) node).getDocumentElement();

            node = document.get().adoptNode(node.cloneNode(true));
            return node;
        }

        throw new RuntimeException(format("Not implemented: convert %s to XML", className));

    }

    if (object instanceof NativeArray) {
        NativeArray array = (NativeArray) object;
        ArrayNodeList list = new ArrayNodeList();
        for (Object x : array) {
            Object o = toDOM(scope, x, document);
            if (o instanceof Node)
                list.add(document.cloneAndAdopt((Node) o));
            else {
                for (Node node : XMLUtils.iterable((NodeList) o)) {
                    list.add(document.cloneAndAdopt(node));
                }
            }
        }
        return list;
    }

    if (object instanceof NativeObject) {
        // JSON case: each key of the JS object is an XML element
        NativeObject json = (NativeObject) object;
        ArrayNodeList list = new ArrayNodeList();

        for (Object _id : json.getIds()) {

            String jsQName = JSUtils.toString(_id);

            if (jsQName.length() == 0) {
                final Object seq = toDOM(scope, json.get(jsQName, json), document);
                for (Node node : XMLUtils.iterable(seq)) {
                    if (node instanceof Document)
                        node = ((Document) node).getDocumentElement();
                    list.add(document.cloneAndAdopt(node));
                }
            } else if (jsQName.charAt(0) == '@') {
                final QName qname = QName.parse(jsQName.substring(1), null, new JSNamespaceBinder(scope));
                Attr attribute = document.get().createAttributeNS(qname.getNamespaceURI(),
                        qname.getLocalPart());
                StringBuilder sb = new StringBuilder();
                for (Node node : XMLUtils.iterable(toDOM(scope, json.get(jsQName, json), document))) {
                    sb.append(node.getTextContent());
                }

                attribute.setTextContent(sb.toString());
                list.add(attribute);
            } else {
                final QName qname = QName.parse(jsQName, null, new JSNamespaceBinder(scope));
                Element element = qname.hasNamespace()
                        ? document.get().createElementNS(qname.getNamespaceURI(), qname.getLocalPart())
                        : document.get().createElement(qname.getLocalPart());

                list.add(element);

                final Object seq = toDOM(scope, json.get(jsQName, json), document);
                for (Node node : XMLUtils.iterable(seq)) {
                    if (node instanceof Document)
                        node = ((Document) node).getDocumentElement();
                    node = document.get().adoptNode(node.cloneNode(true));
                    if (node.getNodeType() == Node.ATTRIBUTE_NODE)
                        element.setAttributeNodeNS((Attr) node);
                    else
                        element.appendChild(node);
                }
            }
        }

        return list;
    }

    if (object instanceof Double) {
        // Wrap a double
        final Double x = (Double) object;
        if (x.longValue() == x.doubleValue())
            return document.get().createTextNode(Long.toString(x.longValue()));
        return document.get().createTextNode(Double.toString(x));
    }

    if (object instanceof Integer) {
        return document.get().createTextNode(Integer.toString((Integer) object));
    }

    if (object instanceof CharSequence) {
        return document.get().createTextNode(object.toString());
    }

    if (object instanceof UniqueTag)
        throw new XPMRuntimeException("Undefined cannot be converted to XML", object.getClass());

    if (object instanceof JSNode) {
        Node node = ((JSNode) object).getNode();
        if (document.has()) {
            if (node instanceof Document)
                node = ((Document) node).getDocumentElement();
            return document.get().adoptNode(node);
        }
        return node;
    }

    if (object instanceof JSNodeList) {
        return ((JSNodeList) object).getList();
    }

    if (object instanceof Scriptable) {
        ((Scriptable) object).getDefaultValue(String.class);
    }

    // By default, convert to string
    return document.get().createTextNode(object.toString());
}