/plugins/CodeBrowser/tags/CodeBrowser_1_4_2/com/bitart/codebrowser/CBRoot.java

# · Java · 303 lines · 181 code · 39 blank · 83 comment · 21 complexity · d8555bdfc2896a9116bac62b25783ef0 MD5 · raw file

  1. /******************************************************************************
  2. * Copyright 2002 BITart Gerd Knops. All rights reserved.
  3. *
  4. * Project : CodeBrowser
  5. * File : CBRoot.java
  6. * Author : Gerd Knops gerti@BITart.com
  7. *
  8. *******************************************************************************
  9. * :mode=java:folding=indent:collapseFolds=1:
  10. * History:
  11. * 020510 Creation of file
  12. *
  13. *******************************************************************************
  14. *
  15. * Description:
  16. * This is the root TreeNode for the CodeBrowser display.
  17. * Here we take care of having the file parsed via ctags, and then
  18. * we create child nodes as required.
  19. *
  20. * $Id: CBRoot.java 1655 2005-10-21 17:12:23Z ezust $
  21. *
  22. *******************************************************************************
  23. *
  24. * DISCLAIMER
  25. *
  26. * BITart and Gerd Knops make no warranties, representations or commitments
  27. * with regard to the contents of this software. BITart and Gerd Knops
  28. * specifically disclaim any and all warranties, wether express, implied or
  29. * statutory, including, but not limited to, any warranty of merchantability
  30. * or fitness for a particular purpose, and non-infringement. Under no
  31. * circumstances will BITart or Gerd Knops be liable for loss of data,
  32. * special, incidental or consequential damages out of the use of this
  33. * software, even if those damages were forseeable, or BITart or Gerd Knops
  34. * was informed of their potential.
  35. *
  36. ******************************************************************************/
  37. package com.bitart.codebrowser;
  38. /******************************************************************************
  39. * Imports
  40. ******************************************************************************/
  41. import java.util.*;
  42. import java.io.*;
  43. import javax.swing.*;
  44. import javax.swing.tree.*;
  45. import org.gjt.sp.jedit.*;
  46. /*****************************************************************************/
  47. public class CBRoot implements TreeNode
  48. {
  49. /******************************************************************************
  50. * Vars
  51. ******************************************************************************/
  52. static final boolean DEBUG=false;
  53. Vector children=null;
  54. /******************************************************************************
  55. * Factory methods
  56. ******************************************************************************/
  57. public CBRoot(String path,String lang)
  58. {
  59. parse(path,lang);
  60. }
  61. /******************************************************************************
  62. * Implementation
  63. ******************************************************************************/
  64. public void parse(String path,String lang)
  65. {
  66. if(DEBUG) System.err.println("Parsing "+path);
  67. children=new Vector();
  68. //if(lang.equals("text")) return;
  69. Hashtable cbTypes=new Hashtable();
  70. Vector tv=new Vector();
  71. boolean buildxml=false;
  72. if(path.toLowerCase().endsWith("build.xml")) buildxml=true;
  73. try
  74. {
  75. //System.err.println("Starting ctags...");
  76. String[] args;
  77. if(!buildxml)
  78. {
  79. args=new String[]{
  80. jEdit.getProperty("options.codebrowser.ctags_path"),
  81. "--fields=KsSz",
  82. "--excmd=pattern",
  83. "--sort=no",
  84. "-f",
  85. "-",
  86. path
  87. };
  88. }
  89. else
  90. {
  91. args=new String[]{
  92. jEdit.getProperty("options.codebrowser.ctags_path"),
  93. "--fields=KsSz",
  94. "--excmd=pattern",
  95. "--sort=no",
  96. "--language-force=ant",
  97. "-f",
  98. "-",
  99. path
  100. };
  101. lang="ant";
  102. }
  103. /*
  104. System.err.println("Args: ");
  105. for(int i=0;i<args.length;i++)
  106. {
  107. System.err.println("\t"+args[i]);
  108. }
  109. */
  110. Process p=Runtime.getRuntime().exec(args);
  111. BufferedReader in=new BufferedReader(new InputStreamReader(p.getInputStream()));
  112. //System.err.println("ctags started!");
  113. String line;
  114. while((line=in.readLine())!=null)
  115. {
  116. //System.err.println("Got line: "+line);
  117. // Get rid of crlf
  118. while(line.endsWith("\n") || line.endsWith("\r"))
  119. {
  120. line=line.substring(0,line.length()-1);
  121. }
  122. //
  123. // split off extension
  124. //
  125. int idx;
  126. idx=line.lastIndexOf(";\"\t");
  127. if(idx<0) continue;
  128. // extensions in Vector v, remove from line
  129. Vector v=split("\t",line.substring(idx+3));
  130. line=line.substring(0,idx);
  131. // Create a hash from extensions
  132. Hashtable info=new Hashtable();
  133. for(int i=0;i<v.size();i++)
  134. {
  135. String s=(String)v.elementAt(i);
  136. int ei=s.indexOf(':');
  137. if(ei<0) continue;
  138. info.put(s.substring(0,ei),s.substring(ei+1));
  139. }
  140. // item name
  141. idx=line.indexOf("\t");
  142. if(idx<0) continue;
  143. info.put("cb_tag_cb",line.substring(0,idx));
  144. line=line.substring(idx+1);
  145. // file name, not needed
  146. idx=line.indexOf("\t");
  147. if(idx<0) continue;
  148. //info.put("cb_file_cb",line.substring(0,idx));
  149. // pattern
  150. info.put("cb_pattern_cb",line.substring(idx+1));
  151. //System.err.println("Parsed into: "+info);
  152. String type=(String)info.get("kind");
  153. if(type==null || type.length()==0) continue;
  154. CBType t=(CBType)cbTypes.get(type);
  155. if(t==null)
  156. {
  157. t=new CBType(this,type,lang);
  158. cbTypes.put(type,t);
  159. tv.add(type);
  160. }
  161. t.add(info);
  162. }
  163. }
  164. catch(IOException ioe)
  165. {
  166. System.err.println(ioe);
  167. }
  168. //System.err.println("Done reading");
  169. Collections.sort(tv);
  170. for(int i=0;i<tv.size();i++)
  171. {
  172. children.add(cbTypes.get(tv.elementAt(i)));
  173. }
  174. }
  175. public Vector split(String where,String str)
  176. /***********************************************************************
  177. * Splits the String txt on occurances of str, returns a Vector
  178. * of Strings.
  179. * @param where The String to split on.
  180. * @param str The String to split.
  181. * @return A Vector of strings.
  182. ***********************************************************************/
  183. {
  184. Vector v=new Vector();
  185. int idx;
  186. while((idx=str.indexOf(where))>=0)
  187. {
  188. String s="";
  189. if(idx>0) s=str.substring(0,idx);
  190. v.addElement(s);
  191. str=str.substring(idx+where.length());
  192. }
  193. v.addElement(str);
  194. return v;
  195. }
  196. public void expandPaths(JTree tree)
  197. {
  198. Object[] objs={
  199. this,
  200. this
  201. };
  202. for(int i=children.size()-1;i>=0;i--)
  203. {
  204. CBType t=(CBType)children.elementAt(i);
  205. if(t.getState())
  206. {
  207. objs[1]=t;
  208. tree.expandPath(new TreePath(objs));
  209. }
  210. }
  211. }
  212. public void setSorted(boolean flag,JTree tree)
  213. {
  214. DefaultTreeModel tm=null;
  215. if(tree!=null) tm=(DefaultTreeModel)tree.getModel();
  216. for(int i=0;i<children.size();i++)
  217. {
  218. CBType t=(CBType)children.elementAt(i);
  219. t.setSorted(flag);
  220. if(tm!=null && t.getState())
  221. {
  222. int l=t.getChildCount();
  223. int[] idc=new int[l];
  224. for(int j=0;j<l;j++)
  225. {
  226. idc[j]=j;
  227. }
  228. tm.nodesChanged(t,idc);
  229. }
  230. }
  231. }
  232. /******************************************************************************
  233. * TreeNode interface
  234. ******************************************************************************/
  235. public Enumeration children()
  236. {
  237. return children.elements();
  238. }
  239. public boolean getAllowsChildren()
  240. {
  241. return true;
  242. }
  243. public TreeNode getChildAt(int index)
  244. {
  245. return (TreeNode)children.elementAt(index);
  246. }
  247. public int getChildCount()
  248. {
  249. return children.size();
  250. }
  251. public int getIndex(TreeNode child)
  252. {
  253. return children.indexOf(child);
  254. }
  255. public TreeNode getParent()
  256. {
  257. return null;
  258. }
  259. public boolean isLeaf()
  260. {
  261. return false;
  262. }
  263. }
  264. /*************************************************************************EOF*/