8046274: Removing dependency on jakarta-regexp

Reviewed-by: lancea
This commit is contained in:
Joe Wang 2016-05-10 16:19:44 -07:00
parent 0537b18b12
commit 2150ee4ab4
15 changed files with 28 additions and 5362 deletions

View File

@ -3387,7 +3387,6 @@ included with JRE 8, JDK 8, and OpenJDK 8, except where noted:
Apache Commons Math 2.2
Apache Derby 10.10.1.2 [included with JDK 8]
Apache Jakarta BCEL 5.2
Apache Jakarta Regexp 1.4
Apache Santuario XML Security for Java 1.5.4
Apache Xalan-Java 2.7.1
Apache Xerces Java 2.10.0

View File

@ -4,64 +4,29 @@
*/
package com.sun.org.apache.bcel.internal.util;
/* ====================================================================
* The Apache Software License, Version 1.1
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* Copyright (c) 2001 The Apache Software Foundation. All rights
* reserved.
* http://www.apache.org/licenses/LICENSE-2.0
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution,
* if any, must include the following acknowledgment:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowledgment may appear in the software itself,
* if and wherever such third-party acknowledgments normally appear.
*
* 4. The names "Apache" and "Apache Software Foundation" and
* "Apache BCEL" must not be used to endorse or promote products
* derived from this software without prior written permission. For
* written permission, please contact apache@apache.org.
*
* 5. Products derived from this software may not be called "Apache",
* "Apache BCEL", nor may "Apache" appear in their name, without
* prior written permission of the Apache Software Foundation.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*/
import java.util.*;
import com.sun.org.apache.bcel.internal.Constants;
import com.sun.org.apache.bcel.internal.generic.*;
import com.sun.org.apache.regexp.internal.*;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* InstructionFinder is a tool to search for given instructions patterns,
@ -231,28 +196,22 @@ public class InstructionFinder {
if(start == -1)
throw new ClassGenException("Instruction handle " + from +
" not found in instruction list.");
try {
RE regex = new RE(search);
ArrayList matches = new ArrayList();
while(start < il_string.length() && regex.match(il_string, start)) {
int startExpr = regex.getParenStart(0);
int endExpr = regex.getParenEnd(0);
int lenExpr = regex.getParenLength(0);
Pattern regex = Pattern.compile(search);
List<InstructionHandle[]> matches = new ArrayList<>();
Matcher matcher = regex.matcher(il_string);
while(start < il_string.length() && matcher.find(start)) {
int startExpr = matcher.start();
int endExpr = matcher.end();
int lenExpr = endExpr - startExpr;
InstructionHandle[] match = getMatch(startExpr, lenExpr);
InstructionHandle[] match = getMatch(startExpr, lenExpr);
if((constraint == null) || constraint.checkCode(match))
matches.add(match);
start = endExpr;
}
return matches.iterator();
} catch(RESyntaxException e) {
System.err.println(e);
if((constraint == null) || constraint.checkCode(match))
matches.add(match);
start = endExpr;
}
return null;
return matches.iterator();
}
/**

View File

@ -1,76 +0,0 @@
/*
* reserved comment block
* DO NOT REMOVE OR ALTER!
*/
/*
* Copyright 1999-2004 The Apache Software Foundation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.sun.org.apache.regexp.internal;
/**
* Encapsulates char[] as CharacterIterator
*
* @author <a href="mailto:ales.novak@netbeans.com">Ales Novak</a>
*/
public final class CharacterArrayCharacterIterator implements CharacterIterator
{
/** encapsulated */
private final char[] src;
/** offset in the char array */
private final int off;
/** used portion of the array */
private final int len;
/** @param src - encapsulated String */
public CharacterArrayCharacterIterator(char[] src, int off, int len)
{
this.src = src;
this.off = off;
this.len = len;
}
/** @return a substring */
public String substring(int beginIndex, int endIndex)
{
if (endIndex > len) {
throw new IndexOutOfBoundsException("endIndex=" + endIndex
+ "; sequence size=" + len);
}
if (beginIndex < 0 || beginIndex > endIndex) {
throw new IndexOutOfBoundsException("beginIndex=" + beginIndex
+ "; endIndex=" + endIndex);
}
return new String(src, off + beginIndex, endIndex - beginIndex);
}
/** @return a substring */
public String substring(int beginIndex)
{
return substring(beginIndex, len);
}
/** @return a character at the specified position. */
public char charAt(int pos)
{
return src[off + pos];
}
/** @return <tt>true</tt> iff if the specified index is after the end of the character stream */
public boolean isEnd(int pos)
{
return (pos >= len);
}
}

View File

@ -1,42 +0,0 @@
/*
* reserved comment block
* DO NOT REMOVE OR ALTER!
*/
/*
* Copyright 1999-2004 The Apache Software Foundation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.sun.org.apache.regexp.internal;
/**
* Encapsulates different types of character sources - String, InputStream, ...
* Defines a set of common methods
*
* @author <a href="mailto:ales.novak@netbeans.com">Ales Novak</a>
*/
public interface CharacterIterator
{
/** @return a substring */
String substring(int beginIndex, int endIndex);
/** @return a substring */
String substring(int beginIndex);
/** @return a character at the specified position. */
char charAt(int pos);
/** @return <tt>true</tt> iff if the specified index is after the end of the character stream */
boolean isEnd(int pos);
}

View File

@ -1,225 +0,0 @@
/*
* reserved comment block
* DO NOT REMOVE OR ALTER!
*/
/*
* Copyright 1999-2004 The Apache Software Foundation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.sun.org.apache.regexp.internal;
import java.io.PrintWriter;
import java.util.Hashtable;
/**
* A subclass of RECompiler which can dump a regular expression program
* for debugging purposes.
*
* @author <a href="mailto:jonl@muppetlabs.com">Jonathan Locke</a>
*/
public class REDebugCompiler extends RECompiler
{
/**
* Mapping from opcodes to descriptive strings
*/
static Hashtable hashOpcode = new Hashtable();
static
{
hashOpcode.put(new Integer(RE.OP_RELUCTANTSTAR), "OP_RELUCTANTSTAR");
hashOpcode.put(new Integer(RE.OP_RELUCTANTPLUS), "OP_RELUCTANTPLUS");
hashOpcode.put(new Integer(RE.OP_RELUCTANTMAYBE), "OP_RELUCTANTMAYBE");
hashOpcode.put(new Integer(RE.OP_END), "OP_END");
hashOpcode.put(new Integer(RE.OP_BOL), "OP_BOL");
hashOpcode.put(new Integer(RE.OP_EOL), "OP_EOL");
hashOpcode.put(new Integer(RE.OP_ANY), "OP_ANY");
hashOpcode.put(new Integer(RE.OP_ANYOF), "OP_ANYOF");
hashOpcode.put(new Integer(RE.OP_BRANCH), "OP_BRANCH");
hashOpcode.put(new Integer(RE.OP_ATOM), "OP_ATOM");
hashOpcode.put(new Integer(RE.OP_STAR), "OP_STAR");
hashOpcode.put(new Integer(RE.OP_PLUS), "OP_PLUS");
hashOpcode.put(new Integer(RE.OP_MAYBE), "OP_MAYBE");
hashOpcode.put(new Integer(RE.OP_NOTHING), "OP_NOTHING");
hashOpcode.put(new Integer(RE.OP_GOTO), "OP_GOTO");
hashOpcode.put(new Integer(RE.OP_ESCAPE), "OP_ESCAPE");
hashOpcode.put(new Integer(RE.OP_OPEN), "OP_OPEN");
hashOpcode.put(new Integer(RE.OP_CLOSE), "OP_CLOSE");
hashOpcode.put(new Integer(RE.OP_BACKREF), "OP_BACKREF");
hashOpcode.put(new Integer(RE.OP_POSIXCLASS), "OP_POSIXCLASS");
hashOpcode.put(new Integer(RE.OP_OPEN_CLUSTER), "OP_OPEN_CLUSTER");
hashOpcode.put(new Integer(RE.OP_CLOSE_CLUSTER), "OP_CLOSE_CLUSTER");
}
/**
* Returns a descriptive string for an opcode.
* @param opcode Opcode to convert to a string
* @return Description of opcode
*/
String opcodeToString(char opcode)
{
// Get string for opcode
String ret =(String)hashOpcode.get(new Integer(opcode));
// Just in case we have a corrupt program
if (ret == null)
{
ret = "OP_????";
}
return ret;
}
/**
* Return a string describing a (possibly unprintable) character.
* @param c Character to convert to a printable representation
* @return String representation of character
*/
String charToString(char c)
{
// If it's unprintable, convert to '\###'
if (c < ' ' || c > 127)
{
return "\\" + (int)c;
}
// Return the character as a string
return String.valueOf(c);
}
/**
* Returns a descriptive string for a node in a regular expression program.
* @param node Node to describe
* @return Description of node
*/
String nodeToString(int node)
{
// Get opcode and opdata for node
char opcode = instruction[node + RE.offsetOpcode];
int opdata = (int)instruction[node + RE.offsetOpdata];
// Return opcode as a string and opdata value
return opcodeToString(opcode) + ", opdata = " + opdata;
}
/**
* Inserts a node with a given opcode and opdata at insertAt. The node relative next
* pointer is initialized to 0.
* @param opcode Opcode for new node
* @param opdata Opdata for new node (only the low 16 bits are currently used)
* @param insertAt Index at which to insert the new node in the program * /
void nodeInsert(char opcode, int opdata, int insertAt) {
System.out.println( "====> " + opcode + " " + opdata + " " + insertAt );
PrintWriter writer = new PrintWriter( System.out );
dumpProgram( writer );
super.nodeInsert( opcode, opdata, insertAt );
System.out.println( "====< " );
dumpProgram( writer );
writer.flush();
}/**/
/**
* Appends a node to the end of a node chain
* @param node Start of node chain to traverse
* @param pointTo Node to have the tail of the chain point to * /
void setNextOfEnd(int node, int pointTo) {
System.out.println( "====> " + node + " " + pointTo );
PrintWriter writer = new PrintWriter( System.out );
dumpProgram( writer );
super.setNextOfEnd( node, pointTo );
System.out.println( "====< " );
dumpProgram( writer );
writer.flush();
}/**/
/**
* Dumps the current program to a PrintWriter
* @param p PrintWriter for program dump output
*/
public void dumpProgram(PrintWriter p)
{
// Loop through the whole program
for (int i = 0; i < lenInstruction; )
{
// Get opcode, opdata and next fields of current program node
char opcode = instruction[i + RE.offsetOpcode];
char opdata = instruction[i + RE.offsetOpdata];
short next = (short)instruction[i + RE.offsetNext];
// Display the current program node
p.print(i + ". " + nodeToString(i) + ", next = ");
// If there's no next, say 'none', otherwise give absolute index of next node
if (next == 0)
{
p.print("none");
}
else
{
p.print(i + next);
}
// Move past node
i += RE.nodeSize;
// If character class
if (opcode == RE.OP_ANYOF)
{
// Opening bracket for start of char class
p.print(", [");
// Show each range in the char class
int rangeCount = opdata;
for (int r = 0; r < rangeCount; r++)
{
// Get first and last chars in range
char charFirst = instruction[i++];
char charLast = instruction[i++];
// Print range as X-Y, unless range encompasses only one char
if (charFirst == charLast)
{
p.print(charToString(charFirst));
}
else
{
p.print(charToString(charFirst) + "-" + charToString(charLast));
}
}
// Annotate the end of the char class
p.print("]");
}
// If atom
if (opcode == RE.OP_ATOM)
{
// Open quote
p.print(", \"");
// Print each character in the atom
for (int len = opdata; len-- != 0; )
{
p.print(charToString(instruction[i++]));
}
// Close quote
p.print("\"");
}
// Print a newline
p.println("");
}
}
}

View File

@ -1,158 +0,0 @@
/*
* reserved comment block
* DO NOT REMOVE OR ALTER!
*/
/*
* Copyright 1999-2004 The Apache Software Foundation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.sun.org.apache.regexp.internal;
import java.io.Serializable;
/**
* A class that holds compiled regular expressions. This is exposed mainly
* for use by the recompile utility (which helps you produce precompiled
* REProgram objects). You should not otherwise need to work directly with
* this class.
*
* @see RE
* @see RECompiler
*
* @author <a href="mailto:jonl@muppetlabs.com">Jonathan Locke</a>
*/
public class REProgram implements Serializable
{
static final int OPT_HASBACKREFS = 1;
char[] instruction; // The compiled regular expression 'program'
int lenInstruction; // The amount of the instruction buffer in use
char[] prefix; // Prefix string optimization
int flags; // Optimization flags (REProgram.OPT_*)
int maxParens = -1;
/**
* Constructs a program object from a character array
* @param instruction Character array with RE opcode instructions in it
*/
public REProgram(char[] instruction)
{
this(instruction, instruction.length);
}
/**
* Constructs a program object from a character array
* @param parens Count of parens in the program
* @param instruction Character array with RE opcode instructions in it
*/
public REProgram(int parens, char[] instruction)
{
this(instruction, instruction.length);
this.maxParens = parens;
}
/**
* Constructs a program object from a character array
* @param instruction Character array with RE opcode instructions in it
* @param lenInstruction Amount of instruction array in use
*/
public REProgram(char[] instruction, int lenInstruction)
{
setInstructions(instruction, lenInstruction);
}
/**
* Returns a copy of the current regular expression program in a character
* array that is exactly the right length to hold the program. If there is
* no program compiled yet, getInstructions() will return null.
* @return A copy of the current compiled RE program
*/
public char[] getInstructions()
{
// Ensure program has been compiled!
if (lenInstruction != 0)
{
// Return copy of program
char[] ret = new char[lenInstruction];
System.arraycopy(instruction, 0, ret, 0, lenInstruction);
return ret;
}
return null;
}
/**
* Sets a new regular expression program to run. It is this method which
* performs any special compile-time search optimizations. Currently only
* two optimizations are in place - one which checks for backreferences
* (so that they can be lazily allocated) and another which attempts to
* find an prefix anchor string so that substantial amounts of input can
* potentially be skipped without running the actual program.
* @param instruction Program instruction buffer
* @param lenInstruction Length of instruction buffer in use
*/
public void setInstructions(char[] instruction, int lenInstruction)
{
// Save reference to instruction array
this.instruction = instruction;
this.lenInstruction = lenInstruction;
// Initialize other program-related variables
flags = 0;
prefix = null;
// Try various compile-time optimizations if there's a program
if (instruction != null && lenInstruction != 0)
{
// If the first node is a branch
if (lenInstruction >= RE.nodeSize && instruction[0 + RE.offsetOpcode] == RE.OP_BRANCH)
{
// to the end node
int next = instruction[0 + RE.offsetNext];
if (instruction[next + RE.offsetOpcode] == RE.OP_END)
{
// and the branch starts with an atom
if (lenInstruction >= (RE.nodeSize * 2) && instruction[RE.nodeSize + RE.offsetOpcode] == RE.OP_ATOM)
{
// then get that atom as an prefix because there's no other choice
int lenAtom = instruction[RE.nodeSize + RE.offsetOpdata];
prefix = new char[lenAtom];
System.arraycopy(instruction, RE.nodeSize * 2, prefix, 0, lenAtom);
}
}
}
BackrefScanLoop:
// Check for backreferences
for (int i = 0; i < lenInstruction; i += RE.nodeSize)
{
switch (instruction[i + RE.offsetOpcode])
{
case RE.OP_ANYOF:
i += (instruction[i + RE.offsetOpdata] * 2);
break;
case RE.OP_ATOM:
i += instruction[i + RE.offsetOpdata];
break;
case RE.OP_BACKREF:
flags |= OPT_HASBACKREFS;
break BackrefScanLoop;
}
}
}
}
}

View File

@ -1,43 +0,0 @@
/*
* reserved comment block
* DO NOT REMOVE OR ALTER!
*/
/*
* Copyright 1999-2004 The Apache Software Foundation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.sun.org.apache.regexp.internal;
/**
* Exception thrown to indicate a syntax error in a regular expression.
* This is a non-checked exception because you should only have problems compiling
* a regular expression during development.
* If you are making regular expresion programs dynamically then you can catch it
* if you wish. But should not be forced to.
*
* @author <a href="mailto:jonl@muppetlabs.com">Jonathan Locke</a>
* @author <a href="mailto:gholam@xtra.co.nz>Michael McCallum</a>
*/
public class RESyntaxException extends RuntimeException
{
/**
* Constructor.
* @param s Further description of the syntax error
*/
public RESyntaxException(String s)
{
super("Syntax error: " + s);
}
}

View File

@ -1,883 +0,0 @@
/*
* reserved comment block
* DO NOT REMOVE OR ALTER!
*/
/*
* Copyright 1999-2004 The Apache Software Foundation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.sun.org.apache.regexp.internal;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.io.File;
import java.io.ByteArrayOutputStream;
import java.io.ObjectOutputStream;
import java.io.ByteArrayInputStream;
import java.io.ObjectInputStream;
import java.io.StringBufferInputStream;
import java.io.StringReader;
import java.io.IOException;
/**
* Data driven (and optionally interactive) testing harness to exercise regular
* expression compiler and matching engine.
*
* @author <a href="mailto:jonl@muppetlabs.com">Jonathan Locke</a>
* @author <a href="mailto:jon@latchkey.com">Jon S. Stevens</a>
* @author <a href="mailto:gholam@xtra.co.nz">Michael McCallum</a>
*/
public class RETest
{
// True if we want to see output from success cases
static final boolean showSuccesses = false;
// A new line character.
static final String NEW_LINE = System.getProperty( "line.separator" );
// Construct a debug compiler
REDebugCompiler compiler = new REDebugCompiler();
/**
* Main program entrypoint. If an argument is given, it will be compiled
* and interactive matching will ensue. If no argument is given, the
* file RETest.txt will be used as automated testing input.
* @param args Command line arguments (optional regular expression)
*/
public static void main(String[] args)
{
try
{
if (!test( args )) {
System.exit(1);
}
}
catch (Exception e)
{
e.printStackTrace();
System.exit(1);
}
}
/**
* Testing entrypoint.
* @param args Command line arguments
* @exception Exception thrown in case of error
*/
public static boolean test( String[] args ) throws Exception
{
RETest test = new RETest();
// Run interactive tests against a single regexp
if (args.length == 2)
{
test.runInteractiveTests(args[1]);
}
else if (args.length == 1)
{
// Run automated tests
test.runAutomatedTests(args[0]);
}
else
{
System.out.println( "Usage: RETest ([-i] [regex]) ([/path/to/testfile.txt])" );
System.out.println( "By Default will run automated tests from file 'docs/RETest.txt' ..." );
System.out.println();
test.runAutomatedTests("docs/RETest.txt");
}
return test.failures == 0;
}
/**
* Constructor
*/
public RETest()
{
}
/**
* Compile and test matching against a single expression
* @param expr Expression to compile and test
*/
void runInteractiveTests(String expr)
{
RE r = new RE();
try
{
// Compile expression
r.setProgram(compiler.compile(expr));
// Show expression
say("" + NEW_LINE + "" + expr + "" + NEW_LINE + "");
// Show program for compiled expression
PrintWriter writer = new PrintWriter( System.out );
compiler.dumpProgram( writer );
writer.flush();
boolean running = true;
// Test matching against compiled expression
while ( running )
{
// Read from keyboard
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.print("> ");
System.out.flush();
String match = br.readLine();
if ( match != null )
{
// Try a match against the keyboard input
if (r.match(match))
{
say("Match successful.");
}
else
{
say("Match failed.");
}
// Show subparen registers
showParens(r);
}
else
{
running = false;
System.out.println();
}
}
}
catch (Exception e)
{
say("Error: " + e.toString());
e.printStackTrace();
}
}
/**
* Exit with a fatal error.
* @param s Last famous words before exiting
*/
void die(String s)
{
say("FATAL ERROR: " + s);
System.exit(-1);
}
/**
* Fail with an error. Will print a big failure message to System.out.
*
* @param log Output before failure
* @param s Failure description
*/
void fail(StringBuffer log, String s)
{
System.out.print(log.toString());
fail(s);
}
/**
* Fail with an error. Will print a big failure message to System.out.
*
* @param s Failure description
*/
void fail(String s)
{
failures++;
say("" + NEW_LINE + "");
say("*******************************************************");
say("********************* FAILURE! **********************");
say("*******************************************************");
say("" + NEW_LINE + "");
say(s);
say("");
// make sure the writer gets flushed.
if (compiler != null) {
PrintWriter writer = new PrintWriter( System.out );
compiler.dumpProgram( writer );
writer.flush();
say("" + NEW_LINE + "");
}
}
/**
* Say something to standard out
* @param s What to say
*/
void say(String s)
{
System.out.println(s);
}
/**
* Dump parenthesized subexpressions found by a regular expression matcher object
* @param r Matcher object with results to show
*/
void showParens(RE r)
{
// Loop through each paren
for (int i = 0; i < r.getParenCount(); i++)
{
// Show paren register
say("$" + i + " = " + r.getParen(i));
}
}
/*
* number in automated test
*/
int testCount = 0;
/*
* Count of failures in automated test
*/
int failures = 0;
/**
* Run automated tests in RETest.txt file (from Perl 4.0 test battery)
* @exception Exception thrown in case of error
*/
void runAutomatedTests(String testDocument) throws Exception
{
long ms = System.currentTimeMillis();
// Some unit tests
testPrecompiledRE();
testSplitAndGrep();
testSubst();
testOther();
// Test from script file
File testInput = new File(testDocument);
if (! testInput.exists()) {
throw new Exception ("Could not find: " + testDocument);
}
BufferedReader br = new BufferedReader(new FileReader(testInput));
try
{
// While input is available, parse lines
while (br.ready())
{
RETestCase testcase = getNextTestCase(br);
if (testcase != null) {
testcase.runTest();
}
}
}
finally
{
br.close();
}
// Show match time
say(NEW_LINE + NEW_LINE + "Match time = " + (System.currentTimeMillis() - ms) + " ms.");
// Print final results
if (failures > 0) {
say("*************** THERE ARE FAILURES! *******************");
}
say("Tests complete. " + testCount + " tests, " + failures + " failure(s).");
}
/**
* Run automated unit test
* @exception Exception thrown in case of error
*/
void testOther() throws Exception
{
// Serialization test 1: Compile regexp and serialize/deserialize it
RE r = new RE("(a*)b");
say("Serialized/deserialized (a*)b");
ByteArrayOutputStream out = new ByteArrayOutputStream(128);
new ObjectOutputStream(out).writeObject(r);
ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
r = (RE)new ObjectInputStream(in).readObject();
if (!r.match("aaab"))
{
fail("Did not match 'aaab' with deserialized RE.");
} else {
say("aaaab = true");
showParens(r);
}
// Serialization test 2: serialize/deserialize used regexp
out.reset();
say("Deserialized (a*)b");
new ObjectOutputStream(out).writeObject(r);
in = new ByteArrayInputStream(out.toByteArray());
r = (RE)new ObjectInputStream(in).readObject();
if (r.getParenCount() != 0)
{
fail("Has parens after deserialization.");
}
if (!r.match("aaab"))
{
fail("Did not match 'aaab' with deserialized RE.");
} else {
say("aaaab = true");
showParens(r);
}
// Test MATCH_CASEINDEPENDENT
r = new RE("abc(\\w*)");
say("MATCH_CASEINDEPENDENT abc(\\w*)");
r.setMatchFlags(RE.MATCH_CASEINDEPENDENT);
say("abc(d*)");
if (!r.match("abcddd"))
{
fail("Did not match 'abcddd'.");
} else {
say("abcddd = true");
showParens(r);
}
if (!r.match("aBcDDdd"))
{
fail("Did not match 'aBcDDdd'.");
} else {
say("aBcDDdd = true");
showParens(r);
}
if (!r.match("ABCDDDDD"))
{
fail("Did not match 'ABCDDDDD'.");
} else {
say("ABCDDDDD = true");
showParens(r);
}
r = new RE("(A*)b\\1");
r.setMatchFlags(RE.MATCH_CASEINDEPENDENT);
if (!r.match("AaAaaaBAAAAAA"))
{
fail("Did not match 'AaAaaaBAAAAAA'.");
} else {
say("AaAaaaBAAAAAA = true");
showParens(r);
}
r = new RE("[A-Z]*");
r.setMatchFlags(RE.MATCH_CASEINDEPENDENT);
if (!r.match("CaBgDe12"))
{
fail("Did not match 'CaBgDe12'.");
} else {
say("CaBgDe12 = true");
showParens(r);
}
// Test MATCH_MULTILINE. Test for eol/bol symbols.
r = new RE("^abc$", RE.MATCH_MULTILINE);
if (!r.match("\nabc")) {
fail("\"\\nabc\" doesn't match \"^abc$\"");
}
if (!r.match("\rabc")) {
fail("\"\\rabc\" doesn't match \"^abc$\"");
}
if (!r.match("\r\nabc")) {
fail("\"\\r\\nabc\" doesn't match \"^abc$\"");
}
if (!r.match("\u0085abc")) {
fail("\"\\u0085abc\" doesn't match \"^abc$\"");
}
if (!r.match("\u2028abc")) {
fail("\"\\u2028abc\" doesn't match \"^abc$\"");
}
if (!r.match("\u2029abc")) {
fail("\"\\u2029abc\" doesn't match \"^abc$\"");
}
// Test MATCH_MULTILINE. Test that '.' does not matches new line.
r = new RE("^a.*b$", RE.MATCH_MULTILINE);
if (r.match("a\nb")) {
fail("\"a\\nb\" matches \"^a.*b$\"");
}
if (r.match("a\rb")) {
fail("\"a\\rb\" matches \"^a.*b$\"");
}
if (r.match("a\r\nb")) {
fail("\"a\\r\\nb\" matches \"^a.*b$\"");
}
if (r.match("a\u0085b")) {
fail("\"a\\u0085b\" matches \"^a.*b$\"");
}
if (r.match("a\u2028b")) {
fail("\"a\\u2028b\" matches \"^a.*b$\"");
}
if (r.match("a\u2029b")) {
fail("\"a\\u2029b\" matches \"^a.*b$\"");
}
}
private void testPrecompiledRE()
{
// Pre-compiled regular expression "a*b"
char[] re1Instructions =
{
0x007c, 0x0000, 0x001a, 0x007c, 0x0000, 0x000d, 0x0041,
0x0001, 0x0004, 0x0061, 0x007c, 0x0000, 0x0003, 0x0047,
0x0000, 0xfff6, 0x007c, 0x0000, 0x0003, 0x004e, 0x0000,
0x0003, 0x0041, 0x0001, 0x0004, 0x0062, 0x0045, 0x0000,
0x0000,
};
REProgram re1 = new REProgram(re1Instructions);
// Simple test of pre-compiled regular expressions
RE r = new RE(re1);
say("a*b");
boolean result = r.match("aaab");
say("aaab = " + result);
showParens(r);
if (!result) {
fail("\"aaab\" doesn't match to precompiled \"a*b\"");
}
result = r.match("b");
say("b = " + result);
showParens(r);
if (!result) {
fail("\"b\" doesn't match to precompiled \"a*b\"");
}
result = r.match("c");
say("c = " + result);
showParens(r);
if (result) {
fail("\"c\" matches to precompiled \"a*b\"");
}
result = r.match("ccccaaaaab");
say("ccccaaaaab = " + result);
showParens(r);
if (!result) {
fail("\"ccccaaaaab\" doesn't match to precompiled \"a*b\"");
}
}
private void testSplitAndGrep()
{
String[] expected = {"xxxx", "xxxx", "yyyy", "zzz"};
RE r = new RE("a*b");
String[] s = r.split("xxxxaabxxxxbyyyyaaabzzz");
for (int i = 0; i < expected.length && i < s.length; i++) {
assertEquals("Wrong splitted part", expected[i], s[i]);
}
assertEquals("Wrong number of splitted parts", expected.length,
s.length);
r = new RE("x+");
expected = new String[] {"xxxx", "xxxx"};
s = r.grep(s);
for (int i = 0; i < s.length; i++)
{
say("s[" + i + "] = " + s[i]);
assertEquals("Grep fails", expected[i], s[i]);
}
assertEquals("Wrong number of string found by grep", expected.length,
s.length);
}
private void testSubst()
{
RE r = new RE("a*b");
String expected = "-foo-garply-wacky-";
String actual = r.subst("aaaabfooaaabgarplyaaabwackyb", "-");
assertEquals("Wrong result of substitution in \"a*b\"", expected, actual);
// Test subst() with backreferences
r = new RE("http://[\\.\\w\\-\\?/~_@&=%]+");
actual = r.subst("visit us: http://www.apache.org!",
"1234<a href=\"$0\">$0</a>", RE.REPLACE_BACKREFERENCES);
assertEquals("Wrong subst() result", "visit us: 1234<a href=\"http://www.apache.org\">http://www.apache.org</a>!", actual);
// Test subst() with backreferences without leading characters
// before first backreference
r = new RE("(.*?)=(.*)");
actual = r.subst("variable=value",
"$1_test_$212", RE.REPLACE_BACKREFERENCES);
assertEquals("Wrong subst() result", "variable_test_value12", actual);
// Test subst() with NO backreferences
r = new RE("^a$");
actual = r.subst("a",
"b", RE.REPLACE_BACKREFERENCES);
assertEquals("Wrong subst() result", "b", actual);
// Test subst() with NO backreferences
r = new RE("^a$", RE.MATCH_MULTILINE);
actual = r.subst("\r\na\r\n",
"b", RE.REPLACE_BACKREFERENCES);
assertEquals("Wrong subst() result", "\r\nb\r\n", actual);
}
public void assertEquals(String message, String expected, String actual)
{
if (expected != null && !expected.equals(actual)
|| actual != null && !actual.equals(expected))
{
fail(message + " (expected \"" + expected
+ "\", actual \"" + actual + "\")");
}
}
public void assertEquals(String message, int expected, int actual)
{
if (expected != actual) {
fail(message + " (expected \"" + expected
+ "\", actual \"" + actual + "\")");
}
}
/**
* Converts yesno string to boolean.
* @param yesno string representation of expected result
* @return true if yesno is "YES", false if yesno is "NO"
* stops program otherwise.
*/
private boolean getExpectedResult(String yesno)
{
if ("NO".equals(yesno))
{
return false;
}
else if ("YES".equals(yesno))
{
return true;
}
else
{
// Bad test script
die("Test script error!");
return false; //to please javac
}
}
/**
* Finds next test description in a given script.
* @param br <code>BufferedReader</code> for a script file
* @return strign tag for next test description
* @exception IOException if some io problems occured
*/
private String findNextTest(BufferedReader br) throws IOException
{
String number = "";
while (br.ready())
{
number = br.readLine();
if (number == null)
{
break;
}
number = number.trim();
if (number.startsWith("#"))
{
break;
}
if (!number.equals(""))
{
say("Script error. Line = " + number);
System.exit(-1);
}
}
return number;
}
/**
* Creates testcase for the next test description in the script file.
* @param br <code>BufferedReader</code> for script file.
* @return a new tescase or null.
* @exception IOException if some io problems occured
*/
private RETestCase getNextTestCase(BufferedReader br) throws IOException
{
// Find next re test case
final String tag = findNextTest(br);
// Are we done?
if (!br.ready())
{
return null;
}
// Get expression
final String expr = br.readLine();
// Get test information
final String matchAgainst = br.readLine();
final boolean badPattern = "ERR".equals(matchAgainst);
boolean shouldMatch = false;
int expectedParenCount = 0;
String[] expectedParens = null;
if (!badPattern) {
shouldMatch = getExpectedResult(br.readLine().trim());
if (shouldMatch) {
expectedParenCount = Integer.parseInt(br.readLine().trim());
expectedParens = new String[expectedParenCount];
for (int i = 0; i < expectedParenCount; i++) {
expectedParens[i] = br.readLine();
}
}
}
return new RETestCase(this, tag, expr, matchAgainst, badPattern,
shouldMatch, expectedParens);
}
}
final class RETestCase
{
final private StringBuffer log = new StringBuffer();
final private int number;
final private String tag; // number from script file
final private String pattern;
final private String toMatch;
final private boolean badPattern;
final private boolean shouldMatch;
final private String[] parens;
final private RETest test;
private RE regexp;
public RETestCase(RETest test, String tag, String pattern,
String toMatch, boolean badPattern,
boolean shouldMatch, String[] parens)
{
this.number = ++test.testCount;
this.test = test;
this.tag = tag;
this.pattern = pattern;
this.toMatch = toMatch;
this.badPattern = badPattern;
this.shouldMatch = shouldMatch;
if (parens != null) {
this.parens = new String[parens.length];
for (int i = 0; i < parens.length; i++) {
this.parens[i] = parens[i];
}
} else {
this.parens = null;
}
}
public void runTest()
{
test.say(tag + "(" + number + "): " + pattern);
if (testCreation()) {
testMatch();
}
}
boolean testCreation()
{
try
{
// Compile it
regexp = new RE();
regexp.setProgram(test.compiler.compile(pattern));
// Expression didn't cause an expected error
if (badPattern)
{
test.fail(log, "Was expected to be an error, but wasn't.");
return false;
}
return true;
}
// Some expressions *should* cause exceptions to be thrown
catch (Exception e)
{
// If it was supposed to be an error, report success and continue
if (badPattern)
{
log.append(" Match: ERR\n");
success("Produces an error (" + e.toString() + "), as expected.");
return false;
}
// Wasn't supposed to be an error
String message = (e.getMessage() == null) ? e.toString() : e.getMessage();
test.fail(log, "Produces an unexpected exception \"" + message + "\"");
e.printStackTrace();
}
catch (Error e)
{
// Internal error happened
test.fail(log, "Compiler threw fatal error \"" + e.getMessage() + "\"");
e.printStackTrace();
}
return false;
}
private void testMatch()
{
log.append(" Match against: '" + toMatch + "'\n");
// Try regular matching
try
{
// Match against the string
boolean result = regexp.match(toMatch);
log.append(" Matched: " + (result ? "YES" : "NO") + "\n");
// Check result, parens, and iterators
if (checkResult(result) && (!shouldMatch || checkParens()))
{
// test match(CharacterIterator, int)
// for every CharacterIterator implementation.
log.append(" Match using StringCharacterIterator\n");
if (!tryMatchUsingCI(new StringCharacterIterator(toMatch)))
return;
log.append(" Match using CharacterArrayCharacterIterator\n");
if (!tryMatchUsingCI(new CharacterArrayCharacterIterator(toMatch.toCharArray(), 0, toMatch.length())))
return;
log.append(" Match using StreamCharacterIterator\n");
if (!tryMatchUsingCI(new StreamCharacterIterator(new StringBufferInputStream(toMatch))))
return;
log.append(" Match using ReaderCharacterIterator\n");
if (!tryMatchUsingCI(new ReaderCharacterIterator(new StringReader(toMatch))))
return;
}
}
// Matcher blew it
catch(Exception e)
{
test.fail(log, "Matcher threw exception: " + e.toString());
e.printStackTrace();
}
// Internal error
catch(Error e)
{
test.fail(log, "Matcher threw fatal error \"" + e.getMessage() + "\"");
e.printStackTrace();
}
}
private boolean checkResult(boolean result)
{
// Write status
if (result == shouldMatch) {
success((shouldMatch ? "Matched" : "Did not match")
+ " \"" + toMatch + "\", as expected:");
return true;
} else {
if (shouldMatch) {
test.fail(log, "Did not match \"" + toMatch + "\", when expected to.");
} else {
test.fail(log, "Matched \"" + toMatch + "\", when not expected to.");
}
return false;
}
}
private boolean checkParens()
{
// Show subexpression registers
if (RETest.showSuccesses)
{
test.showParens(regexp);
}
log.append(" Paren count: " + regexp.getParenCount() + "\n");
if (!assertEquals(log, "Wrong number of parens", parens.length, regexp.getParenCount()))
{
return false;
}
// Check registers against expected contents
for (int p = 0; p < regexp.getParenCount(); p++)
{
log.append(" Paren " + p + ": " + regexp.getParen(p) + "\n");
// Compare expected result with actual
if ("null".equals(parens[p]) && regexp.getParen(p) == null)
{
// Consider "null" in test file equal to null
continue;
}
if (!assertEquals(log, "Wrong register " + p, parens[p], regexp.getParen(p)))
{
return false;
}
}
return true;
}
boolean tryMatchUsingCI(CharacterIterator matchAgainst)
{
try {
boolean result = regexp.match(matchAgainst, 0);
log.append(" Match: " + (result ? "YES" : "NO") + "\n");
return checkResult(result) && (!shouldMatch || checkParens());
}
// Matcher blew it
catch(Exception e)
{
test.fail(log, "Matcher threw exception: " + e.toString());
e.printStackTrace();
}
// Internal error
catch(Error e)
{
test.fail(log, "Matcher threw fatal error \"" + e.getMessage() + "\"");
e.printStackTrace();
}
return false;
}
public boolean assertEquals(StringBuffer log, String message, String expected, String actual)
{
if (expected != null && !expected.equals(actual)
|| actual != null && !actual.equals(expected))
{
test.fail(log, message + " (expected \"" + expected
+ "\", actual \"" + actual + "\")");
return false;
}
return true;
}
public boolean assertEquals(StringBuffer log, String message, int expected, int actual)
{
if (expected != actual) {
test.fail(log, message + " (expected \"" + expected
+ "\", actual \"" + actual + "\")");
return false;
}
return true;
}
/**
* Show a success
* @param s Success story
*/
void success(String s)
{
if (RETest.showSuccesses)
{
test.say("" + RETest.NEW_LINE + "-----------------------" + RETest.NEW_LINE + "");
test.say("Expression #" + (number) + " \"" + pattern + "\" ");
test.say("Success: " + s);
}
}
}

View File

@ -1,61 +0,0 @@
/*
* reserved comment block
* DO NOT REMOVE OR ALTER!
*/
/*
* Copyright 1999-2004 The Apache Software Foundation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.sun.org.apache.regexp.internal;
/**
* This is a class that contains utility helper methods for this package.
*
* @author <a href="mailto:jonl@muppetlabs.com">Jonathan Locke</a>
*/
public class REUtil
{
/** complex: */
private static final String complexPrefix = "complex:";
/**
* Creates a regular expression, permitting simple or complex syntax
* @param expression The expression, beginning with a prefix if it's complex or
* having no prefix if it's simple
* @param matchFlags Matching style flags
* @return The regular expression object
* @exception RESyntaxException thrown in case of error
*/
public static RE createRE(String expression, int matchFlags) throws RESyntaxException
{
if (expression.startsWith(complexPrefix))
{
return new RE(expression.substring(complexPrefix.length()), matchFlags);
}
return new RE(RE.simplePatternToFullRegularExpression(expression), matchFlags);
}
/**
* Creates a regular expression, permitting simple or complex syntax
* @param expression The expression, beginning with a prefix if it's complex or
* having no prefix if it's simple
* @return The regular expression object
* @exception RESyntaxException thrown in case of error
*/
public static RE createRE(String expression) throws RESyntaxException
{
return createRE(expression, RE.MATCH_NORMAL);
}
}

View File

@ -1,164 +0,0 @@
/*
* reserved comment block
* DO NOT REMOVE OR ALTER!
*/
/*
* Copyright 1999-2004 The Apache Software Foundation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.sun.org.apache.regexp.internal;
import java.io.Reader;
import java.io.IOException;
/**
* Encapsulates java.io.Reader as CharacterIterator
*
* @author <a href="mailto:ales.novak@netbeans.com">Ales Novak</a>
*/
public final class ReaderCharacterIterator implements CharacterIterator
{
/** Underlying reader */
private final Reader reader;
/** Buffer of read chars */
private final StringBuffer buff;
/** read end? */
private boolean closed;
/** @param reader a Reader, which is parsed */
public ReaderCharacterIterator(Reader reader)
{
this.reader = reader;
this.buff = new StringBuffer(512);
this.closed = false;
}
/** @return a substring */
public String substring(int beginIndex, int endIndex)
{
try
{
ensure(endIndex);
return buff.toString().substring(beginIndex, endIndex);
}
catch (IOException e)
{
throw new StringIndexOutOfBoundsException(e.getMessage());
}
}
/** @return a substring */
public String substring(int beginIndex)
{
try
{
readAll();
return buff.toString().substring(beginIndex);
}
catch (IOException e)
{
throw new StringIndexOutOfBoundsException(e.getMessage());
}
}
/** @return a character at the specified position. */
public char charAt(int pos)
{
try
{
ensure(pos);
return buff.charAt(pos);
}
catch (IOException e)
{
throw new StringIndexOutOfBoundsException(e.getMessage());
}
}
/** @return <tt>true</tt> iff if the specified index is after the end of the character stream */
public boolean isEnd(int pos)
{
if (buff.length() > pos)
{
return false;
}
else
{
try
{
ensure(pos);
return (buff.length() <= pos);
}
catch (IOException e)
{
throw new StringIndexOutOfBoundsException(e.getMessage());
}
}
}
/** Reads n characters from the stream and appends them to the buffer */
private int read(int n) throws IOException
{
if (closed)
{
return 0;
}
char[] c = new char[n];
int count = 0;
int read = 0;
do
{
read = reader.read(c);
if (read < 0) // EOF
{
closed = true;
break;
}
count += read;
buff.append(c, 0, read);
}
while (count < n);
return count;
}
/** Reads rest of the stream. */
private void readAll() throws IOException
{
while(! closed)
{
read(1000);
}
}
/** Reads chars up to the idx */
private void ensure(int idx) throws IOException
{
if (closed)
{
return;
}
if (idx < buff.length())
{
return;
}
read(idx + 1 - buff.length());
}
}

View File

@ -1,161 +0,0 @@
/*
* reserved comment block
* DO NOT REMOVE OR ALTER!
*/
/*
* Copyright 1999-2004 The Apache Software Foundation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.sun.org.apache.regexp.internal;
import java.io.InputStream;
import java.io.IOException;
/**
* Encapsulates java.io.InputStream as CharacterIterator.
*
* @author <a href="mailto:ales.novak@netbeans.com">Ales Novak</a>
*/
public final class StreamCharacterIterator implements CharacterIterator
{
/** Underlying is */
private final InputStream is;
/** Buffer of read chars */
private final StringBuffer buff;
/** read end? */
private boolean closed;
/** @param is an InputStream, which is parsed */
public StreamCharacterIterator(InputStream is)
{
this.is = is;
this.buff = new StringBuffer(512);
this.closed = false;
}
/** @return a substring */
public String substring(int beginIndex, int endIndex)
{
try
{
ensure(endIndex);
return buff.toString().substring(beginIndex, endIndex);
}
catch (IOException e)
{
throw new StringIndexOutOfBoundsException(e.getMessage());
}
}
/** @return a substring */
public String substring(int beginIndex)
{
try
{
readAll();
return buff.toString().substring(beginIndex);
}
catch (IOException e)
{
throw new StringIndexOutOfBoundsException(e.getMessage());
}
}
/** @return a character at the specified position. */
public char charAt(int pos)
{
try
{
ensure(pos);
return buff.charAt(pos);
}
catch (IOException e)
{
throw new StringIndexOutOfBoundsException(e.getMessage());
}
}
/** @return <tt>true</tt> iff if the specified index is after the end of the character stream */
public boolean isEnd(int pos)
{
if (buff.length() > pos)
{
return false;
}
else
{
try
{
ensure(pos);
return (buff.length() <= pos);
}
catch (IOException e)
{
throw new StringIndexOutOfBoundsException(e.getMessage());
}
}
}
/** Reads n characters from the stream and appends them to the buffer */
private int read(int n) throws IOException
{
if (closed)
{
return 0;
}
int c;
int i = n;
while (--i >= 0)
{
c = is.read();
if (c < 0) // EOF
{
closed = true;
break;
}
buff.append((char) c);
}
return n - i;
}
/** Reads rest of the stream. */
private void readAll() throws IOException
{
while(! closed)
{
read(1000);
}
}
/** Reads chars up to the idx */
private void ensure(int idx) throws IOException
{
if (closed)
{
return;
}
if (idx < buff.length())
{
return;
}
read(idx + 1 - buff.length());
}
}

View File

@ -1,62 +0,0 @@
/*
* reserved comment block
* DO NOT REMOVE OR ALTER!
*/
/*
* Copyright 1999-2004 The Apache Software Foundation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.sun.org.apache.regexp.internal;
/**
* Encapsulates String as CharacterIterator.
*
* @author <a href="mailto:ales.novak@netbeans.com">Ales Novak</a>
*/
public final class StringCharacterIterator implements CharacterIterator
{
/** encapsulated */
private final String src;
/** @param src - encapsulated String */
public StringCharacterIterator(String src)
{
this.src = src;
}
/** @return a substring */
public String substring(int beginIndex, int endIndex)
{
return src.substring(beginIndex, endIndex);
}
/** @return a substring */
public String substring(int beginIndex)
{
return src.substring(beginIndex);
}
/** @return a character at the specified position. */
public char charAt(int pos)
{
return src.charAt(pos);
}
/** @return <tt>true</tt> iff if the specified index is after the end of the character stream */
public boolean isEnd(int pos)
{
return (pos >= src.length());
}
}

View File

@ -1,137 +0,0 @@
/*
* reserved comment block
* DO NOT REMOVE OR ALTER!
*/
/*
* Copyright 1999-2004 The Apache Software Foundation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.sun.org.apache.regexp.internal;
import com.sun.org.apache.regexp.internal.RECompiler;
import com.sun.org.apache.regexp.internal.RESyntaxException;
/**
* 'recompile' is a command line tool that pre-compiles one or more regular expressions
* for use with the regular expression matcher class 'RE'. For example, the command
* "java recompile a*b" produces output like this:
*
* <pre>
*
* // Pre-compiled regular expression "a*b"
* char[] re1Instructions =
* {
* 0x007c, 0x0000, 0x001a, 0x007c, 0x0000, 0x000d, 0x0041,
* 0x0001, 0x0004, 0x0061, 0x007c, 0x0000, 0x0003, 0x0047,
* 0x0000, 0xfff6, 0x007c, 0x0000, 0x0003, 0x004e, 0x0000,
* 0x0003, 0x0041, 0x0001, 0x0004, 0x0062, 0x0045, 0x0000,
* 0x0000,
* };
*
* REProgram re1 = new REProgram(re1Instructions);
*
* </pre>
*
* By pasting this output into your code, you can construct a regular expression matcher
* (RE) object directly from the pre-compiled data (the character array re1), thus avoiding
* the overhead of compiling the expression at runtime. For example:
*
* <pre>
*
* RE r = new RE(re1);
*
* </pre>
*
* @see RE
* @see RECompiler
*
* @author <a href="mailto:jonl@muppetlabs.com">Jonathan Locke</a>
*/
public class recompile
{
/**
* Main application entrypoint.
* @param arg Command line arguments
*/
static public void main(String[] arg)
{
// Create a compiler object
RECompiler r = new RECompiler();
// Print usage if arguments are incorrect
if (arg.length <= 0 || arg.length % 2 != 0)
{
System.out.println("Usage: recompile <patternname> <pattern>");
System.exit(0);
}
// Loop through arguments, compiling each
for (int i = 0; i < arg.length; i += 2)
{
try
{
// Compile regular expression
String name = arg[i];
String pattern = arg[i+1];
String instructions = name + "PatternInstructions";
// Output program as a nice, formatted character array
System.out.print("\n // Pre-compiled regular expression '" + pattern + "'\n"
+ " private static char[] " + instructions + " = \n {");
// Compile program for pattern
REProgram program = r.compile(pattern);
// Number of columns in output
int numColumns = 7;
// Loop through program
char[] p = program.getInstructions();
for (int j = 0; j < p.length; j++)
{
// End of column?
if ((j % numColumns) == 0)
{
System.out.print("\n ");
}
// Print character as padded hex number
String hex = Integer.toHexString(p[j]);
while (hex.length() < 4)
{
hex = "0" + hex;
}
System.out.print("0x" + hex + ", ");
}
// End of program block
System.out.println("\n };");
System.out.println("\n private static RE " + name + "Pattern = new RE(new REProgram(" + instructions + "));");
}
catch (RESyntaxException e)
{
System.out.println("Syntax error in expression \"" + arg[i] + "\": " + e.toString());
}
catch (Exception e)
{
System.out.println("Unexpected exception: " + e.toString());
}
catch (Error e)
{
System.out.println("Internal error: " + e.toString());
}
}
}
}