File: DomUtils.java

package info (click to toggle)
libhtml5parser-java 1.4%2Br1.3.1-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, sid, trixie
  • size: 3,376 kB
  • sloc: java: 27,064; cpp: 158; xml: 141; sh: 136; ruby: 44; makefile: 3
file content (36 lines) | stat: -rw-r--r-- 1,054 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import java.util.HashSet;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.Element;

public class DomUtils {

  private static HashSet<Document> pinned_list = new HashSet<Document>();

  public static synchronized void pin(Document d) {
    pinned_list.add(d);
  }

  public static synchronized void unpin(Document d) {
    pinned_list.remove(d);
  }

  // return all the text content contained by a single element 
  public static void getElementContent(Element e, StringBuffer b) {
    for (Node n = e.getFirstChild(); n!=null; n=n.getNextSibling()) {
      if (n.getNodeType() == n.TEXT_NODE) {
        b.append(n.getNodeValue());
      } else if (n.getNodeType() == n.ELEMENT_NODE) {
        getElementContent((Element) e, b);
      }
    }
  }

  // replace all child nodes of a given element with a single text element
  public static void setElementContent(Element e, String s) {
    while (e.hasChildNodes()) {
      e.removeChild(e.getFirstChild());
    }
    e.appendChild(e.getOwnerDocument().createTextNode(s));
  }
}