/* * * * Copyright (C) 2005-2008 Yves Zoundi * * This library is free software; you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as published * by the Free Software Foundation; either version 2.1 of the License, or * (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. * * */package net.sf.xpontus.plugins.codecompletion.xml;import net.sf.xpontus.parsers.TokenNode;import org.apache.commons.lang.text.StrBuilder;import javax.swing.tree.DefaultMutableTreeNode;import javax.swing.tree.TreeNode;/** * * @author Yves Zoundi <yveszoundi at users dot sf dot net> */public class XMLTokenWalker { private int column; private int line; private TokenNode nearestTokenNode; public TokenNode getNearestTokenNode() { if (nearestTokenNode == null) { System.out.println("token node is null"); return null; } System.out.println("CompletionToken:" + nearestTokenNode.toString()); if (nearestTokenNode.endLine != -1) { System.out.println("endLine of token:" + nearestTokenNode.endLine); if (nearestTokenNode.endLine < line) { TreeNode parent = nearestTokenNode.getParent(); if (parent instanceof TokenNode) { nearestTokenNode = (TokenNode) parent; } } } else { System.out.println("No end information"); } return nearestTokenNode; } void setPositionInformation(int lineInfo, int columnInfo) { this.line = lineInfo + 1; this.column = columnInfo + 2; System.out.println("Line max is:" + this.line + "," + this.column); nearestTokenNode = null; } public void walk(DefaultMutableTreeNode n) { int total = n.getChildCount(); for (int i = 0; i < total; i++) { TokenNode tn = (TokenNode) n.getChildAt(i); int tokenLine = tn.line; int tokenColumn = tn.column; if (tokenLine > line) { System.out.println("ligne depassee :" + tokenLine); break; } if ((tokenLine == line) && (tokenColumn > column)) { break; } if ((tokenLine < line) || ((tokenLine == line) && (tokenColumn < column))) { if (nearestTokenNode != null) { if (isBestMatch(tn)) { nearestTokenNode = tn; } } else { nearestTokenNode = tn; } } walk(tn); } } public void printToken(TokenNode n) { StrBuilder sb = new StrBuilder(); sb.append("image:" + n.toString()); sb.append(",startLine:" + n.line); sb.append(",startColumn:" + n.column); sb.append(",endLine:" + n.endLine); sb.append(",endColumn:" + n.endColumn); System.out.println(sb.toString()); } private boolean isBestMatch(TokenNode tn) { System.out.println("================================="); System.out.println("Evaluating token:" + tn.toString()); printToken(tn); if (nearestTokenNode.endLine != -1) { System.out.println("Last Token Information"); printToken(nearestTokenNode); if (nearestTokenNode.endLine > tn.endLine) { return true; } return (tn.line >= nearestTokenNode.line); } return false; }}