Commit 8d06274e by Tom Tromey

[multiple changes]

2005-03-26  Chris Burdess  <dog@gnu.org>

	* gnu/xml/dom/DomNode.java (notifyNode): grow listener array as
	required.

2005-03-13  Michael Koch  <konqueror@gmx.de>

	* gnu/xml/aelfred2/XmlParser.java: Fixed typo.

2005-03-11  Chris Burdess  <dog@gnu.org>

	* gnu/xml/aelfred2/SAXDriver.java: Corrected bug handling URI
	warnings.

2005-02-27  Chris Burdess  <dog@gnu.org>

	* gnu/xml/aelfred2/JAXPFactory.java,
	gnu/xml/aelfred2/SAXDriver.java,
	gnu/xml/aelfred2/XmlParser.java,
	gnu/xml/aelfred2/XmlReader.java: Applied GNU Classpath source code
	formatting conventions. Replaced arrays of Object with struct-like
	classes for easier maintainability. Made SAXDriver.stringInterning
	package private to allow access from XmlParser inside the loop without
	a method call overhead.

2005-03-11  Chris Burdess  <dog@gnu.org>

	* gnu/xml/aelfred2/SAXDriver.java: Corrected bug handling URI
	warnings.

From-SVN: r97200
parent 1fff69c9
2005-03-26 Chris Burdess <dog@gnu.org>
* gnu/xml/dom/DomNode.java (notifyNode): grow listener array as
required.
2005-03-13 Michael Koch <konqueror@gmx.de>
* gnu/xml/aelfred2/XmlParser.java: Fixed typo.
2005-03-11 Chris Burdess <dog@gnu.org>
* gnu/xml/aelfred2/SAXDriver.java: Corrected bug handling URI
warnings.
2005-02-27 Chris Burdess <dog@gnu.org>
* gnu/xml/aelfred2/JAXPFactory.java,
gnu/xml/aelfred2/SAXDriver.java,
gnu/xml/aelfred2/XmlParser.java,
gnu/xml/aelfred2/XmlReader.java: Applied GNU Classpath source code
formatting conventions. Replaced arrays of Object with struct-like
classes for easier maintainability. Made SAXDriver.stringInterning
package private to allow access from XmlParser inside the loop without
a method call overhead.
2005-03-11 Chris Burdess <dog@gnu.org>
* gnu/xml/aelfred2/SAXDriver.java: Corrected bug handling URI
warnings.
2005-03-28 Alexandre Oliva <aoliva@redhat.com>
* configure.ac: Revert 2005-03-25's patch. Propagate MULTIlib
......
......@@ -60,35 +60,38 @@ import javax.xml.parsers.SAXParserFactory;
*
* @author David Brownell
*/
public final class JAXPFactory extends SAXParserFactory
public final class JAXPFactory
extends SAXParserFactory
{
private Hashtable flags = new Hashtable ();
private Hashtable flags = new Hashtable();
/**
* Constructs a factory which normally returns a non-validating
* parser.
*/
public JAXPFactory () { }
public JAXPFactory()
{
}
public SAXParser newSAXParser ()
public SAXParser newSAXParser()
throws ParserConfigurationException, SAXException
{
JaxpParser jaxp = new JaxpParser ();
Enumeration e = flags.keys ();
XMLReader parser = jaxp.getXMLReader ();
parser.setFeature (
SAXDriver.FEATURE + "namespaces",
isNamespaceAware ());
parser.setFeature (
SAXDriver.FEATURE + "validation",
isValidating ());
JaxpParser jaxp = new JaxpParser();
Enumeration e = flags.keys();
XMLReader parser = jaxp.getXMLReader();
parser.setFeature(SAXDriver.FEATURE + "namespaces",
isNamespaceAware());
parser.setFeature(SAXDriver.FEATURE + "validation",
isValidating());
// that makes SAX2 feature flags trump JAXP
while (e.hasMoreElements ()) {
String uri = (String) e.nextElement ();
Boolean value = (Boolean) flags.get (uri);
parser.setFeature (uri, value.booleanValue ());
while (e.hasMoreElements())
{
String uri = (String) e.nextElement();
Boolean value = (Boolean) flags.get(uri);
parser.setFeature(uri, value.booleanValue());
}
return jaxp;
......@@ -96,101 +99,133 @@ public final class JAXPFactory extends SAXParserFactory
// yes, this "feature transfer" mechanism doesn't play well
public void setFeature (String name, boolean value)
throws
ParserConfigurationException,
SAXNotRecognizedException,
public void setFeature(String name, boolean value)
throws ParserConfigurationException, SAXNotRecognizedException,
SAXNotSupportedException
{
try {
try
{
// force "early" detection of errors where possible
// (flags can't necessarily be set before parsing)
new JaxpParser ().getXMLReader ().setFeature (name, value);
flags.put (name, new Boolean (value));
} catch (SAXNotRecognizedException e) {
throw new SAXNotRecognizedException (name);
} catch (SAXNotSupportedException e) {
throw new SAXNotSupportedException (name);
} catch (Exception e) {
throw new ParserConfigurationException (
e.getClass ().getName ()
new JaxpParser().getXMLReader().setFeature(name, value);
flags.put(name, new Boolean(value));
}
catch (SAXNotRecognizedException e)
{
throw new SAXNotRecognizedException(name);
}
catch (SAXNotSupportedException e)
{
throw new SAXNotSupportedException(name);
}
catch (Exception e)
{
throw new ParserConfigurationException(e.getClass().getName()
+ ": "
+ e.getMessage ());
+ e.getMessage());
}
}
public boolean getFeature (String name)
throws
ParserConfigurationException,
SAXNotRecognizedException,
public boolean getFeature(String name)
throws ParserConfigurationException, SAXNotRecognizedException,
SAXNotSupportedException
{
Boolean value = (Boolean) flags.get (name);
Boolean value = (Boolean) flags.get(name);
if (value != null)
return value.booleanValue ();
{
return value.booleanValue();
}
else
try {
return new JaxpParser ().getXMLReader ().getFeature (name);
} catch (SAXNotRecognizedException e) {
throw new SAXNotRecognizedException (name);
} catch (SAXNotSupportedException e) {
throw new SAXNotSupportedException (name);
} catch (SAXException e) {
throw new ParserConfigurationException (
e.getClass ().getName ()
{
try
{
return new JaxpParser().getXMLReader().getFeature(name);
}
catch (SAXNotRecognizedException e)
{
throw new SAXNotRecognizedException(name);
}
catch (SAXNotSupportedException e)
{
throw new SAXNotSupportedException(name);
}
catch (SAXException e)
{
throw new ParserConfigurationException(e.getClass().getName()
+ ": "
+ e.getMessage ());
+ e.getMessage());
}
}
}
private static class JaxpParser extends SAXParser
private static class JaxpParser
extends SAXParser
{
private XmlReader ae2 = new XmlReader ();
private XmlReader ae2 = new XmlReader();
private XMLReaderAdapter parser = null;
JaxpParser () { }
JaxpParser()
{
}
public void setProperty (String id, Object value)
public void setProperty(String id, Object value)
throws SAXNotRecognizedException, SAXNotSupportedException
{ ae2.setProperty (id, value); }
{
ae2.setProperty(id, value);
}
public Object getProperty (String id)
public Object getProperty(String id)
throws SAXNotRecognizedException, SAXNotSupportedException
{ return ae2.getProperty (id); }
{
return ae2.getProperty(id);
}
public Parser getParser ()
public Parser getParser()
throws SAXException
{
if (parser == null)
parser = new XMLReaderAdapter (ae2);
{
parser = new XMLReaderAdapter(ae2);
}
return parser;
}
public XMLReader getXMLReader ()
throws SAXException
{ return ae2; }
{
return ae2;
}
public boolean isNamespaceAware ()
public boolean isNamespaceAware()
{
try
{
return ae2.getFeature(SAXDriver.FEATURE + "namespaces");
}
catch (Exception e)
{
try {
return ae2.getFeature (SAXDriver.FEATURE + "namespaces");
} catch (Exception e) {
throw new Error ();
throw new Error();
}
}
public boolean isValidating ()
public boolean isValidating()
{
try {
return ae2.getFeature (SAXDriver.FEATURE + "validation");
} catch (Exception e) {
throw new Error ();
try
{
return ae2.getFeature(SAXDriver.FEATURE + "validation");
}
catch (Exception e)
{
throw new Error();
}
}
// TODO isXIncludeAware()
}
}
......@@ -60,15 +60,11 @@ import java.net.URL;
import java.util.Locale;
import java.util.Stack;
// maintaining 1.1 compatibility for now ... more portable, PJava, etc
// Iterator, Hashmap and ArrayList ought to be faster
import java.util.ArrayList;
import java.util.Collections;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.List;
import java.util.Vector;
import org.xml.sax.*;
import org.xml.sax.ext.*;
......@@ -135,7 +131,8 @@ import org.xml.sax.helpers.NamespaceSupport;
final public class SAXDriver
implements Locator, Attributes2, XMLReader, Parser, AttributeList
{
private final DefaultHandler2 base = new DefaultHandler2 ();
private final DefaultHandler2 base = new DefaultHandler2();
private XmlParser parser;
private EntityResolver entityResolver = base;
......@@ -158,27 +155,31 @@ final public class SAXDriver
private boolean extPE = true;
private boolean resolveAll = true;
private boolean useResolver2 = true;
private boolean stringInterning = true;
// package private to allow (read-only) access in XmlParser
boolean stringInterning = true;
private int attributeCount;
private boolean attributes;
private String nsTemp [];
private String[] nsTemp;
private NamespaceSupport prefixStack;
//
// Constructor.
//
/** Constructs a SAX Parser. */
public SAXDriver ()
/**
* Constructs a SAX Parser.
*/
public SAXDriver()
{
reset ();
reset();
}
private void reset ()
private void reset()
{
elementName = null;
entityStack = new Stack ();
entityStack = new Stack();
attributesList = Collections.synchronizedList(new ArrayList());
attributeCount = 0;
attributes = false;
......@@ -196,21 +197,21 @@ final public class SAXDriver
* only locales using the English language are supported.
* @param locale The locale for which diagnostics will be generated
*/
public void setLocale (Locale locale)
public void setLocale(Locale locale)
throws SAXException
{
if ("en".equals (locale.getLanguage ()))
return ;
if ("en".equals(locale.getLanguage()))
{
return;
}
throw new SAXException ("AElfred2 only supports English locales.");
}
/**
* <b>SAX2</b>: Returns the object used when resolving external
* entities during parsing (both general and parameter entities).
*/
public EntityResolver getEntityResolver ()
public EntityResolver getEntityResolver()
{
return (entityResolver == base) ? null : entityResolver;
}
......@@ -219,23 +220,28 @@ final public class SAXDriver
* <b>SAX1, SAX2</b>: Set the entity resolver for this parser.
* @param handler The object to receive entity events.
*/
public void setEntityResolver (EntityResolver resolver)
public void setEntityResolver(EntityResolver resolver)
{
if (resolver instanceof EntityResolver2)
{
resolver2 = (EntityResolver2) resolver;
}
else
{
resolver2 = null;
}
if (resolver == null)
{
resolver = base;
}
entityResolver = resolver;
}
/**
* <b>SAX2</b>: Returns the object used to process declarations related
* to notations and unparsed entities.
*/
public DTDHandler getDTDHandler ()
public DTDHandler getDTDHandler()
{
return (dtdHandler == base) ? null : dtdHandler;
}
......@@ -244,10 +250,12 @@ final public class SAXDriver
* <b>SAX1, SAX2</b>: Set the DTD handler for this parser.
* @param handler The object to receive DTD events.
*/
public void setDTDHandler (DTDHandler handler)
public void setDTDHandler(DTDHandler handler)
{
if (handler == null)
{
handler = base;
}
this.dtdHandler = handler;
}
......@@ -264,9 +272,9 @@ final public class SAXDriver
*
* @param handler The object to receive document events.
*/
public void setDocumentHandler (DocumentHandler handler)
public void setDocumentHandler(DocumentHandler handler)
{
contentHandler = new Adapter (handler);
contentHandler = new Adapter(handler);
xmlNames = true;
}
......@@ -274,9 +282,9 @@ final public class SAXDriver
* <b>SAX2</b>: Returns the object used to report the logical
* content of an XML document.
*/
public ContentHandler getContentHandler ()
public ContentHandler getContentHandler()
{
return contentHandler == base ? null : contentHandler;
return (contentHandler == base) ? null : contentHandler;
}
/**
......@@ -285,10 +293,12 @@ final public class SAXDriver
* this content handler will supplant it (but XML 1.0 style name
* reporting may remain enabled).
*/
public void setContentHandler (ContentHandler handler)
public void setContentHandler(ContentHandler handler)
{
if (handler == null)
{
handler = base;
}
contentHandler = handler;
}
......@@ -296,10 +306,12 @@ final public class SAXDriver
* <b>SAX1, SAX2</b>: Set the error handler for this parser.
* @param handler The object to receive error events.
*/
public void setErrorHandler (ErrorHandler handler)
public void setErrorHandler(ErrorHandler handler)
{
if (handler == null)
{
handler = base;
}
this.errorHandler = handler;
}
......@@ -307,9 +319,10 @@ final public class SAXDriver
* <b>SAX2</b>: Returns the object used to receive callbacks for XML
* errors of all levels (fatal, nonfatal, warning); this is never null;
*/
public ErrorHandler getErrorHandler ()
{ return errorHandler == base ? null : errorHandler; }
public ErrorHandler getErrorHandler()
{
return (errorHandler == base) ? null : errorHandler;
}
/**
* <b>SAX1, SAX2</b>: Auxiliary API to parse an XML document, used mostly
......@@ -327,52 +340,65 @@ final public class SAXDriver
* @exception IOException IOExceptions are normally through through
* the parser if there are problems reading the source document.
*/
public void parse (InputSource source)
public void parse(InputSource source)
throws SAXException, IOException
{
synchronized (base) {
parser = new XmlParser ();
synchronized (base)
{
parser = new XmlParser();
if (namespaces)
prefixStack = new NamespaceSupport ();
{
prefixStack = new NamespaceSupport();
}
else if (!xmlNames)
throw new IllegalStateException ();
parser.setHandler (this);
try {
{
throw new IllegalStateException();
}
parser.setHandler(this);
try
{
Reader r = source.getCharacterStream();
InputStream in = source.getByteStream();
parser.doParse (source.getSystemId (),
source.getPublicId (),
parser.doParse(source.getSystemId(),
source.getPublicId(),
r,
in,
source.getEncoding ());
} catch (SAXException e) {
source.getEncoding());
}
catch (SAXException e)
{
throw e;
} catch (IOException e) {
}
catch (IOException e)
{
throw e;
} catch (RuntimeException e) {
}
catch (RuntimeException e)
{
throw e;
} catch (Exception e) {
throw new SAXParseException (e.getMessage (), this, e);
} finally {
contentHandler.endDocument ();
}
catch (Exception e)
{
throw new SAXParseException(e.getMessage(), this, e);
}
finally
{
contentHandler.endDocument();
reset();
}
}
}
/**
* <b>SAX1, SAX2</b>: Preferred API to parse an XML document, using a
* system identifier (URI).
*/
public void parse (String systemId)
public void parse(String systemId)
throws SAXException, IOException
{
parse (new InputSource (systemId));
parse(new InputSource(systemId));
}
//
......@@ -387,63 +413,92 @@ final public class SAXDriver
* @exception SAXNotRecognizedException thrown if the feature flag
* is neither built in, nor yet assigned.
*/
public boolean getFeature (String featureId)
public boolean getFeature(String featureId)
throws SAXNotRecognizedException, SAXNotSupportedException
{
if ((FEATURE + "validation").equals (featureId))
if ((FEATURE + "validation").equals(featureId))
{
return false;
}
// external entities (both types) are optionally included
if ((FEATURE + "external-general-entities").equals (featureId))
if ((FEATURE + "external-general-entities").equals(featureId))
{
return extGE;
if ((FEATURE + "external-parameter-entities") .equals (featureId))
}
if ((FEATURE + "external-parameter-entities").equals(featureId))
{
return extPE;
}
// element/attribute names are as written in document; no mangling
if ((FEATURE + "namespace-prefixes").equals (featureId))
if ((FEATURE + "namespace-prefixes").equals(featureId))
{
return xmlNames;
}
// report element/attribute namespaces?
if ((FEATURE + "namespaces").equals (featureId))
if ((FEATURE + "namespaces").equals(featureId))
{
return namespaces;
}
// all PEs and GEs are reported
if ((FEATURE + "lexical-handler/parameter-entities").equals (featureId))
if ((FEATURE + "lexical-handler/parameter-entities").equals(featureId))
{
return true;
}
// default is true
if ((FEATURE + "string-interning").equals (featureId))
if ((FEATURE + "string-interning").equals(featureId))
{
return stringInterning;
}
// EXTENSIONS 1.1
// always returns isSpecified info
if ((FEATURE + "use-attributes2").equals (featureId))
if ((FEATURE + "use-attributes2").equals(featureId))
{
return true;
}
// meaningful between startDocument/endDocument
if ((FEATURE + "is-standalone").equals (featureId)) {
if ((FEATURE + "is-standalone").equals(featureId))
{
if (parser == null)
throw new SAXNotSupportedException (featureId);
return parser.isStandalone ();
{
throw new SAXNotSupportedException(featureId);
}
return parser.isStandalone();
}
// optionally don't absolutize URIs in declarations
if ((FEATURE + "resolve-dtd-uris").equals (featureId))
if ((FEATURE + "resolve-dtd-uris").equals(featureId))
{
return resolveAll;
}
// optionally use resolver2 interface methods, if possible
if ((FEATURE + "use-entity-resolver2").equals (featureId))
if ((FEATURE + "use-entity-resolver2").equals(featureId))
{
return useResolver2;
}
throw new SAXNotRecognizedException (featureId);
throw new SAXNotRecognizedException(featureId);
}
// package private
DeclHandler getDeclHandler () { return declHandler; }
DeclHandler getDeclHandler()
{
return declHandler;
}
// package private
boolean resolveURIs () { return resolveAll; }
boolean resolveURIs()
{
return resolveAll;
}
/**
* <b>SAX2</b>: Returns the specified property.
......@@ -451,24 +506,28 @@ final public class SAXDriver
* @exception SAXNotRecognizedException thrown if the property value
* is neither built in, nor yet stored.
*/
public Object getProperty (String propertyId)
public Object getProperty(String propertyId)
throws SAXNotRecognizedException
{
if ((PROPERTY + "declaration-handler").equals (propertyId))
return declHandler == base ? null : declHandler;
if ((PROPERTY + "declaration-handler").equals(propertyId))
{
return (declHandler == base) ? null : declHandler;
}
if ((PROPERTY + "lexical-handler").equals (propertyId))
return lexicalHandler == base ? null : lexicalHandler;
if ((PROPERTY + "lexical-handler").equals(propertyId))
{
return (lexicalHandler == base) ? null : lexicalHandler;
}
// unknown properties
throw new SAXNotRecognizedException (propertyId);
throw new SAXNotRecognizedException(propertyId);
}
/**
* <b>SAX2</b>: Sets the state of feature flags in this parser. Some
* built-in feature flags are mutable.
*/
public void setFeature (String featureId, boolean value)
public void setFeature(String featureId, boolean value)
throws SAXNotRecognizedException, SAXNotSupportedException
{
boolean state;
......@@ -477,83 +536,110 @@ final public class SAXDriver
state = getFeature (featureId);
if (state == value)
{
return;
}
if (parser != null)
throw new SAXNotSupportedException ("not while parsing");
{
throw new SAXNotSupportedException("not while parsing");
}
if ((FEATURE + "namespace-prefixes").equals (featureId)) {
if ((FEATURE + "namespace-prefixes").equals(featureId))
{
// in this implementation, this only affects xmlns reporting
xmlNames = value;
// forcibly prevent illegal parser state
if (!xmlNames)
{
namespaces = true;
}
return;
}
if ((FEATURE + "namespaces").equals (featureId)) {
if ((FEATURE + "namespaces").equals(featureId))
{
namespaces = value;
// forcibly prevent illegal parser state
if (!namespaces)
{
xmlNames = true;
}
return;
}
if ((FEATURE + "external-general-entities").equals (featureId)) {
if ((FEATURE + "external-general-entities").equals(featureId))
{
extGE = value;
return;
}
if ((FEATURE + "external-parameter-entities") .equals (featureId)) {
if ((FEATURE + "external-parameter-entities").equals(featureId))
{
extPE = value;
return;
}
if ((FEATURE + "resolve-dtd-uris").equals (featureId)) {
if ((FEATURE + "resolve-dtd-uris").equals(featureId))
{
resolveAll = value;
return;
}
if ((FEATURE + "use-entity-resolver2").equals (featureId)) {
if ((FEATURE + "use-entity-resolver2").equals(featureId))
{
useResolver2 = value;
return;
}
throw new SAXNotRecognizedException (featureId);
throw new SAXNotRecognizedException(featureId);
}
/**
* <b>SAX2</b>: Assigns the specified property. Like SAX1 handlers,
* these may be changed at any time.
*/
public void setProperty (String propertyId, Object value)
public void setProperty(String propertyId, Object value)
throws SAXNotRecognizedException, SAXNotSupportedException
{
// see if the property is recognized
getProperty (propertyId);
getProperty(propertyId);
// Properties with a defined value, we just change it if we can.
if ((PROPERTY + "declaration-handler").equals (propertyId)) {
if ((PROPERTY + "declaration-handler").equals(propertyId))
{
if (value == null)
{
declHandler = base;
else if (! (value instanceof DeclHandler))
throw new SAXNotSupportedException (propertyId);
}
else if (!(value instanceof DeclHandler))
{
throw new SAXNotSupportedException(propertyId);
}
else
{
declHandler = (DeclHandler) value;
}
return ;
}
if ((PROPERTY + "lexical-handler").equals (propertyId)) {
if ((PROPERTY + "lexical-handler").equals(propertyId))
{
if (value == null)
{
lexicalHandler = base;
else if (! (value instanceof LexicalHandler))
throw new SAXNotSupportedException (propertyId);
}
else if (!(value instanceof LexicalHandler))
{
throw new SAXNotSupportedException(propertyId);
}
else
{
lexicalHandler = (LexicalHandler) value;
return ;
}
throw new SAXNotSupportedException (propertyId);
return;
}
throw new SAXNotSupportedException(propertyId);
}
//
// This is where the driver receives XmlParser callbacks and translates
......@@ -561,12 +647,12 @@ final public class SAXDriver
// SAX2 support.
//
void startDocument ()
void startDocument()
throws SAXException
{
contentHandler.setDocumentLocator (this);
contentHandler.startDocument ();
attributesList.clear ();
contentHandler.setDocumentLocator(this);
contentHandler.startDocument();
attributesList.clear();
}
void xmlDecl(String version,
......@@ -584,19 +670,23 @@ final public class SAXDriver
}
}
void skippedEntity (String name)
void skippedEntity(String name)
throws SAXException
{ contentHandler.skippedEntity (name); }
{
contentHandler.skippedEntity(name);
}
InputSource getExternalSubset (String name, String baseURI)
InputSource getExternalSubset(String name, String baseURI)
throws SAXException, IOException
{
if (resolver2 == null || !useResolver2 || !extPE)
{
return null;
return resolver2.getExternalSubset (name, baseURI);
}
return resolver2.getExternalSubset(name, baseURI);
}
InputSource resolveEntity (boolean isPE, String name,
InputSource resolveEntity(boolean isPE, String name,
InputSource in, String baseURI)
throws SAXException, IOException
{
......@@ -604,64 +694,83 @@ final public class SAXDriver
// external entities might be skipped
if (isPE && !extPE)
{
return null;
}
if (!isPE && !extGE)
{
return null;
}
// ... or not
lexicalHandler.startEntity (name);
if (resolver2 != null && useResolver2) {
source = resolver2.resolveEntity (name, in.getPublicId (),
baseURI, in.getSystemId ());
if (source == null) {
in.setSystemId (absolutize (baseURI,
in.getSystemId (), false));
lexicalHandler.startEntity(name);
if (resolver2 != null && useResolver2)
{
source = resolver2.resolveEntity(name, in.getPublicId(),
baseURI, in.getSystemId());
if (source == null)
{
in.setSystemId(absolutize(baseURI,
in.getSystemId(), false));
source = in;
}
} else {
in.setSystemId (absolutize (baseURI, in.getSystemId (), false));
source = entityResolver.resolveEntity (in.getPublicId (),
in.getSystemId ());
}
else
{
in.setSystemId(absolutize(baseURI, in.getSystemId(), false));
source = entityResolver.resolveEntity(in.getPublicId(),
in.getSystemId());
if (source == null)
{
source = in;
}
startExternalEntity (name, source.getSystemId (), true);
}
startExternalEntity(name, source.getSystemId(), true);
return source;
}
// absolutize a system ID relative to the specified base URI
// (temporarily) package-visible for external entity decls
String absolutize (String baseURI, String systemId, boolean nice)
String absolutize(String baseURI, String systemId, boolean nice)
throws MalformedURLException, SAXException
{
// FIXME normalize system IDs -- when?
// - Convert to UTF-8
// - Map reserved and non-ASCII characters to %HH
try {
if (baseURI == null) {
try
{
if (baseURI == null)
{
if (XmlParser.uriWarnings)
{
warn ("No base URI; hope this SYSTEM id is absolute: "
+ systemId);
return new URL (systemId).toString ();
} else
return new URL (new URL (baseURI), systemId).toString ();
} catch (MalformedURLException e) {
}
return new URL(systemId).toString();
}
else
{
return new URL(new URL(baseURI), systemId).toString();
}
}
catch (MalformedURLException e)
{
// Let unknown URI schemes pass through unless we need
// the JVM to map them to i/o streams for us...
if (!nice)
{
throw e;
}
// sometimes sysids for notations or unparsed entities
// aren't really URIs...
warn ("Can't absolutize SYSTEM id: " + e.getMessage ());
warn("Can't absolutize SYSTEM id: " + e.getMessage());
return systemId;
}
}
void startExternalEntity (String name, String systemId,
boolean stackOnly)
void startExternalEntity(String name, String systemId, boolean stackOnly)
throws SAXException
{
// The following warning was deleted because the application has the
......@@ -672,34 +781,38 @@ final public class SAXDriver
warn ("URI was not reported to parser for entity " + name);
*/
if (!stackOnly) // spliced [dtd] needs startEntity
lexicalHandler.startEntity (name);
entityStack.push (systemId);
{
lexicalHandler.startEntity(name);
}
entityStack.push(systemId);
}
void endExternalEntity (String name)
void endExternalEntity(String name)
throws SAXException
{
if (!"[document]".equals (name))
lexicalHandler.endEntity (name);
entityStack.pop ();
if (!"[document]".equals(name))
{
lexicalHandler.endEntity(name);
}
entityStack.pop();
}
void startInternalEntity (String name)
void startInternalEntity(String name)
throws SAXException
{
lexicalHandler.startEntity (name);
lexicalHandler.startEntity(name);
}
void endInternalEntity (String name)
void endInternalEntity(String name)
throws SAXException
{
lexicalHandler.endEntity (name);
lexicalHandler.endEntity(name);
}
void doctypeDecl (String name, String publicId, String systemId)
void doctypeDecl(String name, String publicId, String systemId)
throws SAXException
{
lexicalHandler.startDTD (name, publicId, systemId);
lexicalHandler.startDTD(name, publicId, systemId);
// ... the "name" is a declaration and should be given
// to the DeclHandler (but sax2 doesn't).
......@@ -709,50 +822,60 @@ final public class SAXDriver
// doesn't provide the internal subset "pre-parse"
}
void notationDecl (String name, String ids [])
void notationDecl(String name, String publicId, String systemId,
String baseUri)
throws SAXException
{
try {
dtdHandler.notationDecl (name, ids [0],
(resolveAll && ids [1] != null)
? absolutize (ids [2], ids [1], true)
: ids [1]);
} catch (IOException e) {
try
{
dtdHandler.notationDecl(name, publicId,
(resolveAll && systemId != null)
? absolutize(baseUri, systemId, true)
: systemId);
}
catch (IOException e)
{
// "can't happen"
throw new SAXParseException (e.getMessage (), this, e);
throw new SAXParseException(e.getMessage(), this, e);
}
}
void unparsedEntityDecl (String name, String ids [], String notation)
void unparsedEntityDecl(String name, String publicId, String systemId,
String baseUri, String notation)
throws SAXException
{
try {
dtdHandler.unparsedEntityDecl (name, ids [0],
try
{
dtdHandler.unparsedEntityDecl(name, publicId,
resolveAll
? absolutize (ids [2], ids [1], true)
: ids [1],
? absolutize(baseUri, systemId, true)
: systemId,
notation);
} catch (IOException e) {
}
catch (IOException e)
{
// "can't happen"
throw new SAXParseException (e.getMessage (), this, e);
throw new SAXParseException(e.getMessage(), this, e);
}
}
void endDoctype ()
void endDoctype()
throws SAXException
{
lexicalHandler.endDTD ();
lexicalHandler.endDTD();
}
private void declarePrefix (String prefix, String uri)
private void declarePrefix(String prefix, String uri)
throws SAXException
{
int index = uri.indexOf (':');
int index = uri.indexOf(':');
// many versions of nwalsh docbook stylesheets
// have bogus URLs; so this can't be an error...
if (index < 1 && uri.length () != 0)
warn ("relative URI for namespace: " + uri);
if (index < 1 && uri.length() != 0)
{
warn("relative URI for namespace: " + uri);
}
// FIXME: char [0] must be ascii alpha; chars [1..index]
// must be ascii alphanumeric or in "+-." [RFC 2396]
......@@ -762,88 +885,126 @@ final public class SAXDriver
boolean prefixEquality = prefix.equals("xml");
boolean uriEquality = uri.equals("http://www.w3.org/XML/1998/namespace");
if ((prefixEquality || uriEquality) && !(prefixEquality && uriEquality))
fatal ("xml is by definition bound to the namespace name " +
{
fatal("xml is by definition bound to the namespace name " +
"http://www.w3.org/XML/1998/namespace");
}
//xmlns prefix declaration is illegal but xml prefix declaration is llegal...
if (prefixEquality && uriEquality)
{
return;
}
//name for xmlns prefix must be http://www.w3.org/2000/xmlns/
prefixEquality = prefix.equals("xmlns");
uriEquality = uri.equals("http://www.w3.org/2000/xmlns/");
if ((prefixEquality || uriEquality) && !(prefixEquality && uriEquality))
{
fatal("http://www.w3.org/2000/xmlns/ is by definition bound" +
" to prefix xmlns");
}
//even if the uri is http://www.w3.org/2000/xmlns/ it is illegal to declare it
//even if the uri is http://www.w3.org/2000/xmlns/
// it is illegal to declare it
if (prefixEquality && uriEquality)
{
fatal ("declaring the xmlns prefix is illegal");
}
uri = uri.intern ();
prefixStack.declarePrefix (prefix, uri);
contentHandler.startPrefixMapping (prefix, uri);
uri = uri.intern();
prefixStack.declarePrefix(prefix, uri);
contentHandler.startPrefixMapping(prefix, uri);
}
void attribute (String qname, String value, boolean isSpecified)
void attribute(String qname, String value, boolean isSpecified)
throws SAXException
{
if (!attributes) {
if (!attributes)
{
attributes = true;
if (namespaces)
prefixStack.pushContext ();
{
prefixStack.pushContext();
}
}
// process namespace decls immediately;
// then maybe forget this as an attribute
if (namespaces) {
if (namespaces)
{
int index;
// default NS declaration?
if (getFeature (FEATURE + "string-interning")) {
if ("xmlns" == qname) {
declarePrefix ("", value);
if (stringInterning)
{
if ("xmlns" == qname)
{
declarePrefix("", value);
if (!xmlNames)
{
return;
}
}
// NS prefix declaration?
else if ((index = qname.indexOf (':')) == 5
&& qname.startsWith ("xmlns")) {
String prefix = qname.substring (6);
else if ((index = qname.indexOf(':')) == 5
&& qname.startsWith("xmlns"))
{
String prefix = qname.substring(6);
if (prefix.equals(""))
fatal ("missing prefix in namespace declaration attribute");
if (value.length () == 0) {
verror ("missing URI in namespace declaration attribute: "
{
fatal("missing prefix " +
"in namespace declaration attribute");
}
if (value.length() == 0)
{
verror("missing URI in namespace declaration attribute: "
+ qname);
} else
declarePrefix (prefix, value);
}
else
{
declarePrefix(prefix, value);
}
if (!xmlNames)
{
return;
}
} else {
if ("xmlns".equals(qname)) {
declarePrefix ("", value);
}
}
else
{
if ("xmlns".equals(qname))
{
declarePrefix("", value);
if (!xmlNames)
{
return;
}
}
// NS prefix declaration?
else if ((index = qname.indexOf (':')) == 5
&& qname.startsWith ("xmlns")) {
String prefix = qname.substring (6);
else if ((index = qname.indexOf(':')) == 5
&& qname.startsWith("xmlns"))
{
String prefix = qname.substring(6);
if (value.length () == 0) {
verror ("missing URI in namespace decl attribute: "
if (value.length() == 0)
{
verror("missing URI in namespace decl attribute: "
+ qname);
} else
declarePrefix (prefix, value);
}
else
{
declarePrefix(prefix, value);
}
if (!xmlNames)
{
return;
}
}
}
}
// remember this attribute ...
attributeCount++;
// attribute type comes from querying parser's DTD records
......@@ -851,7 +1012,7 @@ final public class SAXDriver
}
void startElement (String elname)
void startElement(String elname)
throws SAXException
{
ContentHandler handler = contentHandler;
......@@ -869,43 +1030,62 @@ final public class SAXDriver
// the context, and two chunks of name processing.
//
if (!attributes) {
if (!attributes)
{
if (namespaces)
prefixStack.pushContext ();
} else if (namespaces) {
{
prefixStack.pushContext();
}
}
else if (namespaces)
{
// now we can patch up namespace refs; we saw all the
// declarations, so now we'll do the Right Thing
Iterator itt = attributesList.iterator ();
while(itt.hasNext())
Iterator itt = attributesList.iterator();
while (itt.hasNext())
{
Attribute attribute = (Attribute) itt.next();
String qname = attribute.name;
int index;
// default NS declaration?
if (getFeature (FEATURE + "string-interning")) {
if (stringInterning)
{
if ("xmlns" == qname)
{
continue;
} else {
}
}
else
{
if ("xmlns".equals(qname))
{
continue;
}
}
//Illegal in the new Namespaces Draft
//should it be only in 1.1 docs??
if (qname.equals (":"))
fatal ("namespace names consisting of a single colon " +
{
fatal("namespace names consisting of a single colon " +
"character are invalid");
index = qname.indexOf (':');
}
index = qname.indexOf(':');
// NS prefix declaration?
if (index == 5 && qname.startsWith ("xmlns"))
if (index == 5 && qname.startsWith("xmlns"))
{
continue;
}
// it's not a NS decl; patch namespace info items
if (prefixStack.processName (qname, nsTemp, true) == null)
fatal ("undeclared attribute prefix in: " + qname);
else {
if (prefixStack.processName(qname, nsTemp, true) == null)
{
fatal("undeclared attribute prefix in: " + qname);
}
else
{
attribute.nameSpace = nsTemp[0];
attribute.localName = nsTemp[1];
}
......@@ -914,87 +1094,98 @@ final public class SAXDriver
// save element name so attribute callbacks work
elementName = elname;
if (namespaces) {
if (prefixStack.processName (elname, nsTemp, false) == null) {
fatal ("undeclared element prefix in: " + elname);
nsTemp [0] = nsTemp [1] = "";
}
handler.startElement (nsTemp [0], nsTemp [1], elname, this);
} else
handler.startElement ("", "", elname, this);
if (namespaces)
{
if (prefixStack.processName(elname, nsTemp, false) == null)
{
fatal("undeclared element prefix in: " + elname);
nsTemp[0] = nsTemp[1] = "";
}
handler.startElement(nsTemp[0], nsTemp[1], elname, this);
}
else
{
handler.startElement("", "", elname, this);
}
// elementName = null;
// elements with no attributes are pretty common!
if (attributes) {
if (attributes)
{
attributesList.clear();
attributeCount = 0;
attributes = false;
}
}
void endElement (String elname)
void endElement(String elname)
throws SAXException
{
ContentHandler handler = contentHandler;
if (!namespaces) {
handler.endElement ("", "", elname);
if (!namespaces)
{
handler.endElement("", "", elname);
return;
}
prefixStack.processName (elname, nsTemp, false);
handler.endElement (nsTemp [0], nsTemp [1], elname);
prefixStack.processName(elname, nsTemp, false);
handler.endElement(nsTemp[0], nsTemp[1], elname);
Enumeration prefixes = prefixStack.getDeclaredPrefixes ();
Enumeration prefixes = prefixStack.getDeclaredPrefixes();
while (prefixes.hasMoreElements ())
handler.endPrefixMapping ((String) prefixes.nextElement ());
prefixStack.popContext ();
while (prefixes.hasMoreElements())
{
handler.endPrefixMapping((String) prefixes.nextElement());
}
prefixStack.popContext();
}
void startCDATA ()
void startCDATA()
throws SAXException
{
lexicalHandler.startCDATA ();
lexicalHandler.startCDATA();
}
void charData (char ch[], int start, int length)
void charData(char[] ch, int start, int length)
throws SAXException
{
contentHandler.characters (ch, start, length);
contentHandler.characters(ch, start, length);
}
void endCDATA ()
void endCDATA()
throws SAXException
{
lexicalHandler.endCDATA ();
lexicalHandler.endCDATA();
}
void ignorableWhitespace (char ch[], int start, int length)
void ignorableWhitespace(char[] ch, int start, int length)
throws SAXException
{
contentHandler.ignorableWhitespace (ch, start, length);
contentHandler.ignorableWhitespace(ch, start, length);
}
void processingInstruction (String target, String data)
void processingInstruction(String target, String data)
throws SAXException
{
contentHandler.processingInstruction (target, data);
contentHandler.processingInstruction(target, data);
}
void comment (char ch[], int start, int length)
void comment(char[] ch, int start, int length)
throws SAXException
{
if (lexicalHandler != base)
lexicalHandler.comment (ch, start, length);
{
lexicalHandler.comment(ch, start, length);
}
}
void fatal (String message)
void fatal(String message)
throws SAXException
{
SAXParseException fatal;
fatal = new SAXParseException (message, this);
errorHandler.fatalError (fatal);
fatal = new SAXParseException(message, this);
errorHandler.fatalError(fatal);
// Even if the application can continue ... we can't!
throw fatal;
......@@ -1002,25 +1193,24 @@ final public class SAXDriver
// We can safely report a few validity errors that
// make layered SAX2 DTD validation more conformant
void verror (String message)
void verror(String message)
throws SAXException
{
SAXParseException err;
err = new SAXParseException (message, this);
errorHandler.error (err);
err = new SAXParseException(message, this);
errorHandler.error(err);
}
void warn (String message)
void warn(String message)
throws SAXException
{
SAXParseException err;
err = new SAXParseException (message, this);
errorHandler.warning (err);
err = new SAXParseException(message, this);
errorHandler.warning(err);
}
//
// Implementation of org.xml.sax.Attributes.
//
......@@ -1029,7 +1219,7 @@ final public class SAXDriver
* <b>SAX1 AttributeList, SAX2 Attributes</b> method
* (don't invoke on parser);
*/
public int getLength ()
public int getLength()
{
return attributesList.size();
}
......@@ -1037,7 +1227,7 @@ final public class SAXDriver
/**
* <b>SAX2 Attributes</b> method (don't invoke on parser);
*/
public String getURI (int index)
public String getURI(int index)
{
if (index < 0 || index >= attributesList.size())
{
......@@ -1049,7 +1239,7 @@ final public class SAXDriver
/**
* <b>SAX2 Attributes</b> method (don't invoke on parser);
*/
public String getLocalName (int index)
public String getLocalName(int index)
{
if (index < 0 || index >= attributesList.size())
{
......@@ -1070,7 +1260,7 @@ final public class SAXDriver
/**
* <b>SAX2 Attributes</b> method (don't invoke on parser);
*/
public String getQName (int index)
public String getQName(int index)
{
if (index < 0 || index >= attributesList.size())
{
......@@ -1083,7 +1273,7 @@ final public class SAXDriver
/**
* <b>SAX1 AttributeList</b> method (don't invoke on parser);
*/
public String getName (int index)
public String getName(int index)
{
return getQName(index);
}
......@@ -1092,7 +1282,7 @@ final public class SAXDriver
* <b>SAX1 AttributeList, SAX2 Attributes</b> method
* (don't invoke on parser);
*/
public String getType (int index)
public String getType(int index)
{
if (index < 0 || index >= attributesList.size())
{
......@@ -1111,12 +1301,11 @@ final public class SAXDriver
return type;
}
/**
* <b>SAX1 AttributeList, SAX2 Attributes</b> method
* (don't invoke on parser);
*/
public String getValue (int index)
public String getValue(int index)
{
if (index < 0 || index >= attributesList.size())
{
......@@ -1125,11 +1314,10 @@ final public class SAXDriver
return ((Attribute) attributesList.get(index)).value;
}
/**
* <b>SAX2 Attributes</b> method (don't invoke on parser);
*/
public int getIndex (String uri, String local)
public int getIndex(String uri, String local)
{
int length = getLength();
......@@ -1147,11 +1335,10 @@ final public class SAXDriver
return -1;
}
/**
* <b>SAX2 Attributes</b> method (don't invoke on parser);
*/
public int getIndex (String xmlName)
public int getIndex(String xmlName)
{
int length = getLength();
......@@ -1165,11 +1352,10 @@ final public class SAXDriver
return -1;
}
/**
* <b>SAX2 Attributes</b> method (don't invoke on parser);
*/
public String getType (String uri, String local)
public String getType(String uri, String local)
{
int index = getIndex(uri, local);
......@@ -1180,12 +1366,11 @@ final public class SAXDriver
return getType(index);
}
/**
* <b>SAX1 AttributeList, SAX2 Attributes</b> method
* (don't invoke on parser);
*/
public String getType (String xmlName)
public String getType(String xmlName)
{
int index = getIndex(xmlName);
......@@ -1196,11 +1381,10 @@ final public class SAXDriver
return getType(index);
}
/**
* <b>SAX Attributes</b> method (don't invoke on parser);
*/
public String getValue (String uri, String local)
public String getValue(String uri, String local)
{
int index = getIndex(uri, local);
......@@ -1211,12 +1395,11 @@ final public class SAXDriver
return getValue(index);
}
/**
* <b>SAX1 AttributeList, SAX2 Attributes</b> method
* (don't invoke on parser);
*/
public String getValue (String xmlName)
public String getValue(String xmlName)
{
int index = getIndex(xmlName);
......@@ -1227,20 +1410,20 @@ final public class SAXDriver
return getValue(index);
}
//
// Implementation of org.xml.sax.ext.Attributes2
//
/** @return false unless the attribute was declared in the DTD.
* @throws java.lang.ArrayIndexOutOfBoundsException
* When the supplied index does not identify an attribute.
*/
public boolean isDeclared (int index)
public boolean isDeclared(int index)
{
if (index < 0 || index >= attributeCount)
throw new ArrayIndexOutOfBoundsException ();
{
throw new ArrayIndexOutOfBoundsException();
}
String type = parser.getAttributeType(elementName, getQName(index));
return (type != null);
}
......@@ -1249,11 +1432,13 @@ final public class SAXDriver
* @throws java.lang.IllegalArgumentException
* When the supplied names do not identify an attribute.
*/
public boolean isDeclared (String qName)
public boolean isDeclared(String qName)
{
int index = getIndex (qName);
int index = getIndex(qName);
if (index < 0)
throw new IllegalArgumentException ();
{
throw new IllegalArgumentException();
}
String type = parser.getAttributeType(elementName, qName);
return (type != null);
}
......@@ -1262,17 +1447,16 @@ final public class SAXDriver
* @throws java.lang.IllegalArgumentException
* When the supplied names do not identify an attribute.
*/
public boolean isDeclared (String uri, String localName)
public boolean isDeclared(String uri, String localName)
{
int index = getIndex (uri, localName);
int index = getIndex(uri, localName);
return isDeclared(index);
}
/**
* <b>SAX-ext Attributes2</b> method (don't invoke on parser);
*/
public boolean isSpecified (int index)
public boolean isSpecified(int index)
{
return ((Attribute) attributesList.get(index)).specified;
}
......@@ -1280,7 +1464,7 @@ final public class SAXDriver
/**
* <b>SAX-ext Attributes2</b> method (don't invoke on parser);
*/
public boolean isSpecified (String uri, String local)
public boolean isSpecified(String uri, String local)
{
int index = getIndex (uri, local);
return isSpecified(index);
......@@ -1289,13 +1473,12 @@ final public class SAXDriver
/**
* <b>SAX-ext Attributes2</b> method (don't invoke on parser);
*/
public boolean isSpecified (String xmlName)
public boolean isSpecified(String xmlName)
{
int index = getIndex (xmlName);
return isSpecified(index);
}
//
// Implementation of org.xml.sax.Locator.
//
......@@ -1303,7 +1486,7 @@ final public class SAXDriver
/**
* <b>SAX Locator</b> method (don't invoke on parser);
*/
public String getPublicId ()
public String getPublicId()
{
return null; // FIXME track public IDs too
}
......@@ -1311,85 +1494,114 @@ final public class SAXDriver
/**
* <b>SAX Locator</b> method (don't invoke on parser);
*/
public String getSystemId ()
public String getSystemId()
{
if (entityStack.empty())
{
if (entityStack.empty ())
return null;
}
else
return (String) entityStack.peek ();
{
return (String) entityStack.peek();
}
}
/**
* <b>SAX Locator</b> method (don't invoke on parser);
*/
public int getLineNumber ()
public int getLineNumber()
{
return parser.getLineNumber ();
return parser.getLineNumber();
}
/**
* <b>SAX Locator</b> method (don't invoke on parser);
*/
public int getColumnNumber ()
public int getColumnNumber()
{
return parser.getColumnNumber ();
return parser.getColumnNumber();
}
// adapter between SAX2 content handler and SAX1 document handler callbacks
private static class Adapter implements ContentHandler
private static class Adapter
implements ContentHandler
{
private DocumentHandler docHandler;
Adapter (DocumentHandler dh)
{ docHandler = dh; }
private DocumentHandler docHandler;
Adapter(DocumentHandler dh)
{
docHandler = dh;
}
public void setDocumentLocator (Locator l)
{ docHandler.setDocumentLocator (l); }
public void setDocumentLocator(Locator l)
{
docHandler.setDocumentLocator(l);
}
public void startDocument () throws SAXException
{ docHandler.startDocument (); }
public void startDocument()
throws SAXException
{
docHandler.startDocument();
}
public void processingInstruction (String target, String data)
public void processingInstruction(String target, String data)
throws SAXException
{ docHandler.processingInstruction (target, data); }
{
docHandler.processingInstruction(target, data);
}
public void startPrefixMapping (String prefix, String uri)
{ /* ignored */ }
public void startPrefixMapping(String prefix, String uri)
{
/* ignored */
}
public void startElement (
String namespace,
public void startElement(String namespace,
String local,
String name,
Attributes attrs
) throws SAXException
{ docHandler.startElement (name, (AttributeList) attrs); }
Attributes attrs)
throws SAXException
{
docHandler.startElement(name, (AttributeList) attrs);
}
public void characters (char buf [], int offset, int len)
public void characters(char[] buf, int offset, int len)
throws SAXException
{ docHandler.characters (buf, offset, len); }
{
docHandler.characters(buf, offset, len);
}
public void ignorableWhitespace (char buf [], int offset, int len)
public void ignorableWhitespace(char[] buf, int offset, int len)
throws SAXException
{ docHandler.ignorableWhitespace (buf, offset, len); }
{
docHandler.ignorableWhitespace(buf, offset, len);
}
public void skippedEntity (String name)
{ /* ignored */ }
public void skippedEntity(String name)
{
/* ignored */
}
public void endElement (String u, String l, String name)
public void endElement(String u, String l, String name)
throws SAXException
{ docHandler.endElement (name); }
{
docHandler.endElement(name);
}
public void endPrefixMapping (String prefix)
{ /* ignored */ }
public void endPrefixMapping(String prefix)
{
/* ignored */
}
public void endDocument () throws SAXException
{ docHandler.endDocument (); }
public void endDocument()
throws SAXException
{
docHandler.endDocument();
}
}
}
class Attribute
{
private static class Attribute
{
String name;
String value;
......@@ -1404,5 +1616,7 @@ class Attribute
this.nameSpace = "";
this.specified = specified;
}
}
}
}
This source diff could not be displayed because it is too large. You can view the blob instead.
......@@ -70,136 +70,169 @@ import gnu.xml.pipeline.ValidationConsumer;
*
* @author David Brownell
*/
public final class XmlReader implements XMLReader
public final class XmlReader
implements XMLReader
{
private SAXDriver aelfred2 = new SAXDriver ();
private EventFilter filter = new EventFilter ();
static class FatalErrorHandler
extends DefaultHandler2
{
public void error(SAXParseException e)
throws SAXException
{
throw e;
}
}
private SAXDriver aelfred2 = new SAXDriver();
private EventFilter filter = new EventFilter();
private boolean isValidating;
private boolean active;
/** Constructs a SAX Parser. */
public XmlReader ()
{ }
/**
* Constructs a SAX Parser.
*/
public XmlReader()
{
}
/**
* Constructs a SAX Parser, optionally treating validity errors
* as if they were fatal errors.
*/
public XmlReader (boolean invalidIsFatal)
public XmlReader(boolean invalidIsFatal)
{
if (invalidIsFatal)
setErrorHandler (new DefaultHandler2 () {
public void error (SAXParseException e)
throws SAXException
{ throw e; }
});
{
setErrorHandler(new FatalErrorHandler());
}
}
/**
* <b>SAX2</b>: Returns the object used to report the logical
* content of an XML document.
*/
public ContentHandler getContentHandler ()
{ return filter.getContentHandler (); }
public ContentHandler getContentHandler()
{
return filter.getContentHandler();
}
/**
* <b>SAX2</b>: Assigns the object used to report the logical
* content of an XML document.
* @exception IllegalStateException if called mid-parse
*/
public void setContentHandler (ContentHandler handler)
public void setContentHandler(ContentHandler handler)
{
if (active)
throw new IllegalStateException ("already parsing");
filter.setContentHandler (handler);
{
throw new IllegalStateException("already parsing");
}
filter.setContentHandler(handler);
}
/**
* <b>SAX2</b>: Returns the object used to process declarations related
* to notations and unparsed entities.
*/
public DTDHandler getDTDHandler ()
{ return filter.getDTDHandler (); }
public DTDHandler getDTDHandler()
{
return filter.getDTDHandler();
}
/**
* <b>SAX1</b> Assigns DTD handler
* @exception IllegalStateException if called mid-parse
*/
public void setDTDHandler (DTDHandler handler)
public void setDTDHandler(DTDHandler handler)
{
if (active)
throw new IllegalStateException ("already parsing");
filter.setDTDHandler (handler);
{
throw new IllegalStateException("already parsing");
}
filter.setDTDHandler(handler);
}
/**
* <b>SAX2</b>: Returns the object used when resolving external
* entities during parsing (both general and parameter entities).
*/
public EntityResolver getEntityResolver ()
{ return aelfred2.getEntityResolver (); }
public EntityResolver getEntityResolver()
{
return aelfred2.getEntityResolver();
}
/** <b>SAX1</b> Assigns parser's entity resolver */
public void setEntityResolver (EntityResolver handler)
{ aelfred2.setEntityResolver (handler); }
/**
* <b>SAX1</b> Assigns parser's entity resolver
*/
public void setEntityResolver(EntityResolver handler)
{
aelfred2.setEntityResolver(handler);
}
/**
* <b>SAX2</b>: Returns the object used to receive callbacks for XML
* errors of all levels (fatal, nonfatal, warning); this is never null;
*/
public ErrorHandler getErrorHandler ()
{ return aelfred2.getErrorHandler (); }
public ErrorHandler getErrorHandler()
{
return aelfred2.getErrorHandler();
}
/**
* <b>SAX1</b> Assigns error handler
* @exception IllegalStateException if called mid-parse
*/
public void setErrorHandler (ErrorHandler handler)
public void setErrorHandler(ErrorHandler handler)
{
if (active)
throw new IllegalStateException ("already parsing");
aelfred2.setErrorHandler (handler);
{
throw new IllegalStateException("already parsing");
}
aelfred2.setErrorHandler(handler);
}
/**
* <b>SAX2</b>: Assigns the specified property.
* @exception IllegalStateException if called mid-parse
*/
public void setProperty (String propertyId, Object value)
public void setProperty(String propertyId, Object value)
throws SAXNotRecognizedException, SAXNotSupportedException
{
if (active)
throw new IllegalStateException ("already parsing");
if (getProperty (propertyId) != value)
filter.setProperty (propertyId, value);
{
throw new IllegalStateException("already parsing");
}
if (getProperty(propertyId) != value)
{
filter.setProperty(propertyId, value);
}
}
/**
* <b>SAX2</b>: Returns the specified property.
*/
public Object getProperty (String propertyId)
public Object getProperty(String propertyId)
throws SAXNotRecognizedException
{
if ((SAXDriver.PROPERTY + "declaration-handler")
.equals (propertyId)
|| (SAXDriver.PROPERTY + "lexical-handler")
.equals (propertyId))
return filter.getProperty (propertyId);
throw new SAXNotRecognizedException (propertyId);
if ((SAXDriver.PROPERTY + "declaration-handler").equals(propertyId)
|| (SAXDriver.PROPERTY + "lexical-handler").equals(propertyId))
{
return filter.getProperty(propertyId);
}
throw new SAXNotRecognizedException(propertyId);
}
private void forceValidating ()
private void forceValidating()
throws SAXNotRecognizedException, SAXNotSupportedException
{
aelfred2.setFeature (
SAXDriver.FEATURE + "namespace-prefixes",
aelfred2.setFeature(SAXDriver.FEATURE + "namespace-prefixes",
true);
aelfred2.setFeature (
SAXDriver.FEATURE + "external-general-entities",
aelfred2.setFeature(SAXDriver.FEATURE + "external-general-entities",
true);
aelfred2.setFeature (
SAXDriver.FEATURE + "external-parameter-entities",
aelfred2.setFeature(SAXDriver.FEATURE + "external-parameter-entities",
true);
}
......@@ -208,22 +241,32 @@ public final class XmlReader implements XMLReader
* Note that this parser requires reporting of namespace prefixes when
* validating.
*/
public void setFeature (String featureId, boolean state)
public void setFeature(String featureId, boolean state)
throws SAXNotRecognizedException, SAXNotSupportedException
{
boolean value = getFeature (featureId);
boolean value = getFeature(featureId);
if (state == value)
{
return;
}
if ((SAXDriver.FEATURE + "validation").equals (featureId)) {
if ((SAXDriver.FEATURE + "validation").equals(featureId))
{
if (active)
throw new SAXNotSupportedException ("already parsing");
{
throw new SAXNotSupportedException("already parsing");
}
if (state)
forceValidating ();
{
forceValidating();
}
isValidating = state;
} else
aelfred2.setFeature (featureId, state);
}
else
{
aelfred2.setFeature(featureId, state);
}
}
/**
......@@ -233,13 +276,15 @@ public final class XmlReader implements XMLReader
*
* @see SAXDriver
*/
public boolean getFeature (String featureId)
public boolean getFeature(String featureId)
throws SAXNotRecognizedException, SAXNotSupportedException
{
if ((SAXDriver.FEATURE + "validation").equals (featureId))
if ((SAXDriver.FEATURE + "validation").equals(featureId))
{
return isValidating;
}
return aelfred2.getFeature (featureId);
return aelfred2.getFeature(featureId);
}
/**
......@@ -247,18 +292,20 @@ public final class XmlReader implements XMLReader
* only locales using the English language are supported.
* @param locale The locale for which diagnostics will be generated
*/
public void setLocale (Locale locale)
public void setLocale(Locale locale)
throws SAXException
{ aelfred2.setLocale (locale); }
{
aelfred2.setLocale(locale);
}
/**
* <b>SAX1</b>: Preferred API to parse an XML document, using a
* system identifier (URI).
*/
public void parse (String systemId)
public void parse(String systemId)
throws SAXException, IOException
{
parse (new InputSource (systemId));
parse(new InputSource(systemId));
}
/**
......@@ -276,40 +323,52 @@ public final class XmlReader implements XMLReader
* @exception IOException IOExceptions are normally through through
* the parser if there are problems reading the source document.
*/
public void parse (InputSource source)
public void parse(InputSource source)
throws SAXException, IOException
{
EventFilter next;
boolean nsdecls;
synchronized (aelfred2) {
synchronized (aelfred2)
{
if (active)
throw new IllegalStateException ("already parsing");
{
throw new IllegalStateException("already parsing");
}
active = true;
}
// set up the output pipeline
if (isValidating) {
forceValidating ();
next = new ValidationConsumer (filter);
} else
if (isValidating)
{
forceValidating();
next = new ValidationConsumer(filter);
}
else
{
next = filter;
}
// connect pipeline and error handler
// don't let _this_ call to bind() affect xmlns* attributes
nsdecls = aelfred2.getFeature (
SAXDriver.FEATURE + "namespace-prefixes");
EventFilter.bind (aelfred2, next);
nsdecls = aelfred2.getFeature(SAXDriver.FEATURE + "namespace-prefixes");
EventFilter.bind(aelfred2, next);
if (!nsdecls)
aelfred2.setFeature (
SAXDriver.FEATURE + "namespace-prefixes",
{
aelfred2.setFeature(SAXDriver.FEATURE + "namespace-prefixes",
false);
}
// parse, clean up
try {
aelfred2.parse (source);
} finally {
try
{
aelfred2.parse(source);
}
finally
{
active = false;
}
}
}
......@@ -1686,13 +1686,16 @@ public abstract class DomNode
{
continue;
}
if (count < notificationSet.length)
if (count >= notificationSet.length)
{
notificationSet[count++] = rec;
// very simple growth algorithm
int len = Math.max(notificationSet.length, 1);
ListenerRecord[] tmp = new ListenerRecord[len * 2];
System.arraycopy(notificationSet, 0, tmp, 0,
notificationSet.length);
notificationSet = tmp;
}
else
// XXX fire up some cheap growth algorithm
throw new RuntimeException("Event notification set size exceeded");
notificationSet[count++] = rec;
}
// Notify just those listeners
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment