Java Code Examples for org.w3c.dom.Text#getData()

The following examples show how to use org.w3c.dom.Text#getData() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: JTidyHTMLHandler.java    From Knowage-Server with GNU Affero General Public License v3.0 6 votes vote down vote up
/**
 * 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;
}
 
Example 2
Source File: XMLCompareUtils.java    From XPagesExtensionLibrary with Apache License 2.0 6 votes vote down vote up
/**
 * 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);
        }
    }
}
 
Example 3
Source File: XMLTree.java    From pdfxtk with Apache License 2.0 6 votes vote down vote up
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);
   }
 
Example 4
Source File: XMLMemento.java    From APICloud-Studio with GNU General Public License v3.0 5 votes vote down vote up
public String getTextData()
{
	Text textNode = getTextNode();
	if (textNode != null)
	{
		return textNode.getData();
	}
	return null;
}
 
Example 5
Source File: OpenTemplate.java    From Open-LaTeX-Studio with MIT License 5 votes vote down vote up
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;
}
 
Example 6
Source File: MetaDataObjectSerializer_indent.java    From uima-uimaj with Apache License 2.0 5 votes vote down vote up
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;
}
 
Example 7
Source File: HTMLTitleElementImpl.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
public String getText() {
    Text contentNode = getContentNode();
    return contentNode == null ? "" : contentNode.getData();
}
 
Example 8
Source File: Utils.java    From Doradus with Apache License 2.0 3 votes vote down vote up
/**
 * 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);
    }
}