List of usage examples for org.w3c.dom Node getClass
@HotSpotIntrinsicCandidate public final native Class<?> getClass();
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)); }// w w w . j a va 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()); }
From source file:ubic.gemma.loader.entrez.pubmed.XMLUtils.java
/** * Make the horrible DOM API slightly more bearable: get the text value we know this element contains. * <p>/*from w w w. j a v a 2 s . c o m*/ * Borrowed from the Spring API. * <p> * Note that we can't really use the alternative Node.getTextContent() because it isn't supported by older Xerces * implementations (1.x), which tend to leak into the classloader. Causes recurring problems with tests. * * @throws IOException */ public static String getTextValue(org.w3c.dom.Element ele) throws IOException { if (ele == null) return null; StringBuilder value = new StringBuilder(); org.w3c.dom.NodeList nl = ele.getChildNodes(); for (int i = 0; i < nl.getLength(); i++) { org.w3c.dom.Node item = nl.item(i); if (item instanceof org.w3c.dom.CharacterData) { if (!(item instanceof org.w3c.dom.Comment)) { value.append(item.getNodeValue()); } } else { throw new IOException("element is just allowed to have text and comment nodes, not: " + item.getClass().getName()); } } return value.toString(); }
From source file:util.XmlWriter.java
private void write(Node node, int tabs) throws IOException { if (node instanceof Element) write((Element) node, tabs); else if (node instanceof CDATASection) writeCdata((CDATASection) node); else if (node instanceof Text) writeText((Text) node);//w ww .j a v a2 s . c om else throw new IOException("XmlWriter: unsupported node type: " + node.getClass()); }