PageRenderTime 146ms CodeModel.GetById 140ms app.highlight 4ms RepoModel.GetById 0ms app.codeStats 1ms

/jEdit/tags/jedit-4-2-pre4/macros/Java/Get_Package_Name.bsh

#
Unknown | 131 lines | 118 code | 13 blank | 0 comment | 0 complexity | 10032cde24c4bb49589a3934fc98c70c MD5 | raw file
  1/*
  2 * Get_Package_Name.bsh - a BeanShell macro script for the
  3 * jEdit text editor - insert package name based upon path
  4 * of current buffer
  5 * Copyright (C) 2001 John Gellene
  6 * jgellene@nyc.rr.com
  7 * http://community.jedit.org
  8 *
  9 * This program is free software; you can redistribute it and/or
 10 * modify it under the terms of the GNU General Public License
 11 * as published by the Free Software Foundation; either version 2
 12 * of the License, or any later version.
 13 *
 14 * This program is distributed in the hope that it will be useful,
 15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 17 * GNU General Public License for more details.
 18 *
 19 * You should have received a copy of the GNU General Public License
 20 * along with this program; if not, write to the Free Software
 21 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 22 *
 23 * Based on code contributed by Richard Wan
 24 *
 25 * $Id: Get_Package_Name.bsh 3872 2001-11-06 17:28:22Z jgellene $
 26 *
 27 * Checked for jEdit 4.0 API
 28 *
 29 */
 30
 31boolean testClassPath()
 32{
 33	classpath = System.getProperty("java.class.path");
 34	classSeparator = (File.separatorChar == '/' ? ':' : ';');
 35	return (classpath.indexOf(classSeparator) != -1)
 36		|| (!classpath.endsWith("jedit.jar"));
 37}
 38
 39
 40File getCanonicalFile(File file)
 41{
 42    try
 43    {
 44        return new File(file.getCanonicalPath());
 45    }
 46    catch(IOException e)
 47    {
 48        return new File(file.getAbsolutePath());
 49    }
 50}
 51
 52File getRoot(File file)
 53{
 54    tokens = new StringTokenizer(System.getProperty("java.class.path"),
 55        File.pathSeparator);
 56    fileSet = new Hashtable();
 57    while(tokens.hasMoreTokens())
 58	{
 59		String tok = tokens.nextToken();
 60        fileSet.put(getCanonicalFile(new File(tok)), tok);
 61	}
 62
 63    while(file != null)
 64    {
 65        if(fileSet.get(getCanonicalFile(file)) != null)
 66            break;
 67        parent = file.getParent();
 68        file = (parent != null) ? new File(parent) : null;
 69    }
 70    return file;
 71}
 72
 73
 74String determinePackageName(String path)
 75{
 76    pathFile = new File(buffer.getPath());
 77    File root = getRoot(pathFile);
 78    if(root == null)
 79        return null;
 80    parent = pathFile.getParent();
 81    packagePath = parent.substring(root.getPath().length(), parent.length());
 82    packagePath = packagePath.replace(File.separatorChar, '.');
 83    if (packagePath.endsWith("."))
 84    {
 85        packagePath = packagePath.substring(0, packagePath.length() - 1);
 86    }
 87    if (packagePath.startsWith("."))
 88    {
 89        packagePath = packagePath.substring(1, packagePath.length());
 90    }
 91    return packagePath;
 92}
 93
 94// main routine
 95if(!testClassPath())
 96{
 97	Macros.error(view, "This macro will not work when the Java interpreter\n"
 98		+ "loads jEdit with the '-jar' command line option.");
 99}
100else
101{
102	result = determinePackageName(buffer.getPath());
103	if(result == null)
104	{
105		Macros.error(view, "Could not find a package name.");
106	}
107	else textArea.setSelectedText(result);
108}
109
110/*
111	Macro index data (in DocBook format)
112
113<listitem>
114    <para><filename>Get_Package_Name.bsh</filename></para>
115    <abstract><para>
116        Inserts a plausible Java package name for the current buffer.
117    </para></abstract>
118    <para>
119        The macro compares the buffer's path name with the elements of the
120        classpath being used by the jEdit session.  An error message will be
121        displayed if no suitable package name is found. This macro will not
122        work if jEdit is being run as a JAR file without specifying a
123        classpath.  In that case the classpath seen by the macro consists
124        solely of the JAR file.
125    </para>
126</listitem>
127
128*/
129
130// end Get_Package_Name.bsh
131