下面列出了org.w3c.dom.Text#getData ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Gets the title text of the HTML document.
*
* @rawDoc the DOM Element to extract title Node from
* @return the title text
*/
protected String getTitle(Element rawDoc) {
if (rawDoc == null) {
return null;
}
String title = "";
NodeList children = rawDoc.getElementsByTagName("title");
if (children.getLength() > 0) {
Element titleElement = ((Element) children.item(0));
Text text = (Text) titleElement.getFirstChild();
if (text != null) {
title = text.getData();
}
}
return title;
}
/**
* This method takes a node and removes any empty text node children from it.
* Empty text nodes are defined as text nodes containing a only spaces,
* \n characters, \r characters and \t characters.
* You should normalize the node before calling this method on the off chance
* it will trim important spaces from text.
* @param node
*/
private static void removeEmptyTextNodes(Node node) {
NodeList nodeList = node.getChildNodes();
int length = nodeList.getLength();
for(int i=length-1; i>=0; i--) {
Node child = nodeList.item(i);
if(child.getNodeType()==Node.TEXT_NODE) {
Text txt = (Text) child;
String data = txt.getData();
if(StringUtil.isSpace(data)) {
node.removeChild(child);
}
} else {
removeEmptyTextNodes(child);
}
}
}
public Component getTreeCellRendererComponent(JTree tree, Object value, boolean selected, boolean expanded, boolean leaf, int row, boolean hasFocus) {
Icon icon;
String label;
if (value instanceof Element) {
Element e = (Element) value;
label = "<"+e.getTagName()+">";
icon = elementIcon;
} else if (value instanceof Attr) {
Attr a = (Attr) value;
label = a.getName()+"="+a.getValue();
icon = attributeIcon;
} else if (value instanceof Text) {
Text t = (Text) value;
label = t.getData();
icon = textIcon;
} else {
label = "?";
icon = null;
}
if (label.length() > MAX_TEXT_LENGTH) {
label = label.substring(0, MAX_TEXT_LENGTH-1-3)+"...";
}
return new Cell(label, icon, selected, hasFocus);
}
public String getTextData()
{
Text textNode = getTextNode();
if (textNode != null)
{
return textNode.getData();
}
return null;
}
private Template getTemplateFromElement(NodeList templateNodes) {
List<Element> templateElements = new ArrayList<>(4);
for (int i = 0; i < templateNodes.getLength(); i++) {
Node node = templateNodes.item(i);
if (node instanceof Element) {
templateElements.add((Element) node);
}
}
Template template = new Template();
for (Element templateElement : templateElements) {
Text elementText = (Text) templateElement.getFirstChild();
String elementString = elementText.getData();
switch (templateElement.getTagName()) {
case "name":
template.setName(elementString);
break;
case "filename":
template.setPath(getFullPathToTemplate(elementString));
break;
case "description":
template.setDescription(elementString);
break;
case "source":
template.setSource(elementString);
break;
default:
}
}
return template;
}
private static boolean isWhitespaceText(Node n) {
if (!(n instanceof Text)) {
return false;
}
Text t = (Text) n;
String s = t.getData();
for (int i = 0; i < s.length(); i++) {
if (!Character.isWhitespace(s.charAt(i))) {
return false;
}
}
return true;
}
public String getText() {
Text contentNode = getContentNode();
return contentNode == null ? "" : contentNode.getData();
}
/**
* Assert that the given org.w3c.doc.Node is a comment element or a Text element and
* that it ontains whitespace only, otherwise throw an IllegalArgumentException using
* the given error message. This is helpful when nothing is expected at a certain
* place in a DOM tree, yet comments or whitespace text nodes can appear.
*
* @param node A DOM Node object.
* @param errMsg String used to in the IllegalArgumentException
* constructor if thrown.
* @throws IllegalArgumentException If the expression is false.
*/
public static void requireEmptyText(Node node, String errMsg)
throws IllegalArgumentException {
require((node instanceof Text) || (node instanceof Comment),
errMsg + ": " + node.toString());
if (node instanceof Text) {
Text text = (Text)node;
String textValue = text.getData();
require(textValue.trim().length() == 0, errMsg + ": " + textValue);
}
}