KickJava   Java API By Example, From Geeks To Geeks.

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


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 /*
21  * PositionFinderVisitor.java
22  *
23  * Created on October 26, 2005, 3:02 PM
24  *
25  * To change this template, choose Tools | Template Manager
26  * and open the template in the editor.
27  */

28
29 package org.netbeans.modules.xml.xdm.visitor;
30
31 import java.util.List JavaDoc;
32 import org.netbeans.modules.xml.xdm.nodes.Attribute;
33 import org.netbeans.modules.xml.xdm.nodes.Document;
34 import org.netbeans.modules.xml.xdm.nodes.Element;
35 import org.netbeans.modules.xml.xdm.nodes.Node;
36 import org.netbeans.modules.xml.xdm.nodes.NodeImpl;
37 import org.netbeans.modules.xml.xdm.nodes.Text;
38 import org.netbeans.modules.xml.xdm.nodes.Token;
39 import org.netbeans.modules.xml.xdm.nodes.TokenType;
40 import org.w3c.dom.NamedNodeMap JavaDoc;
41 import org.w3c.dom.NodeList JavaDoc;
42 /**
43  *
44  * @author rico
45  */

46 public class PositionFinderVisitor implements XMLNodeVisitor{
47     
48     int position = 0;
49     Node node;
50     boolean found;
51     
52     public int findPosition(Node rootNode, Node node){
53         reset();
54         this.node = node;
55         rootNode.accept(this);
56         
57         return position;
58     }
59     
60     public void reset(){
61         position = 0;
62         found = false;
63     }
64     
65     public void visit(Document doc) {
66         //xml processing instruction
67
position += getLengthOfTokens(doc);
68         NodeList JavaDoc nodes = doc.getChildNodes();
69         for(int i = 0; i < nodes.getLength(); i++){
70             Node n = (Node)nodes.item(i);
71             n.accept(this);
72             if(found) return;
73         }
74     }
75     
76     public void visit(Element e) {
77         if(e.getId() == node.getId()){
78             found = true;
79         } else{
80             position += getElementStartTokenLength(e, true); //open start tag
81
NamedNodeMap JavaDoc attrs = e.getAttributes();
82             for(int i = 0; i < attrs.getLength(); i++){
83                 Node attr = (Node)attrs.item(i);
84                 attr.accept(this);
85                 if(found) return;
86             }
87             position += getStartTagWhiteSpaceTokensLength(e); //all whitespaces
88
position++; //close of start tag
89
NodeList JavaDoc children = e.getChildNodes();
90             for(int i = 0; i < children.getLength(); i++){
91                 Node n = (Node)children.item(i);
92                 n.accept(this);
93                 if(found) return;
94             }
95             position += getElementStartTokenLength(e, false); //open end tag
96
position += getEndTagWhiteSpaceTokensLength(e); //all whitespaces
97
position++; //close of end tag
98
}
99     }
100     
101     public void visit(Text txt) {
102         if(txt.getId() == node.getId()){
103             found = true;
104         } else{
105             int txtLen = getLength(txt);
106             if(txtLen > 0)
107                 position += txtLen;
108         }
109     }
110     
111     public void visit(Attribute attr) {
112         if(attr.getId() == node.getId()){
113             //add preceding white spaces
114
position += getLeadingWhiteSpaces(attr);
115             found = true;
116         } else{
117             position += getLengthOfTokens(attr);
118         }
119     }
120     
121     private int getLength(Text n) {
122         int len = 0;
123         for(Token token:((NodeImpl)n).getTokens())
124             len += token.getValue().length();
125         return len;
126     }
127     
128     /**
129      * Obtains the length of a start token of elements, e.g., "<", or "<elementname",
130      * "</", or "</elementname".
131      * @param node The element being queried
132      * @param beginTag Is this for the start tag (<) or end tag (</)?
133      * @return length of start element
134      */

135     private int getElementStartTokenLength(Element element, boolean beginTag){
136         String JavaDoc value = "";
137         List JavaDoc<Token> tokens = element.getTokens();
138         for(Token token : tokens){
139             if(token.getType() != TokenType.TOKEN_ELEMENT_START_TAG){
140                 continue;
141             }
142             String JavaDoc tokenValue = token.getValue();
143             if(beginTag){
144                 if(!tokenValue.startsWith("</")){
145                     value = tokenValue;
146                 }
147             } else{ //end tag
148
if(tokenValue.startsWith("</")){
149                     value = tokenValue;
150                 }
151             }
152         }
153         return value.length();
154     }
155     
156     private int getStartTagWhiteSpaceTokensLength(NodeImpl node){
157         StringBuilder JavaDoc buf = new StringBuilder JavaDoc();
158         List JavaDoc<Token> tokens = node.getTokens();
159         for(Token token : tokens){
160             if (token.getType() == TokenType.TOKEN_ELEMENT_END_TAG){
161                 break; // only count whitspace before first tag end
162
}
163             if (token.getType() == TokenType.TOKEN_WHITESPACE) {
164                 buf.append(token.getValue());
165             }
166         }
167         return buf.toString().length();
168     }
169     
170     private int getEndTagWhiteSpaceTokensLength(NodeImpl node){
171         StringBuilder JavaDoc buf = new StringBuilder JavaDoc();
172         List JavaDoc<Token> tokens = node.getTokens();
173         boolean counting = false;
174         for(Token token : tokens){
175             if (token.getType() == TokenType.TOKEN_ELEMENT_START_TAG){
176                 if (token.getValue().startsWith("</")) {
177                     counting = true;
178                 }
179             }
180             if (! counting) continue;
181             if (token.getType() == TokenType.TOKEN_WHITESPACE) {
182                 buf.append(token.getValue());
183             }
184         }
185         return buf.toString().length();
186     }
187     
188     private int getLeadingWhiteSpaces(Attribute attr){
189         Token firstToken = attr.getTokens().get(0); //get the first token
190
if(firstToken.getType() == TokenType.TOKEN_WHITESPACE){
191             return firstToken.getValue().length();
192         }
193         return 0;
194     }
195     
196     private int getLengthOfTokens(NodeImpl node){
197         StringBuilder JavaDoc buf = new StringBuilder JavaDoc();
198         List JavaDoc<Token> tokens = node.getTokens();
199         for(Token token : tokens){
200             buf.append(token.getValue());
201         }
202         return buf.length();
203     }
204     
205 }
206
Popular Tags