/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
- /*
- * Preview_Javadoc_of_Buffer.bsh
- * a BeanShell macro script for the jEdit text editor
- *
- * Copyright (C) 2001 Tom Gutwin
- * tgutwin@webarts.bc.ca
- *
- * - Macro to create and preview the JavaDocs for the current buffer
- * - Includes the ability to use the Bouvard Doclet.
- * Visit
- * http://community.jedit.org/modules.php?
- * op=modload&name=web_links&file=index&req=viewlink&cid=6
- * for the Bouvard link
- * If you want... map it to a button on the button bar and away you go
- *
- * ****Features
- * - javadoc of current buffer's package
- * by setting a boolean flag in the macro code
- * save a different macroname if you want
- * ie Preview_JavaDoc_Of_Current_Buffer_Package.bsh
- *
- * - default Doclet is the standard Java Doclet
- *
- * - doclet choice dialog can be bypassed if using the
- * default doclet
- * by setting a boolean macro flag in the code
- *
- * - has built in support for Bouvard Doclet (see link below)
- *
- * - Added many of the standard Doclet commandline parms
- * like header, footer etc. with default values.
- * They are all controlled via boolean flags in the code
- * Switch them on/off as you like.
- *
- * - can be easily extended for other doclets
- *
- * - easy selection of output dir
- * defaults to system temp dir
- * set with a var in the macro code
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU General Public License
- * as published by the Free Software Foundation; either version 2
- * of the License, or any later version.
- *
- * This program 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 General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with the jEdit program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
- *
- * $Id: Preview_Javadoc_of_Buffer.bsh 4514 2003-02-28 04:28:36Z spestov $
- */
- String _getClassName()
- {
- /*
- * Get_Class_Name.bsh - a BeanShell macro script for the
- * jEdit text editor - gets class name from buffer name
- * Copyright (C) 2001 John Gellene
- * jgellene@nyc.rr.com
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU General Public License
- * as published by the Free Software Foundation; either version 2
- * of the License, or any later version.
- *
- * This program 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 General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with the jEdit program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
- *
- * Get_Class_Name.bsh,v 1.1.1.1 2001/09/02 05:39:29 spestov Exp $
- */
- name = buffer.getName();
- index = name.lastIndexOf('.');
- return (index != -1 ? name.substring(0, index) : name);
- }
- String _determinePackageName()
- {
- packageName = "";
- text = buffer.getText(0, buffer.getLength());
- //String lineSep = System.getProperty("line.separator");
- packageWord = text.indexOf("\npackage");
- if (packageWord != -1)
- {
- packageEOLine = text.indexOf(";", packageWord);
- if (packageEOLine!= -1)
- packageName = text.substring(packageWord+8,packageEOLine);
- }
- return packageName;
- }
- /** Chooses a directory and returns the path. **/
- /* Use this method if you want to customize the macro to choose
- output and input directorys for things */
- String _chooseADir(String dialogTitle, String startDir)
- {
- String retVal = "";
- JFileChooser chooser = new JFileChooser(startDir);
- chooser.setDialogTitle(dialogTitle);
- chooser.setMultiSelectionEnabled(false);
- chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
- if(chooser.showDialog(view, "Okay") == JFileChooser.APPROVE_OPTION)
- {
- retVal = chooser.getSelectedFile().getAbsolutePath();
- }
- return retVal;
- }
- void _infoView(View view, String urlStr)
- {
- // first, check if the plugin is installed.
- boolean version1 = false;
- //With version 1.0 of the plugin ... the name changed to
- //infoviewer.InfoViewerPlugin
- if(jEdit.getPlugin("InfoViewerPlugin") == null)
- {
- if(jEdit.getPlugin("infoviewer.InfoViewerPlugin") == null)
- {
- Macros.error(view,"You must install the InfoViewerPlugin"
- + " to use this macro.");
- return;
- }
- version1 = true;
- }
- try
- {
- // API change with version 1.0 of the InfoViewer
- if (version1)
- jEdit.getPlugin("infoviewer.InfoViewerPlugin").
- openURL(view, urlStr);// version 1.0
- else
- jEdit.getPlugin("InfoViewerPlugin").
- sendURL(new URL(urlStr), view); // pre 1.0
- }
- catch (MalformedURLException mu)
- {
- Macros.error(view,"Cannot find the url " + urlStr);
- }
- }
- String _returnCommandInConsole(View view, String shell, String command)
- {
- import console.Shell;
- // first, check if the plugin is installed.
- if(jEdit.getPlugin("console.ConsolePlugin") == null)
- {
- Macros.error(view,"You must install the Console plugin"
- + " to use this macro.");
- return;
- }
- manager = view.getDockableWindowManager();
- // Open the console if it isn't already open
- manager.addDockableWindow("console");
- // Obtain the console instance
- console.Shell _shell = console.Shell.getShell(shell);
- _console = manager.getDockable("console");
- // Ensure we are using the console shell
- _console.setShell(_shell);
- // Call its run() method
- _console.run(_shell, _console, command);
- outputPane = _console.getOutputPane();
- text = outputPane.getText();
- textLength = text.length();
- _shell.waitFor(_console);
- _shell.waitFor(_console);
- text = outputPane.getText().substring(textLength);
- return text;
- }
- /**
- * Ensures that a folder exists.
- * <TABLE WIDTH="100%" BORDER="0" CELLSPACING="1" CELLPADDING="2">
- * <TR><TD COLSPAN="2">
- * <H2>Ensures that a folder exists</H2>
- * </TD></TR>
- *
- * <TR><TD COLSPAN="2"> <BR>
- * <B>Description:</B><BR>
- * use it like this:
- * <br> _ensureFolderExists(new File(fileName).getParentFile());
- * </TD></TR>
- * </TABLE>
- *
- * @param folder The File object to check.
- **/
- void _ensureFolderExists(File folder)
- {
- if ( folder != null && ! folder.exists() )
- {
- _ensureFolderExists(folder.getParentFile());
- folder.mkdir();
- }
- }
- String versionStr = "1.1";
- String SYSTEM_TEMP_DIR = System.getProperty("java.io.tmpdir");
- String SYSTEM_FILE_SEPERATOR = File.separator;
- String SYSTEM_PATH_SEPERATOR = System.getProperty("path.separator");
- // first, check if the plugin is installed.
- if(jEdit.getPlugin("console.ConsolePlugin") == null ||
- (jEdit.getPlugin("InfoViewerPlugin") == null) &&
- (jEdit.getPlugin("infoviewer.InfoViewerPlugin") == null))
- {
- if(jEdit.getPlugin("console.ConsolePlugin") == null)
- Macros.error(view,"You must install the Console plugin"
- + " to use this macro.");
- else
- Macros.error(view,"You must install the InfoViewerPlugin plugin"
- + " to use this macro.");
- }
- else
- {
- String currBufferPath = buffer.getPath();
- //Macros.message(view,"Current Buffer Path:\n"+currBufferPath);
- if (currBufferPath.endsWith(".java"))
- {
- /* Store Some class/package/path info for use later */
- // Get the Package name
- String packName = _determinePackageName();
- // Get the Class Name
- String className = _getClassName();
- /* ********************************************************************** */
- /* Change the Following Vars to personalize things */
- /* You should also Change the Header and Botton javadoc text down below */
- /* ********************************************************************** */
- // header message
- String yourProductUrlStr = "http://www.yourproductsite.com";
- // Output directory .... set this to somewhere permanent if you want
- String outputDir = SYSTEM_TEMP_DIR;
- // This macro attempts to get the source search path right
- // if its not getting it... this var gets added to the Javadoc search path
- String extraSourceDir = "";
- // This macro doesn't do much with the javadoc classpath
- // if its not getting it... this var gets added to the Javadoc classpath
- String extraClassesDir = "";
- // flag to do the Javadoc on the package instead of just the file
- boolean doFullPackage = false;
- // set to false to default to the standard java doclet
- boolean showDocletDialog = true;
- /* Set some default options for the javadoc command */
- /* change these to suit how you like your output to show up */
- /* better yet... make a little dialog for input at runtime */
- int showOnlyLevel = 3; // protected is the default
- String[] showOnlyStr = {"-public ","-protected ","-package ","-private "};
- /* some (not All) standard doclet options */
- /* ************************************** */
- // if you want one of the following options...
- // set the corresponding flag to true
- boolean[] optionFlags = {false,false,false,false,false,
- false,false,false,false};
- String[] optionStrs = {"-use ","-version ","-author ","-nosince ","-notree ",
- "-noindex ","-nohelp ","-nodeprecated ","-verbose "};
- // beware OS/2 users...
- // ALL the following options break the IBM 1.3.0 JDK Javadoc tool
- // don't ask me?
- boolean addWindowTitle = true;
- StringBuffer windowTitle = new StringBuffer("-windowtitle \"Javadoc for ");
- windowTitle.append((doFullPackage?"package ":"class "));
- windowTitle.append(className);
- windowTitle.append("\" ");
- /* DocTitle Text */
- boolean addDocTitle = true;
- StringBuffer docTitle = new StringBuffer("-doctitle \"");
- docTitle.append("My Company Name Javadoc API");
- docTitle.append("\" ");
- /* Header Text */
- boolean addHeader = true;
- StringBuffer header = new StringBuffer("-header \"");
- header.append("<B><A href=");
- header.append(yourProductUrlStr);
- header.append(">ProductName</A></B>");
- header.append("<BR>Version xx.xx.xx");
- // Format the current time.
- java.text.SimpleDateFormat formatter
- = new java.text.SimpleDateFormat ("yyyy.MMMMM.dd 'at' hh:mm:ss zzz");
- java.util.Date currentTime_1 = new java.util.Date();
- String dateString = formatter.format(currentTime_1);
- header.append("<BR><font size=-1>");
- header.append(dateString);
- header.append("</font>\" ");
- /* Footer Text */
- boolean addFooter = true;
- StringBuffer footer = new StringBuffer("-footer \"");
- footer.append("Produced Using the <A href=http://www.jedit.org>");
- footer.append("jEdit</A><BR>Preview Javadoc Beanshell Macro.<BR>");
- footer.append("Copyright © 2001, <A href=http://www.webarts.bc.ca>");
- footer.append("Tom B. Gutwin</A>");
- footer.append("\" ");
- /* Bottom Text */
- boolean addBottom = true;
- StringBuffer bottom = new StringBuffer("-bottom \"");
- bottom.append("Some HTML to go at the bottom of the pages");
- bottom.append("\" ");
- /* ********************************************************************** */
- /* All users setting now complete */
- /* ********************************************************************** */
- // Store the directory where the buffer file lives
- String savedBufferdir = currBufferPath.substring(0,
- currBufferPath.length()-6-className.length());
- // build the full package.classname
- StringBuffer fullClassName = new StringBuffer();
- if (packName != null && !packName.equals(""))
- {
- fullClassName.append(packName);
- fullClassName.append(".");
- }
- fullClassName.append(className);
- /* Javadoc needs the file to live in a directory structure */
- /* named like its Package name */
- // If need be copy the file to the temp dir into its package dir */
- String currBufferdir = savedBufferdir;
- StringBuffer tmpBufferName = new StringBuffer(SYSTEM_TEMP_DIR);
- if (savedBufferdir.indexOf(
- packName.replace('.',File.separatorChar).trim()) == -1 )
- {
- // The buffer file is not in an appropriate named dir
- // copy and work on it in temp
- if (!SYSTEM_TEMP_DIR.endsWith(SYSTEM_FILE_SEPERATOR))
- tmpBufferName.append(SYSTEM_FILE_SEPERATOR);
- _ensureFolderExists(new File(tmpBufferName.toString() +
- packName.replace('.', File.separatorChar).trim()));
- tmpBufferName.append(fullClassName.toString().
- replace('.', File.separatorChar).trim());
- tmpBufferName.append(".java");
- buffer.save(view, tmpBufferName.toString(), false);
- // the rest of the macro uses the currBufferdir variable
- currBufferPath = tmpBufferName.toString();
- currBufferdir = currBufferPath.substring(0,
- currBufferPath.length()-6-className.length());
- }
- // some debug statements
- //Macros.message(view,"Package Name ="+packName);
- //Macros.message(view,"fullClassName ="+fullClassName);
- //Macros.message(view,"Buffer Path ="+currBufferPath);
- //Macros.message(view,"Buffer Dir ="+currBufferdir);
- /* ************************************************* */
- /* *********** On with the Processing ************* */
- Object[] options = { "Standard Java Doclet", "Bouvard Doclet" };
- String[] docletClassName = { "", "bp.doclet.Bouvard" };
- String[] docletClassPath = { "", "Bouvard.jar" };
- String proceed = options[0];
- /* Shows a Doclet Selection Dialog */
- if (showDocletDialog)
- proceed = JOptionPane.showInputDialog(view,
- "Choose the Doclet to use for previewing "+
- "the \n" + className + " JavaDocs.",
- "JavaDoc Buffer Macro - version "+versionStr,
- JOptionPane.QUESTION_MESSAGE, null, options,
- options[0]);
- if (proceed != null)
- {
- // this is where you could use the _chooseADir(String startDir) method
- // to choose an output dir at runtime.
- /*
- String outputDir = _chooseADir("Please Choose an Output Directory.", "/");
- */
- docletChoice = 0;
- for (int choiceNum = 0; choiceNum < options.length;choiceNum++)
- {
- if (((String)proceed).equals((String)options[choiceNum]))
- {
- docletChoice = choiceNum;
- choiceNum = options.length;
- }
- }
- // The currBufferSrcDir expects the current buffer to be in a subDirectory
- // path the same as the package name :(
- // THIS is definitely NOT always the case
- String currBufferSrcDir = currBufferPath.substring(0,
- currBufferPath.length()-fullClassName.toString().length()-5);
- if(outputDir != null && !outputDir.equals(""))
- {
- // you might need some of the follwing if you want to add some
- // commandline parms
- String jedit_userdir=System.getProperty("user.home") +
- SYSTEM_FILE_SEPERATOR +".jedit";
- String jedit_homedir=jEdit.getJEditHome();
- String currClassPath=System.getProperty("java.class.path");
- String java_home=System.getProperty("java.home");
- // Construct the command which will be executed
- StringBuffer command = new StringBuffer(java_home);
- if (java_home.toLowerCase().endsWith("jre"))
- command.append(File.separator).append("..");
- command.append(File.separator).append("bin");
- command.append(File.separator).append("javadoc ");
- if (!((String)proceed).equals((String)options[0]))
- {
- // Bouvard Doclet
- // This assumes the Bouvard.jar already exists
- // in the jeditUser/jars dir
- command.append("-doclet \"");
- command.append(docletClassName[docletChoice]);
- command.append("\" ");
- command.append("-docletpath \"");
- command.append(jedit_userdir);
- command.append(SYSTEM_FILE_SEPERATOR);
- command.append("jars");
- command.append(SYSTEM_FILE_SEPERATOR);
- command.append(docletClassPath[docletChoice]);
- command.append("\" ");
- }
- else
- { // standard doclet parms
- if (addWindowTitle)
- command.append(windowTitle.toString());
- if (addDocTitle)
- command.append(docTitle.toString());
- if (addHeader)
- command.append(header.toString());
- if (addFooter)
- command.append(footer.toString());
- if (addBottom)
- command.append(bottom.toString());
- /* add the on/off options */
- for (int opt = 0; opt <optionFlags.length; opt++)
- {
- if (optionFlags[opt])
- command.append(optionStrs[opt]);
- }
- }
- /* Specify the output dir */
- command.append("-d \"");
- command.append(outputDir);
- command.append("\" ");
- /* Set the Level of detail to show */
- command.append(showOnlyStr[showOnlyLevel]);
- /* if not found add your source dir to the 'extraClassesdir' var */
- command.append("-classpath \"");
- command.append(System.getProperty("java.class.path"));
- command.append(SYSTEM_PATH_SEPERATOR);
- command.append(extraClassesDir);
- command.append("\" ");
- /* **** Done with the options ***** */
- /* Specify the package or file name */
- if (doFullPackage)
- {
- /* Guess where the package source is located */
- /* if not found... add your source dir to the 'extraSourcedir' var */
- command.append("-sourcepath \"");
- command.append(savedBufferdir);
- command.append(SYSTEM_PATH_SEPERATOR);
- command.append(currBufferSrcDir);
- command.append(SYSTEM_PATH_SEPERATOR);
- command.append(currBufferdir);
- command.append(SYSTEM_PATH_SEPERATOR);
- command.append(extraSourceDir);
- command.append("\" ");
- command.append(packName);
- }
- else
- {
- command.append(currBufferPath);
- }
- retVal = _returnCommandInConsole(view, "System", command.toString());
- if (retVal.indexOf("error") == -1)
- {
- // Build the url for the Viewer
- // If you don't want to use the InfoViewer plugin...
- // easy, change the implemewntation in this macros _infoView method
- StringBuffer urlStr = new StringBuffer();
- urlStr.append("file://");
- urlStr.append(outputDir.replace('\\', '/').trim());
- if (!outputDir.endsWith(SYSTEM_FILE_SEPERATOR))
- urlStr.append("/");
- if (doFullPackage)
- {
- urlStr.append("index.html");
- }
- else
- {
- urlStr.append(packName.replace('.', '/').trim());
- urlStr.append("/");
- urlStr.append(className);
- urlStr.append(".html");
- //Macros.message(view, urlStr.toString());
- }
- // now which viewer (standard or Pecuchet)
- if (((String)proceed).equals((String)options[0]))
- _infoView(view, urlStr.toString());
- else
- {
- if (((String)proceed).equals((String)options[1]))
- {
- // Bouvard Browser
- // This assumes the Pecuchet.jar already exists
- // and xerces is in the classpath already
- StringBuffer classPath = new StringBuffer("\"");
- classPath.append(jedit_userdir);
- classPath.append(SYSTEM_FILE_SEPERATOR);
- classPath.append("jars");
- classPath.append(SYSTEM_FILE_SEPERATOR);
- classPath.append("Pecuchet.jar");
- classPath.append(SYSTEM_PATH_SEPERATOR);
- classPath.append(jedit_homedir);
- classPath.append(SYSTEM_FILE_SEPERATOR);
- classPath.append("jars");
- classPath.append(SYSTEM_FILE_SEPERATOR);
- classPath.append("Pecuchet.jar");
- classPath.append(SYSTEM_PATH_SEPERATOR);
- classPath.append(currClassPath);
- classPath.append("\"");
- StringBuffer bViewerCommand = new StringBuffer("java -classpath ");
- bViewerCommand.append(classPath);
- bViewerCommand.append(" ");
- bViewerCommand.append("bp.app.Main ");
- bViewerCommand.append(outputDir);
- //bViewerCommand.append(SYSTEM_FILE_SEPERATOR);
- bViewerCommand.append("data.bou &");
- retVal = _returnCommandInConsole(view, "System", bViewerCommand.toString());
- StringBuffer macroMessage =
- new StringBuffer("When the Pecuchet browser starts.\n");
- macroMessage.append("Open file: ");
- macroMessage.append(outputDir);
- macroMessage.append("data.bou");
- Macros.message(view, macroMessage.toString());
- }
- else
- {
- // put the viewers for other doclets here
- }
- }
- }
- else
- Macros.error(view, "Javadoc did NOT complete successfully. " +
- "See the console output");
- }
- }
- }
- else
- Macros.error(view, "Current Buffer does NOT appear to be a Java File");
- }
- /*
- Macro index data (in DocBook format)
- <listitem>
- <para><filename>Preview_Javadoc_of_Buffer.bsh</filename></para>
- <abstract><para>
- Create and display javadoc for current buffer.
- </para></abstract>
- <para>
- The macro includes configuration variables for using
- different doclets for generating javadocs and for genreating
- javadocs of the package of which the current buffer is a part.
- Details for use are included in the note accompanying the macro's
- source code.
- </para>
- </listitem>
- */
- // end Preview_JavaDoc_of_Buffer.bsh