001: package org.apache.regexp;
002:
003: /*
004: * ====================================================================
005: *
006: * The Apache Software License, Version 1.1
007: *
008: * Copyright (c) 1999 The Apache Software Foundation. All rights
009: * reserved.
010: *
011: * Redistribution and use in source and binary forms, with or without
012: * modification, are permitted provided that the following conditions
013: * are met:
014: *
015: * 1. Redistributions of source code must retain the above copyright
016: * notice, this list of conditions and the following disclaimer.
017: *
018: * 2. Redistributions in binary form must reproduce the above copyright
019: * notice, this list of conditions and the following disclaimer in
020: * the documentation and/or other materials provided with the
021: * distribution.
022: *
023: * 3. The end-user documentation included with the redistribution, if
024: * any, must include the following acknowlegement:
025: * "This product includes software developed by the
026: * Apache Software Foundation (http://www.apache.org/)."
027: * Alternately, this acknowlegement may appear in the software itself,
028: * if and wherever such third-party acknowlegements normally appear.
029: *
030: * 4. The names "The Jakarta Project", "Jakarta-Regexp", and "Apache Software
031: * Foundation" must not be used to endorse or promote products derived
032: * from this software without prior written permission. For written
033: * permission, please contact apache@apache.org.
034: *
035: * 5. Products derived from this software may not be called "Apache"
036: * nor may "Apache" appear in their names without prior written
037: * permission of the Apache Group.
038: *
039: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
040: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
041: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
042: * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
043: * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
044: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
045: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
046: * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
047: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
048: * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
049: * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
050: * SUCH DAMAGE.
051: * ====================================================================
052: *
053: * This software consists of voluntary contributions made by many
054: * individuals on behalf of the Apache Software Foundation. For more
055: * information on the Apache Software Foundation, please see
056: * <http://www.apache.org/>.
057: *
058: */
059:
060: import org.apache.regexp.RECompiler;
061: import org.apache.regexp.RESyntaxException;
062:
063: /**
064: * 'recompile' is a command line tool that pre-compiles one or more regular expressions
065: * for use with the regular expression matcher class 'RE'. For example, the command
066: * "java recompile a*b" produces output like this:
067: *
068: * <pre>
069: *
070: * // Pre-compiled regular expression "a*b"
071: * char[] re1Instructions =
072: * {
073: * 0x007c, 0x0000, 0x001a, 0x007c, 0x0000, 0x000d, 0x0041,
074: * 0x0001, 0x0004, 0x0061, 0x007c, 0x0000, 0x0003, 0x0047,
075: * 0x0000, 0xfff6, 0x007c, 0x0000, 0x0003, 0x004e, 0x0000,
076: * 0x0003, 0x0041, 0x0001, 0x0004, 0x0062, 0x0045, 0x0000,
077: * 0x0000,
078: * };
079: *
080: * REProgram re1 = new REProgram(re1Instructions);
081: *
082: * </pre>
083: *
084: * By pasting this output into your code, you can construct a regular expression matcher
085: * (RE) object directly from the pre-compiled data (the character array re1), thus avoiding
086: * the overhead of compiling the expression at runtime. For example:
087: *
088: * <pre>
089: *
090: * RE r = new RE(re1);
091: *
092: * </pre>
093: *
094: * @see RE
095: * @see RECompiler
096: *
097: * @author <a href="mailto:jonl@muppetlabs.com">Jonathan Locke</a>
098: * @version $Id: recompile.java,v 1.1.1.1 2002/01/31 03:14:37 rcm Exp $
099: */
100: public class recompile {
101: /**
102: * Main application entrypoint.
103: * @param arg Command line arguments
104: */
105: static public void main(String[] arg) {
106: // Create a compiler object
107: RECompiler r = new RECompiler();
108:
109: // Print usage if arguments are incorrect
110: if (arg.length <= 0 || arg.length % 2 != 0) {
111: System.out
112: .println("Usage: recompile <patternname> <pattern>");
113: System.exit(0);
114: }
115:
116: // Loop through arguments, compiling each
117: for (int i = 0; i < arg.length; i += 2) {
118: try {
119: // Compile regular expression
120: String name = arg[i];
121: String pattern = arg[i + 1];
122: String instructions = name + "PatternInstructions";
123:
124: // Output program as a nice, formatted character array
125: System.out
126: .print("\n // Pre-compiled regular expression '"
127: + pattern
128: + "'\n"
129: + " private static char[] "
130: + instructions + " = \n {");
131:
132: // Compile program for pattern
133: REProgram program = r.compile(pattern);
134:
135: // Number of columns in output
136: int numColumns = 7;
137:
138: // Loop through program
139: char[] p = program.getInstructions();
140: for (int j = 0; j < p.length; j++) {
141: // End of column?
142: if ((j % numColumns) == 0) {
143: System.out.print("\n ");
144: }
145:
146: // Print character as padded hex number
147: String hex = Integer.toHexString(p[j]);
148: while (hex.length() < 4) {
149: hex = "0" + hex;
150: }
151: System.out.print("0x" + hex + ", ");
152: }
153:
154: // End of program block
155: System.out.println("\n };");
156: System.out.println("\n private static RE " + name
157: + "Pattern = new RE(new REProgram("
158: + instructions + "));");
159: } catch (RESyntaxException e) {
160: System.out.println("Syntax error in expression \""
161: + arg[i] + "\": " + e.toString());
162: } catch (Exception e) {
163: System.out.println("Unexpected exception: "
164: + e.toString());
165: } catch (Error e) {
166: System.out.println("Internal error: " + e.toString());
167: }
168: }
169: }
170: }
|