KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > icesoft > faces > util > HalterDump


1 /*
2  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
3  *
4  * "The contents of this file are subject to the Mozilla Public License
5  * Version 1.1 (the "License"); you may not use this file except in
6  * compliance with the License. You may obtain a copy of the License at
7  * http://www.mozilla.org/MPL/
8  *
9  * Software distributed under the License is distributed on an "AS IS"
10  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
11  * License for the specific language governing rights and limitations under
12  * the License.
13  *
14  * The Original Code is ICEfaces 1.5 open source software code, released
15  * November 5, 2006. The Initial Developer of the Original Code is ICEsoft
16  * Technologies Canada, Corp. Portions created by ICEsoft are Copyright (C)
17  * 2004-2006 ICEsoft Technologies Canada, Corp. All Rights Reserved.
18  *
19  * Contributor(s): _____________________.
20  *
21  * Alternatively, the contents of this file may be used under the terms of
22  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"
23  * License), in which case the provisions of the LGPL License are
24  * applicable instead of those above. If you wish to allow use of your
25  * version of this file only under the terms of the LGPL License and not to
26  * allow others to use your version of this file under the MPL, indicate
27  * your decision by deleting the provisions above and replace them with
28  * the notice and other provisions required by the LGPL License. If you do
29  * not delete the provisions above, a recipient may use your version of
30  * this file under either the MPL or the LGPL License."
31  *
32  */

33
34 package com.icesoft.faces.util;
35
36 import org.w3c.dom.NamedNodeMap JavaDoc;
37 import org.w3c.dom.Node JavaDoc;
38 import org.w3c.dom.NodeList JavaDoc;
39
40 import javax.faces.component.UIComponent;
41 import javax.faces.context.ResponseWriter;
42 import java.io.IOException JavaDoc;
43
44 public class HalterDump {
45     private UIComponent component;
46     private ResponseWriter writer;
47     private boolean halt = false;
48     private boolean resume = true;
49     private Node JavaDoc rootNode;
50     private Node JavaDoc halter;
51     private Node JavaDoc resumer;
52
53     public HalterDump(ResponseWriter writer, UIComponent component, Node JavaDoc root) {
54         this.component = component;
55         this.writer = writer;
56         this.rootNode = root;
57     }
58
59
60     public void streamWrite(Node JavaDoc halter) throws IOException JavaDoc {
61         this.halter = halter;
62         halt = false;
63         dumpNode(rootNode);
64     }
65
66     public void dumpNode(Node JavaDoc node) throws IOException JavaDoc {
67         if (halt) {
68             return;
69         }
70         if (node.equals(halter)) {
71             halt = true;
72             resumer = halter;
73             halter = null;
74         }
75
76         if (resume) {
77             startNode(node);
78         }
79
80         if (halt) {
81             resume = false;
82             return;
83         }
84
85         NodeList JavaDoc children = node.getChildNodes();
86         int length = children.getLength();
87         for (int i = 0; i < length; i++) {
88             Node JavaDoc nextChildNode = children.item(i);
89             dumpNode(nextChildNode);
90         }
91
92         if (node.equals(resumer)) {
93             resume = true;
94             halt = false;
95         }
96
97         if (halt) {
98             resume = false;
99             return;
100         }
101
102         if (resume) {
103             endNode(node);
104         }
105
106     }
107
108     private void startNode(Node JavaDoc node) throws IOException JavaDoc {
109         switch (node.getNodeType()) {
110
111             case Node.DOCUMENT_NODE:
112                 //nothing
113
break;
114
115             case Node.ELEMENT_NODE:
116                 String JavaDoc name = node.getNodeName();
117
118                 //XHTML tag should be lower case
119
writer.startElement(node.getNodeName().toLowerCase(),
120                                     component);
121                 NamedNodeMap JavaDoc attributes = node.getAttributes();
122
123                 for (int i = 0; i < attributes.getLength(); i++) {
124                     Node JavaDoc current = attributes.item(i);
125                     //todo: boolean type as well
126
writer.writeAttribute(current.getNodeName(),
127                                           current.getNodeValue(),
128                                           current.getNodeName());
129                 }
130                 break;
131
132             case Node.TEXT_NODE:
133                 writer.writeText(node.getNodeValue(), "text");
134                 break;
135         }
136     }
137
138     private void endNode(Node JavaDoc node) throws IOException JavaDoc {
139         switch (node.getNodeType()) {
140
141             case Node.DOCUMENT_NODE:
142                 //nothing
143
break;
144
145             case Node.ELEMENT_NODE:
146                 String JavaDoc name = node.getNodeName();
147
148                 writer.endElement(node.getNodeName().toLowerCase());
149                 break;
150
151             case Node.TEXT_NODE:
152                 break;
153         }
154     }
155
156 }
157
Popular Tags