PageRenderTime 45ms CodeModel.GetById 20ms RepoModel.GetById 0ms 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
Possible License(s): BSD-3-Clause, AGPL-1.0, Apache-2.0, LGPL-2.0, LGPL-3.0, GPL-2.0, CC-BY-SA-3.0, LGPL-2.1, GPL-3.0, MPL-2.0-no-copyleft-exception, IPL-1.0
  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. package sidekick.html;
  22. // Imports
  23. import javax.swing.tree.DefaultTreeModel;
  24. import javax.swing.tree.DefaultMutableTreeNode;
  25. import javax.swing.tree.TreePath;
  26. import javax.swing.tree.TreeNode;
  27. import org.gjt.sp.jedit.Buffer;
  28. import sidekick.util.SideKickAsset;
  29. import sidekick.util.SideKickElement;
  30. import xml.XmlParsedData;
  31. import xml.completion.CompletionInfo;
  32. import sidekick.html.parser.html.HtmlDocument;
  33. /**
  34. * Stores a buffer structure tree.
  35. *
  36. * Plugins can extend this class to persist plugin-specific information. For
  37. * example, the XML plugin stores code completion-related structures using a
  38. * subclass.
  39. *
  40. * danson: modified for HtmlSideKick.
  41. */
  42. public class HtmlSideKickParsedData extends XmlParsedData
  43. {
  44. /**
  45. * @param fileName
  46. * The file name being parsed, used as the root of the
  47. * tree.
  48. */
  49. public HtmlSideKickParsedData(String fileName, Buffer buffer)
  50. {
  51. super(fileName, true);
  52. CompletionInfo completionInfo = CompletionInfo.getCompletionInfoForBuffer(buffer);
  53. setCompletionInfo("", completionInfo);
  54. }
  55. /*
  56. public IAsset getAssetAtOffset(int pos)
  57. {
  58. IAsset asset = super.getAssetAtOffset(pos);
  59. System.out.println(asset.getName() + ": " + asset.getStart());
  60. return asset;
  61. }
  62. */
  63. //{{{ getXPathForPosition() method
  64. @Override
  65. public String getXPathForPosition(int pos)
  66. {
  67. TreePath path = getTreePathForPosition(pos);
  68. DefaultMutableTreeNode tn = (DefaultMutableTreeNode)path.getLastPathComponent();
  69. TreeNode[]steps = tn.getPath();
  70. DefaultMutableTreeNode parent = (DefaultMutableTreeNode)steps[0];
  71. String xpath = "";
  72. if(steps.length == 1)
  73. {
  74. //there is only the node with the file name
  75. xpath = null;
  76. }
  77. else
  78. {
  79. parent = (DefaultMutableTreeNode)steps[1];
  80. SideKickElement curTag = ((SideKickAsset)parent.getUserObject()).getElement();
  81. String name;
  82. if(curTag instanceof HtmlDocument.TagBlock)
  83. {
  84. name = ((HtmlDocument.TagBlock)curTag).startTag.tagName;
  85. }
  86. else
  87. {
  88. name = ((HtmlDocument.Tag)curTag).tagName;
  89. }
  90. xpath = "/" + name;
  91. for(int i=2;i<steps.length;i++)
  92. {
  93. DefaultMutableTreeNode cur=(DefaultMutableTreeNode)steps[i];
  94. curTag = ((SideKickAsset)cur.getUserObject()).getElement();
  95. if(curTag instanceof HtmlDocument.TagBlock)
  96. {
  97. name = ((HtmlDocument.TagBlock)curTag).startTag.tagName;
  98. }
  99. else if(curTag instanceof HtmlDocument.Tag)
  100. {
  101. name = ((HtmlDocument.Tag)curTag).tagName;
  102. }
  103. else
  104. {
  105. //won't include this step in the XPath
  106. continue;
  107. }
  108. int jCur = parent.getIndex(cur);
  109. int cntChild = 0;
  110. for(int j=0;j<=jCur;j++)
  111. {
  112. DefaultMutableTreeNode aChild = (DefaultMutableTreeNode)parent.getChildAt(j);
  113. SideKickElement aTag = ((SideKickAsset)aChild.getUserObject()).getElement();
  114. String aName;
  115. if(aTag instanceof HtmlDocument.TagBlock)
  116. {
  117. aName = ((HtmlDocument.TagBlock)aTag).startTag.tagName;
  118. }
  119. else if(aTag instanceof HtmlDocument.Tag)
  120. {
  121. aName = ((HtmlDocument.Tag)aTag).tagName;
  122. }
  123. else
  124. {
  125. aName = null;
  126. }
  127. if(name.equals(aName))
  128. {
  129. cntChild++;
  130. }
  131. }
  132. xpath += "/"+name+"["+cntChild+"]";
  133. parent = cur;
  134. }
  135. }
  136. return xpath;
  137. }
  138. //}}}
  139. }