PageRenderTime 369ms CodeModel.GetById 363ms app.highlight 4ms RepoModel.GetById 1ms app.codeStats 0ms

/bundles/plugins-trunk/XML/sidekick/html/HtmlSideKickParsedData.java

#
Java | 161 lines | 92 code | 22 blank | 47 comment | 12 complexity | 307ebf8b92223065525d18864ecc10b0 MD5 | raw file
  1/*
  2 * SideKickParsedData.java
  3 *
  4 * Copyright (C) 2003, 2004 Slava Pestov
  5 * portions Copyright (C) 2009 Eric Le Lay
  6 *
  7 * This program is free software; you can redistribute it and/or
  8 * modify it under the terms of the GNU General Public License
  9 * as published by the Free Software Foundation; either version 2
 10 * of the License, or any later version.
 11 *
 12 * This program is distributed in the hope that it will be useful,
 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 15 * GNU General Public License for more details.
 16 *
 17 * You should have received a copy of the GNU General Public License
 18 * along with this program; if not, write to the Free Software
 19 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 20 */
 21
 22package sidekick.html;
 23
 24// Imports
 25import javax.swing.tree.DefaultTreeModel;
 26import javax.swing.tree.DefaultMutableTreeNode;
 27import javax.swing.tree.TreePath;
 28import javax.swing.tree.TreeNode;
 29
 30
 31import org.gjt.sp.jedit.Buffer;
 32
 33import sidekick.util.SideKickAsset;
 34import sidekick.util.SideKickElement;
 35
 36import xml.XmlParsedData;
 37import xml.completion.CompletionInfo;
 38import sidekick.html.parser.html.HtmlDocument;
 39
 40/**
 41 * Stores a buffer structure tree.
 42 * 
 43 * Plugins can extend this class to persist plugin-specific information. For
 44 * example, the XML plugin stores code completion-related structures using a
 45 * subclass.
 46 * 
 47 * danson: modified for HtmlSideKick.
 48 */
 49public class HtmlSideKickParsedData extends XmlParsedData
 50{
 51	/**
 52	 * @param fileName
 53	 *                The file name being parsed, used as the root of the
 54	 *                tree.
 55	 */
 56	public HtmlSideKickParsedData(String fileName, Buffer buffer)
 57	{
 58		super(fileName, true);
 59		CompletionInfo completionInfo = CompletionInfo.getCompletionInfoForBuffer(buffer);
 60		setCompletionInfo("", completionInfo);
 61	}
 62
 63    /*
 64	public IAsset getAssetAtOffset(int pos)
 65	{
 66		IAsset asset = super.getAssetAtOffset(pos);
 67		System.out.println(asset.getName() + ": " + asset.getStart());
 68		return asset;
 69	}
 70    */
 71    
 72    //{{{ getXPathForPosition() method
 73    @Override
 74	public String getXPathForPosition(int pos)
 75	{
 76		TreePath path = getTreePathForPosition(pos);
 77		DefaultMutableTreeNode tn = (DefaultMutableTreeNode)path.getLastPathComponent();
 78		TreeNode[]steps = tn.getPath();
 79		DefaultMutableTreeNode parent = (DefaultMutableTreeNode)steps[0];
 80		String xpath = "";
 81		if(steps.length == 1)
 82		{
 83			//there is only the node with the file name
 84			xpath = null;
 85		}
 86		else
 87		{
 88			parent = (DefaultMutableTreeNode)steps[1];
 89			
 90
 91			SideKickElement curTag = ((SideKickAsset)parent.getUserObject()).getElement();
 92			String name;
 93			
 94			if(curTag instanceof HtmlDocument.TagBlock)
 95			{
 96				name = ((HtmlDocument.TagBlock)curTag).startTag.tagName;
 97			}
 98			else
 99			{
100				name = ((HtmlDocument.Tag)curTag).tagName;
101			}
102			
103			xpath = "/" + name;
104			
105			for(int i=2;i<steps.length;i++)
106			{
107				DefaultMutableTreeNode cur=(DefaultMutableTreeNode)steps[i];
108
109				curTag = ((SideKickAsset)cur.getUserObject()).getElement();
110				
111				if(curTag instanceof HtmlDocument.TagBlock)
112				{
113					name = ((HtmlDocument.TagBlock)curTag).startTag.tagName;
114				}
115				else if(curTag instanceof HtmlDocument.Tag)
116				{
117					name = ((HtmlDocument.Tag)curTag).tagName;
118				}
119				else
120				{
121					//won't include this step in the XPath
122					continue;
123				}
124				
125				int jCur = parent.getIndex(cur);
126				int cntChild = 0;
127				for(int j=0;j<=jCur;j++)
128				{
129					DefaultMutableTreeNode aChild = (DefaultMutableTreeNode)parent.getChildAt(j);
130					SideKickElement aTag = ((SideKickAsset)aChild.getUserObject()).getElement();
131					String aName;
132					
133					if(aTag instanceof HtmlDocument.TagBlock)
134					{
135						aName = ((HtmlDocument.TagBlock)aTag).startTag.tagName;
136					}
137					else if(aTag instanceof HtmlDocument.Tag)
138					{
139						aName = ((HtmlDocument.Tag)aTag).tagName;
140					}
141					else
142					{
143						aName = null;
144					}
145					
146					if(name.equals(aName))
147					{
148						cntChild++;
149					}
150				}
151				
152				xpath += "/"+name+"["+cntChild+"]";
153				
154				parent = cur;
155			}
156		}
157		return xpath;
158	}
159	//}}}
160
161}