/jEdit/tags/jedit-4-2-pre5/macros/Java/Preview_Javadoc_of_Buffer.bsh

# · Unknown · 608 lines · 551 code · 57 blank · 0 comment · 0 complexity · 0ab46d2ec7fca8196e99222c1bfcd3ce MD5 · raw file

  1. /*
  2. * Preview_Javadoc_of_Buffer.bsh
  3. * a BeanShell macro script for the jEdit text editor
  4. *
  5. * Copyright (C) 2001 Tom Gutwin
  6. * tgutwin@webarts.bc.ca
  7. *
  8. * - Macro to create and preview the JavaDocs for the current buffer
  9. * - Includes the ability to use the Bouvard Doclet.
  10. * Visit
  11. * http://community.jedit.org/modules.php?
  12. * op=modload&name=web_links&file=index&req=viewlink&cid=6
  13. * for the Bouvard link
  14. * If you want... map it to a button on the button bar and away you go
  15. *
  16. * ****Features
  17. * - javadoc of current buffer's package
  18. * by setting a boolean flag in the macro code
  19. * save a different macroname if you want
  20. * ie Preview_JavaDoc_Of_Current_Buffer_Package.bsh
  21. *
  22. * - default Doclet is the standard Java Doclet
  23. *
  24. * - doclet choice dialog can be bypassed if using the
  25. * default doclet
  26. * by setting a boolean macro flag in the code
  27. *
  28. * - has built in support for Bouvard Doclet (see link below)
  29. *
  30. * - Added many of the standard Doclet commandline parms
  31. * like header, footer etc. with default values.
  32. * They are all controlled via boolean flags in the code
  33. * Switch them on/off as you like.
  34. *
  35. * - can be easily extended for other doclets
  36. *
  37. * - easy selection of output dir
  38. * defaults to system temp dir
  39. * set with a var in the macro code
  40. *
  41. * This program is free software; you can redistribute it and/or
  42. * modify it under the terms of the GNU General Public License
  43. * as published by the Free Software Foundation; either version 2
  44. * of the License, or any later version.
  45. *
  46. * This program is distributed in the hope that it will be useful,
  47. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  48. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  49. * GNU General Public License for more details.
  50. *
  51. * You should have received a copy of the GNU General Public License
  52. * along with the jEdit program; if not, write to the Free Software
  53. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  54. *
  55. * $Id: Preview_Javadoc_of_Buffer.bsh 4514 2003-02-28 04:28:36Z spestov $
  56. */
  57. String _getClassName()
  58. {
  59. /*
  60. * Get_Class_Name.bsh - a BeanShell macro script for the
  61. * jEdit text editor - gets class name from buffer name
  62. * Copyright (C) 2001 John Gellene
  63. * jgellene@nyc.rr.com
  64. *
  65. * This program is free software; you can redistribute it and/or
  66. * modify it under the terms of the GNU General Public License
  67. * as published by the Free Software Foundation; either version 2
  68. * of the License, or any later version.
  69. *
  70. * This program is distributed in the hope that it will be useful,
  71. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  72. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  73. * GNU General Public License for more details.
  74. *
  75. * You should have received a copy of the GNU General Public License
  76. * along with the jEdit program; if not, write to the Free Software
  77. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  78. *
  79. * Get_Class_Name.bsh,v 1.1.1.1 2001/09/02 05:39:29 spestov Exp $
  80. */
  81. name = buffer.getName();
  82. index = name.lastIndexOf('.');
  83. return (index != -1 ? name.substring(0, index) : name);
  84. }
  85. String _determinePackageName()
  86. {
  87. packageName = "";
  88. text = buffer.getText(0, buffer.getLength());
  89. //String lineSep = System.getProperty("line.separator");
  90. packageWord = text.indexOf("\npackage");
  91. if (packageWord != -1)
  92. {
  93. packageEOLine = text.indexOf(";", packageWord);
  94. if (packageEOLine!= -1)
  95. packageName = text.substring(packageWord+8,packageEOLine);
  96. }
  97. return packageName;
  98. }
  99. /** Chooses a directory and returns the path. **/
  100. /* Use this method if you want to customize the macro to choose
  101. output and input directorys for things */
  102. String _chooseADir(String dialogTitle, String startDir)
  103. {
  104. String retVal = "";
  105. JFileChooser chooser = new JFileChooser(startDir);
  106. chooser.setDialogTitle(dialogTitle);
  107. chooser.setMultiSelectionEnabled(false);
  108. chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
  109. if(chooser.showDialog(view, "Okay") == JFileChooser.APPROVE_OPTION)
  110. {
  111. retVal = chooser.getSelectedFile().getAbsolutePath();
  112. }
  113. return retVal;
  114. }
  115. void _infoView(View view, String urlStr)
  116. {
  117. // first, check if the plugin is installed.
  118. boolean version1 = false;
  119. //With version 1.0 of the plugin ... the name changed to
  120. //infoviewer.InfoViewerPlugin
  121. if(jEdit.getPlugin("InfoViewerPlugin") == null)
  122. {
  123. if(jEdit.getPlugin("infoviewer.InfoViewerPlugin") == null)
  124. {
  125. Macros.error(view,"You must install the InfoViewerPlugin"
  126. + " to use this macro.");
  127. return;
  128. }
  129. version1 = true;
  130. }
  131. try
  132. {
  133. // API change with version 1.0 of the InfoViewer
  134. if (version1)
  135. jEdit.getPlugin("infoviewer.InfoViewerPlugin").
  136. openURL(view, urlStr);// version 1.0
  137. else
  138. jEdit.getPlugin("InfoViewerPlugin").
  139. sendURL(new URL(urlStr), view); // pre 1.0
  140. }
  141. catch (MalformedURLException mu)
  142. {
  143. Macros.error(view,"Cannot find the url " + urlStr);
  144. }
  145. }
  146. String _returnCommandInConsole(View view, String shell, String command)
  147. {
  148. import console.Shell;
  149. // first, check if the plugin is installed.
  150. if(jEdit.getPlugin("console.ConsolePlugin") == null)
  151. {
  152. Macros.error(view,"You must install the Console plugin"
  153. + " to use this macro.");
  154. return;
  155. }
  156. manager = view.getDockableWindowManager();
  157. // Open the console if it isn't already open
  158. manager.addDockableWindow("console");
  159. // Obtain the console instance
  160. console.Shell _shell = console.Shell.getShell(shell);
  161. _console = manager.getDockable("console");
  162. // Ensure we are using the console shell
  163. _console.setShell(_shell);
  164. // Call its run() method
  165. _console.run(_shell, _console, command);
  166. outputPane = _console.getOutputPane();
  167. text = outputPane.getText();
  168. textLength = text.length();
  169. _shell.waitFor(_console);
  170. _shell.waitFor(_console);
  171. text = outputPane.getText().substring(textLength);
  172. return text;
  173. }
  174. /**
  175. * Ensures that a folder exists.
  176. * <TABLE WIDTH="100%" BORDER="0" CELLSPACING="1" CELLPADDING="2">
  177. * <TR><TD COLSPAN="2">
  178. * <H2>Ensures that a folder exists</H2>
  179. * </TD></TR>
  180. *
  181. * <TR><TD COLSPAN="2">&nbsp;<BR>
  182. * <B>Description:</B><BR>
  183. * use it like this:
  184. * <br> _ensureFolderExists(new File(fileName).getParentFile());
  185. * </TD></TR>
  186. * </TABLE>
  187. *
  188. * @param folder The File object to check.
  189. **/
  190. void _ensureFolderExists(File folder)
  191. {
  192. if ( folder != null && ! folder.exists() )
  193. {
  194. _ensureFolderExists(folder.getParentFile());
  195. folder.mkdir();
  196. }
  197. }
  198. String versionStr = "1.1";
  199. String SYSTEM_TEMP_DIR = System.getProperty("java.io.tmpdir");
  200. String SYSTEM_FILE_SEPERATOR = File.separator;
  201. String SYSTEM_PATH_SEPERATOR = System.getProperty("path.separator");
  202. // first, check if the plugin is installed.
  203. if(jEdit.getPlugin("console.ConsolePlugin") == null ||
  204. (jEdit.getPlugin("InfoViewerPlugin") == null) &&
  205. (jEdit.getPlugin("infoviewer.InfoViewerPlugin") == null))
  206. {
  207. if(jEdit.getPlugin("console.ConsolePlugin") == null)
  208. Macros.error(view,"You must install the Console plugin"
  209. + " to use this macro.");
  210. else
  211. Macros.error(view,"You must install the InfoViewerPlugin plugin"
  212. + " to use this macro.");
  213. }
  214. else
  215. {
  216. String currBufferPath = buffer.getPath();
  217. //Macros.message(view,"Current Buffer Path:\n"+currBufferPath);
  218. if (currBufferPath.endsWith(".java"))
  219. {
  220. /* Store Some class/package/path info for use later */
  221. // Get the Package name
  222. String packName = _determinePackageName();
  223. // Get the Class Name
  224. String className = _getClassName();
  225. /* ********************************************************************** */
  226. /* Change the Following Vars to personalize things */
  227. /* You should also Change the Header and Botton javadoc text down below */
  228. /* ********************************************************************** */
  229. // header message
  230. String yourProductUrlStr = "http://www.yourproductsite.com";
  231. // Output directory .... set this to somewhere permanent if you want
  232. String outputDir = SYSTEM_TEMP_DIR;
  233. // This macro attempts to get the source search path right
  234. // if its not getting it... this var gets added to the Javadoc search path
  235. String extraSourceDir = "";
  236. // This macro doesn't do much with the javadoc classpath
  237. // if its not getting it... this var gets added to the Javadoc classpath
  238. String extraClassesDir = "";
  239. // flag to do the Javadoc on the package instead of just the file
  240. boolean doFullPackage = false;
  241. // set to false to default to the standard java doclet
  242. boolean showDocletDialog = true;
  243. /* Set some default options for the javadoc command */
  244. /* change these to suit how you like your output to show up */
  245. /* better yet... make a little dialog for input at runtime */
  246. int showOnlyLevel = 3; // protected is the default
  247. String[] showOnlyStr = {"-public ","-protected ","-package ","-private "};
  248. /* some (not All) standard doclet options */
  249. /* ************************************** */
  250. // if you want one of the following options...
  251. // set the corresponding flag to true
  252. boolean[] optionFlags = {false,false,false,false,false,
  253. false,false,false,false};
  254. String[] optionStrs = {"-use ","-version ","-author ","-nosince ","-notree ",
  255. "-noindex ","-nohelp ","-nodeprecated ","-verbose "};
  256. // beware OS/2 users...
  257. // ALL the following options break the IBM 1.3.0 JDK Javadoc tool
  258. // don't ask me?
  259. boolean addWindowTitle = true;
  260. StringBuffer windowTitle = new StringBuffer("-windowtitle \"Javadoc for ");
  261. windowTitle.append((doFullPackage?"package ":"class "));
  262. windowTitle.append(className);
  263. windowTitle.append("\" ");
  264. /* DocTitle Text */
  265. boolean addDocTitle = true;
  266. StringBuffer docTitle = new StringBuffer("-doctitle \"");
  267. docTitle.append("My Company Name Javadoc API");
  268. docTitle.append("\" ");
  269. /* Header Text */
  270. boolean addHeader = true;
  271. StringBuffer header = new StringBuffer("-header \"");
  272. header.append("<B><A href=");
  273. header.append(yourProductUrlStr);
  274. header.append(">ProductName</A></B>");
  275. header.append("<BR>Version xx.xx.xx");
  276. // Format the current time.
  277. java.text.SimpleDateFormat formatter
  278. = new java.text.SimpleDateFormat ("yyyy.MMMMM.dd 'at' hh:mm:ss zzz");
  279. java.util.Date currentTime_1 = new java.util.Date();
  280. String dateString = formatter.format(currentTime_1);
  281. header.append("<BR><font size=-1>");
  282. header.append(dateString);
  283. header.append("</font>\" ");
  284. /* Footer Text */
  285. boolean addFooter = true;
  286. StringBuffer footer = new StringBuffer("-footer \"");
  287. footer.append("Produced Using the <A href=http://www.jedit.org>");
  288. footer.append("jEdit</A><BR>Preview Javadoc Beanshell Macro.<BR>");
  289. footer.append("Copyright &copy; 2001, <A href=http://www.webarts.bc.ca>");
  290. footer.append("Tom B. Gutwin</A>");
  291. footer.append("\" ");
  292. /* Bottom Text */
  293. boolean addBottom = true;
  294. StringBuffer bottom = new StringBuffer("-bottom \"");
  295. bottom.append("Some HTML to go at the bottom of the pages");
  296. bottom.append("\" ");
  297. /* ********************************************************************** */
  298. /* All users setting now complete */
  299. /* ********************************************************************** */
  300. // Store the directory where the buffer file lives
  301. String savedBufferdir = currBufferPath.substring(0,
  302. currBufferPath.length()-6-className.length());
  303. // build the full package.classname
  304. StringBuffer fullClassName = new StringBuffer();
  305. if (packName != null && !packName.equals(""))
  306. {
  307. fullClassName.append(packName);
  308. fullClassName.append(".");
  309. }
  310. fullClassName.append(className);
  311. /* Javadoc needs the file to live in a directory structure */
  312. /* named like its Package name */
  313. // If need be copy the file to the temp dir into its package dir */
  314. String currBufferdir = savedBufferdir;
  315. StringBuffer tmpBufferName = new StringBuffer(SYSTEM_TEMP_DIR);
  316. if (savedBufferdir.indexOf(
  317. packName.replace('.',File.separatorChar).trim()) == -1 )
  318. {
  319. // The buffer file is not in an appropriate named dir
  320. // copy and work on it in temp
  321. if (!SYSTEM_TEMP_DIR.endsWith(SYSTEM_FILE_SEPERATOR))
  322. tmpBufferName.append(SYSTEM_FILE_SEPERATOR);
  323. _ensureFolderExists(new File(tmpBufferName.toString() +
  324. packName.replace('.', File.separatorChar).trim()));
  325. tmpBufferName.append(fullClassName.toString().
  326. replace('.', File.separatorChar).trim());
  327. tmpBufferName.append(".java");
  328. buffer.save(view, tmpBufferName.toString(), false);
  329. // the rest of the macro uses the currBufferdir variable
  330. currBufferPath = tmpBufferName.toString();
  331. currBufferdir = currBufferPath.substring(0,
  332. currBufferPath.length()-6-className.length());
  333. }
  334. // some debug statements
  335. //Macros.message(view,"Package Name ="+packName);
  336. //Macros.message(view,"fullClassName ="+fullClassName);
  337. //Macros.message(view,"Buffer Path ="+currBufferPath);
  338. //Macros.message(view,"Buffer Dir ="+currBufferdir);
  339. /* ************************************************* */
  340. /* *********** On with the Processing ************* */
  341. Object[] options = { "Standard Java Doclet", "Bouvard Doclet" };
  342. String[] docletClassName = { "", "bp.doclet.Bouvard" };
  343. String[] docletClassPath = { "", "Bouvard.jar" };
  344. String proceed = options[0];
  345. /* Shows a Doclet Selection Dialog */
  346. if (showDocletDialog)
  347. proceed = JOptionPane.showInputDialog(view,
  348. "Choose the Doclet to use for previewing "+
  349. "the \n" + className + " JavaDocs.",
  350. "JavaDoc Buffer Macro - version "+versionStr,
  351. JOptionPane.QUESTION_MESSAGE, null, options,
  352. options[0]);
  353. if (proceed != null)
  354. {
  355. // this is where you could use the _chooseADir(String startDir) method
  356. // to choose an output dir at runtime.
  357. /*
  358. String outputDir = _chooseADir("Please Choose an Output Directory.", "/");
  359. */
  360. docletChoice = 0;
  361. for (int choiceNum = 0; choiceNum < options.length;choiceNum++)
  362. {
  363. if (((String)proceed).equals((String)options[choiceNum]))
  364. {
  365. docletChoice = choiceNum;
  366. choiceNum = options.length;
  367. }
  368. }
  369. // The currBufferSrcDir expects the current buffer to be in a subDirectory
  370. // path the same as the package name :(
  371. // THIS is definitely NOT always the case
  372. String currBufferSrcDir = currBufferPath.substring(0,
  373. currBufferPath.length()-fullClassName.toString().length()-5);
  374. if(outputDir != null && !outputDir.equals(""))
  375. {
  376. // you might need some of the follwing if you want to add some
  377. // commandline parms
  378. String jedit_userdir=System.getProperty("user.home") +
  379. SYSTEM_FILE_SEPERATOR +".jedit";
  380. String jedit_homedir=jEdit.getJEditHome();
  381. String currClassPath=System.getProperty("java.class.path");
  382. String java_home=System.getProperty("java.home");
  383. // Construct the command which will be executed
  384. StringBuffer command = new StringBuffer(java_home);
  385. if (java_home.toLowerCase().endsWith("jre"))
  386. command.append(File.separator).append("..");
  387. command.append(File.separator).append("bin");
  388. command.append(File.separator).append("javadoc ");
  389. if (!((String)proceed).equals((String)options[0]))
  390. {
  391. // Bouvard Doclet
  392. // This assumes the Bouvard.jar already exists
  393. // in the jeditUser/jars dir
  394. command.append("-doclet \"");
  395. command.append(docletClassName[docletChoice]);
  396. command.append("\" ");
  397. command.append("-docletpath \"");
  398. command.append(jedit_userdir);
  399. command.append(SYSTEM_FILE_SEPERATOR);
  400. command.append("jars");
  401. command.append(SYSTEM_FILE_SEPERATOR);
  402. command.append(docletClassPath[docletChoice]);
  403. command.append("\" ");
  404. }
  405. else
  406. { // standard doclet parms
  407. if (addWindowTitle)
  408. command.append(windowTitle.toString());
  409. if (addDocTitle)
  410. command.append(docTitle.toString());
  411. if (addHeader)
  412. command.append(header.toString());
  413. if (addFooter)
  414. command.append(footer.toString());
  415. if (addBottom)
  416. command.append(bottom.toString());
  417. /* add the on/off options */
  418. for (int opt = 0; opt <optionFlags.length; opt++)
  419. {
  420. if (optionFlags[opt])
  421. command.append(optionStrs[opt]);
  422. }
  423. }
  424. /* Specify the output dir */
  425. command.append("-d \"");
  426. command.append(outputDir);
  427. command.append("\" ");
  428. /* Set the Level of detail to show */
  429. command.append(showOnlyStr[showOnlyLevel]);
  430. /* if not found add your source dir to the 'extraClassesdir' var */
  431. command.append("-classpath \"");
  432. command.append(System.getProperty("java.class.path"));
  433. command.append(SYSTEM_PATH_SEPERATOR);
  434. command.append(extraClassesDir);
  435. command.append("\" ");
  436. /* **** Done with the options ***** */
  437. /* Specify the package or file name */
  438. if (doFullPackage)
  439. {
  440. /* Guess where the package source is located */
  441. /* if not found... add your source dir to the 'extraSourcedir' var */
  442. command.append("-sourcepath \"");
  443. command.append(savedBufferdir);
  444. command.append(SYSTEM_PATH_SEPERATOR);
  445. command.append(currBufferSrcDir);
  446. command.append(SYSTEM_PATH_SEPERATOR);
  447. command.append(currBufferdir);
  448. command.append(SYSTEM_PATH_SEPERATOR);
  449. command.append(extraSourceDir);
  450. command.append("\" ");
  451. command.append(packName);
  452. }
  453. else
  454. {
  455. command.append(currBufferPath);
  456. }
  457. retVal = _returnCommandInConsole(view, "System", command.toString());
  458. if (retVal.indexOf("error") == -1)
  459. {
  460. // Build the url for the Viewer
  461. // If you don't want to use the InfoViewer plugin...
  462. // easy, change the implemewntation in this macros _infoView method
  463. StringBuffer urlStr = new StringBuffer();
  464. urlStr.append("file://");
  465. urlStr.append(outputDir.replace('\\', '/').trim());
  466. if (!outputDir.endsWith(SYSTEM_FILE_SEPERATOR))
  467. urlStr.append("/");
  468. if (doFullPackage)
  469. {
  470. urlStr.append("index.html");
  471. }
  472. else
  473. {
  474. urlStr.append(packName.replace('.', '/').trim());
  475. urlStr.append("/");
  476. urlStr.append(className);
  477. urlStr.append(".html");
  478. //Macros.message(view, urlStr.toString());
  479. }
  480. // now which viewer (standard or Pecuchet)
  481. if (((String)proceed).equals((String)options[0]))
  482. _infoView(view, urlStr.toString());
  483. else
  484. {
  485. if (((String)proceed).equals((String)options[1]))
  486. {
  487. // Bouvard Browser
  488. // This assumes the Pecuchet.jar already exists
  489. // and xerces is in the classpath already
  490. StringBuffer classPath = new StringBuffer("\"");
  491. classPath.append(jedit_userdir);
  492. classPath.append(SYSTEM_FILE_SEPERATOR);
  493. classPath.append("jars");
  494. classPath.append(SYSTEM_FILE_SEPERATOR);
  495. classPath.append("Pecuchet.jar");
  496. classPath.append(SYSTEM_PATH_SEPERATOR);
  497. classPath.append(jedit_homedir);
  498. classPath.append(SYSTEM_FILE_SEPERATOR);
  499. classPath.append("jars");
  500. classPath.append(SYSTEM_FILE_SEPERATOR);
  501. classPath.append("Pecuchet.jar");
  502. classPath.append(SYSTEM_PATH_SEPERATOR);
  503. classPath.append(currClassPath);
  504. classPath.append("\"");
  505. StringBuffer bViewerCommand = new StringBuffer("java -classpath ");
  506. bViewerCommand.append(classPath);
  507. bViewerCommand.append(" ");
  508. bViewerCommand.append("bp.app.Main ");
  509. bViewerCommand.append(outputDir);
  510. //bViewerCommand.append(SYSTEM_FILE_SEPERATOR);
  511. bViewerCommand.append("data.bou &");
  512. retVal = _returnCommandInConsole(view, "System", bViewerCommand.toString());
  513. StringBuffer macroMessage =
  514. new StringBuffer("When the Pecuchet browser starts.\n");
  515. macroMessage.append("Open file: ");
  516. macroMessage.append(outputDir);
  517. macroMessage.append("data.bou");
  518. Macros.message(view, macroMessage.toString());
  519. }
  520. else
  521. {
  522. // put the viewers for other doclets here
  523. }
  524. }
  525. }
  526. else
  527. Macros.error(view, "Javadoc did NOT complete successfully. " +
  528. "See the console output");
  529. }
  530. }
  531. }
  532. else
  533. Macros.error(view, "Current Buffer does NOT appear to be a Java File");
  534. }
  535. /*
  536. Macro index data (in DocBook format)
  537. <listitem>
  538. <para><filename>Preview_Javadoc_of_Buffer.bsh</filename></para>
  539. <abstract><para>
  540. Create and display javadoc for current buffer.
  541. </para></abstract>
  542. <para>
  543. The macro includes configuration variables for using
  544. different doclets for generating javadocs and for genreating
  545. javadocs of the package of which the current buffer is a part.
  546. Details for use are included in the note accompanying the macro's
  547. source code.
  548. </para>
  549. </listitem>
  550. */
  551. // end Preview_JavaDoc_of_Buffer.bsh