This commit is contained in:
Lana Steuck 2016-05-12 18:46:32 +00:00
commit 37d12e2b3f
35 changed files with 565 additions and 5532 deletions

@ -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

@ -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();
}
/**

@ -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);
}
}

@ -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);
}

@ -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("");
}
}
}

@ -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;
}
}
}
}
}

@ -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);
}
}

@ -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);
}
}
}

@ -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);
}
}

@ -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());
}
}

@ -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());
}
}

@ -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());
}
}

@ -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());
}
}
}
}

@ -86,5 +86,6 @@ module java.xml {
uses javax.xml.transform.TransformerFactory;
uses javax.xml.validation.SchemaFactory;
uses javax.xml.xpath.XPathFactory;
uses org.xml.sax.XMLReader;
}

@ -93,6 +93,7 @@ package org.xml.sax;
* @see org.xml.sax.DocumentHandler#startElement startElement
* @see org.xml.sax.helpers.AttributeListImpl AttributeListImpl
*/
@Deprecated(since="5")
public interface AttributeList {

@ -68,6 +68,7 @@ package org.xml.sax;
* @see org.xml.sax.Locator
* @see org.xml.sax.HandlerBase
*/
@Deprecated(since="5")
public interface DocumentHandler {

@ -73,6 +73,7 @@ import java.util.Locale;
* @see org.xml.sax.HandlerBase
* @see org.xml.sax.InputSource
*/
@Deprecated(since="5")
public interface Parser
{

@ -1,5 +1,5 @@
/*
* Copyright (c) 2001, 2013, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2001, 2016, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -32,8 +32,7 @@
package org.xml.sax.helpers;
import java.lang.reflect.Method;
import java.lang.reflect.InvocationTargetException;
import java.util.Objects;
/**
* Create a new instance of a class by name.
@ -57,31 +56,26 @@ import java.lang.reflect.InvocationTargetException;
* @version 2.0.1 (sax2r2)
*/
class NewInstance {
private static final String DEFAULT_PACKAGE = "com.sun.org.apache.xerces.internal";
/**
* Creates a new instance of the specified class name
*
* Package private so this code is not exposed at the API level.
*/
static Object newInstance (ClassLoader classLoader, String className)
static <T> T newInstance (Class<T> type, ClassLoader loader, String clsName)
throws ClassNotFoundException, IllegalAccessException,
InstantiationException
{
// make sure we have access to restricted packages
boolean internal = false;
if (System.getSecurityManager() != null) {
if (className != null && className.startsWith(DEFAULT_PACKAGE)) {
internal = true;
}
ClassLoader classLoader = Objects.requireNonNull(loader);
String className = Objects.requireNonNull(clsName);
if (className.startsWith(DEFAULT_PACKAGE)) {
return type.cast(new com.sun.org.apache.xerces.internal.parsers.SAXParser());
}
Class driverClass;
if (classLoader == null || internal) {
driverClass = Class.forName(className);
} else {
driverClass = classLoader.loadClass(className);
}
return driverClass.newInstance();
Class<?> driverClass = classLoader.loadClass(className);
return type.cast(driverClass.newInstance());
}
}

@ -30,8 +30,6 @@
package org.xml.sax.helpers;
import org.xml.sax.Parser;
/**
* Java-specific class for dynamically loading SAX parsers.
@ -65,6 +63,8 @@ import org.xml.sax.Parser;
* @author David Megginson
* @version 2.0.1 (sax2r2)
*/
@SuppressWarnings( "deprecation" )
@Deprecated(since="5")
public class ParserFactory {
private static SecuritySupport ss = new SecuritySupport();
@ -97,7 +97,7 @@ public class ParserFactory {
* @see #makeParser(java.lang.String)
* @see org.xml.sax.Parser
*/
public static Parser makeParser ()
public static org.xml.sax.Parser makeParser ()
throws ClassNotFoundException,
IllegalAccessException,
InstantiationException,
@ -134,14 +134,13 @@ public class ParserFactory {
* @see #makeParser()
* @see org.xml.sax.Parser
*/
public static Parser makeParser (String className)
public static org.xml.sax.Parser makeParser (String className)
throws ClassNotFoundException,
IllegalAccessException,
InstantiationException,
ClassCastException
{
return (Parser) NewInstance.newInstance (
ss.getContextClassLoader(), className);
return NewInstance.newInstance (org.xml.sax.Parser.class, ss.getClassLoader(), className);
}
}

@ -1,5 +1,5 @@
/*
* Copyright (c) 2004, 2013, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2004, 2016, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -37,72 +37,56 @@ import java.security.*;
*/
class SecuritySupport {
ClassLoader getContextClassLoader() throws SecurityException{
return (ClassLoader)
AccessController.doPrivileged(new PrivilegedAction() {
public Object run() {
ClassLoader cl = null;
//try {
cl = Thread.currentThread().getContextClassLoader();
//} catch (SecurityException ex) { }
if (cl == null)
cl = ClassLoader.getSystemClassLoader();
return cl;
/**
* Returns the current thread's context class loader, or the system class loader
* if the context class loader is null.
* @return the current thread's context class loader, or the system class loader
* @throws SecurityException
*/
ClassLoader getClassLoader() throws SecurityException{
return AccessController.doPrivileged((PrivilegedAction<ClassLoader>)() -> {
ClassLoader cl = Thread.currentThread().getContextClassLoader();
if (cl == null) {
cl = ClassLoader.getSystemClassLoader();
}
return cl;
});
}
String getSystemProperty(final String propName) {
return (String)
AccessController.doPrivileged(new PrivilegedAction() {
public Object run() {
return System.getProperty(propName);
}
});
return AccessController.doPrivileged((PrivilegedAction<String>)()
-> System.getProperty(propName));
}
FileInputStream getFileInputStream(final File file)
throws FileNotFoundException
{
try {
return (FileInputStream)
AccessController.doPrivileged(new PrivilegedExceptionAction() {
public Object run() throws FileNotFoundException {
return new FileInputStream(file);
}
});
return AccessController.doPrivileged((PrivilegedExceptionAction<FileInputStream>)() ->
new FileInputStream(file));
} catch (PrivilegedActionException e) {
throw (FileNotFoundException)e.getException();
}
}
InputStream getResourceAsStream(final ClassLoader cl,
final String name)
InputStream getResourceAsStream(final ClassLoader cl, final String name)
{
return (InputStream)
AccessController.doPrivileged(new PrivilegedAction() {
public Object run() {
InputStream ris;
if (cl == null) {
ris = SecuritySupport.class.getResourceAsStream(name);
} else {
ris = cl.getResourceAsStream(name);
}
return ris;
}
});
return AccessController.doPrivileged((PrivilegedAction<InputStream>) () -> {
InputStream ris;
if (cl == null) {
ris = SecuritySupport.class.getResourceAsStream(name);
} else {
ris = cl.getResourceAsStream(name);
}
return ris;
});
}
boolean doesFileExist(final File f) {
return ((Boolean)
AccessController.doPrivileged(new PrivilegedAction() {
public Object run() {
return new Boolean(f.exists());
}
})).booleanValue();
return (AccessController.doPrivileged((PrivilegedAction<Boolean>)() ->
new Boolean(f.exists())));
}
}

@ -1,5 +1,5 @@
/*
* Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -32,10 +32,17 @@
package org.xml.sax.helpers;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import org.xml.sax.XMLReader;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.util.Iterator;
import java.util.Objects;
import java.util.ServiceConfigurationError;
import java.util.ServiceLoader;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
/**
@ -70,7 +77,11 @@ import org.xml.sax.SAXException;
* @since 1.4, SAX 2.0
* @author David Megginson, David Brownell
* @version 2.0.1 (sax2r2)
*
* @deprecated It is recommended to use {@link javax.xml.parsers.SAXParserFactory}
* instead.
*/
@Deprecated(since="9")
final public class XMLReaderFactory
{
/**
@ -83,47 +94,43 @@ final public class XMLReaderFactory
}
private static final String property = "org.xml.sax.driver";
private static SecuritySupport ss = new SecuritySupport();
private static final SecuritySupport ss = new SecuritySupport();
private static String _clsFromJar = null;
private static boolean _jarread = false;
/**
* Attempt to create an XMLReader from system defaults.
* In environments which can support it, the name of the XMLReader
* class is determined by trying each these options in order, and
* using the first one which succeeds:
* <ul>
*
* Obtains a new instance of a {@link org.xml.sax.XMLReader}.
* This method uses the following ordered lookup procedure to find and load
* the {@link org.xml.sax.XMLReader} implementation class:
* <p>
* <ol>
* <li>If the system property {@code org.xml.sax.driver}
* has a value, that is used as an XMLReader class name. </li>
* <li>
* Use the service-provider loading facility, defined by the
* {@link java.util.ServiceLoader} class, to attempt to locate and load an
* implementation of the service {@link org.xml.sax.XMLReader} by using the
* {@linkplain java.lang.Thread#getContextClassLoader() current thread's context class loader}.
* If the context class loader is null, the
* {@linkplain ClassLoader#getSystemClassLoader() system class loader} will
* be used.
* </li>
* <li>
* Deprecated. Look for a class name in the {@code META-INF/services/org.xml.sax.driver}
* file in a jar file available to the runtime.</li>
* <li>
* <p>
* Otherwise, the system-default implementation is returned.
* </li>
* </ol>
*
* <li>The JAR "Services API" is used to look for a class name
* in the <em>META-INF/services/org.xml.sax.driver</em> file in
* jarfiles available to the runtime.</li>
* @apiNote
* The process that looks for a class name in the
* {@code META-INF/services/org.xml.sax.driver} file in a jar file does not
* conform to the specification of the service-provider loading facility
* as defined in {@link java.util.ServiceLoader} and therefore does not
* support modularization. It is deprecated as of Java SE 9 and subject to
* removal in a future release.
*
* <li> SAX parser distributions are strongly encouraged to provide
* a default XMLReader class name that will take effect only when
* previous options (on this list) are not successful.</li>
*
* <li>Finally, if {@link ParserFactory#makeParser()} can
* return a system default SAX1 parser, that parser is wrapped in
* a {@link ParserAdapter}. (This is a migration aid for SAX1
* environments, where the {@code org.xml.sax.parser} system
* property will often be usable.) </li>
* </ul>
*
* <p> In environments such as small embedded systems, which can not
* support that flexibility, other mechanisms to determine the default
* may be used.
*
* <p>Note that many Java environments allow system properties to be
* initialized on a command line. This means that <em>in most cases</em>
* setting a good value for that property ensures that calls to this
* method will succeed, except when security policies intervene.
* This will also maximize application portability to older SAX
* environments, with less robust implementations of this method.
*
* @return A new XMLReader.
* @return a new XMLReader.
* @exception org.xml.sax.SAXException If no default XMLReader class
* can be identified and instantiated.
* @see #createXMLReader(java.lang.String)
@ -132,7 +139,7 @@ final public class XMLReaderFactory
throws SAXException
{
String className = null;
ClassLoader cl = ss.getContextClassLoader();
ClassLoader cl = ss.getClassLoader();
// 1. try the JVM-instance-wide system property
try {
@ -140,62 +147,26 @@ final public class XMLReaderFactory
}
catch (RuntimeException e) { /* continue searching */ }
// 2. if that fails, try META-INF/services/
// 2. try the ServiceLoader
if (className == null) {
if (!_jarread) {
_jarread = true;
String service = "META-INF/services/" + property;
InputStream in;
BufferedReader reader;
try {
if (cl != null) {
in = ss.getResourceAsStream(cl, service);
// If no provider found then try the current ClassLoader
if (in == null) {
cl = null;
in = ss.getResourceAsStream(cl, service);
}
} else {
// No Context ClassLoader, try the current ClassLoader
in = ss.getResourceAsStream(cl, service);
}
if (in != null) {
reader = new BufferedReader (new InputStreamReader (in, "UTF8"));
_clsFromJar = reader.readLine ();
in.close ();
}
} catch (Exception e) {
}
final XMLReader provider = findServiceProvider(XMLReader.class, cl);
if (provider != null) {
return provider;
}
className = _clsFromJar;
}
// 3. Distro-specific fallback
// 3. try META-INF/services/org.xml.sax.driver. This old process allows
// legacy providers to be found
if (className == null) {
// BEGIN DISTRIBUTION-SPECIFIC
// EXAMPLE:
// className = "com.example.sax.XmlReader";
// or a $JAVA_HOME/jre/lib/*properties setting...
className = "com.sun.org.apache.xerces.internal.parsers.SAXParser";
// END DISTRIBUTION-SPECIFIC
className = jarLookup(cl);
}
// do we know the XMLReader implementation class yet?
if (className != null)
return loadClass (cl, className);
// 4. panic -- adapt any SAX1 parser
try {
return new ParserAdapter (ParserFactory.makeParser ());
} catch (Exception e) {
throw new SAXException ("Can't create default XMLReader; "
+ "is system property org.xml.sax.driver set?");
// 4. Distro-specific fallback
if (className == null) {
return new com.sun.org.apache.xerces.internal.parsers.SAXParser();
}
return loadClass (cl, className);
}
@ -217,14 +188,14 @@ final public class XMLReaderFactory
public static XMLReader createXMLReader (String className)
throws SAXException
{
return loadClass (ss.getContextClassLoader(), className);
return loadClass (ss.getClassLoader(), className);
}
private static XMLReader loadClass (ClassLoader loader, String className)
throws SAXException
{
try {
return (XMLReader) NewInstance.newInstance (loader, className);
return NewInstance.newInstance (XMLReader.class, loader, className);
} catch (ClassNotFoundException e1) {
throw new SAXException("SAX2 driver class " + className +
" not found", e1);
@ -240,4 +211,64 @@ final public class XMLReaderFactory
" does not implement XMLReader", e4);
}
}
/**
* Locates a provider by directly reading the jar service file.
* @param loader the ClassLoader to be used to read the service file
* @return the name of the provider, or null if nothing is found
*/
private static String jarLookup(final ClassLoader loader) {
final ClassLoader cl = Objects.requireNonNull(loader);
String clsFromJar = null;
String service = "META-INF/services/" + property;
InputStream in;
BufferedReader reader;
try {
in = ss.getResourceAsStream(cl, service);
// If no provider found then try the current ClassLoader
if (in == null) {
in = ss.getResourceAsStream(null, service);
}
if (in != null) {
reader = new BufferedReader (new InputStreamReader (in, "UTF8"));
clsFromJar = reader.readLine ();
in.close ();
}
} catch (IOException e) {
}
return clsFromJar;
}
/*
* Try to find provider using the ServiceLoader API
*
* @param type Base class / Service interface of the factory to find.
*
* @return instance of provider class if found or null
*/
private static <T> T findServiceProvider(final Class<T> type, final ClassLoader loader)
throws SAXException {
ClassLoader cl = Objects.requireNonNull(loader);
try {
return AccessController.doPrivileged((PrivilegedAction<T>) () -> {
final ServiceLoader<T> serviceLoader;
serviceLoader = ServiceLoader.load(type, cl);
final Iterator<T> iterator = serviceLoader.iterator();
if (iterator.hasNext()) {
return iterator.next();
} else {
return null;
}
});
} catch(ServiceConfigurationError e) {
final RuntimeException x = new RuntimeException(
"Provider for " + type + " cannot be created", e);
throw new SAXException("Provider for " + type + " cannot be created", x);
}
}
}

@ -76,14 +76,20 @@ endif
TEST_ROOT := $(shell $(PWD))
# Root of all test results
ifdef ALT_OUTPUTDIR
ABS_OUTPUTDIR = $(shell $(CD) $(ALT_OUTPUTDIR) && $(PWD))
ifdef TEST_OUTPUT_DIR
$(shell $(MKDIR) -p $(TEST_OUTPUT_DIR)/jtreg)
ABS_TEST_OUTPUT_DIR := \
$(shell $(CD) $(TEST_OUTPUT_DIR)/jtreg && $(PWD))
else
ABS_OUTPUTDIR = $(shell $(CD) $(TEST_ROOT)/.. && $(PWD))
endif
ifdef ALT_OUTPUTDIR
ABS_OUTPUTDIR = $(shell $(CD) $(ALT_OUTPUTDIR) && $(PWD))
else
ABS_OUTPUTDIR = $(shell $(CD) $(TEST_ROOT)/.. && $(PWD))
endif
ABS_PLATFORM_BUILD_ROOT = $(ABS_OUTPUTDIR)
ABS_TEST_OUTPUT_DIR := $(ABS_PLATFORM_BUILD_ROOT)/testoutput/$(UNIQUE_DIR)
ABS_PLATFORM_BUILD_ROOT = $(ABS_OUTPUTDIR)
ABS_TEST_OUTPUT_DIR := $(ABS_PLATFORM_BUILD_ROOT)/testoutput/$(UNIQUE_DIR)
endif
# Expect JPRT to set PRODUCT_HOME (the product or jdk in this case to test)
ifndef PRODUCT_HOME

@ -29,3 +29,5 @@ javax/xml/jaxp/isolatedjdk/catalog/PropertiesTest.sh generic-all
# 8150145
javax/xml/jaxp/unittest/common/TransformationWarningsTest.java generic-all
# 8156508
javax/xml/jaxp/unittest/stream/FactoryFindTest.java generic-all

@ -38,7 +38,7 @@ import org.testng.annotations.Test;
* @library /javax/xml/jaxp/libs
* @build jdk.testlibrary.*
* @run testng BasicModularXMLParserTest
* @bug 8078820
* @bug 8078820 8156119
* @summary Tests JAXP lib can instantiate the following interfaces
* with customized provider module on boot layer
*
@ -51,6 +51,7 @@ import org.testng.annotations.Test;
* javax.xml.transform.TransformerFactory
* javax.xml.validation.SchemaFactory
* javax.xml.xpath.XPathFactory
* org.xml.sax.XMLReader
*/
@Test

@ -50,7 +50,7 @@ import jdk.testlibrary.CompilerUtils;
* @library /javax/xml/jaxp/libs
* @build jdk.testlibrary.*
* @run testng LayerModularXMLParserTest
* @bug 8078820
* @bug 8078820 8156119
* @summary Tests JAXP lib works with layer and TCCL
*/
@ -75,7 +75,7 @@ public class LayerModularXMLParserTest {
* services provided by provider2
*/
private static final String[] services2 = { "javax.xml.datatype.DatatypeFactory",
"javax.xml.stream.XMLEventFactory" };
"javax.xml.stream.XMLEventFactory", "org.xml.sax.XMLReader" };
/*
* Compiles all modules used by the test

@ -0,0 +1,122 @@
/*
* Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertTrue;
import java.io.File;
import java.net.URL;
import java.net.URLClassLoader;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import jdk.testlibrary.CompilerUtils;
import org.testng.Assert;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.XMLReaderFactory;
/*
* @test
* @library /javax/xml/jaxp/libs
* @build jdk.testlibrary.*
* @run testng XMLReaderFactoryTest
* @bug 8152912 8015099 8156119
* @summary Tests XMLReaderFactory can work as ServiceLoader compliant, as well as backward compatible
*/
@Test
public class XMLReaderFactoryTest {
private static final String TEST_SRC = System.getProperty("test.src");
private static final Path SRC_DIR = Paths.get(TEST_SRC, "src").resolve("xmlprovider3");
private static final Path CLASSES_DIR = Paths.get("classes");
private static final Path LEGACY_DIR = CLASSES_DIR.resolve("legacy");
private static final Path SERVICE_DIR = CLASSES_DIR.resolve("service");
// resources to copy to the class path
private static final String LEGACY_SERVICE_FILE = "legacy/META-INF/services/org.xml.sax.driver";
private static final String SERVICE_FILE = "service/META-INF/services/org.xml.sax.XMLReader";
/*
* Compile class and copy service files
*/
@BeforeTest
public void setup() throws Exception {
setup(LEGACY_DIR, LEGACY_SERVICE_FILE);
setup(SERVICE_DIR, SERVICE_FILE);
}
private void setup(Path dest, String serviceFile) throws Exception {
Files.createDirectories(dest);
assertTrue(CompilerUtils.compile(SRC_DIR, dest));
Path file = Paths.get(serviceFile.replace('/', File.separatorChar));
Path source = SRC_DIR.resolve(file);
Path target = CLASSES_DIR.resolve(file);
Files.createDirectories(target.getParent());
Files.copy(source, target);
}
public void testService() throws Exception {
ClassLoader clBackup = Thread.currentThread().getContextClassLoader();
try {
URL[] classUrls = { SERVICE_DIR.toUri().toURL() };
URLClassLoader loader = new URLClassLoader(classUrls, ClassLoader.getSystemClassLoader().getParent());
// set TCCL and try locating the provider
Thread.currentThread().setContextClassLoader(loader);
XMLReader reader = XMLReaderFactory.createXMLReader();
assertEquals(reader.getClass().getName(), "xp3.XMLReaderImpl");
} finally {
Thread.currentThread().setContextClassLoader(clBackup);
}
}
public void testLegacy() throws Exception {
ClassLoader clBackup = Thread.currentThread().getContextClassLoader();
try {
URL[] classUrls = { LEGACY_DIR.toUri().toURL() };
URLClassLoader loader = new URLClassLoader(classUrls, ClassLoader.getSystemClassLoader().getParent());
// set TCCL and try locating the provider
Thread.currentThread().setContextClassLoader(loader);
XMLReader reader1 = XMLReaderFactory.createXMLReader();
assertEquals(reader1.getClass().getName(), "xp3.XMLReaderImpl");
// now point to a random URL
Thread.currentThread().setContextClassLoader(
new URLClassLoader(new URL[0], ClassLoader.getSystemClassLoader().getParent()));
// ClassNotFoundException if also trying to load class of reader1, which
// would be the case before 8152912
XMLReader reader2 = XMLReaderFactory.createXMLReader();
assertEquals(reader2.getClass().getName(), "com.sun.org.apache.xerces.internal.parsers.SAXParser");
} finally {
Thread.currentThread().setContextClassLoader(clBackup);
}
}
}

@ -31,6 +31,8 @@ import java.lang.System;
import java.util.Iterator;
import java.util.ServiceLoader;
import org.xml.sax.helpers.XMLReaderFactory;
public class XMLFactoryHelper {
/*
* instantiate a xml factory by reflection e.g.
@ -41,7 +43,9 @@ public class XMLFactoryHelper {
try {
// set thread context class loader to module class loader
Thread.currentThread().setContextClassLoader(XMLFactoryHelper.class.getClassLoader());
if (serviceName.equals("javax.xml.validation.SchemaFactory"))
if (serviceName.equals("org.xml.sax.XMLReader"))
return XMLReaderFactory.createXMLReader();
else if (serviceName.equals("javax.xml.validation.SchemaFactory"))
return Class.forName(serviceName).getMethod("newInstance", String.class)
.invoke(null, W3C_XML_SCHEMA_NS_URI);
else

@ -30,6 +30,8 @@ import java.util.HashSet;
import java.util.Set;
import java.util.stream.Stream;
import org.xml.sax.helpers.XMLReaderFactory;
public class Main {
/*
* @param args, the names of provider modules, which have been loaded
@ -69,7 +71,9 @@ public class Main {
*/
private static Object instantiateXMLService(String serviceName) {
try {
if (serviceName.equals("javax.xml.validation.SchemaFactory"))
if (serviceName.equals("org.xml.sax.XMLReader"))
return XMLReaderFactory.createXMLReader();
else if (serviceName.equals("javax.xml.validation.SchemaFactory"))
return Class.forName(serviceName).getMethod("newInstance", String.class)
.invoke(null, W3C_XML_SCHEMA_NS_URI);
else
@ -102,6 +106,7 @@ public class Main {
"javax.xml.parsers.DocumentBuilderFactory", "javax.xml.parsers.SAXParserFactory",
"javax.xml.stream.XMLEventFactory", "javax.xml.stream.XMLInputFactory",
"javax.xml.stream.XMLOutputFactory", "javax.xml.transform.TransformerFactory",
"javax.xml.validation.SchemaFactory", "javax.xml.xpath.XPathFactory" };
"javax.xml.validation.SchemaFactory", "javax.xml.xpath.XPathFactory",
"org.xml.sax.XMLReader"};
}

@ -26,4 +26,5 @@ module xmlprovider2 {
provides javax.xml.datatype.DatatypeFactory with xp2.DatatypeFactoryImpl;
provides javax.xml.stream.XMLEventFactory with xp2.XMLEventFactoryImpl;
provides org.xml.sax.XMLReader with xp2.XMLReaderImpl;
}

@ -0,0 +1,106 @@
/*
* Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package xp2;
import java.io.IOException;
import org.xml.sax.ContentHandler;
import org.xml.sax.DTDHandler;
import org.xml.sax.EntityResolver;
import org.xml.sax.ErrorHandler;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.SAXNotRecognizedException;
import org.xml.sax.SAXNotSupportedException;
import org.xml.sax.XMLReader;
public class XMLReaderImpl implements XMLReader {
@Override
public boolean getFeature(String name) throws SAXNotRecognizedException, SAXNotSupportedException {
return false;
}
@Override
public void setFeature(String name, boolean value) throws SAXNotRecognizedException,
SAXNotSupportedException {
}
@Override
public Object getProperty(String name) throws SAXNotRecognizedException, SAXNotSupportedException {
return null;
}
@Override
public void setProperty(String name, Object value) throws SAXNotRecognizedException,
SAXNotSupportedException {
}
@Override
public void setEntityResolver(EntityResolver resolver) {
// TODO Auto-generated method stub
}
@Override
public EntityResolver getEntityResolver() {
return null;
}
@Override
public void setDTDHandler(DTDHandler handler) {
}
@Override
public DTDHandler getDTDHandler() {
return null;
}
@Override
public void setContentHandler(ContentHandler handler) {
}
@Override
public ContentHandler getContentHandler() {
return null;
}
@Override
public void setErrorHandler(ErrorHandler handler) {
}
@Override
public ErrorHandler getErrorHandler() {
return null;
}
@Override
public void parse(InputSource input) throws IOException, SAXException {
}
@Override
public void parse(String systemId) throws IOException, SAXException {
}
}

@ -0,0 +1,106 @@
/*
* Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package xp3;
import java.io.IOException;
import org.xml.sax.ContentHandler;
import org.xml.sax.DTDHandler;
import org.xml.sax.EntityResolver;
import org.xml.sax.ErrorHandler;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.SAXNotRecognizedException;
import org.xml.sax.SAXNotSupportedException;
import org.xml.sax.XMLReader;
public class XMLReaderImpl implements XMLReader {
@Override
public boolean getFeature(String name) throws SAXNotRecognizedException, SAXNotSupportedException {
return false;
}
@Override
public void setFeature(String name, boolean value) throws SAXNotRecognizedException,
SAXNotSupportedException {
}
@Override
public Object getProperty(String name) throws SAXNotRecognizedException, SAXNotSupportedException {
return null;
}
@Override
public void setProperty(String name, Object value) throws SAXNotRecognizedException,
SAXNotSupportedException {
}
@Override
public void setEntityResolver(EntityResolver resolver) {
// TODO Auto-generated method stub
}
@Override
public EntityResolver getEntityResolver() {
return null;
}
@Override
public void setDTDHandler(DTDHandler handler) {
}
@Override
public DTDHandler getDTDHandler() {
return null;
}
@Override
public void setContentHandler(ContentHandler handler) {
}
@Override
public ContentHandler getContentHandler() {
return null;
}
@Override
public void setErrorHandler(ErrorHandler handler) {
}
@Override
public ErrorHandler getErrorHandler() {
return null;
}
@Override
public void parse(InputSource input) throws IOException, SAXException {
}
@Override
public void parse(String systemId) throws IOException, SAXException {
}
}