use of org.w3c.dom.Comment in project aries by apache.
the class CmNamespaceHandler method getTextValue.
private static String getTextValue(Element element) {
StringBuffer value = new StringBuffer();
NodeList nl = element.getChildNodes();
for (int i = 0; i < nl.getLength(); i++) {
Node item = nl.item(i);
if ((item instanceof CharacterData && !(item instanceof Comment)) || item instanceof EntityReference) {
value.append(item.getNodeValue());
}
}
return value.toString();
}
use of org.w3c.dom.Comment in project karaf by apache.
the class NamespaceHandler method getTextValue.
private static String getTextValue(Element element) {
StringBuffer value = new StringBuffer();
NodeList nl = element.getChildNodes();
for (int i = 0; i < nl.getLength(); i++) {
Node item = nl.item(i);
if ((item instanceof CharacterData && !(item instanceof Comment)) || item instanceof EntityReference) {
value.append(item.getNodeValue());
}
}
return value.toString();
}
use of org.w3c.dom.Comment in project sling by apache.
the class MyErrorHandler method convert.
// ------------------------------------------------------ Protected Methods
/**
* Create and return a TreeNode that corresponds to the specified Node,
* including processing all of the attributes and children nodes.
*
* @param parent The parent TreeNode (if any) for the new TreeNode
* @param node The XML document Node to be converted
*/
protected TreeNode convert(TreeNode parent, Node node) {
// Construct a new TreeNode for this node
TreeNode treeNode = new TreeNode(node.getNodeName(), parent);
// Convert all attributes of this node
NamedNodeMap attributes = node.getAttributes();
if (attributes != null) {
int n = attributes.getLength();
for (int i = 0; i < n; i++) {
Node attribute = attributes.item(i);
treeNode.addAttribute(attribute.getNodeName(), attribute.getNodeValue());
}
}
// Create and attach all children of this node
NodeList children = node.getChildNodes();
if (children != null) {
int n = children.getLength();
for (int i = 0; i < n; i++) {
Node child = children.item(i);
if (child instanceof Comment)
continue;
if (child instanceof Text) {
String body = ((Text) child).getData();
if (body != null) {
body = body.trim();
if (body.length() > 0)
treeNode.setBody(body);
}
} else {
TreeNode treeChild = convert(treeNode, child);
}
}
}
// Return the completed TreeNode graph
return (treeNode);
}
use of org.w3c.dom.Comment in project tomee by apache.
the class SuperProperties method loadFromXML.
public synchronized void loadFromXML(final InputStream in) throws IOException {
if (in == null) {
throw new NullPointerException();
}
final DocumentBuilder builder = getDocumentBuilder();
try {
final Document doc = builder.parse(in);
final NodeList entries = doc.getElementsByTagName("entry");
if (entries == null) {
return;
}
final int entriesListLength = entries.getLength();
for (int i = 0; i < entriesListLength; i++) {
final Element entry = (Element) entries.item(i);
final String key = entry.getAttribute("key");
final String value = entry.getTextContent();
put(key, value);
// search backwards for a comment
for (Node node = entry.getPreviousSibling(); node != null && !(node instanceof Element); node = node.getPreviousSibling()) {
if (node instanceof Comment) {
final InputStream cin = new ByteArrayInputStream(((Comment) node).getData().getBytes());
// read comment line by line
final StringBuilder comment = new StringBuilder();
final LinkedHashMap<String, String> attributes = new LinkedHashMap<String, String>();
int nextByte;
char nextChar;
boolean firstLine = true;
int commentIndent = Integer.MAX_VALUE;
do {
// read one line
final StringBuilder commentLine = new StringBuilder();
int commentLineIndent = 0;
boolean inIndent = true;
while (true) {
nextByte = cin.read();
if (nextByte < 0) {
break;
}
// & 0xff
nextChar = (char) nextByte;
if (inIndent && nextChar == ' ') {
commentLineIndent++;
commentLine.append(' ');
} else if (inIndent && nextChar == '\t') {
commentLineIndent += 4;
commentLine.append(" ");
} else if (nextChar == '\r' || nextChar == '\n') {
break;
} else {
inIndent = false;
commentLine.append(nextChar);
}
}
// Determine indent
if (!firstLine && commentIndent == Integer.MAX_VALUE && commentLine.length() > 0) {
// if this is a new comment block, the comment indent size for this
// block is based the first full line of the comment (ignoring the
// line with the <!--
commentIndent = commentLineIndent;
}
commentLineIndent = Math.min(commentIndent, commentLineIndent);
if (commentLine.toString().trim().startsWith("@")) {
// process property attribute
final String attribute = commentLine.toString().trim().substring(1);
final String[] parts = attribute.split("=", 2);
final String attributeName = parts[0].trim();
final String attributeValue = parts.length == 2 ? parts[1].trim() : "";
attributes.put(attributeName, attributeValue);
} else {
// append comment
if (comment.length() != 0) {
comment.append(lineSeparator);
}
comment.append(commentLine.toString().substring(commentLineIndent));
}
firstLine = false;
} while (nextByte > 0);
if (comment.length() > 0) {
setComment(key, comment.toString());
}
this.attributes.put(normalize(key), attributes);
break;
}
}
}
} catch (final SAXException e) {
throw new InvalidPropertiesFormatException(e);
}
}
use of org.w3c.dom.Comment in project stanbol by apache.
the class DomSerializer2 method createSubnodes.
private void createSubnodes(Document document, Element element, List tagChildren) {
if (tagChildren != null) {
Iterator it = tagChildren.iterator();
while (it.hasNext()) {
Object item = it.next();
if (item instanceof CommentToken) {
CommentToken commentNode = (CommentToken) item;
Comment comment = document.createComment(commentNode.getContent().toString());
element.appendChild(comment);
} else if (item instanceof ContentToken) {
ContentToken contentToken = (ContentToken) item;
String content = contentToken.getContent();
String nodeName = element.getNodeName();
boolean specialCase = props.isUseCdataForScriptAndStyle() && ("script".equalsIgnoreCase(nodeName) || "style".equalsIgnoreCase(nodeName));
if (escapeXml && !specialCase) {
content = escapeXml(content, props, true);
}
element.appendChild(specialCase ? document.createCDATASection(content) : document.createTextNode(content));
} else if (item instanceof TagNode) {
TagNode subTagNode = (TagNode) item;
Element subelement = document.createElement(subTagNode.getName());
;
setAttributes(subTagNode, subelement);
// recursively create subnodes
createSubnodes(document, subelement, subTagNode.getChildren());
element.appendChild(subelement);
} else if (item instanceof List) {
List sublist = (List) item;
createSubnodes(document, element, sublist);
}
}
}
}
Aggregations