1. /*
  2. * Copyright 2000-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 class implements the name mangling rules of the jasper in tomcat4.1.x
  21. * which is likely to remain for some time
  22. * @see org.apache.jasper.JspCompilationContext
  23. */
  24. public class Jasper41Mangler implements JspMangler {
  25. /**
  26. * map from a jsp file to a java filename; does not do packages
  27. *
  28. * @param jspFile file
  29. * @return java filename
  30. */
  31. public String mapJspToJavaName(File jspFile) {
  32. String jspUri = jspFile.getAbsolutePath();
  33. int start = jspUri.lastIndexOf(File.separatorChar) + 1;
  34. int end = jspUri.length();
  35. StringBuffer modifiedClassName;
  36. modifiedClassName = new StringBuffer(jspUri.length() - start);
  37. if (!Character.isJavaIdentifierStart(jspUri.charAt(start))
  38. || jspUri.charAt(start) == '_') {
  39. // If the first char is not a start of Java identifier or is _
  40. // prepend a '_'.
  41. modifiedClassName.append('_');
  42. }
  43. for (int i = start; i < end; i++) {
  44. char ch = jspUri.charAt(i);
  45. if (Character.isJavaIdentifierPart(ch)) {
  46. modifiedClassName.append(ch);
  47. } else if (ch == '.') {
  48. modifiedClassName.append('_');
  49. } else {
  50. modifiedClassName.append(mangleChar(ch));
  51. }
  52. }
  53. return modifiedClassName.toString();
  54. }
  55. /**
  56. * Mangle the specified character to create a legal Java class name.
  57. */
  58. private static final String mangleChar(char ch) {
  59. String s = Integer.toHexString(ch);
  60. int nzeros = 5 - s.length();
  61. char[] result = new char[6];
  62. result[0] = '_';
  63. for (int i = 1; i <= nzeros; i++) {
  64. result[i] = '0';
  65. }
  66. for (int i = nzeros + 1, j = 0; i < 6; i++, j++) {
  67. result[i] = s.charAt(j);
  68. }
  69. return new String(result);
  70. }
  71. /**
  72. * taking in the substring representing the path relative to the source dir
  73. * return a new string representing the destination path
  74. * @todo
  75. */
  76. public String mapPath(String path) {
  77. return null;
  78. }
  79. }