1 package com.instantbank.collections.main.web;
2
3 import java.util.Collection;
4 import java.util.Iterator;
5 import java.util.Vector;
6 import javax.servlet.jsp.PageContext;
7 import javax.servlet.jsp.JspException;
8 import javax.servlet.jsp.tagext.BodyTagSupport;
9 import oracle.xml.parser.v2.XMLDocument;
10 import oracle.xml.parser.v2.XMLNode;
11 import org.w3c.dom.Node;
12 import org.w3c.dom.NodeList;
13
14 public class NodeIteratorTag extends BodyTagSupport {
15 private Collection collection;
16 private boolean isRoot = false;
17 private Iterator iterator;
18 private int maxRows = 0;
19 private String name = "";
20 private Node node;
21 private int rowCount = 0;
22
23
24 public void setId(String id) {
25 this.id = id;
26 }
27
28
29 public void setMaxRows(int maxRows) {
30 this.maxRows = maxRows;
31 }
32
33
34 public void setName(String name) {
35 this.name = name;
36 }
37
38
39 public void setNode(Node node) {
40 this.node = node;
41 }
42
43
44 public int doStartTag() throws JspException {
45 Node parent = node;
46 XMLDocument root = null;
47
48 try {
49 if(node instanceof XMLDocument) {
50 root = (XMLDocument)node;
51 parent = root.getDocumentElement();
52 }
53 if(name.equals("")) {
54 collection = getCollection(parent.getChildNodes());
55 }
56 else {
57 NodeList l;
58 l = ((XMLNode)parent).selectNodes(name);
59 collection = getCollection(((XMLNode)parent).selectNodes(name));
60 }
61 }
62 catch(Exception e) {
63 throw new JspException(e.getMessage());
64 }
65 rowCount = 0;
66 return collection.size() > 0 ? EVAL_BODY_TAG : SKIP_BODY;
67 }
68
69
70 public void doInitBody() throws JspException {
71 Node obj;
72
73 iterator = collection.iterator();
74 obj = (Node)iterator.next();
75 pageContext.setAttribute(this.getId(), obj, PageContext.PAGE_SCOPE);
76 }
77
78
79 public int doAfterBody() throws JspException {
80 Node obj;
81
82 rowCount++;
83 if(iterator.hasNext() && (maxRows == 0 || rowCount < maxRows)) {
84 obj = (Node)iterator.next();
85 pageContext.setAttribute(this.getId(), obj, PageContext.PAGE_SCOPE);
86 return EVAL_BODY_TAG;
87 }
88 else {
89 try {
90 getBodyContent().writeOut(getPreviousOut());
91 }
92 catch(java.io.IOException e) {
93 throw new JspException(e.getMessage());
94 }
95 return SKIP_BODY;
96 }
97 }
98
99
100 public void release() {
101 node = null;
102 maxRows = 0;
103 }
104
105
106 private Collection getCollection(NodeList list) {
107 Vector vector = new Vector();
108
109 int length = list.getLength();
110 for(int i = 0; i < length; ++i) {
111 vector.addElement(list.item(i));
112 }
113 return vector;
114 }
115 }
116
117