1 19 20 package org.netbeans.modules.xml.xdm.visitor; 21 import java.util.List ; 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 ; 30 import org.w3c.dom.NodeList ; 31 32 36 public class FlushVisitor extends ChildVisitor { 37 38 public String flushModel(org.netbeans.modules.xml.xdm.nodes.Document root) { 39 buffer = new StringBuilder (); 40 root.accept(this); 41 return buffer.toString(); 42 } 43 44 public String flush(NodeList children) { 45 buffer = new StringBuilder (); 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 <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 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 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 } 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 <Token> tokens = ((NodeImpl)node).getTokens(); 95 for (Token token :tokens) 96 buffer.append(token.getValue()); 97 super.visitNode(node); 98 } 99 100 private StringBuilder buffer; 101 } 102 | Popular Tags |