286N/A/*
286N/A * reserved comment block
286N/A * DO NOT REMOVE OR ALTER!
286N/A */
286N/A/*
286N/A * Copyright 1999-2004 The Apache Software Foundation.
286N/A *
286N/A * Licensed under the Apache License, Version 2.0 (the "License");
286N/A * you may not use this file except in compliance with the License.
286N/A * You may obtain a copy of the License at
286N/A *
286N/A * http://www.apache.org/licenses/LICENSE-2.0
286N/A *
286N/A * Unless required by applicable law or agreed to in writing, software
286N/A * distributed under the License is distributed on an "AS IS" BASIS,
286N/A * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
286N/A * See the License for the specific language governing permissions and
286N/A * limitations under the License.
286N/A */
286N/A
286N/Apackage com.sun.org.apache.regexp.internal;
286N/A
286N/Aimport com.sun.org.apache.regexp.internal.RECompiler;
286N/Aimport com.sun.org.apache.regexp.internal.RESyntaxException;
286N/A
286N/A/**
286N/A * 'recompile' is a command line tool that pre-compiles one or more regular expressions
286N/A * for use with the regular expression matcher class 'RE'. For example, the command
286N/A * "java recompile a*b" produces output like this:
286N/A *
286N/A * <pre>
286N/A *
286N/A * // Pre-compiled regular expression "a*b"
286N/A * char[] re1Instructions =
286N/A * {
286N/A * 0x007c, 0x0000, 0x001a, 0x007c, 0x0000, 0x000d, 0x0041,
286N/A * 0x0001, 0x0004, 0x0061, 0x007c, 0x0000, 0x0003, 0x0047,
286N/A * 0x0000, 0xfff6, 0x007c, 0x0000, 0x0003, 0x004e, 0x0000,
286N/A * 0x0003, 0x0041, 0x0001, 0x0004, 0x0062, 0x0045, 0x0000,
286N/A * 0x0000,
286N/A * };
286N/A *
286N/A * REProgram re1 = new REProgram(re1Instructions);
286N/A *
286N/A * </pre>
286N/A *
286N/A * By pasting this output into your code, you can construct a regular expression matcher
286N/A * (RE) object directly from the pre-compiled data (the character array re1), thus avoiding
286N/A * the overhead of compiling the expression at runtime. For example:
286N/A *
286N/A * <pre>
286N/A *
286N/A * RE r = new RE(re1);
286N/A *
286N/A * </pre>
286N/A *
286N/A * @see RE
286N/A * @see RECompiler
286N/A *
286N/A * @author <a href="mailto:jonl@muppetlabs.com">Jonathan Locke</a>
286N/A */
286N/Apublic class recompile
286N/A{
286N/A /**
286N/A * Main application entrypoint.
286N/A * @param arg Command line arguments
286N/A */
286N/A static public void main(String[] arg)
286N/A {
286N/A // Create a compiler object
286N/A RECompiler r = new RECompiler();
286N/A
286N/A // Print usage if arguments are incorrect
286N/A if (arg.length <= 0 || arg.length % 2 != 0)
286N/A {
286N/A System.out.println("Usage: recompile <patternname> <pattern>");
286N/A System.exit(0);
286N/A }
286N/A
286N/A // Loop through arguments, compiling each
286N/A for (int i = 0; i < arg.length; i += 2)
286N/A {
286N/A try
286N/A {
286N/A // Compile regular expression
286N/A String name = arg[i];
286N/A String pattern = arg[i+1];
286N/A String instructions = name + "PatternInstructions";
286N/A
286N/A // Output program as a nice, formatted character array
286N/A System.out.print("\n // Pre-compiled regular expression '" + pattern + "'\n"
286N/A + " private static char[] " + instructions + " = \n {");
286N/A
286N/A // Compile program for pattern
286N/A REProgram program = r.compile(pattern);
286N/A
286N/A // Number of columns in output
286N/A int numColumns = 7;
286N/A
286N/A // Loop through program
286N/A char[] p = program.getInstructions();
286N/A for (int j = 0; j < p.length; j++)
286N/A {
286N/A // End of column?
286N/A if ((j % numColumns) == 0)
286N/A {
286N/A System.out.print("\n ");
286N/A }
286N/A
286N/A // Print character as padded hex number
286N/A String hex = Integer.toHexString(p[j]);
286N/A while (hex.length() < 4)
286N/A {
286N/A hex = "0" + hex;
286N/A }
286N/A System.out.print("0x" + hex + ", ");
286N/A }
286N/A
286N/A // End of program block
286N/A System.out.println("\n };");
286N/A System.out.println("\n private static RE " + name + "Pattern = new RE(new REProgram(" + instructions + "));");
286N/A }
286N/A catch (RESyntaxException e)
286N/A {
286N/A System.out.println("Syntax error in expression \"" + arg[i] + "\": " + e.toString());
286N/A }
286N/A catch (Exception e)
286N/A {
286N/A System.out.println("Unexpected exception: " + e.toString());
286N/A }
286N/A catch (Error e)
286N/A {
286N/A System.out.println("Internal error: " + e.toString());
286N/A }
286N/A }
286N/A }
286N/A}