List of usage examples for org.w3c.dom Node CDATA_SECTION_NODE
short CDATA_SECTION_NODE
To view the source code for org.w3c.dom Node CDATA_SECTION_NODE.
Click Source Link
CDATASection
. From source file:bridge.toolkit.commands.S1000DConverter.java
/** * Iterate through the DOM tree//from w w w.j ava 2s . c o m * * @param node * @param output * @throws IOException */ public static void writeNode(Node node, Writer output) throws IOException { int type = node.getNodeType(); switch (type) { case Node.ATTRIBUTE_NODE: output.write(' '); output.write(node.getNodeName()); output.write("=\""); writeXMLData(node.getNodeValue(), true, output); output.write('"'); break; case Node.CDATA_SECTION_NODE: case Node.TEXT_NODE: writeXMLData(node.getNodeValue(), false, output); break; case Node.COMMENT_NODE: output.write("<!--"); output.write(((Comment) node).getNodeValue()); output.write("-->"); break; case Node.DOCUMENT_FRAGMENT_NODE: writeNodes(node.getChildNodes(), output); break; case Node.DOCUMENT_NODE: writeNodes(node.getChildNodes(), output); break; case Node.DOCUMENT_TYPE_NODE: break; case Node.ELEMENT_NODE: { NamedNodeMap atts = node.getAttributes(); output.write('<'); output.write(node.getNodeName()); if (atts != null) { int length = atts.getLength(); for (int i = 0; i < length; i++) writeNode(atts.item(i), output); } if (node.hasChildNodes()) { output.write('>'); writeNodes(node.getChildNodes(), output); output.write("</"); output.write(node.getNodeName()); output.write('>'); } else { output.write("/>"); } break; } case Node.ENTITY_NODE: break; case Node.ENTITY_REFERENCE_NODE: writeNodes(node.getChildNodes(), output); break; case Node.NOTATION_NODE: break; case Node.PROCESSING_INSTRUCTION_NODE: break; default: throw new Error("Unexpected DOM node type: " + type); } }
From source file:org.adl.samplerte.server.LMSManifestHandler.java
License:asdf
/**************************************************************************** ** //from w ww . ja v a2 s .c om ** Method: getText() Input: Node node - The current node Output: String - * The text of the desired node. Description: This method gets the child * text node of the node that is passed in for input. Side Effects: ** ****************************************************************************/ public String getText(Node node) { String value = new String(); NodeList kids = node.getChildNodes(); // cycle through all children of node to get the text if (kids != null) { for (int i = 0; i < kids.getLength(); i++) { // make sure we have a text element if ((kids.item(i).getNodeType() == Node.TEXT_NODE) || (kids.item(i).getNodeType() == Node.CDATA_SECTION_NODE)) { value = value + kids.item(i).getNodeValue().trim(); } } } else { if (DebugIndicator.ON) { System.out.println("node has no kids"); } } return value; }
From source file:com.draagon.meta.loader.xml.XMLFileMetaDataLoader.java
/** * Parse the MetaAttribute Value // w ww .j a v a2s . co m */ protected void parseMetaAttributeValue(MetaAttribute attr, Element el) { /////////////////////////////////////////////////// // Get the Node value // Get the first node Node nv = el.getFirstChild(); // Loop through and ignore the comments while (nv != null && nv.getNodeType() == Node.COMMENT_NODE) { nv.getNextSibling(); } // If a valid node exists, then get the data if (nv != null) { switch (nv.getNodeType()) { // If CDATA just set the whole thing case Node.CDATA_SECTION_NODE: attr.setValueAsString(((CharacterData) nv).getData()); break; // If an Element just pass it in for parsing case Node.ELEMENT_NODE: attr.setValue(nv); break; // If just text, then pass it in case Node.TEXT_NODE: attr.setValueAsString(nv.getNodeValue()); break; default: log.warn("Unsupported Node Type for node [" + nv + "]"); } } }
From source file:com.concursive.connect.web.modules.wiki.utils.HTMLToWikiUtils.java
public static void processTable(NodeList nodeList, StringBuffer sb, int rowCount, boolean doText, boolean anyNodeType, String contextPath, int projectId, int pass) { LOG.trace("line reset"); for (int i = 0; i < nodeList.getLength(); i++) { Node n = nodeList.item(i); if (n != null) { if (n.getNodeType() == Node.TEXT_NODE || n.getNodeType() == Node.CDATA_SECTION_NODE) { if (doText && anyNodeType) { if (StringUtils.hasText(n.getNodeValue())) { String thisLine = StringUtils.fromHtmlValue(n.getNodeValue()); LOG.trace("table - text: " + thisLine); sb.append(thisLine); }//from w ww . j a v a2 s .c o m } } else if (n.getNodeType() == Node.ELEMENT_NODE) { Element element = ((Element) n); String tag = element.getTagName(); if ("tr".equals(tag)) { LOG.trace("table - tr"); ++rowCount; if (rowCount == 1) { LOG.debug("Looking for style"); processTable(n.getChildNodes(), sb, rowCount, false, false, contextPath, projectId, 1); } processTable(n.getChildNodes(), sb, rowCount, false, false, contextPath, projectId, 2); sb.append(CRLF); } else if ("td".equals(tag) || "th".equals(tag)) { if (LOG.isTraceEnabled()) { LOG.trace("table - " + tag + " - " + i + " " + hasNonTextNodes(n.getChildNodes()) + " - pass " + pass); } String separator = "|"; if (tag.equals("th")) { separator = "||"; } // Determin how many columns are spanned by this column int colspan = 1; if (element.hasAttribute("colspan")) { colspan = Integer.parseInt(element.getAttribute("colspan")); } // if (sb.toString().endsWith(separator)) { // sb.append(" "); // } // Style pass boolean hasStyle = false; if (pass == 1) { if (element.hasAttribute("style")) { String style = element.getAttribute("style"); if (style.endsWith(";")) { style = style.substring(0, style.length() - 1); } // Start the wiki markup for (int sI = 0; sI < colspan; sI++) { sb.append(separator); } // Append the style data sb.append("{").append(style).append("}"); hasStyle = true; } // Close the wiki markup if the last cell if (hasStyle) { if (i + 1 == nodeList.getLength()) { sb.append(separator); // The style pass needs to add it's own CRLF sb.append(CRLF); } } } // Data pass if (pass == 2) { // Start the wiki markup for (int sI = 0; sI < colspan; sI++) { sb.append(separator); } if (n.getChildNodes().getLength() > 0) { // Cell data uses a "!" for each return if (hasNonTextNodes(n.getChildNodes())) { processChildNodes(getNodeList(n), sb, 0, true, true, false, "!", contextPath, projectId); } else { processChildNodes(getNodeList(n), sb, 0, true, true, false, "!", contextPath, projectId); } // If the cell didn't have any data, then add a space if (sb.toString().endsWith(separator)) { sb.append(" "); } } else { sb.append(" "); } // Close the wiki markup if (i + 1 == nodeList.getLength()) { sb.append(separator); } } } else { LOG.trace("table - text - ** " + tag); processTable(n.getChildNodes(), sb, rowCount, true, true, contextPath, projectId, 0); } } } } }
From source file:DomUtils.java
/** * Construct the XPath of the supplied DOM Node. * <p/>/*w ww .j av a 2 s . c o m*/ * Supports element, comment and cdata sections DOM Node types. * @param node DOM node for XPath generation. * @return XPath string representation of the supplied DOM Node. */ public static String getXPath(Node node) { StringBuffer xpath = new StringBuffer(); Node parent = node.getParentNode(); switch (node.getNodeType()) { case Node.ELEMENT_NODE: xpath.append(getXPathToken((Element) node)); break; case Node.COMMENT_NODE: int commentNum = DomUtils.countNodesBefore(node, Node.COMMENT_NODE); xpath.append("/{COMMENT}[" + commentNum + 1 + "]"); break; case Node.CDATA_SECTION_NODE: int cdataNum = DomUtils.countNodesBefore(node, Node.CDATA_SECTION_NODE); xpath.append("/{CDATA}[" + cdataNum + 1 + "]"); break; default: throw new UnsupportedOperationException( "XPath generation for supplied DOM Node type not supported. Only supports element, comment and cdata section DOM nodes."); } while (parent != null && parent.getNodeType() == Node.ELEMENT_NODE) { xpath.insert(0, getXPathToken((Element) parent)); parent = parent.getParentNode(); } return xpath.toString(); }
From source file:com.twinsoft.convertigo.engine.translators.WebServiceTranslator.java
private SOAPElement addSoapElement(Context context, SOAPEnvelope se, SOAPElement soapParent, Node node) throws Exception { String prefix = node.getPrefix(); String namespace = node.getNamespaceURI(); String nodeName = node.getNodeName(); String localName = node.getLocalName(); String value = node.getNodeValue(); boolean includeResponseElement = true; if (context.sequenceName != null) { includeResponseElement = ((Sequence) context.requestedObject).isIncludeResponseElement(); }/*from w ww .j av a2 s .c o m*/ SOAPElement soapElement = null; if (node.getNodeType() == Node.ELEMENT_NODE) { Element element = (Element) node; boolean toAdd = true; if (!includeResponseElement && "response".equalsIgnoreCase(localName)) { toAdd = false; } if ("http://schemas.xmlsoap.org/soap/envelope/".equals(element.getParentNode().getNamespaceURI()) || "http://schemas.xmlsoap.org/soap/envelope/".equals(namespace) || nodeName.toLowerCase().endsWith(":envelope") || nodeName.toLowerCase().endsWith(":body") //element.getParentNode().getNodeName().toUpperCase().indexOf("NS0:") != -1 || //nodeName.toUpperCase().indexOf("NS0:") != -1 ) { toAdd = false; } if (toAdd) { if (prefix == null || prefix.equals("")) { soapElement = soapParent.addChildElement(nodeName); } else { soapElement = soapParent.addChildElement(se.createName(localName, prefix, namespace)); } } else { soapElement = soapParent; } if (soapElement != null) { if (soapParent.equals(se.getBody()) && !soapParent.equals(soapElement)) { if (XsdForm.qualified == context.project.getSchemaElementForm()) { if (soapElement.getAttribute("xmlns") == null) { soapElement.addAttribute(se.createName("xmlns"), context.project.getTargetNamespace()); } } } if (element.hasAttributes()) { String attrType = element.getAttribute("type"); if (("attachment").equals(attrType)) { if (context.requestedObject instanceof AbstractHttpTransaction) { AttachmentDetails attachment = AttachmentManager.getAttachment(element); if (attachment != null) { byte[] raw = attachment.getData(); if (raw != null) soapElement.addTextNode(Base64.encodeBase64String(raw)); } /* DON'T WORK YET *\ AttachmentPart ap = responseMessage.createAttachmentPart(new ByteArrayInputStream(raw), element.getAttribute("content-type")); ap.setContentId(key); ap.setContentLocation(element.getAttribute("url")); responseMessage.addAttachmentPart(ap); \* DON'T WORK YET */ } } if (!includeResponseElement && "response".equalsIgnoreCase(localName)) { // do not add attributes } else { NamedNodeMap attributes = element.getAttributes(); int len = attributes.getLength(); for (int i = 0; i < len; i++) { Node item = attributes.item(i); addSoapElement(context, se, soapElement, item); } } } if (element.hasChildNodes()) { NodeList childNodes = element.getChildNodes(); int len = childNodes.getLength(); for (int i = 0; i < len; i++) { Node item = childNodes.item(i); switch (item.getNodeType()) { case Node.ELEMENT_NODE: addSoapElement(context, se, soapElement, item); break; case Node.CDATA_SECTION_NODE: case Node.TEXT_NODE: String text = item.getNodeValue(); text = (text == null) ? "" : text; soapElement.addTextNode(text); break; default: break; } } } } } else if (node.getNodeType() == Node.ATTRIBUTE_NODE) { if (prefix == null || prefix.equals("")) { soapElement = soapParent.addAttribute(se.createName(nodeName), value); } else { soapElement = soapParent.addAttribute(se.createName(localName, prefix, namespace), value); } } return soapElement; }
From source file:com.esri.geoevent.solutions.adapter.cot.CoTAdapterInbound.java
public static String elementToString(Node n) throws Exception { try {/*from ww w . j a va 2 s . c om*/ String name = n.getNodeName(); short type = n.getNodeType(); if (Node.CDATA_SECTION_NODE == type) { return "<![CDATA[" + n.getNodeValue() + "]]>"; } if (name.startsWith("#")) { return ""; } StringBuffer sb = new StringBuffer(); sb.append('<').append(name); NamedNodeMap attrs = n.getAttributes(); if (attrs != null) { for (int i = 0; i < attrs.getLength(); i++) { Node attr = attrs.item(i); sb.append(' ').append(attr.getNodeName()).append("=\"").append(attr.getNodeValue()) .append("\""); } } String textContent = null; NodeList children = n.getChildNodes(); if (children.getLength() == 0) { if ((textContent = n.getTextContent()) != null && !"".equals(textContent)) { sb.append(textContent).append("</").append(name).append('>'); } else { sb.append("/>").append('\n'); } } else { sb.append('>').append('\n'); boolean hasValidChildren = false; for (int i = 0; i < children.getLength(); i++) { String childToString = elementToString(children.item(i)); if (!"".equals(childToString)) { sb.append(childToString); hasValidChildren = true; } } if (!hasValidChildren && ((textContent = n.getTextContent()) != null)) { sb.append(textContent); } sb.append("</").append(name).append('>'); } return sb.toString(); } catch (Exception e) { log.error(e); log.error(e.getStackTrace()); throw (e); } }
From source file:DOMProcessor.java
/** Converts the given DOM node into XML. Recursively converts * any child nodes. This version allows the XML version, encoding and stand-alone * status to be set./*w ww . ja v a2s. c o m*/ * @param node DOM Node to display. * @param version XML version, or null if default ('1.0') is to be used. * @param encoding XML encoding, or null if encoding is not to be specified. * @param standalone XML stand-alone status or null if not to be specified. */ private void outputNodeAsXML(Node node, String version, String encoding, Boolean standalone) { // Store node name, type and value. String name = node.getNodeName(), value = makeFriendly(node.getNodeValue()); int type = node.getNodeType(); // Ignore empty nodes (e.g. blank lines etc.) if ((value != null) && (value.trim().equals(""))) { return; } switch (type) { case Node.DOCUMENT_NODE: // Start of document. { if (version == null) { out.print("<?xml version=\"1.0\" "); } else { out.print("<?xml version=\"" + version + "\" "); } if (encoding != null) { out.print("encoding=\"" + encoding + "\" "); } if (standalone != null) { if (standalone.booleanValue()) { out.print("standalone=\"yes\" "); } else { out.print("standalone=\"no\" "); } } out.println("?>"); // Output the document's child nodes. NodeList children = node.getChildNodes(); for (int i = 0; i < children.getLength(); i++) { outputNodeAsXML(children.item(i)); } break; } case Node.ELEMENT_NODE: // Document element with attributes. { // Output opening element tag. indent++; indent(); out.print("<" + name); // Output any attributes the element might have. NamedNodeMap attributes = node.getAttributes(); for (int i = 0; i < attributes.getLength(); i++) { Node attribute = attributes.item(i); out.print(" " + attribute.getNodeName() + "=\"" + attribute.getNodeValue() + "\""); } out.print(">"); // Output any child nodes that exist. NodeList children = node.getChildNodes(); for (int i = 0; i < children.getLength(); i++) { outputNodeAsXML(children.item(i)); } break; } case Node.CDATA_SECTION_NODE: // Display text. case Node.TEXT_NODE: { out.print(value); break; } case Node.COMMENT_NODE: // Comment node. { indent++; indent(); out.print("<!--" + value + "-->"); indent--; break; } case Node.ENTITY_REFERENCE_NODE: // Entity reference nodes. { indent++; indent(); out.print("&" + name + ";"); indent--; break; } case Node.PROCESSING_INSTRUCTION_NODE: // Processing instruction. { indent++; indent(); out.print("<?" + name); if ((value != null) && (value.length() > 0)) { out.print(" " + value); } out.println("?>"); indent--; break; } } // Finally output closing tags for each element. if (type == Node.ELEMENT_NODE) { out.print("</" + node.getNodeName() + ">"); indent--; if (node.getNextSibling() == null) { indent(); // Only throw new line if this is the last sibling. } } }
From source file:com.connexta.arbitro.ctx.xacml3.XACML3EvaluationCtx.java
private Set<String> getChildXPaths(Node root, String xPath) { Set<String> xPaths = new HashSet<String>(); NamespaceContext namespaceContext = null; XPathFactory factory = XPathFactory.newInstance(); XPath xpath = factory.newXPath(); if (namespaceContext == null) { //see if the request root is in a namespace String namespace = null;// www . j a va 2s.c o m if (root != null) { namespace = root.getNamespaceURI(); } // name spaces are used, so we need to lookup the correct // prefix to use in the search string NamedNodeMap namedNodeMap = root.getAttributes(); Map<String, String> nsMap = new HashMap<String, String>(); if (namedNodeMap != null) { for (int i = 0; i < namedNodeMap.getLength(); i++) { Node n = namedNodeMap.item(i); // we found the matching namespace, so get the prefix // and then break out String prefix = DOMHelper.getLocalName(n); String nodeValue = n.getNodeValue(); nsMap.put(prefix, nodeValue); } } // if there is not any namespace is defined for content element, default XACML request // name space would be there. if (XACMLConstants.REQUEST_CONTEXT_3_0_IDENTIFIER.equals(namespace) || XACMLConstants.REQUEST_CONTEXT_2_0_IDENTIFIER.equals(namespace) || XACMLConstants.REQUEST_CONTEXT_1_0_IDENTIFIER.equals(namespace)) { nsMap.put("xacml", namespace); } namespaceContext = new DefaultNamespaceContext(nsMap); } xpath.setNamespaceContext(namespaceContext); try { XPathExpression expression = xpath.compile(xPath); NodeList matches = (NodeList) expression.evaluate(root, XPathConstants.NODESET); if (matches != null && matches.getLength() > 0) { for (int i = 0; i < matches.getLength(); i++) { String text = null; Node node = matches.item(i); short nodeType = node.getNodeType(); // see if this is straight text, or a node with data under // it and then get the values accordingly if ((nodeType == Node.CDATA_SECTION_NODE) || (nodeType == Node.COMMENT_NODE) || (nodeType == Node.TEXT_NODE) || (nodeType == Node.ATTRIBUTE_NODE)) { // there is no child to this node text = node.getNodeValue(); } else { // the data is in a child node text = "/" + DOMHelper.getLocalName(node); } String newXPath = '(' + xPath + ")[" + (i + 1) + ']'; xPaths.add(newXPath); } } } catch (Exception e) { // TODO } return xPaths; }
From source file:com.enonic.esl.xml.XMLTool.java
public static String serialize(Node n, boolean includeSelf, String encoding) { DocumentFragment df = XMLTool.createDocument().createDocumentFragment(); NodeList children = n.getChildNodes(); // Check whether the child is a CDATA node Node firstChild = n.getFirstChild(); if (firstChild != null && firstChild.getNodeType() == Node.CDATA_SECTION_NODE) { return null; }// w ww .jav a2 s . c om if (includeSelf) { df.appendChild(df.getOwnerDocument().importNode(n, true)); } else { if (children == null || children.getLength() == 0) { return null; } // If only one node is found and it is a CDATA section, there is no need for serialization if (children.getLength() == 1 && children.item(0).getNodeType() == Node.CDATA_SECTION_NODE) { return null; } for (int i = 0; i < children.getLength(); i++) { df.appendChild(df.getOwnerDocument().importNode(children.item(i), true)); } } return serialize(df, 4); }