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