001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: /*
043: * PositionFinderVisitor.java
044: *
045: * Created on October 26, 2005, 3:02 PM
046: *
047: * To change this template, choose Tools | Template Manager
048: * and open the template in the editor.
049: */
050:
051: package org.netbeans.modules.xml.xdm.visitor;
052:
053: import java.util.List;
054: import org.netbeans.modules.xml.xdm.nodes.Attribute;
055: import org.netbeans.modules.xml.xdm.nodes.Document;
056: import org.netbeans.modules.xml.xdm.nodes.Element;
057: import org.netbeans.modules.xml.xdm.nodes.Node;
058: import org.netbeans.modules.xml.xdm.nodes.NodeImpl;
059: import org.netbeans.modules.xml.xdm.nodes.Text;
060: import org.netbeans.modules.xml.xdm.nodes.Token;
061: import org.netbeans.modules.xml.xdm.nodes.TokenType;
062: import org.w3c.dom.NamedNodeMap;
063: import org.w3c.dom.NodeList;
064:
065: /**
066: *
067: * @author rico
068: */
069: public class PositionFinderVisitor implements XMLNodeVisitor {
070:
071: int position = 0;
072: Node node;
073: boolean found;
074:
075: public int findPosition(Node rootNode, Node node) {
076: reset();
077: this .node = node;
078: rootNode.accept(this );
079:
080: return position;
081: }
082:
083: public void reset() {
084: position = 0;
085: found = false;
086: }
087:
088: public void visit(Document doc) {
089: //xml processing instruction
090: position += getLengthOfTokens(doc);
091: NodeList nodes = doc.getChildNodes();
092: for (int i = 0; i < nodes.getLength(); i++) {
093: Node n = (Node) nodes.item(i);
094: n.accept(this );
095: if (found)
096: return;
097: }
098: }
099:
100: public void visit(Element e) {
101: if (e.getId() == node.getId()) {
102: found = true;
103: } else {
104: position += getElementStartTokenLength(e, true); //open start tag
105: NamedNodeMap attrs = e.getAttributes();
106: for (int i = 0; i < attrs.getLength(); i++) {
107: Node attr = (Node) attrs.item(i);
108: attr.accept(this );
109: if (found)
110: return;
111: }
112: position += getStartTagWhiteSpaceTokensLength(e); //all whitespaces
113: position++; //close of start tag
114: NodeList children = e.getChildNodes();
115: for (int i = 0; i < children.getLength(); i++) {
116: Node n = (Node) children.item(i);
117: n.accept(this );
118: if (found)
119: return;
120: }
121: position += getElementStartTokenLength(e, false); //open end tag
122: position += getEndTagWhiteSpaceTokensLength(e); //all whitespaces
123: position++; //close of end tag
124: }
125: }
126:
127: public void visit(Text txt) {
128: if (txt.getId() == node.getId()) {
129: found = true;
130: } else {
131: int txtLen = getLength(txt);
132: if (txtLen > 0)
133: position += txtLen;
134: }
135: }
136:
137: public void visit(Attribute attr) {
138: if (attr.getId() == node.getId()) {
139: //add preceding white spaces
140: position += getLeadingWhiteSpaces(attr);
141: found = true;
142: } else {
143: position += getLengthOfTokens(attr);
144: }
145: }
146:
147: private int getLength(Text n) {
148: int len = 0;
149: for (Token token : ((NodeImpl) n).getTokens())
150: len += token.getValue().length();
151: return len;
152: }
153:
154: /**
155: * Obtains the length of a start token of elements, e.g., "<", or "<elementname",
156: * "</", or "</elementname".
157: * @param node The element being queried
158: * @param beginTag Is this for the start tag (<) or end tag (</)?
159: * @return length of start element
160: */
161: private int getElementStartTokenLength(Element element,
162: boolean beginTag) {
163: String value = "";
164: List<Token> tokens = element.getTokens();
165: for (Token token : tokens) {
166: if (token.getType() != TokenType.TOKEN_ELEMENT_START_TAG) {
167: continue;
168: }
169: String tokenValue = token.getValue();
170: if (beginTag) {
171: if (!tokenValue.startsWith("</")) {
172: value = tokenValue;
173: }
174: } else { //end tag
175: if (tokenValue.startsWith("</")) {
176: value = tokenValue;
177: }
178: }
179: }
180: return value.length();
181: }
182:
183: private int getStartTagWhiteSpaceTokensLength(NodeImpl node) {
184: StringBuilder buf = new StringBuilder();
185: List<Token> tokens = node.getTokens();
186: for (Token token : tokens) {
187: if (token.getType() == TokenType.TOKEN_ELEMENT_END_TAG) {
188: break; // only count whitspace before first tag end
189: }
190: if (token.getType() == TokenType.TOKEN_WHITESPACE) {
191: buf.append(token.getValue());
192: }
193: }
194: return buf.toString().length();
195: }
196:
197: private int getEndTagWhiteSpaceTokensLength(NodeImpl node) {
198: StringBuilder buf = new StringBuilder();
199: List<Token> tokens = node.getTokens();
200: boolean counting = false;
201: for (Token token : tokens) {
202: if (token.getType() == TokenType.TOKEN_ELEMENT_START_TAG) {
203: if (token.getValue().startsWith("</")) {
204: counting = true;
205: }
206: }
207: if (!counting)
208: continue;
209: if (token.getType() == TokenType.TOKEN_WHITESPACE) {
210: buf.append(token.getValue());
211: }
212: }
213: return buf.toString().length();
214: }
215:
216: private int getLeadingWhiteSpaces(Attribute attr) {
217: Token firstToken = attr.getTokens().get(0); //get the first token
218: if (firstToken.getType() == TokenType.TOKEN_WHITESPACE) {
219: return firstToken.getValue().length();
220: }
221: return 0;
222: }
223:
224: private int getLengthOfTokens(NodeImpl node) {
225: StringBuilder buf = new StringBuilder();
226: List<Token> tokens = node.getTokens();
227: for (Token token : tokens) {
228: buf.append(token.getValue());
229: }
230: return buf.length();
231: }
232:
233: }
|