1. package com.sun.org.apache.regexp.internal;
  2. /*
  3. * ====================================================================
  4. *
  5. * The Apache Software License, Version 1.1
  6. *
  7. * Copyright (c) 1999 The Apache Software Foundation. All rights
  8. * reserved.
  9. *
  10. * Redistribution and use in source and binary forms, with or without
  11. * modification, are permitted provided that the following conditions
  12. * are met:
  13. *
  14. * 1. Redistributions of source code must retain the above copyright
  15. * notice, this list of conditions and the following disclaimer.
  16. *
  17. * 2. Redistributions in binary form must reproduce the above copyright
  18. * notice, this list of conditions and the following disclaimer in
  19. * the documentation and/or other materials provided with the
  20. * distribution.
  21. *
  22. * 3. The end-user documentation included with the redistribution, if
  23. * any, must include the following acknowlegement:
  24. * "This product includes software developed by the
  25. * Apache Software Foundation (http://www.apache.org/)."
  26. * Alternately, this acknowlegement may appear in the software itself,
  27. * if and wherever such third-party acknowlegements normally appear.
  28. *
  29. * 4. The names "The Jakarta Project", "Jakarta-Regexp", and "Apache Software
  30. * Foundation" must not be used to endorse or promote products derived
  31. * from this software without prior written permission. For written
  32. * permission, please contact apache@apache.org.
  33. *
  34. * 5. Products derived from this software may not be called "Apache"
  35. * nor may "Apache" appear in their names without prior written
  36. * permission of the Apache Group.
  37. *
  38. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
  39. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  40. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  41. * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
  42. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  43. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  44. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  45. * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  46. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  47. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  48. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  49. * SUCH DAMAGE.
  50. * ====================================================================
  51. *
  52. * This software consists of voluntary contributions made by many
  53. * individuals on behalf of the Apache Software Foundation. For more
  54. * information on the Apache Software Foundation, please see
  55. * <http://www.apache.org/>.
  56. *
  57. */
  58. import com.sun.org.apache.regexp.internal.RECompiler;
  59. import com.sun.org.apache.regexp.internal.RESyntaxException;
  60. /**
  61. * 'recompile' is a command line tool that pre-compiles one or more regular expressions
  62. * for use with the regular expression matcher class 'RE'. For example, the command
  63. * "java recompile a*b" produces output like this:
  64. *
  65. * <pre>
  66. *
  67. * // Pre-compiled regular expression "a*b"
  68. * char[] re1Instructions =
  69. * {
  70. * 0x007c, 0x0000, 0x001a, 0x007c, 0x0000, 0x000d, 0x0041,
  71. * 0x0001, 0x0004, 0x0061, 0x007c, 0x0000, 0x0003, 0x0047,
  72. * 0x0000, 0xfff6, 0x007c, 0x0000, 0x0003, 0x004e, 0x0000,
  73. * 0x0003, 0x0041, 0x0001, 0x0004, 0x0062, 0x0045, 0x0000,
  74. * 0x0000,
  75. * };
  76. *
  77. * REProgram re1 = new REProgram(re1Instructions);
  78. *
  79. * </pre>
  80. *
  81. * By pasting this output into your code, you can construct a regular expression matcher
  82. * (RE) object directly from the pre-compiled data (the character array re1), thus avoiding
  83. * the overhead of compiling the expression at runtime. For example:
  84. *
  85. * <pre>
  86. *
  87. * RE r = new RE(re1);
  88. *
  89. * </pre>
  90. *
  91. * @see RE
  92. * @see RECompiler
  93. *
  94. * @author <a href="mailto:jonl@muppetlabs.com">Jonathan Locke</a>
  95. * @version $Id: recompile.java,v 1.1 2000/04/27 01:22:33 jon Exp $
  96. */
  97. public class recompile
  98. {
  99. /**
  100. * Main application entrypoint.
  101. * @param arg Command line arguments
  102. */
  103. static public void _main(String[] arg)
  104. {
  105. // Create a compiler object
  106. RECompiler r = new RECompiler();
  107. // Print usage if arguments are incorrect
  108. if (arg.length <= 0 || arg.length % 2 != 0)
  109. {
  110. System.out.println("Usage: recompile <patternname> <pattern>");
  111. System.exit(0);
  112. }
  113. // Loop through arguments, compiling each
  114. for (int i = 0; i < arg.length; i += 2)
  115. {
  116. try
  117. {
  118. // Compile regular expression
  119. String name = arg[i];
  120. String pattern = arg[i+1];
  121. String instructions = name + "PatternInstructions";
  122. // Output program as a nice, formatted character array
  123. System.out.print("\n // Pre-compiled regular expression '" + pattern + "'\n"
  124. + " private static char[] " + instructions + " = \n {");
  125. // Compile program for pattern
  126. REProgram program = r.compile(pattern);
  127. // Number of columns in output
  128. int numColumns = 7;
  129. // Loop through program
  130. char[] p = program.getInstructions();
  131. for (int j = 0; j < p.length; j++)
  132. {
  133. // End of column?
  134. if ((j % numColumns) == 0)
  135. {
  136. System.out.print("\n ");
  137. }
  138. // Print character as padded hex number
  139. String hex = Integer.toHexString(p[j]);
  140. while (hex.length() < 4)
  141. {
  142. hex = "0" + hex;
  143. }
  144. System.out.print("0x" + hex + ", ");
  145. }
  146. // End of program block
  147. System.out.println("\n };");
  148. System.out.println("\n private static RE " + name + "Pattern = new RE(new REProgram(" + instructions + "));");
  149. }
  150. catch (RESyntaxException e)
  151. {
  152. System.out.println("Syntax error in expression \"" + arg[i] + "\": " + e.toString());
  153. }
  154. catch (Exception e)
  155. {
  156. System.out.println("Unexpected exception: " + e.toString());
  157. }
  158. catch (Error e)
  159. {
  160. System.out.println("Internal error: " + e.toString());
  161. }
  162. }
  163. }
  164. }