KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > xdm > visitor > FlushVisitor


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.xml.xdm.visitor;
21 import java.util.List JavaDoc;
22 import org.netbeans.modules.xml.xdm.nodes.Attribute;
23 import org.netbeans.modules.xml.xdm.nodes.Element;
24 import org.netbeans.modules.xml.xdm.nodes.Node;
25 import org.netbeans.modules.xml.xdm.nodes.NodeImpl;
26 import org.netbeans.modules.xml.xdm.nodes.Text;
27 import org.netbeans.modules.xml.xdm.nodes.Token;
28 import org.netbeans.modules.xml.xdm.nodes.TokenType;
29 import org.w3c.dom.NamedNodeMap JavaDoc;
30 import org.w3c.dom.NodeList JavaDoc;
31
32 /**
33  *
34  * @author Srividhya Narayanan
35  */

36 public class FlushVisitor extends ChildVisitor {
37     
38     public String JavaDoc flushModel(org.netbeans.modules.xml.xdm.nodes.Document root) {
39         buffer = new StringBuilder JavaDoc();
40         root.accept(this);
41         return buffer.toString();
42     }
43
44     public String JavaDoc flush(NodeList JavaDoc children) {
45         buffer = new StringBuilder JavaDoc();
46         for (int i=0; i<children.getLength(); i++) {
47             Node child = (Node) children.item(i);
48             child.accept(this);
49         }
50         return buffer.toString();
51     }
52     
53     public void visit(Element e) {
54         java.util.ListIterator JavaDoc<Token> tokensIter = e.getTokens().listIterator();
55         while(tokensIter.hasNext()) {
56             Token token = tokensIter.next();
57             buffer.append(token.getValue());
58             if(token.getType()==TokenType.TOKEN_ELEMENT_START_TAG) break;
59         }
60
61         
62         if(e.hasAttributes()) {
63             NamedNodeMap JavaDoc attributes = e.getAttributes();
64             for (int i =0; i<attributes.getLength(); i++) {
65                 Node l = (Node)attributes.item(i);
66                 l.accept(this);
67             }
68         }
69
70         while (tokensIter.hasNext()) {
71             Token token = tokensIter.next();
72             buffer.append(token.getValue());
73             if(token.getType()==TokenType.TOKEN_ELEMENT_END_TAG) break;
74         }
75         
76         if(e.hasChildNodes()) {
77             NodeList JavaDoc children = e.getChildNodes();
78             for (int i =0; i<children.getLength(); i++) {
79                 Node l = (Node)children.item(i);
80                 if (l instanceof Attribute) {
81                     //
82
} else {
83                     l.accept(this);
84                 }
85             }
86         }
87
88         while(tokensIter.hasNext()) {
89             buffer.append(tokensIter.next().getValue());
90         }
91     }
92     
93     protected void visitNode(Node node) {
94         List JavaDoc<Token> tokens = ((NodeImpl)node).getTokens();
95         for (Token token :tokens)
96             buffer.append(token.getValue());
97         super.visitNode(node);
98     }
99     
100     private StringBuilder JavaDoc buffer;
101 }
102
Popular Tags