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

/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
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 3872 2001-11-06 17:28:22Z jgellene $
  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. // main routine
  87. if(!testClassPath())
  88. {
  89. Macros.error(view, "This macro will not work when the Java interpreter\n"
  90. + "loads jEdit with the '-jar' command line option.");
  91. }
  92. else
  93. {
  94. result = determinePackageName(buffer.getPath());
  95. if(result == null)
  96. {
  97. Macros.error(view, "Could not find a package name.");
  98. }
  99. else textArea.setSelectedText(result);
  100. }
  101. /*
  102. Macro index data (in DocBook format)
  103. <listitem>
  104. <para><filename>Get_Package_Name.bsh</filename></para>
  105. <abstract><para>
  106. Inserts a plausible Java package name for the current buffer.
  107. </para></abstract>
  108. <para>
  109. The macro compares the buffer's path name with the elements of the
  110. classpath being used by the jEdit session. An error message will be
  111. displayed if no suitable package name is found. This macro will not
  112. work if jEdit is being run as a JAR file without specifying a
  113. classpath. In that case the classpath seen by the macro consists
  114. solely of the JAR file.
  115. </para>
  116. </listitem>
  117. */
  118. // end Get_Package_Name.bsh