KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > org > apache > regexp > internal > recompile


1 package com.sun.org.apache.regexp.internal;
2
3 /*
4  * ====================================================================
5  *
6  * The Apache Software License, Version 1.1
7  *
8  * Copyright (c) 1999 The Apache Software Foundation. All rights
9  * reserved.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  *
15  * 1. Redistributions of source code must retain the above copyright
16  * notice, this list of conditions and the following disclaimer.
17  *
18  * 2. Redistributions in binary form must reproduce the above copyright
19  * notice, this list of conditions and the following disclaimer in
20  * the documentation and/or other materials provided with the
21  * distribution.
22  *
23  * 3. The end-user documentation included with the redistribution, if
24  * any, must include the following acknowlegement:
25  * "This product includes software developed by the
26  * Apache Software Foundation (http://www.apache.org/)."
27  * Alternately, this acknowlegement may appear in the software itself,
28  * if and wherever such third-party acknowlegements normally appear.
29  *
30  * 4. The names "The Jakarta Project", "Jakarta-Regexp", and "Apache Software
31  * Foundation" must not be used to endorse or promote products derived
32  * from this software without prior written permission. For written
33  * permission, please contact apache@apache.org.
34  *
35  * 5. Products derived from this software may not be called "Apache"
36  * nor may "Apache" appear in their names without prior written
37  * permission of the Apache Group.
38  *
39  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
40  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
41  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
42  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
43  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
45  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
46  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
47  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
48  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
49  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
50  * SUCH DAMAGE.
51  * ====================================================================
52  *
53  * This software consists of voluntary contributions made by many
54  * individuals on behalf of the Apache Software Foundation. For more
55  * information on the Apache Software Foundation, please see
56  * <http://www.apache.org/>.
57  *
58  */

59
60 import com.sun.org.apache.regexp.internal.RECompiler;
61 import com.sun.org.apache.regexp.internal.RESyntaxException;
62
63 /**
64  * 'recompile' is a command line tool that pre-compiles one or more regular expressions
65  * for use with the regular expression matcher class 'RE'. For example, the command
66  * "java recompile a*b" produces output like this:
67  *
68  * <pre>
69  *
70  * // Pre-compiled regular expression "a*b"
71  * char[] re1Instructions =
72  * {
73  * 0x007c, 0x0000, 0x001a, 0x007c, 0x0000, 0x000d, 0x0041,
74  * 0x0001, 0x0004, 0x0061, 0x007c, 0x0000, 0x0003, 0x0047,
75  * 0x0000, 0xfff6, 0x007c, 0x0000, 0x0003, 0x004e, 0x0000,
76  * 0x0003, 0x0041, 0x0001, 0x0004, 0x0062, 0x0045, 0x0000,
77  * 0x0000,
78  * };
79  *
80  * REProgram re1 = new REProgram(re1Instructions);
81  *
82  * </pre>
83  *
84  * By pasting this output into your code, you can construct a regular expression matcher
85  * (RE) object directly from the pre-compiled data (the character array re1), thus avoiding
86  * the overhead of compiling the expression at runtime. For example:
87  *
88  * <pre>
89  *
90  * RE r = new RE(re1);
91  *
92  * </pre>
93  *
94  * @see RE
95  * @see RECompiler
96  *
97  * @author <a HREF="mailto:jonl@muppetlabs.com">Jonathan Locke</a>
98  * @version $Id: recompile.java,v 1.1 2000/04/27 01:22:33 jon Exp $
99  */

100 public class recompile
101 {
102     /**
103      * Main application entrypoint.
104      * @param arg Command line arguments
105      */

106     static public void _main(String JavaDoc[] arg)
107     {
108         // Create a compiler object
109
RECompiler r = new RECompiler();
110
111         // Print usage if arguments are incorrect
112
if (arg.length <= 0 || arg.length % 2 != 0)
113         {
114             System.out.println("Usage: recompile <patternname> <pattern>");
115             System.exit(0);
116         }
117
118         // Loop through arguments, compiling each
119
for (int i = 0; i < arg.length; i += 2)
120         {
121             try
122             {
123                 // Compile regular expression
124
String JavaDoc name = arg[i];
125                 String JavaDoc pattern = arg[i+1];
126                 String JavaDoc instructions = name + "PatternInstructions";
127
128                 // Output program as a nice, formatted character array
129
System.out.print("\n // Pre-compiled regular expression '" + pattern + "'\n"
130                                  + " private static char[] " + 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                 {
142                     // End of column?
143
if ((j % numColumns) == 0)
144                     {
145                         System.out.print("\n ");
146                     }
147
148                     // Print character as padded hex number
149
String JavaDoc hex = Integer.toHexString(p[j]);
150                     while (hex.length() < 4)
151                     {
152                         hex = "0" + hex;
153                     }
154                     System.out.print("0x" + hex + ", ");
155                 }
156
157                 // End of program block
158
System.out.println("\n };");
159                 System.out.println("\n private static RE " + name + "Pattern = new RE(new REProgram(" + instructions + "));");
160             }
161             catch (RESyntaxException e)
162             {
163                 System.out.println("Syntax error in expression \"" + arg[i] + "\": " + e.toString());
164             }
165             catch (Exception JavaDoc e)
166             {
167                 System.out.println("Unexpected exception: " + e.toString());
168             }
169             catch (Error JavaDoc e)
170             {
171                 System.out.println("Internal error: " + e.toString());
172             }
173         }
174     }
175 }
176
Popular Tags