feat(jdk8): move files to new folder to avoid resources compiled.

This commit is contained in:
2025-09-07 15:25:52 +08:00
parent 3f0047bf6f
commit 8c35cfb1c0
17415 changed files with 217 additions and 213 deletions

View File

@@ -0,0 +1,336 @@
/*
* Copyright (c) 2000, 2005, 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. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* 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.
*/
// SAX default implementation for AttributeList.
// http://www.saxproject.org
// No warranty; no copyright -- use this as you will.
// $Id: AttributeListImpl.java,v 1.2 2004/11/03 22:53:08 jsuttor Exp $
package org.xml.sax.helpers;
import org.xml.sax.AttributeList;
import java.util.Vector;
/**
* Default implementation for AttributeList.
*
* <blockquote>
* <em>This module, both source code and documentation, is in the
* Public Domain, and comes with <strong>NO WARRANTY</strong>.</em>
* See <a href='http://www.saxproject.org'>http://www.saxproject.org</a>
* for further information.
* </blockquote>
*
* <p>AttributeList implements the deprecated SAX1 {@link
* org.xml.sax.AttributeList AttributeList} interface, and has been
* replaced by the new SAX2 {@link org.xml.sax.helpers.AttributesImpl
* AttributesImpl} interface.</p>
*
* <p>This class provides a convenience implementation of the SAX
* {@link org.xml.sax.AttributeList AttributeList} interface. This
* implementation is useful both for SAX parser writers, who can use
* it to provide attributes to the application, and for SAX application
* writers, who can use it to create a persistent copy of an element's
* attribute specifications:</p>
*
* <pre>
* private AttributeList myatts;
*
* public void startElement (String name, AttributeList atts)
* {
* // create a persistent copy of the attribute list
* // for use outside this method
* myatts = new AttributeListImpl(atts);
* [...]
* }
* </pre>
*
* <p>Please note that SAX parsers are not required to use this
* class to provide an implementation of AttributeList; it is
* supplied only as an optional convenience. In particular,
* parser writers are encouraged to invent more efficient
* implementations.</p>
*
* @deprecated This class implements a deprecated interface,
* {@link org.xml.sax.AttributeList AttributeList};
* that interface has been replaced by
* {@link org.xml.sax.Attributes Attributes},
* which is implemented in the
* {@link org.xml.sax.helpers.AttributesImpl
* AttributesImpl} helper class.
* @since SAX 1.0
* @author David Megginson
* @see org.xml.sax.AttributeList
* @see org.xml.sax.DocumentHandler#startElement
*/
public class AttributeListImpl implements AttributeList
{
/**
* Create an empty attribute list.
*
* <p>This constructor is most useful for parser writers, who
* will use it to create a single, reusable attribute list that
* can be reset with the clear method between elements.</p>
*
* @see #addAttribute
* @see #clear
*/
public AttributeListImpl ()
{
}
/**
* Construct a persistent copy of an existing attribute list.
*
* <p>This constructor is most useful for application writers,
* who will use it to create a persistent copy of an existing
* attribute list.</p>
*
* @param atts The attribute list to copy
* @see org.xml.sax.DocumentHandler#startElement
*/
public AttributeListImpl (AttributeList atts)
{
setAttributeList(atts);
}
////////////////////////////////////////////////////////////////////
// Methods specific to this class.
////////////////////////////////////////////////////////////////////
/**
* Set the attribute list, discarding previous contents.
*
* <p>This method allows an application writer to reuse an
* attribute list easily.</p>
*
* @param atts The attribute list to copy.
*/
public void setAttributeList (AttributeList atts)
{
int count = atts.getLength();
clear();
for (int i = 0; i < count; i++) {
addAttribute(atts.getName(i), atts.getType(i), atts.getValue(i));
}
}
/**
* Add an attribute to an attribute list.
*
* <p>This method is provided for SAX parser writers, to allow them
* to build up an attribute list incrementally before delivering
* it to the application.</p>
*
* @param name The attribute name.
* @param type The attribute type ("NMTOKEN" for an enumeration).
* @param value The attribute value (must not be null).
* @see #removeAttribute
* @see org.xml.sax.DocumentHandler#startElement
*/
public void addAttribute (String name, String type, String value)
{
names.addElement(name);
types.addElement(type);
values.addElement(value);
}
/**
* Remove an attribute from the list.
*
* <p>SAX application writers can use this method to filter an
* attribute out of an AttributeList. Note that invoking this
* method will change the length of the attribute list and
* some of the attribute's indices.</p>
*
* <p>If the requested attribute is not in the list, this is
* a no-op.</p>
*
* @param name The attribute name.
* @see #addAttribute
*/
public void removeAttribute (String name)
{
int i = names.indexOf(name);
if (i >= 0) {
names.removeElementAt(i);
types.removeElementAt(i);
values.removeElementAt(i);
}
}
/**
* Clear the attribute list.
*
* <p>SAX parser writers can use this method to reset the attribute
* list between DocumentHandler.startElement events. Normally,
* it will make sense to reuse the same AttributeListImpl object
* rather than allocating a new one each time.</p>
*
* @see org.xml.sax.DocumentHandler#startElement
*/
public void clear ()
{
names.removeAllElements();
types.removeAllElements();
values.removeAllElements();
}
////////////////////////////////////////////////////////////////////
// Implementation of org.xml.sax.AttributeList
////////////////////////////////////////////////////////////////////
/**
* Return the number of attributes in the list.
*
* @return The number of attributes in the list.
* @see org.xml.sax.AttributeList#getLength
*/
public int getLength ()
{
return names.size();
}
/**
* Get the name of an attribute (by position).
*
* @param i The position of the attribute in the list.
* @return The attribute name as a string, or null if there
* is no attribute at that position.
* @see org.xml.sax.AttributeList#getName(int)
*/
public String getName (int i)
{
if (i < 0) {
return null;
}
try {
return (String)names.elementAt(i);
} catch (ArrayIndexOutOfBoundsException e) {
return null;
}
}
/**
* Get the type of an attribute (by position).
*
* @param i The position of the attribute in the list.
* @return The attribute type as a string ("NMTOKEN" for an
* enumeration, and "CDATA" if no declaration was
* read), or null if there is no attribute at
* that position.
* @see org.xml.sax.AttributeList#getType(int)
*/
public String getType (int i)
{
if (i < 0) {
return null;
}
try {
return (String)types.elementAt(i);
} catch (ArrayIndexOutOfBoundsException e) {
return null;
}
}
/**
* Get the value of an attribute (by position).
*
* @param i The position of the attribute in the list.
* @return The attribute value as a string, or null if
* there is no attribute at that position.
* @see org.xml.sax.AttributeList#getValue(int)
*/
public String getValue (int i)
{
if (i < 0) {
return null;
}
try {
return (String)values.elementAt(i);
} catch (ArrayIndexOutOfBoundsException e) {
return null;
}
}
/**
* Get the type of an attribute (by name).
*
* @param name The attribute name.
* @return The attribute type as a string ("NMTOKEN" for an
* enumeration, and "CDATA" if no declaration was
* read).
* @see org.xml.sax.AttributeList#getType(java.lang.String)
*/
public String getType (String name)
{
return getType(names.indexOf(name));
}
/**
* Get the value of an attribute (by name).
*
* @param name The attribute name.
* @see org.xml.sax.AttributeList#getValue(java.lang.String)
*/
public String getValue (String name)
{
return getValue(names.indexOf(name));
}
////////////////////////////////////////////////////////////////////
// Internal state.
////////////////////////////////////////////////////////////////////
Vector names = new Vector();
Vector types = new Vector();
Vector values = new Vector();
}
// end of AttributeListImpl.java

View File

@@ -0,0 +1,641 @@
/*
* Copyright (c) 2000, 2005, 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. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* 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.
*/
// AttributesImpl.java - default implementation of Attributes.
// http://www.saxproject.org
// Written by David Megginson
// NO WARRANTY! This class is in the public domain.
// $Id: AttributesImpl.java,v 1.2 2004/11/03 22:53:08 jsuttor Exp $
package org.xml.sax.helpers;
import org.xml.sax.Attributes;
/**
* Default implementation of the Attributes interface.
*
* <blockquote>
* <em>This module, both source code and documentation, is in the
* Public Domain, and comes with <strong>NO WARRANTY</strong>.</em>
* See <a href='http://www.saxproject.org'>http://www.saxproject.org</a>
* for further information.
* </blockquote>
*
* <p>This class provides a default implementation of the SAX2
* {@link org.xml.sax.Attributes Attributes} interface, with the
* addition of manipulators so that the list can be modified or
* reused.</p>
*
* <p>There are two typical uses of this class:</p>
*
* <ol>
* <li>to take a persistent snapshot of an Attributes object
* in a {@link org.xml.sax.ContentHandler#startElement startElement} event; or</li>
* <li>to construct or modify an Attributes object in a SAX2 driver or filter.</li>
* </ol>
*
* <p>This class replaces the now-deprecated SAX1 {@link
* org.xml.sax.helpers.AttributeListImpl AttributeListImpl}
* class; in addition to supporting the updated Attributes
* interface rather than the deprecated {@link org.xml.sax.AttributeList
* AttributeList} interface, it also includes a much more efficient
* implementation using a single array rather than a set of Vectors.</p>
*
* @since SAX 2.0
* @author David Megginson
*/
public class AttributesImpl implements Attributes
{
////////////////////////////////////////////////////////////////////
// Constructors.
////////////////////////////////////////////////////////////////////
/**
* Construct a new, empty AttributesImpl object.
*/
public AttributesImpl ()
{
length = 0;
data = null;
}
/**
* Copy an existing Attributes object.
*
* <p>This constructor is especially useful inside a
* {@link org.xml.sax.ContentHandler#startElement startElement} event.</p>
*
* @param atts The existing Attributes object.
*/
public AttributesImpl (Attributes atts)
{
setAttributes(atts);
}
////////////////////////////////////////////////////////////////////
// Implementation of org.xml.sax.Attributes.
////////////////////////////////////////////////////////////////////
/**
* Return the number of attributes in the list.
*
* @return The number of attributes in the list.
* @see org.xml.sax.Attributes#getLength
*/
public int getLength ()
{
return length;
}
/**
* Return an attribute's Namespace URI.
*
* @param index The attribute's index (zero-based).
* @return The Namespace URI, the empty string if none is
* available, or null if the index is out of range.
* @see org.xml.sax.Attributes#getURI
*/
public String getURI (int index)
{
if (index >= 0 && index < length) {
return data[index*5];
} else {
return null;
}
}
/**
* Return an attribute's local name.
*
* @param index The attribute's index (zero-based).
* @return The attribute's local name, the empty string if
* none is available, or null if the index if out of range.
* @see org.xml.sax.Attributes#getLocalName
*/
public String getLocalName (int index)
{
if (index >= 0 && index < length) {
return data[index*5+1];
} else {
return null;
}
}
/**
* Return an attribute's qualified (prefixed) name.
*
* @param index The attribute's index (zero-based).
* @return The attribute's qualified name, the empty string if
* none is available, or null if the index is out of bounds.
* @see org.xml.sax.Attributes#getQName
*/
public String getQName (int index)
{
if (index >= 0 && index < length) {
return data[index*5+2];
} else {
return null;
}
}
/**
* Return an attribute's type by index.
*
* @param index The attribute's index (zero-based).
* @return The attribute's type, "CDATA" if the type is unknown, or null
* if the index is out of bounds.
* @see org.xml.sax.Attributes#getType(int)
*/
public String getType (int index)
{
if (index >= 0 && index < length) {
return data[index*5+3];
} else {
return null;
}
}
/**
* Return an attribute's value by index.
*
* @param index The attribute's index (zero-based).
* @return The attribute's value or null if the index is out of bounds.
* @see org.xml.sax.Attributes#getValue(int)
*/
public String getValue (int index)
{
if (index >= 0 && index < length) {
return data[index*5+4];
} else {
return null;
}
}
/**
* Look up an attribute's index by Namespace name.
*
* <p>In many cases, it will be more efficient to look up the name once and
* use the index query methods rather than using the name query methods
* repeatedly.</p>
*
* @param uri The attribute's Namespace URI, or the empty
* string if none is available.
* @param localName The attribute's local name.
* @return The attribute's index, or -1 if none matches.
* @see org.xml.sax.Attributes#getIndex(java.lang.String,java.lang.String)
*/
public int getIndex (String uri, String localName)
{
int max = length * 5;
for (int i = 0; i < max; i += 5) {
if (data[i].equals(uri) && data[i+1].equals(localName)) {
return i / 5;
}
}
return -1;
}
/**
* Look up an attribute's index by qualified (prefixed) name.
*
* @param qName The qualified name.
* @return The attribute's index, or -1 if none matches.
* @see org.xml.sax.Attributes#getIndex(java.lang.String)
*/
public int getIndex (String qName)
{
int max = length * 5;
for (int i = 0; i < max; i += 5) {
if (data[i+2].equals(qName)) {
return i / 5;
}
}
return -1;
}
/**
* Look up an attribute's type by Namespace-qualified name.
*
* @param uri The Namespace URI, or the empty string for a name
* with no explicit Namespace URI.
* @param localName The local name.
* @return The attribute's type, or null if there is no
* matching attribute.
* @see org.xml.sax.Attributes#getType(java.lang.String,java.lang.String)
*/
public String getType (String uri, String localName)
{
int max = length * 5;
for (int i = 0; i < max; i += 5) {
if (data[i].equals(uri) && data[i+1].equals(localName)) {
return data[i+3];
}
}
return null;
}
/**
* Look up an attribute's type by qualified (prefixed) name.
*
* @param qName The qualified name.
* @return The attribute's type, or null if there is no
* matching attribute.
* @see org.xml.sax.Attributes#getType(java.lang.String)
*/
public String getType (String qName)
{
int max = length * 5;
for (int i = 0; i < max; i += 5) {
if (data[i+2].equals(qName)) {
return data[i+3];
}
}
return null;
}
/**
* Look up an attribute's value by Namespace-qualified name.
*
* @param uri The Namespace URI, or the empty string for a name
* with no explicit Namespace URI.
* @param localName The local name.
* @return The attribute's value, or null if there is no
* matching attribute.
* @see org.xml.sax.Attributes#getValue(java.lang.String,java.lang.String)
*/
public String getValue (String uri, String localName)
{
int max = length * 5;
for (int i = 0; i < max; i += 5) {
if (data[i].equals(uri) && data[i+1].equals(localName)) {
return data[i+4];
}
}
return null;
}
/**
* Look up an attribute's value by qualified (prefixed) name.
*
* @param qName The qualified name.
* @return The attribute's value, or null if there is no
* matching attribute.
* @see org.xml.sax.Attributes#getValue(java.lang.String)
*/
public String getValue (String qName)
{
int max = length * 5;
for (int i = 0; i < max; i += 5) {
if (data[i+2].equals(qName)) {
return data[i+4];
}
}
return null;
}
////////////////////////////////////////////////////////////////////
// Manipulators.
////////////////////////////////////////////////////////////////////
/**
* Clear the attribute list for reuse.
*
* <p>Note that little memory is freed by this call:
* the current array is kept so it can be
* reused.</p>
*/
public void clear ()
{
if (data != null) {
for (int i = 0; i < (length * 5); i++)
data [i] = null;
}
length = 0;
}
/**
* Copy an entire Attributes object.
*
* <p>It may be more efficient to reuse an existing object
* rather than constantly allocating new ones.</p>
*
* @param atts The attributes to copy.
*/
public void setAttributes (Attributes atts)
{
clear();
length = atts.getLength();
if (length > 0) {
data = new String[length*5];
for (int i = 0; i < length; i++) {
data[i*5] = atts.getURI(i);
data[i*5+1] = atts.getLocalName(i);
data[i*5+2] = atts.getQName(i);
data[i*5+3] = atts.getType(i);
data[i*5+4] = atts.getValue(i);
}
}
}
/**
* Add an attribute to the end of the list.
*
* <p>For the sake of speed, this method does no checking
* to see if the attribute is already in the list: that is
* the responsibility of the application.</p>
*
* @param uri The Namespace URI, or the empty string if
* none is available or Namespace processing is not
* being performed.
* @param localName The local name, or the empty string if
* Namespace processing is not being performed.
* @param qName The qualified (prefixed) name, or the empty string
* if qualified names are not available.
* @param type The attribute type as a string.
* @param value The attribute value.
*/
public void addAttribute (String uri, String localName, String qName,
String type, String value)
{
ensureCapacity(length+1);
data[length*5] = uri;
data[length*5+1] = localName;
data[length*5+2] = qName;
data[length*5+3] = type;
data[length*5+4] = value;
length++;
}
/**
* Set an attribute in the list.
*
* <p>For the sake of speed, this method does no checking
* for name conflicts or well-formedness: such checks are the
* responsibility of the application.</p>
*
* @param index The index of the attribute (zero-based).
* @param uri The Namespace URI, or the empty string if
* none is available or Namespace processing is not
* being performed.
* @param localName The local name, or the empty string if
* Namespace processing is not being performed.
* @param qName The qualified name, or the empty string
* if qualified names are not available.
* @param type The attribute type as a string.
* @param value The attribute value.
* @exception java.lang.ArrayIndexOutOfBoundsException When the
* supplied index does not point to an attribute
* in the list.
*/
public void setAttribute (int index, String uri, String localName,
String qName, String type, String value)
{
if (index >= 0 && index < length) {
data[index*5] = uri;
data[index*5+1] = localName;
data[index*5+2] = qName;
data[index*5+3] = type;
data[index*5+4] = value;
} else {
badIndex(index);
}
}
/**
* Remove an attribute from the list.
*
* @param index The index of the attribute (zero-based).
* @exception java.lang.ArrayIndexOutOfBoundsException When the
* supplied index does not point to an attribute
* in the list.
*/
public void removeAttribute (int index)
{
if (index >= 0 && index < length) {
if (index < length - 1) {
System.arraycopy(data, (index+1)*5, data, index*5,
(length-index-1)*5);
}
index = (length - 1) * 5;
data [index++] = null;
data [index++] = null;
data [index++] = null;
data [index++] = null;
data [index] = null;
length--;
} else {
badIndex(index);
}
}
/**
* Set the Namespace URI of a specific attribute.
*
* @param index The index of the attribute (zero-based).
* @param uri The attribute's Namespace URI, or the empty
* string for none.
* @exception java.lang.ArrayIndexOutOfBoundsException When the
* supplied index does not point to an attribute
* in the list.
*/
public void setURI (int index, String uri)
{
if (index >= 0 && index < length) {
data[index*5] = uri;
} else {
badIndex(index);
}
}
/**
* Set the local name of a specific attribute.
*
* @param index The index of the attribute (zero-based).
* @param localName The attribute's local name, or the empty
* string for none.
* @exception java.lang.ArrayIndexOutOfBoundsException When the
* supplied index does not point to an attribute
* in the list.
*/
public void setLocalName (int index, String localName)
{
if (index >= 0 && index < length) {
data[index*5+1] = localName;
} else {
badIndex(index);
}
}
/**
* Set the qualified name of a specific attribute.
*
* @param index The index of the attribute (zero-based).
* @param qName The attribute's qualified name, or the empty
* string for none.
* @exception java.lang.ArrayIndexOutOfBoundsException When the
* supplied index does not point to an attribute
* in the list.
*/
public void setQName (int index, String qName)
{
if (index >= 0 && index < length) {
data[index*5+2] = qName;
} else {
badIndex(index);
}
}
/**
* Set the type of a specific attribute.
*
* @param index The index of the attribute (zero-based).
* @param type The attribute's type.
* @exception java.lang.ArrayIndexOutOfBoundsException When the
* supplied index does not point to an attribute
* in the list.
*/
public void setType (int index, String type)
{
if (index >= 0 && index < length) {
data[index*5+3] = type;
} else {
badIndex(index);
}
}
/**
* Set the value of a specific attribute.
*
* @param index The index of the attribute (zero-based).
* @param value The attribute's value.
* @exception java.lang.ArrayIndexOutOfBoundsException When the
* supplied index does not point to an attribute
* in the list.
*/
public void setValue (int index, String value)
{
if (index >= 0 && index < length) {
data[index*5+4] = value;
} else {
badIndex(index);
}
}
////////////////////////////////////////////////////////////////////
// Internal methods.
////////////////////////////////////////////////////////////////////
/**
* Ensure the internal array's capacity.
*
* @param n The minimum number of attributes that the array must
* be able to hold.
*/
private void ensureCapacity (int n) {
if (n <= 0) {
return;
}
int max;
if (data == null || data.length == 0) {
max = 25;
}
else if (data.length >= n * 5) {
return;
}
else {
max = data.length;
}
while (max < n * 5) {
max *= 2;
}
String newData[] = new String[max];
if (length > 0) {
System.arraycopy(data, 0, newData, 0, length*5);
}
data = newData;
}
/**
* Report a bad array index in a manipulator.
*
* @param index The index to report.
* @exception java.lang.ArrayIndexOutOfBoundsException Always.
*/
private void badIndex (int index)
throws ArrayIndexOutOfBoundsException
{
String msg =
"Attempt to modify attribute at illegal index: " + index;
throw new ArrayIndexOutOfBoundsException(msg);
}
////////////////////////////////////////////////////////////////////
// Internal state.
////////////////////////////////////////////////////////////////////
int length;
String data [];
}
// end of AttributesImpl.java

View File

@@ -0,0 +1,491 @@
/*
* Copyright (c) 2000, 2006, 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. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* 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.
*/
// DefaultHandler.java - default implementation of the core handlers.
// http://www.saxproject.org
// Written by David Megginson
// NO WARRANTY! This class is in the public domain.
// $Id: DefaultHandler.java,v 1.3 2006/04/13 02:06:32 jeffsuttor Exp $
package org.xml.sax.helpers;
import java.io.IOException;
import org.xml.sax.InputSource;
import org.xml.sax.Locator;
import org.xml.sax.Attributes;
import org.xml.sax.EntityResolver;
import org.xml.sax.DTDHandler;
import org.xml.sax.ContentHandler;
import org.xml.sax.ErrorHandler;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
/**
* Default base class for SAX2 event handlers.
*
* <blockquote>
* <em>This module, both source code and documentation, is in the
* Public Domain, and comes with <strong>NO WARRANTY</strong>.</em>
* See <a href='http://www.saxproject.org'>http://www.saxproject.org</a>
* for further information.
* </blockquote>
*
* <p>This class is available as a convenience base class for SAX2
* applications: it provides default implementations for all of the
* callbacks in the four core SAX2 handler classes:</p>
*
* <ul>
* <li>{@link org.xml.sax.EntityResolver EntityResolver}</li>
* <li>{@link org.xml.sax.DTDHandler DTDHandler}</li>
* <li>{@link org.xml.sax.ContentHandler ContentHandler}</li>
* <li>{@link org.xml.sax.ErrorHandler ErrorHandler}</li>
* </ul>
*
* <p>Application writers can extend this class when they need to
* implement only part of an interface; parser writers can
* instantiate this class to provide default handlers when the
* application has not supplied its own.</p>
*
* <p>This class replaces the deprecated SAX1
* {@link org.xml.sax.HandlerBase HandlerBase} class.</p>
*
* @since SAX 2.0
* @author David Megginson,
* @see org.xml.sax.EntityResolver
* @see org.xml.sax.DTDHandler
* @see org.xml.sax.ContentHandler
* @see org.xml.sax.ErrorHandler
*/
public class DefaultHandler
implements EntityResolver, DTDHandler, ContentHandler, ErrorHandler
{
////////////////////////////////////////////////////////////////////
// Default implementation of the EntityResolver interface.
////////////////////////////////////////////////////////////////////
/**
* Resolve an external entity.
*
* <p>Always return null, so that the parser will use the system
* identifier provided in the XML document. This method implements
* the SAX default behaviour: application writers can override it
* in a subclass to do special translations such as catalog lookups
* or URI redirection.</p>
*
* @param publicId The public identifier, or null if none is
* available.
* @param systemId The system identifier provided in the XML
* document.
* @return The new input source, or null to require the
* default behaviour.
* @exception java.io.IOException If there is an error setting
* up the new input source.
* @exception org.xml.sax.SAXException Any SAX exception, possibly
* wrapping another exception.
* @see org.xml.sax.EntityResolver#resolveEntity
*/
public InputSource resolveEntity (String publicId, String systemId)
throws IOException, SAXException
{
return null;
}
////////////////////////////////////////////////////////////////////
// Default implementation of DTDHandler interface.
////////////////////////////////////////////////////////////////////
/**
* Receive notification of a notation declaration.
*
* <p>By default, do nothing. Application writers may override this
* method in a subclass if they wish to keep track of the notations
* declared in a document.</p>
*
* @param name The notation name.
* @param publicId The notation public identifier, or null if not
* available.
* @param systemId The notation system identifier.
* @exception org.xml.sax.SAXException Any SAX exception, possibly
* wrapping another exception.
* @see org.xml.sax.DTDHandler#notationDecl
*/
public void notationDecl (String name, String publicId, String systemId)
throws SAXException
{
// no op
}
/**
* Receive notification of an unparsed entity declaration.
*
* <p>By default, do nothing. Application writers may override this
* method in a subclass to keep track of the unparsed entities
* declared in a document.</p>
*
* @param name The entity name.
* @param publicId The entity public identifier, or null if not
* available.
* @param systemId The entity system identifier.
* @param notationName The name of the associated notation.
* @exception org.xml.sax.SAXException Any SAX exception, possibly
* wrapping another exception.
* @see org.xml.sax.DTDHandler#unparsedEntityDecl
*/
public void unparsedEntityDecl (String name, String publicId,
String systemId, String notationName)
throws SAXException
{
// no op
}
////////////////////////////////////////////////////////////////////
// Default implementation of ContentHandler interface.
////////////////////////////////////////////////////////////////////
/**
* Receive a Locator object for document events.
*
* <p>By default, do nothing. Application writers may override this
* method in a subclass if they wish to store the locator for use
* with other document events.</p>
*
* @param locator A locator for all SAX document events.
* @see org.xml.sax.ContentHandler#setDocumentLocator
* @see org.xml.sax.Locator
*/
public void setDocumentLocator (Locator locator)
{
// no op
}
/**
* Receive notification of the beginning of the document.
*
* <p>By default, do nothing. Application writers may override this
* method in a subclass to take specific actions at the beginning
* of a document (such as allocating the root node of a tree or
* creating an output file).</p>
*
* @exception org.xml.sax.SAXException Any SAX exception, possibly
* wrapping another exception.
* @see org.xml.sax.ContentHandler#startDocument
*/
public void startDocument ()
throws SAXException
{
// no op
}
/**
* Receive notification of the end of the document.
*
* <p>By default, do nothing. Application writers may override this
* method in a subclass to take specific actions at the end
* of a document (such as finalising a tree or closing an output
* file).</p>
*
* @exception org.xml.sax.SAXException Any SAX exception, possibly
* wrapping another exception.
* @see org.xml.sax.ContentHandler#endDocument
*/
public void endDocument ()
throws SAXException
{
// no op
}
/**
* Receive notification of the start of a Namespace mapping.
*
* <p>By default, do nothing. Application writers may override this
* method in a subclass to take specific actions at the start of
* each Namespace prefix scope (such as storing the prefix mapping).</p>
*
* @param prefix The Namespace prefix being declared.
* @param uri The Namespace URI mapped to the prefix.
* @exception org.xml.sax.SAXException Any SAX exception, possibly
* wrapping another exception.
* @see org.xml.sax.ContentHandler#startPrefixMapping
*/
public void startPrefixMapping (String prefix, String uri)
throws SAXException
{
// no op
}
/**
* Receive notification of the end of a Namespace mapping.
*
* <p>By default, do nothing. Application writers may override this
* method in a subclass to take specific actions at the end of
* each prefix mapping.</p>
*
* @param prefix The Namespace prefix being declared.
* @exception org.xml.sax.SAXException Any SAX exception, possibly
* wrapping another exception.
* @see org.xml.sax.ContentHandler#endPrefixMapping
*/
public void endPrefixMapping (String prefix)
throws SAXException
{
// no op
}
/**
* Receive notification of the start of an element.
*
* <p>By default, do nothing. Application writers may override this
* method in a subclass to take specific actions at the start of
* each element (such as allocating a new tree node or writing
* output to a file).</p>
*
* @param uri The Namespace URI, or the empty string if the
* element has no Namespace URI or if Namespace
* processing is not being performed.
* @param localName The local name (without prefix), or the
* empty string if Namespace processing is not being
* performed.
* @param qName The qualified name (with prefix), or the
* empty string if qualified names are not available.
* @param attributes The attributes attached to the element. If
* there are no attributes, it shall be an empty
* Attributes object.
* @exception org.xml.sax.SAXException Any SAX exception, possibly
* wrapping another exception.
* @see org.xml.sax.ContentHandler#startElement
*/
public void startElement (String uri, String localName,
String qName, Attributes attributes)
throws SAXException
{
// no op
}
/**
* Receive notification of the end of an element.
*
* <p>By default, do nothing. Application writers may override this
* method in a subclass to take specific actions at the end of
* each element (such as finalising a tree node or writing
* output to a file).</p>
*
* @param uri The Namespace URI, or the empty string if the
* element has no Namespace URI or if Namespace
* processing is not being performed.
* @param localName The local name (without prefix), or the
* empty string if Namespace processing is not being
* performed.
* @param qName The qualified name (with prefix), or the
* empty string if qualified names are not available.
* @exception org.xml.sax.SAXException Any SAX exception, possibly
* wrapping another exception.
* @see org.xml.sax.ContentHandler#endElement
*/
public void endElement (String uri, String localName, String qName)
throws SAXException
{
// no op
}
/**
* Receive notification of character data inside an element.
*
* <p>By default, do nothing. Application writers may override this
* method to take specific actions for each chunk of character data
* (such as adding the data to a node or buffer, or printing it to
* a file).</p>
*
* @param ch The characters.
* @param start The start position in the character array.
* @param length The number of characters to use from the
* character array.
* @exception org.xml.sax.SAXException Any SAX exception, possibly
* wrapping another exception.
* @see org.xml.sax.ContentHandler#characters
*/
public void characters (char ch[], int start, int length)
throws SAXException
{
// no op
}
/**
* Receive notification of ignorable whitespace in element content.
*
* <p>By default, do nothing. Application writers may override this
* method to take specific actions for each chunk of ignorable
* whitespace (such as adding data to a node or buffer, or printing
* it to a file).</p>
*
* @param ch The whitespace characters.
* @param start The start position in the character array.
* @param length The number of characters to use from the
* character array.
* @exception org.xml.sax.SAXException Any SAX exception, possibly
* wrapping another exception.
* @see org.xml.sax.ContentHandler#ignorableWhitespace
*/
public void ignorableWhitespace (char ch[], int start, int length)
throws SAXException
{
// no op
}
/**
* Receive notification of a processing instruction.
*
* <p>By default, do nothing. Application writers may override this
* method in a subclass to take specific actions for each
* processing instruction, such as setting status variables or
* invoking other methods.</p>
*
* @param target The processing instruction target.
* @param data The processing instruction data, or null if
* none is supplied.
* @exception org.xml.sax.SAXException Any SAX exception, possibly
* wrapping another exception.
* @see org.xml.sax.ContentHandler#processingInstruction
*/
public void processingInstruction (String target, String data)
throws SAXException
{
// no op
}
/**
* Receive notification of a skipped entity.
*
* <p>By default, do nothing. Application writers may override this
* method in a subclass to take specific actions for each
* processing instruction, such as setting status variables or
* invoking other methods.</p>
*
* @param name The name of the skipped entity.
* @exception org.xml.sax.SAXException Any SAX exception, possibly
* wrapping another exception.
* @see org.xml.sax.ContentHandler#processingInstruction
*/
public void skippedEntity (String name)
throws SAXException
{
// no op
}
////////////////////////////////////////////////////////////////////
// Default implementation of the ErrorHandler interface.
////////////////////////////////////////////////////////////////////
/**
* Receive notification of a parser warning.
*
* <p>The default implementation does nothing. Application writers
* may override this method in a subclass to take specific actions
* for each warning, such as inserting the message in a log file or
* printing it to the console.</p>
*
* @param e The warning information encoded as an exception.
* @exception org.xml.sax.SAXException Any SAX exception, possibly
* wrapping another exception.
* @see org.xml.sax.ErrorHandler#warning
* @see org.xml.sax.SAXParseException
*/
public void warning (SAXParseException e)
throws SAXException
{
// no op
}
/**
* Receive notification of a recoverable parser error.
*
* <p>The default implementation does nothing. Application writers
* may override this method in a subclass to take specific actions
* for each error, such as inserting the message in a log file or
* printing it to the console.</p>
*
* @param e The error information encoded as an exception.
* @exception org.xml.sax.SAXException Any SAX exception, possibly
* wrapping another exception.
* @see org.xml.sax.ErrorHandler#warning
* @see org.xml.sax.SAXParseException
*/
public void error (SAXParseException e)
throws SAXException
{
// no op
}
/**
* Report a fatal XML parsing error.
*
* <p>The default implementation throws a SAXParseException.
* Application writers may override this method in a subclass if
* they need to take specific actions for each fatal error (such as
* collecting all of the errors into a single report): in any case,
* the application must stop all regular processing when this
* method is invoked, since the document is no longer reliable, and
* the parser may no longer report parsing events.</p>
*
* @param e The error information encoded as an exception.
* @exception org.xml.sax.SAXException Any SAX exception, possibly
* wrapping another exception.
* @see org.xml.sax.ErrorHandler#fatalError
* @see org.xml.sax.SAXParseException
*/
public void fatalError (SAXParseException e)
throws SAXException
{
throw e;
}
}
// end of DefaultHandler.java

View File

@@ -0,0 +1,238 @@
/*
* Copyright (c) 2000, 2005, 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. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* 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.
*/
// SAX default implementation for Locator.
// http://www.saxproject.org
// No warranty; no copyright -- use this as you will.
// $Id: LocatorImpl.java,v 1.2 2004/11/03 22:53:09 jsuttor Exp $
package org.xml.sax.helpers;
import org.xml.sax.Locator;
/**
* Provide an optional convenience implementation of Locator.
*
* <blockquote>
* <em>This module, both source code and documentation, is in the
* Public Domain, and comes with <strong>NO WARRANTY</strong>.</em>
* See <a href='http://www.saxproject.org'>http://www.saxproject.org</a>
* for further information.
* </blockquote>
*
* <p>This class is available mainly for application writers, who
* can use it to make a persistent snapshot of a locator at any
* point during a document parse:</p>
*
* <pre>
* Locator locator;
* Locator startloc;
*
* public void setLocator (Locator locator)
* {
* // note the locator
* this.locator = locator;
* }
*
* public void startDocument ()
* {
* // save the location of the start of the document
* // for future use.
* Locator startloc = new LocatorImpl(locator);
* }
*</pre>
*
* <p>Normally, parser writers will not use this class, since it
* is more efficient to provide location information only when
* requested, rather than constantly updating a Locator object.</p>
*
* @since SAX 1.0
* @author David Megginson
* @see org.xml.sax.Locator Locator
*/
public class LocatorImpl implements Locator
{
/**
* Zero-argument constructor.
*
* <p>This will not normally be useful, since the main purpose
* of this class is to make a snapshot of an existing Locator.</p>
*/
public LocatorImpl ()
{
}
/**
* Copy constructor.
*
* <p>Create a persistent copy of the current state of a locator.
* When the original locator changes, this copy will still keep
* the original values (and it can be used outside the scope of
* DocumentHandler methods).</p>
*
* @param locator The locator to copy.
*/
public LocatorImpl (Locator locator)
{
setPublicId(locator.getPublicId());
setSystemId(locator.getSystemId());
setLineNumber(locator.getLineNumber());
setColumnNumber(locator.getColumnNumber());
}
////////////////////////////////////////////////////////////////////
// Implementation of org.xml.sax.Locator
////////////////////////////////////////////////////////////////////
/**
* Return the saved public identifier.
*
* @return The public identifier as a string, or null if none
* is available.
* @see org.xml.sax.Locator#getPublicId
* @see #setPublicId
*/
public String getPublicId ()
{
return publicId;
}
/**
* Return the saved system identifier.
*
* @return The system identifier as a string, or null if none
* is available.
* @see org.xml.sax.Locator#getSystemId
* @see #setSystemId
*/
public String getSystemId ()
{
return systemId;
}
/**
* Return the saved line number (1-based).
*
* @return The line number as an integer, or -1 if none is available.
* @see org.xml.sax.Locator#getLineNumber
* @see #setLineNumber
*/
public int getLineNumber ()
{
return lineNumber;
}
/**
* Return the saved column number (1-based).
*
* @return The column number as an integer, or -1 if none is available.
* @see org.xml.sax.Locator#getColumnNumber
* @see #setColumnNumber
*/
public int getColumnNumber ()
{
return columnNumber;
}
////////////////////////////////////////////////////////////////////
// Setters for the properties (not in org.xml.sax.Locator)
////////////////////////////////////////////////////////////////////
/**
* Set the public identifier for this locator.
*
* @param publicId The new public identifier, or null
* if none is available.
* @see #getPublicId
*/
public void setPublicId (String publicId)
{
this.publicId = publicId;
}
/**
* Set the system identifier for this locator.
*
* @param systemId The new system identifier, or null
* if none is available.
* @see #getSystemId
*/
public void setSystemId (String systemId)
{
this.systemId = systemId;
}
/**
* Set the line number for this locator (1-based).
*
* @param lineNumber The line number, or -1 if none is available.
* @see #getLineNumber
*/
public void setLineNumber (int lineNumber)
{
this.lineNumber = lineNumber;
}
/**
* Set the column number for this locator (1-based).
*
* @param columnNumber The column number, or -1 if none is available.
* @see #getColumnNumber
*/
public void setColumnNumber (int columnNumber)
{
this.columnNumber = columnNumber;
}
////////////////////////////////////////////////////////////////////
// Internal state.
////////////////////////////////////////////////////////////////////
private String publicId;
private String systemId;
private int lineNumber;
private int columnNumber;
}
// end of LocatorImpl.java

View File

@@ -0,0 +1,850 @@
/*
* Copyright (c) 2000, 2015, 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. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* 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.
*/
// NamespaceSupport.java - generic Namespace support for SAX.
// http://www.saxproject.org
// Written by David Megginson
// This class is in the Public Domain. NO WARRANTY!
// $Id: NamespaceSupport.java,v 1.5 2004/11/03 22:53:09 jsuttor Exp $
package org.xml.sax.helpers;
import java.util.ArrayList;
import java.util.Collections;
import java.util.EmptyStackException;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* Encapsulate Namespace logic for use by applications using SAX,
* or internally by SAX drivers.
*
* <blockquote>
* <em>This module, both source code and documentation, is in the
* Public Domain, and comes with <strong>NO WARRANTY</strong>.</em>
* See <a href='http://www.saxproject.org'>http://www.saxproject.org</a>
* for further information.
* </blockquote>
*
* <p>This class encapsulates the logic of Namespace processing: it
* tracks the declarations currently in force for each context and
* automatically processes qualified XML names into their Namespace
* parts; it can also be used in reverse for generating XML qnames
* from Namespaces.</p>
*
* <p>Namespace support objects are reusable, but the reset method
* must be invoked between each session.</p>
*
* <p>Here is a simple session:</p>
*
* <pre>
* String parts[] = new String[3];
* NamespaceSupport support = new NamespaceSupport();
*
* support.pushContext();
* support.declarePrefix("", "http://www.w3.org/1999/xhtml");
* support.declarePrefix("dc", "http://www.purl.org/dc#");
*
* parts = support.processName("p", parts, false);
* System.out.println("Namespace URI: " + parts[0]);
* System.out.println("Local name: " + parts[1]);
* System.out.println("Raw name: " + parts[2]);
*
* parts = support.processName("dc:title", parts, false);
* System.out.println("Namespace URI: " + parts[0]);
* System.out.println("Local name: " + parts[1]);
* System.out.println("Raw name: " + parts[2]);
*
* support.popContext();
* </pre>
*
* <p>Note that this class is optimized for the use case where most
* elements do not contain Namespace declarations: if the same
* prefix/URI mapping is repeated for each context (for example), this
* class will be somewhat less efficient.</p>
*
* <p>Although SAX drivers (parsers) may choose to use this class to
* implement namespace handling, they are not required to do so.
* Applications must track namespace information themselves if they
* want to use namespace information.
*
* @since SAX 2.0
* @author David Megginson
*/
public class NamespaceSupport
{
////////////////////////////////////////////////////////////////////
// Constants.
////////////////////////////////////////////////////////////////////
/**
* The XML Namespace URI as a constant.
* The value is <code>http://www.w3.org/XML/1998/namespace</code>
* as defined in the "Namespaces in XML" * recommendation.
*
* <p>This is the Namespace URI that is automatically mapped
* to the "xml" prefix.</p>
*/
public final static String XMLNS =
"http://www.w3.org/XML/1998/namespace";
/**
* The namespace declaration URI as a constant.
* The value is <code>http://www.w3.org/xmlns/2000/</code>, as defined
* in a backwards-incompatible erratum to the "Namespaces in XML"
* recommendation. Because that erratum postdated SAX2, SAX2 defaults
* to the original recommendation, and does not normally use this URI.
*
*
* <p>This is the Namespace URI that is optionally applied to
* <em>xmlns</em> and <em>xmlns:*</em> attributes, which are used to
* declare namespaces. </p>
*
* @since SAX 2.1alpha
* @see #setNamespaceDeclUris
* @see #isNamespaceDeclUris
*/
public final static String NSDECL =
"http://www.w3.org/xmlns/2000/";
/**
* An empty enumeration.
*/
private final static Enumeration EMPTY_ENUMERATION =
Collections.enumeration(new ArrayList<String>());
////////////////////////////////////////////////////////////////////
// Constructor.
////////////////////////////////////////////////////////////////////
/**
* Create a new Namespace support object.
*/
public NamespaceSupport ()
{
reset();
}
////////////////////////////////////////////////////////////////////
// Context management.
////////////////////////////////////////////////////////////////////
/**
* Reset this Namespace support object for reuse.
*
* <p>It is necessary to invoke this method before reusing the
* Namespace support object for a new session. If namespace
* declaration URIs are to be supported, that flag must also
* be set to a non-default value.
* </p>
*
* @see #setNamespaceDeclUris
*/
public void reset ()
{
contexts = new Context[32];
namespaceDeclUris = false;
contextPos = 0;
contexts[contextPos] = currentContext = new Context();
currentContext.declarePrefix("xml", XMLNS);
}
/**
* Start a new Namespace context.
* The new context will automatically inherit
* the declarations of its parent context, but it will also keep
* track of which declarations were made within this context.
*
* <p>Event callback code should start a new context once per element.
* This means being ready to call this in either of two places.
* For elements that don't include namespace declarations, the
* <em>ContentHandler.startElement()</em> callback is the right place.
* For elements with such a declaration, it'd done in the first
* <em>ContentHandler.startPrefixMapping()</em> callback.
* A boolean flag can be used to
* track whether a context has been started yet. When either of
* those methods is called, it checks the flag to see if a new context
* needs to be started. If so, it starts the context and sets the
* flag. After <em>ContentHandler.startElement()</em>
* does that, it always clears the flag.
*
* <p>Normally, SAX drivers would push a new context at the beginning
* of each XML element. Then they perform a first pass over the
* attributes to process all namespace declarations, making
* <em>ContentHandler.startPrefixMapping()</em> callbacks.
* Then a second pass is made, to determine the namespace-qualified
* names for all attributes and for the element name.
* Finally all the information for the
* <em>ContentHandler.startElement()</em> callback is available,
* so it can then be made.
*
* <p>The Namespace support object always starts with a base context
* already in force: in this context, only the "xml" prefix is
* declared.</p>
*
* @see org.xml.sax.ContentHandler
* @see #popContext
*/
public void pushContext ()
{
int max = contexts.length;
contextPos++;
// Extend the array if necessary
if (contextPos >= max) {
Context newContexts[] = new Context[max*2];
System.arraycopy(contexts, 0, newContexts, 0, max);
max *= 2;
contexts = newContexts;
}
// Allocate the context if necessary.
currentContext = contexts[contextPos];
if (currentContext == null) {
contexts[contextPos] = currentContext = new Context();
}
// Set the parent, if any.
if (contextPos > 0) {
currentContext.setParent(contexts[contextPos - 1]);
}
}
/**
* Revert to the previous Namespace context.
*
* <p>Normally, you should pop the context at the end of each
* XML element. After popping the context, all Namespace prefix
* mappings that were previously in force are restored.</p>
*
* <p>You must not attempt to declare additional Namespace
* prefixes after popping a context, unless you push another
* context first.</p>
*
* @see #pushContext
*/
public void popContext ()
{
contexts[contextPos].clear();
contextPos--;
if (contextPos < 0) {
throw new EmptyStackException();
}
currentContext = contexts[contextPos];
}
////////////////////////////////////////////////////////////////////
// Operations within a context.
////////////////////////////////////////////////////////////////////
/**
* Declare a Namespace prefix. All prefixes must be declared
* before they are referenced. For example, a SAX driver (parser)
* would scan an element's attributes
* in two passes: first for namespace declarations,
* then a second pass using {@link #processName processName()} to
* interpret prefixes against (potentially redefined) prefixes.
*
* <p>This method declares a prefix in the current Namespace
* context; the prefix will remain in force until this context
* is popped, unless it is shadowed in a descendant context.</p>
*
* <p>To declare the default element Namespace, use the empty string as
* the prefix.</p>
*
* <p>Note that there is an asymmetry in this library: {@link
* #getPrefix getPrefix} will not return the "" prefix,
* even if you have declared a default element namespace.
* To check for a default namespace,
* you have to look it up explicitly using {@link #getURI getURI}.
* This asymmetry exists to make it easier to look up prefixes
* for attribute names, where the default prefix is not allowed.</p>
*
* @param prefix The prefix to declare, or the empty string to
* indicate the default element namespace. This may never have
* the value "xml" or "xmlns".
* @param uri The Namespace URI to associate with the prefix.
* @return true if the prefix was legal, false otherwise
*
* @see #processName
* @see #getURI
* @see #getPrefix
*/
public boolean declarePrefix (String prefix, String uri)
{
if (prefix.equals("xml") || prefix.equals("xmlns")) {
return false;
} else {
currentContext.declarePrefix(prefix, uri);
return true;
}
}
/**
* Process a raw XML qualified name, after all declarations in the
* current context have been handled by {@link #declarePrefix
* declarePrefix()}.
*
* <p>This method processes a raw XML qualified name in the
* current context by removing the prefix and looking it up among
* the prefixes currently declared. The return value will be the
* array supplied by the caller, filled in as follows:</p>
*
* <dl>
* <dt>parts[0]</dt>
* <dd>The Namespace URI, or an empty string if none is
* in use.</dd>
* <dt>parts[1]</dt>
* <dd>The local name (without prefix).</dd>
* <dt>parts[2]</dt>
* <dd>The original raw name.</dd>
* </dl>
*
* <p>All of the strings in the array will be internalized. If
* the raw name has a prefix that has not been declared, then
* the return value will be null.</p>
*
* <p>Note that attribute names are processed differently than
* element names: an unprefixed element name will receive the
* default Namespace (if any), while an unprefixed attribute name
* will not.</p>
*
* @param qName The XML qualified name to be processed.
* @param parts An array supplied by the caller, capable of
* holding at least three members.
* @param isAttribute A flag indicating whether this is an
* attribute name (true) or an element name (false).
* @return The supplied array holding three internalized strings
* representing the Namespace URI (or empty string), the
* local name, and the XML qualified name; or null if there
* is an undeclared prefix.
* @see #declarePrefix
* @see java.lang.String#intern */
public String [] processName (String qName, String parts[],
boolean isAttribute)
{
String myParts[] = currentContext.processName(qName, isAttribute);
if (myParts == null) {
return null;
} else {
parts[0] = myParts[0];
parts[1] = myParts[1];
parts[2] = myParts[2];
return parts;
}
}
/**
* Look up a prefix and get the currently-mapped Namespace URI.
*
* <p>This method looks up the prefix in the current context.
* Use the empty string ("") for the default Namespace.</p>
*
* @param prefix The prefix to look up.
* @return The associated Namespace URI, or null if the prefix
* is undeclared in this context.
* @see #getPrefix
* @see #getPrefixes
*/
public String getURI (String prefix)
{
return currentContext.getURI(prefix);
}
/**
* Return an enumeration of all prefixes whose declarations are
* active in the current context.
* This includes declarations from parent contexts that have
* not been overridden.
*
* <p><strong>Note:</strong> if there is a default prefix, it will not be
* returned in this enumeration; check for the default prefix
* using the {@link #getURI getURI} with an argument of "".</p>
*
* @return An enumeration of prefixes (never empty).
* @see #getDeclaredPrefixes
* @see #getURI
*/
public Enumeration getPrefixes ()
{
return currentContext.getPrefixes();
}
/**
* Return one of the prefixes mapped to a Namespace URI.
*
* <p>If more than one prefix is currently mapped to the same
* URI, this method will make an arbitrary selection; if you
* want all of the prefixes, use the {@link #getPrefixes}
* method instead.</p>
*
* <p><strong>Note:</strong> this will never return the empty (default) prefix;
* to check for a default prefix, use the {@link #getURI getURI}
* method with an argument of "".</p>
*
* @param uri the namespace URI
* @return one of the prefixes currently mapped to the URI supplied,
* or null if none is mapped or if the URI is assigned to
* the default namespace
* @see #getPrefixes(java.lang.String)
* @see #getURI
*/
public String getPrefix (String uri)
{
return currentContext.getPrefix(uri);
}
/**
* Return an enumeration of all prefixes for a given URI whose
* declarations are active in the current context.
* This includes declarations from parent contexts that have
* not been overridden.
*
* <p>This method returns prefixes mapped to a specific Namespace
* URI. The xml: prefix will be included. If you want only one
* prefix that's mapped to the Namespace URI, and you don't care
* which one you get, use the {@link #getPrefix getPrefix}
* method instead.</p>
*
* <p><strong>Note:</strong> the empty (default) prefix is <em>never</em> included
* in this enumeration; to check for the presence of a default
* Namespace, use the {@link #getURI getURI} method with an
* argument of "".</p>
*
* @param uri The Namespace URI.
* @return An enumeration of prefixes (never empty).
* @see #getPrefix
* @see #getDeclaredPrefixes
* @see #getURI
*/
public Enumeration getPrefixes (String uri)
{
List<String> prefixes = new ArrayList<>();
Enumeration allPrefixes = getPrefixes();
while (allPrefixes.hasMoreElements()) {
String prefix = (String)allPrefixes.nextElement();
if (uri.equals(getURI(prefix))) {
prefixes.add(prefix);
}
}
return Collections.enumeration(prefixes);
}
/**
* Return an enumeration of all prefixes declared in this context.
*
* <p>The empty (default) prefix will be included in this
* enumeration; note that this behaviour differs from that of
* {@link #getPrefix} and {@link #getPrefixes}.</p>
*
* @return An enumeration of all prefixes declared in this
* context.
* @see #getPrefixes
* @see #getURI
*/
public Enumeration getDeclaredPrefixes ()
{
return currentContext.getDeclaredPrefixes();
}
/**
* Controls whether namespace declaration attributes are placed
* into the {@link #NSDECL NSDECL} namespace
* by {@link #processName processName()}. This may only be
* changed before any contexts have been pushed.
*
* @since SAX 2.1alpha
*
* @exception IllegalStateException when attempting to set this
* after any context has been pushed.
*/
public void setNamespaceDeclUris (boolean value)
{
if (contextPos != 0)
throw new IllegalStateException ();
if (value == namespaceDeclUris)
return;
namespaceDeclUris = value;
if (value)
currentContext.declarePrefix ("xmlns", NSDECL);
else {
contexts[contextPos] = currentContext = new Context();
currentContext.declarePrefix("xml", XMLNS);
}
}
/**
* Returns true if namespace declaration attributes are placed into
* a namespace. This behavior is not the default.
*
* @since SAX 2.1alpha
*/
public boolean isNamespaceDeclUris ()
{ return namespaceDeclUris; }
////////////////////////////////////////////////////////////////////
// Internal state.
////////////////////////////////////////////////////////////////////
private Context contexts[];
private Context currentContext;
private int contextPos;
private boolean namespaceDeclUris;
////////////////////////////////////////////////////////////////////
// Internal classes.
////////////////////////////////////////////////////////////////////
/**
* Internal class for a single Namespace context.
*
* <p>This module caches and reuses Namespace contexts,
* so the number allocated
* will be equal to the element depth of the document, not to the total
* number of elements (i.e. 5-10 rather than tens of thousands).
* Also, data structures used to represent contexts are shared when
* possible (child contexts without declarations) to further reduce
* the amount of memory that's consumed.
* </p>
*/
final class Context {
/**
* Create the root-level Namespace context.
*/
Context ()
{
copyTables();
}
/**
* (Re)set the parent of this Namespace context.
* The context must either have been freshly constructed,
* or must have been cleared.
*
* @param context The parent Namespace context object.
*/
void setParent (Context parent)
{
this.parent = parent;
declarations = null;
prefixTable = parent.prefixTable;
uriTable = parent.uriTable;
elementNameTable = parent.elementNameTable;
attributeNameTable = parent.attributeNameTable;
defaultNS = parent.defaultNS;
declSeen = false;
}
/**
* Makes associated state become collectible,
* invalidating this context.
* {@link #setParent} must be called before
* this context may be used again.
*/
void clear ()
{
parent = null;
prefixTable = null;
uriTable = null;
elementNameTable = null;
attributeNameTable = null;
defaultNS = null;
}
/**
* Declare a Namespace prefix for this context.
*
* @param prefix The prefix to declare.
* @param uri The associated Namespace URI.
* @see org.xml.sax.helpers.NamespaceSupport#declarePrefix
*/
void declarePrefix (String prefix, String uri)
{
// Lazy processing...
// if (!declsOK)
// throw new IllegalStateException (
// "can't declare any more prefixes in this context");
if (!declSeen) {
copyTables();
}
if (declarations == null) {
declarations = new ArrayList<>();
}
prefix = prefix.intern();
uri = uri.intern();
if ("".equals(prefix)) {
if ("".equals(uri)) {
defaultNS = null;
} else {
defaultNS = uri;
}
} else {
prefixTable.put(prefix, uri);
uriTable.put(uri, prefix); // may wipe out another prefix
}
declarations.add(prefix);
}
/**
* Process an XML qualified name in this context.
*
* @param qName The XML qualified name.
* @param isAttribute true if this is an attribute name.
* @return An array of three strings containing the
* URI part (or empty string), the local part,
* and the raw name, all internalized, or null
* if there is an undeclared prefix.
* @see org.xml.sax.helpers.NamespaceSupport#processName
*/
String [] processName (String qName, boolean isAttribute)
{
String name[];
Map<String, String[]> table;
// Select the appropriate table.
if (isAttribute) {
table = attributeNameTable;
} else {
table = elementNameTable;
}
// Start by looking in the cache, and
// return immediately if the name
// is already known in this content
name = (String[])table.get(qName);
if (name != null) {
return name;
}
// We haven't seen this name in this
// context before. Maybe in the parent
// context, but we can't assume prefix
// bindings are the same.
name = new String[3];
name[2] = qName.intern();
int index = qName.indexOf(':');
// No prefix.
if (index == -1) {
if (isAttribute) {
if (qName == "xmlns" && namespaceDeclUris)
name[0] = NSDECL;
else
name[0] = "";
} else if (defaultNS == null) {
name[0] = "";
} else {
name[0] = defaultNS;
}
name[1] = name[2];
}
// Prefix
else {
String prefix = qName.substring(0, index);
String local = qName.substring(index+1);
String uri;
if ("".equals(prefix)) {
uri = defaultNS;
} else {
uri = (String)prefixTable.get(prefix);
}
if (uri == null
|| (!isAttribute && "xmlns".equals (prefix))) {
return null;
}
name[0] = uri;
name[1] = local.intern();
}
// Save in the cache for future use.
// (Could be shared with parent context...)
table.put(name[2], name);
return name;
}
/**
* Look up the URI associated with a prefix in this context.
*
* @param prefix The prefix to look up.
* @return The associated Namespace URI, or null if none is
* declared.
* @see org.xml.sax.helpers.NamespaceSupport#getURI
*/
String getURI (String prefix)
{
if ("".equals(prefix)) {
return defaultNS;
} else if (prefixTable == null) {
return null;
} else {
return (String)prefixTable.get(prefix);
}
}
/**
* Look up one of the prefixes associated with a URI in this context.
*
* <p>Since many prefixes may be mapped to the same URI,
* the return value may be unreliable.</p>
*
* @param uri The URI to look up.
* @return The associated prefix, or null if none is declared.
* @see org.xml.sax.helpers.NamespaceSupport#getPrefix
*/
String getPrefix (String uri)
{
if (uriTable == null) {
return null;
} else {
return (String)uriTable.get(uri);
}
}
/**
* Return an enumeration of prefixes declared in this context.
*
* @return An enumeration of prefixes (possibly empty).
* @see org.xml.sax.helpers.NamespaceSupport#getDeclaredPrefixes
*/
Enumeration getDeclaredPrefixes ()
{
if (declarations == null) {
return EMPTY_ENUMERATION;
} else {
return Collections.enumeration(declarations);
}
}
/**
* Return an enumeration of all prefixes currently in force.
*
* <p>The default prefix, if in force, is <em>not</em>
* returned, and will have to be checked for separately.</p>
*
* @return An enumeration of prefixes (never empty).
* @see org.xml.sax.helpers.NamespaceSupport#getPrefixes
*/
Enumeration getPrefixes ()
{
if (prefixTable == null) {
return EMPTY_ENUMERATION;
} else {
return Collections.enumeration(prefixTable.keySet());
}
}
////////////////////////////////////////////////////////////////
// Internal methods.
////////////////////////////////////////////////////////////////
/**
* Copy on write for the internal tables in this context.
*
* <p>This class is optimized for the normal case where most
* elements do not contain Namespace declarations.</p>
*/
private void copyTables ()
{
if (prefixTable != null) {
prefixTable = new HashMap<>(prefixTable);
} else {
prefixTable = new HashMap<>();
}
if (uriTable != null) {
uriTable = new HashMap<>(uriTable);
} else {
uriTable = new HashMap<>();
}
elementNameTable = new HashMap<>();
attributeNameTable = new HashMap<>();
declSeen = true;
}
////////////////////////////////////////////////////////////////
// Protected state.
////////////////////////////////////////////////////////////////
Map<String, String> prefixTable;
Map<String, String> uriTable;
Map<String, String[]> elementNameTable;
Map<String, String[]> attributeNameTable;
String defaultNS = null;
////////////////////////////////////////////////////////////////
// Internal state.
////////////////////////////////////////////////////////////////
private List<String> declarations = null;
private boolean declSeen = false;
private Context parent = null;
}
}
// end of NamespaceSupport.java

View File

@@ -0,0 +1,87 @@
/*
* Copyright (c) 2001, 2013, 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. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* 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.
*/
// NewInstance.java - create a new instance of a class by name.
// http://www.saxproject.org
// Written by Edwin Goei, edwingo@apache.org
// and by David Brownell, dbrownell@users.sourceforge.net
// NO WARRANTY! This class is in the Public Domain.
// $Id: NewInstance.java,v 1.2 2005/06/10 03:50:50 jeffsuttor Exp $
package org.xml.sax.helpers;
import java.lang.reflect.Method;
import java.lang.reflect.InvocationTargetException;
/**
* Create a new instance of a class by name.
*
* <blockquote>
* <em>This module, both source code and documentation, is in the
* Public Domain, and comes with <strong>NO WARRANTY</strong>.</em>
* See <a href='http://www.saxproject.org'>http://www.saxproject.org</a>
* for further information.
* </blockquote>
*
* <p>This class contains a static method for creating an instance of a
* class from an explicit class name. It tries to use the thread's context
* ClassLoader if possible and falls back to using
* Class.forName(String).</p>
*
* <p>This code is designed to compile and run on JDK version 1.1 and later
* including versions of Java 2.</p>
*
* @author Edwin Goei, David Brownell
* @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)
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;
}
}
Class driverClass;
if (classLoader == null || internal) {
driverClass = Class.forName(className);
} else {
driverClass = classLoader.loadClass(className);
}
return driverClass.newInstance();
}
}

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,147 @@
/*
* Copyright (c) 2000, 2013, 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. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* 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.
*/
// SAX parser factory.
// http://www.saxproject.org
// No warranty; no copyright -- use this as you will.
// $Id: ParserFactory.java,v 1.2 2004/11/03 22:53:09 jsuttor Exp $
package org.xml.sax.helpers;
import org.xml.sax.Parser;
/**
* Java-specific class for dynamically loading SAX parsers.
*
* <blockquote>
* <em>This module, both source code and documentation, is in the
* Public Domain, and comes with <strong>NO WARRANTY</strong>.</em>
* See <a href='http://www.saxproject.org'>http://www.saxproject.org</a>
* for further information.
* </blockquote>
*
* <p><strong>Note:</strong> This class is designed to work with the now-deprecated
* SAX1 {@link org.xml.sax.Parser Parser} class. SAX2 applications should use
* {@link org.xml.sax.helpers.XMLReaderFactory XMLReaderFactory} instead.</p>
*
* <p>ParserFactory is not part of the platform-independent definition
* of SAX; it is an additional convenience class designed
* specifically for Java XML application writers. SAX applications
* can use the static methods in this class to allocate a SAX parser
* dynamically at run-time based either on the value of the
* `org.xml.sax.parser' system property or on a string containing the class
* name.</p>
*
* <p>Note that the application still requires an XML parser that
* implements SAX1.</p>
*
* @deprecated This class works with the deprecated
* {@link org.xml.sax.Parser Parser}
* interface.
* @since SAX 1.0
* @author David Megginson
* @version 2.0.1 (sax2r2)
*/
public class ParserFactory {
private static SecuritySupport ss = new SecuritySupport();
/**
* Private null constructor.
*/
private ParserFactory ()
{
}
/**
* Create a new SAX parser using the `org.xml.sax.parser' system property.
*
* <p>The named class must exist and must implement the
* {@link org.xml.sax.Parser Parser} interface.</p>
*
* @exception java.lang.NullPointerException There is no value
* for the `org.xml.sax.parser' system property.
* @exception java.lang.ClassNotFoundException The SAX parser
* class was not found (check your CLASSPATH).
* @exception IllegalAccessException The SAX parser class was
* found, but you do not have permission to load
* it.
* @exception InstantiationException The SAX parser class was
* found but could not be instantiated.
* @exception java.lang.ClassCastException The SAX parser class
* was found and instantiated, but does not implement
* org.xml.sax.Parser.
* @see #makeParser(java.lang.String)
* @see org.xml.sax.Parser
*/
public static Parser makeParser ()
throws ClassNotFoundException,
IllegalAccessException,
InstantiationException,
NullPointerException,
ClassCastException
{
String className = ss.getSystemProperty("org.xml.sax.parser");
if (className == null) {
throw new NullPointerException("No value for sax.parser property");
} else {
return makeParser(className);
}
}
/**
* Create a new SAX parser object using the class name provided.
*
* <p>The named class must exist and must implement the
* {@link org.xml.sax.Parser Parser} interface.</p>
*
* @param className A string containing the name of the
* SAX parser class.
* @exception java.lang.ClassNotFoundException The SAX parser
* class was not found (check your CLASSPATH).
* @exception IllegalAccessException The SAX parser class was
* found, but you do not have permission to load
* it.
* @exception InstantiationException The SAX parser class was
* found but could not be instantiated.
* @exception java.lang.ClassCastException The SAX parser class
* was found and instantiated, but does not implement
* org.xml.sax.Parser.
* @see #makeParser()
* @see org.xml.sax.Parser
*/
public static Parser makeParser (String className)
throws ClassNotFoundException,
IllegalAccessException,
InstantiationException,
ClassCastException
{
return (Parser) NewInstance.newInstance (
ss.getContextClassLoader(), className);
}
}

View File

@@ -0,0 +1,108 @@
/*
* Copyright (c) 2004, 2013, 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. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* 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 org.xml.sax.helpers;
import java.io.*;
import java.security.*;
/**
* This class is duplicated for each JAXP subpackage so keep it in sync.
* It is package private and therefore is not exposed as part of the JAXP
* API.
*
* Security related methods that only work on J2SE 1.2 and newer.
*/
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;
}
});
}
String getSystemProperty(final String propName) {
return (String)
AccessController.doPrivileged(new PrivilegedAction() {
public Object run() {
return 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);
}
});
} catch (PrivilegedActionException e) {
throw (FileNotFoundException)e.getException();
}
}
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;
}
});
}
boolean doesFileExist(final File f) {
return ((Boolean)
AccessController.doPrivileged(new PrivilegedAction() {
public Object run() {
return new Boolean(f.exists());
}
})).booleanValue();
}
}

View File

@@ -0,0 +1,737 @@
/*
* Copyright (c) 2000, 2005, 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. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* 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.
*/
// XMLFilterImpl.java - base SAX2 filter implementation.
// http://www.saxproject.org
// Written by David Megginson
// NO WARRANTY! This class is in the Public Domain.
// $Id: XMLFilterImpl.java,v 1.3 2004/11/03 22:53:09 jsuttor Exp $
package org.xml.sax.helpers;
import java.io.IOException;
import org.xml.sax.XMLReader;
import org.xml.sax.XMLFilter;
import org.xml.sax.InputSource;
import org.xml.sax.Locator;
import org.xml.sax.Attributes;
import org.xml.sax.EntityResolver;
import org.xml.sax.DTDHandler;
import org.xml.sax.ContentHandler;
import org.xml.sax.ErrorHandler;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
import org.xml.sax.SAXNotSupportedException;
import org.xml.sax.SAXNotRecognizedException;
/**
* Base class for deriving an XML filter.
*
* <blockquote>
* <em>This module, both source code and documentation, is in the
* Public Domain, and comes with <strong>NO WARRANTY</strong>.</em>
* See <a href='http://www.saxproject.org'>http://www.saxproject.org</a>
* for further information.
* </blockquote>
*
* <p>This class is designed to sit between an {@link org.xml.sax.XMLReader
* XMLReader} and the client application's event handlers. By default, it
* does nothing but pass requests up to the reader and events
* on to the handlers unmodified, but subclasses can override
* specific methods to modify the event stream or the configuration
* requests as they pass through.</p>
*
* @since SAX 2.0
* @author David Megginson
* @see org.xml.sax.XMLFilter
* @see org.xml.sax.XMLReader
* @see org.xml.sax.EntityResolver
* @see org.xml.sax.DTDHandler
* @see org.xml.sax.ContentHandler
* @see org.xml.sax.ErrorHandler
*/
public class XMLFilterImpl
implements XMLFilter, EntityResolver, DTDHandler, ContentHandler, ErrorHandler
{
////////////////////////////////////////////////////////////////////
// Constructors.
////////////////////////////////////////////////////////////////////
/**
* Construct an empty XML filter, with no parent.
*
* <p>This filter will have no parent: you must assign a parent
* before you start a parse or do any configuration with
* setFeature or setProperty, unless you use this as a pure event
* consumer rather than as an {@link XMLReader}.</p>
*
* @see org.xml.sax.XMLReader#setFeature
* @see org.xml.sax.XMLReader#setProperty
* @see #setParent
*/
public XMLFilterImpl ()
{
super();
}
/**
* Construct an XML filter with the specified parent.
*
* @see #setParent
* @see #getParent
*/
public XMLFilterImpl (XMLReader parent)
{
super();
setParent(parent);
}
////////////////////////////////////////////////////////////////////
// Implementation of org.xml.sax.XMLFilter.
////////////////////////////////////////////////////////////////////
/**
* Set the parent reader.
*
* <p>This is the {@link org.xml.sax.XMLReader XMLReader} from which
* this filter will obtain its events and to which it will pass its
* configuration requests. The parent may itself be another filter.</p>
*
* <p>If there is no parent reader set, any attempt to parse
* or to set or get a feature or property will fail.</p>
*
* @param parent The parent XML reader.
* @see #getParent
*/
public void setParent (XMLReader parent)
{
this.parent = parent;
}
/**
* Get the parent reader.
*
* @return The parent XML reader, or null if none is set.
* @see #setParent
*/
public XMLReader getParent ()
{
return parent;
}
////////////////////////////////////////////////////////////////////
// Implementation of org.xml.sax.XMLReader.
////////////////////////////////////////////////////////////////////
/**
* Set the value of a feature.
*
* <p>This will always fail if the parent is null.</p>
*
* @param name The feature name.
* @param value The requested feature value.
* @exception org.xml.sax.SAXNotRecognizedException If the feature
* value can't be assigned or retrieved from the parent.
* @exception org.xml.sax.SAXNotSupportedException When the
* parent recognizes the feature name but
* cannot set the requested value.
*/
public void setFeature (String name, boolean value)
throws SAXNotRecognizedException, SAXNotSupportedException
{
if (parent != null) {
parent.setFeature(name, value);
} else {
throw new SAXNotRecognizedException("Feature: " + name);
}
}
/**
* Look up the value of a feature.
*
* <p>This will always fail if the parent is null.</p>
*
* @param name The feature name.
* @return The current value of the feature.
* @exception org.xml.sax.SAXNotRecognizedException If the feature
* value can't be assigned or retrieved from the parent.
* @exception org.xml.sax.SAXNotSupportedException When the
* parent recognizes the feature name but
* cannot determine its value at this time.
*/
public boolean getFeature (String name)
throws SAXNotRecognizedException, SAXNotSupportedException
{
if (parent != null) {
return parent.getFeature(name);
} else {
throw new SAXNotRecognizedException("Feature: " + name);
}
}
/**
* Set the value of a property.
*
* <p>This will always fail if the parent is null.</p>
*
* @param name The property name.
* @param value The requested property value.
* @exception org.xml.sax.SAXNotRecognizedException If the property
* value can't be assigned or retrieved from the parent.
* @exception org.xml.sax.SAXNotSupportedException When the
* parent recognizes the property name but
* cannot set the requested value.
*/
public void setProperty (String name, Object value)
throws SAXNotRecognizedException, SAXNotSupportedException
{
if (parent != null) {
parent.setProperty(name, value);
} else {
throw new SAXNotRecognizedException("Property: " + name);
}
}
/**
* Look up the value of a property.
*
* @param name The property name.
* @return The current value of the property.
* @exception org.xml.sax.SAXNotRecognizedException If the property
* value can't be assigned or retrieved from the parent.
* @exception org.xml.sax.SAXNotSupportedException When the
* parent recognizes the property name but
* cannot determine its value at this time.
*/
public Object getProperty (String name)
throws SAXNotRecognizedException, SAXNotSupportedException
{
if (parent != null) {
return parent.getProperty(name);
} else {
throw new SAXNotRecognizedException("Property: " + name);
}
}
/**
* Set the entity resolver.
*
* @param resolver The new entity resolver.
*/
public void setEntityResolver (EntityResolver resolver)
{
entityResolver = resolver;
}
/**
* Get the current entity resolver.
*
* @return The current entity resolver, or null if none was set.
*/
public EntityResolver getEntityResolver ()
{
return entityResolver;
}
/**
* Set the DTD event handler.
*
* @param handler the new DTD handler
*/
public void setDTDHandler (DTDHandler handler)
{
dtdHandler = handler;
}
/**
* Get the current DTD event handler.
*
* @return The current DTD handler, or null if none was set.
*/
public DTDHandler getDTDHandler ()
{
return dtdHandler;
}
/**
* Set the content event handler.
*
* @param handler the new content handler
*/
public void setContentHandler (ContentHandler handler)
{
contentHandler = handler;
}
/**
* Get the content event handler.
*
* @return The current content handler, or null if none was set.
*/
public ContentHandler getContentHandler ()
{
return contentHandler;
}
/**
* Set the error event handler.
*
* @param handler the new error handler
*/
public void setErrorHandler (ErrorHandler handler)
{
errorHandler = handler;
}
/**
* Get the current error event handler.
*
* @return The current error handler, or null if none was set.
*/
public ErrorHandler getErrorHandler ()
{
return errorHandler;
}
/**
* Parse a document.
*
* @param input The input source for the document entity.
* @exception org.xml.sax.SAXException Any SAX exception, possibly
* wrapping another exception.
* @exception java.io.IOException An IO exception from the parser,
* possibly from a byte stream or character stream
* supplied by the application.
*/
public void parse (InputSource input)
throws SAXException, IOException
{
setupParse();
parent.parse(input);
}
/**
* Parse a document.
*
* @param systemId The system identifier as a fully-qualified URI.
* @exception org.xml.sax.SAXException Any SAX exception, possibly
* wrapping another exception.
* @exception java.io.IOException An IO exception from the parser,
* possibly from a byte stream or character stream
* supplied by the application.
*/
public void parse (String systemId)
throws SAXException, IOException
{
parse(new InputSource(systemId));
}
////////////////////////////////////////////////////////////////////
// Implementation of org.xml.sax.EntityResolver.
////////////////////////////////////////////////////////////////////
/**
* Filter an external entity resolution.
*
* @param publicId The entity's public identifier, or null.
* @param systemId The entity's system identifier.
* @return A new InputSource or null for the default.
* @exception org.xml.sax.SAXException The client may throw
* an exception during processing.
* @exception java.io.IOException The client may throw an
* I/O-related exception while obtaining the
* new InputSource.
*/
public InputSource resolveEntity (String publicId, String systemId)
throws SAXException, IOException
{
if (entityResolver != null) {
return entityResolver.resolveEntity(publicId, systemId);
} else {
return null;
}
}
////////////////////////////////////////////////////////////////////
// Implementation of org.xml.sax.DTDHandler.
////////////////////////////////////////////////////////////////////
/**
* Filter a notation declaration event.
*
* @param name The notation name.
* @param publicId The notation's public identifier, or null.
* @param systemId The notation's system identifier, or null.
* @exception org.xml.sax.SAXException The client may throw
* an exception during processing.
*/
public void notationDecl (String name, String publicId, String systemId)
throws SAXException
{
if (dtdHandler != null) {
dtdHandler.notationDecl(name, publicId, systemId);
}
}
/**
* Filter an unparsed entity declaration event.
*
* @param name The entity name.
* @param publicId The entity's public identifier, or null.
* @param systemId The entity's system identifier, or null.
* @param notationName The name of the associated notation.
* @exception org.xml.sax.SAXException The client may throw
* an exception during processing.
*/
public void unparsedEntityDecl (String name, String publicId,
String systemId, String notationName)
throws SAXException
{
if (dtdHandler != null) {
dtdHandler.unparsedEntityDecl(name, publicId, systemId,
notationName);
}
}
////////////////////////////////////////////////////////////////////
// Implementation of org.xml.sax.ContentHandler.
////////////////////////////////////////////////////////////////////
/**
* Filter a new document locator event.
*
* @param locator The document locator.
*/
public void setDocumentLocator (Locator locator)
{
this.locator = locator;
if (contentHandler != null) {
contentHandler.setDocumentLocator(locator);
}
}
/**
* Filter a start document event.
*
* @exception org.xml.sax.SAXException The client may throw
* an exception during processing.
*/
public void startDocument ()
throws SAXException
{
if (contentHandler != null) {
contentHandler.startDocument();
}
}
/**
* Filter an end document event.
*
* @exception org.xml.sax.SAXException The client may throw
* an exception during processing.
*/
public void endDocument ()
throws SAXException
{
if (contentHandler != null) {
contentHandler.endDocument();
}
}
/**
* Filter a start Namespace prefix mapping event.
*
* @param prefix The Namespace prefix.
* @param uri The Namespace URI.
* @exception org.xml.sax.SAXException The client may throw
* an exception during processing.
*/
public void startPrefixMapping (String prefix, String uri)
throws SAXException
{
if (contentHandler != null) {
contentHandler.startPrefixMapping(prefix, uri);
}
}
/**
* Filter an end Namespace prefix mapping event.
*
* @param prefix The Namespace prefix.
* @exception org.xml.sax.SAXException The client may throw
* an exception during processing.
*/
public void endPrefixMapping (String prefix)
throws SAXException
{
if (contentHandler != null) {
contentHandler.endPrefixMapping(prefix);
}
}
/**
* Filter a start element event.
*
* @param uri The element's Namespace URI, or the empty string.
* @param localName The element's local name, or the empty string.
* @param qName The element's qualified (prefixed) name, or the empty
* string.
* @param atts The element's attributes.
* @exception org.xml.sax.SAXException The client may throw
* an exception during processing.
*/
public void startElement (String uri, String localName, String qName,
Attributes atts)
throws SAXException
{
if (contentHandler != null) {
contentHandler.startElement(uri, localName, qName, atts);
}
}
/**
* Filter an end element event.
*
* @param uri The element's Namespace URI, or the empty string.
* @param localName The element's local name, or the empty string.
* @param qName The element's qualified (prefixed) name, or the empty
* string.
* @exception org.xml.sax.SAXException The client may throw
* an exception during processing.
*/
public void endElement (String uri, String localName, String qName)
throws SAXException
{
if (contentHandler != null) {
contentHandler.endElement(uri, localName, qName);
}
}
/**
* Filter a character data event.
*
* @param ch An array of characters.
* @param start The starting position in the array.
* @param length The number of characters to use from the array.
* @exception org.xml.sax.SAXException The client may throw
* an exception during processing.
*/
public void characters (char ch[], int start, int length)
throws SAXException
{
if (contentHandler != null) {
contentHandler.characters(ch, start, length);
}
}
/**
* Filter an ignorable whitespace event.
*
* @param ch An array of characters.
* @param start The starting position in the array.
* @param length The number of characters to use from the array.
* @exception org.xml.sax.SAXException The client may throw
* an exception during processing.
*/
public void ignorableWhitespace (char ch[], int start, int length)
throws SAXException
{
if (contentHandler != null) {
contentHandler.ignorableWhitespace(ch, start, length);
}
}
/**
* Filter a processing instruction event.
*
* @param target The processing instruction target.
* @param data The text following the target.
* @exception org.xml.sax.SAXException The client may throw
* an exception during processing.
*/
public void processingInstruction (String target, String data)
throws SAXException
{
if (contentHandler != null) {
contentHandler.processingInstruction(target, data);
}
}
/**
* Filter a skipped entity event.
*
* @param name The name of the skipped entity.
* @exception org.xml.sax.SAXException The client may throw
* an exception during processing.
*/
public void skippedEntity (String name)
throws SAXException
{
if (contentHandler != null) {
contentHandler.skippedEntity(name);
}
}
////////////////////////////////////////////////////////////////////
// Implementation of org.xml.sax.ErrorHandler.
////////////////////////////////////////////////////////////////////
/**
* Filter a warning event.
*
* @param e The warning as an exception.
* @exception org.xml.sax.SAXException The client may throw
* an exception during processing.
*/
public void warning (SAXParseException e)
throws SAXException
{
if (errorHandler != null) {
errorHandler.warning(e);
}
}
/**
* Filter an error event.
*
* @param e The error as an exception.
* @exception org.xml.sax.SAXException The client may throw
* an exception during processing.
*/
public void error (SAXParseException e)
throws SAXException
{
if (errorHandler != null) {
errorHandler.error(e);
}
}
/**
* Filter a fatal error event.
*
* @param e The error as an exception.
* @exception org.xml.sax.SAXException The client may throw
* an exception during processing.
*/
public void fatalError (SAXParseException e)
throws SAXException
{
if (errorHandler != null) {
errorHandler.fatalError(e);
}
}
////////////////////////////////////////////////////////////////////
// Internal methods.
////////////////////////////////////////////////////////////////////
/**
* Set up before a parse.
*
* <p>Before every parse, check whether the parent is
* non-null, and re-register the filter for all of the
* events.</p>
*/
private void setupParse ()
{
if (parent == null) {
throw new NullPointerException("No parent for filter");
}
parent.setEntityResolver(this);
parent.setDTDHandler(this);
parent.setContentHandler(this);
parent.setErrorHandler(this);
}
////////////////////////////////////////////////////////////////////
// Internal state.
////////////////////////////////////////////////////////////////////
private XMLReader parent = null;
private Locator locator = null;
private EntityResolver entityResolver = null;
private DTDHandler dtdHandler = null;
private ContentHandler contentHandler = null;
private ErrorHandler errorHandler = null;
}
// end of XMLFilterImpl.java

View File

@@ -0,0 +1,562 @@
/*
* Copyright (c) 2000, 2005, 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. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* 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.
*/
// XMLReaderAdapter.java - adapt an SAX2 XMLReader to a SAX1 Parser
// http://www.saxproject.org
// Written by David Megginson
// NO WARRANTY! This class is in the public domain.
// $Id: XMLReaderAdapter.java,v 1.3 2004/11/03 22:53:09 jsuttor Exp $
package org.xml.sax.helpers;
import java.io.IOException;
import java.util.Locale;
import org.xml.sax.Parser; // deprecated
import org.xml.sax.Locator;
import org.xml.sax.InputSource;
import org.xml.sax.AttributeList; // deprecated
import org.xml.sax.EntityResolver;
import org.xml.sax.DTDHandler;
import org.xml.sax.DocumentHandler; // deprecated
import org.xml.sax.ErrorHandler;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xml.sax.Attributes;
import org.xml.sax.ContentHandler;
import org.xml.sax.SAXNotSupportedException;
/**
* Adapt a SAX2 XMLReader as a SAX1 Parser.
*
* <blockquote>
* <em>This module, both source code and documentation, is in the
* Public Domain, and comes with <strong>NO WARRANTY</strong>.</em>
* See <a href='http://www.saxproject.org'>http://www.saxproject.org</a>
* for further information.
* </blockquote>
*
* <p>This class wraps a SAX2 {@link org.xml.sax.XMLReader XMLReader}
* and makes it act as a SAX1 {@link org.xml.sax.Parser Parser}. The XMLReader
* must support a true value for the
* http://xml.org/sax/features/namespace-prefixes property or parsing will fail
* with a {@link org.xml.sax.SAXException SAXException}; if the XMLReader
* supports a false value for the http://xml.org/sax/features/namespaces
* property, that will also be used to improve efficiency.</p>
*
* @since SAX 2.0
* @author David Megginson
* @see org.xml.sax.Parser
* @see org.xml.sax.XMLReader
*/
public class XMLReaderAdapter implements Parser, ContentHandler
{
////////////////////////////////////////////////////////////////////
// Constructor.
////////////////////////////////////////////////////////////////////
/**
* Create a new adapter.
*
* <p>Use the "org.xml.sax.driver" property to locate the SAX2
* driver to embed.</p>
*
* @exception org.xml.sax.SAXException If the embedded driver
* cannot be instantiated or if the
* org.xml.sax.driver property is not specified.
*/
public XMLReaderAdapter ()
throws SAXException
{
setup(XMLReaderFactory.createXMLReader());
}
/**
* Create a new adapter.
*
* <p>Create a new adapter, wrapped around a SAX2 XMLReader.
* The adapter will make the XMLReader act like a SAX1
* Parser.</p>
*
* @param xmlReader The SAX2 XMLReader to wrap.
* @exception java.lang.NullPointerException If the argument is null.
*/
public XMLReaderAdapter (XMLReader xmlReader)
{
setup(xmlReader);
}
/**
* Internal setup.
*
* @param xmlReader The embedded XMLReader.
*/
private void setup (XMLReader xmlReader)
{
if (xmlReader == null) {
throw new NullPointerException("XMLReader must not be null");
}
this.xmlReader = xmlReader;
qAtts = new AttributesAdapter();
}
////////////////////////////////////////////////////////////////////
// Implementation of org.xml.sax.Parser.
////////////////////////////////////////////////////////////////////
/**
* Set the locale for error reporting.
*
* <p>This is not supported in SAX2, and will always throw
* an exception.</p>
*
* @param locale the locale for error reporting.
* @see org.xml.sax.Parser#setLocale
* @exception org.xml.sax.SAXException Thrown unless overridden.
*/
public void setLocale (Locale locale)
throws SAXException
{
throw new SAXNotSupportedException("setLocale not supported");
}
/**
* Register the entity resolver.
*
* @param resolver The new resolver.
* @see org.xml.sax.Parser#setEntityResolver
*/
public void setEntityResolver (EntityResolver resolver)
{
xmlReader.setEntityResolver(resolver);
}
/**
* Register the DTD event handler.
*
* @param handler The new DTD event handler.
* @see org.xml.sax.Parser#setDTDHandler
*/
public void setDTDHandler (DTDHandler handler)
{
xmlReader.setDTDHandler(handler);
}
/**
* Register the SAX1 document event handler.
*
* <p>Note that the SAX1 document handler has no Namespace
* support.</p>
*
* @param handler The new SAX1 document event handler.
* @see org.xml.sax.Parser#setDocumentHandler
*/
public void setDocumentHandler (DocumentHandler handler)
{
documentHandler = handler;
}
/**
* Register the error event handler.
*
* @param handler The new error event handler.
* @see org.xml.sax.Parser#setErrorHandler
*/
public void setErrorHandler (ErrorHandler handler)
{
xmlReader.setErrorHandler(handler);
}
/**
* Parse the document.
*
* <p>This method will throw an exception if the embedded
* XMLReader does not support the
* http://xml.org/sax/features/namespace-prefixes property.</p>
*
* @param systemId The absolute URL of the document.
* @exception java.io.IOException If there is a problem reading
* the raw content of the document.
* @exception org.xml.sax.SAXException If there is a problem
* processing the document.
* @see #parse(org.xml.sax.InputSource)
* @see org.xml.sax.Parser#parse(java.lang.String)
*/
public void parse (String systemId)
throws IOException, SAXException
{
parse(new InputSource(systemId));
}
/**
* Parse the document.
*
* <p>This method will throw an exception if the embedded
* XMLReader does not support the
* http://xml.org/sax/features/namespace-prefixes property.</p>
*
* @param input An input source for the document.
* @exception java.io.IOException If there is a problem reading
* the raw content of the document.
* @exception org.xml.sax.SAXException If there is a problem
* processing the document.
* @see #parse(java.lang.String)
* @see org.xml.sax.Parser#parse(org.xml.sax.InputSource)
*/
public void parse (InputSource input)
throws IOException, SAXException
{
setupXMLReader();
xmlReader.parse(input);
}
/**
* Set up the XML reader.
*/
private void setupXMLReader ()
throws SAXException
{
xmlReader.setFeature("http://xml.org/sax/features/namespace-prefixes", true);
try {
xmlReader.setFeature("http://xml.org/sax/features/namespaces",
false);
} catch (SAXException e) {
// NO OP: it's just extra information, and we can ignore it
}
xmlReader.setContentHandler(this);
}
////////////////////////////////////////////////////////////////////
// Implementation of org.xml.sax.ContentHandler.
////////////////////////////////////////////////////////////////////
/**
* Set a document locator.
*
* @param locator The document locator.
* @see org.xml.sax.ContentHandler#setDocumentLocator
*/
public void setDocumentLocator (Locator locator)
{
if (documentHandler != null)
documentHandler.setDocumentLocator(locator);
}
/**
* Start document event.
*
* @exception org.xml.sax.SAXException The client may raise a
* processing exception.
* @see org.xml.sax.ContentHandler#startDocument
*/
public void startDocument ()
throws SAXException
{
if (documentHandler != null)
documentHandler.startDocument();
}
/**
* End document event.
*
* @exception org.xml.sax.SAXException The client may raise a
* processing exception.
* @see org.xml.sax.ContentHandler#endDocument
*/
public void endDocument ()
throws SAXException
{
if (documentHandler != null)
documentHandler.endDocument();
}
/**
* Adapt a SAX2 start prefix mapping event.
*
* @param prefix The prefix being mapped.
* @param uri The Namespace URI being mapped to.
* @see org.xml.sax.ContentHandler#startPrefixMapping
*/
public void startPrefixMapping (String prefix, String uri)
{
}
/**
* Adapt a SAX2 end prefix mapping event.
*
* @param prefix The prefix being mapped.
* @see org.xml.sax.ContentHandler#endPrefixMapping
*/
public void endPrefixMapping (String prefix)
{
}
/**
* Adapt a SAX2 start element event.
*
* @param uri The Namespace URI.
* @param localName The Namespace local name.
* @param qName The qualified (prefixed) name.
* @param atts The SAX2 attributes.
* @exception org.xml.sax.SAXException The client may raise a
* processing exception.
* @see org.xml.sax.ContentHandler#endDocument
*/
public void startElement (String uri, String localName,
String qName, Attributes atts)
throws SAXException
{
if (documentHandler != null) {
qAtts.setAttributes(atts);
documentHandler.startElement(qName, qAtts);
}
}
/**
* Adapt a SAX2 end element event.
*
* @param uri The Namespace URI.
* @param localName The Namespace local name.
* @param qName The qualified (prefixed) name.
* @exception org.xml.sax.SAXException The client may raise a
* processing exception.
* @see org.xml.sax.ContentHandler#endElement
*/
public void endElement (String uri, String localName,
String qName)
throws SAXException
{
if (documentHandler != null)
documentHandler.endElement(qName);
}
/**
* Adapt a SAX2 characters event.
*
* @param ch An array of characters.
* @param start The starting position in the array.
* @param length The number of characters to use.
* @exception org.xml.sax.SAXException The client may raise a
* processing exception.
* @see org.xml.sax.ContentHandler#characters
*/
public void characters (char ch[], int start, int length)
throws SAXException
{
if (documentHandler != null)
documentHandler.characters(ch, start, length);
}
/**
* Adapt a SAX2 ignorable whitespace event.
*
* @param ch An array of characters.
* @param start The starting position in the array.
* @param length The number of characters to use.
* @exception org.xml.sax.SAXException The client may raise a
* processing exception.
* @see org.xml.sax.ContentHandler#ignorableWhitespace
*/
public void ignorableWhitespace (char ch[], int start, int length)
throws SAXException
{
if (documentHandler != null)
documentHandler.ignorableWhitespace(ch, start, length);
}
/**
* Adapt a SAX2 processing instruction event.
*
* @param target The processing instruction target.
* @param data The remainder of the processing instruction
* @exception org.xml.sax.SAXException The client may raise a
* processing exception.
* @see org.xml.sax.ContentHandler#processingInstruction
*/
public void processingInstruction (String target, String data)
throws SAXException
{
if (documentHandler != null)
documentHandler.processingInstruction(target, data);
}
/**
* Adapt a SAX2 skipped entity event.
*
* @param name The name of the skipped entity.
* @see org.xml.sax.ContentHandler#skippedEntity
* @exception org.xml.sax.SAXException Throwable by subclasses.
*/
public void skippedEntity (String name)
throws SAXException
{
}
////////////////////////////////////////////////////////////////////
// Internal state.
////////////////////////////////////////////////////////////////////
XMLReader xmlReader;
DocumentHandler documentHandler;
AttributesAdapter qAtts;
////////////////////////////////////////////////////////////////////
// Internal class.
////////////////////////////////////////////////////////////////////
/**
* Internal class to wrap a SAX2 Attributes object for SAX1.
*/
final class AttributesAdapter implements AttributeList
{
AttributesAdapter ()
{
}
/**
* Set the embedded Attributes object.
*
* @param The embedded SAX2 Attributes.
*/
void setAttributes (Attributes attributes)
{
this.attributes = attributes;
}
/**
* Return the number of attributes.
*
* @return The length of the attribute list.
* @see org.xml.sax.AttributeList#getLength
*/
public int getLength ()
{
return attributes.getLength();
}
/**
* Return the qualified (prefixed) name of an attribute by position.
*
* @return The qualified name.
* @see org.xml.sax.AttributeList#getName
*/
public String getName (int i)
{
return attributes.getQName(i);
}
/**
* Return the type of an attribute by position.
*
* @return The type.
* @see org.xml.sax.AttributeList#getType(int)
*/
public String getType (int i)
{
return attributes.getType(i);
}
/**
* Return the value of an attribute by position.
*
* @return The value.
* @see org.xml.sax.AttributeList#getValue(int)
*/
public String getValue (int i)
{
return attributes.getValue(i);
}
/**
* Return the type of an attribute by qualified (prefixed) name.
*
* @return The type.
* @see org.xml.sax.AttributeList#getType(java.lang.String)
*/
public String getType (String qName)
{
return attributes.getType(qName);
}
/**
* Return the value of an attribute by qualified (prefixed) name.
*
* @return The value.
* @see org.xml.sax.AttributeList#getValue(java.lang.String)
*/
public String getValue (String qName)
{
return attributes.getValue(qName);
}
private Attributes attributes;
}
}
// end of XMLReaderAdapter.java

View File

@@ -0,0 +1,244 @@
/*
* Copyright (c) 2000, 2013, 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. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* 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.
*/
// XMLReaderFactory.java - factory for creating a new reader.
// http://www.saxproject.org
// Written by David Megginson
// and by David Brownell
// NO WARRANTY! This class is in the Public Domain.
// $Id: XMLReaderFactory.java,v 1.2.2.1 2005/07/31 22:48:08 jeffsuttor Exp $
package org.xml.sax.helpers;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import org.xml.sax.XMLReader;
import org.xml.sax.SAXException;
/**
* Factory for creating an XML reader.
*
* <blockquote>
* <em>This module, both source code and documentation, is in the
* Public Domain, and comes with <strong>NO WARRANTY</strong>.</em>
* See <a href='http://www.saxproject.org'>http://www.saxproject.org</a>
* for further information.
* </blockquote>
*
* <p>This class contains static methods for creating an XML reader
* from an explicit class name, or based on runtime defaults:</p>
*
* <pre>
* try {
* XMLReader myReader = XMLReaderFactory.createXMLReader();
* } catch (SAXException e) {
* System.err.println(e.getMessage());
* }
* </pre>
*
* <p><strong>Note to Distributions bundled with parsers:</strong>
* You should modify the implementation of the no-arguments
* <em>createXMLReader</em> to handle cases where the external
* configuration mechanisms aren't set up. That method should do its
* best to return a parser when one is in the class path, even when
* nothing bound its class name to <code>org.xml.sax.driver</code> so
* those configuration mechanisms would see it.</p>
*
* @since SAX 2.0
* @author David Megginson, David Brownell
* @version 2.0.1 (sax2r2)
*/
final public class XMLReaderFactory
{
/**
* Private constructor.
*
* <p>This constructor prevents the class from being instantiated.</p>
*/
private XMLReaderFactory ()
{
}
private static final String property = "org.xml.sax.driver";
private static 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:</p> <ul>
*
* <li>If the system property <code>org.xml.sax.driver</code>
* has a value, that is used as an XMLReader class name. </li>
*
* <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>
*
* <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</code> 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>
*
* <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.
* </p>
*
* @return A new XMLReader.
* @exception org.xml.sax.SAXException If no default XMLReader class
* can be identified and instantiated.
* @see #createXMLReader(java.lang.String)
*/
public static XMLReader createXMLReader ()
throws SAXException
{
String className = null;
ClassLoader cl = ss.getContextClassLoader();
// 1. try the JVM-instance-wide system property
try {
className = ss.getSystemProperty(property);
}
catch (RuntimeException e) { /* continue searching */ }
// 2. if that fails, try META-INF/services/
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) {
}
}
className = _clsFromJar;
}
// 3. Distro-specific fallback
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
}
// 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?");
}
}
/**
* Attempt to create an XML reader from a class name.
*
* <p>Given a class name, this method attempts to load
* and instantiate the class as an XML reader.</p>
*
* <p>Note that this method will not be usable in environments where
* the caller (perhaps an applet) is not permitted to load classes
* dynamically.</p>
*
* @return A new XML reader.
* @exception org.xml.sax.SAXException If the class cannot be
* loaded, instantiated, and cast to XMLReader.
* @see #createXMLReader()
*/
public static XMLReader createXMLReader (String className)
throws SAXException
{
return loadClass (ss.getContextClassLoader(), className);
}
private static XMLReader loadClass (ClassLoader loader, String className)
throws SAXException
{
try {
return (XMLReader) NewInstance.newInstance (loader, className);
} catch (ClassNotFoundException e1) {
throw new SAXException("SAX2 driver class " + className +
" not found", e1);
} catch (IllegalAccessException e2) {
throw new SAXException("SAX2 driver class " + className +
" found but cannot be loaded", e2);
} catch (InstantiationException e3) {
throw new SAXException("SAX2 driver class " + className +
" loaded but cannot be instantiated (no empty public constructor?)",
e3);
} catch (ClassCastException e4) {
throw new SAXException("SAX2 driver class " + className +
" does not implement XMLReader", e4);
}
}
}