001: /*
002: * Version: MPL 1.1/GPL 2.0/LGPL 2.1
003: *
004: * "The contents of this file are subject to the Mozilla Public License
005: * Version 1.1 (the "License"); you may not use this file except in
006: * compliance with the License. You may obtain a copy of the License at
007: * http://www.mozilla.org/MPL/
008: *
009: * Software distributed under the License is distributed on an "AS IS"
010: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
011: * License for the specific language governing rights and limitations under
012: * the License.
013: *
014: * The Original Code is ICEfaces 1.5 open source software code, released
015: * November 5, 2006. The Initial Developer of the Original Code is ICEsoft
016: * Technologies Canada, Corp. Portions created by ICEsoft are Copyright (C)
017: * 2004-2006 ICEsoft Technologies Canada, Corp. All Rights Reserved.
018: *
019: * Contributor(s): _____________________.
020: *
021: * Alternatively, the contents of this file may be used under the terms of
022: * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"
023: * License), in which case the provisions of the LGPL License are
024: * applicable instead of those above. If you wish to allow use of your
025: * version of this file only under the terms of the LGPL License and not to
026: * allow others to use your version of this file under the MPL, indicate
027: * your decision by deleting the provisions above and replace them with
028: * the notice and other provisions required by the LGPL License. If you do
029: * not delete the provisions above, a recipient may use your version of
030: * this file under either the MPL or the LGPL License."
031: *
032: */
033:
034: package com.icesoft.faces.util;
035:
036: import org.w3c.dom.NamedNodeMap;
037: import org.w3c.dom.Node;
038: import org.w3c.dom.NodeList;
039:
040: import javax.faces.component.UIComponent;
041: import javax.faces.context.ResponseWriter;
042: import java.io.IOException;
043:
044: public class HalterDump {
045: private UIComponent component;
046: private ResponseWriter writer;
047: private boolean halt = false;
048: private boolean resume = true;
049: private Node rootNode;
050: private Node halter;
051: private Node resumer;
052:
053: public HalterDump(ResponseWriter writer, UIComponent component,
054: Node root) {
055: this .component = component;
056: this .writer = writer;
057: this .rootNode = root;
058: }
059:
060: public void streamWrite(Node halter) throws IOException {
061: this .halter = halter;
062: halt = false;
063: dumpNode(rootNode);
064: }
065:
066: public void dumpNode(Node node) throws IOException {
067: if (halt) {
068: return;
069: }
070: if (node.equals(halter)) {
071: halt = true;
072: resumer = halter;
073: halter = null;
074: }
075:
076: if (resume) {
077: startNode(node);
078: }
079:
080: if (halt) {
081: resume = false;
082: return;
083: }
084:
085: NodeList children = node.getChildNodes();
086: int length = children.getLength();
087: for (int i = 0; i < length; i++) {
088: Node nextChildNode = children.item(i);
089: dumpNode(nextChildNode);
090: }
091:
092: if (node.equals(resumer)) {
093: resume = true;
094: halt = false;
095: }
096:
097: if (halt) {
098: resume = false;
099: return;
100: }
101:
102: if (resume) {
103: endNode(node);
104: }
105:
106: }
107:
108: private void startNode(Node node) throws IOException {
109: switch (node.getNodeType()) {
110:
111: case Node.DOCUMENT_NODE:
112: //nothing
113: break;
114:
115: case Node.ELEMENT_NODE:
116: String name = node.getNodeName();
117:
118: //XHTML tag should be lower case
119: writer.startElement(node.getNodeName().toLowerCase(),
120: component);
121: NamedNodeMap attributes = node.getAttributes();
122:
123: for (int i = 0; i < attributes.getLength(); i++) {
124: Node current = attributes.item(i);
125: //todo: boolean type as well
126: writer.writeAttribute(current.getNodeName(), current
127: .getNodeValue(), current.getNodeName());
128: }
129: break;
130:
131: case Node.TEXT_NODE:
132: writer.writeText(node.getNodeValue(), "text");
133: break;
134: }
135: }
136:
137: private void endNode(Node node) throws IOException {
138: switch (node.getNodeType()) {
139:
140: case Node.DOCUMENT_NODE:
141: //nothing
142: break;
143:
144: case Node.ELEMENT_NODE:
145: String name = node.getNodeName();
146:
147: writer.endElement(node.getNodeName().toLowerCase());
148: break;
149:
150: case Node.TEXT_NODE:
151: break;
152: }
153: }
154:
155: }
|