PageRenderTime 46ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

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

#
Unknown | 136 lines | 123 code | 13 blank | 0 comment | 0 complexity | aaa138d78b5c47fa53fc3e5c02b0ff56 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. * 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 4995 2004-03-19 15:58:00Z spestov $
  26. *
  27. * Checked for jEdit 4.0 API
  28. *
  29. */
  30. boolean testClassPath()
  31. {
  32. classpath = System.getProperty("java.class.path");
  33. classSeparator = (File.separatorChar == '/' ? ':' : ';');
  34. return (classpath.indexOf(classSeparator) != -1)
  35. || (!classpath.endsWith("jedit.jar"));
  36. }
  37. File getCanonicalFile(File file)
  38. {
  39. try
  40. {
  41. return new File(file.getCanonicalPath());
  42. }
  43. catch(IOException e)
  44. {
  45. return new File(file.getAbsolutePath());
  46. }
  47. }
  48. File getRoot(File file)
  49. {
  50. tokens = new StringTokenizer(System.getProperty("java.class.path"),
  51. File.pathSeparator);
  52. fileSet = new Hashtable();
  53. while(tokens.hasMoreTokens())
  54. {
  55. String tok = tokens.nextToken();
  56. fileSet.put(getCanonicalFile(new File(tok)), tok);
  57. }
  58. while(file != null)
  59. {
  60. if(fileSet.get(getCanonicalFile(file)) != null)
  61. break;
  62. parent = file.getParent();
  63. file = (parent != null) ? new File(parent) : null;
  64. }
  65. return file;
  66. }
  67. String determinePackageName(String path)
  68. {
  69. pathFile = new File(buffer.getPath());
  70. File root = getRoot(pathFile);
  71. if(root == null)
  72. return null;
  73. parent = pathFile.getParent();
  74. packagePath = parent.substring(root.getPath().length(), parent.length());
  75. packagePath = packagePath.replace(File.separatorChar, '.');
  76. if (packagePath.endsWith("."))
  77. {
  78. packagePath = packagePath.substring(0, packagePath.length() - 1);
  79. }
  80. if (packagePath.startsWith("."))
  81. {
  82. packagePath = packagePath.substring(1, packagePath.length());
  83. }
  84. return packagePath;
  85. }
  86. if( buffer.isReadOnly() )
  87. Macros.error( view, "Buffer is read-only." );
  88. else
  89. {
  90. // main routine
  91. if(!testClassPath())
  92. {
  93. Macros.error(view, "This macro will not work when the Java interpreter\n"
  94. + "loads jEdit with the '-jar' command line option.");
  95. }
  96. else
  97. {
  98. result = determinePackageName(buffer.getPath());
  99. if(result == null)
  100. {
  101. Macros.error(view, "Could not find a package name.");
  102. }
  103. else textArea.setSelectedText(result);
  104. }
  105. }
  106. /*
  107. Macro index data (in DocBook format)
  108. <listitem>
  109. <para><filename>Get_Package_Name.bsh</filename></para>
  110. <abstract><para>
  111. Inserts a plausible Java package name for the current buffer.
  112. </para></abstract>
  113. <para>
  114. The macro compares the buffer's path name with the elements of the
  115. classpath being used by the jEdit session. An error message will be
  116. displayed if no suitable package name is found. This macro will not
  117. work if jEdit is being run as a JAR file without specifying a
  118. classpath. In that case the classpath seen by the macro consists
  119. solely of the JAR file.
  120. </para>
  121. </listitem>
  122. */
  123. // end Get_Package_Name.bsh