001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package org.apache.regexp;
019:
020: import org.apache.regexp.RECompiler;
021: import org.apache.regexp.RESyntaxException;
022:
023: /**
024: * 'recompile' is a command line tool that pre-compiles one or more regular expressions
025: * for use with the regular expression matcher class 'RE'. For example, the command
026: * <code>java org.apache.regexp.recompile re1 "a*b"</code> produces output like this:
027: *
028: * <pre>
029: *
030: * // Pre-compiled regular expression 'a*b'
031: * private static final char[] re1Instructions =
032: * {
033: * 0x002a, 0x0000, 0x0007, 0x0041, 0x0001, 0xfffd, 0x0061,
034: * 0x0041, 0x0001, 0x0004, 0x0062, 0x0045, 0x0000, 0x0000,
035: * };
036: *
037: * private static final REProgram re1 = new REProgram(re1Instructions);
038: *
039: * </pre>
040: *
041: * By pasting this output into your code, you can construct a regular expression matcher
042: * (RE) object directly from the pre-compiled data (the character array re1), thus avoiding
043: * the overhead of compiling the expression at runtime. For example:
044: *
045: * <pre>
046: *
047: * RE r = new RE(re1);
048: *
049: * </pre>
050: *
051: * @see RE
052: * @see RECompiler
053: *
054: * @author <a href="mailto:jonl@muppetlabs.com">Jonathan Locke</a>
055: * @version $Id: recompile.java 518156 2007-03-14 14:31:26Z vgritsenko $
056: */
057: public class recompile {
058: /**
059: * Main application entrypoint.
060: *
061: * @param arg Command line arguments
062: */
063: static public void main(String[] arg) {
064: // Create a compiler object
065: RECompiler r = new RECompiler();
066:
067: // Print usage if arguments are incorrect
068: if (arg.length <= 0 || arg.length % 2 != 0) {
069: System.out
070: .println("Usage: recompile <patternname> <pattern>");
071: System.exit(0);
072: }
073:
074: // Loop through arguments, compiling each
075: for (int i = 0; i < arg.length; i += 2) {
076: try {
077: // Compile regular expression
078: String name = arg[i];
079: String pattern = arg[i + 1];
080: String instructions = name + "Instructions";
081:
082: // Output program as a nice, formatted character array
083: System.out
084: .print("\n // Pre-compiled regular expression '"
085: + pattern
086: + "'\n"
087: + " private static final char[] "
088: + instructions + " = \n {");
089:
090: // Compile program for pattern
091: REProgram program = r.compile(pattern);
092:
093: // Number of columns in output
094: int numColumns = 7;
095:
096: // Loop through program
097: char[] p = program.getInstructions();
098: for (int j = 0; j < p.length; j++) {
099: // End of column?
100: if ((j % numColumns) == 0) {
101: System.out.print("\n ");
102: }
103:
104: // Print character as padded hex number
105: String hex = Integer.toHexString(p[j]);
106: while (hex.length() < 4) {
107: hex = "0" + hex;
108: }
109: System.out.print("0x" + hex + ", ");
110: }
111:
112: // End of program block
113: System.out.println("\n };");
114: System.out
115: .println("\n private static final REProgram "
116: + name
117: + " = new REProgram("
118: + instructions + ");");
119: } catch (RESyntaxException e) {
120: System.out.println("Syntax error in expression \""
121: + arg[i] + "\": " + e.toString());
122: } catch (Exception e) {
123: System.out.println("Unexpected exception: "
124: + e.toString());
125: } catch (Error e) {
126: System.out.println("Internal error: " + e.toString());
127: }
128: }
129: }
130: }
|