Commit acdf61e1 by Tom Tromey

Reverted erroneous commit

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