1 /* 2 * reserved comment block 3 * DO NOT REMOVE OR ALTER! 4 */ 5 /* 6 * Copyright 1999-2004 The Apache Software Foundation. 7 * 8 * Licensed under the Apache License, Version 2.0 (the "License"); 9 * you may not use this file except in compliance with the License. 10 * You may obtain a copy of the License at 11 * 12 * http://www.apache.org/licenses/LICENSE-2.0 13 * 14 * Unless required by applicable law or agreed to in writing, software 15 * distributed under the License is distributed on an "AS IS" BASIS, 16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 17 * See the License for the specific language governing permissions and 18 * limitations under the License. 19 */ 20 21 package com.sun.org.apache.regexp.internal; 22 23 import com.sun.org.apache.regexp.internal.RECompiler; 24 import com.sun.org.apache.regexp.internal.RESyntaxException; 25 26 /** 27 * 'recompile' is a command line tool that pre-compiles one or more regular expressions 28 * for use with the regular expression matcher class 'RE'. For example, the command 29 * "java recompile a*b" produces output like this: 30 * 31 * <pre> 32 * 33 * // Pre-compiled regular expression "a*b" 34 * char[] re1Instructions = 35 * { 36 * 0x007c, 0x0000, 0x001a, 0x007c, 0x0000, 0x000d, 0x0041, 37 * 0x0001, 0x0004, 0x0061, 0x007c, 0x0000, 0x0003, 0x0047, 38 * 0x0000, 0xfff6, 0x007c, 0x0000, 0x0003, 0x004e, 0x0000, 39 * 0x0003, 0x0041, 0x0001, 0x0004, 0x0062, 0x0045, 0x0000, 40 * 0x0000, 41 * }; 42 * 43 * REProgram re1 = new REProgram(re1Instructions); 44 * 45 * </pre> 46 * 47 * By pasting this output into your code, you can construct a regular expression matcher 48 * (RE) object directly from the pre-compiled data (the character array re1), thus avoiding 49 * the overhead of compiling the expression at runtime. For example: 50 * 51 * <pre> 52 * 53 * RE r = new RE(re1); 54 * 55 * </pre> 56 * 57 * @see RE 58 * @see RECompiler 59 * 60 * @author <a href="mailto:jonl@muppetlabs.com">Jonathan Locke</a> 61 */ 62 public class recompile 63 { 64 /** 65 * Main application entrypoint. 66 * @param arg Command line arguments 67 */ 68 static public void main(String[] arg) 69 { 70 // Create a compiler object 71 RECompiler r = new RECompiler(); 72 73 // Print usage if arguments are incorrect 74 if (arg.length <= 0 || arg.length % 2 != 0) 75 { 76 System.out.println("Usage: recompile <patternname> <pattern>"); 77 System.exit(0); 78 } 79 80 // Loop through arguments, compiling each 81 for (int i = 0; i < arg.length; i += 2) 82 { 83 try 84 { 85 // Compile regular expression 86 String name = arg[i]; 87 String pattern = arg[i+1]; 88 String instructions = name + "PatternInstructions"; 89 90 // Output program as a nice, formatted character array 91 System.out.print("\n // Pre-compiled regular expression '" + pattern + "'\n" 92 + " private static char[] " + instructions + " = \n {"); 93 94 // Compile program for pattern 95 REProgram program = r.compile(pattern); 96 97 // Number of columns in output 98 int numColumns = 7; 99 100 // Loop through program 101 char[] p = program.getInstructions(); 102 for (int j = 0; j < p.length; j++) 103 { 104 // End of column? 105 if ((j % numColumns) == 0) 106 { 107 System.out.print("\n "); 108 } 109 110 // Print character as padded hex number 111 String hex = Integer.toHexString(p[j]); 112 while (hex.length() < 4) 113 { 114 hex = "0" + hex; 115 } 116 System.out.print("0x" + hex + ", "); 117 } 118 119 // End of program block 120 System.out.println("\n };"); 121 System.out.println("\n private static RE " + name + "Pattern = new RE(new REProgram(" + instructions + "));"); 122 } 123 catch (RESyntaxException e) 124 { 125 System.out.println("Syntax error in expression \"" + arg[i] + "\": " + e.toString()); 126 } 127 catch (Exception e) 128 { 129 System.out.println("Unexpected exception: " + e.toString()); 130 } 131 catch (Error e) 132 { 133 System.out.println("Internal error: " + e.toString()); 134 } 135 } 136 } 137 }