1. /*
  2. * Copyright 2001-2004 The Apache Software Foundation
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. *
  16. */
  17. package org.apache.tools.ant.taskdefs.optional.jsp;
  18. import java.io.File;
  19. /**
  20. * This is a class derived from the Jasper code
  21. * (org.apache.jasper.compiler.CommandLineCompiler) to map from a JSP filename
  22. * to a valid Java classname.
  23. *
  24. */
  25. public class JspNameMangler implements JspMangler {
  26. /**
  27. * this is the list of keywords which can not be used as classnames
  28. */
  29. public static final String[] keywords = {
  30. "assert",
  31. "abstract", "boolean", "break", "byte",
  32. "case", "catch", "char", "class",
  33. "const", "continue", "default", "do",
  34. "double", "else", "extends", "final",
  35. "finally", "float", "for", "goto",
  36. "if", "implements", "import",
  37. "instanceof", "int", "interface",
  38. "long", "native", "new", "package",
  39. "private", "protected", "public",
  40. "return", "short", "static", "super",
  41. "switch", "synchronized", "this",
  42. "throw", "throws", "transient",
  43. "try", "void", "volatile", "while"
  44. };
  45. /**
  46. * map from a jsp file to a java filename; does not do packages
  47. *
  48. * @param jspFile file
  49. * @return java filename
  50. */
  51. public String mapJspToJavaName(File jspFile) {
  52. return mapJspToBaseName(jspFile) + ".java";
  53. }
  54. /**
  55. * map from a jsp file to a base name; does not deal with extensions
  56. *
  57. * @param jspFile jspFile file
  58. * @return exensionless potentially remapped name
  59. */
  60. private String mapJspToBaseName(File jspFile) {
  61. String className;
  62. className = stripExtension(jspFile);
  63. // since we don't mangle extensions like the servlet does,
  64. // we need to check for keywords as class names
  65. for (int i = 0; i < keywords.length; ++i) {
  66. if (className.equals(keywords[i])) {
  67. className += "%";
  68. break;
  69. }
  70. }
  71. // Fix for invalid characters. If you think of more add to the list.
  72. StringBuffer modifiedClassName = new StringBuffer(className.length());
  73. // first char is more restrictive than the rest
  74. char firstChar = className.charAt(0);
  75. if (Character.isJavaIdentifierStart(firstChar)) {
  76. modifiedClassName.append(firstChar);
  77. } else {
  78. modifiedClassName.append(mangleChar(firstChar));
  79. }
  80. // this is the rest
  81. for (int i = 1; i < className.length(); i++) {
  82. char subChar = className.charAt(i);
  83. if (Character.isJavaIdentifierPart(subChar)) {
  84. modifiedClassName.append(subChar);
  85. } else {
  86. modifiedClassName.append(mangleChar(subChar));
  87. }
  88. }
  89. return modifiedClassName.toString();
  90. }
  91. /**
  92. * get short filename from file
  93. *
  94. * @param jspFile file in
  95. * @return file without any jsp extension
  96. */
  97. private String stripExtension(File jspFile) {
  98. String className;
  99. String filename = jspFile.getName();
  100. if (filename.endsWith(".jsp")) {
  101. className = filename.substring(0, filename.length() - 4);
  102. } else {
  103. className = filename;
  104. }
  105. return className;
  106. }
  107. /**
  108. * definition of the char escaping algorithm
  109. *
  110. * @param ch char to mangle
  111. * @return mangled string; 5 digit hex value
  112. */
  113. private static final String mangleChar(char ch) {
  114. if (ch == File.separatorChar) {
  115. ch = '/';
  116. }
  117. String s = Integer.toHexString(ch);
  118. int nzeros = 5 - s.length();
  119. char[] result = new char[6];
  120. result[0] = '_';
  121. for (int i = 1; i <= nzeros; ++i) {
  122. result[i] = '0';
  123. }
  124. int resultIndex = 0;
  125. for (int i = nzeros + 1; i < 6; ++i) {
  126. result[i] = s.charAt(resultIndex++);
  127. }
  128. return new String(result);
  129. }
  130. /**
  131. * taking in the substring representing the path relative to the source dir
  132. * return a new string representing the destination path
  133. * not supported, as jasper in tomcat4.0 doesnt either
  134. */
  135. public String mapPath(String path) {
  136. return null;
  137. }
  138. }