~ubuntu-branches/ubuntu/vivid/herold/vivid

« back to all changes in this revision

Viewing changes to java/org/dbdoclet/xiphias/dom/DOMTraverser.java

  • Committer: Package Import Robot
  • Author(s): Mathieu Malaterre
  • Date: 2012-09-20 10:00:14 UTC
  • Revision ID: package-import@ubuntu.com-20120920100014-5pcwbw2err6on8yg
Tags: upstream-6.0.1
ImportĀ upstreamĀ versionĀ 6.0.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
package org.dbdoclet.xiphias.dom;
 
2
 
 
3
import java.util.ArrayList;
 
4
 
 
5
import org.w3c.dom.Element;
 
6
import org.w3c.dom.NamedNodeMap;
 
7
import org.w3c.dom.Node;
 
8
import org.w3c.dom.NodeList;
 
9
 
 
10
public class DOMTraverser {
 
11
 
 
12
        private ArrayList<INodeVisitor> visitors = new ArrayList<INodeVisitor>();
 
13
 
 
14
    public DOMTraverser(INodeVisitor visitor) {
 
15
        addVisitor(visitor);
 
16
    }
 
17
    
 
18
    public void addVisitor(INodeVisitor visitor) {
 
19
        visitors.add(visitor);
 
20
    }
 
21
 
 
22
    public void traverse(Node node) throws Exception {
 
23
 
 
24
        if (node.getNodeType() == Node.ELEMENT_NODE) {
 
25
                openTag(node);
 
26
        }
 
27
        
 
28
        accept(node);
 
29
        
 
30
        if ((node.getNodeType() == Node.ELEMENT_NODE) && node.hasAttributes()) {
 
31
 
 
32
                NamedNodeMap attrs = ((Element) node).getAttributes();
 
33
 
 
34
            for (int i = 0; i < attrs.getLength(); i++) {
 
35
                Node attr = attrs.item(i);
 
36
                accept(attr);
 
37
            }
 
38
        }
 
39
 
 
40
        if (node.hasChildNodes()) {
 
41
 
 
42
                NodeList children = node.getChildNodes();
 
43
 
 
44
            for (int i = 0; i < children.getLength(); i++) {
 
45
                Node child = children.item(i);
 
46
                traverse(child);
 
47
            }
 
48
        }
 
49
 
 
50
        if (node.getNodeType() == Node.ELEMENT_NODE) {
 
51
                closeTag(node);
 
52
        }
 
53
        
 
54
    }
 
55
 
 
56
    private void openTag(Node node) throws Exception {
 
57
 
 
58
        for (INodeVisitor visitor : visitors) {
 
59
                visitor.openTag(node);
 
60
        }
 
61
    }
 
62
 
 
63
    private void accept(Node node) throws Exception {
 
64
 
 
65
        for (INodeVisitor visitor : visitors) {
 
66
                visitor.accept(node);
 
67
        }
 
68
    }
 
69
    
 
70
    private void closeTag(Node node) throws Exception {
 
71
 
 
72
        for (INodeVisitor visitor : visitors) {
 
73
                visitor.closeTag(node);
 
74
        }
 
75
    }
 
76
}