List of usage examples for org.w3c.dom Node TEXT_NODE
short TEXT_NODE
To view the source code for org.w3c.dom Node TEXT_NODE.
Click Source Link
Text
node. From source file:kenh.xscript.ScriptUtils.java
/** * Check if <code>Node</code> has text. * @param node//from w w w. j a va 2 s. com * @return */ private static boolean includeTextNode(Node node) { short type = node.getNodeType(); switch (type) { case Node.ELEMENT_NODE: NodeList nodes = node.getChildNodes(); if (nodes == null) return false; for (int i = 0; i < nodes.getLength(); i++) { Node child = nodes.item(i); if (child.getNodeType() == Node.TEXT_NODE) { String text = child.getNodeValue(); if (StringUtils.isNotBlank(text)) return true; } else if (child.getNodeType() == Node.CDATA_SECTION_NODE) { return true; } } return false; case Node.TEXT_NODE: return true; default: return false; } }
From source file:esg.security.yadis.XrdsDoc.java
public List parse(String input, Set targetTypes) throws XrdsParseException { Document document = parseXmlInput(input); NodeList XRDs = document.getElementsByTagNameNS(XRD_NS, XRD_ELEM_XRD); Node lastXRD;/*from www .j a va2 s . c o m*/ if (XRDs.getLength() < 1 || (lastXRD = XRDs.item(XRDs.getLength() - 1)) == null) throw new XrdsParseException("No XRD elements found."); // get the canonical ID, if any (needed for XRIs) String canonicalId = null; Node canonicalIdNode; NodeList canonicalIDs = document.getElementsByTagNameNS(XRD_NS, XRD_ELEM_CANONICALID); for (int i = 0; i < canonicalIDs.getLength(); i++) { canonicalIdNode = canonicalIDs.item(i); if (canonicalIdNode.getParentNode() != lastXRD) continue; if (canonicalId != null) throw new XrdsParseException("More than one Canonical ID found."); canonicalId = canonicalIdNode.getFirstChild() != null && canonicalIdNode.getFirstChild().getNodeType() == Node.TEXT_NODE ? canonicalIdNode.getFirstChild().getNodeValue() : null; } // extract the services that match the specified target types NodeList types = document.getElementsByTagNameNS(XRD_NS, XRD_ELEM_TYPE); Map serviceTypes = new HashMap(); Set selectedServices = new HashSet(); Node typeNode, serviceNode; for (int i = 0; i < types.getLength(); i++) { typeNode = types.item(i); String type = typeNode != null && typeNode.getFirstChild() != null && typeNode.getFirstChild().getNodeType() == Node.TEXT_NODE ? typeNode.getFirstChild().getNodeValue() : null; if (type == null) continue; serviceNode = typeNode.getParentNode(); if (targetTypes == null) selectedServices.add(serviceNode); else if (targetTypes.contains(type)) selectedServices.add(serviceNode); addServiceType(serviceTypes, serviceNode, type); } // extract local IDs Map serviceLocalIDs = extractElementsByParent(XRD_NS, XRD_ELEM_LOCALID, selectedServices, document); Map serviceDelegates = extractElementsByParent(OPENID_NS, OPENID_ELEM_DELEGATE, selectedServices, document); // build XrdsServiceEndpoints for all URIs in the found services List result = new ArrayList(); NodeList uris = document.getElementsByTagNameNS(XRD_NS, XRD_ELEM_URI); Node uriNode; for (int i = 0; i < uris.getLength(); i++) { uriNode = uris.item(i); if (uriNode == null || !selectedServices.contains(uriNode.getParentNode())) continue; String uri = uriNode.getFirstChild() != null && uriNode.getFirstChild().getNodeType() == Node.TEXT_NODE ? uriNode.getFirstChild().getNodeValue() : null; serviceNode = uriNode.getParentNode(); Set typeSet = (Set) serviceTypes.get(serviceNode); String localId = (String) serviceLocalIDs.get(serviceNode); String delegate = (String) serviceDelegates.get(serviceNode); XrdsServiceElem endpoint = new XrdsServiceElem(uri, typeSet, getPriority(serviceNode), getPriority(uriNode), localId, delegate, canonicalId); result.add(endpoint); } Collections.sort(result); return result; }
From source file:Main.java
private static void renderNode(StringBuffer sb, Node node, String margin, String indent, String lab, String rab, String nl) {/* w w w. j a v a 2s. c o m*/ if (node == null) { sb.append("null"); return; } switch (node.getNodeType()) { case Node.DOCUMENT_NODE: //sb.append(margin + lab +"?xml version=\"1.0\" encoding=\"UTF-8\"?" + rab + nl); Node root = ((Document) node).getDocumentElement(); renderNode(sb, root, margin, indent, lab, rab, nl); break; case Node.ELEMENT_NODE: String name = getNodeNameWithNamespace(node); NodeList children = node.getChildNodes(); int nChildren = children.getLength(); NamedNodeMap attributes = node.getAttributes(); int nAttrs = attributes.getLength(); boolean singleShortTextChild = (nAttrs == 0) && (nChildren == 1) && (children.item(0).getNodeType() == Node.TEXT_NODE) && (children.item(0).getTextContent().length() < 70) && (!children.item(0).getTextContent().contains("\n")); if (singleShortTextChild) { sb.append(margin + lab + name + ((nChildren == 0) ? "/" : "") + rab); } else if (nAttrs == 0 && !singleShortTextChild) { sb.append(margin + lab + name + ((nChildren == 0) ? "/" : "") + rab + nl); } else if (nAttrs == 1) { Node attr = attributes.item(0); String attrName = getNodeNameWithNamespace(attr); sb.append(margin + lab + name + " " + attrName + "=\"" + escapeChars(attr.getNodeValue()) + "\"" + ((nChildren == 0) ? "/" : "") + rab + nl); } else { sb.append(margin + lab + name + nl); for (int i = 0; i < nAttrs; i++) { Node attr = attributes.item(i); String attrName = getNodeNameWithNamespace(attr); sb.append(margin + indent + attrName + "=\"" + escapeChars(attr.getNodeValue())); if (i < nAttrs - 1) sb.append("\"" + nl); else sb.append("\"" + ((nChildren == 0) ? "/" : "") + rab + nl); } } if (singleShortTextChild) { String text = escapeChars(node.getTextContent()); sb.append(text.trim()); sb.append(lab + "/" + name + rab + nl); } else { for (int i = 0; i < nChildren; i++) { renderNode(sb, children.item(i), margin + indent, indent, lab, rab, nl); } } if (nChildren != 0 && !singleShortTextChild) sb.append(margin + lab + "/" + name + rab + nl); break; case Node.TEXT_NODE: String text = escapeChars(node.getNodeValue()); String[] lines = text.split("\n"); for (String line : lines) { line = line.trim(); if (!line.equals("")) sb.append(margin + line + nl); } break; case Node.CDATA_SECTION_NODE: String cdataText = node.getNodeValue(); String[] cdataLines = cdataText.split("\n"); sb.append(margin + lab + "![CDATA[" + nl); for (String line : cdataLines) { line = line.trim(); if (!line.equals("")) sb.append(margin + indent + line + nl); } sb.append(margin + "]]" + rab + nl); break; case Node.PROCESSING_INSTRUCTION_NODE: sb.append(margin + lab + "?" + node.getNodeName() + " " + escapeChars(node.getNodeValue()) + "?" + rab + nl); break; case Node.ENTITY_REFERENCE_NODE: sb.append("&" + node.getNodeName() + ";"); break; case Node.DOCUMENT_TYPE_NODE: // Ignore document type nodes break; case Node.COMMENT_NODE: sb.append(margin + lab + "!--" + node.getNodeValue() + "--" + rab + nl); break; } return; }
From source file:org.gvnix.dynamic.configuration.roo.addon.config.XmlDynamicConfiguration.java
/** * The node is a valid type to be generated as dynamic property ? * <p>//from w ww.java 2s. c om * TEXT_NODE, TEXT_NODE and ATTRIBUTE_NODE nodes are valid. * </p> * * @param node Node to check * @return Has valid format */ protected boolean isValidNode(Node node) { short type = node.getNodeType(); if (type == Node.ELEMENT_NODE || type == Node.TEXT_NODE || type == Node.ATTRIBUTE_NODE) { return true; } return false; }
From source file:edu.indiana.lib.twinpeaks.util.DomUtils.java
/** * Get the text associated with this element at this level only, or * recursivley, searching through all child elements * @param parent the node containing text * @param recursiveSearch Search all child elements? * @return Text (trimmed of leading/trailing whitespace, null if none) *//*from w w w . j ava 2 s . c om*/ public static String textSearch(Node parent, boolean recursiveSearch) { String text = null; if (parent != null) { for (Node child = parent.getFirstChild(); child != null; child = child.getNextSibling()) { switch (child.getNodeType()) { case Node.TEXT_NODE: text = normalizeText(text, child.getNodeValue()); break; case Node.ELEMENT_NODE: if (recursiveSearch) { text = normalizeText(text, getText(child)); } break; default: break; } } } return text == null ? text : text.trim(); }
From source file:Main.java
/** * Performs the actual recursive dumping of a DOM tree to a given * <CODE>PrintWriter</CODE>. Note that dump is intended to be a detailed * debugging aid rather than pretty to look at. * /* w w w. j a va2 s. co m*/ * @param out The <CODE>PrintWriter</CODE> to write to. * @param node The <CODE>Node</CODE> under consideration. * @param indent The level of indentation. * @see #dump(PrintWriter, Node) * @since TFP 1.0 */ private static void doDump(PrintWriter out, final Node node, int indent) { if (node != null) { for (int index = 0; index < indent; ++index) out.write(' '); switch (node.getNodeType()) { case Node.DOCUMENT_NODE: { Document document = (Document) node; out.println("DOCUMENT:"); doDump(out, document.getDoctype(), indent + 1); doDump(out, document.getDocumentElement(), indent + 1); break; } case Node.DOCUMENT_TYPE_NODE: { DocumentType type = (DocumentType) node; out.println("DOCTYPE: [" + "name=" + format(type.getName()) + "," + "publicId=" + format(type.getPublicId()) + "," + "systemId=" + format(type.getSystemId()) + "]"); break; } case Node.ELEMENT_NODE: { Element element = (Element) node; out.println("ELEMENT: [" + "ns=" + format(element.getNamespaceURI()) + "," + "name=" + format(element.getLocalName()) + "]"); NamedNodeMap attrs = element.getAttributes(); for (int index = 0; index < attrs.getLength(); ++index) doDump(out, attrs.item(index), indent + 1); for (Node child = element.getFirstChild(); child != null;) { doDump(out, child, indent + 1); child = child.getNextSibling(); } break; } case Node.ATTRIBUTE_NODE: { Attr attr = (Attr) node; out.println("ATTRIBUTE: [" + "ns=" + format(attr.getNamespaceURI()) + "," + "prefix=" + format(attr.getPrefix()) + "," + "name=" + format(attr.getLocalName()) + "," + "value=" + format(attr.getNodeValue()) + "]"); break; } case Node.TEXT_NODE: { Text text = (Text) node; out.println("TEXT: [" + format(text.getNodeValue()) + "]"); for (Node child = text.getFirstChild(); child != null;) { doDump(out, child, indent + 1); child = child.getNextSibling(); } break; } case Node.CDATA_SECTION_NODE: { CDATASection data = (CDATASection) node; out.println("CDATA: [" + format(data.getNodeValue()) + "]"); break; } case Node.COMMENT_NODE: { Comment comm = (Comment) node; out.println("COMMENT: [" + format(comm.getNodeValue()) + "]"); break; } default: out.println("UNKNOWN: [type=" + node.getNodeType() + "]"); break; } } }
From source file:com.mtgi.analytics.XmlBehaviorEventPersisterTest.java
@Test public void testNestedEvents() throws InterruptedException, IOException, XMLStreamException, SAXException { //we reuse the test event creation code from jdbc persister test to get ourselves an interesting dataset. final ArrayList<BehaviorEvent> events = new ArrayList<BehaviorEvent>(); int[] counter = { 0 }; for (int i = 0; i < 3; ++i) JdbcBehaviorEventPersisterTest.createEvent(null, 1, 3, 3, counter, events); LinkedList<BehaviorEvent> queue = new LinkedList<BehaviorEvent>(events); persister.persist(queue);/*from w w w. j a v a2 s . com*/ assertEquals("queue unmodified by persistence operation", 39, queue.size()); assertEquals("persister reports correct file size", file.length(), persister.getFileSize()); //now perform verification of log data against the expected results. String fileName = persister.rotateLog(); Document actualXML = buildTestDocument(FileUtils.readFileToString(new File(fileName))); //read up the expected results for comparison. InputStream expectedData = XmlBehaviorEventPersisterTest.class .getResourceAsStream("XmlBehaviorEventPersisterTest.testNestedEvents-result.xml"); Document controlXML = buildTestDocument(new InputSource(expectedData)); expectedData.close(); XMLUnit.setIgnoreWhitespace(true); //compare the logged data to our expectations, ignoring time-sensitive values. Diff diff = new Diff(controlXML, actualXML); diff.overrideDifferenceListener(new DifferenceListener() { //filter out artificial differences in id, start time, duration public int differenceFound(Difference diff) { Node cn = diff.getControlNodeDetail().getNode(); Node tn = diff.getTestNodeDetail().getNode(); if (cn != null && tn != null) { short ctype = cn.getNodeType(); short ttype = tn.getNodeType(); if (ctype == ttype) { if (ctype == Node.ATTRIBUTE_NODE) { if (cn.getNodeName().equals("id") || cn.getNodeName().equals("parent-id")) { //we can at least verify that the logged ID matches the ID assigned to our data model. int index = -1; for (Node n = ((Attr) cn).getOwnerElement(); n != null; n = n.getPreviousSibling()) if (n.getNodeType() == Node.ELEMENT_NODE) ++index; BehaviorEvent event = events.get(index); if (cn.getNodeName().equals("id")) { assertEquals("logged event has same id as data model", event.getId().toString(), tn.getNodeValue()); } else { BehaviorEvent parent = event.getParent(); assertNotNull("node " + event.getId() + " has parent", parent); assertEquals("logged event has same parent-id as data model", parent.getId().toString(), tn.getNodeValue()); } return RETURN_IGNORE_DIFFERENCE_NODES_SIMILAR; } } else if (ctype == Node.TEXT_NODE) { String cname = cn.getParentNode().getNodeName(); String tname = tn.getParentNode().getNodeName(); if (cname.equals(tname)) { //TODO: sanity check values. if ("duration-ns".equals(cname) || "start".equals(cname)) return RETURN_IGNORE_DIFFERENCE_NODES_SIMILAR; } } } } return RETURN_ACCEPT_DIFFERENCE; } public void skippedComparison(Node n0, Node n1) { } }); if (!diff.similar()) fail(diff.appendMessage(new StringBuffer()).toString()); }
From source file:DOMTreeFull.java
public static String toString(Node node) { StringBuffer sb = new StringBuffer(); // is there anything to do? if (node == null) { return ""; }// w w w.j a v a2s . co m int type = node.getNodeType(); sb.append(whatArray[type]); sb.append(" : "); sb.append(node.getNodeName()); String value = node.getNodeValue(); if (value != null) { sb.append(" Value: \""); sb.append(value); sb.append("\""); } switch (type) { // document case Node.DOCUMENT_NODE: { break; } // element with attributes case Node.ELEMENT_NODE: { Attr attrs[] = sortAttributes(node.getAttributes()); if (attrs.length > 0) sb.append(" ATTRS:"); for (int i = 0; i < attrs.length; i++) { Attr attr = attrs[i]; sb.append(' '); sb.append(attr.getNodeName()); sb.append("=\""); sb.append(normalize(attr.getNodeValue())); sb.append('"'); } sb.append('>'); break; } // handle entity reference nodes case Node.ENTITY_REFERENCE_NODE: { break; } // cdata sections case Node.CDATA_SECTION_NODE: { break; } // text case Node.TEXT_NODE: { break; } // processing instruction case Node.PROCESSING_INSTRUCTION_NODE: { break; } // comment node case Node.COMMENT_NODE: { break; } // DOCTYPE node case Node.DOCUMENT_TYPE_NODE: { break; } // Notation node case Node.NOTATION_NODE: { sb.append("public:"); String id = ((Notation) node).getPublicId(); if (id == null) { sb.append("PUBLIC "); sb.append(id); sb.append(" "); } id = ((Notation) node).getSystemId(); if (id == null) { sb.append("system: "); sb.append(id); sb.append(" "); } break; } } return sb.toString(); }
From source file:com.amalto.core.save.context.BeforeSaving.java
public boolean validateFormat(String msg) { NodeList nodeList;// w w w . j a v a2 s.c o m try { Document document = Util.parse(msg.toLowerCase()); nodeList = Util.getNodeList(document, "//report/message"); //$NON-NLS-1$ } catch (Exception e) { return false; } if (nodeList.getLength() != 1) { return false; } Node reportNode = nodeList.item(0); if (reportNode.getNodeType() != Node.ELEMENT_NODE) { return false; } NamedNodeMap attrMap = reportNode.getAttributes(); Node attribute = attrMap.getNamedItem("type"); //$NON-NLS-1$ if (attribute == null) { return false; } if (!"info".equalsIgnoreCase(attribute.getNodeValue()) //$NON-NLS-1$ && !"error".equalsIgnoreCase(attribute.getNodeValue())) { //$NON-NLS-1$ return false; } NodeList messageNodeList = reportNode.getChildNodes(); if (messageNodeList.getLength() > 1) { return false; } if (messageNodeList.getLength() == 0) { return true; } Node messageNode = messageNodeList.item(0); return messageNode.getNodeType() == Node.TEXT_NODE; }
From source file:com.gargoylesoftware.htmlunit.xml.XmlUtil.java
private static DomNode createFrom(final SgmlPage page, final Node source, final boolean handleXHTMLAsHTML) { if (source.getNodeType() == Node.TEXT_NODE) { return new DomText(page, source.getNodeValue()); }/*from w ww . j a va 2s. c om*/ if (source.getNodeType() == Node.PROCESSING_INSTRUCTION_NODE) { return new DomProcessingInstruction(page, source.getNodeName(), source.getNodeValue()); } if (source.getNodeType() == Node.COMMENT_NODE) { return new DomComment(page, source.getNodeValue()); } if (source.getNodeType() == Node.DOCUMENT_TYPE_NODE) { final DocumentType documentType = (DocumentType) source; return new DomDocumentType(page, documentType.getName(), documentType.getPublicId(), documentType.getSystemId()); } final String ns = source.getNamespaceURI(); String localName = source.getLocalName(); if (handleXHTMLAsHTML && HTMLParser.XHTML_NAMESPACE.equals(ns)) { final ElementFactory factory = HTMLParser.getFactory(localName); return factory.createElementNS(page, ns, localName, namedNodeMapToSaxAttributes(source.getAttributes())); } final NamedNodeMap nodeAttributes = source.getAttributes(); if (page != null && page.isHtmlPage()) { localName = localName.toUpperCase(Locale.ROOT); } final String qualifiedName; if (source.getPrefix() == null) { qualifiedName = localName; } else { qualifiedName = source.getPrefix() + ':' + localName; } final String namespaceURI = source.getNamespaceURI(); if (HTMLParser.SVG_NAMESPACE.equals(namespaceURI)) { return HTMLParser.SVG_FACTORY.createElementNS(page, namespaceURI, qualifiedName, namedNodeMapToSaxAttributes(nodeAttributes)); } final Map<String, DomAttr> attributes = new LinkedHashMap<>(); for (int i = 0; i < nodeAttributes.getLength(); i++) { final Attr attribute = (Attr) nodeAttributes.item(i); final String attributeNamespaceURI = attribute.getNamespaceURI(); final String attributeQualifiedName; if (attribute.getPrefix() != null) { attributeQualifiedName = attribute.getPrefix() + ':' + attribute.getLocalName(); } else { attributeQualifiedName = attribute.getLocalName(); } final String value = attribute.getNodeValue(); final boolean specified = attribute.getSpecified(); final DomAttr xmlAttribute = new DomAttr(page, attributeNamespaceURI, attributeQualifiedName, value, specified); attributes.put(attribute.getNodeName(), xmlAttribute); } return new DomElement(namespaceURI, qualifiedName, page, attributes); }