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

Save This Page
Home » jakarta-regexp-1.5 » org.apache » regexp » [javadoc | source]