From 554fd8c5195424bdbcabf5de30fdc183aba391bd Mon Sep 17 00:00:00 2001
From: upstream source tree This parser currently implements the SAX1 Parser API, but
+ * it may not continue to do so in the future.
+ *
+ * @author Written by David Megginson (version 1.2a from Microstar)
+ * @author Updated by David Brownell <dbrownell@users.sourceforge.net>
+ * @see org.xml.sax.Parser
+ */
+final public class SAXDriver
+ implements Locator, Attributes2, XMLReader, Parser, AttributeList
+{
+
+ private final DefaultHandler2 base = new DefaultHandler2();
+ private XmlParser parser;
+
+ private EntityResolver entityResolver = base;
+ private EntityResolver2 resolver2 = null;
+ private ContentHandler contentHandler = base;
+ private DTDHandler dtdHandler = base;
+ private ErrorHandler errorHandler = base;
+ private DeclHandler declHandler = base;
+ private LexicalHandler lexicalHandler = base;
+
+ private String elementName;
+ private Stack entityStack;
+
+ // one vector (of object/struct): faster, smaller
+ private List attributesList;
+
+ private boolean namespaces = true;
+ private boolean xmlNames = false;
+ private boolean extGE = true;
+ private boolean extPE = true;
+ private boolean resolveAll = true;
+ private boolean useResolver2 = true;
+
+ // package private to allow (read-only) access in XmlParser
+ boolean stringInterning = true;
+
+ private int attributeCount;
+ private boolean attributes;
+ private String[] nsTemp;
+ private NamespaceSupport prefixStack;
+
+ //
+ // Constructor.
+ //
+
+ /**
+ * Constructs a SAX Parser.
+ */
+ public SAXDriver()
+ {
+ reset();
+ }
+
+ private void reset()
+ {
+ elementName = null;
+ entityStack = new Stack();
+ attributesList = Collections.synchronizedList(new ArrayList());
+ attributeCount = 0;
+ attributes = false;
+ nsTemp = new String[3];
+ prefixStack = null;
+ }
+
+
+ //
+ // Implementation of org.xml.sax.Parser.
+ //
+
+ /**
+ * SAX1: Sets the locale used for diagnostics; currently,
+ * only locales using the English language are supported.
+ * @param locale The locale for which diagnostics will be generated
+ */
+ public void setLocale(Locale locale)
+ throws SAXException
+ {
+ if ("en".equals(locale.getLanguage()))
+ {
+ return;
+ }
+ throw new SAXException ("AElfred2 only supports English locales.");
+ }
+
+ /**
+ * SAX2: Returns the object used when resolving external
+ * entities during parsing (both general and parameter entities).
+ */
+ public EntityResolver getEntityResolver()
+ {
+ return (entityResolver == base) ? null : entityResolver;
+ }
+
+ /**
+ * SAX1, SAX2: Set the entity resolver for this parser.
+ * @param handler The object to receive entity events.
+ */
+ public void setEntityResolver(EntityResolver resolver)
+ {
+ if (resolver instanceof EntityResolver2)
+ {
+ resolver2 = (EntityResolver2) resolver;
+ }
+ else
+ {
+ resolver2 = null;
+ }
+ if (resolver == null)
+ {
+ resolver = base;
+ }
+ entityResolver = resolver;
+ }
+
+ /**
+ * SAX2: Returns the object used to process declarations related
+ * to notations and unparsed entities.
+ */
+ public DTDHandler getDTDHandler()
+ {
+ return (dtdHandler == base) ? null : dtdHandler;
+ }
+
+ /**
+ * SAX1, SAX2: Set the DTD handler for this parser.
+ * @param handler The object to receive DTD events.
+ */
+ public void setDTDHandler(DTDHandler handler)
+ {
+ if (handler == null)
+ {
+ handler = base;
+ }
+ this.dtdHandler = handler;
+ }
+
+
+ /**
+ * SAX1: Set the document handler for this parser. If a
+ * content handler was set, this document handler will supplant it.
+ * The parser is set to report all XML 1.0 names rather than to
+ * filter out "xmlns" attributes (the "namespace-prefixes" feature
+ * is set to true).
+ *
+ * @deprecated SAX2 programs should use the XMLReader interface
+ * and a ContentHandler.
+ *
+ * @param handler The object to receive document events.
+ */
+ public void setDocumentHandler(DocumentHandler handler)
+ {
+ contentHandler = new Adapter(handler);
+ xmlNames = true;
+ }
+
+ /**
+ * SAX2: Returns the object used to report the logical
+ * content of an XML document.
+ */
+ public ContentHandler getContentHandler()
+ {
+ return (contentHandler == base) ? null : contentHandler;
+ }
+
+ /**
+ * SAX2: Assigns the object used to report the logical
+ * content of an XML document. If a document handler was set,
+ * this content handler will supplant it (but XML 1.0 style name
+ * reporting may remain enabled).
+ */
+ public void setContentHandler(ContentHandler handler)
+ {
+ if (handler == null)
+ {
+ handler = base;
+ }
+ contentHandler = handler;
+ }
+
+ /**
+ * SAX1, SAX2: Set the error handler for this parser.
+ * @param handler The object to receive error events.
+ */
+ public void setErrorHandler(ErrorHandler handler)
+ {
+ if (handler == null)
+ {
+ handler = base;
+ }
+ this.errorHandler = handler;
+ }
+
+ /**
+ * SAX2: 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;
+ }
+
+ /**
+ * SAX1, SAX2: Auxiliary API to parse an XML document, used mostly
+ * when no URI is available.
+ * If you want anything useful to happen, you should set
+ * at least one type of handler.
+ * @param source The XML input source. Don't set 'encoding' unless
+ * you know for a fact that it's correct.
+ * @see #setEntityResolver
+ * @see #setDTDHandler
+ * @see #setContentHandler
+ * @see #setErrorHandler
+ * @exception SAXException The handlers may throw any SAXException,
+ * and the parser normally throws SAXParseException objects.
+ * @exception IOException IOExceptions are normally through through
+ * the parser if there are problems reading the source document.
+ */
+ public void parse(InputSource source)
+ throws SAXException, IOException
+ {
+ synchronized (base)
+ {
+ parser = new XmlParser();
+ if (namespaces)
+ {
+ prefixStack = new NamespaceSupport();
+ }
+ else if (!xmlNames)
+ {
+ throw new IllegalStateException();
+ }
+ parser.setHandler(this);
+
+ try
+ {
+ Reader r = source.getCharacterStream();
+ InputStream in = source.getByteStream();
+
+ parser.doParse(source.getSystemId(),
+ source.getPublicId(),
+ r,
+ in,
+ source.getEncoding());
+ }
+ catch (SAXException e)
+ {
+ throw e;
+ }
+ catch (IOException e)
+ {
+ throw e;
+ }
+ catch (RuntimeException e)
+ {
+ throw e;
+ }
+ catch (Exception e)
+ {
+ throw new SAXParseException(e.getMessage(), this, e);
+ }
+ finally
+ {
+ contentHandler.endDocument();
+ reset();
+ }
+ }
+ }
+
+ /**
+ * SAX1, SAX2: Preferred API to parse an XML document, using a
+ * system identifier (URI).
+ */
+ public void parse(String systemId)
+ throws SAXException, IOException
+ {
+ parse(new InputSource(systemId));
+ }
+
+ //
+ // Implementation of SAX2 "XMLReader" interface
+ //
+ static final String FEATURE = "http://xml.org/sax/features/";
+ static final String PROPERTY = "http://xml.org/sax/properties/";
+
+ /**
+ * SAX2: Tells the value of the specified feature flag.
+ *
+ * @exception SAXNotRecognizedException thrown if the feature flag
+ * is neither built in, nor yet assigned.
+ */
+ public boolean getFeature(String featureId)
+ throws SAXNotRecognizedException, SAXNotSupportedException
+ {
+ if ((FEATURE + "validation").equals(featureId))
+ {
+ return false;
+ }
+
+ // external entities (both types) are optionally included
+ if ((FEATURE + "external-general-entities").equals(featureId))
+ {
+ return extGE;
+ }
+ 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))
+ {
+ return xmlNames;
+ }
+
+ // report element/attribute namespaces?
+ if ((FEATURE + "namespaces").equals(featureId))
+ {
+ return namespaces;
+ }
+
+ // all PEs and GEs are reported
+ if ((FEATURE + "lexical-handler/parameter-entities").equals(featureId))
+ {
+ return true;
+ }
+
+ // default is true
+ if ((FEATURE + "string-interning").equals(featureId))
+ {
+ return stringInterning;
+ }
+
+ // EXTENSIONS 1.1
+
+ // always returns isSpecified info
+ if ((FEATURE + "use-attributes2").equals(featureId))
+ {
+ return true;
+ }
+
+ // meaningful between startDocument/endDocument
+ if ((FEATURE + "is-standalone").equals(featureId))
+ {
+ if (parser == null)
+ {
+ throw new SAXNotSupportedException(featureId);
+ }
+ return parser.isStandalone();
+ }
+
+ // optionally don't absolutize URIs in declarations
+ if ((FEATURE + "resolve-dtd-uris").equals(featureId))
+ {
+ return resolveAll;
+ }
+
+ // optionally use resolver2 interface methods, if possible
+ if ((FEATURE + "use-entity-resolver2").equals(featureId))
+ {
+ return useResolver2;
+ }
+
+ throw new SAXNotRecognizedException(featureId);
+ }
+
+ // package private
+ DeclHandler getDeclHandler()
+ {
+ return declHandler;
+ }
+
+ // package private
+ boolean resolveURIs()
+ {
+ return resolveAll;
+ }
+
+ /**
+ * SAX2: Returns the specified property.
+ *
+ * @exception SAXNotRecognizedException thrown if the property value
+ * is neither built in, nor yet stored.
+ */
+ public Object getProperty(String propertyId)
+ throws SAXNotRecognizedException
+ {
+ if ((PROPERTY + "declaration-handler").equals(propertyId))
+ {
+ return (declHandler == base) ? null : declHandler;
+ }
+
+ if ((PROPERTY + "lexical-handler").equals(propertyId))
+ {
+ return (lexicalHandler == base) ? null : lexicalHandler;
+ }
+
+ // unknown properties
+ throw new SAXNotRecognizedException(propertyId);
+ }
+
+ /**
+ * SAX2: Sets the state of feature flags in this parser. Some
+ * built-in feature flags are mutable.
+ */
+ public void setFeature(String featureId, boolean value)
+ throws SAXNotRecognizedException, SAXNotSupportedException
+ {
+ boolean state;
+
+ // Features with a defined value, we just change it if we can.
+ state = getFeature (featureId);
+
+ if (state == value)
+ {
+ return;
+ }
+ if (parser != null)
+ {
+ throw new SAXNotSupportedException("not while parsing");
+ }
+
+ 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))
+ {
+ namespaces = value;
+ // forcibly prevent illegal parser state
+ if (!namespaces)
+ {
+ xmlNames = true;
+ }
+ return;
+ }
+
+ if ((FEATURE + "external-general-entities").equals(featureId))
+ {
+ extGE = value;
+ return;
+ }
+ if ((FEATURE + "external-parameter-entities").equals(featureId))
+ {
+ extPE = value;
+ return;
+ }
+ if ((FEATURE + "resolve-dtd-uris").equals(featureId))
+ {
+ resolveAll = value;
+ return;
+ }
+
+ if ((FEATURE + "use-entity-resolver2").equals(featureId))
+ {
+ useResolver2 = value;
+ return;
+ }
+
+ throw new SAXNotRecognizedException(featureId);
+ }
+
+ /**
+ * SAX2: Assigns the specified property. Like SAX1 handlers,
+ * these may be changed at any time.
+ */
+ public void setProperty(String propertyId, Object value)
+ throws SAXNotRecognizedException, SAXNotSupportedException
+ {
+ // see if the property is recognized
+ getProperty(propertyId);
+
+ // Properties with a defined value, we just change it if we can.
+
+ if ((PROPERTY + "declaration-handler").equals(propertyId))
+ {
+ if (value == null)
+ {
+ declHandler = base;
+ }
+ else if (!(value instanceof DeclHandler))
+ {
+ throw new SAXNotSupportedException(propertyId);
+ }
+ else
+ {
+ declHandler = (DeclHandler) value;
+ }
+ return ;
+ }
+
+ if ((PROPERTY + "lexical-handler").equals(propertyId))
+ {
+ if (value == null)
+ {
+ lexicalHandler = base;
+ }
+ else if (!(value instanceof LexicalHandler))
+ {
+ throw new SAXNotSupportedException(propertyId);
+ }
+ else
+ {
+ lexicalHandler = (LexicalHandler) value;
+ }
+ return;
+ }
+
+ throw new SAXNotSupportedException(propertyId);
+ }
+
+ //
+ // This is where the driver receives XmlParser callbacks and translates
+ // them into SAX callbacks. Some more callbacks have been added for
+ // SAX2 support.
+ //
+
+ void startDocument()
+ throws SAXException
+ {
+ contentHandler.setDocumentLocator(this);
+ contentHandler.startDocument();
+ attributesList.clear();
+ }
+
+ void skippedEntity(String name)
+ throws SAXException
+ {
+ contentHandler.skippedEntity(name);
+ }
+
+ InputSource getExternalSubset(String name, String baseURI)
+ throws SAXException, IOException
+ {
+ if (resolver2 == null || !useResolver2 || !extPE)
+ {
+ return null;
+ }
+ return resolver2.getExternalSubset(name, baseURI);
+ }
+
+ InputSource resolveEntity(boolean isPE, String name,
+ InputSource in, String baseURI)
+ throws SAXException, IOException
+ {
+ InputSource source;
+
+ // 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));
+ source = in;
+ }
+ }
+ else
+ {
+ in.setSystemId(absolutize(baseURI,
+ in.getSystemId(),
+ entityResolver != base));
+ source = entityResolver.resolveEntity(in.getPublicId(),
+ in.getSystemId());
+ if (source == null)
+ {
+ source = in;
+ }
+ }
+ 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)
+ throws MalformedURLException, SAXException
+ {
+ // FIXME normalize system IDs -- when?
+ // - Convert to UTF-8
+ // - Map reserved and non-ASCII characters to %HH
+
+ 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)
+ {
+ // 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());
+ return systemId;
+ }
+ }
+
+ void startExternalEntity(String name, String systemId, boolean stackOnly)
+ throws SAXException
+ {
+ // The following warning was deleted because the application has the
+ // option of not setting systemId. Sun's JAXP or Xerces seems to
+ // ignore this case.
+ /*
+ if (systemId == null)
+ warn ("URI was not reported to parser for entity " + name);
+ */
+ if (!stackOnly) // spliced [dtd] needs startEntity
+ {
+ lexicalHandler.startEntity(name);
+ }
+ entityStack.push(systemId);
+ }
+
+ void endExternalEntity(String name)
+ throws SAXException
+ {
+ if (!"[document]".equals(name))
+ {
+ lexicalHandler.endEntity(name);
+ }
+ entityStack.pop();
+ }
+
+ void startInternalEntity(String name)
+ throws SAXException
+ {
+ lexicalHandler.startEntity(name);
+ }
+
+ void endInternalEntity(String name)
+ throws SAXException
+ {
+ lexicalHandler.endEntity(name);
+ }
+
+ void doctypeDecl(String name, String publicId, String systemId)
+ throws SAXException
+ {
+ lexicalHandler.startDTD(name, publicId, systemId);
+
+ // ... the "name" is a declaration and should be given
+ // to the DeclHandler (but sax2 doesn't).
+
+ // the IDs for the external subset are lexical details,
+ // as are the contents of the internal subset; but sax2
+ // doesn't provide the internal subset "pre-parse"
+ }
+
+ void notationDecl(String name, String publicId, String systemId,
+ String baseUri)
+ throws SAXException
+ {
+ 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);
+ }
+ }
+
+ void unparsedEntityDecl(String name, String publicId, String systemId,
+ String baseUri, String notation)
+ throws SAXException
+ {
+ try
+ {
+ dtdHandler.unparsedEntityDecl(name, publicId,
+ resolveAll
+ ? absolutize(baseUri, systemId, true)
+ : systemId,
+ notation);
+ }
+ catch (IOException e)
+ {
+ // "can't happen"
+ throw new SAXParseException(e.getMessage(), this, e);
+ }
+ }
+
+ void endDoctype()
+ throws SAXException
+ {
+ lexicalHandler.endDTD();
+ }
+
+ private void declarePrefix(String prefix, String uri)
+ throws SAXException
+ {
+ 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);
+ }
+
+ // FIXME: char [0] must be ascii alpha; chars [1..index]
+ // must be ascii alphanumeric or in "+-." [RFC 2396]
+
+ //Namespace Constraints
+ //name for xml prefix must be http://www.w3.org/XML/1998/namespace
+ 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 " +
+ "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
+ if (prefixEquality && uriEquality)
+ {
+ fatal ("declaring the xmlns prefix is illegal");
+ }
+
+ uri = uri.intern();
+ prefixStack.declarePrefix(prefix, uri);
+ contentHandler.startPrefixMapping(prefix, uri);
+ }
+
+ void attribute(String qname, String value, boolean isSpecified)
+ throws SAXException
+ {
+ if (!attributes)
+ {
+ attributes = true;
+ if (namespaces)
+ {
+ prefixStack.pushContext();
+ }
+ }
+
+ // process namespace decls immediately;
+ // then maybe forget this as an attribute
+ if (namespaces)
+ {
+ int index;
+
+ // default NS declaration?
+ 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);
+
+ if (prefix.equals(""))
+ {
+ fatal("missing prefix " +
+ "in namespace declaration attribute");
+ }
+ if (value.length() == 0)
+ {
+ verror("missing URI in namespace declaration attribute: "
+ + qname);
+ }
+ else
+ {
+ declarePrefix(prefix, value);
+ }
+ if (!xmlNames)
+ {
+ return;
+ }
+ }
+ }
+ 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);
+
+ if (value.length() == 0)
+ {
+ verror("missing URI in namespace decl attribute: "
+ + qname);
+ }
+ else
+ {
+ declarePrefix(prefix, value);
+ }
+ if (!xmlNames)
+ {
+ return;
+ }
+ }
+ }
+ }
+ // remember this attribute ...
+ attributeCount++;
+
+ // attribute type comes from querying parser's DTD records
+ attributesList.add(new Attribute(qname, value, isSpecified));
+
+ }
+
+ void startElement(String elname)
+ throws SAXException
+ {
+ ContentHandler handler = contentHandler;
+
+ //
+ // NOTE: this implementation of namespace support adds something
+ // like six percent to parsing CPU time, in a large (~50 MB)
+ // document that doesn't use namespaces at all. (Measured by PC
+ // sampling, with a bug where endElement processing was omitted.)
+ // [Measurement referred to older implementation, older JVM ...]
+ //
+ // It ought to become notably faster in such cases. Most
+ // costs are the prefix stack calling Hashtable.get() (2%),
+ // String.hashCode() (1.5%) and about 1.3% each for pushing
+ // the context, and two chunks of name processing.
+ //
+
+ if (!attributes)
+ {
+ 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())
+ {
+ Attribute attribute = (Attribute) itt.next();
+ String qname = attribute.name;
+ int index;
+
+ // default NS declaration?
+ if (stringInterning)
+ {
+ if ("xmlns" == qname)
+ {
+ continue;
+ }
+ }
+ 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 " +
+ "character are invalid");
+ }
+ index = qname.indexOf(':');
+
+ // NS prefix declaration?
+ 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
+ {
+ attribute.nameSpace = nsTemp[0];
+ attribute.localName = nsTemp[1];
+ }
+ }
+ }
+
+ // 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);
+ }
+ // elementName = null;
+
+ // elements with no attributes are pretty common!
+ if (attributes)
+ {
+ attributesList.clear();
+ attributeCount = 0;
+ attributes = false;
+ }
+ }
+
+ void endElement(String elname)
+ throws SAXException
+ {
+ ContentHandler handler = contentHandler;
+
+ if (!namespaces)
+ {
+ handler.endElement("", "", elname);
+ return;
+ }
+ prefixStack.processName(elname, nsTemp, false);
+ handler.endElement(nsTemp[0], nsTemp[1], elname);
+
+ Enumeration prefixes = prefixStack.getDeclaredPrefixes();
+
+ while (prefixes.hasMoreElements())
+ {
+ handler.endPrefixMapping((String) prefixes.nextElement());
+ }
+ prefixStack.popContext();
+ }
+
+ void startCDATA()
+ throws SAXException
+ {
+ lexicalHandler.startCDATA();
+ }
+
+ void charData(char[] ch, int start, int length)
+ throws SAXException
+ {
+ contentHandler.characters(ch, start, length);
+ }
+
+ void endCDATA()
+ throws SAXException
+ {
+ lexicalHandler.endCDATA();
+ }
+
+ void ignorableWhitespace(char[] ch, int start, int length)
+ throws SAXException
+ {
+ contentHandler.ignorableWhitespace(ch, start, length);
+ }
+
+ void processingInstruction(String target, String data)
+ throws SAXException
+ {
+ contentHandler.processingInstruction(target, data);
+ }
+
+ void comment(char[] ch, int start, int length)
+ throws SAXException
+ {
+ if (lexicalHandler != base)
+ {
+ lexicalHandler.comment(ch, start, length);
+ }
+ }
+
+ void fatal(String message)
+ throws SAXException
+ {
+ SAXParseException fatal;
+
+ fatal = new SAXParseException(message, this);
+ errorHandler.fatalError(fatal);
+
+ // Even if the application can continue ... we can't!
+ throw fatal;
+ }
+
+ // We can safely report a few validity errors that
+ // make layered SAX2 DTD validation more conformant
+ void verror(String message)
+ throws SAXException
+ {
+ SAXParseException err;
+
+ err = new SAXParseException(message, this);
+ errorHandler.error(err);
+ }
+
+ void warn(String message)
+ throws SAXException
+ {
+ SAXParseException err;
+
+ err = new SAXParseException(message, this);
+ errorHandler.warning(err);
+ }
+
+ //
+ // Implementation of org.xml.sax.Attributes.
+ //
+
+ /**
+ * SAX1 AttributeList, SAX2 Attributes method
+ * (don't invoke on parser);
+ */
+ public int getLength()
+ {
+ return attributesList.size();
+ }
+
+ /**
+ * SAX2 Attributes method (don't invoke on parser);
+ */
+ public String getURI(int index)
+ {
+ if (index < 0 || index >= attributesList.size())
+ {
+ return null;
+ }
+ return ((Attribute) attributesList.get(index)).nameSpace;
+ }
+
+ /**
+ * SAX2 Attributes method (don't invoke on parser);
+ */
+ public String getLocalName(int index)
+ {
+ if (index < 0 || index >= attributesList.size())
+ {
+ return null;
+ }
+ Attribute attr = (Attribute) attributesList.get(index);
+ // FIXME attr.localName is sometimes null, why?
+ if (namespaces && attr.localName == null)
+ {
+ // XXX fix this here for now
+ int ci = attr.name.indexOf(':');
+ attr.localName = (ci == -1) ? attr.name :
+ attr.name.substring(ci + 1);
+ }
+ return (attr.localName == null) ? "" : attr.localName;
+ }
+
+ /**
+ * SAX2 Attributes method (don't invoke on parser);
+ */
+ public String getQName(int index)
+ {
+ if (index < 0 || index >= attributesList.size())
+ {
+ return null;
+ }
+ Attribute attr = (Attribute) attributesList.get(index);
+ return (attr.name == null) ? "" : attr.name;
+ }
+
+ /**
+ * SAX1 AttributeList method (don't invoke on parser);
+ */
+ public String getName(int index)
+ {
+ return getQName(index);
+ }
+
+ /**
+ * SAX1 AttributeList, SAX2 Attributes method
+ * (don't invoke on parser);
+ */
+ public String getType(int index)
+ {
+ if (index < 0 || index >= attributesList.size())
+ {
+ return null;
+ }
+ String type = parser.getAttributeType(elementName, getQName(index));
+ if (type == null)
+ {
+ return "CDATA";
+ }
+ // ... use DeclHandler.attributeDecl to see enumerations
+ if (type == "ENUMERATION")
+ {
+ return "NMTOKEN";
+ }
+ return type;
+ }
+
+ /**
+ * SAX1 AttributeList, SAX2 Attributes method
+ * (don't invoke on parser);
+ */
+ public String getValue(int index)
+ {
+ if (index < 0 || index >= attributesList.size())
+ {
+ return null;
+ }
+ return ((Attribute) attributesList.get(index)).value;
+ }
+
+ /**
+ * SAX2 Attributes method (don't invoke on parser);
+ */
+ public int getIndex(String uri, String local)
+ {
+ int length = getLength();
+
+ for (int i = 0; i < length; i++)
+ {
+ if (!getURI(i).equals(uri))
+ {
+ continue;
+ }
+ if (getLocalName(i).equals(local))
+ {
+ return i;
+ }
+ }
+ return -1;
+ }
+
+ /**
+ * SAX2 Attributes method (don't invoke on parser);
+ */
+ public int getIndex(String xmlName)
+ {
+ int length = getLength();
+
+ for (int i = 0; i < length; i++)
+ {
+ if (getQName(i).equals(xmlName))
+ {
+ return i;
+ }
+ }
+ return -1;
+ }
+
+ /**
+ * SAX2 Attributes method (don't invoke on parser);
+ */
+ public String getType(String uri, String local)
+ {
+ int index = getIndex(uri, local);
+
+ if (index < 0)
+ {
+ return null;
+ }
+ return getType(index);
+ }
+
+ /**
+ * SAX1 AttributeList, SAX2 Attributes method
+ * (don't invoke on parser);
+ */
+ public String getType(String xmlName)
+ {
+ int index = getIndex(xmlName);
+
+ if (index < 0)
+ {
+ return null;
+ }
+ return getType(index);
+ }
+
+ /**
+ * SAX Attributes method (don't invoke on parser);
+ */
+ public String getValue(String uri, String local)
+ {
+ int index = getIndex(uri, local);
+
+ if (index < 0)
+ {
+ return null;
+ }
+ return getValue(index);
+ }
+
+ /**
+ * SAX1 AttributeList, SAX2 Attributes method
+ * (don't invoke on parser);
+ */
+ public String getValue(String xmlName)
+ {
+ int index = getIndex(xmlName);
+
+ if (index < 0)
+ {
+ return null;
+ }
+ 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)
+ {
+ if (index < 0 || index >= attributeCount)
+ {
+ throw new ArrayIndexOutOfBoundsException();
+ }
+ String type = parser.getAttributeType(elementName, getQName(index));
+ return (type != null);
+ }
+
+ /** @return false unless the attribute was declared in the DTD.
+ * @throws java.lang.IllegalArgumentException
+ * When the supplied names do not identify an attribute.
+ */
+ public boolean isDeclared(String qName)
+ {
+ int index = getIndex(qName);
+ if (index < 0)
+ {
+ throw new IllegalArgumentException();
+ }
+ String type = parser.getAttributeType(elementName, qName);
+ return (type != null);
+ }
+
+ /** @return false unless the attribute was declared in the DTD.
+ * @throws java.lang.IllegalArgumentException
+ * When the supplied names do not identify an attribute.
+ */
+ public boolean isDeclared(String uri, String localName)
+ {
+ int index = getIndex(uri, localName);
+ return isDeclared(index);
+ }
+
+ /**
+ * SAX-ext Attributes2 method (don't invoke on parser);
+ */
+ public boolean isSpecified(int index)
+ {
+ return ((Attribute) attributesList.get(index)).specified;
+ }
+
+ /**
+ * SAX-ext Attributes2 method (don't invoke on parser);
+ */
+ public boolean isSpecified(String uri, String local)
+ {
+ int index = getIndex (uri, local);
+ return isSpecified(index);
+ }
+
+ /**
+ * SAX-ext Attributes2 method (don't invoke on parser);
+ */
+ public boolean isSpecified(String xmlName)
+ {
+ int index = getIndex (xmlName);
+ return isSpecified(index);
+ }
+
+ //
+ // Implementation of org.xml.sax.Locator.
+ //
+
+ /**
+ * SAX Locator method (don't invoke on parser);
+ */
+ public String getPublicId()
+ {
+ return null; // FIXME track public IDs too
+ }
+
+ /**
+ * SAX Locator method (don't invoke on parser);
+ */
+ public String getSystemId()
+ {
+ if (entityStack.empty())
+ {
+ return null;
+ }
+ else
+ {
+ return (String) entityStack.peek();
+ }
+ }
+
+ /**
+ * SAX Locator method (don't invoke on parser);
+ */
+ public int getLineNumber()
+ {
+ return parser.getLineNumber();
+ }
+
+ /**
+ * SAX Locator method (don't invoke on parser);
+ */
+ public int getColumnNumber()
+ {
+ return parser.getColumnNumber();
+ }
+
+ // adapter between SAX2 content handler and SAX1 document handler callbacks
+ private static class Adapter
+ implements ContentHandler
+ {
+
+ private DocumentHandler docHandler;
+
+ Adapter(DocumentHandler dh)
+ {
+ docHandler = dh;
+ }
+
+ public void setDocumentLocator(Locator l)
+ {
+ docHandler.setDocumentLocator(l);
+ }
+
+ public void startDocument()
+ throws SAXException
+ {
+ docHandler.startDocument();
+ }
+
+ public void processingInstruction(String target, String data)
+ throws SAXException
+ {
+ docHandler.processingInstruction(target, data);
+ }
+
+ public void startPrefixMapping(String prefix, String uri)
+ {
+ /* ignored */
+ }
+
+ public void startElement(String namespace,
+ String local,
+ String name,
+ Attributes attrs)
+ throws SAXException
+ {
+ docHandler.startElement(name, (AttributeList) attrs);
+ }
+
+ public void characters(char[] buf, int offset, int len)
+ throws SAXException
+ {
+ docHandler.characters(buf, offset, len);
+ }
+
+ public void ignorableWhitespace(char[] buf, int offset, int len)
+ throws SAXException
+ {
+ docHandler.ignorableWhitespace(buf, offset, len);
+ }
+
+ public void skippedEntity(String name)
+ {
+ /* ignored */
+ }
+
+ public void endElement(String u, String l, String name)
+ throws SAXException
+ {
+ docHandler.endElement(name);
+ }
+
+ public void endPrefixMapping(String prefix)
+ {
+ /* ignored */
+ }
+
+ public void endDocument()
+ throws SAXException
+ {
+ docHandler.endDocument();
+ }
+ }
+
+ private static class Attribute
+ {
+
+ String name;
+ String value;
+ String nameSpace;
+ String localName;
+ boolean specified;
+
+ Attribute(String name, String value, boolean specified)
+ {
+ this.name = name;
+ this.value = value;
+ this.nameSpace = "";
+ this.specified = specified;
+ }
+
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/aelfred2/XmlParser.java b/libjava/classpath/gnu/xml/aelfred2/XmlParser.java
new file mode 100644
index 000000000..813593d93
--- /dev/null
+++ b/libjava/classpath/gnu/xml/aelfred2/XmlParser.java
@@ -0,0 +1,5831 @@
+/* XmlParser.java --
+ Copyright (C) 1999,2000,2001 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version.
+
+Partly derived from code which carried the following notice:
+
+ Copyright (c) 1997, 1998 by Microstar Software Ltd.
+
+ AElfred is free for both commercial and non-commercial use and
+ redistribution, provided that Microstar's copyright and disclaimer are
+ retained intact. You are free to modify AElfred for your own use and
+ to redistribute AElfred with your modifications, provided that the
+ modifications are clearly documented.
+
+ This program is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ merchantability or fitness for a particular purpose. Please use it AT
+ YOUR OWN RISK.
+*/
+
+package gnu.xml.aelfred2;
+
+import gnu.java.security.action.GetPropertyAction;
+
+import java.io.BufferedInputStream;
+import java.io.CharConversionException;
+import java.io.EOFException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.io.IOException;
+import java.io.Reader;
+import java.io.UnsupportedEncodingException;
+import java.net.URL;
+import java.net.URLConnection;
+import java.security.AccessController;
+
+import java.util.Iterator;
+import java.util.HashMap;
+import java.util.LinkedList;
+
+import org.xml.sax.InputSource;
+import org.xml.sax.SAXException;
+
+
+/**
+ * Parse XML documents and return parse events through call-backs.
+ * Use the Only one thread at a time may use this parser; since it is
+ * private to this package, post-parse cleanup is done by the caller,
+ * which MUST NOT REUSE the parser (just null it).
+ *
+ * @param systemId Absolute URI of the document; should never be null,
+ * but may be so iff a reader or a stream is provided.
+ * @param publicId The public identifier of the document, or null.
+ * @param reader A character stream; must be null if stream isn't.
+ * @param stream A byte input stream; must be null if reader isn't.
+ * @param encoding The suggested encoding, or null if unknown.
+ * @exception java.lang.Exception Basically SAXException or IOException
+ */
+ // package private
+ void doParse(String systemId, String publicId, Reader reader,
+ InputStream stream, String encoding)
+ throws Exception
+ {
+ if (handler == null)
+ {
+ throw new IllegalStateException("no callback handler");
+ }
+
+ initializeVariables();
+
+ // predeclare the built-in entities here (replacement texts)
+ // we don't need to intern(), since we're guaranteed literals
+ // are always (globally) interned.
+ setInternalEntity("amp", "&");
+ setInternalEntity("lt", "<");
+ setInternalEntity("gt", ">");
+ setInternalEntity("apos", "'");
+ setInternalEntity("quot", """);
+
+ try
+ {
+ // pushURL first to ensure locator is correct in startDocument
+ // ... it might report an IO or encoding exception.
+ handler.startDocument();
+ pushURL(false, "[document]",
+ // default baseURI: null
+ new ExternalIdentifiers(publicId, systemId, null),
+ reader, stream, encoding, false);
+
+ parseDocument();
+ }
+ catch (EOFException e)
+ {
+ //empty input
+ error("empty document, with no root element.");
+ }
+ finally
+ {
+ if (reader != null)
+ {
+ try
+ {
+ reader.close();
+ }
+ catch (IOException e)
+ {
+ /* ignore */
+ }
+ }
+ if (stream != null)
+ {
+ try
+ {
+ stream.close();
+ }
+ catch (IOException e)
+ {
+ /* ignore */
+ }
+ }
+ if (is != null)
+ {
+ try
+ {
+ is.close();
+ }
+ catch (IOException e)
+ {
+ /* ignore */
+ }
+ }
+ scratch = null;
+ }
+ }
+
+ //////////////////////////////////////////////////////////////////////
+ // Error reporting.
+ //////////////////////////////////////////////////////////////////////
+
+ /**
+ * Report an error.
+ * @param message The error message.
+ * @param textFound The text that caused the error (or null).
+ * @see SAXDriver#error
+ * @see #line
+ */
+ private void error(String message, String textFound, String textExpected)
+ throws SAXException
+ {
+ if (textFound != null)
+ {
+ message = message + " (found \"" + textFound + "\")";
+ }
+ if (textExpected != null)
+ {
+ message = message + " (expected \"" + textExpected + "\")";
+ }
+ handler.fatal(message);
+
+ // "can't happen"
+ throw new SAXException(message);
+ }
+
+ /**
+ * Report a serious error.
+ * @param message The error message.
+ * @param textFound The text that caused the error (or null).
+ */
+ private void error(String message, char textFound, String textExpected)
+ throws SAXException
+ {
+ error(message, Character.toString(textFound), textExpected);
+ }
+
+ /**
+ * Report typical case fatal errors.
+ */
+ private void error(String message)
+ throws SAXException
+ {
+ handler.fatal(message);
+ }
+
+ //////////////////////////////////////////////////////////////////////
+ // Major syntactic productions.
+ //////////////////////////////////////////////////////////////////////
+
+ /**
+ * Parse an XML document.
+ * This is the top-level parsing function for a single XML
+ * document. As a minimum, a well-formed document must have
+ * a document element, and a valid document must have a prolog
+ * (one with doctype) as well.
+ */
+ private void parseDocument()
+ throws Exception
+ {
+ try
+ { // added by MHK
+ boolean sawDTD = parseProlog();
+ require('<');
+ parseElement(!sawDTD);
+ }
+ catch (EOFException ee)
+ { // added by MHK
+ error("premature end of file", "[EOF]", null);
+ }
+
+ try
+ {
+ parseMisc(); //skip all white, PIs, and comments
+ char c = readCh(); //if this doesn't throw an exception...
+ error("unexpected characters after document end", c, null);
+ }
+ catch (EOFException e)
+ {
+ return;
+ }
+ }
+
+ static final char[] startDelimComment = { '<', '!', '-', '-' };
+ static final char[] endDelimComment = { '-', '-' };
+
+ /**
+ * Skip a comment.
+ * (The (The (The '<![CDATA[' has already been read.)
+ */
+ private void parseCDSect()
+ throws Exception
+ {
+ parseUntil(endDelimCDATA);
+ dataBufferFlush();
+ }
+
+ /**
+ * Parse the prolog of an XML document.
+ * We do not look for the XML declaration here, because it was
+ * handled by pushURL ().
+ * @see pushURL
+ * @return true if a DTD was read.
+ */
+ private boolean parseProlog()
+ throws Exception
+ {
+ parseMisc();
+
+ if (tryRead("
+ * [23] XMLDecl ::= '<?xml' VersionInfo EncodingDecl? SDDecl? S? '?>'
+ * [24] VersionInfo ::= S 'version' Eq
+ * ("'" VersionNum "'" | '"' VersionNum '"' )
+ * [26] VersionNum ::= ([a-zA-Z0-9_.:] | '-')*
+ * [32] SDDecl ::= S 'standalone' Eq
+ * ( "'"" ('yes' | 'no') "'"" | '"' ("yes" | "no") '"' )
+ * [80] EncodingDecl ::= S 'encoding' Eq
+ * ( "'" EncName "'" | "'" EncName "'" )
+ * [81] EncName ::= [A-Za-z] ([A-Za-z0-9._] | '-')*
+ *
+ * (The (The It is also used after autodetection, at which point only very
+ * limited adjustments to the encoding may be used (switching between
+ * related builtin decoders).
+ *
+ * @param encodingName The name of the encoding specified by the user.
+ * @exception IOException if the encoding isn't supported either
+ * internally to this parser, or by the hosting JVM.
+ * @see #parseXMLDecl
+ * @see #parseTextDecl
+ */
+ private void setupDecoding(String encodingName)
+ throws SAXException, IOException
+ {
+ encodingName = encodingName.toUpperCase();
+
+ // ENCODING_EXTERNAL indicates an encoding that wasn't
+ // autodetected ... we can use builtin decoders, or
+ // ones from the JVM (InputStreamReader).
+
+ // Otherwise we can only tweak what was autodetected, and
+ // only for single byte (ASCII derived) builtin encodings.
+
+ // ASCII-derived encodings
+ if (encoding == ENCODING_UTF_8 || encoding == ENCODING_EXTERNAL)
+ {
+ if (encodingName.equals("ISO-8859-1")
+ || encodingName.equals("8859_1")
+ || encodingName.equals("ISO8859_1"))
+ {
+ encoding = ENCODING_ISO_8859_1;
+ return;
+ }
+ else if (encodingName.equals("US-ASCII")
+ || encodingName.equals("ASCII"))
+ {
+ encoding = ENCODING_ASCII;
+ return;
+ }
+ else if (encodingName.equals("UTF-8")
+ || encodingName.equals("UTF8"))
+ {
+ encoding = ENCODING_UTF_8;
+ return;
+ }
+ else if (encoding != ENCODING_EXTERNAL)
+ {
+ // used to start with a new reader ...
+ throw new UnsupportedEncodingException(encodingName);
+ }
+ // else fallthrough ...
+ // it's ASCII-ish and something other than a builtin
+ }
+
+ // Unicode and such
+ if (encoding == ENCODING_UCS_2_12 || encoding == ENCODING_UCS_2_21)
+ {
+ if (!(encodingName.equals("ISO-10646-UCS-2")
+ || encodingName.equals("UTF-16")
+ || encodingName.equals("UTF-16BE")
+ || encodingName.equals("UTF-16LE")))
+ {
+ error("unsupported Unicode encoding", encodingName, "UTF-16");
+ }
+ return;
+ }
+
+ // four byte encodings
+ if (encoding == ENCODING_UCS_4_1234
+ || encoding == ENCODING_UCS_4_4321
+ || encoding == ENCODING_UCS_4_2143
+ || encoding == ENCODING_UCS_4_3412)
+ {
+ // Strictly: "UCS-4" == "UTF-32BE"; also, "UTF-32LE" exists
+ if (!encodingName.equals("ISO-10646-UCS-4"))
+ {
+ error("unsupported 32-bit encoding", encodingName,
+ "ISO-10646-UCS-4");
+ }
+ return;
+ }
+
+ // assert encoding == ENCODING_EXTERNAL
+ // if (encoding != ENCODING_EXTERNAL)
+ // throw new RuntimeException ("encoding = " + encoding);
+
+ if (encodingName.equals("UTF-16BE"))
+ {
+ encoding = ENCODING_UCS_2_12;
+ return;
+ }
+ if (encodingName.equals("UTF-16LE"))
+ {
+ encoding = ENCODING_UCS_2_21;
+ return;
+ }
+
+ // We couldn't use the builtin decoders at all. But we can try to
+ // create a reader, since we haven't messed up buffering. Tweak
+ // the encoding name if necessary.
+
+ if (encodingName.equals("UTF-16")
+ || encodingName.equals("ISO-10646-UCS-2"))
+ {
+ encodingName = "Unicode";
+ }
+ // Ignoring all the EBCDIC aliases here
+
+ reader = new InputStreamReader(is, encodingName);
+ sourceType = INPUT_READER;
+ }
+
+ /**
+ * Parse miscellaneous markup outside the document element and DOCTYPE
+ * declaration.
+ * (The Reading toplevel PE references is handled as a lexical issue
+ * by the caller, as is whitespace.
+ */
+ private void parseMarkupdecl()
+ throws Exception
+ {
+ char[] saved = null;
+ boolean savedPE = expandPE;
+
+ // prevent "<%foo;" and ensures saved entity is right
+ require('<');
+ unread('<');
+ expandPE = false;
+
+ if (tryRead(" 0)
+ {
+ parseConditionalSect(saved);
+ }
+ else
+ {
+ error("conditional sections illegal in internal subset");
+ }
+ }
+ else
+ {
+ error("expected markup declaration");
+ }
+
+ // VC: Proper Decl/PE Nesting
+ if (readBuffer != saved)
+ {
+ handler.verror("Illegal Declaration/PE nesting");
+ }
+ }
+
+ /**
+ * Parse an element, with its tags.
+ * (The '<' has already been read.)
+ * NOTE: this method actually chains onto parseContent (), if necessary,
+ * and parseContent () will take care of calling parseETag ().
+ */
+ private void parseElement(boolean maybeGetSubset)
+ throws Exception
+ {
+ String gi;
+ char c;
+ int oldElementContent = currentElementContent;
+ String oldElement = currentElement;
+ ElementDecl element;
+
+ // This is the (global) counter for the
+ // array of specified attributes.
+ tagAttributePos = 0;
+
+ // Read the element type name.
+ gi = readNmtoken(true);
+
+ // If we saw no DTD, and this is the document root element,
+ // let the application modify the input stream by providing one.
+ if (maybeGetSubset)
+ {
+ InputSource subset = handler.getExternalSubset(gi,
+ handler.getSystemId());
+ if (subset != null)
+ {
+ String publicId = subset.getPublicId();
+ String systemId = subset.getSystemId();
+
+ handler.warn("modifying document by adding DTD");
+ handler.doctypeDecl(gi, publicId, systemId);
+ pushString(null, ">");
+
+ // NOTE: [dtd] is so we say what SAX2 expects,
+ // though it's misleading (subset, not entire dtd)
+ pushURL(true, "[dtd]",
+ new ExternalIdentifiers(publicId, systemId, null),
+ subset.getCharacterStream(),
+ subset.getByteStream(),
+ subset.getEncoding(),
+ false);
+
+ // Loop until we end up back at '>'
+ while (true)
+ {
+ doReport = expandPE = true;
+ skipWhitespace();
+ doReport = expandPE = false;
+ if (tryRead('>'))
+ {
+ break;
+ }
+ else
+ {
+ expandPE = true;
+ parseMarkupdecl();
+ expandPE = false;
+ }
+ }
+
+ // the ">" string isn't popped yet
+ if (inputStack.size() != 1)
+ {
+ error("external subset has unmatched '>'");
+ }
+
+ handler.endDoctype();
+ }
+ }
+
+ // Determine the current content type.
+ currentElement = gi;
+ element = (ElementDecl) elementInfo.get(gi);
+ currentElementContent = getContentType(element, CONTENT_ANY);
+
+ // Read the attributes, if any.
+ // After this loop, "c" is the closing delimiter.
+ boolean white = tryWhitespace();
+ c = readCh();
+ while (c != '/' && c != '>')
+ {
+ unread(c);
+ if (!white)
+ {
+ error("need whitespace between attributes");
+ }
+ parseAttribute(gi);
+ white = tryWhitespace();
+ c = readCh();
+ }
+
+ // Supply any defaulted attributes.
+ Iterator atts = declaredAttributes(element);
+ if (atts != null)
+ {
+ String aname;
+loop:
+ while (atts.hasNext())
+ {
+ aname = (String) atts.next();
+ // See if it was specified.
+ for (int i = 0; i < tagAttributePos; i++)
+ {
+ if (tagAttributes[i] == aname)
+ {
+ continue loop;
+ }
+ }
+ // ... or has a default
+ String value = getAttributeDefaultValue(gi, aname);
+
+ if (value == null)
+ {
+ continue;
+ }
+ handler.attribute(aname, value, false);
+ }
+ }
+
+ // Figure out if this is a start tag
+ // or an empty element, and dispatch an
+ // event accordingly.
+ switch (c)
+ {
+ case '>':
+ handler.startElement(gi);
+ parseContent();
+ break;
+ case '/':
+ require('>');
+ handler.startElement(gi);
+ handler.endElement(gi);
+ break;
+ }
+
+ // Restore the previous state.
+ currentElement = oldElement;
+ currentElementContent = oldElementContent;
+ }
+
+ /**
+ * Parse an attribute assignment.
+ * NOTE: parseContent () chains to here, we already read the
+ * "</".
+ */
+ private void parseETag()
+ throws Exception
+ {
+ require(currentElement);
+ skipWhitespace();
+ require('>');
+ handler.endElement(currentElement);
+ // not re-reporting any SAXException re bogus end tags,
+ // even though that diagnostic might be clearer ...
+ }
+
+ /**
+ * Parse the content of an element.
+ * NOTE: consumes ETtag.
+ */
+ private void parseContent()
+ throws Exception
+ {
+ char c;
+
+ while (true)
+ {
+ // consume characters (or ignorable whitspace) until delimiter
+ parseCharData();
+
+ // Handle delimiters
+ c = readCh();
+ switch (c)
+ {
+ case '&': // Found "&"
+ c = readCh();
+ if (c == '#')
+ {
+ parseCharRef();
+ }
+ else
+ {
+ unread(c);
+ parseEntityRef(true);
+ }
+ isDirtyCurrentElement = true;
+ break;
+
+ case '<': // Found "<"
+ dataBufferFlush();
+ c = readCh();
+ switch (c)
+ {
+ case '!': // Found "
+ * [45] elementdecl ::= '<!ELEMENT' S Name S contentspec S? '>'
+ *
+ * NOTE: the '<!ELEMENT' has already been read.
+ */
+ private void parseElementDecl()
+ throws Exception
+ {
+ String name;
+
+ requireWhitespace();
+ // Read the element type name.
+ name = readNmtoken(true);
+
+ requireWhitespace();
+ // Read the content model.
+ parseContentspec(name);
+
+ skipWhitespace();
+ require('>');
+ }
+
+ /**
+ * Content specification.
+ * NOTE: the opening '(' and S have already been read.
+ *
+ * @param saved Buffer for entity that should have the terminal ')'
+ */
+ private void parseElements(char[] saved)
+ throws Exception
+ {
+ char c;
+ char sep;
+
+ // Parse the first content particle
+ skipWhitespace();
+ parseCp();
+
+ // Check for end or for a separator.
+ skipWhitespace();
+ c = readCh();
+ switch (c)
+ {
+ case ')':
+ // VC: Proper Group/PE Nesting
+ if (readBuffer != saved)
+ {
+ handler.verror("Illegal Group/PE nesting");
+ }
+
+ dataBufferAppend(')');
+ c = readCh();
+ switch (c)
+ {
+ case '*':
+ case '+':
+ case '?':
+ dataBufferAppend(c);
+ break;
+ default:
+ unread(c);
+ }
+ return;
+ case ',': // Register the separator.
+ case '|':
+ sep = c;
+ dataBufferAppend(c);
+ break;
+ default:
+ error("bad separator in content model", c, null);
+ return;
+ }
+
+ // Parse the rest of the content model.
+ while (true)
+ {
+ skipWhitespace();
+ parseCp();
+ skipWhitespace();
+ c = readCh();
+ if (c == ')')
+ {
+ // VC: Proper Group/PE Nesting
+ if (readBuffer != saved)
+ {
+ handler.verror("Illegal Group/PE nesting");
+ }
+
+ dataBufferAppend(')');
+ break;
+ }
+ else if (c != sep)
+ {
+ error("bad separator in content model", c, null);
+ return;
+ }
+ else
+ {
+ dataBufferAppend(c);
+ }
+ }
+
+ // Check for the occurrence indicator.
+ c = readCh();
+ switch (c)
+ {
+ case '?':
+ case '*':
+ case '+':
+ dataBufferAppend(c);
+ return;
+ default:
+ unread(c);
+ return;
+ }
+ }
+
+ /**
+ * Parse a content particle.
+ * NOTE: the '<!ATTLIST' has already been read.
+ */
+ private void parseAttlistDecl()
+ throws Exception
+ {
+ String elementName;
+
+ requireWhitespace();
+ elementName = readNmtoken(true);
+ boolean white = tryWhitespace();
+ while (!tryRead('>'))
+ {
+ if (!white)
+ {
+ error("whitespace required before attribute definition");
+ }
+ parseAttDef(elementName);
+ white = tryWhitespace();
+ }
+ }
+
+ /**
+ * Parse a single attribute definition.
+ * NOTE: the '(' has already been read.
+ */
+ private void parseEnumeration(boolean isNames)
+ throws Exception
+ {
+ dataBufferAppend('(');
+
+ // Read the first token.
+ skipWhitespace();
+ dataBufferAppend(readNmtoken(isNames));
+ // Read the remaining tokens.
+ skipWhitespace();
+ while (!tryRead(')'))
+ {
+ require('|');
+ dataBufferAppend('|');
+ skipWhitespace();
+ dataBufferAppend(readNmtoken (isNames));
+ skipWhitespace();
+ }
+ dataBufferAppend(')');
+ }
+
+ /**
+ * Parse a notation type for an attribute.
+ * NOTE: the 'NOTATION' has already been read
+ */
+ private void parseNotationType()
+ throws Exception
+ {
+ requireWhitespace();
+ require('(');
+
+ parseEnumeration(true);
+ }
+
+ /**
+ * Parse the default value for an attribute.
+ * NOTE: the '>![' has already been read.
+ */
+ private void parseConditionalSect(char[] saved)
+ throws Exception
+ {
+ skipWhitespace();
+ if (tryRead("INCLUDE"))
+ {
+ skipWhitespace();
+ require('[');
+ // VC: Proper Conditional Section/PE Nesting
+ if (readBuffer != saved)
+ {
+ handler.verror("Illegal Conditional Section/PE nesting");
+ }
+ skipWhitespace();
+ while (!tryRead("]]>"))
+ {
+ parseMarkupdecl();
+ skipWhitespace();
+ }
+ }
+ else if (tryRead("IGNORE"))
+ {
+ skipWhitespace();
+ require('[');
+ // VC: Proper Conditional Section/PE Nesting
+ if (readBuffer != saved)
+ {
+ handler.verror("Illegal Conditional Section/PE nesting");
+ }
+ int nesting = 1;
+ char c;
+ expandPE = false;
+ for (int nest = 1; nest > 0; )
+ {
+ c = readCh();
+ switch (c)
+ {
+ case '<':
+ if (tryRead("!["))
+ {
+ nest++;
+ }
+ break;
+ case ']':
+ if (tryRead("]>"))
+ {
+ nest--;
+ }
+ }
+ }
+ expandPE = true;
+ }
+ else
+ {
+ error("conditional section must begin with INCLUDE or IGNORE");
+ }
+ }
+
+ private void parseCharRef()
+ throws SAXException, IOException
+ {
+ parseCharRef(true /* do flushDataBuffer by default */);
+ }
+
+ /**
+ * Try to read a character reference without consuming data from buffer.
+ * NOTE: the '' has already been read.
+ */
+ private void tryReadCharRef()
+ throws SAXException, IOException
+ {
+ int value = 0;
+ char c;
+
+ if (tryRead('x'))
+ {
+loop1:
+ while (true)
+ {
+ c = readCh();
+ if (c == ';')
+ {
+ break loop1;
+ }
+ else
+ {
+ int n = Character.digit(c, 16);
+ if (n == -1)
+ {
+ error("illegal character in character reference", c, null);
+ break loop1;
+ }
+ value *= 16;
+ value += n;
+ }
+ }
+ }
+ else
+ {
+loop2:
+ while (true)
+ {
+ c = readCh();
+ if (c == ';')
+ {
+ break loop2;
+ }
+ else
+ {
+ int n = Character.digit(c, 10);
+ if (n == -1)
+ {
+ error("illegal character in character reference", c, null);
+ break loop2;
+ }
+ value *= 10;
+ value += n;
+ }
+ }
+ }
+
+ // check for character refs being legal XML
+ if ((value < 0x0020
+ && ! (value == '\n' || value == '\t' || value == '\r'))
+ || (value >= 0xD800 && value <= 0xDFFF)
+ || value == 0xFFFE || value == 0xFFFF
+ || value > 0x0010ffff)
+ {
+ error("illegal XML character reference U+"
+ + Integer.toHexString(value));
+ }
+
+ // Check for surrogates: 00000000 0000xxxx yyyyyyyy zzzzzzzz
+ // (1101|10xx|xxyy|yyyy + 1101|11yy|zzzz|zzzz:
+ if (value > 0x0010ffff)
+ {
+ // too big for surrogate
+ error("character reference " + value + " is too large for UTF-16",
+ Integer.toString(value), null);
+ }
+
+ }
+
+ /**
+ * Read and interpret a character reference.
+ * NOTE: the '' has already been read.
+ */
+ private void parseCharRef(boolean doFlush)
+ throws SAXException, IOException
+ {
+ int value = 0;
+ char c;
+
+ if (tryRead('x'))
+ {
+loop1:
+ while (true)
+ {
+ c = readCh();
+ if (c == ';')
+ {
+ break loop1;
+ }
+ else
+ {
+ int n = Character.digit(c, 16);
+ if (n == -1)
+ {
+ error("illegal character in character reference", c, null);
+ break loop1;
+ }
+ value *= 16;
+ value += n;
+ }
+ }
+ }
+ else
+ {
+loop2:
+ while (true)
+ {
+ c = readCh();
+ if (c == ';')
+ {
+ break loop2;
+ }
+ else
+ {
+ int n = Character.digit(c, 10);
+ if (n == -1)
+ {
+ error("illegal character in character reference", c, null);
+ break loop2;
+ }
+ value *= 10;
+ value += c - '0';
+ }
+ }
+ }
+
+ // check for character refs being legal XML
+ if ((value < 0x0020
+ && ! (value == '\n' || value == '\t' || value == '\r'))
+ || (value >= 0xD800 && value <= 0xDFFF)
+ || value == 0xFFFE || value == 0xFFFF
+ || value > 0x0010ffff)
+ {
+ error("illegal XML character reference U+"
+ + Integer.toHexString(value));
+ }
+
+ // Check for surrogates: 00000000 0000xxxx yyyyyyyy zzzzzzzz
+ // (1101|10xx|xxyy|yyyy + 1101|11yy|zzzz|zzzz:
+ if (value <= 0x0000ffff)
+ {
+ // no surrogates needed
+ dataBufferAppend((char) value);
+ }
+ else if (value <= 0x0010ffff)
+ {
+ value -= 0x10000;
+ // > 16 bits, surrogate needed
+ dataBufferAppend((char) (0xd800 | (value >> 10)));
+ dataBufferAppend((char) (0xdc00 | (value & 0x0003ff)));
+ }
+ else
+ {
+ // too big for surrogate
+ error("character reference " + value + " is too large for UTF-16",
+ Integer.toString(value), null);
+ }
+ if (doFlush)
+ {
+ dataBufferFlush();
+ }
+ }
+
+ /**
+ * Parse and expand an entity reference.
+ * NOTE: the '&' has already been read.
+ * @param externalAllowed External entities are allowed here.
+ */
+ private void parseEntityRef(boolean externalAllowed)
+ throws SAXException, IOException
+ {
+ String name;
+
+ name = readNmtoken(true);
+ require(';');
+ switch (getEntityType(name))
+ {
+ case ENTITY_UNDECLARED:
+ // NOTE: XML REC describes amazingly convoluted handling for
+ // this case. Nothing as meaningful as being a WFness error
+ // unless the processor might _legitimately_ not have seen a
+ // declaration ... which is what this implements.
+ String message;
+
+ message = "reference to undeclared general entity " + name;
+ if (skippedPE && !docIsStandalone)
+ {
+ handler.verror(message);
+ // we don't know this entity, and it might be external...
+ if (externalAllowed)
+ {
+ handler.skippedEntity(name);
+ }
+ }
+ else
+ {
+ error(message);
+ }
+ break;
+ case ENTITY_INTERNAL:
+ pushString(name, getEntityValue(name));
+
+ //workaround for possible input pop before marking
+ //the buffer reading position
+ char t = readCh();
+ unread(t);
+ int bufferPosMark = readBufferPos;
+
+ int end = readBufferPos + getEntityValue(name).length();
+ for (int k = readBufferPos; k < end; k++)
+ {
+ t = readCh();
+ if (t == '&')
+ {
+ t = readCh();
+ if (t == '#')
+ {
+ //try to match a character ref
+ tryReadCharRef();
+
+ //everything has been read
+ if (readBufferPos >= end)
+ {
+ break;
+ }
+ k = readBufferPos;
+ continue;
+ }
+ else if (Character.isLetter(t))
+ {
+ //looks like an entity ref
+ unread(t);
+ readNmtoken(true);
+ require(';');
+
+ //everything has been read
+ if (readBufferPos >= end)
+ {
+ break;
+ }
+ k = readBufferPos;
+ continue;
+ }
+ error(" malformed entity reference");
+ }
+
+ }
+ readBufferPos = bufferPosMark;
+ break;
+ case ENTITY_TEXT:
+ if (externalAllowed)
+ {
+ pushURL(false, name, getEntityIds(name),
+ null, null, null, true);
+ }
+ else
+ {
+ error("reference to external entity in attribute value.",
+ name, null);
+ }
+ break;
+ case ENTITY_NDATA:
+ if (externalAllowed)
+ {
+ error("unparsed entity reference in content", name, null);
+ }
+ else
+ {
+ error("reference to external entity in attribute value.",
+ name, null);
+ }
+ break;
+ default:
+ throw new RuntimeException();
+ }
+ }
+
+ /**
+ * Parse and expand a parameter entity reference.
+ * NOTE: the '%' has already been read.
+ */
+ private void parsePEReference()
+ throws SAXException, IOException
+ {
+ String name;
+
+ name = "%" + readNmtoken(true);
+ require(';');
+ switch (getEntityType(name))
+ {
+ case ENTITY_UNDECLARED:
+ // VC: Entity Declared
+ handler.verror("reference to undeclared parameter entity " + name);
+
+ // we should disable handling of all subsequent declarations
+ // unless this is a standalone document (info discarded)
+ break;
+ case ENTITY_INTERNAL:
+ if (inLiteral)
+ {
+ pushString(name, getEntityValue(name));
+ }
+ else
+ {
+ pushString(name, ' ' + getEntityValue(name) + ' ');
+ }
+ break;
+ case ENTITY_TEXT:
+ if (!inLiteral)
+ {
+ pushString(null, " ");
+ }
+ pushURL(true, name, getEntityIds(name), null, null, null, true);
+ if (!inLiteral)
+ {
+ pushString(null, " ");
+ }
+ break;
+ }
+ }
+
+ /**
+ * Parse an entity declaration.
+ * NOTE: the '<!ENTITY' has already been read.
+ */
+ private void parseEntityDecl()
+ throws Exception
+ {
+ boolean peFlag = false;
+ int flags = 0;
+
+ // Check for a parameter entity.
+ expandPE = false;
+ requireWhitespace();
+ if (tryRead('%'))
+ {
+ peFlag = true;
+ requireWhitespace();
+ }
+ expandPE = true;
+
+ // Read the entity name, and prepend
+ // '%' if necessary.
+ String name = readNmtoken(true);
+ //NE08
+ if (name.indexOf(':') >= 0)
+ {
+ error("Illegal character(':') in entity name ", name, null);
+ }
+ if (peFlag)
+ {
+ name = "%" + name;
+ }
+
+ // Read the entity value.
+ requireWhitespace();
+ char c = readCh();
+ unread (c);
+ if (c == '"' || c == '\'')
+ {
+ // Internal entity ... replacement text has expanded refs
+ // to characters and PEs, but not to general entities
+ String value = readLiteral(flags);
+ setInternalEntity(name, value);
+ }
+ else
+ {
+ // Read the external IDs
+ ExternalIdentifiers ids = readExternalIds(false, false);
+
+ // Check for NDATA declaration.
+ boolean white = tryWhitespace();
+ if (!peFlag && tryRead("NDATA"))
+ {
+ if (!white)
+ {
+ error("whitespace required before NDATA");
+ }
+ requireWhitespace();
+ String notationName = readNmtoken(true);
+ if (!skippedPE)
+ {
+ setExternalEntity(name, ENTITY_NDATA, ids, notationName);
+ handler.unparsedEntityDecl(name, ids.publicId, ids.systemId,
+ ids.baseUri, notationName);
+ }
+ }
+ else if (!skippedPE)
+ {
+ setExternalEntity(name, ENTITY_TEXT, ids, null);
+ handler.getDeclHandler()
+ .externalEntityDecl(name, ids.publicId,
+ handler.resolveURIs()
+ // FIXME: ASSUMES not skipped
+ // "false" forces error on bad URI
+ ? handler.absolutize(ids.baseUri,
+ ids.systemId,
+ false)
+ : ids.systemId);
+ }
+ }
+
+ // Finish the declaration.
+ skipWhitespace();
+ require('>');
+ }
+
+ /**
+ * Parse a notation declaration.
+ * NOTE: the '<!NOTATION' has already been read.
+ */
+ private void parseNotationDecl()
+ throws Exception
+ {
+ String nname;
+ ExternalIdentifiers ids;
+
+ requireWhitespace();
+ nname = readNmtoken(true);
+ //NE08
+ if (nname.indexOf(':') >= 0)
+ {
+ error("Illegal character(':') in notation name ", nname, null);
+ }
+ requireWhitespace();
+
+ // Read the external identifiers.
+ ids = readExternalIds(true, false);
+
+ // Register the notation.
+ setNotation(nname, ids);
+
+ skipWhitespace();
+ require('>');
+ }
+
+ /**
+ * Parse character data.
+ * Precondition: Entity expansion is not required.
+ * Precondition: data buffer has no characters that
+ * will get sent to the application.
+ */
+ private void require(String delim)
+ throws SAXException, IOException
+ {
+ int length = delim.length();
+ char[] ch;
+
+ if (length < dataBuffer.length)
+ {
+ ch = dataBuffer;
+ delim.getChars(0, length, ch, 0);
+ }
+ else
+ {
+ ch = delim.toCharArray();
+ }
+
+ if (USE_CHEATS && length <= (readBufferLength - readBufferPos))
+ {
+ int offset = readBufferPos;
+
+ for (int i = 0; i < length; i++, offset++)
+ {
+ if (ch[i] != readBuffer[offset])
+ {
+ error ("required string", null, delim);
+ }
+ }
+ readBufferPos = offset;
+
+ }
+ else
+ {
+ for (int i = 0; i < length; i++)
+ {
+ require(ch[i]);
+ }
+ }
+ }
+
+ /**
+ * Require a character to appear, or throw an exception.
+ */
+ private void require(char delim)
+ throws SAXException, IOException
+ {
+ char c = readCh();
+
+ if (c != delim)
+ {
+ error("required character", c, Character.toString(delim));
+ }
+ }
+
+ /**
+ * Create an interned string from a character array.
+ * Ælfred uses this method to create an interned version
+ * of all names and name tokens, so that it can test equality
+ * with This is much more efficient than constructing a non-interned
+ * string first, and then interning it.
+ *
+ * @param ch an array of characters for building the string.
+ * @param start the starting position in the array.
+ * @param length the number of characters to place in the string.
+ * @return an interned string.
+ * @see #intern (String)
+ * @see java.lang.String#intern
+ */
+ public String intern(char[] ch, int start, int length)
+ {
+ int index = 0;
+ int hash = 0;
+ Object[] bucket;
+
+ // Generate a hash code. This is a widely used string hash,
+ // often attributed to Brian Kernighan.
+ for (int i = start; i < start + length; i++)
+ {
+ hash = 31 * hash + ch[i];
+ }
+ hash = (hash & 0x7fffffff) % SYMBOL_TABLE_LENGTH;
+
+ // Get the bucket -- consists of {array,String} pairs
+ if ((bucket = symbolTable[hash]) == null)
+ {
+ // first string in this bucket
+ bucket = new Object[8];
+
+ // Search for a matching tuple, and
+ // return the string if we find one.
+ }
+ else
+ {
+ while (index < bucket.length)
+ {
+ char[] chFound = (char[]) bucket[index];
+
+ // Stop when we hit an empty entry.
+ if (chFound == null)
+ {
+ break;
+ }
+
+ // If they're the same length, check for a match.
+ if (chFound.length == length)
+ {
+ for (int i = 0; i < chFound.length; i++)
+ {
+ // continue search on failure
+ if (ch[start + i] != chFound[i])
+ {
+ break;
+ }
+ else if (i == length - 1)
+ {
+ // That's it, we have a match!
+ return (String) bucket[index + 1];
+ }
+ }
+ }
+ index += 2;
+ }
+ // Not found -- we'll have to add it.
+
+ // Do we have to grow the bucket?
+ bucket = (Object[]) extendArray(bucket, bucket.length, index);
+ }
+ symbolTable[hash] = bucket;
+
+ // OK, add it to the end of the bucket -- "local" interning.
+ // Intern "globally" to let applications share interning benefits.
+ // That is, "!=" and "==" work on our strings, not just equals().
+ String s = new String(ch, start, length).intern();
+ bucket[index] = s.toCharArray();
+ bucket[index + 1] = s;
+ return s;
+ }
+
+ /**
+ * Ensure the capacity of an array, allocating a new one if
+ * necessary. Usually extends only for name hash collisions.
+ */
+ private Object extendArray(Object array, int currentSize, int requiredSize)
+ {
+ if (requiredSize < currentSize)
+ {
+ return array;
+ }
+ else
+ {
+ Object newArray = null;
+ int newSize = currentSize * 2;
+
+ if (newSize <= requiredSize)
+ {
+ newSize = requiredSize + 1;
+ }
+
+ if (array instanceof char[])
+ {
+ newArray = new char[newSize];
+ }
+ else if (array instanceof Object[])
+ {
+ newArray = new Object[newSize];
+ }
+ else
+ {
+ throw new RuntimeException();
+ }
+
+ System.arraycopy(array, 0, newArray, 0, currentSize);
+ return newArray;
+ }
+ }
+
+ //////////////////////////////////////////////////////////////////////
+ // XML query routines.
+ //////////////////////////////////////////////////////////////////////
+
+ boolean isStandalone()
+ {
+ return docIsStandalone;
+ }
+
+ //
+ // Elements
+ //
+
+ private int getContentType(ElementDecl element, int defaultType)
+ {
+ int retval;
+
+ if (element == null)
+ {
+ return defaultType;
+ }
+ retval = element.contentType;
+ if (retval == CONTENT_UNDECLARED)
+ {
+ retval = defaultType;
+ }
+ return retval;
+ }
+
+ /**
+ * Look up the content type of an element.
+ * @param name The element type name.
+ * @return An integer constant representing the content type.
+ * @see #CONTENT_UNDECLARED
+ * @see #CONTENT_ANY
+ * @see #CONTENT_EMPTY
+ * @see #CONTENT_MIXED
+ * @see #CONTENT_ELEMENTS
+ */
+ public int getElementContentType(String name)
+ {
+ ElementDecl element = (ElementDecl) elementInfo.get(name);
+ return getContentType(element, CONTENT_UNDECLARED);
+ }
+
+ /**
+ * Register an element.
+ * Array format:
+ * [0] element type name
+ * [1] content model (mixed, elements only)
+ * [2] attribute hash table
+ */
+ private void setElement(String name, int contentType,
+ String contentModel, HashMap attributes)
+ throws SAXException
+ {
+ if (skippedPE)
+ {
+ return;
+ }
+
+ ElementDecl element = (ElementDecl) elementInfo.get(name);
+
+ // first or for this type?
+ if (element == null)
+ {
+ element = new ElementDecl();
+ element.contentType = contentType;
+ element.contentModel = contentModel;
+ element.attributes = attributes;
+ elementInfo.put(name, element);
+ return;
+ }
+
+ // declaration?
+ if (contentType != CONTENT_UNDECLARED)
+ {
+ // ... following an associated
+ if (element.contentType == CONTENT_UNDECLARED)
+ {
+ element.contentType = contentType;
+ element.contentModel = contentModel;
+ }
+ else
+ {
+ // VC: Unique Element Type Declaration
+ handler.verror("multiple declarations for element type: "
+ + name);
+ }
+ }
+
+ // first , before ?
+ else if (attributes != null)
+ {
+ element.attributes = attributes;
+ }
+ }
+
+ /**
+ * Look up the attribute hash table for an element.
+ * The hash table is the second item in the element array.
+ */
+ private HashMap getElementAttributes(String name)
+ {
+ ElementDecl element = (ElementDecl) elementInfo.get(name);
+ return (element == null) ? null : element.attributes;
+ }
+
+ //
+ // Attributes
+ //
+
+ /**
+ * Get the declared attributes for an element type.
+ * @param elname The name of the element type.
+ * @return An iterator over all the attributes declared for
+ * a specific element type. The results will be valid only
+ * after the DTD (if any) has been parsed.
+ * @see #getAttributeType
+ * @see #getAttributeEnumeration
+ * @see #getAttributeDefaultValueType
+ * @see #getAttributeDefaultValue
+ * @see #getAttributeExpandedValue
+ */
+ private Iterator declaredAttributes(ElementDecl element)
+ {
+ HashMap attlist;
+
+ if (element == null)
+ {
+ return null;
+ }
+ if ((attlist = element.attributes) == null)
+ {
+ return null;
+ }
+ return attlist.keySet().iterator();
+ }
+
+ /**
+ * Get the declared attributes for an element type.
+ * @param elname The name of the element type.
+ * @return An iterator over all the attributes declared for
+ * a specific element type. The results will be valid only
+ * after the DTD (if any) has been parsed.
+ * @see #getAttributeType
+ * @see #getAttributeEnumeration
+ * @see #getAttributeDefaultValueType
+ * @see #getAttributeDefaultValue
+ * @see #getAttributeExpandedValue
+ */
+ public Iterator declaredAttributes(String elname)
+ {
+ return declaredAttributes((ElementDecl) elementInfo.get(elname));
+ }
+
+ /**
+ * Retrieve the declared type of an attribute.
+ * @param name The name of the associated element.
+ * @param aname The name of the attribute.
+ * @return An interend string denoting the type, or null
+ * indicating an undeclared attribute.
+ */
+ public String getAttributeType(String name, String aname)
+ {
+ AttributeDecl attribute = getAttribute(name, aname);
+ return (attribute == null) ? null : attribute.type;
+ }
+
+ /**
+ * Retrieve the allowed values for an enumerated attribute type.
+ * @param name The name of the associated element.
+ * @param aname The name of the attribute.
+ * @return A string containing the token list.
+ */
+ public String getAttributeEnumeration(String name, String aname)
+ {
+ AttributeDecl attribute = getAttribute(name, aname);
+ // assert: attribute.enumeration is "ENUMERATION" or "NOTATION"
+ return (attribute == null) ? null : attribute.enumeration;
+ }
+
+ /**
+ * Retrieve the default value of a declared attribute.
+ * @param name The name of the associated element.
+ * @param aname The name of the attribute.
+ * @return The default value, or null if the attribute was
+ * #IMPLIED or simply undeclared and unspecified.
+ * @see #getAttributeExpandedValue
+ */
+ public String getAttributeDefaultValue(String name, String aname)
+ {
+ AttributeDecl attribute = getAttribute(name, aname);
+ return (attribute == null) ? null : attribute.value;
+ }
+
+ /*
+
+// FIXME: Leaving this in, until W3C finally resolves the confusion
+// between parts of the XML 2nd REC about when entity declararations
+// are guaranteed to be known. Current code matches what section 5.1
+// (conformance) describes, but some readings of the self-contradicting
+// text in 4.1 (the "Entity Declared" WFC and VC) seem to expect that
+// attribute expansion/normalization must be deferred in some cases
+// (just TRY to identify them!).
+
+ * Retrieve the expanded value of a declared attribute.
+ * General entities (and char refs) will be expanded (once).
+ * @param name The name of the associated element.
+ * @param aname The name of the attribute.
+ * @return The expanded default value, or null if the attribute was
+ * #IMPLIED or simply undeclared
+ * @see #getAttributeDefaultValue
+ public String getAttributeExpandedValue (String name, String aname)
+ throws Exception
+ {
+ AttributeDecl attribute = getAttribute (name, aname);
+
+ if (attribute == null) {
+ return null;
+ } else if (attribute.defaultValue == null && attribute.value != null) {
+ // we MUST use the same buf for both quotes else the literal
+ // can't be properly terminated
+ char buf [] = new char [1];
+ int flags = LIT_ENTITY_REF | LIT_ATTRIBUTE;
+ String type = getAttributeType (name, aname);
+
+ if (type != "CDATA" && type != null)
+ flags |= LIT_NORMALIZE;
+ buf [0] = '"';
+ pushCharArray (null, buf, 0, 1);
+ pushString (null, attribute.value);
+ pushCharArray (null, buf, 0, 1);
+ attribute.defaultValue = readLiteral (flags);
+ }
+ return attribute.defaultValue;
+ }
+ */
+
+ /**
+ * Retrieve the default value mode of a declared attribute.
+ * @see #ATTRIBUTE_DEFAULT_SPECIFIED
+ * @see #ATTRIBUTE_DEFAULT_IMPLIED
+ * @see #ATTRIBUTE_DEFAULT_REQUIRED
+ * @see #ATTRIBUTE_DEFAULT_FIXED
+ */
+ public int getAttributeDefaultValueType(String name, String aname)
+ {
+ AttributeDecl attribute = getAttribute(name, aname);
+ return (attribute == null) ? ATTRIBUTE_DEFAULT_UNDECLARED :
+ attribute.valueType;
+ }
+
+ /**
+ * Register an attribute declaration for later retrieval.
+ * Format:
+ * - String type
+ * - String default value
+ * - int value type
+ * - enumeration
+ * - processed default value
+ */
+ private void setAttribute(String elName, String name, String type,
+ String enumeration, String value, int valueType)
+ throws Exception
+ {
+ HashMap attlist;
+
+ if (skippedPE)
+ {
+ return;
+ }
+
+ // Create a new hashtable if necessary.
+ attlist = getElementAttributes(elName);
+ if (attlist == null)
+ {
+ attlist = new HashMap();
+ }
+
+ // ignore multiple attribute declarations!
+ if (attlist.get(name) != null)
+ {
+ // warn ...
+ return;
+ }
+ else
+ {
+ AttributeDecl attribute = new AttributeDecl();
+ attribute.type = type;
+ attribute.value = value;
+ attribute.valueType = valueType;
+ attribute.enumeration = enumeration;
+ attlist.put(name, attribute);
+
+ // save; but don't overwrite any existing
+ setElement(elName, CONTENT_UNDECLARED, null, attlist);
+ }
+ }
+
+ /**
+ * Retrieve the attribute declaration for the given element name and name.
+ */
+ private AttributeDecl getAttribute(String elName, String name)
+ {
+ HashMap attlist = getElementAttributes(elName);
+ return (attlist == null) ? null : (AttributeDecl) attlist.get(name);
+ }
+
+ //
+ // Entities
+ //
+
+ /**
+ * Find the type of an entity.
+ * @returns An integer constant representing the entity type.
+ * @see #ENTITY_UNDECLARED
+ * @see #ENTITY_INTERNAL
+ * @see #ENTITY_NDATA
+ * @see #ENTITY_TEXT
+ */
+ public int getEntityType(String ename)
+ {
+ EntityInfo entity = (EntityInfo) entityInfo.get(ename);
+ return (entity == null) ? ENTITY_UNDECLARED : entity.type;
+ }
+
+ /**
+ * Return an external entity's identifiers.
+ * @param ename The name of the external entity.
+ * @return The entity's public identifier, system identifier, and base URI.
+ * Null if the entity was not declared as an external entity.
+ * @see #getEntityType
+ */
+ public ExternalIdentifiers getEntityIds(String ename)
+ {
+ EntityInfo entity = (EntityInfo) entityInfo.get(ename);
+ return (entity == null) ? null : entity.ids;
+ }
+
+ /**
+ * Return an internal entity's replacement text.
+ * @param ename The name of the internal entity.
+ * @return The entity's replacement text, or null if
+ * the entity was not declared as an internal entity.
+ * @see #getEntityType
+ */
+ public String getEntityValue(String ename)
+ {
+ EntityInfo entity = (EntityInfo) entityInfo.get(ename);
+ return (entity == null) ? null : entity.value;
+ }
+
+ /**
+ * Register an entity declaration for later retrieval.
+ */
+ private void setInternalEntity(String eName, String value)
+ throws SAXException
+ {
+ if (skippedPE)
+ {
+ return;
+ }
+
+ if (entityInfo.get(eName) == null)
+ {
+ EntityInfo entity = new EntityInfo();
+ entity.type = ENTITY_INTERNAL;
+ entity.value = value;
+ entityInfo.put(eName, entity);
+ }
+ if (handler.stringInterning)
+ {
+ if ("lt" == eName || "gt" == eName || "quot" == eName
+ || "apos" == eName || "amp" == eName)
+ {
+ return;
+ }
+ }
+ else
+ {
+ if ("lt".equals(eName) || "gt".equals(eName) || "quot".equals(eName)
+ || "apos".equals(eName) || "amp".equals(eName))
+ {
+ return;
+ }
+ }
+ handler.getDeclHandler().internalEntityDecl(eName, value);
+ }
+
+ /**
+ * Register an external entity declaration for later retrieval.
+ */
+ private void setExternalEntity(String eName, int eClass,
+ ExternalIdentifiers ids, String nName)
+ {
+ if (entityInfo.get(eName) == null)
+ {
+ EntityInfo entity = new EntityInfo();
+ entity.type = eClass;
+ entity.ids = ids;
+ entity.notationName = nName;
+ entityInfo.put(eName, entity);
+ }
+ }
+
+ //
+ // Notations.
+ //
+
+ /**
+ * Report a notation declaration, checking for duplicates.
+ */
+ private void setNotation(String nname, ExternalIdentifiers ids)
+ throws SAXException
+ {
+ if (skippedPE)
+ {
+ return;
+ }
+
+ handler.notationDecl(nname, ids.publicId, ids.systemId, ids.baseUri);
+ if (notationInfo.get(nname) == null)
+ {
+ notationInfo.put(nname, nname);
+ }
+ else
+ {
+ // VC: Unique Notation Name
+ handler.verror("Duplicate notation name decl: " + nname);
+ }
+ }
+
+ //
+ // Location.
+ //
+
+ /**
+ * Return the current line number.
+ */
+ public int getLineNumber()
+ {
+ return line;
+ }
+
+ /**
+ * Return the current column number.
+ */
+ public int getColumnNumber()
+ {
+ return column;
+ }
+
+ //////////////////////////////////////////////////////////////////////
+ // High-level I/O.
+ //////////////////////////////////////////////////////////////////////
+
+ /**
+ * Read a single character from the readBuffer.
+ * The readDataChunk () method maintains the buffer.
+ * If we hit the end of an entity, try to pop the stack and
+ * keep going.
+ * (This approach doesn't really enforce XML's rules about
+ * entity boundaries, but this is not currently a validating
+ * parser).
+ * This routine also attempts to keep track of the current
+ * position in external entities, but it's not entirely accurate.
+ * @return The next available input character.
+ * @see #unread (char)
+ * @see #readDataChunk
+ * @see #readBuffer
+ * @see #line
+ * @return The next character from the current input source.
+ */
+ private char readCh()
+ throws SAXException, IOException
+ {
+ // As long as there's nothing in the
+ // read buffer, try reading more data
+ // (for an external entity) or popping
+ // the entity stack (for either).
+ while (readBufferPos >= readBufferLength)
+ {
+ switch (sourceType)
+ {
+ case INPUT_READER:
+ case INPUT_STREAM:
+ readDataChunk();
+ while (readBufferLength < 1)
+ {
+ popInput();
+ if (readBufferLength < 1)
+ {
+ readDataChunk();
+ }
+ }
+ break;
+
+ default:
+
+ popInput();
+ break;
+ }
+ }
+
+ char c = readBuffer[readBufferPos++];
+
+ if (c == '\n')
+ {
+ line++;
+ column = 0;
+ }
+ else
+ {
+ if (c == '<')
+ {
+ /* the most common return to parseContent () ... NOP */
+ }
+ else if (((c < 0x0020 && (c != '\t') && (c != '\r')) || c > 0xFFFD)
+ || ((c >= 0x007f) && (c <= 0x009f) && (c != 0x0085)
+ && xmlVersion == XML_11))
+ {
+ error("illegal XML character U+" + Integer.toHexString(c));
+ }
+
+ // If we're in the DTD and in a context where PEs get expanded,
+ // do so ... 1/14/2000 errata identify those contexts. There
+ // are also spots in the internal subset where PE refs are fatal
+ // errors, hence yet another flag.
+ else if (c == '%' && expandPE)
+ {
+ if (peIsError)
+ {
+ error("PE reference within decl in internal subset.");
+ }
+ parsePEReference();
+ return readCh();
+ }
+ column++;
+ }
+
+ return c;
+ }
+
+ /**
+ * Push a single character back onto the current input stream.
+ * This method usually pushes the character back onto
+ * the readBuffer.
+ * I don't think that this would ever be called with
+ * readBufferPos = 0, because the methods always reads a character
+ * before unreading it, but just in case, I've added a boundary
+ * condition.
+ * @param c The character to push back.
+ * @see #readCh
+ * @see #unread (char[])
+ * @see #readBuffer
+ */
+ private void unread(char c)
+ throws SAXException
+ {
+ // Normal condition.
+ if (c == '\n')
+ {
+ line--;
+ column = -1;
+ }
+ if (readBufferPos > 0)
+ {
+ readBuffer[--readBufferPos] = c;
+ }
+ else
+ {
+ pushString(null, Character.toString(c));
+ }
+ }
+
+ /**
+ * Push a char array back onto the current input stream.
+ * NOTE: you must never push back characters that you
+ * haven't actually read: use pushString () instead.
+ * @see #readCh
+ * @see #unread (char)
+ * @see #readBuffer
+ * @see #pushString
+ */
+ private void unread(char[] ch, int length)
+ throws SAXException
+ {
+ for (int i = 0; i < length; i++)
+ {
+ if (ch[i] == '\n')
+ {
+ line--;
+ column = -1;
+ }
+ }
+ if (length < readBufferPos)
+ {
+ readBufferPos -= length;
+ }
+ else
+ {
+ pushCharArray(null, ch, 0, length);
+ }
+ }
+
+ /**
+ * Push, or skip, a new external input source.
+ * The source will be some kind of parsed entity, such as a PE
+ * (including the external DTD subset) or content for the body.
+ *
+ * @param url The java.net.URL object for the entity.
+ * @see SAXDriver#resolveEntity
+ * @see #pushString
+ * @see #sourceType
+ * @see #pushInput
+ * @see #detectEncoding
+ * @see #sourceType
+ * @see #readBuffer
+ */
+ private void pushURL(boolean isPE,
+ String ename,
+ ExternalIdentifiers ids,
+ Reader reader,
+ InputStream stream,
+ String encoding,
+ boolean doResolve)
+ throws SAXException, IOException
+ {
+ boolean ignoreEncoding;
+ String systemId;
+ InputSource source;
+
+ if (!isPE)
+ {
+ dataBufferFlush();
+ }
+
+ scratch.setPublicId(ids.publicId);
+ scratch.setSystemId(ids.systemId);
+
+ // See if we should skip or substitute the entity.
+ // If we're not skipping, resolving reports startEntity()
+ // and updates the (handler's) stack of URIs.
+ if (doResolve)
+ {
+ // assert (stream == null && reader == null && encoding == null)
+ source = handler.resolveEntity(isPE, ename, scratch, ids.baseUri);
+ if (source == null)
+ {
+ handler.warn("skipping entity: " + ename);
+ handler.skippedEntity(ename);
+ if (isPE)
+ {
+ skippedPE = true;
+ }
+ return;
+ }
+
+ // we might be using alternate IDs/encoding
+ systemId = source.getSystemId();
+ // The following warning and setting systemId was deleted bcause
+ // the application has the option of not setting systemId
+ // provided that it has set the characte/byte stream.
+ /*
+ if (systemId == null) {
+ handler.warn ("missing system ID, using " + ids.systemId);
+ systemId = ids.systemId;
+ }
+ */
+ }
+ else
+ {
+ // "[document]", or "[dtd]" via getExternalSubset()
+ scratch.setCharacterStream(reader);
+ scratch.setByteStream(stream);
+ scratch.setEncoding(encoding);
+ source = scratch;
+ systemId = ids.systemId;
+ if (handler.stringInterning)
+ {
+ handler.startExternalEntity(ename, systemId,
+ "[document]" == ename);
+ }
+ else
+ {
+ handler.startExternalEntity(ename, systemId,
+ "[document]".equals(ename));
+ }
+ }
+
+ // we may have been given I/O streams directly
+ if (source.getCharacterStream() != null)
+ {
+ if (source.getByteStream() != null)
+ error("InputSource has two streams!");
+ reader = source.getCharacterStream();
+ }
+ else if (source.getByteStream() != null)
+ {
+ encoding = source.getEncoding();
+ if (encoding == null)
+ {
+ stream = source.getByteStream();
+ }
+ else
+ {
+ try
+ {
+ reader = new InputStreamReader(source.getByteStream(),
+ encoding);
+ }
+ catch (IOException e)
+ {
+ stream = source.getByteStream();
+ }
+ }
+ }
+ else if (systemId == null)
+ {
+ error("InputSource has no URI!");
+ }
+ scratch.setCharacterStream(null);
+ scratch.setByteStream(null);
+ scratch.setEncoding(null);
+
+ // Push the existing status.
+ pushInput(ename);
+
+ // Create a new read buffer.
+ // (Note the four-character margin)
+ readBuffer = new char[READ_BUFFER_MAX + 4];
+ readBufferPos = 0;
+ readBufferLength = 0;
+ readBufferOverflow = -1;
+ is = null;
+ line = 1;
+ column = 0;
+ currentByteCount = 0;
+
+ // If there's an explicit character stream, just
+ // ignore encoding declarations.
+ if (reader != null)
+ {
+ sourceType = INPUT_READER;
+ this.reader = reader;
+ tryEncodingDecl(true);
+ return;
+ }
+
+ // Else we handle the conversion, and need to ensure
+ // it's done right.
+ sourceType = INPUT_STREAM;
+ if (stream != null)
+ {
+ is = stream;
+ }
+ else
+ {
+ // We have to open our own stream to the URL.
+ URL url = new URL(systemId);
+
+ externalEntity = url.openConnection();
+ externalEntity.connect();
+ is = externalEntity.getInputStream();
+ }
+
+ // If we get to here, there must be
+ // an InputStream available.
+ if (!is.markSupported())
+ {
+ is = new BufferedInputStream(is);
+ }
+
+ // Get any external encoding label.
+ if (encoding == null && externalEntity != null)
+ {
+ // External labels can be untrustworthy; filesystems in
+ // particular often have the wrong default for content
+ // that wasn't locally originated. Those we autodetect.
+ if (!"file".equals(externalEntity.getURL().getProtocol()))
+ {
+ int temp;
+
+ // application/xml;charset=something;otherAttr=...
+ // ... with many variants on 'something'
+ encoding = externalEntity.getContentType();
+
+ // MHK code (fix for Saxon 5.5.1/007):
+ // protect against encoding==null
+ if (encoding == null)
+ {
+ temp = -1;
+ }
+ else
+ {
+ temp = encoding.indexOf("charset");
+ }
+
+ // RFC 2376 sez MIME text defaults to ASCII, but since the
+ // JDK will create a MIME type out of thin air, we always
+ // autodetect when there's no explicit charset attribute.
+ if (temp < 0)
+ {
+ encoding = null; // autodetect
+ }
+ else
+ {
+ // only this one attribute
+ if ((temp = encoding.indexOf(';')) > 0)
+ {
+ encoding = encoding.substring(0, temp);
+ }
+
+ if ((temp = encoding.indexOf('=', temp + 7)) > 0)
+ {
+ encoding = encoding.substring(temp + 1);
+
+ // attributes can have comment fields (RFC 822)
+ if ((temp = encoding.indexOf('(')) > 0)
+ {
+ encoding = encoding.substring(0, temp);
+ }
+ // ... and values may be quoted
+ if ((temp = encoding.indexOf('"')) > 0)
+ {
+ encoding =
+ encoding.substring(temp + 1,
+ encoding.indexOf('"', temp + 2));
+ }
+ encoding = encoding.trim();
+ }
+ else
+ {
+ handler.warn("ignoring illegal MIME attribute: "
+ + encoding);
+ encoding = null;
+ }
+ }
+ }
+ }
+
+ // if we got an external encoding label, use it ...
+ if (encoding != null)
+ {
+ this.encoding = ENCODING_EXTERNAL;
+ setupDecoding(encoding);
+ ignoreEncoding = true;
+
+ // ... else autodetect from first bytes.
+ }
+ else
+ {
+ detectEncoding();
+ ignoreEncoding = false;
+ }
+
+ // Read any XML or text declaration.
+ // If we autodetected, it may tell us the "real" encoding.
+ try
+ {
+ tryEncodingDecl(ignoreEncoding);
+ }
+ catch (UnsupportedEncodingException x)
+ {
+ encoding = x.getMessage();
+
+ // if we don't handle the declared encoding,
+ // try letting a JVM InputStreamReader do it
+ try
+ {
+ if (sourceType != INPUT_STREAM)
+ {
+ throw x;
+ }
+
+ is.reset();
+ readBufferPos = 0;
+ readBufferLength = 0;
+ readBufferOverflow = -1;
+ line = 1;
+ currentByteCount = column = 0;
+
+ sourceType = INPUT_READER;
+ this.reader = new InputStreamReader(is, encoding);
+ is = null;
+
+ tryEncodingDecl(true);
+
+ }
+ catch (IOException e)
+ {
+ error("unsupported text encoding",
+ encoding,
+ null);
+ }
+ }
+ }
+
+ /**
+ * Check for an encoding declaration. This is the second part of the
+ * XML encoding autodetection algorithm, relying on detectEncoding to
+ * get to the point that this part can read any encoding declaration
+ * in the document (using only US-ASCII characters).
+ *
+ * Because this part starts to fill parser buffers with this data,
+ * it's tricky to setup a reader so that Java's built-in decoders can be
+ * used for the character encodings that aren't built in to this parser
+ * (such as EUC-JP, KOI8-R, Big5, etc).
+ *
+ * @return any encoding in the declaration, uppercased; or null
+ * @see detectEncoding
+ */
+ private String tryEncodingDecl(boolean ignoreEncoding)
+ throws SAXException, IOException
+ {
+ // Read the XML/text declaration.
+ if (tryRead(" 0)
+ {
+ return parseTextDecl(ignoreEncoding);
+ }
+ else
+ {
+ return parseXMLDecl(ignoreEncoding);
+ }
+ }
+ else
+ {
+ // or similar
+ unread('l');
+ unread('m');
+ unread('x');
+ unread('?');
+ unread('<');
+ }
+ }
+ return null;
+ }
+
+ /**
+ * Attempt to detect the encoding of an entity.
+ * The trick here (as suggested in the XML standard) is that
+ * any entity not in UTF-8, or in UCS-2 with a byte-order mark,
+ * must begin with an XML declaration or an encoding
+ * declaration; we simply have to look for "<?xml" in various
+ * encodings.
+ * This method has no way to distinguish among 8-bit encodings.
+ * Instead, it sets up for UTF-8, then (possibly) revises its assumption
+ * later in setupDecoding (). Any ASCII-derived 8-bit encoding
+ * should work, but most will be rejected later by setupDecoding ().
+ * @see #tryEncoding (byte[], byte, byte, byte, byte)
+ * @see #tryEncoding (byte[], byte, byte)
+ * @see #setupDecoding
+ */
+ private void detectEncoding()
+ throws SAXException, IOException
+ {
+ byte[] signature = new byte[4];
+
+ // Read the first four bytes for
+ // autodetection.
+ is.mark(4);
+ is.read(signature);
+ is.reset();
+
+ //
+ // FIRST: four byte encodings (who uses these?)
+ //
+ if (tryEncoding(signature, (byte) 0x00, (byte) 0x00,
+ (byte) 0x00, (byte) 0x3c))
+ {
+ // UCS-4 must begin with "Utility routine for detectEncoding ().
+ * Always looks for some part of "Looks for a UCS-2 byte-order mark.
+ * Utility routine for detectEncoding ().
+ * @param sig The first four bytes read.
+ * @param b1 The first byte of the signature
+ * @param b2 The second byte of the signature
+ * @see #detectEncoding
+ */
+ private static boolean tryEncoding(byte[] sig, byte b1, byte b2)
+ {
+ return ((sig[0] == b1) && (sig[1] == b2));
+ }
+
+ /**
+ * This method pushes a string back onto input.
+ * It is useful either as the expansion of an internal entity,
+ * or for backtracking during the parse.
+ * Call pushCharArray () to do the actual work.
+ * @param s The string to push back onto input.
+ * @see #pushCharArray
+ */
+ private void pushString(String ename, String s)
+ throws SAXException
+ {
+ char[] ch = s.toCharArray();
+ pushCharArray(ename, ch, 0, ch.length);
+ }
+
+ /**
+ * Push a new internal input source.
+ * This method is useful for expanding an internal entity,
+ * or for unreading a string of characters. It creates a new
+ * readBuffer containing the characters in the array, instead
+ * of characters converted from an input byte stream.
+ * @param ch The char array to push.
+ * @see #pushString
+ * @see #pushURL
+ * @see #readBuffer
+ * @see #sourceType
+ * @see #pushInput
+ */
+ private void pushCharArray(String ename, char[] ch, int start, int length)
+ throws SAXException
+ {
+ // Push the existing status
+ pushInput(ename);
+ if (ename != null && doReport)
+ {
+ dataBufferFlush();
+ handler.startInternalEntity(ename);
+ }
+ sourceType = INPUT_INTERNAL;
+ readBuffer = ch;
+ readBufferPos = start;
+ readBufferLength = length;
+ readBufferOverflow = -1;
+ }
+
+ /**
+ * Save the current input source onto the stack.
+ * This method saves all of the global variables associated with
+ * the current input source, so that they can be restored when a new
+ * input source has finished. It also tests for entity recursion.
+ * The method saves the following global variables onto a stack
+ * using a fixed-length array:
+ * This method restores all of the global variables associated with
+ * the current input source.
+ * @exception java.io.EOFException
+ * If there are no more entries on the input stack.
+ * @see #pushInput
+ * @see #sourceType
+ * @see #externalEntity
+ * @see #readBuffer
+ * @see #readBufferPos
+ * @see #readBufferLength
+ * @see #line
+ * @see #encoding
+ */
+ private void popInput()
+ throws SAXException, IOException
+ {
+ String ename = (String) entityStack.removeLast();
+
+ if (ename != null && doReport)
+ {
+ dataBufferFlush();
+ }
+ switch (sourceType)
+ {
+ case INPUT_STREAM:
+ handler.endExternalEntity(ename);
+ is.close();
+ break;
+ case INPUT_READER:
+ handler.endExternalEntity(ename);
+ reader.close();
+ break;
+ case INPUT_INTERNAL:
+ if (ename != null && doReport)
+ {
+ handler.endInternalEntity(ename);
+ }
+ break;
+ }
+
+ // Throw an EOFException if there
+ // is nothing else to pop.
+ if (inputStack.isEmpty())
+ {
+ throw new EOFException("no more input");
+ }
+
+ Input input = (Input) inputStack.removeLast();
+
+ sourceType = input.sourceType;
+ externalEntity = input.externalEntity;
+ readBuffer = input.readBuffer;
+ readBufferPos = input.readBufferPos;
+ readBufferLength = input.readBufferLength;
+ line = input.line;
+ encoding = input.encoding;
+ readBufferOverflow = input.readBufferOverflow;
+ is = input.is;
+ currentByteCount = input.currentByteCount;
+ column = input.column;
+ reader = input.reader;
+ }
+
+ /**
+ * Return true if we can read the expected character.
+ * Note that the character will be removed from the input stream
+ * on success, but will be put back on failure. Do not attempt to
+ * read the character again if the method succeeds.
+ * @param delim The character that should appear next. For a
+ * insensitive match, you must supply this in upper-case.
+ * @return true if the character was successfully read, or false if
+ * it was not.
+ * @see #tryRead (String)
+ */
+ private boolean tryRead(char delim)
+ throws SAXException, IOException
+ {
+ char c;
+
+ // Read the character
+ c = readCh();
+
+ // Test for a match, and push the character
+ // back if the match fails.
+ if (c == delim)
+ {
+ return true;
+ }
+ else
+ {
+ unread(c);
+ return false;
+ }
+ }
+
+ /**
+ * Return true if we can read the expected string.
+ * This is simply a convenience method.
+ * Note that the string will be removed from the input stream
+ * on success, but will be put back on failure. Do not attempt to
+ * read the string again if the method succeeds.
+ * This method will push back a character rather than an
+ * array whenever possible (probably the majority of cases).
+ * @param delim The string that should appear next.
+ * @return true if the string was successfully read, or false if
+ * it was not.
+ * @see #tryRead (char)
+ */
+ private boolean tryRead(String delim)
+ throws SAXException, IOException
+ {
+ return tryRead(delim.toCharArray());
+ }
+
+ private boolean tryRead(char[] ch)
+ throws SAXException, IOException
+ {
+ char c;
+
+ // Compare the input, character-
+ // by character.
+
+ for (int i = 0; i < ch.length; i++)
+ {
+ c = readCh();
+ if (c != ch[i])
+ {
+ unread(c);
+ if (i != 0)
+ {
+ unread(ch, i);
+ }
+ return false;
+ }
+ }
+ return true;
+ }
+
+ /**
+ * Return true if we can read some whitespace.
+ * This is simply a convenience method.
+ * This method will push back a character rather than an
+ * array whenever possible (probably the majority of cases).
+ * @return true if whitespace was found.
+ */
+ private boolean tryWhitespace()
+ throws SAXException, IOException
+ {
+ char c;
+ c = readCh();
+ if (isWhitespace(c))
+ {
+ skipWhitespace();
+ return true;
+ }
+ else
+ {
+ unread(c);
+ return false;
+ }
+ }
+
+ /**
+ * Read all data until we find the specified string.
+ * This is useful for scanning CDATA sections and PIs.
+ * This is inefficient right now, since it calls tryRead ()
+ * for every character.
+ * @param delim The string delimiter
+ * @see #tryRead (String, boolean)
+ * @see #readCh
+ */
+ private void parseUntil(String delim)
+ throws SAXException, IOException
+ {
+ parseUntil(delim.toCharArray());
+ }
+
+ private void parseUntil(char[] delim)
+ throws SAXException, IOException
+ {
+ char c;
+ int startLine = line;
+
+ try
+ {
+ while (!tryRead(delim))
+ {
+ c = readCh();
+ dataBufferAppend(c);
+ }
+ }
+ catch (EOFException e)
+ {
+ error("end of input while looking for delimiter "
+ + "(started on line " + startLine
+ + ')', null, new String(delim));
+ }
+ }
+
+ //////////////////////////////////////////////////////////////////////
+ // Low-level I/O.
+ //////////////////////////////////////////////////////////////////////
+
+ /**
+ * Prefetch US-ASCII XML/text decl from input stream into read buffer.
+ * Doesn't buffer more than absolutely needed, so that when an encoding
+ * decl says we need to create an InputStreamReader, we can discard our
+ * buffer and reset(). Caller knows the first chars of the decl exist
+ * in the input stream.
+ */
+ private void prefetchASCIIEncodingDecl()
+ throws SAXException, IOException
+ {
+ int ch;
+ readBufferPos = readBufferLength = 0;
+
+ is.mark(readBuffer.length);
+ while (true)
+ {
+ ch = is.read();
+ readBuffer[readBufferLength++] = (char) ch;
+ switch (ch)
+ {
+ case (int) '>':
+ return;
+ case -1:
+ error("file ends before end of XML or encoding declaration.",
+ null, "?>");
+ }
+ if (readBuffer.length == readBufferLength)
+ {
+ error("unfinished XML or encoding declaration");
+ }
+ }
+ }
+
+ /**
+ * Read a chunk of data from an external input source.
+ * This is simply a front-end that fills the rawReadBuffer
+ * with bytes, then calls the appropriate encoding handler.
+ * @see #encoding
+ * @see #rawReadBuffer
+ * @see #readBuffer
+ * @see #filterCR
+ * @see #copyUtf8ReadBuffer
+ * @see #copyIso8859_1ReadBuffer
+ * @see #copyUcs_2ReadBuffer
+ * @see #copyUcs_4ReadBuffer
+ */
+ private void readDataChunk()
+ throws SAXException, IOException
+ {
+ int count;
+
+ // See if we have any overflow (filterCR sets for CR at end)
+ if (readBufferOverflow > -1)
+ {
+ readBuffer[0] = (char) readBufferOverflow;
+ readBufferOverflow = -1;
+ readBufferPos = 1;
+ sawCR = true;
+ }
+ else
+ {
+ readBufferPos = 0;
+ sawCR = false;
+ }
+
+ // input from a character stream.
+ if (sourceType == INPUT_READER)
+ {
+ count = reader.read(readBuffer,
+ readBufferPos, READ_BUFFER_MAX - readBufferPos);
+ if (count < 0)
+ {
+ readBufferLength = readBufferPos;
+ }
+ else
+ {
+ readBufferLength = readBufferPos + count;
+ }
+ if (readBufferLength > 0)
+ {
+ filterCR(count >= 0);
+ }
+ sawCR = false;
+ return;
+ }
+
+ // Read as many bytes as possible into the raw buffer.
+ count = is.read(rawReadBuffer, 0, READ_BUFFER_MAX);
+
+ // Dispatch to an encoding-specific reader method to populate
+ // the readBuffer. In most parser speed profiles, these routines
+ // show up at the top of the CPU usage chart.
+ if (count > 0)
+ {
+ switch (encoding)
+ {
+ // one byte builtins
+ case ENCODING_ASCII:
+ copyIso8859_1ReadBuffer(count, (char) 0x0080);
+ break;
+ case ENCODING_UTF_8:
+ copyUtf8ReadBuffer(count);
+ break;
+ case ENCODING_ISO_8859_1:
+ copyIso8859_1ReadBuffer(count, (char) 0);
+ break;
+
+ // two byte builtins
+ case ENCODING_UCS_2_12:
+ copyUcs2ReadBuffer(count, 8, 0);
+ break;
+ case ENCODING_UCS_2_21:
+ copyUcs2ReadBuffer(count, 0, 8);
+ break;
+
+ // four byte builtins
+ case ENCODING_UCS_4_1234:
+ copyUcs4ReadBuffer(count, 24, 16, 8, 0);
+ break;
+ case ENCODING_UCS_4_4321:
+ copyUcs4ReadBuffer(count, 0, 8, 16, 24);
+ break;
+ case ENCODING_UCS_4_2143:
+ copyUcs4ReadBuffer(count, 16, 24, 0, 8);
+ break;
+ case ENCODING_UCS_4_3412:
+ copyUcs4ReadBuffer(count, 8, 0, 24, 16);
+ break;
+ }
+ }
+ else
+ {
+ readBufferLength = readBufferPos;
+ }
+
+ readBufferPos = 0;
+
+ // Filter out all carriage returns if we've seen any
+ // (including any saved from a previous read)
+ if (sawCR)
+ {
+ filterCR(count >= 0);
+ sawCR = false;
+
+ // must actively report EOF, lest some CRs get lost.
+ if (readBufferLength == 0 && count >= 0)
+ {
+ readDataChunk();
+ }
+ }
+
+ if (count > 0)
+ {
+ currentByteCount += count;
+ }
+ }
+
+ /**
+ * Filter carriage returns in the read buffer.
+ * CRLF becomes LF; CR becomes LF.
+ * @param moreData true iff more data might come from the same source
+ * @see #readDataChunk
+ * @see #readBuffer
+ * @see #readBufferOverflow
+ */
+ private void filterCR(boolean moreData)
+ {
+ int i, j;
+
+ readBufferOverflow = -1;
+
+loop:
+ for (i = j = readBufferPos; j < readBufferLength; i++, j++)
+ {
+ switch (readBuffer[j])
+ {
+ case '\r':
+ if (j == readBufferLength - 1)
+ {
+ if (moreData)
+ {
+ readBufferOverflow = '\r';
+ readBufferLength--;
+ }
+ else // CR at end of buffer
+ {
+ readBuffer[i++] = '\n';
+ }
+ break loop;
+ }
+ else if (readBuffer[j + 1] == '\n')
+ {
+ j++;
+ }
+ readBuffer[i] = '\n';
+ break;
+
+ case '\n':
+ default:
+ readBuffer[i] = readBuffer[j];
+ break;
+ }
+ }
+ readBufferLength = i;
+ }
+
+ /**
+ * Convert a buffer of UTF-8-encoded bytes into UTF-16 characters.
+ * When readDataChunk () calls this method, the raw bytes are in
+ * rawReadBuffer, and the final characters will appear in
+ * readBuffer.
+ * Note that as of Unicode 3.1, good practice became a requirement,
+ * so that each Unicode character has exactly one UTF-8 representation.
+ * @param count The number of bytes to convert.
+ * @see #readDataChunk
+ * @see #rawReadBuffer
+ * @see #readBuffer
+ * @see #getNextUtf8Byte
+ */
+ private void copyUtf8ReadBuffer(int count)
+ throws SAXException, IOException
+ {
+ int i = 0;
+ int j = readBufferPos;
+ int b1;
+ char c = 0;
+
+ /*
+ // check once, so the runtime won't (if it's smart enough)
+ if (count < 0 || count > rawReadBuffer.length)
+ throw new ArrayIndexOutOfBoundsException (Integer.toString (count));
+ */
+
+ while (i < count)
+ {
+ b1 = rawReadBuffer[i++];
+
+ // Determine whether we are dealing
+ // with a one-, two-, three-, or four-
+ // byte sequence.
+ if (b1 < 0)
+ {
+ if ((b1 & 0xe0) == 0xc0)
+ {
+ // 2-byte sequence: 00000yyyyyxxxxxx = 110yyyyy 10xxxxxx
+ c = (char) (((b1 & 0x1f) << 6)
+ | getNextUtf8Byte(i++, count));
+ if (c < 0x0080)
+ {
+ encodingError("Illegal two byte UTF-8 sequence",
+ c, 0);
+ }
+
+ //Sec 2.11
+ // [1] the two-character sequence #xD #xA
+ // [2] the two-character sequence #xD #x85
+ if ((c == 0x0085 || c == 0x000a) && sawCR)
+ {
+ continue;
+ }
+
+ // Sec 2.11
+ // [3] the single character #x85
+
+ if (c == 0x0085 && xmlVersion == XML_11)
+ {
+ readBuffer[j++] = '\r';
+ }
+ }
+ else if ((b1 & 0xf0) == 0xe0)
+ {
+ // 3-byte sequence:
+ // zzzzyyyyyyxxxxxx = 1110zzzz 10yyyyyy 10xxxxxx
+ // most CJKV characters
+ c = (char) (((b1 & 0x0f) << 12) |
+ (getNextUtf8Byte(i++, count) << 6) |
+ getNextUtf8Byte(i++, count));
+ //sec 2.11
+ //[4] the single character #x2028
+ if (c == 0x2028 && xmlVersion == XML_11)
+ {
+ readBuffer[j++] = '\r';
+ sawCR = true;
+ continue;
+ }
+ if (c < 0x0800 || (c >= 0xd800 && c <= 0xdfff))
+ {
+ encodingError("Illegal three byte UTF-8 sequence",
+ c, 0);
+ }
+ }
+ else if ((b1 & 0xf8) == 0xf0)
+ {
+ // 4-byte sequence: 11101110wwwwzzzzyy + 110111yyyyxxxxxx
+ // = 11110uuu 10uuzzzz 10yyyyyy 10xxxxxx
+ // (uuuuu = wwww + 1)
+ // "Surrogate Pairs" ... from the "Astral Planes"
+ // Unicode 3.1 assigned the first characters there
+ int iso646 = b1 & 07;
+ iso646 = (iso646 << 6) + getNextUtf8Byte(i++, count);
+ iso646 = (iso646 << 6) + getNextUtf8Byte(i++, count);
+ iso646 = (iso646 << 6) + getNextUtf8Byte(i++, count);
+
+ if (iso646 <= 0xffff)
+ {
+ encodingError("Illegal four byte UTF-8 sequence",
+ iso646, 0);
+ }
+ else
+ {
+ if (iso646 > 0x0010ffff)
+ {
+ encodingError("UTF-8 value out of range for Unicode",
+ iso646, 0);
+ }
+ iso646 -= 0x010000;
+ readBuffer[j++] = (char) (0xd800 | (iso646 >> 10));
+ readBuffer[j++] = (char) (0xdc00 | (iso646 & 0x03ff));
+ continue;
+ }
+ }
+ else
+ {
+ // The five and six byte encodings aren't supported;
+ // they exceed the Unicode (and XML) range.
+ encodingError("unsupported five or six byte UTF-8 sequence",
+ 0xff & b1, i);
+ // NOTREACHED
+ c = 0;
+ }
+ }
+ else
+ {
+ // 1-byte sequence: 000000000xxxxxxx = 0xxxxxxx
+ // (US-ASCII character, "common" case, one branch to here)
+ c = (char) b1;
+ }
+ readBuffer[j++] = c;
+ if (c == '\r')
+ {
+ sawCR = true;
+ }
+ }
+ // How many characters have we read?
+ readBufferLength = j;
+ }
+
+ /**
+ * Return the next byte value in a UTF-8 sequence.
+ * If it is not possible to get a byte from the current
+ * entity, throw an exception.
+ * @param pos The current position in the rawReadBuffer.
+ * @param count The number of bytes in the rawReadBuffer
+ * @return The significant six bits of a non-initial byte in
+ * a UTF-8 sequence.
+ * @exception EOFException If the sequence is incomplete.
+ */
+ private int getNextUtf8Byte(int pos, int count)
+ throws SAXException, IOException
+ {
+ int val;
+
+ // Take a character from the buffer
+ // or from the actual input stream.
+ if (pos < count)
+ {
+ val = rawReadBuffer[pos];
+ }
+ else
+ {
+ val = is.read();
+ if (val == -1)
+ {
+ encodingError("unfinished multi-byte UTF-8 sequence at EOF",
+ -1, pos);
+ }
+ }
+
+ // Check for the correct bits at the start.
+ if ((val & 0xc0) != 0x80)
+ {
+ encodingError("bad continuation of multi-byte UTF-8 sequence",
+ val, pos + 1);
+ }
+
+ // Return the significant bits.
+ return (val & 0x3f);
+ }
+
+ /**
+ * Convert a buffer of US-ASCII or ISO-8859-1-encoded bytes into
+ * UTF-16 characters.
+ *
+ * When readDataChunk () calls this method, the raw bytes are in
+ * rawReadBuffer, and the final characters will appear in
+ * readBuffer.
+ *
+ * @param count The number of bytes to convert.
+ * @param mask For ASCII conversion, 0x7f; else, 0xff.
+ * @see #readDataChunk
+ * @see #rawReadBuffer
+ * @see #readBuffer
+ */
+ private void copyIso8859_1ReadBuffer(int count, char mask)
+ throws IOException
+ {
+ int i, j;
+ for (i = 0, j = readBufferPos; i < count; i++, j++)
+ {
+ char c = (char) (rawReadBuffer[i] & 0xff);
+ if ((c & mask) != 0)
+ {
+ throw new CharConversionException("non-ASCII character U+"
+ + Integer.toHexString(c));
+ }
+ if (c == 0x0085 && xmlVersion == XML_11)
+ {
+ c = '\r';
+ }
+ readBuffer[j] = c;
+ if (c == '\r')
+ {
+ sawCR = true;
+ }
+ }
+ readBufferLength = j;
+ }
+
+ /**
+ * Convert a buffer of UCS-2-encoded bytes into UTF-16 characters
+ * (as used in Java string manipulation).
+ *
+ * When readDataChunk () calls this method, the raw bytes are in
+ * rawReadBuffer, and the final characters will appear in
+ * readBuffer.
+ * @param count The number of bytes to convert.
+ * @param shift1 The number of bits to shift byte 1.
+ * @param shift2 The number of bits to shift byte 2
+ * @see #readDataChunk
+ * @see #rawReadBuffer
+ * @see #readBuffer
+ */
+ private void copyUcs2ReadBuffer(int count, int shift1, int shift2)
+ throws SAXException
+ {
+ int j = readBufferPos;
+
+ if (count > 0 && (count % 2) != 0)
+ {
+ encodingError("odd number of bytes in UCS-2 encoding", -1, count);
+ }
+ // The loops are faster with less internal brancing; hence two
+ if (shift1 == 0)
+ { // "UTF-16-LE"
+ for (int i = 0; i < count; i += 2)
+ {
+ char c = (char) (rawReadBuffer[i + 1] << 8);
+ c |= 0xff & rawReadBuffer[i];
+ readBuffer[j++] = c;
+ if (c == '\r')
+ {
+ sawCR = true;
+ }
+ }
+ }
+ else
+ { // "UTF-16-BE"
+ for (int i = 0; i < count; i += 2)
+ {
+ char c = (char) (rawReadBuffer[i] << 8);
+ c |= 0xff & rawReadBuffer[i + 1];
+ readBuffer[j++] = c;
+ if (c == '\r')
+ {
+ sawCR = true;
+ }
+ }
+ }
+ readBufferLength = j;
+ }
+
+ /**
+ * Convert a buffer of UCS-4-encoded bytes into UTF-16 characters.
+ *
+ * When readDataChunk () calls this method, the raw bytes are in
+ * rawReadBuffer, and the final characters will appear in
+ * readBuffer.
+ * Java has Unicode chars, and this routine uses surrogate pairs
+ * for ISO-10646 values between 0x00010000 and 0x000fffff. An
+ * exception is thrown if the ISO-10646 character has no Unicode
+ * representation.
+ *
+ * @param count The number of bytes to convert.
+ * @param shift1 The number of bits to shift byte 1.
+ * @param shift2 The number of bits to shift byte 2
+ * @param shift3 The number of bits to shift byte 2
+ * @param shift4 The number of bits to shift byte 2
+ * @see #readDataChunk
+ * @see #rawReadBuffer
+ * @see #readBuffer
+ */
+ private void copyUcs4ReadBuffer(int count, int shift1, int shift2,
+ int shift3, int shift4)
+ throws SAXException
+ {
+ int j = readBufferPos;
+
+ if (count > 0 && (count % 4) != 0)
+ {
+ encodingError("number of bytes in UCS-4 encoding " +
+ "not divisible by 4",
+ -1, count);
+ }
+ for (int i = 0; i < count; i += 4)
+ {
+ int value = (((rawReadBuffer [i] & 0xff) << shift1) |
+ ((rawReadBuffer [i + 1] & 0xff) << shift2) |
+ ((rawReadBuffer [i + 2] & 0xff) << shift3) |
+ ((rawReadBuffer [i + 3] & 0xff) << shift4));
+ if (value < 0x0000ffff)
+ {
+ readBuffer [j++] = (char) value;
+ if (value == (int) '\r')
+ {
+ sawCR = true;
+ }
+ }
+ else if (value < 0x0010ffff)
+ {
+ value -= 0x010000;
+ readBuffer[j++] = (char) (0xd8 | ((value >> 10) & 0x03ff));
+ readBuffer[j++] = (char) (0xdc | (value & 0x03ff));
+ }
+ else
+ {
+ encodingError("UCS-4 value out of range for Unicode",
+ value, i);
+ }
+ }
+ readBufferLength = j;
+ }
+
+ /**
+ * Report a character encoding error.
+ */
+ private void encodingError(String message, int value, int offset)
+ throws SAXException
+ {
+ if (value != -1)
+ {
+ message = message + " (character code: 0x" +
+ Integer.toHexString(value) + ')';
+ error(message);
+ }
+ }
+
+ //////////////////////////////////////////////////////////////////////
+ // Local Variables.
+ //////////////////////////////////////////////////////////////////////
+
+ /**
+ * Re-initialize the variables for each parse.
+ */
+ private void initializeVariables()
+ {
+ // First line
+ line = 1;
+ column = 0;
+
+ // Set up the buffers for data and names
+ dataBufferPos = 0;
+ dataBuffer = new char[DATA_BUFFER_INITIAL];
+ nameBufferPos = 0;
+ nameBuffer = new char[NAME_BUFFER_INITIAL];
+
+ // Set up the DTD hash tables
+ elementInfo = new HashMap();
+ entityInfo = new HashMap();
+ notationInfo = new HashMap();
+ skippedPE = false;
+
+ // Set up the variables for the current
+ // element context.
+ currentElement = null;
+ currentElementContent = CONTENT_UNDECLARED;
+
+ // Set up the input variables
+ sourceType = INPUT_NONE;
+ inputStack = new LinkedList();
+ entityStack = new LinkedList();
+ externalEntity = null;
+ tagAttributePos = 0;
+ tagAttributes = new String[100];
+ rawReadBuffer = new byte[READ_BUFFER_MAX];
+ readBufferOverflow = -1;
+
+ scratch = new InputSource();
+
+ inLiteral = false;
+ expandPE = false;
+ peIsError = false;
+
+ doReport = false;
+
+ inCDATA = false;
+
+ symbolTable = new Object[SYMBOL_TABLE_LENGTH][];
+ }
+
+ static class ExternalIdentifiers
+ {
+
+ String publicId;
+ String systemId;
+ String baseUri;
+
+ ExternalIdentifiers()
+ {
+ }
+
+ ExternalIdentifiers(String publicId, String systemId, String baseUri)
+ {
+ this.publicId = publicId;
+ this.systemId = systemId;
+ this.baseUri = baseUri;
+ }
+
+ }
+
+ static class EntityInfo
+ {
+
+ int type;
+ ExternalIdentifiers ids;
+ String value;
+ String notationName;
+
+ }
+
+ static class AttributeDecl
+ {
+
+ String type;
+ String value;
+ int valueType;
+ String enumeration;
+ String defaultValue;
+
+ }
+
+ static class ElementDecl
+ {
+
+ int contentType;
+ String contentModel;
+ HashMap attributes;
+
+ }
+
+ static class Input
+ {
+
+ int sourceType;
+ URLConnection externalEntity;
+ char[] readBuffer;
+ int readBufferPos;
+ int readBufferLength;
+ int line;
+ int encoding;
+ int readBufferOverflow;
+ InputStream is;
+ int currentByteCount;
+ int column;
+ Reader reader;
+
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/aelfred2/XmlReader.java b/libjava/classpath/gnu/xml/aelfred2/XmlReader.java
new file mode 100644
index 000000000..e0a047612
--- /dev/null
+++ b/libjava/classpath/gnu/xml/aelfred2/XmlReader.java
@@ -0,0 +1,373 @@
+/* XmlReader.java --
+ Copyright (C) 1999,2000,2001 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.aelfred2;
+
+import java.io.IOException;
+import java.util.Locale;
+
+import org.xml.sax.*;
+import org.xml.sax.ext.*;
+
+import gnu.xml.pipeline.EventFilter;
+import gnu.xml.pipeline.ValidationConsumer;
+
+
+/**
+ * This SAX2 parser optionally layers a validator over the Ælfred2
+ * SAX2 parser. While this will not evaluate every XML validity constraint,
+ * it does support all the validity constraints that are of any real utility
+ * outside the strict SGML-compatible world. See the documentation for the
+ * SAXDriver class for information about the SAX2 features and properties
+ * that are supported, and documentation for the ValidationConsumer for
+ * information about what validity constraints may not be supported.
+ * (Ælfred2 tests some of those, even in non-validating mode, to
+ * achieve better conformance.)
+ *
+ * Note that due to its internal construction, you can't change most
+ * handlers until parse() returns. This diverges slightly from SAX, which
+ * expects later binding to be supported. Early binding involves less
+ * runtime overhead, which is an issue for event pipelines as used inside
+ * this parser. Rather than relying on the parser to handle late binding
+ * to your own handlers, do it yourself.
+ *
+ * @see SAXDriver
+ * @see gnu.xml.pipeline.ValidationConsumer
+ *
+ * @author David Brownell
+ */
+public final class XmlReader
+ implements XMLReader
+{
+
+ 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, optionally treating validity errors
+ * as if they were fatal errors.
+ */
+ public XmlReader(boolean invalidIsFatal)
+ {
+ if (invalidIsFatal)
+ {
+ setErrorHandler(new FatalErrorHandler());
+ }
+ }
+
+ /**
+ * SAX2: Returns the object used to report the logical
+ * content of an XML document.
+ */
+ public ContentHandler getContentHandler()
+ {
+ return filter.getContentHandler();
+ }
+
+ /**
+ * SAX2: Assigns the object used to report the logical
+ * content of an XML document.
+ * @exception IllegalStateException if called mid-parse
+ */
+ public void setContentHandler(ContentHandler handler)
+ {
+ if (active)
+ {
+ throw new IllegalStateException("already parsing");
+ }
+ filter.setContentHandler(handler);
+ }
+
+ /**
+ * SAX2: Returns the object used to process declarations related
+ * to notations and unparsed entities.
+ */
+ public DTDHandler getDTDHandler()
+ {
+ return filter.getDTDHandler();
+ }
+
+ /**
+ * SAX1 Assigns DTD handler
+ * @exception IllegalStateException if called mid-parse
+ */
+ public void setDTDHandler(DTDHandler handler)
+ {
+ if (active)
+ {
+ throw new IllegalStateException("already parsing");
+ }
+ filter.setDTDHandler(handler);
+ }
+
+ /**
+ * SAX2: Returns the object used when resolving external
+ * entities during parsing (both general and parameter entities).
+ */
+ public EntityResolver getEntityResolver()
+ {
+ return aelfred2.getEntityResolver();
+ }
+
+ /**
+ * SAX1 Assigns parser's entity resolver
+ */
+ public void setEntityResolver(EntityResolver handler)
+ {
+ aelfred2.setEntityResolver(handler);
+ }
+
+ /**
+ * SAX2: 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();
+ }
+
+ /**
+ * SAX1 Assigns error handler
+ * @exception IllegalStateException if called mid-parse
+ */
+ public void setErrorHandler(ErrorHandler handler)
+ {
+ if (active)
+ {
+ throw new IllegalStateException("already parsing");
+ }
+ aelfred2.setErrorHandler(handler);
+ }
+
+ /**
+ * SAX2: Assigns the specified property.
+ * @exception IllegalStateException if called mid-parse
+ */
+ 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);
+ }
+ }
+
+ /**
+ * SAX2: Returns the specified property.
+ */
+ 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);
+ }
+
+ private void forceValidating()
+ throws SAXNotRecognizedException, SAXNotSupportedException
+ {
+ aelfred2.setFeature(SAXDriver.FEATURE + "namespace-prefixes",
+ true);
+ aelfred2.setFeature(SAXDriver.FEATURE + "external-general-entities",
+ true);
+ aelfred2.setFeature(SAXDriver.FEATURE + "external-parameter-entities",
+ true);
+ }
+
+ /**
+ * SAX2: Sets the state of features supported in this parser.
+ * Note that this parser requires reporting of namespace prefixes when
+ * validating.
+ */
+ public void setFeature(String featureId, boolean state)
+ throws SAXNotRecognizedException, SAXNotSupportedException
+ {
+ boolean value = getFeature(featureId);
+
+ if (state == value)
+ {
+ return;
+ }
+
+ if ((SAXDriver.FEATURE + "validation").equals(featureId))
+ {
+ if (active)
+ {
+ throw new SAXNotSupportedException("already parsing");
+ }
+ if (state)
+ {
+ forceValidating();
+ }
+ isValidating = state;
+ }
+ else
+ {
+ aelfred2.setFeature(featureId, state);
+ }
+ }
+
+ /**
+ * SAX2: Tells whether this parser supports the specified feature.
+ * At this time, this directly parallels the underlying SAXDriver,
+ * except that validation is optionally supported.
+ *
+ * @see SAXDriver
+ */
+ public boolean getFeature(String featureId)
+ throws SAXNotRecognizedException, SAXNotSupportedException
+ {
+ if ((SAXDriver.FEATURE + "validation").equals(featureId))
+ {
+ return isValidating;
+ }
+
+ return aelfred2.getFeature(featureId);
+ }
+
+ /**
+ * SAX1: Sets the locale used for diagnostics; currently,
+ * only locales using the English language are supported.
+ * @param locale The locale for which diagnostics will be generated
+ */
+ public void setLocale(Locale locale)
+ throws SAXException
+ {
+ aelfred2.setLocale(locale);
+ }
+
+ /**
+ * SAX1: Preferred API to parse an XML document, using a
+ * system identifier (URI).
+ */
+ public void parse(String systemId)
+ throws SAXException, IOException
+ {
+ parse(new InputSource(systemId));
+ }
+
+ /**
+ * SAX1: Underlying API to parse an XML document, used
+ * directly when no URI is available. When this is invoked,
+ * and the parser is set to validate, some features will be
+ * automatically reset to appropriate values: for reporting
+ * namespace prefixes, and incorporating external entities.
+ *
+ * @param source The XML input source.
+ *
+ * @exception IllegalStateException if called mid-parse
+ * @exception SAXException The handlers may throw any SAXException,
+ * and the parser normally throws SAXParseException objects.
+ * @exception IOException IOExceptions are normally through through
+ * the parser if there are problems reading the source document.
+ */
+ public void parse(InputSource source)
+ throws SAXException, IOException
+ {
+ EventFilter next;
+ boolean nsdecls;
+
+ synchronized (aelfred2)
+ {
+ if (active)
+ {
+ throw new IllegalStateException("already parsing");
+ }
+ active = true;
+ }
+
+ // set up the output pipeline
+ 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);
+ if (!nsdecls)
+ {
+ aelfred2.setFeature(SAXDriver.FEATURE + "namespace-prefixes",
+ false);
+ }
+
+ // parse, clean up
+ try
+ {
+ aelfred2.parse(source);
+ }
+ finally
+ {
+ active = false;
+ }
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/aelfred2/package.html b/libjava/classpath/gnu/xml/aelfred2/package.html
new file mode 100644
index 000000000..e20425844
--- /dev/null
+++ b/libjava/classpath/gnu/xml/aelfred2/package.html
@@ -0,0 +1,506 @@
+
+
+ This package contains Ælfred2, which includes an
+enhanced SAX2-compatible version of the Ælfred
+non-validating XML parser, a modular (and hence optional)
+DTD validating parser, and modular (and hence optional)
+JAXP glue to those.
+Use these like any other SAX2 parsers. Ælfred is a XML parser written in the java programming language.
+
+ In most Java applets and applications, XML should not be the central
+feature; instead, XML is the means to another end, such as loading
+configuration information, reading meta-data, or parsing transactions. When an XML parser is only a single component of a much larger
+program, it cannot be large, slow, or resource-intensive. With Java
+applets, in particular, code size is a significant issue. The standard
+modem is still not operating at 56 Kbaud, or sometimes even with data
+compression. Assuming an uncompressed 28.8 Kbaud modem, only about
+3 KBytes can be downloaded in one second; compression often doubles
+that speed, but a V.90 modem may not provide another doubling. When
+used with embedded processors, similar size concerns apply. Ælfred is designed for easy and efficient use over the Internet,
+based on the following principles: As you can see from this list, Ælfred is designed for production
+use, but neither validation nor perfect conformance was a requirement.
+Good validating parsers exist, including one in this package,
+and you should use them as appropriate. (See conformance reviews
+available at http://www.xml.com)
+ One of the main goals of Ælfred2 was to significantly improve
+conformance, while not significantly affecting the other goals stated above.
+Since the only use of this parser is with SAX, some classes could be
+removed, and so the overall size of Ælfred was actually reduced.
+Subsequent performance work produced a notable speedup (over twenty
+percent on larger files). That is, the tradeoffs between speed, size, and
+conformance were re-targeted towards conformance and support of newer APIs
+(SAX2), with a a positive performance impact. The role anticipated for this version of Ælfred is as a
+lightweight Free Software SAX parser that can be used in essentially every
+Java program where the handful of conformance violations (noted below)
+are acceptable.
+That certainly includes applets, and
+nowadays one must also mention embedded systems as being even more
+size-critical.
+At this writing, all parsers that are more conformant are
+significantly larger, even when counting the optional
+validation support in this version of Ælfred. Ælfred the Great (AElfred in ASCII) was King of Wessex, and
+some say of King of England, at the time of his death in 899 AD.
+Ælfred introduced a wide-spread literacy program in the hope that
+his people would learn to read English, at least, if Latin was too
+difficult for them. This Ælfred hopes to bring another sort of
+literacy to Java, using XML, at least, if full SGML is too difficult. The initial Æ ligature ("AE)" is also a reminder that XML is
+not limited to ASCII. The Ælfred parser currently builds in support for a handful
+of input encodings. Of course these include UTF-8 and UTF-16, which
+all XML parsers are required to support: If you use any encoding other than UTF-8 or UTF-16 you should
+make sure to label your data appropriately: Encodings accessed through Note that if you are using the Euro symbol with an fixed length
+eight bit encoding, you should probably be using the encoding label
+iso-8859-15 or, with a Microsoft OS, cp-1252.
+Of course, UTF-8 and UTF-16 handle the Euro symbol directly.
+ Known conformance issues should be of negligible importance for
+most applications, and include: When tested against the July 12, 1999 version of the OASIS
+XML Conformance test suite, an earlier version passed 1057 of 1067 tests.
+That contrasts with the original version, which passed 867. The
+current parser is top-ranked in terms of conformance, as is its
+validating sibling (which has some additional conformance violations
+imposed on it by SAX2 API deficiencies as well as some of the more
+curious SGML layering artifacts found in the XML specification). The XML 1.0 specification itself was not without problems,
+and after some delays the W3C has come out with a revised
+"second edition" specification. While that doesn't resolve all
+the problems identified the XML specification, many of the most
+egregious problems have been resolved. (You still need to drink
+magic Kool-Aid before some DTD-related issues make sense.)
+To the extent possible, this parser conforms to that second
+edition specification, and does well against corrected versions
+of the OASIS/NIST XML conformance test cases. See http://xmlconf.sourceforge.net
+for more information about SAX2/XML conformance testing.
+The software in this package is distributed under the GNU General Public
+License (with a special exception described below).
+
+A copy of GNU General Public License (GPL) is included in this distribution,
+in the file COPYING. If you do not have the source code, it is available at:
+
+ http://www.gnu.org/software/classpath/
+ Some of this documentation was modified from the original
+Ælfred README.txt file. All of it has been updated.
+ *
+ *
+ *
+ *
+ *
+ * Name
+ * Notes
+ *
+ *
+ * (URL)/external-general-entities
+ * Value defaults to true
+ * (URL)/external-parameter-entities
+ * Value defaults to true
+ * (URL)/is-standalone
+ * (PRELIMINARY) Returns true iff the document's parsing
+ * has started (some non-error event after startDocument()
+ * was reported) and the document's standalone flag is set.
+ * (URL)/namespace-prefixes
+ * Value defaults to false (but XML 1.0 names are
+ * always reported)
+ * (URL)/lexical-handler/parameter-entities
+ * Value is fixed at true
+ * (URL)/namespaces
+ * Value defaults to true
+ * (URL)/resolve-dtd-uris
+ * (PRELIMINARY) Value defaults to true
+ * (URL)/string-interning
+ * Value is fixed at true
+ * (URL)/use-attributes2
+ * (PRELIMINARY) Value is fixed at true
+ * (URL)/use-entity-resolver2
+ * (PRELIMINARY) Value defaults to true
+ *
+ * (URL)/validation
+ * Value is fixed at false
+ *
+ *
+ * (URL)/declaration-handler
+ * A declaration handler may be provided.
+ * (URL)/lexical-handler
+ * A lexical handler may be provided. SAXDriver
class as your entry point, as all
+ * internal parser interfaces are subject to change.
+ *
+ * @author Written by David Megginson <dmeggins@microstar.com>
+ * (version 1.2a with bugfixes)
+ * @author Updated by David Brownell <dbrownell@users.sourceforge.net>
+ * @see SAXDriver
+ */
+final class XmlParser
+{
+
+ // avoid slow per-character readCh()
+ private final static boolean USE_CHEATS = true;
+
+ ////////////////////////////////////////////////////////////////////////
+ // Constants.
+ ////////////////////////////////////////////////////////////////////////
+
+ //
+ // Constants for element content type.
+ //
+
+ /**
+ * Constant: an element has not been declared.
+ * @see #getElementContentType
+ */
+ public final static int CONTENT_UNDECLARED = 0;
+
+ /**
+ * Constant: the element has a content model of ANY.
+ * @see #getElementContentType
+ */
+ public final static int CONTENT_ANY = 1;
+
+ /**
+ * Constant: the element has declared content of EMPTY.
+ * @see #getElementContentType
+ */
+ public final static int CONTENT_EMPTY = 2;
+
+ /**
+ * Constant: the element has mixed content.
+ * @see #getElementContentType
+ */
+ public final static int CONTENT_MIXED = 3;
+
+ /**
+ * Constant: the element has element content.
+ * @see #getElementContentType
+ */
+ public final static int CONTENT_ELEMENTS = 4;
+
+
+ //
+ // Constants for the entity type.
+ //
+
+ /**
+ * Constant: the entity has not been declared.
+ * @see #getEntityType
+ */
+ public final static int ENTITY_UNDECLARED = 0;
+
+ /**
+ * Constant: the entity is internal.
+ * @see #getEntityType
+ */
+ public final static int ENTITY_INTERNAL = 1;
+
+ /**
+ * Constant: the entity is external, non-parsable data.
+ * @see #getEntityType
+ */
+ public final static int ENTITY_NDATA = 2;
+
+ /**
+ * Constant: the entity is external XML data.
+ * @see #getEntityType
+ */
+ public final static int ENTITY_TEXT = 3;
+
+ //
+ // Attribute type constants are interned literal strings.
+ //
+
+ //
+ // Constants for supported encodings. "external" is just a flag.
+ //
+ private final static int ENCODING_EXTERNAL = 0;
+ private final static int ENCODING_UTF_8 = 1;
+ private final static int ENCODING_ISO_8859_1 = 2;
+ private final static int ENCODING_UCS_2_12 = 3;
+ private final static int ENCODING_UCS_2_21 = 4;
+ private final static int ENCODING_UCS_4_1234 = 5;
+ private final static int ENCODING_UCS_4_4321 = 6;
+ private final static int ENCODING_UCS_4_2143 = 7;
+ private final static int ENCODING_UCS_4_3412 = 8;
+ private final static int ENCODING_ASCII = 9;
+
+ //
+ // Constants for attribute default value.
+ //
+
+ /**
+ * Constant: the attribute is not declared.
+ * @see #getAttributeDefaultValueType
+ */
+ public final static int ATTRIBUTE_DEFAULT_UNDECLARED = 30;
+
+ /**
+ * Constant: the attribute has a literal default value specified.
+ * @see #getAttributeDefaultValueType
+ * @see #getAttributeDefaultValue
+ */
+ public final static int ATTRIBUTE_DEFAULT_SPECIFIED = 31;
+
+ /**
+ * Constant: the attribute was declared #IMPLIED.
+ * @see #getAttributeDefaultValueType
+ */
+ public final static int ATTRIBUTE_DEFAULT_IMPLIED = 32;
+
+ /**
+ * Constant: the attribute was declared #REQUIRED.
+ * @see #getAttributeDefaultValueType
+ */
+ public final static int ATTRIBUTE_DEFAULT_REQUIRED = 33;
+
+ /**
+ * Constant: the attribute was declared #FIXED.
+ * @see #getAttributeDefaultValueType
+ * @see #getAttributeDefaultValue
+ */
+ public final static int ATTRIBUTE_DEFAULT_FIXED = 34;
+
+ //
+ // Constants for input.
+ //
+ private final static int INPUT_NONE = 0;
+ private final static int INPUT_INTERNAL = 1;
+ private final static int INPUT_STREAM = 3;
+ private final static int INPUT_READER = 5;
+
+ //
+ // Flags for reading literals.
+ //
+ // expand general entity refs (attribute values in dtd and content)
+ private final static int LIT_ENTITY_REF = 2;
+ // normalize this value (space chars) (attributes, public ids)
+ private final static int LIT_NORMALIZE = 4;
+ // literal is an attribute value
+ private final static int LIT_ATTRIBUTE = 8;
+ // don't expand parameter entities
+ private final static int LIT_DISABLE_PE = 16;
+ // don't expand [or parse] character refs
+ private final static int LIT_DISABLE_CREF = 32;
+ // don't parse general entity refs
+ private final static int LIT_DISABLE_EREF = 64;
+ // literal is a public ID value
+ private final static int LIT_PUBID = 256;
+
+ //
+ // Flags affecting PE handling in DTDs (if expandPE is true).
+ // PEs expand with space padding, except inside literals.
+ //
+ private final static int CONTEXT_NORMAL = 0;
+ private final static int CONTEXT_LITERAL = 1;
+
+ // Emit warnings for relative URIs with no base URI.
+ static boolean uriWarnings;
+ static
+ {
+ String key = "gnu.xml.aelfred2.XmlParser.uriWarnings";
+ GetPropertyAction a = new GetPropertyAction(key);
+ uriWarnings = "true".equals(AccessController.doPrivileged(a));
+ }
+
+ //
+ // The current XML handler interface.
+ //
+ private SAXDriver handler;
+
+ //
+ // I/O information.
+ //
+ private Reader reader; // current reader
+ private InputStream is; // current input stream
+ private int line; // current line number
+ private int column; // current column number
+ private int sourceType; // type of input source
+ private LinkedList inputStack; // stack of input soruces
+ private URLConnection externalEntity; // current external entity
+ private int encoding; // current character encoding
+ private int currentByteCount; // bytes read from current source
+ private InputSource scratch; // temporary
+
+ //
+ // Buffers for decoded but unparsed character input.
+ //
+ private char[] readBuffer;
+ private int readBufferPos;
+ private int readBufferLength;
+ private int readBufferOverflow; // overflow from last data chunk.
+
+ //
+ // Buffer for undecoded raw byte input.
+ //
+ private final static int READ_BUFFER_MAX = 16384;
+ private byte[] rawReadBuffer;
+
+
+ //
+ // Buffer for attribute values, char refs, DTD stuff.
+ //
+ private static int DATA_BUFFER_INITIAL = 4096;
+ private char[] dataBuffer;
+ private int dataBufferPos;
+
+ //
+ // Buffer for parsed names.
+ //
+ private static int NAME_BUFFER_INITIAL = 1024;
+ private char[] nameBuffer;
+ private int nameBufferPos;
+
+ //
+ // Save any standalone flag
+ //
+ private boolean docIsStandalone;
+
+ //
+ // Hashtables for DTD information on elements, entities, and notations.
+ // Populated until we start ignoring decls (because of skipping a PE)
+ //
+ private HashMap elementInfo;
+ private HashMap entityInfo;
+ private HashMap notationInfo;
+ private boolean skippedPE;
+
+ //
+ // Element type currently in force.
+ //
+ private String currentElement;
+ private int currentElementContent;
+
+ //
+ // Stack of entity names, to detect recursion.
+ //
+ private LinkedList entityStack;
+
+ //
+ // PE expansion is enabled in most chunks of the DTD, not all.
+ // When it's enabled, literals are treated differently.
+ //
+ private boolean inLiteral;
+ private boolean expandPE;
+ private boolean peIsError;
+
+ //
+ // can't report entity expansion inside two constructs:
+ // - attribute expansions (internal entities only)
+ // - markup declarations (parameter entities only)
+ //
+ private boolean doReport;
+
+ //
+ // Symbol table, for caching interned names.
+ //
+ // These show up wherever XML names or nmtokens are used: naming elements,
+ // attributes, PIs, notations, entities, and enumerated attribute values.
+ //
+ // NOTE: This hashtable doesn't grow. The default size is intended to be
+ // rather large for most documents. Example: one snapshot of the DocBook
+ // XML 4.1 DTD used only about 350 such names. As a rule, only pathological
+ // documents (ones that don't reuse names) should ever see much collision.
+ //
+ // Be sure that SYMBOL_TABLE_LENGTH always stays prime, for best hashing.
+ // "2039" keeps the hash table size at about two memory pages on typical
+ // 32 bit hardware.
+ //
+ private final static int SYMBOL_TABLE_LENGTH = 2039;
+
+ private Object[][] symbolTable;
+
+ //
+ // Hash table of attributes found in current start tag.
+ //
+ private String[] tagAttributes;
+ private int tagAttributePos;
+
+ //
+ // Utility flag: have we noticed a CR while reading the last
+ // data chunk? If so, we will have to go back and normalise
+ // CR or CR/LF line ends.
+ //
+ private boolean sawCR;
+
+ //
+ // Utility flag: are we in CDATA? If so, whitespace isn't ignorable.
+ //
+ private boolean inCDATA;
+
+ //
+ // Xml version.
+ //
+ private static final int XML_10 = 0;
+ private static final int XML_11 = 1;
+ private int xmlVersion = XML_10;
+
+ //////////////////////////////////////////////////////////////////////
+ // Constructors.
+ ////////////////////////////////////////////////////////////////////////
+
+ /**
+ * Construct a new parser with no associated handler.
+ * @see #setHandler
+ * @see #parse
+ */
+ // package private
+ XmlParser()
+ {
+ }
+
+ /**
+ * Set the handler that will receive parsing events.
+ * @param handler The handler to receive callback events.
+ * @see #parse
+ */
+ // package private
+ void setHandler(SAXDriver handler)
+ {
+ this.handler = handler;
+ }
+
+ /**
+ * Parse an XML document from the character stream, byte stream, or URI
+ * that you provide (in that order of preference). Any URI that you
+ * supply will become the base URI for resolving relative URI, and may
+ * be used to acquire a reader or byte stream.
+ *
+ *
+ * [1] document ::= prolog element Misc*
+ *
+ *
+ * [15] Comment ::= '<!--' ((Char - '-') | ('-' (Char - '-')))* "-->"
+ *
+ * <!--
has already been read.)
+ */
+ private void parseComment()
+ throws Exception
+ {
+ char c;
+ boolean saved = expandPE;
+
+ expandPE = false;
+ parseUntil(endDelimComment);
+ require('>');
+ expandPE = saved;
+ handler.comment(dataBuffer, 0, dataBufferPos);
+ dataBufferPos = 0;
+ }
+
+ static final char[] startDelimPI = { '<', '?' };
+ static final char[] endDelimPI = { '?', '>' };
+
+ /**
+ * Parse a processing instruction and do a call-back.
+ *
+ * [16] PI ::= '<?' PITarget
+ * (S (Char* - (Char* '?>' Char*)))?
+ * '?>'
+ * [17] PITarget ::= Name - ( ('X'|'x') ('M'|m') ('L'|l') )
+ *
+ * <?
has already been read.)
+ */
+ private void parsePI()
+ throws SAXException, IOException
+ {
+ String name;
+ boolean saved = expandPE;
+
+ expandPE = false;
+ name = readNmtoken(true);
+ //NE08
+ if (name.indexOf(':') >= 0)
+ {
+ error("Illegal character(':') in processing instruction name ",
+ name, null);
+ }
+ if ("xml".equalsIgnoreCase(name))
+ {
+ error("Illegal processing instruction target", name, null);
+ }
+ if (!tryRead(endDelimPI))
+ {
+ requireWhitespace();
+ parseUntil(endDelimPI);
+ }
+ expandPE = saved;
+ handler.processingInstruction(name, dataBufferToString());
+ }
+
+ static final char[] endDelimCDATA = { ']', ']', '>' };
+
+ private boolean isDirtyCurrentElement;
+
+ /**
+ * Parse a CDATA section.
+ *
+ * [18] CDSect ::= CDStart CData CDEnd
+ * [19] CDStart ::= '<![CDATA['
+ * [20] CData ::= (Char* - (Char* ']]>' Char*))
+ * [21] CDEnd ::= ']]>'
+ *
+ *
+ * [22] prolog ::= XMLDecl? Misc* (Doctypedecl Misc*)?
+ *
+ * <?xml
and whitespace have already been read.)
+ * @return the encoding in the declaration, uppercased; or null
+ * @see #parseTextDecl
+ * @see #setupDecoding
+ */
+ private String parseXMLDecl(boolean ignoreEncoding)
+ throws SAXException, IOException
+ {
+ String version;
+ String encodingName = null;
+ String standalone = null;
+ int flags = LIT_DISABLE_CREF | LIT_DISABLE_PE | LIT_DISABLE_EREF;
+ String inputEncoding = null;
+
+ switch (this.encoding)
+ {
+ case ENCODING_EXTERNAL:
+ case ENCODING_UTF_8:
+ inputEncoding = "UTF-8";
+ break;
+ case ENCODING_ISO_8859_1:
+ inputEncoding = "ISO-8859-1";
+ break;
+ case ENCODING_UCS_2_12:
+ inputEncoding = "UTF-16BE";
+ break;
+ case ENCODING_UCS_2_21:
+ inputEncoding = "UTF-16LE";
+ break;
+ }
+
+ // Read the version.
+ require("version");
+ parseEq();
+ checkLegalVersion(version = readLiteral(flags));
+ if (!version.equals("1.0"))
+ {
+ if (version.equals("1.1"))
+ {
+ handler.warn("expected XML version 1.0, not: " + version);
+ xmlVersion = XML_11;
+ }
+ else
+ {
+ error("illegal XML version", version, "1.0 or 1.1");
+ }
+ }
+ else
+ {
+ xmlVersion = XML_10;
+ }
+ // Try reading an encoding declaration.
+ boolean white = tryWhitespace();
+
+ if (tryRead("encoding"))
+ {
+ if (!white)
+ {
+ error("whitespace required before 'encoding='");
+ }
+ parseEq();
+ encodingName = readLiteral(flags);
+ if (!ignoreEncoding)
+ {
+ setupDecoding(encodingName);
+ }
+ }
+
+ // Try reading a standalone declaration
+ if (encodingName != null)
+ {
+ white = tryWhitespace();
+ }
+ if (tryRead("standalone"))
+ {
+ if (!white)
+ {
+ error("whitespace required before 'standalone='");
+ }
+ parseEq();
+ standalone = readLiteral(flags);
+ if ("yes".equals(standalone))
+ {
+ docIsStandalone = true;
+ }
+ else if (!"no".equals(standalone))
+ {
+ error("standalone flag must be 'yes' or 'no'");
+ }
+ }
+
+ skipWhitespace();
+ require("?>");
+
+ if (inputEncoding == null)
+ {
+ inputEncoding = encodingName;
+ }
+ return encodingName;
+ }
+
+ /**
+ * Parse a text declaration.
+ *
+ * [79] TextDecl ::= '<?xml' VersionInfo? EncodingDecl S? '?>'
+ * [80] EncodingDecl ::= S 'encoding' Eq
+ * ( '"' EncName '"' | "'" EncName "'" )
+ * [81] EncName ::= [A-Za-z] ([A-Za-z0-9._] | '-')*
+ *
+ * <?xml
' and whitespace have already been read.)
+ * @return the encoding in the declaration, uppercased; or null
+ * @see #parseXMLDecl
+ * @see #setupDecoding
+ */
+ private String parseTextDecl(boolean ignoreEncoding)
+ throws SAXException, IOException
+ {
+ String encodingName = null;
+ int flags = LIT_DISABLE_CREF | LIT_DISABLE_PE | LIT_DISABLE_EREF;
+
+ // Read an optional version.
+ if (tryRead ("version"))
+ {
+ String version;
+ parseEq();
+ checkLegalVersion(version = readLiteral(flags));
+
+ if (version.equals("1.1"))
+ {
+ if (xmlVersion == XML_10)
+ {
+ error("external subset has later version number.", "1.0",
+ version);
+ }
+ handler.warn("expected XML version 1.0, not: " + version);
+ xmlVersion = XML_11;
+ }
+ else if (!version.equals("1.0"))
+ {
+ error("illegal XML version", version, "1.0 or 1.1");
+ }
+ requireWhitespace();
+ }
+
+ // Read the encoding.
+ require("encoding");
+ parseEq();
+ encodingName = readLiteral(flags);
+ if (!ignoreEncoding)
+ {
+ setupDecoding(encodingName);
+ }
+ skipWhitespace();
+ require("?>");
+
+ return encodingName;
+ }
+
+ /**
+ * Sets up internal state so that we can decode an entity using the
+ * specified encoding. This is used when we start to read an entity
+ * and we have been given knowledge of its encoding before we start to
+ * read any data (e.g. from a SAX input source or from a MIME type).
+ *
+ *
+ * [27] Misc ::= Comment | PI | S
+ *
+ */
+ private void parseMisc()
+ throws Exception
+ {
+ while (true)
+ {
+ skipWhitespace();
+ if (tryRead(startDelimPI))
+ {
+ parsePI();
+ }
+ else if (tryRead(startDelimComment))
+ {
+ parseComment();
+ }
+ else
+ {
+ return;
+ }
+ }
+ }
+
+ /**
+ * Parse a document type declaration.
+ *
+ * [28] doctypedecl ::= '<!DOCTYPE' S Name (S ExternalID)? S?
+ * ('[' (markupdecl | PEReference | S)* ']' S?)? '>'
+ *
+ * <!DOCTYPE
has already been read.)
+ */
+ private void parseDoctypedecl()
+ throws Exception
+ {
+ String rootName;
+ ExternalIdentifiers ids;
+
+ // Read the document type name.
+ requireWhitespace();
+ rootName = readNmtoken(true);
+
+ // Read the External subset's IDs
+ skipWhitespace();
+ ids = readExternalIds(false, true);
+
+ // report (a) declaration of name, (b) lexical info (ids)
+ handler.doctypeDecl(rootName, ids.publicId, ids.systemId);
+
+ // Internal subset is parsed first, if present
+ skipWhitespace();
+ if (tryRead('['))
+ {
+
+ // loop until the subset ends
+ while (true)
+ {
+ doReport = expandPE = true;
+ skipWhitespace();
+ doReport = expandPE = false;
+ if (tryRead(']'))
+ {
+ break; // end of subset
+ }
+ else
+ {
+ // WFC, PEs in internal subset (only between decls)
+ peIsError = expandPE = true;
+ parseMarkupdecl();
+ peIsError = expandPE = false;
+ }
+ }
+ }
+ skipWhitespace();
+ require('>');
+
+ // Read the external subset, if any
+ InputSource subset;
+
+ if (ids.systemId == null)
+ {
+ subset = handler.getExternalSubset(rootName,
+ handler.getSystemId());
+ }
+ else
+ {
+ subset = null;
+ }
+ if (ids.systemId != null || subset != null)
+ {
+ pushString(null, ">");
+
+ // NOTE: [dtd] is so we say what SAX2 expects,
+ // though it's misleading (subset, not entire dtd)
+ if (ids.systemId != null)
+ {
+ pushURL(true, "[dtd]", ids, null, null, null, true);
+ }
+ else
+ {
+ handler.warn("modifying document by adding external subset");
+ pushURL(true, "[dtd]",
+ new ExternalIdentifiers(subset.getPublicId(),
+ subset.getSystemId(),
+ null),
+ subset.getCharacterStream(),
+ subset.getByteStream(),
+ subset.getEncoding(),
+ false);
+ }
+
+ // Loop until we end up back at '>'
+ while (true)
+ {
+ doReport = expandPE = true;
+ skipWhitespace();
+ doReport = expandPE = false;
+ if (tryRead('>'))
+ {
+ break;
+ }
+ else
+ {
+ expandPE = true;
+ parseMarkupdecl();
+ expandPE = false;
+ }
+ }
+
+ // the ">" string isn't popped yet
+ if (inputStack.size() != 1)
+ {
+ error("external subset has unmatched '>'");
+ }
+ }
+
+ // done dtd
+ handler.endDoctype();
+ expandPE = false;
+ doReport = true;
+ }
+
+ /**
+ * Parse a markup declaration in the internal or external DTD subset.
+ *
+ * [29] markupdecl ::= elementdecl | Attlistdecl | EntityDecl
+ * | NotationDecl | PI | Comment
+ * [30] extSubsetDecl ::= (markupdecl | conditionalSect
+ * | PEReference | S) *
+ *
+ *
+ * [39] element ::= EmptyElementTag | STag content ETag
+ * [40] STag ::= '<' Name (S Attribute)* S? '>'
+ * [44] EmptyElementTag ::= '<' Name (S Attribute)* S? '/>'
+ *
+ *
+ * [41] Attribute ::= Name Eq AttValue
+ *
+ * @param name The name of the attribute's element.
+ * @see SAXDriver#attribute
+ */
+ private void parseAttribute(String name)
+ throws Exception
+ {
+ String aname;
+ String type;
+ String value;
+ int flags = LIT_ATTRIBUTE | LIT_ENTITY_REF;
+
+ // Read the attribute name.
+ aname = readNmtoken(true);
+ type = getAttributeType(name, aname);
+
+ // Parse '='
+ parseEq();
+
+ // Read the value, normalizing whitespace
+ // unless it is CDATA.
+ if (handler.stringInterning)
+ {
+ if (type == "CDATA" || type == null)
+ {
+ value = readLiteral(flags);
+ }
+ else
+ {
+ value = readLiteral(flags | LIT_NORMALIZE);
+ }
+ }
+ else
+ {
+ if (type == null || type.equals("CDATA"))
+ {
+ value = readLiteral(flags);
+ }
+ else
+ {
+ value = readLiteral(flags | LIT_NORMALIZE);
+ }
+ }
+
+ // WFC: no duplicate attributes
+ for (int i = 0; i < tagAttributePos; i++)
+ {
+ if (aname.equals(tagAttributes [i]))
+ {
+ error("duplicate attribute", aname, null);
+ }
+ }
+
+ // Inform the handler about the
+ // attribute.
+ handler.attribute(aname, value, true);
+ dataBufferPos = 0;
+
+ // Note that the attribute has been
+ // specified.
+ if (tagAttributePos == tagAttributes.length)
+ {
+ String newAttrib[] = new String[tagAttributes.length * 2];
+ System.arraycopy(tagAttributes, 0, newAttrib, 0, tagAttributePos);
+ tagAttributes = newAttrib;
+ }
+ tagAttributes[tagAttributePos++] = aname;
+ }
+
+ /**
+ * Parse an equals sign surrounded by optional whitespace.
+ *
+ * [25] Eq ::= S? '=' S?
+ *
+ */
+ private void parseEq()
+ throws SAXException, IOException
+ {
+ skipWhitespace();
+ require('=');
+ skipWhitespace();
+ }
+
+ /**
+ * Parse an end tag.
+ *
+ * [42] ETag ::= '' Name S? '>'
+ *
+ *
+ * [43] content ::= (element | CharData | Reference
+ * | CDSect | PI | Comment)*
+ * [67] Reference ::= EntityRef | CharRef
+ *
+ *
+ * [46] contentspec ::= 'EMPTY' | 'ANY' | Mixed | elements
+ *
+ */
+ private void parseContentspec(String name)
+ throws Exception
+ {
+ // FIXME: move elementDecl() into setElement(), pass EMTPY/ANY ...
+ if (tryRead("EMPTY"))
+ {
+ setElement(name, CONTENT_EMPTY, null, null);
+ if (!skippedPE)
+ {
+ handler.getDeclHandler().elementDecl(name, "EMPTY");
+ }
+ return;
+ }
+ else if (tryRead("ANY"))
+ {
+ setElement(name, CONTENT_ANY, null, null);
+ if (!skippedPE)
+ {
+ handler.getDeclHandler().elementDecl(name, "ANY");
+ }
+ return;
+ }
+ else
+ {
+ String model;
+ char[] saved;
+
+ require('(');
+ saved = readBuffer;
+ dataBufferAppend('(');
+ skipWhitespace();
+ if (tryRead("#PCDATA"))
+ {
+ dataBufferAppend("#PCDATA");
+ parseMixed(saved);
+ model = dataBufferToString();
+ setElement(name, CONTENT_MIXED, model, null);
+ }
+ else
+ {
+ parseElements(saved);
+ model = dataBufferToString();
+ setElement(name, CONTENT_ELEMENTS, model, null);
+ }
+ if (!skippedPE)
+ {
+ handler.getDeclHandler().elementDecl(name, model);
+ }
+ }
+ }
+
+ /**
+ * Parse an element-content model.
+ *
+ * [47] elements ::= (choice | seq) ('?' | '*' | '+')?
+ * [49] choice ::= '(' S? cp (S? '|' S? cp)+ S? ')'
+ * [50] seq ::= '(' S? cp (S? ',' S? cp)* S? ')'
+ *
+ *
+ *
+ * [48] cp ::= (Name | choice | seq) ('?' | '*' | '+')?
+ *
+ */
+ private void parseCp()
+ throws Exception
+ {
+ if (tryRead('('))
+ {
+ dataBufferAppend('(');
+ parseElements(readBuffer);
+ }
+ else
+ {
+ dataBufferAppend(readNmtoken(true));
+ char c = readCh();
+ switch (c)
+ {
+ case '?':
+ case '*':
+ case '+':
+ dataBufferAppend(c);
+ break;
+ default:
+ unread(c);
+ break;
+ }
+ }
+ }
+
+ /**
+ * Parse mixed content.
+ *
+ * [51] Mixed ::= '(' S? ( '#PCDATA' (S? '|' S? Name)*) S? ')*'
+ * | '(' S? ('#PCDATA') S? ')'
+ *
+ *
+ * @param saved Buffer for entity that should have the terminal ')'
+ */
+ private void parseMixed(char[] saved)
+ throws Exception
+ {
+ // Check for PCDATA alone.
+ skipWhitespace();
+ if (tryRead(')'))
+ {
+ // VC: Proper Group/PE Nesting
+ if (readBuffer != saved)
+ {
+ handler.verror("Illegal Group/PE nesting");
+ }
+
+ dataBufferAppend(")*");
+ tryRead('*');
+ return;
+ }
+
+ // Parse mixed content.
+ skipWhitespace();
+ while (!tryRead(")"))
+ {
+ require('|');
+ dataBufferAppend('|');
+ skipWhitespace();
+ dataBufferAppend(readNmtoken(true));
+ skipWhitespace();
+ }
+
+ // VC: Proper Group/PE Nesting
+ if (readBuffer != saved)
+ {
+ handler.verror("Illegal Group/PE nesting");
+ }
+
+ require('*');
+ dataBufferAppend(")*");
+ }
+
+ /**
+ * Parse an attribute list declaration.
+ *
+ * [52] AttlistDecl ::= '<!ATTLIST' S Name AttDef* S? '>'
+ *
+ *
+ * [53] AttDef ::= S Name S AttType S DefaultDecl
+ *
+ */
+ private void parseAttDef(String elementName)
+ throws Exception
+ {
+ String name;
+ String type;
+ String enumer = null;
+
+ // Read the attribute name.
+ name = readNmtoken(true);
+
+ // Read the attribute type.
+ requireWhitespace();
+ type = readAttType();
+
+ // Get the string of enumerated values if necessary.
+ if (handler.stringInterning)
+ {
+ if ("ENUMERATION" == type || "NOTATION" == type)
+ {
+ enumer = dataBufferToString();
+ }
+ }
+ else
+ {
+ if ("ENUMERATION".equals(type) || "NOTATION".equals(type))
+ {
+ enumer = dataBufferToString();
+ }
+ }
+
+ // Read the default value.
+ requireWhitespace();
+ parseDefault(elementName, name, type, enumer);
+ }
+
+ /**
+ * Parse the attribute type.
+ *
+ * [54] AttType ::= StringType | TokenizedType | EnumeratedType
+ * [55] StringType ::= 'CDATA'
+ * [56] TokenizedType ::= 'ID' | 'IDREF' | 'IDREFS' | 'ENTITY'
+ * | 'ENTITIES' | 'NMTOKEN' | 'NMTOKENS'
+ * [57] EnumeratedType ::= NotationType | Enumeration
+ *
+ */
+ private String readAttType()
+ throws Exception
+ {
+ if (tryRead('('))
+ {
+ parseEnumeration(false);
+ return "ENUMERATION";
+ }
+ else
+ {
+ String typeString = readNmtoken(true);
+ if (handler.stringInterning)
+ {
+ if ("NOTATION" == typeString)
+ {
+ parseNotationType();
+ return typeString;
+ }
+ else if ("CDATA" == typeString
+ || "ID" == typeString
+ || "IDREF" == typeString
+ || "IDREFS" == typeString
+ || "ENTITY" == typeString
+ || "ENTITIES" == typeString
+ || "NMTOKEN" == typeString
+ || "NMTOKENS" == typeString)
+ {
+ return typeString;
+ }
+ }
+ else
+ {
+ if ("NOTATION".equals(typeString))
+ {
+ parseNotationType();
+ return typeString;
+ }
+ else if ("CDATA".equals(typeString)
+ || "ID".equals(typeString)
+ || "IDREF".equals(typeString)
+ || "IDREFS".equals(typeString)
+ || "ENTITY".equals(typeString)
+ || "ENTITIES".equals(typeString)
+ || "NMTOKEN".equals(typeString)
+ || "NMTOKENS".equals(typeString))
+ {
+ return typeString;
+ }
+ }
+ error("illegal attribute type", typeString, null);
+ return null;
+ }
+ }
+
+ /**
+ * Parse an enumeration.
+ *
+ * [59] Enumeration ::= '(' S? Nmtoken (S? '|' S? Nmtoken)* S? ')'
+ *
+ *
+ * [58] NotationType ::= 'NOTATION' S '(' S? NameNtoks
+ * (S? '|' S? name)* S? ')'
+ *
+ *
+ * [60] DefaultDecl ::= '#REQUIRED' | '#IMPLIED'
+ * | (('#FIXED' S)? AttValue)
+ *
+ */
+ private void parseDefault(String elementName, String name,
+ String type, String enumer)
+ throws Exception
+ {
+ int valueType = ATTRIBUTE_DEFAULT_SPECIFIED;
+ String value = null;
+ int flags = LIT_ATTRIBUTE;
+ boolean saved = expandPE;
+ String defaultType = null;
+
+ // LIT_ATTRIBUTE forces '<' checks now (ASAP) and turns whitespace
+ // chars to spaces (doesn't matter when that's done if it doesn't
+ // interfere with char refs expanding to whitespace).
+
+ if (!skippedPE)
+ {
+ flags |= LIT_ENTITY_REF;
+ if (handler.stringInterning)
+ {
+ if ("CDATA" != type)
+ {
+ flags |= LIT_NORMALIZE;
+ }
+ }
+ else
+ {
+ if (!"CDATA".equals(type))
+ {
+ flags |= LIT_NORMALIZE;
+ }
+ }
+ }
+
+ expandPE = false;
+ if (tryRead('#'))
+ {
+ if (tryRead("FIXED"))
+ {
+ defaultType = "#FIXED";
+ valueType = ATTRIBUTE_DEFAULT_FIXED;
+ requireWhitespace();
+ value = readLiteral(flags);
+ }
+ else if (tryRead("REQUIRED"))
+ {
+ defaultType = "#REQUIRED";
+ valueType = ATTRIBUTE_DEFAULT_REQUIRED;
+ }
+ else if (tryRead("IMPLIED"))
+ {
+ defaultType = "#IMPLIED";
+ valueType = ATTRIBUTE_DEFAULT_IMPLIED;
+ }
+ else
+ {
+ error("illegal keyword for attribute default value");
+ }
+ }
+ else
+ {
+ value = readLiteral(flags);
+ }
+ expandPE = saved;
+ setAttribute(elementName, name, type, enumer, value, valueType);
+ if (handler.stringInterning)
+ {
+ if ("ENUMERATION" == type)
+ {
+ type = enumer;
+ }
+ else if ("NOTATION" == type)
+ {
+ type = "NOTATION " + enumer;
+ }
+ }
+ else
+ {
+ if ("ENUMERATION".equals(type))
+ {
+ type = enumer;
+ }
+ else if ("NOTATION".equals(type))
+ {
+ type = "NOTATION " + enumer;
+ }
+ }
+ if (!skippedPE)
+ {
+ handler.getDeclHandler().attributeDecl(elementName, name, type,
+ defaultType, value);
+ }
+ }
+
+ /**
+ * Parse a conditional section.
+ *
+ * [61] conditionalSect ::= includeSect || ignoreSect
+ * [62] includeSect ::= '<![' S? 'INCLUDE' S? '['
+ * extSubsetDecl ']]>'
+ * [63] ignoreSect ::= '<![' S? 'IGNORE' S? '['
+ * ignoreSectContents* ']]>'
+ * [64] ignoreSectContents ::= Ignore
+ * ('<![' ignoreSectContents* ']]>' Ignore )*
+ * [65] Ignore ::= Char* - (Char* ( '<![' | ']]>') Char* )
+ *
+ *
+ * [66] CharRef ::= '' [0-9]+ ';' | '' [0-9a-fA-F]+ ';'
+ *
+ *
+ * [66] CharRef ::= '' [0-9]+ ';' | '' [0-9a-fA-F]+ ';'
+ *
+ *
+ * [68] EntityRef ::= '&' Name ';'
+ *
+ *
+ * [69] PEReference ::= '%' Name ';'
+ *
+ *
+ * [70] EntityDecl ::= GEDecl | PEDecl
+ * [71] GEDecl ::= '<!ENTITY' S Name S EntityDef S? '>'
+ * [72] PEDecl ::= '<!ENTITY' S '%' S Name S PEDef S? '>'
+ * [73] EntityDef ::= EntityValue | (ExternalID NDataDecl?)
+ * [74] PEDef ::= EntityValue | ExternalID
+ * [75] ExternalID ::= 'SYSTEM' S SystemLiteral
+ * | 'PUBLIC' S PubidLiteral S SystemLiteral
+ * [76] NDataDecl ::= S 'NDATA' S Name
+ *
+ *
+ * [82] NotationDecl ::= '<!NOTATION' S Name S
+ * (ExternalID | PublicID) S? '>'
+ * [83] PublicID ::= 'PUBLIC' S PubidLiteral
+ *
+ *
+ * [14] CharData ::= [^<&]* - ([^<&]* ']]>' [^<&]*)
+ *
+ */
+ private void parseCharData()
+ throws Exception
+ {
+ char c;
+ int state = 0;
+ boolean pureWhite = false;
+
+ // assert (dataBufferPos == 0);
+
+ // are we expecting pure whitespace? it might be dirty...
+ if ((currentElementContent == CONTENT_ELEMENTS) && !isDirtyCurrentElement)
+ {
+ pureWhite = true;
+ }
+
+ // always report right out of readBuffer
+ // to minimize (pointless) buffer copies
+ while (true)
+ {
+ int lineAugment = 0;
+ int columnAugment = 0;
+ int i;
+
+loop:
+ for (i = readBufferPos; i < readBufferLength; i++)
+ {
+ switch (c = readBuffer[i])
+ {
+ case '\n':
+ lineAugment++;
+ columnAugment = 0;
+ // pureWhite unmodified
+ break;
+ case '\r': // should not happen!!
+ case '\t':
+ case ' ':
+ // pureWhite unmodified
+ columnAugment++;
+ break;
+ case '&':
+ case '<':
+ columnAugment++;
+ // pureWhite unmodified
+ // CLEAN end of text sequence
+ state = 1;
+ break loop;
+ case ']':
+ // that's not a whitespace char, and
+ // can not terminate pure whitespace either
+ pureWhite = false;
+ if ((i + 2) < readBufferLength)
+ {
+ if (readBuffer [i + 1] == ']'
+ && readBuffer [i + 2] == '>')
+ {
+ // ERROR end of text sequence
+ state = 2;
+ break loop;
+ }
+ }
+ else
+ {
+ // FIXME missing two end-of-buffer cases
+ }
+ columnAugment++;
+ break;
+ default:
+ if ((c < 0x0020 || c > 0xFFFD)
+ || ((c >= 0x007f) && (c <= 0x009f) && (c != 0x0085)
+ && xmlVersion == XML_11))
+ {
+ error("illegal XML character U+"
+ + Integer.toHexString(c));
+ }
+ // that's not a whitespace char
+ pureWhite = false;
+ columnAugment++;
+ }
+ }
+
+ // report text thus far
+ if (lineAugment > 0)
+ {
+ line += lineAugment;
+ column = columnAugment;
+ }
+ else
+ {
+ column += columnAugment;
+ }
+
+ // report characters/whitspace
+ int length = i - readBufferPos;
+
+ if (length != 0)
+ {
+ if (pureWhite)
+ {
+ handler.ignorableWhitespace(readBuffer,
+ readBufferPos, length);
+ }
+ else
+ {
+ handler.charData(readBuffer, readBufferPos, length);
+ }
+ readBufferPos = i;
+ }
+
+ if (state != 0)
+ {
+ break;
+ }
+
+ // fill next buffer from this entity, or
+ // pop stack and continue with previous entity
+ unread(readCh());
+ }
+ if (!pureWhite)
+ {
+ isDirtyCurrentElement = true;
+ }
+ // finish, maybe with error
+ if (state != 1) // finish, no error
+ {
+ error("character data may not contain ']]>'");
+ }
+ }
+
+ //////////////////////////////////////////////////////////////////////
+ // High-level reading and scanning methods.
+ //////////////////////////////////////////////////////////////////////
+
+ /**
+ * Require whitespace characters.
+ */
+ private void requireWhitespace()
+ throws SAXException, IOException
+ {
+ char c = readCh();
+ if (isWhitespace(c))
+ {
+ skipWhitespace();
+ }
+ else
+ {
+ error("whitespace required", c, null);
+ }
+ }
+
+ /**
+ * Skip whitespace characters.
+ *
+ * [3] S ::= (#x20 | #x9 | #xd | #xa)+
+ *
+ */
+ private void skipWhitespace()
+ throws SAXException, IOException
+ {
+ // Start with a little cheat. Most of
+ // the time, the white space will fall
+ // within the current read buffer; if
+ // not, then fall through.
+ if (USE_CHEATS)
+ {
+ int lineAugment = 0;
+ int columnAugment = 0;
+
+loop:
+ for (int i = readBufferPos; i < readBufferLength; i++)
+ {
+ switch (readBuffer[i])
+ {
+ case ' ':
+ case '\t':
+ case '\r':
+ columnAugment++;
+ break;
+ case '\n':
+ lineAugment++;
+ columnAugment = 0;
+ break;
+ case '%':
+ if (expandPE)
+ {
+ break loop;
+ }
+ // else fall through...
+ default:
+ readBufferPos = i;
+ if (lineAugment > 0)
+ {
+ line += lineAugment;
+ column = columnAugment;
+ }
+ else
+ {
+ column += columnAugment;
+ }
+ return;
+ }
+ }
+ }
+
+ // OK, do it the slow way.
+ char c = readCh ();
+ while (isWhitespace(c))
+ {
+ c = readCh();
+ }
+ unread(c);
+ }
+
+ /**
+ * Read a name or (when parsing an enumeration) name token.
+ *
+ * [5] Name ::= (Letter | '_' | ':') (NameChar)*
+ * [7] Nmtoken ::= (NameChar)+
+ *
+ */
+ private String readNmtoken(boolean isName)
+ throws SAXException, IOException
+ {
+ char c;
+
+ if (USE_CHEATS)
+ {
+loop:
+ for (int i = readBufferPos; i < readBufferLength; i++)
+ {
+ c = readBuffer[i];
+ switch (c)
+ {
+ case '%':
+ if (expandPE)
+ {
+ break loop;
+ }
+ // else fall through...
+
+ // What may legitimately come AFTER a name/nmtoken?
+ case '<': case '>': case '&':
+ case ',': case '|': case '*': case '+': case '?':
+ case ')':
+ case '=':
+ case '\'': case '"':
+ case '[':
+ case ' ': case '\t': case '\r': case '\n':
+ case ';':
+ case '/':
+ int start = readBufferPos;
+ if (i == start)
+ {
+ error("name expected", readBuffer[i], null);
+ }
+ readBufferPos = i;
+ return intern(readBuffer, start, i - start);
+
+ default:
+ // FIXME ... per IBM's OASIS test submission, these:
+ // ? U+06dd
+ // Combining U+309B
+ //these switches are kind of ugly but at least we won't
+ //have to go over the whole lits for each char
+ if (isName && i == readBufferPos)
+ {
+ char c2 = (char) (c & 0x00f0);
+ switch (c & 0xff00)
+ {
+ //starting with 01
+ case 0x0100:
+ switch (c2)
+ {
+ case 0x0030:
+ if (c == 0x0132 || c == 0x0133 || c == 0x013f)
+ {
+ error("Not a name start character, U+"
+ + Integer.toHexString(c));
+ }
+ break;
+ case 0x0040:
+ if (c == 0x0140 || c == 0x0149)
+ {
+ error("Not a name start character, U+"
+ + Integer.toHexString(c));
+ }
+ break;
+ case 0x00c0:
+ if (c == 0x01c4 || c == 0x01cc)
+ {
+ error("Not a name start character, U+"
+ + Integer.toHexString(c));
+ }
+ break;
+ case 0x00f0:
+ if (c == 0x01f1 || c == 0x01f3)
+ {
+ error("Not a name start character, U+"
+ + Integer.toHexString(c));
+ }
+ break;
+ case 0x00b0:
+ if (c == 0x01f1 || c == 0x01f3)
+ {
+ error("Not a name start character, U+"
+ + Integer.toHexString(c));
+ }
+ break;
+ default:
+ if (c == 0x017f)
+ {
+ error("Not a name start character, U+"
+ + Integer.toHexString(c));
+ }
+ }
+
+ break;
+ //starting with 11
+ case 0x1100:
+ switch (c2)
+ {
+ case 0x0000:
+ if (c == 0x1104 || c == 0x1108 ||
+ c == 0x110a || c == 0x110d)
+ {
+ error("Not a name start character, U+"
+ + Integer.toHexString(c));
+ }
+ break;
+ case 0x0030:
+ if (c == 0x113b || c == 0x113f)
+ {
+ error("Not a name start character, U+"
+ + Integer.toHexString(c));
+ }
+ break;
+ case 0x0040:
+ if (c == 0x1141 || c == 0x114d
+ || c == 0x114f )
+ {
+ error("Not a name start character, U+"
+ + Integer.toHexString(c));
+ }
+ break;
+ case 0x0050:
+ if (c == 0x1151 || c == 0x1156)
+ {
+ error("Not a name start character, U+"
+ + Integer.toHexString(c));
+ }
+ break;
+ case 0x0060:
+ if (c == 0x1162 || c == 0x1164
+ || c == 0x1166 || c == 0x116b
+ || c == 0x116f)
+ {
+ error("Not a name start character, U+"
+ + Integer.toHexString(c));
+ }
+ break;
+ case 0x00b0:
+ if (c == 0x11b6 || c == 0x11b9
+ || c == 0x11bb || c == 0x116f)
+ {
+ error("Not a name start character, U+"
+ + Integer.toHexString(c));
+ }
+ break;
+ default:
+ if (c == 0x1174 || c == 0x119f
+ || c == 0x11ac || c == 0x11c3
+ || c == 0x11f1)
+ {
+ error("Not a name start character, U+"
+ + Integer.toHexString(c));
+ }
+ }
+ break;
+ default:
+ if (c == 0x0e46 || c == 0x1011
+ || c == 0x212f || c == 0x0587
+ || c == 0x0230 )
+ {
+ error("Not a name start character, U+"
+ + Integer.toHexString(c));
+ }
+ }
+ }
+ // punt on exact tests from Appendix A; approximate
+ // them using the Unicode ID start/part rules
+ if (i == readBufferPos && isName)
+ {
+ if (!Character.isUnicodeIdentifierStart(c)
+ && c != ':' && c != '_')
+ {
+ error("Not a name start character, U+"
+ + Integer.toHexString(c));
+ }
+ }
+ else if (!Character.isUnicodeIdentifierPart(c)
+ && c != '-' && c != ':' && c != '_' && c != '.'
+ && !isExtender(c))
+ {
+ error("Not a name character, U+"
+ + Integer.toHexString(c));
+ }
+ }
+ }
+ }
+
+ nameBufferPos = 0;
+
+ // Read the first character.
+ while (true)
+ {
+ c = readCh();
+ switch (c)
+ {
+ case '%':
+ case '<': case '>': case '&':
+ case ',': case '|': case '*': case '+': case '?':
+ case ')':
+ case '=':
+ case '\'': case '"':
+ case '[':
+ case ' ': case '\t': case '\n': case '\r':
+ case ';':
+ case '/':
+ unread(c);
+ if (nameBufferPos == 0)
+ {
+ error ("name expected");
+ }
+ // punt on exact tests from Appendix A, but approximate them
+ if (isName
+ && !Character.isUnicodeIdentifierStart(nameBuffer[0])
+ && ":_".indexOf(nameBuffer[0]) == -1)
+ {
+ error("Not a name start character, U+"
+ + Integer.toHexString(nameBuffer[0]));
+ }
+ String s = intern(nameBuffer, 0, nameBufferPos);
+ nameBufferPos = 0;
+ return s;
+ default:
+ // punt on exact tests from Appendix A, but approximate them
+
+ if ((nameBufferPos != 0 || !isName)
+ && !Character.isUnicodeIdentifierPart(c)
+ && ":-_.".indexOf(c) == -1
+ && !isExtender(c))
+ {
+ error("Not a name character, U+"
+ + Integer.toHexString(c));
+ }
+ if (nameBufferPos >= nameBuffer.length)
+ {
+ nameBuffer =
+ (char[]) extendArray(nameBuffer,
+ nameBuffer.length, nameBufferPos);
+ }
+ nameBuffer[nameBufferPos++] = c;
+ }
+ }
+ }
+
+ private static boolean isExtender(char c)
+ {
+ // [88] Extender ::= ...
+ return c == 0x00b7 || c == 0x02d0 || c == 0x02d1 || c == 0x0387
+ || c == 0x0640 || c == 0x0e46 || c == 0x0ec6 || c == 0x3005
+ || (c >= 0x3031 && c <= 0x3035)
+ || (c >= 0x309d && c <= 0x309e)
+ || (c >= 0x30fc && c <= 0x30fe);
+ }
+
+ /**
+ * Read a literal. With matching single or double quotes as
+ * delimiters (and not embedded!) this is used to parse:
+ *
+ * [9] EntityValue ::= ... ([^%&] | PEReference | Reference)* ...
+ * [10] AttValue ::= ... ([^<&] | Reference)* ...
+ * [11] SystemLiteral ::= ... (URLchar - "'")* ...
+ * [12] PubidLiteral ::= ... (PubidChar - "'")* ...
+ *
+ * as well as the quoted strings in XML and text declarations
+ * (for version, encoding, and standalone) which have their
+ * own constraints.
+ */
+ private String readLiteral(int flags)
+ throws SAXException, IOException
+ {
+ char delim, c;
+ int startLine = line;
+ boolean saved = expandPE;
+ boolean savedReport = doReport;
+
+ // Find the first delimiter.
+ delim = readCh();
+ if (delim != '"' && delim != '\'')
+ {
+ error("expected '\"' or \"'\"", delim, null);
+ return null;
+ }
+ inLiteral = true;
+ if ((flags & LIT_DISABLE_PE) != 0)
+ {
+ expandPE = false;
+ }
+ doReport = false;
+
+ // Each level of input source has its own buffer; remember
+ // ours, so we won't read the ending delimiter from any
+ // other input source, regardless of entity processing.
+ char[] ourBuf = readBuffer;
+
+ // Read the literal.
+ try
+ {
+ c = readCh();
+ boolean ampRead = false;
+loop:
+ while (! (c == delim && readBuffer == ourBuf))
+ {
+ switch (c)
+ {
+ // attributes and public ids are normalized
+ // in almost the same ways
+ case '\n':
+ case '\r':
+ if ((flags & (LIT_ATTRIBUTE | LIT_PUBID)) != 0)
+ {
+ c = ' ';
+ }
+ break;
+ case '\t':
+ if ((flags & LIT_ATTRIBUTE) != 0)
+ {
+ c = ' ';
+ }
+ break;
+ case '&':
+ c = readCh();
+ // Char refs are expanded immediately, except for
+ // all the cases where it's deferred.
+ if (c == '#')
+ {
+ if ((flags & LIT_DISABLE_CREF) != 0)
+ {
+ dataBufferAppend('&');
+ break;
+ }
+ parseCharRef(false /* Do not do flushDataBuffer */);
+
+ // exotic WFness risk: this is an entity literal,
+ // dataBuffer [dataBufferPos - 1] == '&', and
+ // following chars are a _partial_ entity/char ref
+
+ // It looks like an entity ref ...
+ }
+ else
+ {
+ unread(c);
+ // Expand it?
+ if ((flags & LIT_ENTITY_REF) > 0)
+ {
+ parseEntityRef(false);
+ if (String.valueOf(readBuffer).equals("&"))
+ {
+ ampRead = true;
+ }
+ //Is it just data?
+ }
+ else if ((flags & LIT_DISABLE_EREF) != 0)
+ {
+ dataBufferAppend('&');
+
+ // OK, it will be an entity ref -- expanded later.
+ }
+ else
+ {
+ String name = readNmtoken(true);
+ require(';');
+ dataBufferAppend('&');
+ dataBufferAppend(name);
+ dataBufferAppend(';');
+ }
+ }
+ c = readCh();
+ continue loop;
+
+ case '<':
+ // and why? Perhaps so "&foo;" expands the same
+ // inside and outside an attribute?
+ if ((flags & LIT_ATTRIBUTE) != 0)
+ {
+ error("attribute values may not contain '<'");
+ }
+ break;
+
+ // We don't worry about case '%' and PE refs, readCh does.
+
+ default:
+ break;
+ }
+ dataBufferAppend(c);
+ c = readCh();
+ }
+ }
+ catch (EOFException e)
+ {
+ error("end of input while looking for delimiter (started on line "
+ + startLine + ')', null, Character.toString(delim));
+ }
+ inLiteral = false;
+ expandPE = saved;
+ doReport = savedReport;
+
+ // Normalise whitespace if necessary.
+ if ((flags & LIT_NORMALIZE) > 0)
+ {
+ dataBufferNormalize();
+ }
+
+ // Return the value.
+ return dataBufferToString();
+ }
+
+ /**
+ * Try reading external identifiers.
+ * A system identifier is not required for notations.
+ * @param inNotation Are we parsing a notation decl?
+ * @param isSubset Parsing external subset decl (may be omitted)?
+ * @return A three-member String array containing the identifiers,
+ * or nulls. Order: public, system, baseURI.
+ */
+ private ExternalIdentifiers readExternalIds(boolean inNotation,
+ boolean isSubset)
+ throws Exception
+ {
+ char c;
+ ExternalIdentifiers ids = new ExternalIdentifiers();
+ int flags = LIT_DISABLE_CREF | LIT_DISABLE_PE | LIT_DISABLE_EREF;
+
+ if (tryRead("PUBLIC"))
+ {
+ requireWhitespace();
+ ids.publicId = readLiteral(LIT_NORMALIZE | LIT_PUBID | flags);
+ if (inNotation)
+ {
+ skipWhitespace();
+ c = readCh();
+ unread(c);
+ if (c == '"' || c == '\'')
+ {
+ ids.systemId = readLiteral(flags);
+ }
+ }
+ else
+ {
+ requireWhitespace();
+ ids.systemId = readLiteral(flags);
+ }
+
+ for (int i = 0; i < ids.publicId.length(); i++)
+ {
+ c = ids.publicId.charAt(i);
+ if (c >= 'a' && c <= 'z')
+ {
+ continue;
+ }
+ if (c >= 'A' && c <= 'Z')
+ {
+ continue;
+ }
+ if (" \r\n0123456789-' ()+,./:=?;!*#@$_%".indexOf(c) != -1)
+ {
+ continue;
+ }
+ error("illegal PUBLIC id character U+"
+ + Integer.toHexString(c));
+ }
+ }
+ else if (tryRead("SYSTEM"))
+ {
+ requireWhitespace();
+ ids.systemId = readLiteral(flags);
+ }
+ else if (!isSubset)
+ {
+ error("missing SYSTEM or PUBLIC keyword");
+ }
+
+ if (ids.systemId != null)
+ {
+ if (ids.systemId.indexOf('#') != -1)
+ {
+ handler.verror("SYSTEM id has a URI fragment: " + ids.systemId);
+ }
+ ids.baseUri = handler.getSystemId();
+ if (ids.baseUri == null && uriWarnings)
+ {
+ handler.warn("No base URI; hope URI is absolute: "
+ + ids.systemId);
+ }
+ }
+
+ return ids;
+ }
+
+ /**
+ * Test if a character is whitespace.
+ *
+ * [3] S ::= (#x20 | #x9 | #xd | #xa)+
+ *
+ * @param c The character to test.
+ * @return true if the character is whitespace.
+ */
+ private final boolean isWhitespace(char c)
+ {
+ if (c > 0x20)
+ {
+ return false;
+ }
+ if (c == 0x20 || c == 0x0a || c == 0x09 || c == 0x0d)
+ {
+ return true;
+ }
+ return false; // illegal ...
+ }
+
+ //////////////////////////////////////////////////////////////////////
+ // Utility routines.
+ //////////////////////////////////////////////////////////////////////
+
+ /**
+ * Add a character to the data buffer.
+ */
+ private void dataBufferAppend(char c)
+ {
+ // Expand buffer if necessary.
+ if (dataBufferPos >= dataBuffer.length)
+ {
+ dataBuffer = (char[]) extendArray(dataBuffer,
+ dataBuffer.length, dataBufferPos);
+ }
+ dataBuffer[dataBufferPos++] = c;
+ }
+
+ /**
+ * Add a string to the data buffer.
+ */
+ private void dataBufferAppend(String s)
+ {
+ dataBufferAppend(s.toCharArray(), 0, s.length());
+ }
+
+ /**
+ * Append (part of) a character array to the data buffer.
+ */
+ private void dataBufferAppend(char[] ch, int start, int length)
+ {
+ dataBuffer = (char[]) extendArray(dataBuffer, dataBuffer.length,
+ dataBufferPos + length);
+
+ System.arraycopy(ch, start, dataBuffer, dataBufferPos, length);
+ dataBufferPos += length;
+ }
+
+ /**
+ * Normalise space characters in the data buffer.
+ */
+ private void dataBufferNormalize()
+ {
+ int i = 0;
+ int j = 0;
+ int end = dataBufferPos;
+
+ // Skip spaces at the start.
+ while (j < end && dataBuffer[j] == ' ')
+ {
+ j++;
+ }
+
+ // Skip whitespace at the end.
+ while (end > j && dataBuffer[end - 1] == ' ')
+ {
+ end --;
+ }
+
+ // Start copying to the left.
+ while (j < end)
+ {
+
+ char c = dataBuffer[j++];
+
+ // Normalise all other spaces to
+ // a single space.
+ if (c == ' ')
+ {
+ while (j < end && dataBuffer[j++] == ' ')
+ {
+ continue;
+ }
+ dataBuffer[i++] = ' ';
+ dataBuffer[i++] = dataBuffer[j - 1];
+ }
+ else
+ {
+ dataBuffer[i++] = c;
+ }
+ }
+
+ // The new length is <= the old one.
+ dataBufferPos = i;
+ }
+
+ /**
+ * Convert the data buffer to a string.
+ */
+ private String dataBufferToString()
+ {
+ String s = new String(dataBuffer, 0, dataBufferPos);
+ dataBufferPos = 0;
+ return s;
+ }
+
+ /**
+ * Flush the contents of the data buffer to the handler, as
+ * appropriate, and reset the buffer for new input.
+ */
+ private void dataBufferFlush()
+ throws SAXException
+ {
+ if (currentElementContent == CONTENT_ELEMENTS
+ && dataBufferPos > 0
+ && !inCDATA)
+ {
+ // We can't just trust the buffer to be whitespace, there
+ // are (error) cases when it isn't
+ for (int i = 0; i < dataBufferPos; i++)
+ {
+ if (!isWhitespace(dataBuffer[i]))
+ {
+ handler.charData(dataBuffer, 0, dataBufferPos);
+ dataBufferPos = 0;
+ }
+ }
+ if (dataBufferPos > 0)
+ {
+ handler.ignorableWhitespace(dataBuffer, 0, dataBufferPos);
+ dataBufferPos = 0;
+ }
+ }
+ else if (dataBufferPos > 0)
+ {
+ handler.charData(dataBuffer, 0, dataBufferPos);
+ dataBufferPos = 0;
+ }
+ }
+
+ /**
+ * Require a string to appear, or throw an exception.
+ * ==
instead of String.equals ()
.
+ *
+ *
+ *
+ * @param ename The name of the entity (if any) causing the new input.
+ * @see #popInput
+ * @see #sourceType
+ * @see #externalEntity
+ * @see #readBuffer
+ * @see #readBufferPos
+ * @see #readBufferLength
+ * @see #line
+ * @see #encoding
+ */
+ private void pushInput(String ename)
+ throws SAXException
+ {
+ // Check for entity recursion.
+ if (ename != null)
+ {
+ Iterator entities = entityStack.iterator();
+ while (entities.hasNext())
+ {
+ String e = (String) entities.next();
+ if (e != null && e == ename)
+ {
+ error("recursive reference to entity", ename, null);
+ }
+ }
+ }
+ entityStack.addLast(ename);
+
+ // Don't bother if there is no current input.
+ if (sourceType == INPUT_NONE)
+ {
+ return;
+ }
+
+ // Set up a snapshot of the current
+ // input source.
+ Input input = new Input();
+
+ input.sourceType = sourceType;
+ input.externalEntity = externalEntity;
+ input.readBuffer = readBuffer;
+ input.readBufferPos = readBufferPos;
+ input.readBufferLength = readBufferLength;
+ input.line = line;
+ input.encoding = encoding;
+ input.readBufferOverflow = readBufferOverflow;
+ input.is = is;
+ input.currentByteCount = currentByteCount;
+ input.column = column;
+ input.reader = reader;
+
+ // Push it onto the stack.
+ inputStack.addLast(input);
+ }
+
+ /**
+ * Restore a previous input source.
+ *
+
+
+
+
About Ælfred
+
+Design Principles
+
+
+
+
+
+About the Name Ælfred
+
+Character Encodings
+
+
+
+
+
+
+<?xml version="1.0" encoding="ISO-8859-15"?>
+
+
+java.io.InputStreamReader
+are now fully supported for both external labels (such as MIME types)
+and internal types (as shown above).
+There is one limitation in the support for internal labels:
+the encodings must be derived from the US-ASCII encoding,
+the EBCDIC family of encodings is not recognized.
+Note that Java defines its
+own encoding names, which don't always correspond to the standard
+Internet encoding names defined by the IETF/IANA, and that Java
+may even require use of nonstandard encoding names.
+Please report
+such problems; some of them can be worked around in this parser,
+and many can be worked around by using external labels.
+Known Conformance Violations
+
+
+
+
+
+Copyright and distribution terms
+
+
+ Linking this library statically or dynamically with other modules is
+ making a combined work based on this library. Thus, the terms and
+ conditions of the GNU General Public License cover the whole
+ combination.
+
+ As a special exception, the copyright holders of this library give you
+ permission to link this library with independent modules to produce an
+ executable, regardless of the license terms of these independent
+ modules, and to copy and distribute the resulting executable under
+ terms of your choice, provided that you also meet, for each linked
+ independent module, the terms and conditions of the license of that
+ module. An independent module is a module which is not derived from
+ or based on this library. If you modify this library, you may extend
+ this exception to your version of the library, but you are not
+ obligated to do so. If you do not wish to do so, delete this
+ exception statement from your version.
+
+ Parts derived from code which carried the following notice:
+
+ Copyright (c) 1997, 1998 by Microstar Software Ltd.
+
+ AElfred is free for both commercial and non-commercial use and
+ redistribution, provided that Microstar's copyright and disclaimer are
+ retained intact. You are free to modify AElfred for your own use and
+ to redistribute AElfred with your modifications, provided that the
+ modifications are clearly documented.
+
+ This program is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ merchantability or fitness for a particular purpose. Please use it AT
+ YOUR OWN RISK.
+
+
+
As noted above, Microstar has not updated this parser since +the summer of 1998, when it released version 1.2a on its web site. +This release is intended to benefit the developer community by +refocusing the API on SAX2, and improving conformance to the extent +that most developers should not need to use another XML parser.
+ +The code has been cleaned up (referring to the XML 1.0 spec in +all the production numbers in +comments, rather than some preliminary draft, for one example) and +has been sped up a bit as well. +JAXP support has been added, although developers are still +strongly encouraged to use the SAX2 APIs directly.
+ + +The original version of Ælfred did not support the +SAX2 APIs.
+ +This version supports the SAX2 APIs, exposing the standard +boolean feature descriptors. It supports the "DeclHandler" property +to provide access to all DTD declarations not already exposed +through the SAX1 API. The "LexicalHandler" property is supported, +exposing entity boundaries (including the unnamed external subset) and +things like comments and CDATA boundaries. SAX1 compatibility is +currently provided.
+ + +In the 'pipeline' package in this same software distribution is an +XML Validation component +using any full SAX2 event stream (including all document type declarations) +to validate. There is now a XmlReader class +which combines that class and this enhanced Ælfred parser, creating +an optionally validating SAX2 parser.
+ +As noted in the documentation for that validating component, certain +validity constraints can't reliably be tested by a layered validator. +These include all constraints relying on +layering violations (exposing XML at the level of tokens or below, +required since XML isn't a context-free grammar), some that +SAX2 doesn't support, and a few others. The resulting validating +parser is conformant enough for most applications that aren't doing +strange SGML tricks with DTDs. +Moreover, that validating filter can be used without +a parser ... any application component that emits SAX event streams +can DTD-validate its output on demand.
+ +You'll have noticed that the original version of Ælfred +had small size as a top goal. Ælfred2 normally includes a +DTD validation layer, but you can package without that. +Similarly, JAXP factory support is available but optional. +Then the main added cost due to this revision are for +supporting the SAX2 API itself; DTD validation is as +cleanly layered as allowed by SAX2.
+ +Bugs fixed in Ælfred2 include:
+ +Other bugs may also have been fixed.
+ +For better overall validation support, some of the validity +constraints that can't be verified using the SAX2 event stream +are now reported directly by Ælfred2.
+ + + diff --git a/libjava/classpath/gnu/xml/dom/Consumer.java b/libjava/classpath/gnu/xml/dom/Consumer.java new file mode 100644 index 000000000..836c8d257 --- /dev/null +++ b/libjava/classpath/gnu/xml/dom/Consumer.java @@ -0,0 +1,352 @@ +/* Consumer.java -- + Copyright (C) 2001,2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.xml.dom; + +import org.w3c.dom.DocumentType; +import org.w3c.dom.Node; +import org.w3c.dom.Text; + +import org.xml.sax.Attributes; +import org.xml.sax.SAXException; +import org.xml.sax.ext.Attributes2; + +import gnu.xml.pipeline.DomConsumer; +import gnu.xml.pipeline.EventConsumer; + + +/** + * Event consumer which constructs DOM documents using the implementation + * in this package, using SAX2 events. This packages various backdoors + * into this DOM implementation, as needed to address DOM requirements + * that can't be met by strictly conforming implementations of DOM. + * + *These requirements all relate to {@link DocumentType} nodes and + * features of that node type. These features are normally not used, + * because that interface only exposes a subset of the information found + * in DTDs. More, that subset does not include the most important typing + * information. For example, it excludes element content models and + * attribute typing. It does expose some entity management issues, + * although entity management doesn't relate to document typing. + * + *
Note that SAX2 does not expose the literal text of the DTD's + * internal subset, so it will not be present in DOM trees constructed + * using this API. (Though with a good SAX2 implementation, it could + * be partially recreated...) + * + * @author David Brownell + */ +public class Consumer extends DomConsumer +{ + /** + * Constructs an unconfigured event consumer, + * as a terminus in a SAX event pipeline. + */ + // used by PipelineFactory [terminus] + public Consumer () + throws SAXException + { + super (DomDocument.class); + setHandler (new Backdoor (this)); + } + + /** + * Constructs an unconfigured event consumer, + * as a stage in a SAX event pipeline. + */ + // used by PipelineFactory [filter] + public Consumer (EventConsumer next) + throws SAXException + { + super (DomDocument.class, next); + setHandler (new Backdoor (this)); + } + + /** + * Implements the backdoors needed by DOM. + * All methods in this class use implementation-specific APIs that are + * implied by the DOM specification (needed to implement testable + * behavior) but which are excluded from the DOM specification. + */ + public static class Backdoor extends DomConsumer.Handler + { + /** + * Constructor. + * @param consumer must have been initialized to use the + * {@link DomDocument} class (or a subclass) for + * constructing DOM trees + */ + protected Backdoor (DomConsumer consumer) + throws SAXException + { super (consumer); } + + // helper routine + private DomDoctype getDoctype () + throws SAXException + { + DomDocument doc = (DomDocument) getDocument (); + DocumentType dt = doc.getDoctype (); + + if (dt == null) + throw new SAXException ("doctype missing!"); + return (DomDoctype) dt; + } + + // SAX2 "lexical" event + public void startDTD (String name, String publicId, String systemId) + throws SAXException + { + DomDocument doc = (DomDocument) getDocument (); + + super.startDTD (name, publicId, systemId); + // DOM L2 doctype creation model is bizarre + DomDoctype dt = new DomDoctype (doc, name, publicId, systemId); + doc.appendChild (dt); + } + + // SAX2 "lexical" event + public void endDTD () + throws SAXException + { + super.endDTD (); + // DOM L2 has no way to make things readonly + getDoctype ().makeReadonly (); + } + + // SAX1 DTD event + public void notationDecl ( + String name, + String publicId, String systemId + ) throws SAXException + { + // DOM L2 can't create/save notation nodes + getDoctype ().declareNotation (name, publicId, systemId); + } + + // SAX1 DTD event + public void unparsedEntityDecl ( + String name, + String publicId, String systemId, + String notationName + ) throws SAXException + { + // DOM L2 can't create/save entity nodes + getDoctype ().declareEntity (name, publicId, systemId, + notationName); + } + + // SAX2 declaration event + public void internalEntityDecl (String name, String value) + throws SAXException + { + // DOM L2 can't create/save entity nodes + // NOTE: this doesn't save the value as a child of this + // node, though it could realistically do so. + getDoctype ().declareEntity (name, null, null, null); + } + + // SAX2 declaration event + public void externalEntityDecl ( + String name, + String publicId, + String systemId + ) throws SAXException + { + // DOM L2 can't create/save entity nodes + // NOTE: DOM allows for these to have children, if + // they don't have unbound namespace references. + getDoctype ().declareEntity (name, publicId, systemId, null); + } + + // SAX2 element + public void startElement ( + String uri, + String localName, + String qName, + Attributes atts + ) throws SAXException + { + Node top; + + super.startElement (uri, localName, qName, atts); + + // might there be more work? + top = getTop (); + if (!top.hasAttributes () || !(atts instanceof Attributes2)) + return; + + // remember any attributes that got defaulted + DomNamedNodeMap map = (DomNamedNodeMap) top.getAttributes (); + Attributes2 attrs = (Attributes2) atts; + int length = atts.getLength (); + + //map.compact (); + for (int i = 0; i < length; i++) { + if (attrs.isSpecified (i)) + continue; + + // value was defaulted. + String temp = attrs.getQName (i); + DomAttr attr; + + if ("".equals (temp)) + attr = (DomAttr) map.getNamedItemNS (attrs.getURI (i), + atts.getLocalName (i)); + else + attr = (DomAttr) map.getNamedItem (temp); + + // DOM L2 can't write this flag, only read it + attr.setSpecified (false); + } + } + + public void endElement ( + String uri, + String localName, + String qName + ) throws SAXException + { + DomNode top = (DomNode) getTop (); + top.compact (); + super.endElement (uri, localName, qName); + } + + protected Text createText ( + boolean isCDATA, + char buf [], + int off, + int len + ) { + DomDocument doc = (DomDocument) getDocument (); + + if (isCDATA) + return doc.createCDATASection (buf, off, len); + else + return doc.createTextNode (buf, off, len); + } + + public void elementDecl(String name, String model) + throws SAXException + { + getDoctype().elementDecl(name, model); + } + + public void attributeDecl ( + String ename, + String aname, + String type, + String mode, + String value + ) throws SAXException + { + getDoctype().attributeDecl(ename, aname, type, mode, value); + /* + if (value == null && !"ID".equals (type)) + return; + + DomDoctype.ElementInfo info; + + info = getDoctype ().getElementInfo (ename); + if (value != null) + info.setAttrDefault (aname, value); + if ("ID".equals (type)) + info.setIdAttr (aname); + */ + + } + + // force duplicate name checking off while we're + // using parser output (don't duplicate the work) + public void startDocument () throws SAXException + { + super.startDocument (); + + DomDocument doc = (DomDocument) getDocument (); + doc.setStrictErrorChecking(false); + doc.setBuilding(true); + } + + public void endDocument () + throws SAXException + { + DomDocument doc = (DomDocument) getDocument (); + doc.setStrictErrorChecking(true); + doc.setBuilding(false); + doc.compact (); + DomDoctype doctype = (DomDoctype) doc.getDoctype(); + if (doctype != null) + { + doctype.makeReadonly(); + } + super.endDocument (); + } + + // these three methods collaborate to populate entity + // refs, marking contents readonly on end-of-entity + + public boolean canPopulateEntityRefs () + { return true; } + + public void startEntity (String name) + throws SAXException + { + if (name.charAt (0) == '%' || "[dtd]".equals (name)) + return; + super.startEntity (name); + + DomNode top = (DomNode) getTop (); + + if (top.getNodeType () == Node.ENTITY_REFERENCE_NODE) + top.readonly = false; + } + + public void endEntity (String name) + throws SAXException + { + if (name.charAt (0) == '%' || "[dtd]".equals (name)) + return; + DomNode top = (DomNode) getTop (); + + if (top.getNodeType () == Node.ENTITY_REFERENCE_NODE) { + top.compact (); + top.makeReadonly (); + } + super.endEntity (name); + } + } +} diff --git a/libjava/classpath/gnu/xml/dom/DTDAttributeTypeInfo.java b/libjava/classpath/gnu/xml/dom/DTDAttributeTypeInfo.java new file mode 100644 index 000000000..d6e25529d --- /dev/null +++ b/libjava/classpath/gnu/xml/dom/DTDAttributeTypeInfo.java @@ -0,0 +1,83 @@ +/* DTDAttributeTypeInfo.java -- + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.dom; + +import org.w3c.dom.TypeInfo; + +/** + * Attribute type information supplied by a DTD attribute declaration. + * + * @author Chris Burdess + */ +class DTDAttributeTypeInfo + implements TypeInfo +{ + + final String elementName; + final String name; + final String type; + final String mode; + final String value; + + DTDAttributeTypeInfo(String elementName, String name, String type, + String mode, String value) + { + this.elementName = elementName; + this.name = name; + this.type = type; + this.mode = mode; + this.value = value; + } + + public final String getTypeName() + { + return type; + } + + public final String getTypeNamespace() + { + return "http://www.w3.org/TR/REC-xml"; + } + + public final boolean isDerivedFrom(String typeNamespace, String typeName, + int derivationMethod) + { + return false; + } + +} diff --git a/libjava/classpath/gnu/xml/dom/DTDElementTypeInfo.java b/libjava/classpath/gnu/xml/dom/DTDElementTypeInfo.java new file mode 100644 index 000000000..9063d4c79 --- /dev/null +++ b/libjava/classpath/gnu/xml/dom/DTDElementTypeInfo.java @@ -0,0 +1,111 @@ +/* DTDElementTypeInfo.java -- + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.dom; + +import java.util.HashMap; +import java.util.Iterator; +import org.w3c.dom.TypeInfo; + +/** + * Element type information provided by a DTD element declaration. + * + * @author Chris Burdess + */ +class DTDElementTypeInfo + implements TypeInfo +{ + + final String name; + String model; + HashMap attributes; + String idAttrName; + + DTDElementTypeInfo(String name, String model) + { + this.name = name; + this.model = model; + } + + public final String getTypeName() + { + return null; + } + + public final String getTypeNamespace() + { + return "http://www.w3.org/TR/REC-xml"; + } + + public final boolean isDerivedFrom(String typeNamespace, String typeName, + int derivationMethod) + { + return false; + } + + DTDAttributeTypeInfo getAttributeTypeInfo(String name) + { + if (attributes == null) + { + return null; + } + return (DTDAttributeTypeInfo) attributes.get(name); + } + + void setAttributeTypeInfo(String name, DTDAttributeTypeInfo info) + { + if (attributes == null) + { + attributes = new HashMap(); + } + attributes.put(name, info); + if ("ID".equals(info.type)) + { + idAttrName = name; + } + } + + Iterator attributes() + { + if (attributes == null) + { + return null; + } + return attributes.values().iterator(); + } + +} diff --git a/libjava/classpath/gnu/xml/dom/DomAttr.java b/libjava/classpath/gnu/xml/dom/DomAttr.java new file mode 100644 index 000000000..421baf76a --- /dev/null +++ b/libjava/classpath/gnu/xml/dom/DomAttr.java @@ -0,0 +1,411 @@ +/* DomAttr.java -- + Copyright (C) 1999,2000,2001,2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.dom; + +import gnu.java.lang.CPStringBuilder; + +import org.w3c.dom.Attr; +import org.w3c.dom.DOMException; +import org.w3c.dom.Element; +import org.w3c.dom.Node; +import org.w3c.dom.TypeInfo; +import org.w3c.dom.events.MutationEvent; + + +/** + *
"Attr" implementation. In DOM, attributes cost quite a lot of + * memory because their values are complex structures rather than just + * simple strings. To reduce your costs, avoid having more than one + * child of an attribute; stick to a single Text node child, and ignore + * even that by using the attribute's "nodeValue" property.
+ * + *As a bit of general advice, only look at attribute modification + * events through the DOMAttrModified event (sent to the associated + * element). Implementations are not guaranteed to report other events + * in the same order, so you're very likely to write nonportable code if + * you monitor events at the "children of Attr" level.
+ * + *At this writing, not all attribute modifications will cause the + * DOMAttrModified event to be triggered ... only the ones using the string + * methods (setNodeValue, setValue, and Element.setAttribute) to modify + * those values. That is, if you manipulate those children directly, + * elements won't get notified that attribute values have changed. + * The natural fix for that will report other modifications, but won't + * be able to expose "previous" attribute value; it'll need to be cached + * or something (at which point why bother using child nodes).
+ * + *You are strongly advised not to use "children" of any attribute + * nodes you work with.
+ * + * @author David Brownell + * @author Chris Burdess + */ +public class DomAttr + extends DomNsNode + implements Attr +{ + + private boolean specified; + private String value; // string value cache + + /** + * Constructs an Attr node associated with the specified document. + * The "specified" flag is initialized to true, since this DOM has + * no current "back door" mechanisms to manage default values so + * that every value must effectively be "specified". + * + *This constructor should only be invoked by a Document as part of + * its createAttribute functionality, or through a subclass which is + * similarly used in a "Sub-DOM" style layer. + * + * @param owner The document with which this node is associated + * @param namespaceURI Combined with the local part of the name, + * this is used to uniquely identify a type of attribute + * @param name Name of this attribute, which may include a prefix + */ + protected DomAttr(DomDocument owner, String namespaceURI, String name) + { + super(ATTRIBUTE_NODE, owner, namespaceURI, name); + specified = true; + length = 1; + + // XXX register self to get insertion/removal events + // and character data change events and when they happen, + // report self-mutation + } + + /** + * Constructs an Attr node associated with the specified document. + * The "specified" flag is initialized to true, since this DOM has + * no current "back door" mechanisms to manage default values so + * that every value must effectively be "specified". + * + *
This constructor should only be invoked by a Document as part of + * its createAttribute functionality, or through a subclass which is + * similarly used in a "Sub-DOM" style layer. + *
+ * With this constructor, the prefix and local part are given explicitly + * rather than being computed. This allows them to be explicitly set to + * {@code null} as required by {@link Document#createAttribute(String)}. + *
+ * + * @param owner The document with which this node is associated + * @param namespaceURI Combined with the local part of the name, + * this is used to uniquely identify a type of attribute + * @param name Name of this attribute, which may include a prefix + * @param prefix the namespace prefix of the name. May be {@code null}. + * @param localName the local part of the name. May be {@code null}. + */ + protected DomAttr(DomDocument owner, String namespaceURI, String name, + String prefix, String localName) + { + super(ATTRIBUTE_NODE, owner, namespaceURI, name, prefix, localName); + specified = true; + length = 1; + } + + /** + * DOM L1 + * Returns the attribute name (same as getNodeName) + */ + public final String getName() + { + return getNodeName(); + } + + /** + * DOM L1 + * Returns true if a parser reported this was in the source text. + */ + public final boolean getSpecified() + { + return specified; + } + + /** + * Records whether this attribute was in the source text. + */ + public final void setSpecified(boolean value) + { + specified = value; + } + + /** + * DOM L1 + * Returns the attribute value, with character and entity + * references substituted. + * NOTE: entity refs as children aren't currently handled. + */ + public String getNodeValue() + { + // If we have a simple node-value, use that + if (first == null) + { + return (value == null) ? "" : value; + } + // Otherwise collect child node-values + CPStringBuilder buf = new CPStringBuilder(); + for (DomNode ctx = first; ctx != null; ctx = ctx.next) + { + switch (ctx.nodeType) + { + case Node.TEXT_NODE: + buf.append(ctx.getNodeValue()); + break; + case Node.ENTITY_REFERENCE_NODE: + // TODO + break; + } + } + return buf.toString(); + } + + /** + * DOM L1 + * Assigns the value of the attribute; it will have one child, + * which is a text node with the specified value (same as + * setNodeValue). + */ + public final void setValue(String value) + { + setNodeValue(value); + } + + /** + * DOM L1 + * Returns the value of the attribute as a non-null string; same + * as getNodeValue. + * NOTE: entity refs as children aren't currently handled. + */ + public final String getValue() + { + return getNodeValue(); + } + + /** + * DOM L1 + * Assigns the attribute value; using this API, no entity or + * character references will exist. + * Causes a DOMAttrModified mutation event to be sent. + */ + public void setNodeValue(String value) + { + if (readonly) + { + throw new DomDOMException(DOMException.NO_MODIFICATION_ALLOWED_ERR); + } + if (value == null) + { + value = ""; + } + String oldValue = getNodeValue(); + while (last != null) + { + removeChild(last); + } + // don't create a new node just for this... + /* + Node text = owner.createTextNode(value); + appendChild(text); + */ + this.value = value; + length = 1; + specified = true; + + mutating(oldValue, value, MutationEvent.MODIFICATION); + } + + public final Node getFirstChild() + { + // Create a child text node if necessary + if (first == null) + { + length = 0; + Node text = owner.createTextNode((value == null) ? "" : value); + appendChild(text); + } + return first; + } + + public final Node getLastChild() + { + // Create a child text node if necessary + if (last == null) + { + length = 0; + Node text = owner.createTextNode((value == null) ? "" : value); + appendChild(text); + } + return last; + } + + public Node item(int index) + { + // Create a child text node if necessary + if (first == null) + { + length = 0; + Node text = owner.createTextNode((value == null) ? "" : value); + appendChild(text); + } + return super.item(index); + } + + /** + * DOM L2 + * Returns the element with which this attribute is associated. + */ + public final Element getOwnerElement() + { + return (Element) parent; + } + + public final Node getNextSibling() + { + return null; + } + + public final Node getPreviousSibling() + { + return null; + } + + public Node getParentNode() + { + return null; + } + + /** + * Records the element with which this attribute is associated. + */ + public final void setOwnerElement(Element e) + { + if (parent != null) + { + throw new DomDOMException(DOMException.HIERARCHY_REQUEST_ERR); + } + if (!(e instanceof DomElement)) + { + throw new DomDOMException(DOMException.WRONG_DOCUMENT_ERR); + } + parent = (DomElement) e; + depth = parent.depth + 1; + } + + /** + * The base URI of an Attr is alwaysnull
.
+ */
+ public final String getBaseURI()
+ {
+ return null;
+ }
+
+ /**
+ * Shallow clone of the attribute, breaking all ties with any
+ * elements.
+ */
+ public Object clone()
+ {
+ DomAttr retval = (DomAttr) super.clone();
+ retval.specified = true;
+ return retval;
+ }
+
+ private void mutating(String oldValue, String newValue, short why)
+ {
+ if (!reportMutations || parent == null || equal(newValue, oldValue))
+ {
+ return;
+ }
+
+ // EVENT: DOMAttrModified, target = parent,
+ // prev/new values provided, also attr name
+ MutationEvent event;
+
+ event = (MutationEvent) createEvent ("MutationEvents");
+ event.initMutationEvent ("DOMAttrModified",
+ true /* bubbles */, false /* nocancel */,
+ null, oldValue, newValue, getNodeName (), why);
+ parent.dispatchEvent (event);
+ }
+
+ // DOM Level 3 methods
+
+ public TypeInfo getSchemaTypeInfo()
+ {
+ if (parent != null)
+ {
+ // DTD implementation
+ DomDoctype doctype = (DomDoctype) parent.owner.getDoctype();
+ if (doctype != null)
+ {
+ return doctype.getAttributeTypeInfo(parent.getNodeName(),
+ getNodeName());
+ }
+ // TODO XML Schema implementation
+ }
+ return null;
+ }
+
+ public boolean isId()
+ {
+ if (parent != null)
+ {
+ DomDoctype doctype = (DomDoctype) parent.owner.getDoctype();
+ if (doctype != null)
+ {
+ DTDAttributeTypeInfo info =
+ doctype.getAttributeTypeInfo(parent.getNodeName(),
+ getNodeName());
+ if (info != null && "ID".equals(info.type))
+ {
+ return true;
+ }
+ }
+ DomElement element = (DomElement) parent;
+ if (element.userIdAttrs != null &&
+ element.userIdAttrs.contains(this))
+ {
+ return true;
+ }
+ // TODO XML Schema implementation
+ }
+ return false;
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/dom/DomCDATASection.java b/libjava/classpath/gnu/xml/dom/DomCDATASection.java
new file mode 100644
index 000000000..770f7d5dd
--- /dev/null
+++ b/libjava/classpath/gnu/xml/dom/DomCDATASection.java
@@ -0,0 +1,90 @@
+/* DomCDATASection.java --
+ Copyright (C) 1999,2000,2001,2004 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.dom;
+
+import org.w3c.dom.CDATASection;
+
+/**
+ * "CDATASection" implementation. + * This is a non-core DOM class, supporting the "XML" feature. + * CDATA sections are just ways to represent text using different + * delimeters.
+ * + *You are strongly advised not to use CDATASection nodes. + * The advantage of having slightly prettier ways to print text that may + * have lots of embedded XML delimiters, such as "&" and "<", + * can be dwarfed by the cost of dealing with multiple kinds of text + * nodes in all your algorithms.
+ * + * @author David Brownell + * @author Chris Burdess + */ +public class DomCDATASection + extends DomText + implements CDATASection +{ + + /** + * Constructs a CDATA section node associated with the specified + * document and holding the specified data. + * + *This constructor should only be invoked by a Document as part of + * its createCDATASection functionality, or through a subclass which is + * similarly used in a "Sub-DOM" style layer. + * + */ + protected DomCDATASection(DomDocument owner, String value) + { + super(CDATA_SECTION_NODE, owner, value); + } + + protected DomCDATASection(DomDocument owner, char buf [], int off, int len) + { + super(CDATA_SECTION_NODE, owner, buf, off, len); + } + + /** + * DOM L1 + * Returns the string "#cdata-section". + */ + final public String getNodeName() + { + return "#cdata-section"; + } + +} diff --git a/libjava/classpath/gnu/xml/dom/DomCharacterData.java b/libjava/classpath/gnu/xml/dom/DomCharacterData.java new file mode 100644 index 000000000..396c5f7a7 --- /dev/null +++ b/libjava/classpath/gnu/xml/dom/DomCharacterData.java @@ -0,0 +1,344 @@ +/* DomCharacterData.java -- + Copyright (C) 1999,2000,2001,2004,2006 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.dom; + +import org.w3c.dom.CharacterData; +import org.w3c.dom.DOMException; +import org.w3c.dom.Node; +import org.w3c.dom.NodeList; +import org.w3c.dom.events.MutationEvent; + + +/** + *
Abstract "CharacterData" implementation. This + * facilitates reusing code in classes implementing subtypes of that DOM + * interface (Text, Comment, CDATASection).
+ * + * @author David Brownell + * @author Chris Burdess + */ +public abstract class DomCharacterData + extends DomNode + implements CharacterData +{ + + /** + * Empty node list representing the children of character data nodes. + */ + static class EmptyNodeList + implements NodeList + { + + public int getLength() + { + return 0; + } + + public Node item(int index) + { + return null; + } + + } + + /** + * Singleton empty node list for character data nodes. + */ + static final NodeList CHILD_NODES = new EmptyNodeList(); + + private String text; + + // package private + DomCharacterData(short nodeType, DomDocument doc, String value) + { + super(nodeType, doc); + text = (value == null) ? "" : value; + } + + // package private + DomCharacterData(short nodeType, DomDocument doc, + char[] buf, int offset, int length) + { + super(nodeType, doc); + text = (buf == null) ? "" : new String(buf, offset, length); + } + + /** + * DOM L1 + * Appends the specified data to the value of this node. + * Causes a DOMCharacterDataModified mutation event to be reported. + */ + public void appendData(String arg) + { + if (isReadonly()) + { + throw new DomDOMException(DOMException.NO_MODIFICATION_ALLOWED_ERR); + } + String value = text + arg; + mutating(value); + text = value; + } + + /** + * DOM L1 + * Modifies the value of this node. + * Causes a DOMCharacterDataModified mutation event to be reported. + */ + public void deleteData(int offset, int count) + { + if (isReadonly()) + { + throw new DomDOMException(DOMException.NO_MODIFICATION_ALLOWED_ERR); + } + char[] raw = text.toCharArray(); + if (offset < 0 || count < 0 || offset > raw.length) + { + throw new DomDOMException(DOMException.INDEX_SIZE_ERR); + } + if ((offset + count) > raw.length) + { + count = raw.length - offset; + } + if (count == 0) + { + return; + } + try + { + char[] buf = new char[raw.length - count]; + System.arraycopy(raw, 0, buf, 0, offset); + System.arraycopy(raw, offset + count, buf, offset, + raw.length - (offset + count)); + String value = new String(buf); + mutating(value); + text = value; + } + catch (IndexOutOfBoundsException x) + { + throw new DomDOMException(DOMException.INDEX_SIZE_ERR); + } + } + + /** + * DOM L1 + * Returns the value of this node. + */ + public String getNodeValue() + { + return text; + } + + /** + * DOM L1 + * Returns the value of this node; same as getNodeValue. + */ + public final String getData() + { + return text; + } + + /** + * DOM L1 + * Returns the length of the data. + */ + public int getLength() + { + return text.length(); + } + + /** + * DOM L1 + * Modifies the value of this node. + */ + public void insertData(int offset, String arg) + { + if (isReadonly()) + { + throw new DomDOMException(DOMException.NO_MODIFICATION_ALLOWED_ERR); + } + char[] raw = text.toCharArray(); + char[] tmp = arg.toCharArray (); + char[] buf = new char[raw.length + tmp.length]; + + try + { + System.arraycopy(raw, 0, buf, 0, offset); + System.arraycopy(tmp, 0, buf, offset, tmp.length); + System.arraycopy(raw, offset, buf, offset + tmp.length, + raw.length - offset); + String value = new String(buf); + mutating(value); + text = value; + } + catch (IndexOutOfBoundsException x) + { + throw new DomDOMException(DOMException.INDEX_SIZE_ERR); + } + } + + /** + * DOM L1 + * Modifies the value of this node. Causes DOMCharacterDataModified + * mutation events to be reported (at least one). + */ + public void replaceData(int offset, int count, String arg) + { + if (readonly) + { + throw new DomDOMException(DOMException.NO_MODIFICATION_ALLOWED_ERR); + } + char[] raw = text.toCharArray(); + + // deleteData + if (offset < 0 || count < 0 || offset > raw.length) + { + throw new DomDOMException(DOMException.INDEX_SIZE_ERR); + } + if ((offset + count) > raw.length) + { + count = raw.length - offset; + } + try + { + char[] buf = new char[raw.length - count]; + System.arraycopy(raw, 0, buf, 0, offset); + System.arraycopy(raw, offset + count, buf, offset, + raw.length - (offset + count)); + + // insertData + char[] tmp = arg.toCharArray (); + char[] buf2 = new char[buf.length + tmp.length]; + System.arraycopy(raw, 0, buf, 0, offset); + System.arraycopy(tmp, 0, buf, offset, tmp.length); + System.arraycopy(raw, offset, buf, offset + tmp.length, + raw.length - offset); + String value = new String(buf); + mutating(value); + text = value; + } + catch (IndexOutOfBoundsException x) + { + throw new DomDOMException(DOMException.INDEX_SIZE_ERR); + } + } + + /** + * DOM L1 + * Assigns the value of this node. + * Causes a DOMCharacterDataModified mutation event to be reported. + */ + public void setNodeValue(String value) + { + if (isReadonly()) + { + throw new DomDOMException(DOMException.NO_MODIFICATION_ALLOWED_ERR); + } + if (value == null) + { + value = ""; + } + mutating(value); + text = value; + } + + /** + * DOM L1 + * Assigns the value of this node; same as setNodeValue. + */ + final public void setData(String data) + { + setNodeValue(data); + } + + /** + * DOM L1 + * Returns the specified substring. + */ + public String substringData(int offset, int count) + { + try + { + return text.substring(offset, count); + } + catch (StringIndexOutOfBoundsException e) + { + if (offset >= 0 && count >= 0 && offset < text.length()) + { + return text.substring(offset); + } + throw new DomDOMException(DOMException.INDEX_SIZE_ERR); + } + } + + /** + * Returns an empty node list. + * Character data nodes do not have children. + */ + public NodeList getChildNodes() + { + return CHILD_NODES; + } + + /** + * The base URI for character data isnull
.
+ * @since DOM Level 3 Core
+ */
+ public final String getBaseURI()
+ {
+ return null;
+ }
+
+ private void mutating(String newValue)
+ {
+ if (!reportMutations)
+ {
+ return;
+ }
+
+ // EVENT: DOMCharacterDataModified, target = this,
+ // prev/new values provided
+ MutationEvent event;
+
+ event = (MutationEvent) createEvent("MutationEvents");
+ event.initMutationEvent("DOMCharacterDataModified",
+ true /* bubbles */, false /* nocancel */,
+ null, text, newValue, null, (short) 0);
+ dispatchEvent(event);
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/dom/DomComment.java b/libjava/classpath/gnu/xml/dom/DomComment.java
new file mode 100644
index 000000000..5be6bd518
--- /dev/null
+++ b/libjava/classpath/gnu/xml/dom/DomComment.java
@@ -0,0 +1,80 @@
+/* DomComment.java --
+ Copyright (C) 1999,2000,2001,2004 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.dom;
+
+import org.w3c.dom.Comment;
+
+/**
+ * "Comment" implementation. + * Comments hold data intended for direct consumption by people; + * programs should only use ProcessingInstruction nodes. Note that + * since SAX makes comment reporting optional, XML systems that + * rely on comments (such as by using this class) will often lose + * those comments at some point in the processing pipeline.
+ * + * @author David Brownell + * @author Chris Burdess + */ +public class DomComment + extends DomCharacterData + implements Comment +{ + + /** + * Constructs a comment node associated with the specified + * document and holding the specified data. + * + *This constructor should only be invoked by a Document as part of + * its createComment functionality, or through a subclass which is + * similarly used in a "Sub-DOM" style layer. + */ + protected DomComment(DomDocument owner, String value) + { + super(COMMENT_NODE, owner, value); + } + + /** + * DOM L1 + * Returns the string "#comment". + */ + final public String getNodeName() + { + return "#comment"; + } + +} diff --git a/libjava/classpath/gnu/xml/dom/DomDOMException.java b/libjava/classpath/gnu/xml/dom/DomDOMException.java new file mode 100644 index 000000000..4a3ba7a8c --- /dev/null +++ b/libjava/classpath/gnu/xml/dom/DomDOMException.java @@ -0,0 +1,174 @@ +/* DomDOMException.java -- + Copyright (C) 1999,2000,2001,2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.dom; + +import org.w3c.dom.DOMException; +import org.w3c.dom.Node; + +/** + *
DOMException implementation. The version that + * is provided by the W3C is abstract, so it can't be instantiated. + * + *
This also provides a bit more information about the error + * that is being reported, in terms of the relevant DOM structures + * and data. + * + * @author David Brownell + */ +public class DomDOMException + extends DOMException +{ + + /** @serial Data that caused an error to be reported */ + private String data; + + /** @serial Node associated with the error. */ + private Node node; + + /** @serial Data associated with the error. */ + private int value; + + /** + * Constructs an exception, with the diagnostic message + * corresponding to the specified code. + */ + public DomDOMException(short code) + { + super(code, diagnostic(code)); + } + + /** + * Constructs an exception, with the diagnostic message + * corresponding to the specified code and additional + * information as provided. + */ + public DomDOMException(short code, String data, Node node, int value) + { + super(code, diagnostic(code)); + this.data = data; + this.node = node; + this.value = value; + } + + /** Returns the node to which the diagnotic applies, or null. */ + final public Node getNode() + { + return node; + } + + /** Returns data to which the diagnotic applies, or null. */ + final public String getData() + { + return data; + } + + /** Returns data to which the diagnotic applies, or null. */ + final public int getValue() + { + return value; + } + + /** + * Returns a diagnostic message that may be slightly more useful + * than the generic one, where possible. + */ + public String getMessage() + { + String retval = super.getMessage(); + + if (data != null) + { + retval += "\nMore Information: " + data; + } + if (value != 0) + { + retval += "\nNumber: " + value; + } + if (node != null) + { + retval += "\nNode Name: " + node.getNodeName(); + } + return retval; + } + + // these strings should be localizable. + + private static String diagnostic(short code) + { + switch (code) + { + // DOM L1: + case INDEX_SIZE_ERR: + return "An index or size is out of range."; + case DOMSTRING_SIZE_ERR: + return "A string is too big."; + case HIERARCHY_REQUEST_ERR: + return "The node doesn't belong here."; + case WRONG_DOCUMENT_ERR: + return "The node belongs in another document."; + case INVALID_CHARACTER_ERR: + return "That character is not permitted."; + case NO_DATA_ALLOWED_ERR: + return "This node does not permit data."; + case NO_MODIFICATION_ALLOWED_ERR: + return "No changes are allowed."; + case NOT_FOUND_ERR: + return "The node was not found in that context."; + case NOT_SUPPORTED_ERR: + return "That object is not supported."; + case INUSE_ATTRIBUTE_ERR: + return "The attribute belongs to a different element."; + + // DOM L2: + case INVALID_STATE_ERR: + return "The object is not usable."; + case SYNTAX_ERR: + return "An illegal string was provided."; + case INVALID_MODIFICATION_ERR: + return "An object's type may not be changed."; + case NAMESPACE_ERR: + return "The operation violates XML Namespaces."; + case INVALID_ACCESS_ERR: + return "Parameter or operation isn't supported by this node."; + case TYPE_MISMATCH_ERR: + return "The type of the argument is incompatible with the expected type."; + } + return "Reserved exception number: " + code; + } + +} diff --git a/libjava/classpath/gnu/xml/dom/DomDoctype.java b/libjava/classpath/gnu/xml/dom/DomDoctype.java new file mode 100644 index 000000000..ecf7a9588 --- /dev/null +++ b/libjava/classpath/gnu/xml/dom/DomDoctype.java @@ -0,0 +1,455 @@ +/* DomDoctype.java -- + Copyright (C) 1999,2000,2001,2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.dom; + +import java.util.HashMap; +import org.w3c.dom.DocumentType; +import org.w3c.dom.DOMException; +import org.w3c.dom.DOMImplementation; +import org.w3c.dom.Entity; +import org.w3c.dom.NamedNodeMap; +import org.w3c.dom.Node; +import org.w3c.dom.Notation; + +/** + *
"DocumentType" implementation (with no extensions for supporting + * any document typing information). This is a non-core DOM class, + * supporting the "XML" feature.
+ * + *Few XML applications will actually care about this partial + * DTD support, since it doesn't expose any (!) of the data typing + * facilities which can motivate applications to use DTDs. It does not + * expose element content models, or information about attribute typing + * rules. Plus the information it exposes isn't very useful; as one example, + * DOM exposes information about unparsed ENTITY objects, which is only used + * with certain element attributes, but does not expose the information about + * those attributes which is needed to apply that data!
+ * + *Also, note that there are no nonportable ways to associate even the + * notation and entity information exposed by DOM with a DocumentType. While + * there is a DOM L2 method to construct a DocumentType, it only gives access + * to the textual content of the <!DOCTYPE ...> declaration.
+ * + *In short, you are strongly advised not to rely on this incomplete + * DTD functionality in your application code.
+ * + * @see DomEntity + * @see DomEntityReference + * @see DomNotation + * + * @author David Brownell + * @author Chris Burdess + */ +public class DomDoctype + extends DomExtern + implements DocumentType +{ + + private DomNamedNodeMap notations; + private DomNamedNodeMap entities; + private final DOMImplementation implementation; + private String subset; + + private HashMap elements = new HashMap(); + private boolean ids; + + /** + * Constructs a DocumentType node associated with the specified + * implementation, with the specified name. + * + *This constructor should only be invoked by a DOMImplementation as + * part of its createDocumentType functionality, or through a subclass + * which is similarly used in a "Sub-DOM" style layer. + * + *
Note that at this time there is no standard SAX API granting + * access to the internal subset text, so that relying on that value + * is not currently portable. + * + * @param impl The implementation with which this object is associated + * @param name Name of this root element + * @param publicId If non-null, provides the external subset's + * PUBLIC identifier + * @param systemId If non-null, provides the external subset's + * SYSTEM identifier + * @param internalSubset Provides the literal value (unparsed, no + * entities expanded) of the DTD's internal subset. + */ + protected DomDoctype(DOMImplementation impl, + String name, + String publicId, + String systemId, + String internalSubset) + { + super(DOCUMENT_TYPE_NODE, null, name, publicId, systemId); + implementation = impl; + subset = internalSubset; + } + + /** + * JAXP builder constructor. + * @param doc the document + * @param name the name of the document element + * @param publicId the public ID of the document type declaration + * @param systemId the system ID of the document type declaration + */ + public DomDoctype(DomDocument doc, + String name, + String publicId, + String systemId) + { + super(DOCUMENT_TYPE_NODE, doc, name, publicId, systemId); + implementation = doc.getImplementation(); + } + + /** + * DOM L1 + * Returns the root element's name (just like getNodeName). + */ + final public String getName() + { + return getNodeName(); + } + + /** + * DOM L1 + * Returns information about any general entities declared + * in the DTD. + * + *
Note: DOM L1 doesn't throw a DOMException here, but + * then it doesn't have the strange construction rules of L2. + * + * @exception DOMException HIERARCHY_REQUEST_ERR if the DocumentType + * is not associated with a document. + */ + public NamedNodeMap getEntities() + { + if (entities == null) + { + entities = new DomNamedNodeMap(this, Node.ENTITY_NODE); + } + return entities; + } + + /** + * Records the declaration of a general entity in this DocumentType. + * + * @param name Name of the entity + * @param publicId If non-null, provides the entity's PUBLIC identifier + * @param systemId Provides the entity's SYSTEM identifier + * @param notation If non-null, provides the entity's notation + * (indicating an unparsed entity) + * @return The Entity that was declared, or null if the entity wasn't + * recorded (because it's a parameter entity or because an entity with + * this name was already declared). + * + * @exception DOMException NO_MODIFICATION_ALLOWED_ERR if the + * DocumentType is no longer writable. + * @exception DOMException HIERARCHY_REQUEST_ERR if the DocumentType + * is not associated with a document. + */ + public Entity declareEntity(String name, + String publicId, + String systemId, + String notation) + { + DomEntity entity; + + if (name.charAt(0) == '%' || "[dtd]".equals(name)) + { + return null; + } + if (isReadonly()) + { + throw new DomDOMException(DOMException.NO_MODIFICATION_ALLOWED_ERR); + } + getEntities(); + + DomDocument.checkName(name, (owner != null) ? + "1.1".equals(owner.getXmlVersion()) : false); + if (entities.getNamedItem(name) != null) + { + return null; + } + + entity = new DomEntity(owner, name, publicId, systemId, notation); + entities.setNamedItem(entity); + return entity; + } + + /** + * DOM L1 + * Returns information about any notations declared in the DTD. + * + *
Note: DOM L1 doesn't throw a DOMException here, but + * then it doesn't have the strange construction rules of L2. + * + * @exception DOMException HIERARCHY_REQUEST_ERR if the DocumentType + * is not associated with a document. + */ + public NamedNodeMap getNotations() + { + if (notations == null) + { + notations = new DomNamedNodeMap(this, Node.NOTATION_NODE); + } + return notations; + } + + /** + * Records the declaration of a notation in this DocumentType. + * + * @param name Name of the notation + * @param publicId If non-null, provides the notation's PUBLIC identifier + * @param systemId If non-null, provides the notation's SYSTEM identifier + * @return The notation that was declared. + * + * @exception DOMException NO_MODIFICATION_ALLOWED_ERR if the + * DocumentType is no longer writable. + * @exception DOMException HIERARCHY_REQUEST_ERR if the DocumentType + * is not associated with a document. + */ + public Notation declareNotation(String name, + String publicId, + String systemId) + { + DomNotation notation; + + if (isReadonly()) + { + throw new DomDOMException(DOMException.NO_MODIFICATION_ALLOWED_ERR); + } + getNotations(); + + DomDocument.checkName(name, (owner != null) ? + "1.1".equals(owner.getXmlVersion()) : false); + + notation = new DomNotation(owner, name, publicId, systemId); + notations.setNamedItem(notation); + return notation; + } + + /** + * DOM L2 + * Returns the internal subset of the document, as a string of unparsed + * XML declarations (and comments, PIs, whitespace); or returns null if + * there is no such subset. There is no vendor-independent expectation + * that this attribute be set, or that declarations found in it be + * reflected in the entities or notations attributes + * of this Document "Type" object. + * + *
Some application-specific XML profiles require that documents
+ * only use specific PUBLIC identifiers, without an internal subset
+ * to modify the interperetation of the declarations associated with
+ * that PUBLIC identifier through some standard.
+ */
+ public String getInternalSubset()
+ {
+ return subset;
+ }
+
+ /**
+ * The base URI of a DocumentType is always null
.
+ * See the Infoset Mapping, appendix C.
+ */
+ public final String getBaseURI()
+ {
+ return null;
+ }
+
+ /**
+ * Sets the internal "readonly" flag so the node and its associated
+ * data (only lists of entities and notations, no type information
+ * at the moment) can't be changed.
+ */
+ public void makeReadonly()
+ {
+ super.makeReadonly();
+ if (entities != null)
+ {
+ entities.makeReadonly();
+ }
+ if (notations != null)
+ {
+ notations.makeReadonly();
+ }
+ }
+
+ void setOwner(DomDocument doc)
+ {
+ if (entities != null)
+ {
+ for (DomNode ctx = entities.first; ctx != null; ctx = ctx.next)
+ {
+ ctx.setOwner(doc);
+ }
+ }
+ if (notations != null)
+ {
+ for (DomNode ctx = notations.first; ctx != null; ctx = ctx.next)
+ {
+ ctx.setOwner(doc);
+ }
+ }
+ super.setOwner(doc);
+ }
+
+ /**
+ * DOM L2
+ * Consults the DOM implementation to determine if the requested
+ * feature is supported.
+ */
+ final public boolean supports(String feature, String version)
+ {
+ return implementation.hasFeature(feature, version);
+ }
+
+ /**
+ * Returns the implementation associated with this document type.
+ */
+ final public DOMImplementation getImplementation()
+ {
+ return implementation;
+ }
+
+ public void elementDecl(String name, String model)
+ {
+ DTDElementTypeInfo info = getElementTypeInfo(name);
+ if (info == null)
+ {
+ info = new DTDElementTypeInfo(name, model);
+ elements.put(name, info);
+ }
+ else
+ {
+ info.model = model;
+ }
+ }
+
+ DTDElementTypeInfo getElementTypeInfo(String name)
+ {
+ return (DTDElementTypeInfo) elements.get(name);
+ }
+
+ public void attributeDecl(String eName, String aName, String type,
+ String mode, String value)
+ {
+ DTDAttributeTypeInfo info = new DTDAttributeTypeInfo(eName, aName, type,
+ mode, value);
+ DTDElementTypeInfo elementInfo = getElementTypeInfo(eName);
+ if (elementInfo == null)
+ {
+ elementInfo = new DTDElementTypeInfo(eName, null);
+ elements.put(eName, elementInfo);
+ }
+ elementInfo.setAttributeTypeInfo(aName, info);
+ if ("ID".equals(type))
+ {
+ ids = true;
+ }
+ }
+
+ DTDAttributeTypeInfo getAttributeTypeInfo(String elementName, String name)
+ {
+ DTDElementTypeInfo elementInfo =
+ (DTDElementTypeInfo) elements.get(elementName);
+ return (elementInfo == null) ? null :
+ elementInfo.getAttributeTypeInfo(name);
+ }
+
+ boolean hasIds()
+ {
+ return ids;
+ }
+
+ public boolean isSameNode(Node arg)
+ {
+ if (equals(arg))
+ {
+ return true;
+ }
+ if (!(arg instanceof DocumentType))
+ {
+ return false;
+ }
+ DocumentType doctype = (DocumentType) arg;
+ if (!equal(getPublicId(), doctype.getPublicId()))
+ {
+ return false;
+ }
+ if (!equal(getSystemId(), doctype.getSystemId()))
+ {
+ return false;
+ }
+ if (!equal(getInternalSubset(), doctype.getInternalSubset()))
+ {
+ return false;
+ }
+ // TODO entities
+ // TODO notations
+ return true;
+ }
+
+ /**
+ * Shallow clone of the doctype, except that associated
+ * entities and notations are (deep) cloned.
+ */
+ public Object clone()
+ {
+ DomDoctype node = (DomDoctype) super.clone();
+
+ if (entities != null)
+ {
+ node.entities = new DomNamedNodeMap(node, Node.ENTITY_NODE);
+ for (DomNode ctx = entities.first; ctx != null; ctx = ctx.next)
+ {
+ node.entities.setNamedItem(ctx.cloneNode(true));
+ }
+ }
+ if (notations != null)
+ {
+ node.notations = new DomNamedNodeMap(node, Node.NOTATION_NODE);
+ for (DomNode ctx = notations.first; ctx != null; ctx = ctx.next)
+ {
+ node.notations.setNamedItem(ctx.cloneNode(true));
+ }
+ }
+ return node;
+ }
+
+
+}
diff --git a/libjava/classpath/gnu/xml/dom/DomDocument.java b/libjava/classpath/gnu/xml/dom/DomDocument.java
new file mode 100644
index 000000000..b1e99f46d
--- /dev/null
+++ b/libjava/classpath/gnu/xml/dom/DomDocument.java
@@ -0,0 +1,1555 @@
+/* DomDocument.java --
+ Copyright (C) 1999,2000,2001,2004 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.dom;
+
+import java.util.Iterator;
+import javax.xml.XMLConstants;
+
+import org.w3c.dom.Attr;
+import org.w3c.dom.CDATASection;
+import org.w3c.dom.Comment;
+import org.w3c.dom.Document;
+import org.w3c.dom.DocumentFragment;
+import org.w3c.dom.DocumentType;
+import org.w3c.dom.DOMConfiguration;
+import org.w3c.dom.DOMImplementation;
+import org.w3c.dom.DOMException;
+import org.w3c.dom.Element;
+import org.w3c.dom.Entity;
+import org.w3c.dom.EntityReference;
+import org.w3c.dom.NamedNodeMap;
+import org.w3c.dom.Node;
+import org.w3c.dom.Notation;
+import org.w3c.dom.ProcessingInstruction;
+import org.w3c.dom.Text;
+import org.w3c.dom.UserDataHandler;
+import org.w3c.dom.traversal.DocumentTraversal;
+import org.w3c.dom.traversal.NodeFilter;
+import org.w3c.dom.traversal.NodeIterator;
+import org.w3c.dom.traversal.TreeWalker;
+import org.w3c.dom.xpath.XPathEvaluator;
+import org.w3c.dom.xpath.XPathException;
+import org.w3c.dom.xpath.XPathExpression;
+import org.w3c.dom.xpath.XPathNSResolver;
+
+/**
+ *
"Document" and "DocumentTraversal" implementation. + * + *
Note that when this checks names for legality, it uses an + * approximation of the XML rules, not the real ones. Specifically, + * it uses Unicode rules, with sufficient tweaks to pass a majority + * of basic XML conformance tests. (The huge XML character tables are + * hairy to implement.) + * + * @author David Brownell + * @author Chris Burdess + */ +public class DomDocument + extends DomNode + implements Document, DocumentTraversal, XPathEvaluator +{ + + private final DOMImplementation implementation; + private boolean checkingCharacters = true; + boolean checkingWellformedness = true; + private boolean defaultAttributes = true; + + boolean building; // if true, skip mutation events in the tree + + DomDocumentConfiguration config; + + String inputEncoding; + String encoding; + String version = "1.0"; + boolean standalone; + String systemId; + + /** + * Constructs a Document node, associating it with an instance + * of the DomImpl class. + * + *
Note that this constructor disables character checking.
+ * It is normally used when connecting a DOM to an XML parser,
+ * and duplicating such checks is undesirable. When used for
+ * purposes other than connecting to a parser, you should
+ * re-enable that checking.
+ *
+ * @see #setCheckingCharacters
+ */
+ public DomDocument()
+ {
+ this(new DomImpl());
+ }
+
+ /**
+ * Constructs a Document node, associating it with the specified
+ * implementation. This should only be used in conjunction with
+ * a specialized implementation; it will normally be called by
+ * that implementation.
+ *
+ * @see DomImpl
+ * @see #setCheckingCharacters
+ */
+ protected DomDocument(DOMImplementation impl)
+ {
+ super(DOCUMENT_NODE, null);
+ implementation = impl;
+ }
+
+ /**
+ * Sets the building
flag.
+ * Mutation events in the document are not reported.
+ */
+ public void setBuilding(boolean flag)
+ {
+ building = flag;
+ }
+
+ /**
+ * Sets whether to check for document well-formedness.
+ * If true, an exception will be raised if a second doctype or root
+ * element node is added to the document.
+ */
+ public void setCheckWellformedness(boolean flag)
+ {
+ checkingWellformedness = flag;
+ }
+
+ /**
+ * Sets whether to check for document characters.
+ */
+ public void setCheckingCharacters(boolean flag)
+ {
+ checkingCharacters = flag;
+ }
+
+ /**
+ * Sets whether to default attributes for new elements.
+ */
+ public void setDefaultAttributes(boolean flag)
+ {
+ defaultAttributes = flag;
+ }
+
+ /**
+ * DOM L1
+ * Returns the constant "#document".
+ */
+ final public String getNodeName()
+ {
+ return "#document";
+ }
+
+ /**
+ * DOM L1
+ * Returns the document's root element, or null.
+ */
+ final public Element getDocumentElement()
+ {
+ for (DomNode ctx = first; ctx != null; ctx = ctx.next)
+ {
+ if (ctx.nodeType == ELEMENT_NODE)
+ {
+ return (Element) ctx;
+ }
+ }
+ return null;
+ }
+
+ /**
+ * DOM L1
+ * Returns the document's DocumentType, or null.
+ */
+ final public DocumentType getDoctype()
+ {
+ for (DomNode ctx = first; ctx != null; ctx = ctx.next)
+ {
+ if (ctx.nodeType == DOCUMENT_TYPE_NODE)
+ {
+ return (DocumentType) ctx;
+ }
+ }
+ return null;
+ }
+
+ /**
+ * DOM L1
+ * Returns the document's DOMImplementation.
+ */
+ final public DOMImplementation getImplementation()
+ {
+ return implementation;
+ }
+
+ /**
+ * DOM L1 (relocated in DOM L2)
+ * Returns the element with the specified "ID" attribute, or null.
+ *
+ *
Returns null unless {@link Consumer} was used to populate internal + * DTD declaration information, using package-private APIs. If that + * internal DTD information is available, the document may be searched for + * the element with that ID. + */ + public Element getElementById(String id) + { + if (id == null || id.length() == 0) + { + return null; + } + DomDoctype doctype = (DomDoctype) getDoctype(); + if (doctype != null && !doctype.hasIds()) + { + doctype = null; + } + + // yes, this is linear in size of document. + // it'd be easy enough to maintain a hashtable. + Node current = getDocumentElement(); + Node temp; + + if (current == null) + { + return null; + } + while (current != this) + { + // done? + if (current.getNodeType() == ELEMENT_NODE) + { + DomElement element = (DomElement) current; + if (element.userIdAttrs != null) + { + for (Iterator i = element.userIdAttrs.iterator(); + i.hasNext(); ) + { + Node idAttr = (Node) i.next(); + if (id.equals(idAttr.getNodeValue())) + { + return element; + } + } + } + if (doctype != null) + { + DTDElementTypeInfo info = + doctype.getElementTypeInfo(current.getNodeName()); + if (info != null && + id.equals(element.getAttribute(info.idAttrName))) + { + return element; + } + } + // xml:id + String xmlId = element.getAttribute("xml:id"); + if (xmlId == null) + { + xmlId = element.getAttributeNS(XMLConstants.XML_NS_URI, + "id"); + } + if (id.equals(xmlId)) + { + return element; + } + } + + // descend? + if (current.hasChildNodes()) + { + current = current.getFirstChild(); + continue; + } + + // lateral? + temp = current.getNextSibling(); + if (temp != null) + { + current = temp; + continue; + } + + // back up ... + do + { + temp = current.getParentNode(); + if (temp == null) + { + return null; + } + current = temp; + temp = current.getNextSibling(); + } + while (temp == null); + current = temp; + } + return null; + } + + private void checkNewChild(Node newChild) + { + if (newChild.getNodeType() == ELEMENT_NODE + && getDocumentElement() != null) + { + throw new DomDOMException(DOMException.HIERARCHY_REQUEST_ERR, + "document element already present: " + + getDocumentElement(), newChild, 0); + } + if (newChild.getNodeType() == DOCUMENT_TYPE_NODE + && getDoctype() != null) + { + throw new DomDOMException(DOMException.HIERARCHY_REQUEST_ERR, + "document type already present: " + + getDoctype(), newChild, 0); + } + } + + /** + * DOM L1 + * Appends the specified node to this node's list of children, + * enforcing the constraints that there be only one root element + * and one document type child. + */ + public Node appendChild(Node newChild) + { + if (checkingWellformedness) + { + checkNewChild(newChild); + } + return super.appendChild(newChild); + } + + /** + * DOM L1 + * Inserts the specified node in this node's list of children, + * enforcing the constraints that there be only one root element + * and one document type child. + */ + public Node insertBefore(Node newChild, Node refChild) + { + if (checkingWellformedness) + { + checkNewChild(newChild); + } + return super.insertBefore(newChild, refChild); + } + + /** + * DOM L1 + * Replaces the specified node in this node's list of children, + * enforcing the constraints that there be only one root element + * and one document type child. + */ + public Node replaceChild(Node newChild, Node refChild) + { + if (checkingWellformedness && + ((newChild.getNodeType() == ELEMENT_NODE && + refChild.getNodeType() != ELEMENT_NODE) || + (newChild.getNodeType() == DOCUMENT_TYPE_NODE && + refChild.getNodeType() != DOCUMENT_TYPE_NODE))) + { + checkNewChild(newChild); + } + return super.replaceChild(newChild, refChild); + } + + // NOTE: DOM can't really tell when the name of an entity, + // notation, or PI must follow the namespace rules (excluding + // colons) instead of the XML rules (which allow them without + // much restriction). That's an API issue. verifyXmlName + // aims to enforce the XML rules, not the namespace rules. + + /** + * Throws a DOM exception if the specified name is not a legal XML 1.0 + * Name. + * @deprecated This method is deprecated and may be removed in future + * versions of GNU JAXP + */ + public static void verifyXmlName(String name) + { + // XXX why is this public? + checkName(name, false); + } + + static void checkName(String name, boolean xml11) + { + if (name == null) + { + throw new DomDOMException(DOMException.NAMESPACE_ERR, name, null, 0); + } + int len = name.length(); + if (len == 0) + { + throw new DomDOMException(DOMException.NAMESPACE_ERR, name, null, 0); + } + + // dog: rewritten to use the rules for XML 1.0 and 1.1 + + // Name start character + char c = name.charAt(0); + if (xml11) + { + // XML 1.1 + if ((c < 0x0041 || c > 0x005a) && + (c < 0x0061 || c > 0x007a) && + c != ':' && c != '_' && + (c < 0x00c0 || c > 0x00d6) && + (c < 0x00d8 || c > 0x00f6) && + (c < 0x00f8 || c > 0x02ff) && + (c < 0x0370 || c > 0x037d) && + (c < 0x037f || c > 0x1fff) && + (c < 0x200c || c > 0x200d) && + (c < 0x2070 || c > 0x218f) && + (c < 0x2c00 || c > 0x2fef) && + (c < 0x3001 || c > 0xd7ff) && + (c < 0xf900 || c > 0xfdcf) && + (c < 0xfdf0 || c > 0xfffd) && + (c < 0x10000 || c > 0xeffff)) + { + throw new DomDOMException(DOMException.INVALID_CHARACTER_ERR, + name, null, c); + } + } + else + { + // XML 1.0 + int type = Character.getType(c); + switch (type) + { + case Character.LOWERCASE_LETTER: // Ll + case Character.UPPERCASE_LETTER: // Lu + case Character.OTHER_LETTER: // Lo + case Character.TITLECASE_LETTER: // Lt + case Character.LETTER_NUMBER: // Nl + if ((c > 0xf900 && c < 0xfffe) || + (c >= 0x20dd && c <= 0x20e0)) + { + // Compatibility area and Unicode 2.0 exclusions + throw new DomDOMException(DOMException.INVALID_CHARACTER_ERR, + name, null, c); + } + break; + default: + if (c != ':' && c != '_' && (c < 0x02bb || c > 0x02c1) && + c != 0x0559 && c != 0x06e5 && c != 0x06e6) + { + throw new DomDOMException(DOMException.INVALID_CHARACTER_ERR, + name, null, c); + } + } + } + + // Subsequent characters + for (int i = 1; i < len; i++) + { + c = name.charAt(i); + if (xml11) + { + // XML 1.1 + if ((c < 0x0041 || c > 0x005a) && + (c < 0x0061 || c > 0x007a) && + (c < 0x0030 || c > 0x0039) && + c != ':' && c != '_' && c != '-' && c != '.' && + (c < 0x00c0 || c > 0x00d6) && + (c < 0x00d8 || c > 0x00f6) && + (c < 0x00f8 || c > 0x02ff) && + (c < 0x0370 || c > 0x037d) && + (c < 0x037f || c > 0x1fff) && + (c < 0x200c || c > 0x200d) && + (c < 0x2070 || c > 0x218f) && + (c < 0x2c00 || c > 0x2fef) && + (c < 0x3001 || c > 0xd7ff) && + (c < 0xf900 || c > 0xfdcf) && + (c < 0xfdf0 || c > 0xfffd) && + (c < 0x10000 || c > 0xeffff) && + c != 0x00b7 && + (c < 0x0300 || c > 0x036f) && + (c < 0x203f || c > 0x2040)) + { + throw new DomDOMException(DOMException.INVALID_CHARACTER_ERR, name, + null, c); + } + } + else + { + // XML 1.0 + int type = Character.getType(c); + switch (type) + { + case Character.LOWERCASE_LETTER: // Ll + case Character.UPPERCASE_LETTER: // Lu + case Character.DECIMAL_DIGIT_NUMBER: // Nd + case Character.OTHER_LETTER: // Lo + case Character.TITLECASE_LETTER: // Lt + case Character.LETTER_NUMBER: // Nl + case Character.COMBINING_SPACING_MARK: // Mc + case Character.ENCLOSING_MARK: // Me + case Character.NON_SPACING_MARK: // Mn + case Character.MODIFIER_LETTER: // Lm + if ((c > 0xf900 && c < 0xfffe) || + (c >= 0x20dd && c <= 0x20e0)) + { + // Compatibility area and Unicode 2.0 exclusions + throw new DomDOMException(DOMException.INVALID_CHARACTER_ERR, + name, null, c); + } + break; + default: + if (c != '-' && c != '.' && c != ':' && c != '_' && + c != 0x0387 && (c < 0x02bb || c > 0x02c1) && + c != 0x0559 && c != 0x06e5 && c != 0x06e6 && c != 0x00b7) + { + throw new DomDOMException(DOMException.INVALID_CHARACTER_ERR, + name, null, c); + } + } + } + } + + // FIXME characters with a font or compatibility decomposition (i.e. + // those with a "compatibility formatting tag" in field 5 of the + // database -- marked by field 5 beginning with a "<") are not allowed. + } + + // package private + static void checkNCName(String name, boolean xml11) + { + checkName(name, xml11); + int len = name.length(); + int index = name.indexOf(':'); + if (index != -1) + { + if (index == 0 || index == (len - 1) || name.lastIndexOf(':') != index) + { + throw new DomDOMException(DOMException.NAMESPACE_ERR, name, null, 0); + } + } + } + + // package private + static void checkChar(String value, boolean xml11) + { + char[] chars = value.toCharArray(); + checkChar(chars, 0, chars.length, xml11); + } + + static void checkChar(char[] buf, int off, int len, boolean xml11) + { + for (int i = 0; i < len; i++) + { + char c = buf[i]; + + // assume surrogate pairing checks out OK, for simplicity + if ((c >= 0x0020 && c <= 0xd7ff) || + (c == 0x000a || c == 0x000d || c == 0x0009) || + (c >= 0xe000 && c <= 0xfffd) || + (c >= 0x10000 && c <= 0x10ffff)) + { + continue; + } + if (xml11) + { + if ((c >= 0x0001 && c <= 0x001f) || + (c >= 0x007f && c <= 0x0084) || + (c >= 0x0086 && c <= 0x009f)) + { + continue; + } + } + throw new DomDOMException(DOMException.INVALID_CHARACTER_ERR, + new String(buf, off, len), null, c); + } + } + + /** + * DOM L1 + * Returns a newly created element with the specified name. + * The node name of the created element will be equal to {@code name}. + * The namespace, prefix and local name will all be {@code null}. + */ + public Element createElement(String name) + { + Element element; + + if (checkingCharacters) + { + checkName(name, "1.1".equals(version)); + } + if (name.startsWith("xml:")) + { + element = createElementNS(null, name); + } + else + { + DomElement domElement = new DomElement(this, null, name, null, null); + element = domElement; + } + if (defaultAttributes) + setDefaultAttributes(element, name); + return element; + } + + /** + * DOM L2 + * Returns a newly created element with the specified name + * and namespace information. + */ + public Element createElementNS(String namespaceURI, String name) + { + if (checkingCharacters) + { + checkNCName(name, "1.1".equals(version)); + } + + if ("".equals(namespaceURI)) + { + namespaceURI = null; + } + if (name.startsWith("xml:")) + { + if (namespaceURI != null + && !XMLConstants.XML_NS_URI.equals(namespaceURI)) + { + throw new DomDOMException(DOMException.NAMESPACE_ERR, + "xml namespace is always " + + XMLConstants.XML_NS_URI, this, 0); + } + namespaceURI = XMLConstants.XML_NS_URI; + } + else if (XMLConstants.XMLNS_ATTRIBUTE.equals(name) || + name.startsWith("xmlns:")) + { + throw new DomDOMException(DOMException.NAMESPACE_ERR, + "xmlns is reserved", this, 0); + } + else if (namespaceURI == null && name.indexOf(':') != -1) + { + throw new DomDOMException(DOMException.NAMESPACE_ERR, + "prefixed name '" + name + + "' needs a URI", this, 0); + } + + Element element = new DomElement(this, namespaceURI, name); + if (defaultAttributes) + setDefaultAttributes(element, name); + return element; + } + + private void setDefaultAttributes(Element element, String name) + { + DomDoctype doctype = (DomDoctype) getDoctype(); + if (doctype == null) + { + return; + } + + // default any attributes that need it + DTDElementTypeInfo info = doctype.getElementTypeInfo(name); + if (info != null) + { + for (Iterator i = info.attributes(); i != null && i.hasNext(); ) + { + DTDAttributeTypeInfo attr = (DTDAttributeTypeInfo) i.next(); + String value = attr.value; + if ("#IMPLIED".equals(attr.mode) && value == null) + continue; + DomAttr node = (DomAttr) createAttribute(attr.name); + + if (value == null) + { + value = ""; + } + node.setValue(value); + node.setSpecified(false); + element.setAttributeNode(node); + } + } + } + + /** + * DOM L1 + * Returns a newly created document fragment. + */ + public DocumentFragment createDocumentFragment() + { + return new DomDocumentFragment(this); + } + + /** + * DOM L1 + * Returns a newly created text node with the specified value. + */ + public Text createTextNode(String value) + { + if (checkingCharacters) + { + checkChar(value, "1.1".equals(version)); + } + return new DomText(this, value); + } + + /** + * Returns a newly created text node with the specified value. + */ + public Text createTextNode(char[] buf, int off, int len) + { + if (checkingCharacters) + { + checkChar(buf, off, len, "1.1".equals(version)); + } + return new DomText(this, buf, off, len); + } + + /** + * DOM L1 + * Returns a newly created comment node with the specified value. + */ + public Comment createComment(String value) + { + if (checkingCharacters) + { + checkChar(value, "1.1".equals(version)); + } + return new DomComment(this, value); + } + + /** + * DOM L1 + * Returns a newly created CDATA section node with the specified value. + */ + public CDATASection createCDATASection(String value) + { + if (checkingCharacters) + { + checkChar(value, "1.1".equals(version)); + } + return new DomCDATASection(this, value); + } + + /** + * Returns a newly created CDATA section node with the specified value. + */ + public CDATASection createCDATASection(char[] buf, int off, int len) + { + if (checkingCharacters) + { + checkChar(buf, off, len, "1.1".equals(version)); + } + return new DomCDATASection(this, buf, off, len); + } + + /** + * DOM L1 + * Returns a newly created processing instruction. + */ + public ProcessingInstruction createProcessingInstruction(String target, + String data) + { + if (checkingCharacters) + { + boolean xml11 = "1.1".equals(version); + checkName(target, xml11); + if ("xml".equalsIgnoreCase(target)) + { + throw new DomDOMException(DOMException.SYNTAX_ERR, + "illegal PI target name", + this, 0); + } + checkChar(data, xml11); + } + return new DomProcessingInstruction(this, target, data); + } + + /** + * DOM L1 + * Returns a newly created attribute with the specified name. + */ + public Attr createAttribute(String name) + { + if (checkingCharacters) + { + checkName(name, "1.1".equals(version)); + } + if (name.startsWith("xml:")) + { + return createAttributeNS(XMLConstants.XML_NS_URI, name); + } + else if (XMLConstants.XMLNS_ATTRIBUTE.equals(name) || + name.startsWith("xmlns:")) + { + return createAttributeNS(XMLConstants.XMLNS_ATTRIBUTE_NS_URI, name); + } + else + { + DomAttr ret = new DomAttr(this, null, name, null, null); + return ret; + } + } + + /** + * DOM L2 + * Returns a newly created attribute with the specified name + * and namespace information. + */ + public Attr createAttributeNS(String namespaceURI, String name) + { + if (checkingCharacters) + { + checkNCName(name, "1.1".equals(version)); + } + + if ("".equals(namespaceURI)) + { + namespaceURI = null; + } + if (name.startsWith ("xml:")) + { + if (namespaceURI == null) + { + namespaceURI = XMLConstants.XML_NS_URI; + } + else if (!XMLConstants.XML_NS_URI.equals(namespaceURI)) + { + throw new DomDOMException(DOMException.NAMESPACE_ERR, + "xml namespace is always " + + XMLConstants.XML_NS_URI, + this, 0); + } + } + else if (XMLConstants.XMLNS_ATTRIBUTE.equals(name) || + name.startsWith("xmlns:")) + { + if (namespaceURI == null) + { + namespaceURI = XMLConstants.XMLNS_ATTRIBUTE_NS_URI; + } + else if (!XMLConstants.XMLNS_ATTRIBUTE_NS_URI.equals(namespaceURI)) + { + throw new DomDOMException(DOMException.NAMESPACE_ERR, + "xmlns namespace must be " + + XMLConstants.XMLNS_ATTRIBUTE_NS_URI, + this, 0); + } + } + else if (namespaceURI == null && name.indexOf(':') != -1) + { + throw new DomDOMException(DOMException.NAMESPACE_ERR, + "prefixed name needs a URI: " + name, this, 0); + } + return new DomAttr(this, namespaceURI, name); + } + + /** + * DOM L1 + * Returns a newly created reference to the specified entity. + * The caller should populate this with the appropriate children + * and then mark it as readonly. + * + * @see DomNode#makeReadonly + */ + public EntityReference createEntityReference(String name) + { + DomEntityReference ret = new DomEntityReference(this, name); + DocumentType doctype = getDoctype(); + if (doctype != null) + { + DomEntity ent = (DomEntity) doctype.getEntities().getNamedItem(name); + if (ent != null) + { + for (DomNode ctx = ent.first; ctx != null; ctx = ctx.next) + { + ret.appendChild(ctx.cloneNode(true)); + } + } + } + ret.makeReadonly(); + return ret; + } + + /** + * DOM L2 + * Makes a copy of the specified node, with all nodes "owned" by + * this document and with children optionally copied. This type + * of standard utility has become, well, a standard utility. + * + *
Note that EntityReference nodes created through this method (either + * directly, or recursively) never have children, and that there is no + * portable way to associate them with such children. + * + *
Note also that there is no requirement that the specified node + * be associated with a different document. This differs from the + * cloneNode operation in that the node itself is not given + * an opportunity to participate, so that any information managed + * by node subclasses will be lost. + */ + public Node importNode(Node src, boolean deep) + { + Node dst = null; + switch (src.getNodeType()) + { + case TEXT_NODE: + dst = createTextNode(src.getNodeValue()); + break; + case CDATA_SECTION_NODE: + dst = createCDATASection(src.getNodeValue()); + break; + case COMMENT_NODE: + dst = createComment(src.getNodeValue()); + break; + case PROCESSING_INSTRUCTION_NODE: + dst = createProcessingInstruction(src.getNodeName(), + src.getNodeValue()); + break; + case NOTATION_NODE: + // NOTE: There's no standard way to create + // these, or add them to a doctype. Useless. + Notation notation = (Notation) src; + dst = new DomNotation(this, notation.getNodeName(), + notation.getPublicId(), + notation.getSystemId()); + break; + case ENTITY_NODE: + // NOTE: There's no standard way to create + // these, or add them to a doctype. Useless. + Entity entity = (Entity) src; + dst = new DomEntity(this, entity.getNodeName(), + entity.getPublicId(), + entity.getSystemId(), + entity.getNotationName()); + if (deep) + { + for (Node ctx = src.getFirstChild(); ctx != null; + ctx = ctx.getNextSibling()) + { + dst.appendChild(importNode(ctx, deep)); + } + } + break; + case ENTITY_REFERENCE_NODE: + dst = createEntityReference(src.getNodeName()); + break; + case DOCUMENT_FRAGMENT_NODE: + dst = new DomDocumentFragment(this); + if (deep) + { + for (Node ctx = src.getFirstChild(); ctx != null; + ctx = ctx.getNextSibling()) + { + dst.appendChild(importNode(ctx, deep)); + } + } + break; + case ATTRIBUTE_NODE: + String attr_nsuri = src.getNamespaceURI(); + if (attr_nsuri != null) + { + dst = createAttributeNS(attr_nsuri, src.getNodeName()); + } + else + { + dst = createAttribute(src.getNodeName()); + } + // this is _always_ done regardless of "deep" setting + for (Node ctx = src.getFirstChild(); ctx != null; + ctx = ctx.getNextSibling()) + { + dst.appendChild(importNode(ctx, false)); + } + break; + case ELEMENT_NODE: + String elem_nsuri = src.getNamespaceURI(); + if (elem_nsuri != null) + { + dst = createElementNS(elem_nsuri, src.getNodeName()); + } + else + { + dst = createElement(src.getNodeName()); + } + NamedNodeMap srcAttrs = src.getAttributes(); + NamedNodeMap dstAttrs = dst.getAttributes(); + int len = srcAttrs.getLength(); + for (int i = 0; i < len; i++) + { + Attr a = (Attr) srcAttrs.item(i); + Attr dflt; + + // maybe update defaulted attributes + dflt = (Attr) dstAttrs.getNamedItem(a.getNodeName()); + if (dflt != null) + { + String newval = a.getNodeValue(); + if (!dflt.getNodeValue().equals(newval) + || a.getSpecified () == true) + { + dflt.setNodeValue (newval); + } + continue; + } + + dstAttrs.setNamedItem((Attr) importNode(a, false)); + } + if (deep) + { + for (Node ctx = src.getFirstChild(); ctx != null; + ctx = ctx.getNextSibling()) + { + dst.appendChild(importNode(ctx, true)); + } + } + break; + // can't import document or doctype nodes + case DOCUMENT_NODE: + case DOCUMENT_TYPE_NODE: + // FALLTHROUGH + // can't import unrecognized or nonstandard nodes + default: + throw new DomDOMException(DOMException.NOT_SUPPORTED_ERR, null, src, 0); + } + + // FIXME cleanup a bit -- for deep copies, copy those + // children in one place, here (code sharing is healthy) + + if (src instanceof DomNode) + { + ((DomNode) src).notifyUserDataHandlers(UserDataHandler.NODE_IMPORTED, + src, dst); + } + return dst; + } + + /** + * DOM L2 (Traversal) + * Returns a newly created node iterator. Don't forget to detach + * this iterator when you're done using it! + * + * @see DomIterator + */ + public NodeIterator createNodeIterator(Node root, + int whatToShow, + NodeFilter filter, + boolean expandEntities) + { + return new DomNodeIterator(root, whatToShow, filter, expandEntities, + false); + } + + public TreeWalker createTreeWalker(Node root, + int whatToShow, + NodeFilter filter, + boolean expandEntities) + { + return new DomNodeIterator(root, whatToShow, filter, expandEntities, + true); + } + + // DOM Level 3 methods + + /** + * DOM L3 + */ + public String getInputEncoding() + { + return inputEncoding; + } + + public void setInputEncoding(String inputEncoding) + { + this.inputEncoding = inputEncoding; + } + + /** + * DOM L3 + */ + public String getXmlEncoding() + { + return encoding; + } + + public void setXmlEncoding(String encoding) + { + this.encoding = encoding; + } + + public boolean getXmlStandalone() + { + return standalone; + } + + public void setXmlStandalone(boolean xmlStandalone) + { + standalone = xmlStandalone; + } + + public String getXmlVersion() + { + return version; + } + + public void setXmlVersion(String xmlVersion) + { + if (xmlVersion == null) + { + xmlVersion = "1.0"; + } + if ("1.0".equals(xmlVersion) || + "1.1".equals(xmlVersion)) + { + version = xmlVersion; + } + else + { + throw new DomDOMException(DOMException.NOT_SUPPORTED_ERR); + } + } + + public boolean getStrictErrorChecking() + { + return checkingCharacters; + } + + public void setStrictErrorChecking(boolean strictErrorChecking) + { + checkingCharacters = strictErrorChecking; + } + + public String lookupPrefix(String namespaceURI) + { + Node root = getDocumentElement(); + return (root == null) ? null : root.lookupPrefix(namespaceURI); + } + + public boolean isDefaultNamespace(String namespaceURI) + { + Node root = getDocumentElement(); + return (root == null) ? false : root.isDefaultNamespace(namespaceURI); + } + + public String lookupNamespaceURI(String prefix) + { + Node root = getDocumentElement(); + return (root == null) ? null : root.lookupNamespaceURI(prefix); + } + + public String getBaseURI() + { + return getDocumentURI(); + /* + Node root = getDocumentElement(); + if (root != null) + { + NamedNodeMap attrs = root.getAttributes(); + Node xmlBase = attrs.getNamedItemNS(XMLConstants.XML_NS_URI, "base"); + if (xmlBase != null) + { + return xmlBase.getNodeValue(); + } + } + return systemId; + */ + } + + public String getDocumentURI() + { + return systemId; + } + + public void setDocumentURI(String documentURI) + { + systemId = documentURI; + } + + public Node adoptNode(Node source) + { + int sourceNodeType = source.getNodeType(); + switch (sourceNodeType) + { + case DOCUMENT_NODE: + case DOCUMENT_TYPE_NODE: + throw new DomDOMException(DOMException.NOT_SUPPORTED_ERR); + case ENTITY_NODE: + case NOTATION_NODE: + throw new DomDOMException(DOMException.NO_MODIFICATION_ALLOWED_ERR); + } + if (source instanceof DomNode) + { + // GNU native + DomNode src = (DomNode) source; + DomNode dst = src; + if (dst.parent != null) + { + dst = (DomNode) dst.cloneNode(true); + } + dst.setOwner(this); + src.notifyUserDataHandlers(UserDataHandler.NODE_ADOPTED, src, dst); + return dst; + } + else + { + // Some other implementation + Node dst = null; + switch (sourceNodeType) + { + case Node.ATTRIBUTE_NODE: + { + Attr src = (Attr) source; + String nodeName = src.getNodeName(); + String localName = src.getLocalName(); + String namespaceUri = src.getNamespaceURI(); + dst = (localName == null) ? + createAttribute(nodeName) : + createAttributeNS(namespaceUri, nodeName); + adoptChildren(src, dst); + break; + } + case Node.CDATA_SECTION_NODE: + { + CDATASection src = (CDATASection) source; + dst = createCDATASection(src.getData()); + break; + } + case Node.COMMENT_NODE: + { + Comment src = (Comment) source; + dst = createComment(src.getData()); + break; + } + case Node.DOCUMENT_FRAGMENT_NODE: + { + DocumentFragment src = (DocumentFragment) source; + dst = createDocumentFragment(); + adoptChildren(src, dst); + break; + } + case Node.ELEMENT_NODE: + { + Element src = (Element) source; + String nodeName = src.getNodeName(); + String localName = src.getLocalName(); + String namespaceUri = src.getNamespaceURI(); + dst = (localName == null) ? + createElement(nodeName) : + createElementNS(namespaceUri, nodeName); + adoptAttributes(src, dst); + adoptChildren(src, dst); + break; + } + case Node.ENTITY_REFERENCE_NODE: + { + EntityReference src = (EntityReference) source; + dst = createEntityReference(src.getNodeName()); + adoptChildren(src, dst); + break; + } + case Node.PROCESSING_INSTRUCTION_NODE: + { + ProcessingInstruction src = (ProcessingInstruction) source; + dst = createProcessingInstruction(src.getTarget(), + src.getData()); + break; + } + case Node.TEXT_NODE: + { + Text src = (Text) source; + dst = createTextNode(src.getData()); + break; + } + } + return dst; + } + } + + void adoptChildren(Node src, Node dst) + { + Node node = src.getFirstChild(); + while (node != null) + { + Node next = node.getNextSibling(); + dst.appendChild(adoptNode(node)); + node = next; + } + } + + void adoptAttributes(Node src, Node dst) + { + NamedNodeMap srcAttrs = src.getAttributes(); + NamedNodeMap dstAttrs = dst.getAttributes(); + int len = srcAttrs.getLength(); + for (int i = 0; i < len; i++) + { + Node node = srcAttrs.item(i); + String localName = node.getLocalName(); + if (localName == null) + { + dstAttrs.setNamedItem(adoptNode(node)); + } + else + { + dstAttrs.setNamedItemNS(adoptNode(node)); + } + } + } + + public DOMConfiguration getDomConfig() + { + if (config == null) + { + config = new DomDocumentConfiguration(); + } + return config; + } + + public boolean isEqualNode(Node arg) + { + if (!super.isEqualNode(arg)) + return false; + Document d = (Document) arg; + String dversion = d.getXmlVersion(); + if (dversion == null || !dversion.equals(version)) + return false; + boolean dstandalone = d.getXmlStandalone(); + if (dstandalone != standalone) + return false; + String dencoding = d.getXmlEncoding(); + if (dencoding == null || dencoding.equalsIgnoreCase("UTF-8")) + { + if (encoding != null && !encoding.equalsIgnoreCase("UTF-8")) + return false; + } + else + { + if (!dencoding.equals(encoding)) + return false; + } + return true; + } + + public void normalizeDocument() + { + boolean save = building; + building = true; + normalizeNode(this); + building = save; + } + + void normalizeNode(DomNode node) + { + node.normalize(); + if (config != null) + { + switch (node.nodeType) + { + case CDATA_SECTION_NODE: + if (!config.cdataSections) + { + // replace CDATA section with text node + Text text = createTextNode(node.getNodeValue()); + node.parent.insertBefore(text, node); + node.parent.removeChild(node); + // merge adjacent text nodes + String data = text.getWholeText(); + node = (DomNode) text.replaceWholeText(data); + } + else if (config.splitCdataSections) + { + String value = node.getNodeValue(); + int i = value.indexOf("]]>"); + while (i != -1) + { + Node node2 = createCDATASection(value.substring(0, i)); + node.parent.insertBefore(node2, node); + value = value.substring(i + 3); + node.setNodeValue(value); + i = value.indexOf("]]>"); + } + } + break; + case COMMENT_NODE: + if (!config.comments) + { + node.parent.removeChild(node); + } + break; + case TEXT_NODE: + if (!config.elementContentWhitespace && + ((Text) node).isElementContentWhitespace()) + { + node.parent.removeChild(node); + } + break; + case ENTITY_REFERENCE_NODE: + if (!config.entities) + { + for (DomNode ctx = node.first; ctx != null; ) + { + DomNode ctxNext = ctx.next; + node.parent.insertBefore(ctx, node); + ctx = ctxNext; + } + node.parent.removeChild(node); + } + break; + case ELEMENT_NODE: + if (!config.namespaceDeclarations) + { + DomNamedNodeMap attrs = + (DomNamedNodeMap) node.getAttributes(); + boolean aro = attrs.readonly; + attrs.readonly = false; // Ensure we can delete if necessary + int len = attrs.getLength(); + for (int i = 0; i < len; i++) + { + Node attr = attrs.item(i); + String namespace = attr.getNamespaceURI(); + if (XMLConstants.XMLNS_ATTRIBUTE_NS_URI.equals(namespace)) + { + attrs.removeNamedItemNS(namespace, + attr.getLocalName()); + i--; + len--; + } + } + attrs.readonly = aro; + } + break; + } + } + for (DomNode ctx = node.first; ctx != null; ) + { + DomNode ctxNext = ctx.next; + normalizeNode(ctx); + ctx = ctxNext; + } + } + + public Node renameNode(Node n, String namespaceURI, String qualifiedName) + throws DOMException + { + if (n instanceof DomNsNode) + { + DomNsNode src = (DomNsNode) n; + if (src == null) + { + throw new DomDOMException(DOMException.NOT_FOUND_ERR); + } + if (src.owner != this) + { + throw new DomDOMException(DOMException.WRONG_DOCUMENT_ERR, + null, src, 0); + } + boolean xml11 = "1.1".equals(version); + checkName(qualifiedName, xml11); + int ci = qualifiedName.indexOf(':'); + if ("".equals(namespaceURI)) + { + namespaceURI = null; + } + if (namespaceURI != null) + { + checkNCName(qualifiedName, xml11); + String prefix = (ci == -1) ? "" : + qualifiedName.substring(0, ci); + if (XMLConstants.XML_NS_PREFIX.equals(prefix) && + !XMLConstants.XML_NS_URI.equals(namespaceURI)) + { + throw new DomDOMException(DOMException.NAMESPACE_ERR, + "xml namespace must be " + + XMLConstants.XML_NS_URI, src, 0); + } + else if (src.nodeType == ATTRIBUTE_NODE && + (XMLConstants.XMLNS_ATTRIBUTE.equals(prefix) || + XMLConstants.XMLNS_ATTRIBUTE.equals(qualifiedName)) && + !XMLConstants.XMLNS_ATTRIBUTE_NS_URI.equals(namespaceURI)) + { + throw new DomDOMException(DOMException.NAMESPACE_ERR, + "xmlns namespace must be " + + XMLConstants.XMLNS_ATTRIBUTE_NS_URI, src, 0); + } + if (XMLConstants.XML_NS_URI.equals(namespaceURI) && + !XMLConstants.XML_NS_PREFIX.equals(prefix)) + { + throw new DomDOMException(DOMException.NAMESPACE_ERR, + "xml namespace must be " + + XMLConstants.XML_NS_URI, src, 0); + } + else if (src.nodeType == ATTRIBUTE_NODE && + XMLConstants.XMLNS_ATTRIBUTE_NS_URI.equals(namespaceURI) && + !(XMLConstants.XMLNS_ATTRIBUTE.equals(prefix) || + XMLConstants.XMLNS_ATTRIBUTE.equals(qualifiedName))) + { + throw new DomDOMException(DOMException.NAMESPACE_ERR, + "xmlns namespace must be " + + XMLConstants.XMLNS_ATTRIBUTE_NS_URI, src, 0); + } + + } + src.setNodeName(qualifiedName); + src.setNamespaceURI(namespaceURI); + src.notifyUserDataHandlers(UserDataHandler.NODE_RENAMED, src, src); + // TODO MutationNameEvents + // DOMElementNameChanged or DOMAttributeNameChanged + return src; + } + throw new DomDOMException(DOMException.NOT_SUPPORTED_ERR, null, n, 0); + } + + // -- XPathEvaluator -- + + public XPathExpression createExpression(String expression, + XPathNSResolver resolver) + throws XPathException, DOMException + { + return new DomXPathExpression(this, expression, resolver); + } + + public XPathNSResolver createNSResolver(Node nodeResolver) + { + return new DomXPathNSResolver(nodeResolver); + } + + public Object evaluate(String expression, + Node contextNode, + XPathNSResolver resolver, + short type, + Object result) + throws XPathException, DOMException + { + XPathExpression xpe = + new DomXPathExpression(this, expression, resolver); + return xpe.evaluate(contextNode, type, result); + } + +} diff --git a/libjava/classpath/gnu/xml/dom/DomDocumentBuilder.java b/libjava/classpath/gnu/xml/dom/DomDocumentBuilder.java new file mode 100644 index 000000000..99c254481 --- /dev/null +++ b/libjava/classpath/gnu/xml/dom/DomDocumentBuilder.java @@ -0,0 +1,227 @@ +/* DomDocumentBuilder.java -- + Copyright (C) 2004,2006,2007 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.dom; + +import java.io.File; +import java.io.InputStream; +import java.io.IOException; +import java.io.Reader; +import java.net.MalformedURLException; +import java.net.URL; +import javax.xml.parsers.DocumentBuilder; +import org.w3c.dom.Document; +import org.w3c.dom.DOMConfiguration; +import org.w3c.dom.DOMImplementation; +import org.w3c.dom.ls.DOMImplementationLS; +import org.w3c.dom.ls.LSException; +import org.w3c.dom.ls.LSInput; +import org.w3c.dom.ls.LSParser; +import org.xml.sax.EntityResolver; +import org.xml.sax.ErrorHandler; +import org.xml.sax.InputSource; +import org.xml.sax.SAXException; + +/** + * Document builder using the GNU DOM Load & Save implementation. + * + * @author Chris Burdess + */ +class DomDocumentBuilder + extends DocumentBuilder +{ + + final DOMImplementation impl; + final DOMImplementationLS ls; + final LSParser parser; + + DomDocumentBuilder(DOMImplementation impl, + DOMImplementationLS ls, + LSParser parser) + { + this.impl = impl; + this.ls = ls; + this.parser = parser; + } + + public boolean isNamespaceAware() + { + DOMConfiguration config = parser.getDomConfig(); + return ((Boolean) config.getParameter("namespaces")).booleanValue(); + } + + public boolean isValidating() + { + DOMConfiguration config = parser.getDomConfig(); + return ((Boolean) config.getParameter("validating")).booleanValue(); + } + + public boolean isXIncludeAware() + { + DOMConfiguration config = parser.getDomConfig(); + return ((Boolean) config.getParameter("xinclude-aware")).booleanValue(); + } + + public void setEntityResolver(EntityResolver resolver) + { + DOMConfiguration config = parser.getDomConfig(); + config.setParameter("entity-resolver", resolver); + } + + public void setErrorHandler(ErrorHandler handler) + { + DOMConfiguration config = parser.getDomConfig(); + config.setParameter("error-handler", handler); + } + + public DOMImplementation getDOMImplementation() + { + return impl; + } + + public Document newDocument() + { + return impl.createDocument(null, null, null); + } + + public Document parse(InputStream in) + throws SAXException, IOException + { + LSInput input = ls.createLSInput(); + input.setByteStream(in); + try + { + return parser.parse(input); + } + catch (LSException e) + { + Throwable e2 = e.getCause(); + if (e2 instanceof IOException) + throw (IOException) e2; + else + throw e; + } + } + + public Document parse(InputStream in, String systemId) + throws SAXException, IOException + { + LSInput input = ls.createLSInput(); + input.setByteStream(in); + input.setSystemId(systemId); + try + { + return parser.parse(input); + } + catch (LSException e) + { + Throwable e2 = e.getCause(); + if (e2 instanceof IOException) + throw (IOException) e2; + else + throw e; + } + } + + public Document parse(String systemId) + throws SAXException, IOException + { + try + { + return parser.parseURI(systemId); + } + catch (LSException e) + { + Throwable e2 = e.getCause(); + if (e2 instanceof IOException) + throw (IOException) e2; + else + throw e; + } + } + + public Document parse(InputSource is) + throws SAXException, IOException + { + LSInput input = ls.createLSInput(); + String systemId = is.getSystemId(); + InputStream in = is.getByteStream(); + if (in != null) + { + input.setByteStream(in); + } + else + { + Reader reader = is.getCharacterStream(); + if (reader != null) + { + input.setCharacterStream(reader); + } + else + { + try + { + URL url = new URL(systemId); + input.setByteStream(url.openStream()); + } + catch (MalformedURLException e) + { + // Maybe this is a relative file URL + File cwd = new File(System.getProperty("user.dir")); + URL url = new URL(cwd.toURL(), systemId); + input.setByteStream(url.openStream()); + } + } + } + input.setPublicId(is.getPublicId()); + input.setSystemId(systemId); + input.setEncoding(is.getEncoding()); + try + { + return parser.parse(input); + } + catch (LSException e) + { + Throwable e2 = e.getCause(); + if (e2 instanceof IOException) + throw (IOException) e2; + else + throw e; + } + } + +} diff --git a/libjava/classpath/gnu/xml/dom/DomDocumentBuilderFactory.java b/libjava/classpath/gnu/xml/dom/DomDocumentBuilderFactory.java new file mode 100644 index 000000000..1e3eaa582 --- /dev/null +++ b/libjava/classpath/gnu/xml/dom/DomDocumentBuilderFactory.java @@ -0,0 +1,181 @@ +/* DomDocumentBuilderFactory.java -- + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.dom; + +import javax.xml.XMLConstants; +import javax.xml.parsers.DocumentBuilder; +import javax.xml.parsers.DocumentBuilderFactory; +import javax.xml.parsers.FactoryConfigurationError; +import javax.xml.parsers.ParserConfigurationException; +import org.w3c.dom.DOMConfiguration; +import org.w3c.dom.DOMException; +import org.w3c.dom.DOMImplementation; +import org.w3c.dom.bootstrap.DOMImplementationRegistry; +import org.w3c.dom.ls.DOMImplementationLS; +import org.w3c.dom.ls.LSParser; + +/** + * Document builder factory that uses a DOM Level 3 Load & Save + * implementation. + * + * @author Chris Burdess + */ +public class DomDocumentBuilderFactory + extends DocumentBuilderFactory +{ + + final DOMImplementation impl; + final DOMImplementationLS ls; + private boolean secureProcessing; + + public DomDocumentBuilderFactory() + { + try + { + DOMImplementationRegistry reg = + DOMImplementationRegistry.newInstance(); + impl = reg.getDOMImplementation("LS 3.0"); + if (impl == null) + { + throw new FactoryConfigurationError("no LS implementations found"); + } + ls = (DOMImplementationLS) impl; + } + catch (Exception e) + { + throw new FactoryConfigurationError(e); + } + } + + public DocumentBuilder newDocumentBuilder() + throws ParserConfigurationException + { + LSParser parser = null; + try + { + parser = ls.createLSParser(DOMImplementationLS.MODE_ASYNCHRONOUS, + "http://www.w3.org/TR/REC-xml"); + } + catch (DOMException e) + { + if (e.code == DOMException.NOT_SUPPORTED_ERR) + { + // Fall back to synchronous parser + try + { + parser = ls.createLSParser(DOMImplementationLS.MODE_SYNCHRONOUS, + "http://www.w3.org/TR/REC-xml"); + } + catch (DOMException e2) + { + ParserConfigurationException pce = + new ParserConfigurationException(); + pce.initCause(e2); + throw pce; + } + } + else + { + ParserConfigurationException pce = + new ParserConfigurationException(); + pce.initCause(e); + throw pce; + } + } + DOMConfiguration config = parser.getDomConfig(); + setParameter(config, "namespaces", + isNamespaceAware() ? Boolean.TRUE : Boolean.FALSE); + setParameter(config, "element-content-whitespace", + isIgnoringElementContentWhitespace() ? Boolean.FALSE : + Boolean.TRUE); + setParameter(config, "comments", + isIgnoringComments() ? Boolean.FALSE : Boolean.TRUE); + setParameter(config, "expand-entity-references", + isExpandEntityReferences() ? Boolean.TRUE : Boolean.FALSE); + setParameter(config, "coalescing", + isCoalescing() ? Boolean.TRUE : Boolean.FALSE); + setParameter(config, "validating", + isValidating() ? Boolean.TRUE : Boolean.FALSE); + setParameter(config, "xinclude-aware", + isXIncludeAware() ? Boolean.TRUE : Boolean.FALSE); + return new DomDocumentBuilder(impl, ls, parser); + } + + void setParameter(DOMConfiguration config, String name, Object value) + throws ParserConfigurationException + { + if (!config.canSetParameter(name, value)) + { + throw new ParserConfigurationException(name); + } + config.setParameter(name, value); + } + + public Object getAttribute(String name) + { + // TODO + return null; + } + + public void setAttribute(String name, Object value) + { + // TODO + } + + public void setFeature(String name, boolean value) + throws ParserConfigurationException + { + if (name == null) + throw new NullPointerException(); + if (XMLConstants.FEATURE_SECURE_PROCESSING.equals(name)) + { + secureProcessing = true; + return; + } + throw new ParserConfigurationException(name); + } + + public boolean getFeature(String name) + throws ParserConfigurationException + { + if (XMLConstants.FEATURE_SECURE_PROCESSING.equals(name)) + return secureProcessing; + throw new ParserConfigurationException(name); + } + +} diff --git a/libjava/classpath/gnu/xml/dom/DomDocumentConfiguration.java b/libjava/classpath/gnu/xml/dom/DomDocumentConfiguration.java new file mode 100644 index 000000000..9aab44532 --- /dev/null +++ b/libjava/classpath/gnu/xml/dom/DomDocumentConfiguration.java @@ -0,0 +1,260 @@ +/* DomDocumentConfiguration.java -- + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.dom; + +import java.util.Arrays; +import java.util.List; +import org.w3c.dom.DOMConfiguration; +import org.w3c.dom.DOMErrorHandler; +import org.w3c.dom.DOMException; +import org.w3c.dom.DOMStringList; + +/** + * Document configuration, used to store normalization and other parameters. + * + * @author Chris Burdess + */ +class DomDocumentConfiguration + implements DOMConfiguration, DOMStringList +{ + + private static final List SUPPORTED_PARAMETERS = + Arrays.asList(new String[] { "cdata-sections", + "comments", + "element-content-whitespace", + "entities", + "error-handler", + "namespace-declarations", + "split-cdata-sections", + "infoset"}); + + boolean cdataSections = true; + boolean comments = true; + boolean elementContentWhitespace = true; + boolean entities = true; + DOMErrorHandler errorHandler; + boolean namespaceDeclarations = true; + boolean splitCdataSections = true; + + public void setParameter(String name, Object value) + throws DOMException + { + name = name.toLowerCase(); + if ("cdata-sections".equals(name)) + { + cdataSections = "true".equals(value.toString()); + } + else if ("comments".equals(name)) + { + comments = "true".equals(value.toString()); + } + else if ("element-content-whitespace".equals(name)) + { + elementContentWhitespace = "true".equals(value.toString()); + } + else if ("entities".equals(name)) + { + entities = "true".equals(value.toString()); + } + else if ("error-handler".equals(name)) + { + try + { + errorHandler = (DOMErrorHandler) value; + } + catch (ClassCastException e) + { + throw new DomDOMException(DOMException.TYPE_MISMATCH_ERR, + value.getClass().getName(), null, 0); + } + } + else if ("namespace-declarations".equals(name)) + { + namespaceDeclarations = "true".equals(value.toString()); + } + else if ("split-cdata-sections".equals(name)) + { + comments = "true".equals(value.toString()); + } + else if ("infoset".equals(name)) + { + if ("true".equals(value.toString())) + { + entities = false; + cdataSections = false; + namespaceDeclarations = true; + elementContentWhitespace = true; + comments = true; + } + } + else if (("canonical-form".equals(name) || + "check-character-normalization".equals(name) || + "datatype-normalization".equals(name) || + "normalize-characters".equals(name) || + "validate".equals(name) || + "validate-if-schema".equals(name)) && + "false".equals(value.toString())) + { + // NOOP + } + else if (("namespaces".equals(name) || + "well-formed".equals(name)) && + "true".equals(value.toString())) + { + // NOOP + } + else + { + throw new DomDOMException(DOMException.NOT_SUPPORTED_ERR, + name, null, 0); + } + } + + public Object getParameter(String name) + throws DOMException + { + name = name.toLowerCase(); + if ("cdata-sections".equals(name)) + { + return cdataSections ? Boolean.TRUE : Boolean.FALSE; + } + else if ("comments".equals(name)) + { + return comments ? Boolean.TRUE : Boolean.FALSE; + } + else if ("element-content-whitespace".equals(name)) + { + return elementContentWhitespace ? Boolean.TRUE : Boolean.FALSE; + } + else if ("entities".equals(name)) + { + return entities ? Boolean.TRUE : Boolean.FALSE; + } + else if ("error-handler".equals(name)) + { + return errorHandler; + } + else if ("namespace-declarations".equals(name)) + { + return namespaceDeclarations ? Boolean.TRUE : Boolean.FALSE; + } + else if ("split-cdata-sections".equals(name)) + { + return comments ? Boolean.TRUE : Boolean.FALSE; + } + else if ("canonical-form".equals(name) || + "check-character-normalization".equals(name) || + "datatype-normalization".equals(name) || + "normalize-characters".equals(name) || + "validate".equals(name) || + "validate-if-schema".equals(name)) + { + return Boolean.FALSE; + } + else if ("namespaces".equals(name) || + "well-formed".equals(name)) + { + return Boolean.TRUE; + } + else if ("infoset".equals(name)) + { + return (entities == false && + cdataSections == false && + namespaceDeclarations == true && + comments == true) ? Boolean.TRUE : Boolean.FALSE; + } + throw new DomDOMException(DOMException.NOT_SUPPORTED_ERR, name, null, 0); + } + + public boolean canSetParameter(String name, Object value) + { + name = name.toLowerCase(); + if ("error-handler".equals(name)) + { + return (value == null || value instanceof DOMErrorHandler); + } + else if (contains(name)) + { + return true; + } + else if ("canonical-form".equals(name) || + "check-character-normalization".equals(name) || + "datatype-normalization".equals(name) || + "normalize-characters".equals(name) || + "validate".equals(name) || + "validate-if-schema".equals(name)) + { + return "false".equals(value.toString()); + } + else if ("namespaces".equals(name) || + "well-formed".equals(name)) + { + return "true".equals(value.toString()); + } + return false; + } + + public DOMStringList getParameterNames() + { + return this; + } + + public String item(int i) + { + try + { + return (String) SUPPORTED_PARAMETERS.get(i); + } + catch (IndexOutOfBoundsException e) + { + return null; + } + } + + public int getLength() + { + return SUPPORTED_PARAMETERS.size(); + } + + public boolean contains(String str) + { + str = str.toLowerCase(); + return SUPPORTED_PARAMETERS.contains(str); + } + +} diff --git a/libjava/classpath/gnu/xml/dom/DomDocumentFragment.java b/libjava/classpath/gnu/xml/dom/DomDocumentFragment.java new file mode 100644 index 000000000..8c4e0db3b --- /dev/null +++ b/libjava/classpath/gnu/xml/dom/DomDocumentFragment.java @@ -0,0 +1,75 @@ +/* DomDocumentFragment.java -- + Copyright (C) 1999,2000,2001,2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.dom; + +import org.w3c.dom.DocumentFragment; + +/** + *
"DocumentFragment" implementation.
+ * + * @author David Brownell + * @author Chris Burdess + */ +public class DomDocumentFragment + extends DomNode + implements DocumentFragment +{ + + /** + * Constructs a DocumentFragment node associated with the + * specified document. + * + *This constructor should only be invoked by a Document as part of + * its createDocumentFragment functionality, or through a subclass which + * is similarly used in a "Sub-DOM" style layer. + */ + protected DomDocumentFragment(DomDocument owner) + { + super(DOCUMENT_FRAGMENT_NODE, owner); + } + + /** + * DOM L1 + * Returns the string "#document-fragment". + */ + final public String getNodeName() + { + return "#document-fragment"; + } + +} diff --git a/libjava/classpath/gnu/xml/dom/DomElement.java b/libjava/classpath/gnu/xml/dom/DomElement.java new file mode 100644 index 000000000..429749978 --- /dev/null +++ b/libjava/classpath/gnu/xml/dom/DomElement.java @@ -0,0 +1,582 @@ +/* DomElement.java -- + Copyright (C) 1999,2000,2001,2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.dom; + +import java.util.HashSet; +import java.util.Set; +import javax.xml.XMLConstants; + +import org.w3c.dom.Attr; +import org.w3c.dom.DOMException; +import org.w3c.dom.Element; +import org.w3c.dom.NamedNodeMap; +import org.w3c.dom.Node; +import org.w3c.dom.TypeInfo; + +/** + *
"Element" implementation. + * + * @author David Brownell + * @author Chris Burdess + */ +public class DomElement + extends DomNsNode + implements Element +{ + + /** + * User-defined ID attributes. + * Used by DomAttr.isId and DomDocument.getElementById + */ + Set userIdAttrs; + + // Attributes are VERY expensive in DOM, and not just for + // this implementation. Avoid creating them. + private DomNamedNodeMap attributes; + + // xml:space cache + String xmlSpace = ""; + + /** + * Constructs an Element node associated with the specified document. + * + *
This constructor should only be invoked by a Document as part + * of its createElement functionality, or through a subclass which is + * similarly used in a "Sub-DOM" style layer. + * + * @param owner The document with which this node is associated + * @param namespaceURI Combined with the local part of the name, + * this is used to uniquely identify a type of element + * @param name Name of this element, which may include a prefix + */ + protected DomElement(DomDocument owner, String namespaceURI, String name) + { + super(ELEMENT_NODE, owner, namespaceURI, name); + } + + /** + *
+ * Constructs an Element node associated with the specified document. + * This constructor should only be invoked by a Document as part + * of its createElement functionality, or through a subclass which is + * similarly used in a "Sub-DOM" style layer. + *
+ *+ * With this constructor, the prefix and local part are given explicitly + * rather than being computed. This allows them to be explicitly set to + * {@code null} as required by {@link Document#createElement(String)}. + *
+ * + * @param owner The document with which this node is associated + * @param namespaceURI Combined with the local part of the name, + * this is used to uniquely identify a type of element + * @param name Name of this element, which may include a prefix + * @param prefix the namespace prefix of the name. May be {@code null}. + * @param localName the local part of the name. May be {@code null}. + */ + protected DomElement(DomDocument owner, String namespaceURI, String name, + String prefix, String localName) + { + super(ELEMENT_NODE, owner, namespaceURI, name, prefix, localName); + } + + /** + * DOM L1 + * Returns the element's attributes + */ + public NamedNodeMap getAttributes() + { + if (attributes == null) + { + attributes = new DomNamedNodeMap(this, Node.ATTRIBUTE_NODE); + } + return attributes; + } + + /** + * DOM L2> + * Returns true iff this is an element node with attributes. + */ + public boolean hasAttributes() + { + return attributes != null && attributes.length != 0; + } + + /** + * Shallow clone of the element, except that associated + * attributes are (deep) cloned. + */ + public Object clone() + { + DomElement node = (DomElement) super.clone(); + + if (attributes != null) + { + node.attributes = new DomNamedNodeMap(node, Node.ATTRIBUTE_NODE); + for (DomNode ctx = attributes.first; ctx != null; ctx = ctx.next) + { + node.attributes.setNamedItem(ctx.cloneNode(true), true, true); + } + } + return node; + } + + void setOwner(DomDocument doc) + { + if (attributes != null) + { + for (DomNode ctx = attributes.first; ctx != null; ctx = ctx.next) + { + ctx.setOwner(doc); + } + } + super.setOwner(doc); + } + + /** + * Marks this element, its children, and its associated attributes as + * readonly. + */ + public void makeReadonly() + { + super.makeReadonly(); + if (attributes != null) + { + attributes.makeReadonly(); + } + } + + /** + * DOM L1 + * Returns the element name (same as getNodeName). + */ + final public String getTagName() + { + return getNodeName(); + } + + /** + * DOM L1 + * Returns the value of the specified attribute, or an + * empty string. + */ + public String getAttribute(String name) + { + if ("xml:space" == name) // NB only works on interned string + { + // Use cached value + return xmlSpace; + } + Attr attr = getAttributeNode(name); + return (attr == null) ? "" : attr.getValue(); + } + + /** + * DOM L2 + * Returns true if the element has an attribute with the + * specified name (specified or DTD defaulted). + */ + public boolean hasAttribute(String name) + { + return getAttributeNode(name) != null; + } + + /** + * DOM L2 + * Returns true if the element has an attribute with the + * specified name (specified or DTD defaulted). + */ + public boolean hasAttributeNS(String namespaceURI, String local) + { + return getAttributeNodeNS(namespaceURI, local) != null; + } + + /** + * DOM L2 + * Returns the value of the specified attribute, or an + * empty string. + */ + public String getAttributeNS(String namespaceURI, String local) + { + Attr attr = getAttributeNodeNS(namespaceURI, local); + return (attr == null) ? "" : attr.getValue(); + } + + /** + * DOM L1 + * Returns the appropriate attribute node; the name is the + * nodeName property of the attribute. + */ + public Attr getAttributeNode(String name) + { + return (attributes == null) ? null : + (Attr) attributes.getNamedItem(name); + } + + /** + * DOM L2 + * Returns the appropriate attribute node; the name combines + * the namespace name and the local part. + */ + public Attr getAttributeNodeNS(String namespace, String localPart) + { + return (attributes == null) ? null : + (Attr) attributes.getNamedItemNS(namespace, localPart); + } + + /** + * DOM L1 + * Modifies an existing attribute to have the specified value, + * or creates a new one with that value. The name used is the + * nodeName value. + */ + public void setAttribute(String name, String value) + { + Attr attr = getAttributeNode(name); + if (attr != null) + { + attr.setNodeValue(value); + ((DomAttr) attr).setSpecified(true); + return; + } + attr = owner.createAttribute(name); + attr.setNodeValue(value); + setAttributeNode(attr); + } + + /** + * DOM L2 + * Modifies an existing attribute to have the specified value, + * or creates a new one with that value. + */ + public void setAttributeNS(String uri, String aname, String value) + { + if (("xmlns".equals (aname) || aname.startsWith ("xmlns:")) + && !XMLConstants.XMLNS_ATTRIBUTE_NS_URI.equals (uri)) + { + throw new DomDOMException(DOMException.NAMESPACE_ERR, + "setting xmlns attribute to illegal value", this, 0); + } + + Attr attr = getAttributeNodeNS(uri, aname); + if (attr != null) + { + attr.setNodeValue(value); + return; + } + attr = owner.createAttributeNS(uri, aname); + attr.setNodeValue(value); + setAttributeNodeNS(attr); + } + + /** + * DOM L1 + * Stores the specified attribute, optionally overwriting any + * existing one with that name. + */ + public Attr setAttributeNode(Attr attr) + { + return (Attr) getAttributes().setNamedItem(attr); + } + + /** + * DOM L2 + * Stores the specified attribute, optionally overwriting any + * existing one with that name. + */ + public Attr setAttributeNodeNS(Attr attr) + { + return (Attr) getAttributes().setNamedItemNS(attr); + } + + /** + * DOM L1 + * Removes the appropriate attribute node. + * If there is no such node, this is (bizarrely enough) a NOP so you + * won't see exceptions if your code deletes non-existent attributes. + * + *Note that since there is no portable way for DOM to record + * DTD information, default values for attributes will never be + * provided automatically. + */ + public void removeAttribute(String name) + { + if (attributes == null) + { + return; + } + + try + { + attributes.removeNamedItem(name); + } + catch (DomDOMException e) + { + if (e.code != DOMException.NOT_FOUND_ERR) + { + throw e; + } + } + } + + /** + * DOM L1 + * Removes the appropriate attribute node; the name is the + * nodeName property of the attribute. + * + *
Note that since there is no portable way for DOM to record + * DTD information, default values for attributes will never be + * provided automatically. + */ + public Attr removeAttributeNode(Attr node) + { + if (attributes == null) + { + throw new DomDOMException(DOMException.NOT_FOUND_ERR, null, node, 0); + } + return (Attr) attributes.removeNamedItem(node.getNodeName()); + } + + /** + * DOM L2 + * Removes the appropriate attribute node; the name combines + * the namespace name and the local part. + * + *
Note that since there is no portable way for DOM to record + * DTD information, default values for attributes will never be + * provided automatically. + */ + public void removeAttributeNS(String namespace, String localPart) + { + if (attributes == null) + { + throw new DomDOMException(DOMException.NOT_FOUND_ERR, localPart, null, 0); + } + attributes.removeNamedItemNS (namespace, localPart); + } + + // DOM Level 3 methods + + public String lookupPrefix(String namespaceURI) + { + if (namespaceURI == null) + { + return null; + } + String namespace = getNamespaceURI(); + if (namespace != null && namespace.equals(namespaceURI)) + { + return getPrefix(); + } + if (attributes != null) + { + for (DomNode ctx = attributes.first; ctx != null; ctx = ctx.next) + { + if (XMLConstants.XMLNS_ATTRIBUTE_NS_URI + .equals(ctx.getNamespaceURI())) + { + String value = ctx.getNodeValue(); + if (value.equals(namespaceURI)) + { + return ctx.getLocalName(); + } + } + } + } + return super.lookupPrefix(namespaceURI); + } + + public boolean isDefaultNamespace(String namespaceURI) + { + String namespace = getNamespaceURI(); + if (namespace != null && namespace.equals(namespaceURI)) + { + return getPrefix() == null; + } + if (attributes != null) + { + for (DomNode ctx = attributes.first; ctx != null; ctx = ctx.next) + { + if (XMLConstants.XMLNS_ATTRIBUTE_NS_URI + .equals(ctx.getNamespaceURI())) + { + String qName = ctx.getNodeName(); + return (XMLConstants.XMLNS_ATTRIBUTE.equals(qName)); + } + } + } + return super.isDefaultNamespace(namespaceURI); + } + + public String lookupNamespaceURI(String prefix) + { + String namespace = getNamespaceURI(); + if (namespace != null && equal(prefix, getPrefix())) + { + return namespace; + } + if (attributes != null) + { + for (DomNode ctx = attributes.first; ctx != null; ctx = ctx.next) + { + if (XMLConstants.XMLNS_ATTRIBUTE_NS_URI + .equals(ctx.getNamespaceURI())) + { + if (prefix == null) + { + if (XMLConstants.XMLNS_ATTRIBUTE.equals(ctx.getNodeName())) + { + return ctx.getNodeValue(); + } + } + else + { + if (prefix.equals(ctx.getLocalName())) + { + return ctx.getNodeValue(); + } + } + } + } + } + return super.lookupNamespaceURI(prefix); + } + + public String getBaseURI() + { + if (attributes != null) + { + Node xmlBase = + attributes.getNamedItemNS(XMLConstants.XML_NS_URI, "base"); + if (xmlBase != null) + { + return xmlBase.getNodeValue(); + } + } + return super.getBaseURI(); + } + + public TypeInfo getSchemaTypeInfo() + { + // DTD implementation + DomDoctype doctype = (DomDoctype) owner.getDoctype(); + if (doctype != null) + { + return doctype.getElementTypeInfo(getNodeName()); + } + // TODO XML Schema implementation + return null; + } + + public void setIdAttribute(String name, boolean isId) + { + NamedNodeMap attrs = getAttributes(); + Attr attr = (Attr) attrs.getNamedItem(name); + setIdAttributeNode(attr, isId); + } + + public void setIdAttributeNode(Attr attr, boolean isId) + { + if (readonly) + { + throw new DomDOMException(DOMException.NO_MODIFICATION_ALLOWED_ERR); + } + if (attr == null || attr.getOwnerElement() != this) + { + throw new DomDOMException(DOMException.NOT_FOUND_ERR); + } + if (isId) + { + if (userIdAttrs == null) + { + userIdAttrs = new HashSet(); + } + userIdAttrs.add(attr); + } + else if (userIdAttrs != null) + { + userIdAttrs.remove(attr); + if (userIdAttrs.isEmpty()) + { + userIdAttrs = null; + } + } + } + + public void setIdAttributeNS(String namespaceURI, String localName, + boolean isId) + { + NamedNodeMap attrs = getAttributes(); + Attr attr = (Attr) attrs.getNamedItemNS(namespaceURI, localName); + setIdAttributeNode(attr, isId); + } + + public boolean isEqualNode(Node arg) + { + if (!super.isEqualNode(arg)) + return false; + getAttributes(); + NamedNodeMap argAttrs = arg.getAttributes(); + int len = argAttrs.getLength(); + if (argAttrs == null || (len != attributes.length)) + return false; + for (int i = 0; i < len; i++) + { + Node argCtx = argAttrs.item(i); + // Don't compare namespace nodes + if (XMLConstants.XMLNS_ATTRIBUTE_NS_URI + .equals(argCtx.getNamespaceURI())) + continue; + // Find corresponding attribute node + DomNode ctx = attributes.first; + for (; ctx != null; ctx = ctx.next) + { + if (XMLConstants.XMLNS_ATTRIBUTE_NS_URI + .equals(ctx.getNamespaceURI())) + continue; + if (!ctx.isEqualNode(argCtx)) + continue; + break; + } + if (ctx == null) + return false; // not found + } + return true; + } + +} diff --git a/libjava/classpath/gnu/xml/dom/DomEntity.java b/libjava/classpath/gnu/xml/dom/DomEntity.java new file mode 100644 index 000000000..be44d7359 --- /dev/null +++ b/libjava/classpath/gnu/xml/dom/DomEntity.java @@ -0,0 +1,146 @@ +/* DomEntity.java -- + Copyright (C) 1999,2000,2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.dom; + +import org.w3c.dom.Entity; + +/** + *
"Entity" implementation. This is a non-core DOM class, supporting the + * "XML" feature. There are two types of entities, neither of which works + * particularly well in this API:
In short, avoid using this DOM functionality. + * + * @see DomDoctype + * @see DomEntityReference + * @see DomNotation + * + * @author David Brownell + * @author Chris Burdess + */ +public class DomEntity + extends DomExtern + implements Entity +{ + + private String notation; + + /** + * Constructs an Entity node associated with the specified document, + * with the specified descriptive data. + * + *
This constructor should only be invoked by a DomDoctype as part + * of its declareEntity functionality, or through a subclass which is + * similarly used in a "Sub-DOM" style layer. + * + * @param owner The document with which this entity is associated + * @param name Name of this entity + * @param publicId If non-null, provides the entity's PUBLIC identifier + * @param systemId Provides the entity's SYSTEM identifier (URI) + * @param notation If non-null, provides the unparsed entity's notation. + */ + protected DomEntity(DomDocument owner, + String name, + String publicId, + String systemId, + String notation) + { + super(ENTITY_NODE, owner, name, publicId, systemId); + this.notation = notation; + + // NOTE: if notation == null, this is a parsed entity + // which could reasonably be given child nodes ... + makeReadonly(); + } + + /** + * DOM L1 + * Returns the NOTATION identifier associated with this entity, if any. + */ + final public String getNotationName() + { + return notation; + } + + // DOM Level 3 methods + + public String getInputEncoding() + { + // TODO + return null; + } + + public String getXmlEncoding() + { + // TODO + return null; + } + + public String getXmlVersion() + { + // TODO + return null; + } + + /** + * The base URI of an external entity is its system ID. + * The base URI of an internal entity is the parent document's base URI. + * @since DOM Level 3 Core + */ + public String getBaseURI() + { + String systemId = getSystemId(); + return (systemId == null) ? owner.getBaseURI() : systemId; + } + +} diff --git a/libjava/classpath/gnu/xml/dom/DomEntityReference.java b/libjava/classpath/gnu/xml/dom/DomEntityReference.java new file mode 100644 index 000000000..1fa2ea628 --- /dev/null +++ b/libjava/classpath/gnu/xml/dom/DomEntityReference.java @@ -0,0 +1,130 @@ +/* DomEntityReference.java -- + Copyright (C) 1999,2000,2001,2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.dom; + +import org.w3c.dom.DocumentType; +import org.w3c.dom.Entity; +import org.w3c.dom.EntityReference; + +/** + *
"EntityReference" implementation (reference to parsed entity). + * This is a non-core DOM class, supporting the "XML" feature. + * It does not represent builtin entities (such as "&") + * or character references, which are always directly expanded in + * DOM trees.
+ * + *Note that while the DOM specification permits these nodes to have + * a readonly set of children, this is not required. Similarly, it does + * not require a DOM to couple EntityReference nodes with any Entity nodes + * that have the same entity name (and equivalent children). It also + * effectively guarantees that references created directly or indirectly + * through the Document.ImportNode method will not have children. + * The level of functionality you may get is extremely variable. + * + *
Also significant is that even at their most functional level, the fact + * that EntityReference children must be readonly has caused significant + * problems when modifying work products held in DOM trees. Other problems + * include issues related to undeclared namespace prefixes (and references + * to the current default namespace) that may be found in the text of such + * parsed entities nodes. These must be contextually bound as part of DOM + * tree construction. When such nodes are moved, the namespace associated + * with a given prefix (or default) may change to be in conflict with the + * namespace bound to the node at creation time. + * + *
In short, avoid using this DOM functionality. + * + * @see DomDoctype + * @see DomEntity + * + * @author David Brownell + * @author Chris Burdess + */ +public class DomEntityReference + extends DomNode + implements EntityReference +{ + + private String name; + + /** + * Constructs an EntityReference node associated with the specified + * document. The creator should populate this with whatever contents + * are appropriate, and then mark it as readonly. + * + *
This constructor should only be invoked by a Document as part of + * its createEntityReference functionality, or through a subclass which + * is similarly used in a "Sub-DOM" style layer. + * + * @see DomNode#makeReadonly + */ + protected DomEntityReference(DomDocument owner, String name) + { + super(ENTITY_REFERENCE_NODE, owner); + this.name = name; + } + + /** + * Returns the name of the referenced entity. + * @since DOM Level 1 Core + */ + public final String getNodeName() + { + return name; + } + + /** + * The base URI of an entity reference is the base URI where the entity + * declaration occurs. + * @since DOM Level 3 Core + */ + public final String getBaseURI() + { + DocumentType doctype = owner.getDoctype(); + if (doctype == null) + { + return null; + } + Entity entity = (Entity) doctype.getEntities().getNamedItem(name); + if (entity == null) + { + return null; + } + return entity.getBaseURI(); + } + +} diff --git a/libjava/classpath/gnu/xml/dom/DomEvent.java b/libjava/classpath/gnu/xml/dom/DomEvent.java new file mode 100644 index 000000000..3e9a6550a --- /dev/null +++ b/libjava/classpath/gnu/xml/dom/DomEvent.java @@ -0,0 +1,351 @@ +/* DomEvent.java -- + Copyright (C) 1999,2000,2001 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.dom; + +import gnu.java.lang.CPStringBuilder; + +import org.w3c.dom.Node; + +import org.w3c.dom.events.Event; +import org.w3c.dom.events.EventTarget; +import org.w3c.dom.events.MutationEvent; +import org.w3c.dom.events.UIEvent; + +import org.w3c.dom.views.AbstractView; // used by UIEvent + +/** + * "Event" implementation. Events are + * created (through DocumentEvent interface methods on the document object), + * and are sent to any target node in the document. + * + *
Applications may define application specific event subclasses, but + * should otherwise use the DocumentTraversal interface to acquire + * event objects. + * + * @author David Brownell + */ +public class DomEvent + implements Event +{ + + String type; // init + EventTarget target; + EventTarget currentNode; + short eventPhase; + boolean bubbles; // init + boolean cancelable; // init + long timeStamp; // ? + + /** Returns the event's type (name) as initialized */ + public final String getType() + { + return type; + } + + /** + * Returns event's target; delivery of an event is initiated + * by a target.dispatchEvent(event) invocation. + */ + public final EventTarget getTarget() + { + return target; + } + + /** + * Returns the target to which events are currently being + * delivered. When capturing or bubbling, this will not + * be what getTarget returns. + */ + public final EventTarget getCurrentTarget() + { + return currentNode; + } + + /** + * Returns CAPTURING_PHASE, AT_TARGET, or BUBBLING; + * only meaningful within EventListener.handleEvent + */ + public final short getEventPhase() + { + return eventPhase; + } + + /** + * Returns true if the news of the event bubbles to tree tops + * (as specified during initialization). + */ + public final boolean getBubbles() + { + return bubbles; + } + + /** + * Returns true if the default handling may be canceled + * (as specified during initialization). + */ + public final boolean getCancelable() + { + return cancelable; + } + + /** + * Returns the event's timestamp. + */ + public final long getTimeStamp() + { + return timeStamp; + } + + boolean stop; + boolean doDefault; + + /** + * Requests the event no longer be captured or bubbled; only + * listeners on the event target will see the event, if they + * haven't yet been notified. + * + *
Avoid using this except for application-specific + * events, for which you the protocol explicitly "blesses" the use + * of this with some event types. Otherwise, you are likely to break + * algorithms which depend on event notification either directly or + * through bubbling or capturing.
+ * + *Note that this method is not final, specifically to enable + * enforcing of policies about events always propagating.
+ */ + public void stopPropagation() + { + stop = true; + } + + /** + * Requests that whoever dispatched the event not perform their + * default processing when event delivery completes. Initializes + * event timestamp. + */ + public final void preventDefault() + { + doDefault = false; + } + + /** Initializes basic event state. */ + public void initEvent(String typeArg, + boolean canBubbleArg, + boolean cancelableArg) + { + eventPhase = 0; + type = typeArg; + bubbles = canBubbleArg; + cancelable = cancelableArg; + timeStamp = System.currentTimeMillis(); + } + + /** Constructs, but does not initialize, an event. */ + public DomEvent(String type) + { + this.type = type; + } + + /** + * Returns a basic printable description of the event's type, + * state, and delivery conditions + */ + public String toString() + { + CPStringBuilder buf = new CPStringBuilder("[Event "); + buf.append(type); + switch (eventPhase) + { + case CAPTURING_PHASE: + buf.append(", CAPTURING"); + break; + case AT_TARGET: + buf.append(", AT TARGET"); + break; + case BUBBLING_PHASE: + buf.append(", BUBBLING"); + break; + default: + buf.append(", (inactive)"); + break; + } + if (bubbles && eventPhase != BUBBLING_PHASE) + { + buf.append(", bubbles"); + } + if (cancelable) + { + buf.append(", can cancel"); + } + // were we to provide subclass info, this's where it'd live + buf.append("]"); + return buf.toString(); + } + + /** + * "MutationEvent" implementation. + */ + public static final class DomMutationEvent + extends DomEvent + implements MutationEvent + { + + // package private + Node relatedNode; // init + + private String prevValue; // init + private String newValue; // init + + private String attrName; // init + private short attrChange; // init + + /** Returns any "related" node provided by this type of event */ + public final Node getRelatedNode() + { + return relatedNode; + } + + /** Returns any "previous value" provided by this type of event */ + public final String getPrevValue() + { + return prevValue; + } + + /** Returns any "new value" provided by this type of event */ + public final String getNewValue() + { + return newValue; + } + + /** For attribute change events, returns the attribute's name */ + public final String getAttrName() + { + return attrName; + } + + /** For attribute change events, returns how the attribuet changed */ + public final short getAttrChange() + { + return attrChange; + } + + /** Initializes a mutation event */ + public final void initMutationEvent(String typeArg, + boolean canBubbleArg, + boolean cancelableArg, + Node relatedNodeArg, + String prevValueArg, + String newValueArg, + String attrNameArg, + short attrChangeArg) + { + // super.initEvent is inlined here for speed + // (mutation events are issued on all DOM changes) + eventPhase = 0; + type = typeArg; + bubbles = canBubbleArg; + cancelable = cancelableArg; + timeStamp = System.currentTimeMillis(); + + relatedNode = relatedNodeArg; + prevValue = prevValueArg; + newValue = newValueArg; + attrName = attrNameArg; + attrChange = attrChangeArg; + } + + // clear everything that should be GC-able + void clear() + { + type = null; + target = null; + relatedNode = null; + currentNode = null; + prevValue = newValue = attrName = null; + } + + /** Constructs an uninitialized mutation event. */ + public DomMutationEvent(String type) + { + super(type); + } + + } + + /** + * "UIEvent" implementation. + */ + public static class DomUIEvent + extends DomEvent + implements UIEvent + { + + private AbstractView view; // init + private int detail; // init + + /** Constructs an uninitialized User Interface (UI) event */ + public DomUIEvent (String type) { super (type); } + + public final AbstractView getView () { return view; } + public final int getDetail () { return detail; } + + /** Initializes a UI event */ + public final void initUIEvent(String typeArg, + boolean canBubbleArg, + boolean cancelableArg, + AbstractView viewArg, + int detailArg) + { + super.initEvent(typeArg, canBubbleArg, cancelableArg); + view = viewArg; + detail = detailArg; + } + + } + + /* + + static final class DomMouseEvent extends DomUIEvent + implements MouseEvent + { + // another half dozen state variables/accessors + } + + */ + +} diff --git a/libjava/classpath/gnu/xml/dom/DomExtern.java b/libjava/classpath/gnu/xml/dom/DomExtern.java new file mode 100644 index 000000000..7f2e55afc --- /dev/null +++ b/libjava/classpath/gnu/xml/dom/DomExtern.java @@ -0,0 +1,116 @@ +/* DomExtern.java -- + Copyright (C) 1999,2000,2001,2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.dom; + +/** + *Abstract implemention of nodes describing external DTD-related + * objects. This facilitates reusing code for Entity, Notation, and + * DocumentType (really, external subset) nodes. Such support is not + * part of the core DOM; it's for the "XML" feature.
+ * + *Note that you are strongly advised to avoid using the DOM + * features that take advantage of this class, since (as of L2) none + * of them is defined fully enough to permit full use of the + * XML feature they partially expose.
+ * + * @author David Brownell + * @author Chris Burdess + */ +public abstract class DomExtern + extends DomNode +{ + + private final String name; + private final String publicId; + private final String systemId; + + /** + * Constructs a node associated with the specified document, + * with the specified descriptive data. + * + * @param owner The document with which this object is associated + * @param name Name of this object + * @param publicId If non-null, provides the entity's PUBLIC identifier + * @param systemId If non-null, provides the entity's SYSTEM identifier + */ + // package private + DomExtern(short nodeType, + DomDocument owner, + String name, + String publicId, + String systemId) + { + super(nodeType, owner); + this.name = name; + this.publicId = publicId; + this.systemId = systemId; + } + + /** + * DOM L1 + * Returns the SYSTEM identifier associated with this object, if any. + */ + public final String getSystemId() + { + return systemId; + } + + /** + * DOM L1 + * Returns the PUBLIC identifier associated with this object, if any. + */ + public final String getPublicId() + { + return publicId; + } + + /** + * DOM L1 + * Returns the object's name. + */ + public final String getNodeName() + { + return name; + } + + public final String getLocalName() + { + return name; + } + +} diff --git a/libjava/classpath/gnu/xml/dom/DomImpl.java b/libjava/classpath/gnu/xml/dom/DomImpl.java new file mode 100644 index 000000000..76a63caf6 --- /dev/null +++ b/libjava/classpath/gnu/xml/dom/DomImpl.java @@ -0,0 +1,277 @@ +/* DomImpl.java -- + Copyright (C) 1999,2000,2001,2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.dom; + +import org.w3c.dom.Document; +import org.w3c.dom.DocumentType; +import org.w3c.dom.DOMException; +import org.w3c.dom.DOMImplementation; +import org.w3c.dom.Element; +import org.w3c.dom.ls.DOMImplementationLS; +import org.w3c.dom.ls.LSInput; +import org.w3c.dom.ls.LSOutput; +import org.w3c.dom.ls.LSParser; +import org.w3c.dom.ls.LSSerializer; +import gnu.xml.dom.html2.DomHTMLImpl; +import gnu.xml.dom.ls.DomLSInput; +import gnu.xml.dom.ls.DomLSOutput; +import gnu.xml.dom.ls.DomLSParser; +import gnu.xml.dom.ls.DomLSSerializer; + +/** + *"DOMImplementation" implementation.
+ * + *At this writing, the following features are supported: + * "XML" (L1, L2, L3), + * "Events" (L2), "MutationEvents" (L2), "USER-Events" (a conformant extension), + * "HTMLEvents" (L2), "UIEvents" (L2), "Traversal" (L2), "XPath" (L3), + * "LS" (L3) "LS-Async" (L3). + * It is possible to compile the package so it doesn't support some of these + * features (notably, Traversal). + * + * @author David Brownell + * @author Chris Burdess + */ +public class DomImpl + implements DOMImplementation, DOMImplementationLS +{ + + /** + * Constructs a DOMImplementation object which supports + * "XML" and other DOM Level 2 features. + */ + public DomImpl() + { + } + + /** + * DOM L1 + * Returns true if the specified feature and version are + * supported. Note that the case of the feature name is ignored. + */ + public boolean hasFeature(String name, String version) + { + if (name.length() == 0) + { + return false; + } + name = name.toLowerCase(); + if (name.charAt(0) == '+') + { + name = name.substring(1); + } + + if ("xml".equals(name) || "core".equals(name)) + { + return (version == null || + "".equals(version) || + "1.0".equals(version) || + "2.0".equals(version) || + "3.0".equals(version)); + + } + else if ("ls".equals(name) || "ls-async".equals(name)) + { + return (version == null || + "".equals(version) || + "3.0".equals(version)); + } + else if ("events".equals(name) + || "mutationevents".equals(name) + || "uievents".equals(name) + // || "mouseevents".equals(name) + || "htmlevents".equals(name)) + { + return (version == null || + "".equals(version) || + "2.0".equals(version)); + + // Extension: "USER-" prefix event types can + // be created and passed through the DOM. + + } + else if ("user-events".equals(name)) + { + return (version == null || + "".equals(version) || + "0.1".equals(version)); + + // NOTE: "hasFeature" for events is here interpreted to + // mean the DOM can manufacture those sorts of events, + // since actually choosing to report the events is more + // often part of the environment or application. It's + // only really an issue for mutation events. + + } + else if (DomNode.reportMutations + && "traversal".equals(name)) + { + return (version == null || + "".equals(version) || + "2.0".equals(version)); + } + else if ("xpath".equals(name)) + { + return (version == null || + "".equals(version) || + "3.0".equals(version)); + } + else if ("html".equals(name) || "xhtml".equals(name)) + { + return (version == null || + "".equals(version) || + "2.0".equals(version)); + } + + // views + // stylesheets + // css, css2 + // range + + return false; + } + + /** + * DOM L2 + * Creates and returns a DocumentType, associated with this + * implementation. This DocumentType can have no associated + * objects(notations, entities) until the DocumentType is + * first associated with a document. + * + *
Note that there is no implication that this DTD will + * be parsed by the DOM, or ever have contents. Moreover, the + * DocumentType created here can only be added to a document by + * the createDocument method(below). That means that the only + * portable way to create a Document object is to start parsing, + * queue comment and processing instruction (PI) nodes, and then only + * create a DOM Document after (a) it's known if a DocumentType + * object is needed, and (b) the name and namespace of the root + * element is known. Queued comment and PI nodes would then be + * inserted appropriately in the document prologue, both before and + * after the DTD node, and additional attributes assigned to the + * root element. + *(One hopes that the final DOM REC fixes this serious botch.) + */ + public DocumentType createDocumentType(String rootName, + String publicId, + String systemId) + // CR2 deleted internal subset, ensuring DocumentType + // is 100% useless instead of just 90% so. + { + DomDocument.checkNCName(rootName, false); + return new DomDoctype(this, rootName, publicId, systemId, null); + } + + /** + * DOM L2 + * Creates and returns a Document, populated only with a root element and + * optionally a document type(if that was provided). + */ + public Document createDocument(String namespaceURI, + String rootName, + DocumentType doctype) + { + Document doc = createDocument(); + Element root = null; + + if (rootName != null) + { + root = doc.createElementNS(namespaceURI, rootName); + if (rootName.startsWith("xmlns:")) + { + throw new DomDOMException(DOMException.NAMESPACE_ERR, + "xmlns is reserved", null, 0); + } + } + // Bleech -- L2 seemingly _requires_ omission of xmlns attributes. + if (doctype != null) + { + doc.appendChild(doctype); // handles WRONG_DOCUMENT error + } + if (root != null) + { + doc.appendChild(root); + } + return doc; + } + + protected Document createDocument() + { + return new DomDocument(this); + } + + // DOM Level 3 + + public Object getFeature(String feature, String version) + { + if (hasFeature(feature, version)) + { + if ("html".equalsIgnoreCase(feature) || + "xhtml".equalsIgnoreCase(feature)) + { + return new DomHTMLImpl(); + } + return this; + } + return null; + } + + // -- DOMImplementationLS -- + + public LSParser createLSParser(short mode, String schemaType) + throws DOMException + { + return new DomLSParser(mode, schemaType); + } + + public LSSerializer createLSSerializer() + { + return new DomLSSerializer(); + } + + public LSInput createLSInput() + { + return new DomLSInput(); + } + + public LSOutput createLSOutput() + { + return new DomLSOutput(); + } + +} diff --git a/libjava/classpath/gnu/xml/dom/DomIterator.java b/libjava/classpath/gnu/xml/dom/DomIterator.java new file mode 100644 index 000000000..d5f305cd8 --- /dev/null +++ b/libjava/classpath/gnu/xml/dom/DomIterator.java @@ -0,0 +1,380 @@ +/* DomIterator.java -- + Copyright (C) 1999, 2000, 2001, 2006 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.dom; + +import org.w3c.dom.DOMException; +import org.w3c.dom.Node; +import org.w3c.dom.events.Event; +import org.w3c.dom.events.EventListener; +import org.w3c.dom.events.EventTarget; +import org.w3c.dom.events.MutationEvent; +import org.w3c.dom.traversal.NodeFilter; +import org.w3c.dom.traversal.NodeIterator; + +/** + *
"NodeIterator" implementation, usable with any L2 DOM which + * supports MutationEvents.
+ * + * @author David Brownell + */ +public final class DomIterator + implements NodeIterator, EventListener +{ + + private Node reference; + private boolean right; + private boolean done; + + private final Node root; + private final int whatToShow; + private final NodeFilter filter; + private final boolean expandEntityReferences; + + /** + * Constructs and initializes an iterator. + */ + protected DomIterator(Node root, + int whatToShow, + NodeFilter filter, + boolean entityReferenceExpansion) + { + if (!root.isSupported("MutationEvents", "2.0")) + { + throw new DomDOMException(DOMException.NOT_SUPPORTED_ERR, + "Iterator needs mutation events", root, 0); + } + + this.root = root; + this.whatToShow = whatToShow; + this.filter = filter; + this.expandEntityReferences = entityReferenceExpansion; + + // start condition: going right, seen nothing yet. + reference = null; + right = true; + + EventTarget target = (EventTarget) root; + target.addEventListener("DOMNodeRemoved", this, false); + } + + /** + * DOM L2 + * Flags the iterator as done, unregistering its event listener so + * that the iterator can be garbage collected without relying on weak + * references (a "Java 2" feature) in the event subsystem. + */ + public void detach() + { + EventTarget target = (EventTarget) root; + target.removeEventListener("DOMNodeRemoved", this, false); + done = true; + } + + /** + * DOM L2 + * Returns the flag controlling whether iteration descends + * through entity references. + */ + public boolean getExpandEntityReferences() + { + return expandEntityReferences; + } + + /** + * DOM L2 + * Returns the filter provided during construction. + */ + public NodeFilter getFilter() + { + return filter; + } + + /** + * DOM L2 + * Returns the root of the tree this is iterating through. + */ + public Node getRoot() + { + return root; + } + + /** + * DOM L2 + * Returns the mask of flags provided during construction. + */ + public int getWhatToShow() + { + return whatToShow; + } + + /** + * DOM L2 + * Returns the next node in a forward iteration, masked and filtered. + * Note that the node may be read-only due to entity expansions. + * A null return indicates the iteration is complete, but may still + * be processed backwards. + */ + public Node nextNode() + { + if (done) + { + throw new DomDOMException(DOMException.INVALID_STATE_ERR); + } + right = true; + return walk(true); + } + + /** + * DOM L2 + * Returns the next node in a backward iteration, masked and filtered. + * Note that the node may be read-only due to entity expansions. + * A null return indicates the iteration is complete, but may still + * be processed forwards. + */ + public Node previousNode() + { + if (done) + { + throw new DomDOMException(DOMException.INVALID_STATE_ERR); + } + Node previous = reference; + right = false; + walk(false); + return previous; + } + + private boolean shouldShow(Node node) + // raises Runtime exceptions indirectly, via acceptNode() + { + if ((whatToShow & (1 << (node.getNodeType() - 1))) == 0) + { + return false; + } + if (filter == null) + { + return true; + } + return filter.acceptNode(node) == NodeFilter.FILTER_ACCEPT; + } + + // + // scenario: root = 1, sequence = 1 2 ... 3 4 + // forward walk: 1 2 ... 3 4 null + // then backward: 4 3 ... 2 1 null + // + // At the leftmost end, "previous" == null + // At the rightmost end, "previous" == 4 + // + // The current draft spec really seem to make no sense re the + // role of the reference node, so what it says is ignored here. + // + private Node walk(boolean forward) + { + Node here = reference; + + while ((here = successor(here, forward)) != null + && !shouldShow(here)) + { + continue; + } + if (here != null || !forward) + { + reference = here; + } + return here; + } + + private boolean isLeaf(Node here) + { + boolean leaf = !here.hasChildNodes(); + if (!leaf && !expandEntityReferences) + { + leaf = (here.getNodeType() == Node.ENTITY_REFERENCE_NODE); + } + return leaf; + } + + // + // Returns the immediate successor in a forward (or backward) + // document order walk, sans filtering ... except that it knows + // how to stop, returning null when done. This is a depth first + // preorder traversal when run in the forward direction. + // + private Node successor(Node here, boolean forward) + { + Node next; + + // the "leftmost" end is funky + if (here == null) + { + return forward ? root : null; + } + + // + // Forward, this is preorder: children before siblings. + // Backward, it's postorder: we saw the children already. + // + if (forward && !isLeaf(here)) + { + return here.getFirstChild(); + } + + // There's no way up or sideways from the root, so if we + // couldn't move down to a child, there's nowhere to go. + // + if (here == root) + return null; + + // + // Siblings ... if forward, we visit them, if backwards + // we visit their children first. + // + if (forward) + { + if ((next = here.getNextSibling()) != null) + { + return next; + } + } + else if ((next = here.getPreviousSibling()) != null) + { + if (isLeaf(next)) + { + return next; + } + next = next.getLastChild(); + while (!isLeaf(next)) + { + next = next.getLastChild(); + } + return next; + } + + // + // We can't go down or lateral -- it's up, then. The logic is + // the converse of what's above: backwards is easy (the parent + // is next), forwards isn't. + // + next = here.getParentNode(); + if (!forward) + { + return next; + } + + Node temp = null; + while (next != null + && next != root + && (temp = next.getNextSibling()) == null) + { + next = next.getParentNode(); + } + + // If we have exceeded the root node then stop traversing. + if (next == root.getParentNode()) + { + return null; + } + return temp; + } + + /** + * Not for public use. This lets the iterator know when its + * reference node will be removed from the tree, so that a new + * one may be selected. + * + *This version works by watching removal events as they + * bubble up. So, don't prevent them from bubbling. + */ + public void handleEvent(Event e) + { + MutationEvent event; + Node ancestor, removed; + + if (reference == null + || !"DOMNodeRemoved".equals(e.getType()) + || e.getEventPhase() != Event.BUBBLING_PHASE) + { + return; + } + + event = (MutationEvent) e; + removed = (Node) event.getTarget(); + + // See if the removal will cause trouble for this iterator + // by being the reference node or an ancestor of it. + for (ancestor = reference; + ancestor != null && ancestor != root; + ancestor = ancestor.getParentNode()) + { + if (ancestor == removed) + { + break; + } + } + if (ancestor != removed) + { + return; + } + + // OK, it'll cause trouble. We want to make the "next" + // node in our current traversal direction seem right. + // So we pick the nearest node that's not getting removed, + // but go in the _opposite_ direction from our current + // traversal ... so the "next" doesn't skip anything. + Node candidate; + +search: + while ((candidate = walk(!right)) != null) + { + for (ancestor = candidate; + ancestor != null && ancestor != root; + ancestor = ancestor.getParentNode()) + { + if (ancestor == removed) + { + continue search; + } + } + return; + } + + // The current DOM WD talks about a special case here; + // I've not yet seen it. + } + +} diff --git a/libjava/classpath/gnu/xml/dom/DomNSResolverContext.java b/libjava/classpath/gnu/xml/dom/DomNSResolverContext.java new file mode 100644 index 000000000..3dd957560 --- /dev/null +++ b/libjava/classpath/gnu/xml/dom/DomNSResolverContext.java @@ -0,0 +1,90 @@ +/* DomNSResolverContext.java -- + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.dom; + +import java.util.Iterator; +import javax.xml.namespace.NamespaceContext; +import org.w3c.dom.xpath.XPathNSResolver; + +/** + * Namespace content wrapper for an XPathNSResolver. + * + * @author Chris Burdess + */ +class DomNSResolverContext + implements NamespaceContext, Iterator +{ + + final XPathNSResolver resolver; + + DomNSResolverContext(XPathNSResolver resolver) + { + this.resolver = resolver; + } + + public String getNamespaceURI(String prefix) + { + return resolver.lookupNamespaceURI(prefix); + } + + public String getPrefix(String namespaceURI) + { + return null; + } + + public Iterator getPrefixes(String namespaceURI) + { + return this; + } + + public boolean hasNext() + { + return false; + } + + public Object next() + { + return null; + } + + public void remove() + { + throw new UnsupportedOperationException(); + } + +} diff --git a/libjava/classpath/gnu/xml/dom/DomNamedNodeMap.java b/libjava/classpath/gnu/xml/dom/DomNamedNodeMap.java new file mode 100644 index 000000000..7a767355a --- /dev/null +++ b/libjava/classpath/gnu/xml/dom/DomNamedNodeMap.java @@ -0,0 +1,421 @@ +/* DomNamedNodeMap.java -- + Copyright (C) 1999,2000,2001,2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.dom; + +import org.w3c.dom.DOMException; +import org.w3c.dom.NamedNodeMap; +import org.w3c.dom.Node; + +/** + *
"NamedNodeMap" implementation.
+ * Used mostly to hold element attributes, but sometimes also + * to list notations or entities. + * + * @author David Brownell + * @author Chris Burdess + */ +public class DomNamedNodeMap + implements NamedNodeMap +{ + + final DomNode owner; + final short type; + + DomNode first; + int length; + boolean readonly; + + // package private + DomNamedNodeMap(DomNode owner, short type) + { + this.owner = owner; + this.type = type; + } + + /** + * Exposes the internal "readonly" flag. In DOM, all NamedNodeMap + * objects found in a DocumentType object are read-only (after + * they are fully constructed), and those holding attributes of + * a readonly element will also be readonly. + */ + public final boolean isReadonly() + { + return readonly; + } + + /** + * Sets the internal "readonly" flag so the node and its + * children can't be changed. + */ + public void makeReadonly() + { + readonly = true; + for (DomNode ctx = first; ctx != null; ctx = ctx.next) + { + ctx.makeReadonly(); + } + } + + /** + * DOM L1 + * Returns the named item from the map, or null; names are just + * the nodeName property. + */ + public Node getNamedItem(String name) + { + for (DomNode ctx = first; ctx != null; ctx = ctx.next) + { + if (ctx.getNodeName().equals(name)) + { + return ctx; + } + } + return null; + } + + /** + * DOM L2 + * Returns the named item from the map, or null; names are the + * localName and namespaceURI properties, ignoring any prefix. + */ + public Node getNamedItemNS(String namespaceURI, String localName) + { + if ("".equals(namespaceURI)) + { + namespaceURI = null; + } + for (DomNode ctx = first; ctx != null; ctx = ctx.next) + { + String name = ctx.getLocalName(); + if ((localName == null && name == null) || + (localName != null && localName.equals(name))) + { + String uri = ctx.getNamespaceURI(); + if ("".equals(uri)) + { + uri = null; + } + if ((namespaceURI == null && uri == null) || + (namespaceURI != null && namespaceURI.equals(uri))) + { + return ctx; + } + } + } + return null; + } + + /** + * DOM L1 + * Stores the named item into the map, optionally overwriting + * any existing node with that name. The name used is just + * the nodeName attribute. + */ + public Node setNamedItem(Node arg) + { + return setNamedItem(arg, false, false); + } + + /** + * DOM L2 + * Stores the named item into the map, optionally overwriting + * any existing node with that fully qualified name. The name + * used incorporates the localName and namespaceURI properties, + * and ignores any prefix. + */ + public Node setNamedItemNS(Node arg) + { + return setNamedItem(arg, true, false); + } + + Node setNamedItem(Node arg, boolean ns, boolean cloning) + { + if (readonly) + { + throw new DomDOMException(DOMException.NO_MODIFICATION_ALLOWED_ERR); + } + + DomNode node = (DomNode) arg; + if (!cloning && node.owner != owner.owner) + { + throw new DomDOMException(DOMException.WRONG_DOCUMENT_ERR); + } + if (node.nodeType != type) + { + throw new DomDOMException(DOMException.HIERARCHY_REQUEST_ERR); + } + if (node.nodeType == Node.ATTRIBUTE_NODE) + { + DomNode element = node.parent; + if (element != null && element != owner) + { + throw new DomDOMException(DOMException.INUSE_ATTRIBUTE_ERR); + } + node.parent = owner; + node.depth = owner.depth + 1; + } + + String nodeName = node.getNodeName(); + String localName = ns ? node.getLocalName() : null; + String namespaceURI = ns ? node.getNamespaceURI() : null; + if ("".equals(namespaceURI)) + { + namespaceURI = null; + } + + // maybe attribute ADDITION events (?) + DomNode last = null; + for (DomNode ctx = first; ctx != null; ctx = ctx.next) + { + boolean test = false; + if (ns) + { + String tln = ctx.getLocalName(); + if (tln == null) + { + tln = ctx.getNodeName(); + } + if (tln.equals(localName)) + { + String tu = ctx.getNamespaceURI(); + if ((tu == null && namespaceURI == null) || + (tu != null && tu.equals(namespaceURI))) + { + test = true; + } + } + } + else + { + test = ctx.getNodeName().equals(nodeName); + } + if (test) + { + // replace + node.previous = ctx.previous; + node.next = ctx.next; + if (ctx.previous != null) + { + ctx.previous.next = node; + } + if (ctx.next != null) + { + ctx.next.previous = node; + } + if (first == ctx) + { + first = node; + } + reparent(node, nodeName, ctx.index); + ctx.parent = null; + ctx.next = null; + ctx.previous = null; + ctx.setDepth(0); + ctx.index = 0; + return ctx; + } + last = ctx; + } + // append + if (last != null) + { + last.next = node; + node.previous = last; + } + else + { + first = node; + } + length++; + reparent(node, nodeName, 0); + return null; + } + + void reparent(DomNode node, String nodeName, int i) + { + node.parent = owner; + node.setDepth(owner.depth + 1); + // index renumbering + for (DomNode ctx = node; ctx != null; ctx = ctx.next) + { + ctx.index = i++; + } + // cache xml:space + boolean xmlSpace = "xml:space".equals(nodeName); + if (xmlSpace && owner instanceof DomElement) + { + ((DomElement) owner).xmlSpace = node.getNodeValue(); + } + } + + /** + * DOM L1 + * Removes the named item from the map, or reports an exception; + * names are just the nodeName property. + */ + public Node removeNamedItem(String name) + { + return removeNamedItem(null, name, false); + } + + /** + * DOM L2 + * Removes the named item from the map, or reports an exception; + * names are the localName and namespaceURI properties. + */ + public Node removeNamedItemNS(String namespaceURI, String localName) + { + return removeNamedItem(namespaceURI, localName, true); + } + + Node removeNamedItem(String uri, String name, boolean ns) + { + if (readonly) + { + throw new DomDOMException(DOMException.NO_MODIFICATION_ALLOWED_ERR); + } + + // report attribute REMOVAL event? + + for (DomNode ctx = first; ctx != null; ctx = ctx.next) + { + boolean test = false; + String nodeName = ctx.getNodeName(); + if (ns) + { + String tln = ctx.getLocalName(); + if (name != null && name.equals(tln)) + { + String tu = ctx.getNamespaceURI(); + if ((tu == null && uri == null) || + (tu != null && tu.equals(uri))) + { + test = true; + } + } + } + else + { + test = nodeName.equals(name); + } + if (test) + { + // uncache xml:space + boolean xmlSpace = "xml:space".equals(nodeName); + if (xmlSpace && owner instanceof DomElement) + { + ((DomElement) owner).xmlSpace = ""; + } + // is this a default attribute? + if (ctx.nodeType == Node.ATTRIBUTE_NODE) + { + String def = getDefaultValue(ctx.getNodeName()); + if (def != null) + { + ctx.setNodeValue(def); + ((DomAttr) ctx).setSpecified(false); + return null; + } + } + // remove + if (ctx == first) + { + first = ctx.next; + } + if (ctx.previous != null) + { + ctx.previous.next = ctx.next; + } + if (ctx.next != null) + { + ctx.next.previous = ctx.previous; + } + length--; + ctx.previous = null; + ctx.next = null; + ctx.parent = null; + ctx.setDepth(0); + ctx.index = 0; + return ctx; + } + } + throw new DomDOMException(DOMException.NOT_FOUND_ERR); + } + + String getDefaultValue(String name) + { + DomDoctype doctype = (DomDoctype) owner.owner.getDoctype(); + if (doctype == null) + { + return null; + } + DTDAttributeTypeInfo info = + doctype.getAttributeTypeInfo(owner.getNodeName(), name); + if (info == null) + { + return null; + } + return info.value; + } + + /** + * DOM L1 + * Returns the indexed item from the map, or null. + */ + public Node item(int index) + { + DomNode ctx = first; + int count = 0; + while (ctx != null && count < index) + { + ctx = ctx.next; + count++; + } + return ctx; + } + + /** + * DOM L1 + * Returns the length of the map. + */ + public int getLength() + { + return length; + } + +} diff --git a/libjava/classpath/gnu/xml/dom/DomNode.java b/libjava/classpath/gnu/xml/dom/DomNode.java new file mode 100644 index 000000000..879baaa8d --- /dev/null +++ b/libjava/classpath/gnu/xml/dom/DomNode.java @@ -0,0 +1,2222 @@ +/* DomNode.java -- + Copyright (C) 1999,2000,2001,2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.dom; + +import gnu.java.lang.CPStringBuilder; + +import java.util.HashMap; +import java.util.HashSet; +import java.util.Iterator; +import java.util.Map; + +import org.w3c.dom.Document; +import org.w3c.dom.DOMException; +import org.w3c.dom.DOMImplementation; +import org.w3c.dom.NamedNodeMap; +import org.w3c.dom.Node; +import org.w3c.dom.NodeList; +import org.w3c.dom.Text; +import org.w3c.dom.UserDataHandler; +import org.w3c.dom.events.DocumentEvent; +import org.w3c.dom.events.Event; +import org.w3c.dom.events.EventException; +import org.w3c.dom.events.EventListener; +import org.w3c.dom.events.EventTarget; +import org.w3c.dom.events.MutationEvent; +import org.w3c.dom.traversal.NodeFilter; +import org.w3c.dom.traversal.NodeIterator; + +/** + *"Node", "EventTarget", and "DocumentEvent" implementation. + * This provides most of the core DOM functionality; only more + * specialized features are provided by subclasses. Those subclasses may + * have some particular constraints they must implement, by overriding + * methods defined here. Such constraints are noted here in the method + * documentation.
+ * + *Note that you can create events with type names prefixed with "USER-", + * and pass them through this DOM. This lets you use the DOM event scheme + * for application specific purposes, although you must use a predefined event + * structure (such as MutationEvent) to pass data along with those events. + * Test for existence of this feature with the "USER-Events" DOM feature + * name.
+ * + *Other kinds of events you can send include the "html" events, + * like "load", "unload", "abort", "error", and "blur"; and the mutation + * events. If this DOM has been compiled with mutation event support + * enabled, it will send mutation events when you change parts of the + * tree; otherwise you may create and send such events yourself, but + * they won't be generated by the DOM itself.
+ * + *Note that there is a namespace-aware name comparison method, + * nameAndTypeEquals, which compares the names (and types) of + * two nodes in conformance with the "Namespaces in XML" specification. + * While mostly intended for use with elements and attributes, this should + * also be helpful for ProcessingInstruction nodes and some others which + * do not have namespace URIs. + * + * @author David Brownell + * @author Chris Burdess + */ +public abstract class DomNode + implements Node, NodeList, EventTarget, DocumentEvent, Cloneable, Comparable +{ + + // package private + //final static String xmlNamespace = "http://www.w3.org/XML/1998/namespace"; + //final static String xmlnsURI = "http://www.w3.org/2000/xmlns/"; + + // tunable + // NKIDS_* affects arrays of children (which grow) + // (currently) fixed size: + // ANCESTORS_* is for event capture/bubbling, # ancestors + // NOTIFICATIONS_* is for per-node event delivery, # events + private static final int NKIDS_DELTA = 8; + private static final int ANCESTORS_INIT = 20; + private static final int NOTIFICATIONS_INIT = 10; + + // tunable: enable mutation events or not? Enabling it costs about + // 10-15% in DOM construction time, last time it was measured. + + // package private !!! + static final boolean reportMutations = true; + + // locking protocol changeable only within this class + private static final Object lockNode = new Object(); + + // NON-FINAL class data + + // Optimize event dispatch by not allocating memory each time + private static boolean dispatchDataLock; + private static DomNode[] ancestors = new DomNode[ANCESTORS_INIT]; + private static ListenerRecord[] notificationSet + = new ListenerRecord[NOTIFICATIONS_INIT]; + + // Ditto for the (most common) event object itself! + private static boolean eventDataLock; + private static DomEvent.DomMutationEvent mutationEvent + = new DomEvent.DomMutationEvent(null); + + // + // PER-INSTANCE DATA + // + + DomDocument owner; + DomNode parent; // parent node; + DomNode previous; // previous sibling node + DomNode next; // next sibling node + DomNode first; // first child node + DomNode last; // last child node + int index; // index of this node in its parent's children + int depth; // depth of the node in the document + int length; // number of children + final short nodeType; + + // Bleech ... "package private" so a builder can populate entity refs. + // writable during construction. DOM spec is nasty. + boolean readonly; + + // event registrations + private HashSet listeners; + private int nListeners; + + // DOM Level 3 userData dictionary. + private HashMap userData; + private HashMap userDataHandlers; + + // + // Some of the methods here are declared 'final' because + // knowledge about their implementation is built into this + // class -- for both integrity and performance. + // + + /** + * Reduces space utilization for this node. + */ + public void compact() + { + } + + /** + * Constructs a node and associates it with its owner. Only + * Document and DocumentType nodes may be created with no owner, + * and DocumentType nodes get an owner as soon as they are + * associated with a document. + */ + protected DomNode(short nodeType, DomDocument owner) + { + this.nodeType = nodeType; + + if (owner == null) + { + // DOM calls never go down this path + if (nodeType != DOCUMENT_NODE && nodeType != DOCUMENT_TYPE_NODE) + { + throw new IllegalArgumentException ("no owner!"); + } + } + this.owner = owner; + this.listeners = new HashSet(); + } + + + /** + * DOM L1 + * Returns null; Element subclasses must override this method. + */ + public NamedNodeMap getAttributes() + { + return null; + } + + /** + * DOM L2> + * Returns true iff this is an element node with attributes. + */ + public boolean hasAttributes() + { + return false; + } + + /** + * DOM L1 + * Returns a list, possibly empty, of the children of this node. + * In this implementation, to conserve memory, nodes are the same + * as their list of children. This can have ramifications for + * subclasses, which may need to provide their own getLength method + * for reasons unrelated to the NodeList method of the same name. + */ + public NodeList getChildNodes() + { + return this; + } + + /** + * DOM L1 + * Returns the first child of this node, or null if there are none. + */ + public Node getFirstChild() + { + return first; + } + + /** + * DOM L1 + * Returns the last child of this node, or null if there are none. + */ + public Node getLastChild() + { + return last; + } + + /** + * DOM L1 + * Returns true if this node has children. + */ + public boolean hasChildNodes() + { + return length != 0; + } + + + /** + * Exposes the internal "readonly" flag. In DOM, children of + * entities and entity references are readonly, as are the + * objects associated with DocumentType objets. + */ + public final boolean isReadonly() + { + return readonly; + } + + /** + * Sets the internal "readonly" flag so this subtree can't be changed. + * Subclasses need to override this method for any associated content + * that's not a child node, such as an element's attributes or the + * (few) declarations associated with a DocumentType. + */ + public void makeReadonly() + { + readonly = true; + for (DomNode child = first; child != null; child = child.next) + { + child.makeReadonly(); + } + } + + /** + * Used to adopt a node to a new document. + */ + void setOwner(DomDocument doc) + { + this.owner = doc; + for (DomNode ctx = first; ctx != null; ctx = ctx.next) + { + ctx.setOwner(doc); + } + } + + // just checks the node for inclusion -- may be called many + // times (docfrag) before anything is allowed to change + private void checkMisc(DomNode child) + { + if (readonly && !owner.building) + { + throw new DomDOMException(DOMException.NO_MODIFICATION_ALLOWED_ERR, + null, this, 0); + } + for (DomNode ctx = this; ctx != null; ctx = ctx.parent) + { + if (child == ctx) + { + throw new DomDOMException(DOMException.HIERARCHY_REQUEST_ERR, + "can't make ancestor into a child", + this, 0); + } + } + + DomDocument owner = (nodeType == DOCUMENT_NODE) ? (DomDocument) this : + this.owner; + DomDocument childOwner = child.owner; + short childNodeType = child.nodeType; + + if (childOwner != owner) + { + // new in DOM L2, this case -- patch it up later, in reparent() + if (!(childNodeType == DOCUMENT_TYPE_NODE && childOwner == null)) + { + throw new DomDOMException(DOMException.WRONG_DOCUMENT_ERR, + null, child, 0); + } + } + + // enforce various structural constraints + switch (nodeType) + { + case DOCUMENT_NODE: + switch (childNodeType) + { + case ELEMENT_NODE: + case PROCESSING_INSTRUCTION_NODE: + case COMMENT_NODE: + case DOCUMENT_TYPE_NODE: + return; + } + break; + + case ATTRIBUTE_NODE: + switch (childNodeType) + { + case TEXT_NODE: + case ENTITY_REFERENCE_NODE: + return; + } + break; + + case DOCUMENT_FRAGMENT_NODE: + case ENTITY_REFERENCE_NODE: + case ELEMENT_NODE: + case ENTITY_NODE: + switch (childNodeType) + { + case ELEMENT_NODE: + case TEXT_NODE: + case COMMENT_NODE: + case PROCESSING_INSTRUCTION_NODE: + case CDATA_SECTION_NODE: + case ENTITY_REFERENCE_NODE: + return; + } + break; + case DOCUMENT_TYPE_NODE: + if (!owner.building) + break; + switch (childNodeType) + { + case COMMENT_NODE: + case PROCESSING_INSTRUCTION_NODE: + return; + } + break; + } + if (owner.checkingWellformedness) + { + throw new DomDOMException(DOMException.HIERARCHY_REQUEST_ERR, + "can't append " + + nodeTypeToString(childNodeType) + + " to node of type " + + nodeTypeToString(nodeType), + this, 0); + } + } + + // Here's hoping a good optimizer will detect the case when the + // next several methods are never called, and won't allocate + // object code space of any kind. (Case: not reporting any + // mutation events. We can also remove some static variables + // listed above.) + + private void insertionEvent(DomEvent.DomMutationEvent event, + DomNode target) + { + if (owner == null || owner.building) + { + return; + } + boolean doFree = false; + + if (event == null) + { + event = getMutationEvent(); + } + if (event != null) + { + doFree = true; + } + else + { + event = new DomEvent.DomMutationEvent(null); + } + event.initMutationEvent("DOMNodeInserted", + true /* bubbles */, false /* nocancel */, + this /* related */, null, null, null, (short) 0); + target.dispatchEvent(event); + + // XXX should really visit every descendant of 'target' + // and sent a DOMNodeInsertedIntoDocument event to it... + // bleech, there's no way to keep that acceptably fast. + + if (doFree) + { + event.target = null; + event.relatedNode = null; + event.currentNode = null; + eventDataLock = false; + } // else we created work for the GC + } + + private void removalEvent(DomEvent.DomMutationEvent event, + DomNode target) + { + if (owner == null || owner.building) + { + return; + } + boolean doFree = false; + + if (event == null) + { + event = getMutationEvent(); + } + if (event != null) + { + doFree = true; + } + else + { + event = new DomEvent.DomMutationEvent(null); + } + event.initMutationEvent("DOMNodeRemoved", + true /* bubbles */, false /* nocancel */, + this /* related */, null, null, null, (short) 0); + target.dispatchEvent(event); + + // XXX should really visit every descendant of 'target' + // and sent a DOMNodeRemovedFromDocument event to it... + // bleech, there's no way to keep that acceptably fast. + + event.target = null; + event.relatedNode = null; + event.currentNode = null; + if (doFree) + { + eventDataLock = false; + } + // else we created more work for the GC + } + + // + // Avoid creating lots of memory management work, by using a simple + // allocation strategy for the mutation event objects that get used + // at least once per tree modification. We can't use stack allocation, + // so we do the next simplest thing -- more or less, static allocation. + // Concurrent notifications should be rare, anyway. + // + // Returns the preallocated object, which needs to be carefully freed, + // or null to indicate the caller needs to allocate their own. + // + static private DomEvent.DomMutationEvent getMutationEvent() + { + synchronized (lockNode) + { + if (eventDataLock) + { + return null; + } + eventDataLock = true; + return mutationEvent; + } + } + + // NOTE: this is manually inlined in the insertion + // and removal event methods above; change in sync. + static private void freeMutationEvent() + { + // clear fields to enable GC + mutationEvent.clear(); + eventDataLock = false; + } + + void setDepth(int depth) + { + this.depth = depth; + for (DomNode ctx = first; ctx != null; ctx = ctx.next) + { + ctx.setDepth(depth + 1); + } + } + + /** + * DOM L1 + * Appends the specified node to this node's list of children. + * Document subclasses must override this to enforce the restrictions + * that there be only one element and document type child. + * + *
Causes a DOMNodeInserted mutation event to be reported. + * Will first cause a DOMNodeRemoved event to be reported if the + * parameter already has a parent. If the new child is a document + * fragment node, both events will be reported for each child of + * the fragment; the order in which children are removed and + * inserted is implementation-specific. + * + *
If this DOM has been compiled without mutation event support, + * these events will not be reported. + */ + public Node appendChild(Node newChild) + { + try + { + DomNode child = (DomNode) newChild; + + if (child.nodeType == DOCUMENT_FRAGMENT_NODE) + { + // Append all nodes in the fragment to this node + for (DomNode ctx = child.first; ctx != null; ctx = ctx.next) + { + checkMisc(ctx); + } + for (DomNode ctx = child.first; ctx != null; ) + { + DomNode ctxNext = ctx.next; + appendChild(ctx); + ctx = ctxNext; + } + } + else + { + checkMisc(child); + if (child.parent != null) + { + child.parent.removeChild(child); + } + child.parent = this; + child.index = length++; + child.setDepth(depth + 1); + child.next = null; + if (last == null) + { + first = child; + child.previous = null; + } + else + { + last.next = child; + child.previous = last; + } + last = child; + + if (reportMutations) + { + insertionEvent(null, child); + } + } + + return child; + } + catch (ClassCastException e) + { + throw new DomDOMException(DOMException.WRONG_DOCUMENT_ERR, + null, newChild, 0); + } + } + + /** + * DOM L1 + * Inserts the specified node in this node's list of children. + * Document subclasses must override this to enforce the restrictions + * that there be only one element and document type child. + * + *
Causes a DOMNodeInserted mutation event to be reported. Will + * first cause a DOMNodeRemoved event to be reported if the newChild + * parameter already has a parent. If the new child is a document + * fragment node, both events will be reported for each child of + * the fragment; the order in which children are removed and inserted + * is implementation-specific. + * + *
If this DOM has been compiled without mutation event support, + * these events will not be reported. + */ + public Node insertBefore(Node newChild, Node refChild) + { + if (refChild == null) + { + return appendChild(newChild); + } + + try + { + DomNode child = (DomNode) newChild; + DomNode ref = (DomNode) refChild; + + if (child.nodeType == DOCUMENT_FRAGMENT_NODE) + { + // Append all nodes in the fragment to this node + for (DomNode ctx = child.first; ctx != null; ctx = ctx.next) + { + checkMisc(ctx); + } + for (DomNode ctx = child.first; ctx != null; ) + { + DomNode ctxNext = ctx.next; + insertBefore(ctx, ref); + ctx = ctxNext; + } + } + else + { + checkMisc(child); + if (ref == null || ref.parent != this) + { + throw new DomDOMException(DOMException.NOT_FOUND_ERR, + null, ref, 0); + } + if (ref == child) + { + throw new DomDOMException(DOMException.HIERARCHY_REQUEST_ERR, + "can't insert node before itself", + ref, 0); + } + + if (child.parent != null) + { + child.parent.removeChild(child); + } + child.parent = this; + int i = ref.index; + child.setDepth(depth + 1); + child.next = ref; + if (ref.previous != null) + { + ref.previous.next = child; + } + child.previous = ref.previous; + ref.previous = child; + if (first == ref) + { + first = child; + } + // index renumbering + for (DomNode ctx = child; ctx != null; ctx = ctx.next) + { + ctx.index = i++; + } + + if (reportMutations) + { + insertionEvent(null, child); + } + length++; + } + + return child; + } + catch (ClassCastException e) + { + throw new DomDOMException(DOMException.WRONG_DOCUMENT_ERR, + null, newChild, 0); + } + } + + /** + * DOM L1 + * Replaces the specified node in this node's list of children. + * Document subclasses must override this to test the restrictions + * that there be only one element and document type child. + * + *
Causes DOMNodeRemoved and DOMNodeInserted mutation event to be + * reported. Will cause another DOMNodeRemoved event to be reported if + * the newChild parameter already has a parent. These events may be + * delivered in any order, except that the event reporting removal + * from such an existing parent will always be delivered before the + * event reporting its re-insertion as a child of some other node. + * The order in which children are removed and inserted is implementation + * specific. + * + *
If your application needs to depend on the in which those removal + * and insertion events are delivered, don't use this API. Instead, + * invoke the removeChild and insertBefore methods directly, to guarantee + * a specific delivery order. Similarly, don't use document fragments, + * Otherwise your application code may not work on a DOM which implements + * this method differently. + * + *
If this DOM has been compiled without mutation event support, + * these events will not be reported. + */ + public Node replaceChild(Node newChild, Node refChild) + { + try + { + DomNode child = (DomNode) newChild; + DomNode ref = (DomNode) refChild; + + DomEvent.DomMutationEvent event = getMutationEvent(); + boolean doFree = (event != null); + + if (child.nodeType == DOCUMENT_FRAGMENT_NODE) + { + // Append all nodes in the fragment to this node + for (DomNode ctx = child.first; ctx != null; ctx = ctx.next) + { + checkMisc(ctx); + } + if (ref == null || ref.parent != this) + { + throw new DomDOMException(DOMException.NOT_FOUND_ERR, + null, ref, 0); + } + + if (reportMutations) + { + removalEvent(event, ref); + } + length--; + length += child.length; + + if (child.length == 0) + { + // Removal + if (ref.previous != null) + { + ref.previous.next = ref.next; + } + if (ref.next != null) + { + ref.next.previous = ref.previous; + } + if (first == ref) + { + first = ref.next; + } + if (last == ref) + { + last = ref.previous; + } + } + else + { + int i = ref.index; + for (DomNode ctx = child.first; ctx != null; ctx = ctx.next) + { + // Insertion + ctx.parent = this; + ctx.index = i++; + ctx.setDepth(ref.depth); + if (ctx == child.first) + { + ctx.previous = ref.previous; + } + if (ctx == child.last) + { + ctx.next = ref.next; + } + } + if (first == ref) + { + first = child.first; + } + if (last == ref) + { + last = child.last; + } + } + } + else + { + checkMisc(child); + if (ref == null || ref.parent != this) + { + throw new DomDOMException(DOMException.NOT_FOUND_ERR, + null, ref, 0); + } + + if (reportMutations) + { + removalEvent(event, ref); + } + + if (child.parent != null) + { + child.parent.removeChild(child); + } + child.parent = this; + child.index = ref.index; + child.setDepth(ref.depth); + if (ref.previous != null) + { + ref.previous.next = child; + } + child.previous = ref.previous; + if (ref.next != null) + { + ref.next.previous = child; + } + child.next = ref.next; + if (first == ref) + { + first = child; + } + if (last == ref) + { + last = child; + } + + if (reportMutations) + { + insertionEvent(event, child); + } + if (doFree) + { + freeMutationEvent(); + } + } + ref.parent = null; + ref.index = 0; + ref.setDepth(0); + ref.previous = null; + ref.next = null; + + return ref; + } + catch (ClassCastException e) + { + throw new DomDOMException(DOMException.WRONG_DOCUMENT_ERR, + null, newChild, 0); + } + } + + /** + * DOM L1 + * Removes the specified child from this node's list of children, + * or else reports an exception. + * + *
Causes a DOMNodeRemoved mutation event to be reported. + * + *
If this DOM has been compiled without mutation event support, + * these events will not be reported. + */ + public Node removeChild(Node refChild) + { + try + { + DomNode ref = (DomNode) refChild; + + if (ref == null || ref.parent != this) + { + throw new DomDOMException(DOMException.NOT_FOUND_ERR, + null, ref, 0); + } + if (readonly && !owner.building) + { + throw new DomDOMException(DOMException.NO_MODIFICATION_ALLOWED_ERR, + null, this, 0); + } + + for (DomNode child = first; child != null; child = child.next) + { + if (child == ref) + { + if (reportMutations) + { + removalEvent(null, child); + } + + length--; + if (ref.previous != null) + { + ref.previous.next = ref.next; + } + if (ref.next != null) + { + ref.next.previous = ref.previous; + } + if (first == ref) + { + first = ref.next; + } + if (last == ref) + { + last = ref.previous; + } + // renumber indices + int i = 0; + for (DomNode ctx = first; ctx != null; ctx = ctx.next) + { + ctx.index = i++; + } + ref.parent = null; + ref.setDepth(0); + ref.index = 0; + ref.previous = null; + ref.next = null; + + return ref; + } + } + throw new DomDOMException(DOMException.NOT_FOUND_ERR, + "that's no child of mine", refChild, 0); + } + catch (ClassCastException e) + { + throw new DomDOMException(DOMException.WRONG_DOCUMENT_ERR, + null, refChild, 0); + } + } + + /** + * DOM L1 (NodeList) + * Returns the item with the specified index in this NodeList, + * else null. + */ + public Node item(int index) + { + DomNode child = first; + int count = 0; + while (child != null && count < index) + { + child = child.next; + count++; + } + return child; + } + + /** + * DOM L1 (NodeList) + * Returns the number of elements in this NodeList. + * (Note that many interfaces have a "Length" property, not just + * NodeList, and if a node subtype must implement one of those, + * it will also need to override getChildNodes.) + */ + public int getLength() + { + return length; + } + + /** + * Minimize extra space consumed by this node to hold children and event + * listeners. + */ + public void trimToSize() + { + } + + /** + * DOM L1 + * Returns the previous sibling, if one is known. + */ + public Node getNextSibling() + { + return next; + } + + /** + * DOM L1 + * Returns the previous sibling, if one is known. + */ + public Node getPreviousSibling() + { + return previous; + } + + /** + * DOM L1 + * Returns the parent node, if one is known. + */ + public Node getParentNode() + { + return parent; + } + + /** + * DOM L2 + * Consults the DOM implementation to determine if the requested + * feature is supported. DocumentType subclasses must override + * this method, and associate themselves directly with the + * DOMImplementation node used. (This method relies on being able + * to access the DOMImplementation from the owner document, but + * DocumentType nodes can be created without an owner.) + */ + public boolean isSupported(String feature, String version) + { + Document doc = owner; + DOMImplementation impl = null; + + if (doc == null && nodeType == DOCUMENT_NODE) + { + doc = (Document) this; + } + + if (doc == null) + { + // possible for DocumentType + throw new IllegalStateException ("unbound ownerDocument"); + } + + impl = doc.getImplementation(); + return impl.hasFeature(feature, version); + } + + /** + * DOM L1 (modified in L2) + * Returns the owner document. This is only null for Document nodes, + * and (new in L2) for DocumentType nodes which have not yet been + * associated with the rest of their document. + */ + final public Document getOwnerDocument() + { + return owner; + } + + /** + * DOM L1 + * Does nothing; this must be overridden (along with the + * getNodeValue method) for nodes with a non-null defined value. + */ + public void setNodeValue(String value) + { + } + + /** + * DOM L1 + * Returns null; this must be overridden for nodes types with + * a defined value, along with the setNodeValue method. + */ + public String getNodeValue() + { + return null; + } + + /** This forces GCJ compatibility. + * Without this method GCJ is unable to compile to byte code. + */ + public final short getNodeType() + { + return nodeType; + } + + /** This forces GCJ compatibility. + * Without this method GCJ seems unable to natively compile GNUJAXP. + */ + public abstract String getNodeName(); + + /** + * DOM L2 + * Does nothing; this must be overridden (along with the + * getPrefix method) for element and attribute nodes. + */ + public void setPrefix(String prefix) + { + } + + /** + * DOM L2 + * Returns null; this must be overridden for element and + * attribute nodes. + */ + public String getPrefix() + { + return null; + } + + /** + * DOM L2 + * Returns null; this must be overridden for element and + * attribute nodes. + */ + public String getNamespaceURI() + { + return null; + } + + /** + * DOM L2 + * Returns the node name; this must be overridden for element and + * attribute nodes. + */ + public String getLocalName() + { + return null; + } + + /** + * DOM L1 + * Returns a clone of this node which optionally includes cloned + * versions of child nodes. Clones are always mutable, except for + * entity reference nodes. + */ + public Node cloneNode(boolean deep) + { + if (deep) + { + return cloneNodeDeepInternal(true, null); + } + + DomNode node = (DomNode) clone(); + if (nodeType == ENTITY_REFERENCE_NODE) + { + node.makeReadonly(); + } + notifyUserDataHandlers(UserDataHandler.NODE_CLONED, this, node); + return node; + } + + /** + * Returns a deep clone of this node. + */ + private DomNode cloneNodeDeepInternal(boolean root, DomDocument doc) + { + DomNode node = (DomNode) clone(); + boolean building = false; // Never used unless root is true + if (root) + { + doc = (nodeType == DOCUMENT_NODE) ? (DomDocument) node : node.owner; + building = doc.building; + doc.building = true; // Permit certain structural rules + } + node.owner = doc; + for (DomNode ctx = first; ctx != null; ctx = ctx.next) + { + DomNode newChild = ctx.cloneNodeDeepInternal(false, doc); + node.appendChild(newChild); + } + if (nodeType == ENTITY_REFERENCE_NODE) + { + node.makeReadonly(); + } + if (root) + { + doc.building = building; + } + notifyUserDataHandlers(UserDataHandler.NODE_CLONED, this, node); + return node; + } + + void notifyUserDataHandlers(short op, Node src, Node dst) + { + if (userDataHandlers != null) + { + for (Iterator i = userDataHandlers.entrySet().iterator(); i.hasNext(); ) + { + Map.Entry entry = (Map.Entry) i.next(); + String key = (String) entry.getKey(); + UserDataHandler handler = (UserDataHandler) entry.getValue(); + Object data = userData.get(key); + handler.handle(op, key, data, src, dst); + } + } + } + + /** + * Clones this node; roughly equivalent to cloneNode(false). + * Element subclasses must provide a new implementation which + * invokes this method to handle the basics, and then arranges + * to clone any element attributes directly. Attribute subclasses + * must make similar arrangements, ensuring that existing ties to + * elements are broken by cloning. + */ + public Object clone() + { + try + { + DomNode node = (DomNode) super.clone(); + + node.parent = null; + node.depth = 0; + node.index = 0; + node.length = 0; + node.first = null; + node.last = null; + node.previous = null; + node.next = null; + + node.readonly = false; + node.listeners = new HashSet(); + node.nListeners = 0; + return node; + + } + catch (CloneNotSupportedException x) + { + throw new Error("clone didn't work"); + } + } + + // the elements-by-tagname stuff is needed for both + // elements and documents ... this is in lieu of a + // common base class between Node and NodeNS. + + /** + * DOM L1 + * Creates a NodeList giving array-style access to elements with + * the specified name. Access is fastest if indices change by + * small values, and the DOM is not modified. + */ + public NodeList getElementsByTagName(String tag) + { + return new ShadowList(null, tag); + } + + /** + * DOM L2 + * Creates a NodeList giving array-style access to elements with + * the specified namespace and local name. Access is fastest if + * indices change by small values, and the DOM is not modified. + */ + public NodeList getElementsByTagNameNS(String namespace, String local) + { + return new ShadowList(namespace, local); + } + + + // + // This shadow class is GC-able even when the live list it shadows + // can't be, because of event registration hookups. Its finalizer + // makes that live list become GC-able. + // + final class ShadowList + implements NodeList + { + + private LiveNodeList liveList; + + ShadowList(String ns, String local) + { + liveList = new LiveNodeList(ns, local); + } + + public void finalize() + { + liveList.detach(); + liveList = null; + } + + public Node item(int index) + { + return liveList.item(index); + } + + public int getLength() + { + return liveList.getLength(); + } + } + + final class LiveNodeList + implements NodeList, EventListener, NodeFilter + { + + private final boolean matchAnyURI; + private final boolean matchAnyName; + private final String elementURI; + private final String elementName; + + private DomIterator current; + private int lastIndex; + + LiveNodeList(String uri, String name) + { + elementURI = uri; + elementName = name; + matchAnyURI = "*".equals(uri); + matchAnyName = "*".equals(name); + + DomNode.this.addEventListener("DOMNodeInserted", this, true); + DomNode.this.addEventListener("DOMNodeRemoved", this, true); + } + + void detach() + { + if (current != null) + current.detach(); + current = null; + + DomNode.this.removeEventListener("DOMNodeInserted", this, true); + DomNode.this.removeEventListener("DOMNodeRemoved", this, true); + } + + public short acceptNode(Node element) + { + if (element == DomNode.this) + { + return FILTER_SKIP; + } + + // use namespace-aware matching ... + if (elementURI != null) + { + if (!(matchAnyURI + || elementURI.equals(element.getNamespaceURI()))) + { + return FILTER_SKIP; + } + if (!(matchAnyName + || elementName.equals(element.getLocalName()))) + { + return FILTER_SKIP; + } + + // ... or qName-based kind. + } + else + { + if (!(matchAnyName + || elementName.equals(element.getNodeName()))) + { + return FILTER_SKIP; + } + } + return FILTER_ACCEPT; + } + + private DomIterator createIterator() + { + return new DomIterator(DomNode.this, + NodeFilter.SHOW_ELEMENT, + this, /* filter */ + true /* expand entity refs */ + ); + } + + public void handleEvent(Event e) + { + MutationEvent mutation = (MutationEvent) e; + Node related = mutation.getRelatedNode(); + + // XXX if it's got children ... check all kids too, they + // will invalidate our saved index + + if (related.getNodeType() != Node.ELEMENT_NODE || + related.getNodeName() != elementName || + related.getNamespaceURI() != elementURI) + { + return; + } + + if (current != null) + current.detach(); + current = null; + } + + public Node item(int index) + { + if (current == null) + { + current = createIterator(); + lastIndex = -1; + } + + // last node or before? go backwards + if (index <= lastIndex) { + while (index != lastIndex) { + current.previousNode (); + lastIndex--; + } + Node ret = current.previousNode (); + current.detach(); + current = null; + return ret; + } + + // somewhere after last node + while (++lastIndex != index) + current.nextNode (); + + Node ret = current.nextNode (); + current.detach(); + current = null; + return ret; + } + + public int getLength() + { + int retval = 0; + NodeIterator iter = createIterator(); + + while (iter.nextNode() != null) + { + retval++; + } + iter.detach(); + return retval; + } + + } + + // + // EventTarget support + // + static final class ListenerRecord + { + + String type; + EventListener listener; + boolean useCapture; + + // XXX use JDK 1.2 java.lang.ref.WeakReference to listener, + // and we can both get rid of "shadow" classes and remove + // the need for applications to apply similar trix ... but + // JDK 1.2 support isn't generally available yet + + ListenerRecord(String type, EventListener listener, boolean useCapture) + { + this.type = type.intern(); + this.listener = listener; + this.useCapture = useCapture; + } + + public boolean equals(Object o) + { + ListenerRecord rec = (ListenerRecord)o; + return listener == rec.listener + && useCapture == rec.useCapture + && type == rec.type; + } + + public int hashCode() + { + return listener.hashCode() ^ type.hashCode(); + } + } + + /** + * DOM L2 (Events) + * Returns an instance of the specified type of event object. + * Understands about DOM Mutation, HTML, and UI events. + * + *
If the name of the event type begins with "USER-", then an object + * implementing the "Event" class will be returned; this provides a + * limited facility for application-defined events to use the DOM event + * infrastructure. Alternatively, use one of the standard DOM event + * classes and initialize it using use such a "USER-" event type name; + * or defin, instantiate, and initialize an application-specific subclass + * of DomEvent and pass that to dispatchEvent(). + * + * @param eventType Identifies the particular DOM feature module + * defining the type of event, such as "MutationEvents". + * The event "name" is a different kind of "type". + */ + public Event createEvent(String eventType) + { + eventType = eventType.toLowerCase(); + + if ("mutationevents".equals(eventType)) + { + return new DomEvent.DomMutationEvent(null); + } + + if ("htmlevents".equals(eventType) + || "events".equals(eventType) + || "user-events".equals(eventType)) + { + return new DomEvent(null); + } + + if ("uievents".equals(eventType)) + { + return new DomEvent.DomUIEvent(null); + } + + // mouse events + + throw new DomDOMException(DOMException.NOT_SUPPORTED_ERR, + eventType, null, 0); + } + + /** + * DOM L2 (Events) + * Registers an event listener's interest in a class of events. + */ + public final void addEventListener(String type, + EventListener listener, + boolean useCapture) + { + // prune duplicates + ListenerRecord record; + + record = new ListenerRecord(type, listener, useCapture); + listeners.add(record); + nListeners = listeners.size(); + } + + // XXX this exception should be discarded from DOM + + // this class can be instantiated, unlike the one in the spec + static final class DomEventException + extends EventException + { + + DomEventException() + { + super(UNSPECIFIED_EVENT_TYPE_ERR, "unspecified event type"); + } + + } + + /** + * DOM L2 (Events) + * Delivers an event to all relevant listeners, returning true if the + * caller should perform their default action. Note that the event + * must have been provided by the createEvent() method on this + * class, else it can't be dispatched. + * + * @see #createEvent + * + * @exception NullPointerException When a null event is passed. + * @exception ClassCastException When the event wasn't provided by + * the createEvent method, or otherwise isn't a DomEvent. + * @exception EventException If the event type wasn't specified + */ + public final boolean dispatchEvent(Event event) + throws EventException + { + DomEvent e = (DomEvent) event; + DomNode[] ancestors = null; + int ancestorMax = 0; + boolean haveDispatchDataLock = false; + + if (e.type == null) + { + throw new DomEventException(); + } + + e.doDefault = true; + e.target = this; + + // + // Typical case: one nonrecursive dispatchEvent call at a time + // for this class. If that's our case, we can avoid allocating + // garbage, which is overall a big win. Even with advanced GCs + // that deal well with short-lived garbage, and wayfast allocators, + // it still helps. + // + // Remember -- EVERY mutation goes though here at least once. + // + // When populating a DOM tree, trying to send mutation events is + // the primary cost; this dominates the critical path. + // + try + { + DomNode current; + int index; + boolean haveAncestorRegistrations = false; + ListenerRecord[] notificationSet; + int ancestorLen; + + synchronized (lockNode) + { + if (!dispatchDataLock) + { + haveDispatchDataLock = dispatchDataLock = true; + notificationSet = DomNode.notificationSet; + ancestors = DomNode.ancestors; + } + else + { + notificationSet = new ListenerRecord[NOTIFICATIONS_INIT]; + ancestors = new DomNode[ANCESTORS_INIT]; + } + ancestorLen = ancestors.length; + } + + // Climb to the top of this subtree and handle capture, letting + // each node (from the top down) capture until one stops it or + // until we get to this one. + current = (parent == null) ? this : parent; + if (current.depth >= ANCESTORS_INIT) + { + DomNode[] newants = new DomNode[current.depth + 1]; + System.arraycopy(ancestors, 0, newants, 0, ancestors.length); + ancestors = newants; + ancestorLen = ancestors.length; + } + for (index = 0; index < ancestorLen; index++) + { + if (current == null || current.depth == 0) + break; + + if (current.nListeners != 0) + { + haveAncestorRegistrations = true; + } + ancestors [index] = current; + current = current.parent; + } + if (current.depth > 0) + { + throw new RuntimeException("dispatchEvent capture stack size"); + } + + ancestorMax = index; + e.stop = false; + + if (haveAncestorRegistrations) + { + e.eventPhase = Event.CAPTURING_PHASE; + while (!e.stop && index-- > 0) + { + current = ancestors [index]; + if (current.nListeners != 0) + { + notifyNode(e, current, true, notificationSet); + } + } + } + + // Always deliver events to the target node (this) + // unless stopPropagation was called. If we saw + // no registrations yet (typical!), we never will. + if (!e.stop && nListeners != 0) + { + e.eventPhase = Event.AT_TARGET; + notifyNode (e, this, false, notificationSet); + } + else if (!haveAncestorRegistrations) + { + e.stop = true; + } + + // If the event bubbles and propagation wasn't halted, + // walk back up the ancestor list. Stop bubbling when + // any bubbled event handler stops it. + + if (!e.stop && e.bubbles) + { + e.eventPhase = Event.BUBBLING_PHASE; + for (index = 0; + !e.stop + && index < ancestorMax + && (current = ancestors[index]) != null; + index++) + { + if (current.nListeners != 0) + { + notifyNode(e, current, false, notificationSet); + } + } + } + e.eventPhase = 0; + + // Caller chooses whether to perform the default + // action based on return from this method. + return e.doDefault; + + } + finally + { + if (haveDispatchDataLock) + { + // synchronize to force write ordering + synchronized (lockNode) + { + // null out refs to ensure they'll be GC'd + for (int i = 0; i < ancestorMax; i++) + { + ancestors [i] = null; + } + // notificationSet handled by notifyNode + + dispatchDataLock = false; + } + } + } + } + + private void notifyNode(DomEvent e, + DomNode current, + boolean capture, + ListenerRecord[] notificationSet) + { + int count = 0; + Iterator iter; + + iter = current.listeners.iterator(); + + // do any of this set of listeners get notified? + while (iter.hasNext()) + { + ListenerRecord rec = (ListenerRecord)iter.next(); + + if (rec.useCapture != capture) + { + continue; + } + if (!e.type.equals (rec.type)) + { + continue; + } + if (count >= notificationSet.length) + { + // 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; + } + notificationSet[count++] = rec; + } + iter = null; + + // Notify just those listeners + e.currentNode = current; + for (int i = 0; i < count; i++) + { + try + { + iter = current.listeners.iterator(); + // Late in the DOM CR process (3rd or 4th CR?) the + // removeEventListener spec became asymmetric with respect + // to addEventListener ... effect is now immediate. + while (iter.hasNext()) + { + ListenerRecord rec = (ListenerRecord)iter.next(); + + if (rec.equals(notificationSet[i])) + { + notificationSet[i].listener.handleEvent(e); + break; + } + } + iter = null; + } + catch (Exception x) + { + // ignore all exceptions + } + notificationSet[i] = null; // free for GC + } + } + + /** + * DOM L2 (Events) + * Unregisters an event listener. + */ + public final void removeEventListener(String type, + EventListener listener, + boolean useCapture) + { + listeners.remove(new ListenerRecord(type, listener, useCapture)); + nListeners = listeners.size(); + // no exceptions reported + } + + /** + * DOM L1 (relocated in DOM L2) + * In this node and all contained nodes (including attributes if + * relevant) merge adjacent text nodes. This is done while ignoring + * text which happens to use CDATA delimiters). + */ + public final void normalize() + { + // Suspend readonly status + boolean saved = readonly; + readonly = false; + for (DomNode ctx = first; ctx != null; ctx = ctx.next) + { + boolean saved2 = ctx.readonly; + ctx.readonly = false; + switch (ctx.nodeType) + { + case TEXT_NODE: + case CDATA_SECTION_NODE: + while (ctx.next != null && + (ctx.next.nodeType == TEXT_NODE || + ctx.next.nodeType == CDATA_SECTION_NODE)) + { + Text text = (Text) ctx; + text.appendData(ctx.next.getNodeValue()); + removeChild(ctx.next); + } + break; + case ELEMENT_NODE: + NamedNodeMap attrs = ctx.getAttributes(); + int len = attrs.getLength(); + for (int i = 0; i < len; i++) + { + DomNode attr = (DomNode) attrs.item(i); + boolean saved3 = attr.readonly; + attr.readonly = false; + attr.normalize(); + attr.readonly = saved3; + } + // Fall through + case DOCUMENT_NODE: + case DOCUMENT_FRAGMENT_NODE: + case ATTRIBUTE_NODE: + case ENTITY_REFERENCE_NODE: + ctx.normalize(); + break; + } + ctx.readonly = saved2; + } + readonly = saved; + } + + /** + * Returns true iff node types match, and either (a) both nodes have no + * namespace and their getNodeName() values are the same, or (b) both + * nodes have the same getNamespaceURI() and same getLocalName() values. + * + *
Note that notion of a "Per-Element-Type" attribute name scope, as + * found in a non-normative appendix of the XML Namespaces specification, + * is not supported here. Your application must implement that notion, + * typically by not bothering to check nameAndTypeEquals for attributes + * without namespace URIs unless you already know their elements are + * nameAndTypeEquals. + */ + public boolean nameAndTypeEquals(Node other) + { + if (other == this) + { + return true; + } + // node types must match + if (nodeType != other.getNodeType()) + { + return false; + } + + // if both have namespaces, do a "full" comparision + // this is a "global" partition + String ns1 = this.getNamespaceURI(); + String ns2 = other.getNamespaceURI(); + + if (ns1 != null && ns2 != null) + { + return ns1.equals(ns2) && + equal(getLocalName(), other.getLocalName()); + } + + // if neither has a namespace, this is a "no-namespace" name. + if (ns1 == null && ns2 == null) + { + if (!getNodeName().equals(other.getNodeName())) + { + return false; + } + // can test the non-normative "per-element-type" scope here. + // if this is an attribute node and both nodes have been bound + // to elements (!!), then return the nameAndTypeEquals() + // comparison of those elements. + return true; + } + + // otherwise they're unequal: one scoped, one not. + return false; + } + + // DOM Level 3 methods + + public String getBaseURI() + { + return (parent != null) ? parent.getBaseURI() : null; + } + + public short compareDocumentPosition(Node other) + throws DOMException + { + return (short) compareTo(other); + } + + /** + * DOM nodes have a natural ordering: document order. + */ + public final int compareTo(Object other) + { + if (other instanceof DomNode) + { + DomNode n1 = this; + DomNode n2 = (DomNode) other; + if (n1.owner != n2.owner) + { + return 0; + } + int d1 = n1.depth, d2 = n2.depth; + int delta = d1 - d2; + while (d1 > d2) + { + n1 = n1.parent; + d1--; + } + while (d2 > d1) + { + n2 = n2.parent; + d2--; + } + int c = compareTo2(n1, n2); + return (c != 0) ? c : delta; + } + return 0; + } + + /** + * Compare two nodes at the same depth. + */ + final int compareTo2(DomNode n1, DomNode n2) + { + if (n1 == n2 || n1.depth == 0 || n2.depth == 0) + { + return 0; + } + int c = compareTo2(n1.parent, n2.parent); + return (c != 0) ? c : n1.index - n2.index; + } + + public final String getTextContent() + throws DOMException + { + return getTextContent(true); + } + + final String getTextContent(boolean topLevel) + throws DOMException + { + switch (nodeType) + { + case ELEMENT_NODE: + case ENTITY_NODE: + case ENTITY_REFERENCE_NODE: + case DOCUMENT_FRAGMENT_NODE: + CPStringBuilder buffer = new CPStringBuilder(); + for (DomNode ctx = first; ctx != null; ctx = ctx.next) + { + String textContent = ctx.getTextContent(false); + if (textContent != null) + { + buffer.append(textContent); + } + } + return buffer.toString(); + case TEXT_NODE: + case CDATA_SECTION_NODE: + if (((Text) this).isElementContentWhitespace()) + { + return ""; + } + return getNodeValue(); + case ATTRIBUTE_NODE: + return getNodeValue(); + case COMMENT_NODE: + case PROCESSING_INSTRUCTION_NODE: + return topLevel ? getNodeValue() : ""; + default: + return null; + } + } + + public void setTextContent(String textContent) + throws DOMException + { + switch (nodeType) + { + case ELEMENT_NODE: + case ATTRIBUTE_NODE: + case ENTITY_NODE: + case ENTITY_REFERENCE_NODE: + case DOCUMENT_FRAGMENT_NODE: + for (DomNode ctx = first; ctx != null; ) + { + DomNode n = ctx.next; + removeChild(ctx); + ctx = n; + } + if (textContent != null) + { + Text text = owner.createTextNode(textContent); + appendChild(text); + } + break; + case TEXT_NODE: + case CDATA_SECTION_NODE: + case COMMENT_NODE: + case PROCESSING_INSTRUCTION_NODE: + setNodeValue(textContent); + break; + } + } + + public boolean isSameNode(Node other) + { + return this == other; + } + + public String lookupPrefix(String namespaceURI) + { + return (parent == null || parent == owner) ? null : + parent.lookupPrefix(namespaceURI); + } + + public boolean isDefaultNamespace(String namespaceURI) + { + return (parent == null || parent == owner) ? false : + parent.isDefaultNamespace(namespaceURI); + } + + public String lookupNamespaceURI(String prefix) + { + return (parent == null || parent == owner) ? null : + parent.lookupNamespaceURI(prefix); + } + + public boolean isEqualNode(Node arg) + { + if (this == arg) + return true; + if (arg == null) + return false; + if (nodeType != arg.getNodeType()) + return false; + switch (nodeType) + { + case ELEMENT_NODE: + case ATTRIBUTE_NODE: + if (!equal(getLocalName(), arg.getLocalName()) || + !equal(getNamespaceURI(), arg.getNamespaceURI())) + return false; + break; + case PROCESSING_INSTRUCTION_NODE: + if (!equal(getNodeName(), arg.getNodeName()) || + !equal(getNodeValue(), arg.getNodeValue())) + return false; + break; + case COMMENT_NODE: + case TEXT_NODE: + case CDATA_SECTION_NODE: + if (!equal(getNodeValue(), arg.getNodeValue())) + return false; + break; + } + // Children + Node argCtx = arg.getFirstChild(); + getFirstChild(); // because of DomAttr lazy children + DomNode ctx = first; + for (; ctx != null && argCtx != null; ctx = ctx.next) + { + if (nodeType == DOCUMENT_NODE) + { + // Ignore whitespace outside document element + while (ctx != null && ctx.nodeType == TEXT_NODE) + ctx = ctx.next; + while (argCtx != null && ctx.getNodeType() == TEXT_NODE) + argCtx = argCtx.getNextSibling(); + if (ctx == null && argCtx != null) + return false; + else if (argCtx == null && ctx != null) + return false; + } + if (!ctx.isEqualNode(argCtx)) + return false; + argCtx = argCtx.getNextSibling(); + } + if (ctx != null || argCtx != null) + return false; + + // TODO DocumentType + return true; + } + + boolean equal(String arg1, String arg2) + { + return ((arg1 == null && arg2 == null) || + (arg1 != null && arg1.equals(arg2))); + } + + public Object getFeature(String feature, String version) + { + DOMImplementation impl = (nodeType == DOCUMENT_NODE) ? + ((Document) this).getImplementation() : owner.getImplementation(); + if (impl.hasFeature(feature, version)) + { + return this; + } + return null; + } + + public Object setUserData(String key, Object data, UserDataHandler handler) + { + if (userData == null) + { + userData = new HashMap(); + } + if (handler != null) + { + if (userDataHandlers == null) + { + userDataHandlers = new HashMap(); + } + userDataHandlers.put(key, handler); + } + return userData.put(key, data); + } + + public Object getUserData(String key) + { + if (userData == null) + { + return null; + } + return userData.get(key); + } + + public String toString() + { + String nodeName = getNodeName(); + String nodeValue = getNodeValue(); + CPStringBuilder buf = new CPStringBuilder(getClass().getName()); + buf.append('['); + if (nodeName != null) + { + buf.append(nodeName); + } + if (nodeValue != null) + { + if (nodeName != null) + { + buf.append('='); + } + buf.append('\''); + buf.append(encode(nodeValue)); + buf.append('\''); + } + buf.append(']'); + return buf.toString(); + } + + String encode(String value) + { + CPStringBuilder buf = null; + int len = value.length(); + for (int i = 0; i < len; i++) + { + char c = value.charAt(i); + if (c == '\n') + { + if (buf == null) + { + buf = new CPStringBuilder(value.substring(0, i)); + } + buf.append("\\n"); + } + else if (c == '\r') + { + if (buf == null) + { + buf = new CPStringBuilder(value.substring(0, i)); + } + buf.append("\\r"); + } + else if (buf != null) + { + buf.append(c); + } + } + return (buf != null) ? buf.toString() : value; + } + + String nodeTypeToString(short nodeType) + { + switch (nodeType) + { + case ELEMENT_NODE: + return "ELEMENT_NODE"; + case ATTRIBUTE_NODE: + return "ATTRIBUTE_NODE"; + case TEXT_NODE: + return "TEXT_NODE"; + case CDATA_SECTION_NODE: + return "CDATA_SECTION_NODE"; + case DOCUMENT_NODE: + return "DOCUMENT_NODE"; + case DOCUMENT_TYPE_NODE: + return "DOCUMENT_TYPE_NODE"; + case COMMENT_NODE: + return "COMMENT_NODE"; + case PROCESSING_INSTRUCTION_NODE: + return "PROCESSING_INSTRUCTION_NODE"; + case DOCUMENT_FRAGMENT_NODE: + return "DOCUMENT_FRAGMENT_NODE"; + case ENTITY_NODE: + return "ENTITY_NODE"; + case ENTITY_REFERENCE_NODE: + return "ENTITY_REFERENCE_NODE"; + case NOTATION_NODE: + return "NOTATION_NODE"; + default: + return "UNKNOWN"; + } + } + + public void list(java.io.PrintStream out, int indent) + { + for (int i = 0; i < indent; i++) + out.print(" "); + out.println(toString()); + for (DomNode ctx = first; ctx != null; ctx = ctx.next) + ctx.list(out, indent + 1); + } + +} diff --git a/libjava/classpath/gnu/xml/dom/DomNodeIterator.java b/libjava/classpath/gnu/xml/dom/DomNodeIterator.java new file mode 100644 index 000000000..9f9ec7e39 --- /dev/null +++ b/libjava/classpath/gnu/xml/dom/DomNodeIterator.java @@ -0,0 +1,328 @@ +/* DomNodeIterator.java -- + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ +package gnu.xml.dom; + +import org.w3c.dom.DOMException; +import org.w3c.dom.Node; +import org.w3c.dom.traversal.NodeFilter; +import org.w3c.dom.traversal.NodeIterator; +import org.w3c.dom.traversal.TreeWalker; + +/** + * Node iterator and tree walker. + * + * @author Chris Burdess + */ +public class DomNodeIterator + implements NodeIterator, TreeWalker +{ + + Node root; + final int whatToShow; + final NodeFilter filter; + final boolean entityReferenceExpansion; + final boolean walk; + Node current; + + public DomNodeIterator(Node root, int whatToShow, NodeFilter filter, + boolean entityReferenceExpansion, boolean walk) + { + if (root == null) + { + throw new DOMException(DOMException.NOT_SUPPORTED_ERR, "null root"); + } + this.root = root; + this.whatToShow = whatToShow; + this.filter = filter; + this.entityReferenceExpansion = entityReferenceExpansion; + this.walk = walk; + current = root; + } + + public Node getRoot() + { + return root; + } + + public int getWhatToShow() + { + return whatToShow; + } + + public NodeFilter getFilter() + { + return filter; + } + + public boolean getExpandEntityReferences() + { + return entityReferenceExpansion; + } + + public Node nextNode() + throws DOMException + { + if (root == null) + { + throw new DOMException(DOMException.INVALID_STATE_ERR, "null root"); + } + Node ret; + do + { + if (current.equals(root)) + { + ret = root.getFirstChild(); + } + else if (walk) + { + ret = current.getFirstChild(); + if (ret == null) + { + ret = current.getNextSibling(); + } + if (ret == null) + { + Node tmp = current; + ret = tmp.getParentNode(); + while (!ret.equals(root) && tmp.equals(ret.getLastChild())) + { + tmp = ret; + ret = tmp.getParentNode(); + } + if (ret.equals(root)) + { + ret = null; + } + else + { + ret = ret.getNextSibling(); + } + } + } + else + { + ret = current.getNextSibling(); + } + current = (ret == null) ? current : ret; + } + while (!accept(ret)); + + return ret; + } + + public Node previousNode() + throws DOMException + { + if (root == null) + { + throw new DOMException(DOMException.INVALID_STATE_ERR, "null root"); + } + Node ret; + do + { + if (current.equals(root)) + { + ret = current.getLastChild(); + } + else if (walk) + { + ret = current.getLastChild(); + if (ret == null) + { + ret = current.getPreviousSibling(); + } + if (ret == null) + { + Node tmp = current; + ret = tmp.getParentNode(); + while (!ret.equals(root) && tmp.equals(ret.getFirstChild())) + { + tmp = ret; + ret = tmp.getParentNode(); + } + if (ret.equals(root)) + { + ret = null; + } + else + { + ret = ret.getPreviousSibling(); + } + } + } + else + { + ret = current.getPreviousSibling(); + } + } + while (!accept(ret)); + current = (ret == null) ? current : ret; + return ret; + } + + public Node getCurrentNode() + { + return current; + } + + public void setCurrentNode(Node current) + throws DOMException + { + if (current == null) + { + throw new DOMException(DOMException.NOT_SUPPORTED_ERR, "null root"); + } + this.current = current; + } + + public Node parentNode() + { + Node ret = current.getParentNode(); + if (!accept (ret)) + { + ret = null; + } + current = (ret == null) ? current : ret; + return ret; + } + + public Node firstChild () + { + Node ret = current.getFirstChild(); + while (!accept(ret)) + { + ret = ret.getNextSibling(); + } + current = (ret == null) ? current : ret; + return ret; + } + + public Node lastChild() + { + Node ret = current.getLastChild(); + while (!accept(ret)) + { + ret = ret.getPreviousSibling(); + } + current = (ret == null) ? current : ret; + return ret; + } + + public Node previousSibling() + { + Node ret = current.getPreviousSibling(); + while (!accept(ret)) + { + ret = ret.getPreviousSibling(); + } + current = (ret == null) ? current : ret; + return ret; + } + + public Node nextSibling() + { + Node ret = current.getNextSibling(); + while (!accept(ret)) + { + ret = ret.getNextSibling(); + } + current = (ret == null) ? current : ret; + return ret; + } + + public void detach() + { + root = null; + } + + boolean accept(Node node) + { + if (node == null) + { + return true; + } + boolean ret; + switch (node.getNodeType()) + { + case Node.ATTRIBUTE_NODE: + ret = (whatToShow & NodeFilter.SHOW_ATTRIBUTE) != 0; + break; + case Node.CDATA_SECTION_NODE: + ret = (whatToShow & NodeFilter.SHOW_CDATA_SECTION) != 0; + break; + case Node.COMMENT_NODE: + ret = (whatToShow & NodeFilter.SHOW_COMMENT) != 0; + break; + case Node.DOCUMENT_NODE: + ret = (whatToShow & NodeFilter.SHOW_DOCUMENT) != 0; + break; + case Node.DOCUMENT_FRAGMENT_NODE: + ret = (whatToShow & NodeFilter.SHOW_DOCUMENT_FRAGMENT) != 0; + break; + case Node.DOCUMENT_TYPE_NODE: + ret = (whatToShow & NodeFilter.SHOW_DOCUMENT_TYPE) != 0; + break; + case Node.ELEMENT_NODE: + ret = (whatToShow & NodeFilter.SHOW_ELEMENT) != 0; + break; + case Node.ENTITY_NODE: + ret = (whatToShow & NodeFilter.SHOW_ENTITY) != 0; + break; + case Node.ENTITY_REFERENCE_NODE: + ret = (whatToShow & NodeFilter.SHOW_ENTITY_REFERENCE) != 0; + ret = ret && entityReferenceExpansion; + break; + case Node.NOTATION_NODE: + ret = (whatToShow & NodeFilter.SHOW_NOTATION) != 0; + break; + case Node.PROCESSING_INSTRUCTION_NODE: + ret = (whatToShow & NodeFilter.SHOW_PROCESSING_INSTRUCTION) != 0; + break; + case Node.TEXT_NODE: + ret = (whatToShow & NodeFilter.SHOW_TEXT) != 0; + break; + default: + ret = true; + } + if (ret && filter != null) + { + ret = (filter.acceptNode(node) == NodeFilter.FILTER_ACCEPT); + } + return ret; + } + +} diff --git a/libjava/classpath/gnu/xml/dom/DomNotation.java b/libjava/classpath/gnu/xml/dom/DomNotation.java new file mode 100644 index 000000000..abb0e9da9 --- /dev/null +++ b/libjava/classpath/gnu/xml/dom/DomNotation.java @@ -0,0 +1,102 @@ +/* DomNotation.java -- + Copyright (C) 1999,2000,2001,2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.dom; + +import org.w3c.dom.Notation; + +/** + *
"Notation" implementation. This is a non-core DOM class, supporting + * the "XML" feature.
+ * + *Although unparsed entities using this notation can be detected using + * DOM, neither NOTATIONS nor ENTITY/ENTITIES attributes can be so detected. + * More, there is no portable way to construct a Notation node, so there's + * no way that vendor-neutral DOM construction APIs could even report a + * NOTATION used to identify the intended meaning of a ProcessingInstruction. + *
+ * + *In short, avoid using this DOM functionality. + * + * @see DomDoctype + * @see DomEntity + * @see DomPI + * + * @author David Brownell + * @author Chris Burdess + */ +public class DomNotation + extends DomExtern + implements Notation +{ + + /** + * Constructs a Notation node associated with the specified document, + * with the specified descriptive data. Note that at least one of + * the PUBLIC and SYSTEM identifiers must be provided; unlike other + * external objects in XML, notations may have only a PUBLIC identifier. + * + *
This constructor should only be invoked by a DomDoctype object + * as part of its declareNotation functionality, or through a subclass + * which is similarly used in a "Sub-DOM" style layer. + * + * @param owner The document with which this notation is associated + * @param name Name of this notation + * @param publicId If non-null, provides the notation's PUBLIC identifier + * @param systemId If non-null, rovides the notation's SYSTEM identifier + */ + protected DomNotation(DomDocument owner, + String name, + String publicId, + String systemId) + { + super(NOTATION_NODE, owner, name, publicId, systemId); + makeReadonly(); + } + + /** + * The base URI of an external entity is its system ID. + * The base URI of an internal entity is the parent document's base URI. + * @since DOM Level 3 Core + */ + public String getBaseURI() + { + String systemId = getSystemId(); + return (systemId == null) ? owner.getBaseURI() : systemId; + } + +} diff --git a/libjava/classpath/gnu/xml/dom/DomNsNode.java b/libjava/classpath/gnu/xml/dom/DomNsNode.java new file mode 100644 index 000000000..55e351185 --- /dev/null +++ b/libjava/classpath/gnu/xml/dom/DomNsNode.java @@ -0,0 +1,226 @@ +/* DomNsNode.java -- + Copyright (C) 1999,2000,2001,2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.dom; + +import javax.xml.XMLConstants; +import org.w3c.dom.DOMException; + +/** + *
Abstract implemention of namespace support. This facilitates + * sharing code for attribute and element nodes. + * + * @author David Brownell + * @author Chris Burdess + */ +public abstract class DomNsNode + extends DomNode +{ + + private String name; + private String namespace; + private String prefix; + private String localName; + + /** + * Constructs a node associated with the specified document, and + * with the specified namespace information. + * + * @param owner The document with which this entity is associated + * @param namespaceURI Combined with the local part of the name, + * this identifies a type of element or attribute; may be null. + * If this is the empty string, it is reassigned as null so that + * applications only need to test that case. + * @param name Name of this node, which may include a prefix + */ + // package private + DomNsNode(short nodeType, DomDocument owner, String namespaceURI, String name) + { + super(nodeType, owner); + setNodeName(name); + setNamespaceURI(namespaceURI); + } + + /** + * Constructs a node associated with the specified document, and + * with the specified namespace information. The prefix and local part + * are given explicitly rather than being computed. This allows them + * to be explicitly set to {@code null} as required by + * {@link Document#createElement(String)}. + * + * @param owner The document with which this entity is associated + * @param namespaceURI Combined with the local part of the name, + * this identifies a type of element or attribute; may be null. + * If this is the empty string, it is reassigned as null so that + * applications only need to test that case. + * @param name Name of this node, which may include a prefix + * @param prefix the namespace prefix of the name. May be {@code null}. + * @param localName the local part of the name. May be {@code null}. + */ + // package private + DomNsNode(short nodeType, DomDocument owner, String namespaceURI, String name, + String prefix, String localName) + { + super(nodeType, owner); + this.name = name.intern(); + this.prefix = prefix == null ? null : prefix.intern(); + this.localName = localName == null ? null : localName.intern(); + setNamespaceURI(namespaceURI); + } + + /** + * DOM L1 + * Returns the node's name, including any namespace prefix. + */ + public final String getNodeName() + { + return name; + } + + final void setNodeName(String name) + { + this.name = name.intern(); + int index = name.indexOf(':'); + if (index == -1) + { + prefix = null; + localName = this.name; + } + else + { + prefix = name.substring(0, index).intern(); + localName = name.substring(index + 1).intern(); + } + } + + /** + * DOM L2 + * Returns the node's namespace URI + * or null if the node name is not namespace scoped. + */ + public final String getNamespaceURI() + { + return namespace; + } + + final void setNamespaceURI(String namespaceURI) + { + if ("".equals(namespaceURI)) + { + namespaceURI = null; + } + namespace = (namespaceURI == null) ? null : namespaceURI.intern(); + } + + /** + * DOM L2 + * Returns any prefix part of the node's name (before any colon). + */ + public final String getPrefix() + { + return prefix; + } + + /** + * DOM L2 + * Assigns the prefix part of the node's name (before any colon). + */ + public final void setPrefix(String prefix) + { + if (readonly) + { + throw new DomDOMException(DOMException.NO_MODIFICATION_ALLOWED_ERR); + } + + if (prefix == null) + { + name = localName; + return; + } + else if (namespace == null) + { + throw new DomDOMException(DOMException.NAMESPACE_ERR, + "can't set prefix, node has no namespace URI", + this, 0); + } + + DomDocument.checkName(prefix, "1.1".equals(owner.getXmlVersion())); + if (prefix.indexOf (':') != -1) + { + throw new DomDOMException(DOMException.NAMESPACE_ERR, + "illegal prefix " + prefix, this, 0); + } + + if (XMLConstants.XML_NS_PREFIX.equals(prefix) + && !XMLConstants.XML_NS_URI.equals(namespace)) + { + throw new DomDOMException(DOMException.NAMESPACE_ERR, + "xml namespace is always " + + XMLConstants.XML_NS_URI, this, 0); + } + + if (XMLConstants.XMLNS_ATTRIBUTE.equals(prefix)) + { + if (namespace != null || getNodeType() != ATTRIBUTE_NODE) + { + throw new DomDOMException(DOMException.NAMESPACE_ERR, + "xmlns attribute prefix is reserved", + this, 0); + } + } + else if (getNodeType () == ATTRIBUTE_NODE + && (XMLConstants.XMLNS_ATTRIBUTE.equals(name) || + name.startsWith("xmlns:"))) + { + throw new DomDOMException(DOMException.NAMESPACE_ERR, + "namespace declarations can't change names", + this, 0); + } + + this.prefix = prefix.intern(); + } + + /** + * DOM L2 + * Returns the local part of the node's name (after any colon). + */ + public final String getLocalName() + { + return localName; + } + +} diff --git a/libjava/classpath/gnu/xml/dom/DomProcessingInstruction.java b/libjava/classpath/gnu/xml/dom/DomProcessingInstruction.java new file mode 100644 index 000000000..6d443a293 --- /dev/null +++ b/libjava/classpath/gnu/xml/dom/DomProcessingInstruction.java @@ -0,0 +1,146 @@ +/* DomProcessingInstruction.java -- + Copyright (C) 1999,2000,2001,2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.dom; + +import org.w3c.dom.DOMException; +import org.w3c.dom.ProcessingInstruction; + +/** + *
"ProcessingInstruction" (PI) implementation. + * This is a non-core DOM class, supporting the "XML" feature.
+ * + *Unlike other DOM APIs in the "XML" feature, this one fully + * exposes the functionality it describes. So there is no reason + * inherent in DOM to avoid using this API, unless you want to rely + * on NOTATION declarations to associate meaning with your PIs; + * there is no vendor-neutal way to record those notations in DOM.
+ * + *Also of note is that PI support is part of SAX, so that XML + * systems using PIs can choose among multiple APIs.
+ * + * @see DomNotation + * + * @author David Brownell + * @author Chris Burdess + */ +public class DomProcessingInstruction + extends DomNode + implements ProcessingInstruction +{ + + private String target; + private String data; + + /** + * Constructs a ProcessingInstruction node associated with the + * specified document, with the specified data. + * + *This constructor should only be invoked by a Document object as + * part of its createProcessingInstruction functionality, or through + * a subclass which is similarly used in a "Sub-DOM" style layer. + */ + protected DomProcessingInstruction(DomDocument owner, + String target, String data) + { + super(PROCESSING_INSTRUCTION_NODE, owner); + this.target = target; + this.data = data; + } + + /** + * DOM L1 + * Returns the target of the processing instruction. + */ + public final String getTarget() + { + return target; + } + + /** + * DOM L1 + * Returns the target of the processing instruction + * (same as getTarget). + */ + public final String getNodeName() + { + return target; + } + + /** + * DOM L1 + * Returns the data associated with the processing instruction. + */ + public final String getData() + { + return data; + } + + /** + * DOM L1 + * Returns the data associated with the processing instruction + * (same as getData). + */ + public final String getNodeValue() + { + return data; + } + + /** + * DOM L1 + * Assigns the data associated with the processing instruction; + * same as setNodeValue. + */ + public final void setData(String data) + { + setNodeValue(data); + } + + /** + * DOM L1 + * Assigns the data associated with the processing instruction. + */ + public final void setNodeValue(String data) + { + if (isReadonly()) + { + throw new DomDOMException(DOMException.NO_MODIFICATION_ALLOWED_ERR); + } + this.data = data; + } + +} diff --git a/libjava/classpath/gnu/xml/dom/DomText.java b/libjava/classpath/gnu/xml/dom/DomText.java new file mode 100644 index 000000000..b74c5b4c2 --- /dev/null +++ b/libjava/classpath/gnu/xml/dom/DomText.java @@ -0,0 +1,222 @@ +/* DomText.java -- + Copyright (C) 1999, 2000, 2001, 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.dom; + +import gnu.java.lang.CPStringBuilder; + +import org.w3c.dom.DOMException; +import org.w3c.dom.Text; + +/** + *
"Text" implementation.
+ * + * @author David Brownell + * @author Chris Burdess + */ +public class DomText + extends DomCharacterData + implements Text +{ + + // NOTE: deleted unused per-instance "isIgnorable" + // support to reclaim its space. + + /** + * Constructs a text node associated with the specified + * document and holding the specified data. + * + *This constructor should only be invoked by a Document object
+ * as part of its createTextNode functionality, or through a subclass
+ * which is similarly used in a "Sub-DOM" style layer.
+ */
+ protected DomText(DomDocument owner, String value)
+ {
+ super(TEXT_NODE, owner, value);
+ }
+
+ protected DomText(DomDocument owner, char[] buf, int off, int len)
+ {
+ super(TEXT_NODE, owner, buf, off, len);
+ }
+
+ // Used by DomCDATA
+ DomText(short nodeType, DomDocument owner, String value)
+ {
+ super(nodeType, owner, value);
+ }
+
+ DomText(short nodeType, DomDocument owner, char[] buf, int off, int len)
+ {
+ super(nodeType, owner, buf, off, len);
+ }
+
+ /**
+ * DOM L1
+ * Returns the string "#text".
+ */
+ // can't be 'final' with CDATA subclassing
+ public String getNodeName()
+ {
+ return "#text";
+ }
+
+ /**
+ * DOM L1
+ * Splits this text node in two parts at the offset, returning
+ * the new text node (the sibling with the second part).
+ */
+ public Text splitText(int offset)
+ {
+ if (isReadonly())
+ {
+ throw new DomDOMException(DOMException.NO_MODIFICATION_ALLOWED_ERR);
+ }
+ try
+ {
+ String text = getNodeValue();
+ String before = text.substring(0, offset);
+ String after = text.substring(offset);
+ Text next;
+
+ if (getNodeType() == TEXT_NODE)
+ {
+ next = owner.createTextNode(after);
+ }
+ else // CDATA_SECTION_NODE
+ {
+ next = owner.createCDATASection(after);
+ }
+
+ if (this.next != null)
+ {
+ parent.insertBefore(next, this.next);
+ }
+ else
+ {
+ parent.appendChild(next);
+ }
+ setNodeValue(before);
+ return next;
+
+ }
+ catch (IndexOutOfBoundsException x)
+ {
+ throw new DomDOMException(DOMException.INDEX_SIZE_ERR);
+ }
+ }
+
+ // DOM Level 3
+
+ public boolean isElementContentWhitespace()
+ {
+ if (parent != null)
+ {
+ DomDoctype doctype = (DomDoctype) owner.getDoctype();
+ if (doctype != null)
+ {
+ DTDElementTypeInfo info =
+ doctype.getElementTypeInfo(parent.getNodeName());
+ if (info != null)
+ {
+ if (info.model == null && info.model.indexOf("#PCDATA") != -1)
+ {
+ return false;
+ }
+ return getNodeValue().trim().length() == 0;
+ }
+ }
+ }
+ return false;
+ }
+
+ public String getWholeText()
+ {
+ DomNode ref = this;
+ DomNode ctx;
+ for (ctx = previous; ctx != null &&
+ (ctx.nodeType == TEXT_NODE || ctx.nodeType == CDATA_SECTION_NODE);
+ ctx = ctx.previous)
+ {
+ ref = ctx;
+ }
+ CPStringBuilder buf = new CPStringBuilder(ref.getNodeValue());
+ for (ctx = ref.next; ctx != null &&
+ (ctx.nodeType == TEXT_NODE || ctx.nodeType == CDATA_SECTION_NODE);
+ ctx = ctx.next)
+ {
+ buf.append(ctx.getNodeValue());
+ }
+ return buf.toString ();
+ }
+
+ public Text replaceWholeText(String content)
+ throws DOMException
+ {
+ boolean isEmpty = (content == null || content.length () == 0);
+ if (!isEmpty)
+ {
+ setNodeValue(content);
+ }
+
+ DomNode ref = this;
+ DomNode ctx;
+ for (ctx = previous; ctx != null &&
+ (ctx.nodeType == TEXT_NODE || ctx.nodeType == CDATA_SECTION_NODE);
+ ctx = ctx.previous)
+ {
+ ref = ctx;
+ }
+ ctx = ref.next;
+ if ((isEmpty || ref != this) && parent != null)
+ {
+ parent.removeChild(ref);
+ }
+ for (; ctx != null &&
+ (ctx.nodeType == TEXT_NODE || ctx.nodeType == CDATA_SECTION_NODE);
+ ctx = ref)
+ {
+ ref = ctx.next;
+ if ((isEmpty || ctx != this) && parent != null)
+ {
+ parent.removeChild(ctx);
+ }
+ }
+ return (isEmpty) ? null : this;
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/dom/DomXPathExpression.java b/libjava/classpath/gnu/xml/dom/DomXPathExpression.java
new file mode 100644
index 000000000..0b6962930
--- /dev/null
+++ b/libjava/classpath/gnu/xml/dom/DomXPathExpression.java
@@ -0,0 +1,147 @@
+/* DomXPathExpression.java --
+ Copyright (C) 2004 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.dom;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.List;
+import javax.xml.namespace.QName;
+import javax.xml.xpath.XPath;
+import javax.xml.xpath.XPathConstants;
+import javax.xml.xpath.XPathExpression;
+import javax.xml.xpath.XPathExpressionException;
+import javax.xml.xpath.XPathFactory;
+import org.w3c.dom.DOMException;
+import org.w3c.dom.Node;
+import org.w3c.dom.xpath.XPathException;
+import org.w3c.dom.xpath.XPathNSResolver;
+import org.w3c.dom.xpath.XPathResult;
+import gnu.xml.xpath.DocumentOrderComparator;
+
+/**
+ * An XPath expression.
+ *
+ * @author Chris Burdess
+ */
+class DomXPathExpression
+implements org.w3c.dom.xpath.XPathExpression
+{
+
+ final DomDocument doc;
+ final XPathExpression expression;
+ final XPathNSResolver resolver;
+
+ DomXPathExpression(DomDocument doc, String expression,
+ XPathNSResolver resolver)
+ throws XPathException
+ {
+ this.doc = doc;
+ this.resolver = resolver;
+
+ XPathFactory factory = XPathFactory.newInstance();
+ XPath xpath = factory.newXPath();
+ if (resolver != null)
+ {
+ xpath.setNamespaceContext(new DomNSResolverContext(resolver));
+ }
+ try
+ {
+ this.expression = xpath.compile(expression);
+ }
+ catch (XPathExpressionException e)
+ {
+ throw new XPathException(XPathException.INVALID_EXPRESSION_ERR,
+ e.getMessage ());
+ }
+ }
+
+ public Object evaluate(Node contextNode, short type, Object result)
+ throws XPathException, DOMException
+ {
+ try
+ {
+ QName typeName = null;
+ switch (type)
+ {
+ case XPathResult.BOOLEAN_TYPE:
+ typeName = XPathConstants.BOOLEAN;
+ break;
+ case XPathResult.NUMBER_TYPE:
+ typeName = XPathConstants.NUMBER;
+ break;
+ case XPathResult.STRING_TYPE:
+ typeName = XPathConstants.STRING;
+ break;
+ case XPathResult.ANY_UNORDERED_NODE_TYPE:
+ case XPathResult.FIRST_ORDERED_NODE_TYPE:
+ typeName = XPathConstants.NODE;
+ break;
+ case XPathResult.UNORDERED_NODE_ITERATOR_TYPE:
+ case XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE:
+ case XPathResult.ORDERED_NODE_ITERATOR_TYPE:
+ case XPathResult.ORDERED_NODE_SNAPSHOT_TYPE:
+ typeName = XPathConstants.NODESET;
+ break;
+ default:
+ throw new XPathException(XPathException.TYPE_ERR, null);
+ }
+ Object val = expression.evaluate(contextNode, typeName);
+ switch (type)
+ {
+ case XPathResult.ORDERED_NODE_ITERATOR_TYPE:
+ case XPathResult.ORDERED_NODE_SNAPSHOT_TYPE:
+ // Sort the nodes
+ List ns = new ArrayList((Collection) val);
+ Collections.sort(ns, new DocumentOrderComparator());
+ val = ns;
+ }
+ return new DomXPathResult(val, type);
+ }
+ catch (javax.xml.xpath.XPathException e)
+ {
+ throw new XPathException(XPathException.TYPE_ERR, e.getMessage());
+ }
+ }
+
+ public String toString ()
+ {
+ return getClass ().getName () + "[expression=" + expression + "]";
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/dom/DomXPathNSResolver.java b/libjava/classpath/gnu/xml/dom/DomXPathNSResolver.java
new file mode 100644
index 000000000..fb0719a4e
--- /dev/null
+++ b/libjava/classpath/gnu/xml/dom/DomXPathNSResolver.java
@@ -0,0 +1,64 @@
+/* DomXPathNSResolver.java --
+ Copyright (C) 2004 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.dom;
+
+import org.w3c.dom.Node;
+import org.w3c.dom.xpath.XPathNSResolver;
+
+/**
+ * Generic XPath namespace resolver using a DOM Node.
+ *
+ * @author Chris Burdess
+ */
+class DomXPathNSResolver
+implements XPathNSResolver
+{
+
+ Node node;
+
+ DomXPathNSResolver (Node node)
+ {
+ this.node = node;
+ }
+
+ public String lookupNamespaceURI (String prefix)
+ {
+ return node.lookupNamespaceURI (prefix);
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/dom/DomXPathResult.java b/libjava/classpath/gnu/xml/dom/DomXPathResult.java
new file mode 100644
index 000000000..da8fcf513
--- /dev/null
+++ b/libjava/classpath/gnu/xml/dom/DomXPathResult.java
@@ -0,0 +1,233 @@
+/* DomXPathResult.java --
+ Copyright (C) 2004 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.dom;
+
+import java.util.Collection;
+import java.util.Iterator;
+import org.w3c.dom.Node;
+import org.w3c.dom.xpath.XPathException;
+import org.w3c.dom.xpath.XPathResult;
+
+/**
+ * An XPath result object.
+ *
+ * @author Chris Burdess
+ */
+class DomXPathResult
+implements XPathResult
+{
+
+ final Object value;
+ final short type;
+ Iterator iterator;
+
+ DomXPathResult (Object value, short requestedType)
+ {
+ this.value = value;
+ if (value instanceof Boolean)
+ {
+ type = XPathResult.BOOLEAN_TYPE;
+ }
+ else if (value instanceof Double)
+ {
+ type = XPathResult.NUMBER_TYPE;
+ }
+ else if (value instanceof String)
+ {
+ type = XPathResult.STRING_TYPE;
+ }
+ else if (value instanceof Collection)
+ {
+ Collection ns = (Collection) value;
+ switch (requestedType)
+ {
+ case XPathResult.ANY_TYPE:
+ case XPathResult.ANY_UNORDERED_NODE_TYPE:
+ type = (ns.size () == 1) ? XPathResult.FIRST_ORDERED_NODE_TYPE :
+ XPathResult.ORDERED_NODE_ITERATOR_TYPE;
+ break;
+ default:
+ type = requestedType;
+ }
+ iterator = ns.iterator ();
+ }
+ else
+ {
+ throw new IllegalArgumentException ();
+ }
+ }
+
+ public boolean getBooleanValue()
+ {
+ if (type == XPathResult.BOOLEAN_TYPE)
+ {
+ return ((Boolean) value).booleanValue ();
+ }
+ throw new XPathException (XPathException.TYPE_ERR, value.toString ());
+ }
+
+ public boolean getInvalidIteratorState()
+ {
+ return iterator == null;
+ }
+
+ public double getNumberValue()
+ {
+ if (type == XPathResult.NUMBER_TYPE)
+ {
+ return ((Double) value).doubleValue ();
+ }
+ throw new XPathException (XPathException.TYPE_ERR, value.toString ());
+ }
+
+ public short getResultType()
+ {
+ return type;
+ }
+
+ public Node getSingleNodeValue()
+ {
+ switch (type)
+ {
+ case XPathResult.FIRST_ORDERED_NODE_TYPE:
+ case XPathResult.ORDERED_NODE_ITERATOR_TYPE:
+ case XPathResult.ORDERED_NODE_SNAPSHOT_TYPE:
+ case XPathResult.UNORDERED_NODE_ITERATOR_TYPE:
+ case XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE:
+ Collection ns = (Collection) value;
+ if (ns.isEmpty ())
+ {
+ return null;
+ }
+ else
+ {
+ return (Node) ns.iterator ().next ();
+ }
+ }
+ throw new XPathException (XPathException.TYPE_ERR, value.toString ());
+ }
+
+ public int getSnapshotLength()
+ {
+ switch (type)
+ {
+ case XPathResult.FIRST_ORDERED_NODE_TYPE:
+ case XPathResult.ORDERED_NODE_ITERATOR_TYPE:
+ case XPathResult.ORDERED_NODE_SNAPSHOT_TYPE:
+ case XPathResult.UNORDERED_NODE_ITERATOR_TYPE:
+ case XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE:
+ return ((Collection) value).size ();
+ }
+ throw new XPathException (XPathException.TYPE_ERR, value.toString ());
+ }
+
+ public String getStringValue()
+ {
+ if (type == XPathResult.STRING_TYPE)
+ {
+ return (String) value;
+ }
+ throw new XPathException (XPathException.TYPE_ERR, value.toString ());
+ }
+
+ public Node iterateNext()
+ {
+ if (iterator != null)
+ {
+ if (iterator.hasNext ())
+ {
+ return (Node) iterator.next ();
+ }
+ else
+ {
+ iterator = null;
+ return null;
+ }
+ }
+ throw new XPathException (XPathException.TYPE_ERR, value.toString ());
+ }
+
+ public Node snapshotItem(int index)
+ {
+ switch (type)
+ {
+ case XPathResult.FIRST_ORDERED_NODE_TYPE:
+ case XPathResult.ORDERED_NODE_ITERATOR_TYPE:
+ case XPathResult.ORDERED_NODE_SNAPSHOT_TYPE:
+ case XPathResult.UNORDERED_NODE_ITERATOR_TYPE:
+ case XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE:
+ Collection ns = (Collection) value;
+ Node[] nodes = new Node[ns.size ()];
+ ns.toArray (nodes);
+ return nodes[index];
+ }
+ throw new XPathException (XPathException.TYPE_ERR, value.toString ());
+ }
+
+ public String toString ()
+ {
+ return getClass ().getName () + "[type=" + typeName (type) + ",value=" +
+ value + ']';
+ }
+
+ private String typeName (short type)
+ {
+ switch (type)
+ {
+ case XPathResult.BOOLEAN_TYPE:
+ return "BOOLEAN_TYPE";
+ case XPathResult.NUMBER_TYPE:
+ return "NUMBER_TYPE";
+ case XPathResult.STRING_TYPE:
+ return "STRING_TYPE";
+ case XPathResult.FIRST_ORDERED_NODE_TYPE:
+ return "FIRST_ORDERED_NODE_TYPE";
+ case XPathResult.ORDERED_NODE_ITERATOR_TYPE:
+ return "ORDERED_NODE_ITERATOR_TYPE";
+ case XPathResult.ORDERED_NODE_SNAPSHOT_TYPE:
+ return "ORDERED_NODE_SNAPSHOT_TYPE";
+ case XPathResult.UNORDERED_NODE_ITERATOR_TYPE:
+ return "UNORDERED_NODE_ITERATOR_TYPE";
+ case XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE:
+ return "UNORDERED_NODE_SNAPSHOT_TYPE";
+ default:
+ return "(unknown)";
+ }
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/dom/ImplementationList.java b/libjava/classpath/gnu/xml/dom/ImplementationList.java
new file mode 100644
index 000000000..c99f55d0d
--- /dev/null
+++ b/libjava/classpath/gnu/xml/dom/ImplementationList.java
@@ -0,0 +1,70 @@
+/* ImplementationList.java --
+ Copyright (C) 2004 Free Software Foundation, Inc..
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+package gnu.xml.dom;
+
+import java.util.List;
+
+import org.w3c.dom.DOMImplementation;
+import org.w3c.dom.DOMImplementationList;
+
+/**
+ * Implementation list for GNU JAXP.
+ *
+ * @author Chris Burdess
+ */
+public class ImplementationList
+ implements DOMImplementationList
+{
+
+ private List list;
+
+ ImplementationList(List list)
+ {
+ this.list = list;
+ }
+
+ public int getLength()
+ {
+ return list.size();
+ }
+
+ public DOMImplementation item(int index)
+ {
+ return (DOMImplementation) list.get(index);
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/dom/ImplementationSource.java b/libjava/classpath/gnu/xml/dom/ImplementationSource.java
new file mode 100644
index 000000000..5f404423a
--- /dev/null
+++ b/libjava/classpath/gnu/xml/dom/ImplementationSource.java
@@ -0,0 +1,167 @@
+/* ImplementationSource.java --
+ Copyright (C) 2004 Free Software Foundation, Inc..
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+package gnu.xml.dom;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Iterator;
+import java.util.List;
+
+import org.w3c.dom.DOMImplementation;
+import org.w3c.dom.DOMImplementationList;
+import org.w3c.dom.DOMImplementationSource;
+
+/**
+ * Implementation source for GNU JAXP.
+ *
+ * @author Chris Burdess
+ */
+public class ImplementationSource
+ implements DOMImplementationSource
+{
+
+ private static final String DIGITS = "1234567890";
+
+ /*
+ * GNU DOM implementations.
+ */
+ private static final DOMImplementation[] implementations;
+
+ static
+ {
+ List acc = new ArrayList();
+ acc.add(new gnu.xml.dom.DomImpl());
+ try
+ {
+ Class t = Class.forName("gnu.xml.libxmlj.dom.GnomeDocumentBuilder");
+ acc.add(t.newInstance());
+ }
+ catch (Exception e)
+ {
+ // libxmlj not available
+ }
+ catch (UnsatisfiedLinkError e)
+ {
+ // libxmlj not available
+ }
+ implementations = new DOMImplementation[acc.size()];
+ acc.toArray(implementations);
+ }
+
+ public DOMImplementation getDOMImplementation(String features)
+ {
+ List available = getImplementations(features);
+ if (available.isEmpty())
+ {
+ return null;
+ }
+ return (DOMImplementation) available.get(0);
+ }
+
+ public DOMImplementationList getDOMImplementationList(String features)
+ {
+ List available = getImplementations(features);
+ return new ImplementationList(available);
+ }
+
+ /**
+ * Returns a list of the implementations that support the specified
+ * features.
+ */
+ private final List getImplementations(String features)
+ {
+ List available = new ArrayList(Arrays.asList(implementations));
+ for (Iterator i = parseFeatures(features).iterator(); i.hasNext(); )
+ {
+ String feature = (String) i.next();
+ String version = null;
+ int si = feature.indexOf(' ');
+ if (si != -1)
+ {
+ version = feature.substring(si + 1);
+ feature = feature.substring(0, si);
+ }
+ for (Iterator j = available.iterator(); j.hasNext(); )
+ {
+ DOMImplementation impl = (DOMImplementation) j.next();
+ if (!impl.hasFeature(feature, version))
+ {
+ j.remove();
+ }
+ }
+ }
+ return available;
+ }
+
+ /**
+ * Parses the feature list into feature tokens.
+ */
+ final List parseFeatures(String features)
+ {
+ List list = new ArrayList();
+ int pos = 0, start = 0;
+ int len = features.length();
+ for (; pos < len; pos++)
+ {
+ char c = features.charAt(pos);
+ if (c == ' ')
+ {
+ if (pos + 1 < len &&
+ DIGITS.indexOf(features.charAt(pos + 1)) == -1)
+ {
+ list.add(getFeature(features, start, pos));
+ start = pos + 1;
+ }
+ }
+ }
+ if (pos > start)
+ {
+ list.add(getFeature(features, start, len));
+ }
+ return list;
+ }
+
+ final String getFeature(String features, int start, int end)
+ {
+ if (features.length() > 0 && features.charAt(start) == '+')
+ {
+ return features.substring(start + 1, end);
+ }
+ return features.substring(start, end);
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/dom/JAXPFactory.java b/libjava/classpath/gnu/xml/dom/JAXPFactory.java
new file mode 100644
index 000000000..d7d399b5b
--- /dev/null
+++ b/libjava/classpath/gnu/xml/dom/JAXPFactory.java
@@ -0,0 +1,308 @@
+/* JAXPFactory.java --
+ Copyright (C) 2001 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.dom;
+
+import java.io.IOException;
+
+import org.w3c.dom.Document;
+import org.w3c.dom.DOMImplementation;
+
+import org.xml.sax.EntityResolver;
+import org.xml.sax.ErrorHandler;
+import org.xml.sax.InputSource;
+import org.xml.sax.XMLReader;
+import org.xml.sax.SAXException;
+import org.xml.sax.SAXParseException;
+
+import javax.xml.XMLConstants;
+import javax.xml.parsers.DocumentBuilder;
+import javax.xml.parsers.DocumentBuilderFactory;
+import javax.xml.parsers.ParserConfigurationException;
+import javax.xml.parsers.SAXParserFactory;
+
+
+/**
+ * DOM bootstrapping API, for use with JAXP.
+ *
+ * @see Consumer
+ *
+ * @author David Brownell
+ */
+public final class JAXPFactory
+ extends DocumentBuilderFactory
+{
+
+ private static final String PROPERTY = "http://xml.org/sax/properties/";
+ private static final String FEATURE = "http://xml.org/sax/features/";
+
+ private SAXParserFactory pf;
+ private boolean secureProcessing;
+
+ /**
+ * Default constructor.
+ */
+ public JAXPFactory()
+ {
+ }
+
+ /**
+ * Constructs a JAXP document builder which uses the default
+ * JAXP SAX2 parser and the DOM implementation in this package.
+ */
+ public DocumentBuilder newDocumentBuilder()
+ throws ParserConfigurationException
+ {
+ if (pf == null)
+ {
+ // Force use of AElfred2 since not all JAXP parsers
+ // conform very well to the SAX2 API spec ...
+ pf = new gnu.xml.aelfred2.JAXPFactory();
+ // pf = SAXParserFactory.newInstance ();
+ }
+
+ // JAXP default: false
+ pf.setValidating(isValidating());
+
+ // FIXME: this namespace setup may cause errors in some
+ // conformant SAX2 parsers, which we CAN patch up by
+ // splicing a "NSFilter" stage up front ...
+
+ // JAXP default: false
+ pf.setNamespaceAware(isNamespaceAware());
+
+ try
+ {
+ // undo rude "namespace-prefixes=false" default
+ pf.setFeature(FEATURE + "namespace-prefixes", true);
+
+ return new JAXPBuilder(pf.newSAXParser().getXMLReader(), this);
+ }
+ catch (SAXException e)
+ {
+ String msg = "can't create JAXP DocumentBuilder: " + e.getMessage();
+ throw new ParserConfigurationException(msg);
+ }
+ }
+
+ /** There seems to be no useful specification for attribute names */
+ public void setAttribute(String name, Object value)
+ throws IllegalArgumentException
+ {
+ if ("http://java.sun.com/xml/jaxp/properties/schemaLanguage".equals(name))
+ {
+ // TODO
+ }
+ else
+ {
+ throw new IllegalArgumentException(name);
+ }
+ }
+
+ /** There seems to be no useful specification for attribute names */
+ public Object getAttribute(String name)
+ throws IllegalArgumentException
+ {
+ throw new IllegalArgumentException(name);
+ }
+
+ public void setFeature(String name, boolean value)
+ throws ParserConfigurationException
+ {
+ if (name == null)
+ throw new NullPointerException();
+ if (XMLConstants.FEATURE_SECURE_PROCESSING.equals(name))
+ {
+ secureProcessing = true;
+ return;
+ }
+ throw new ParserConfigurationException(name);
+ }
+
+ public boolean getFeature(String name)
+ throws ParserConfigurationException
+ {
+ if (XMLConstants.FEATURE_SECURE_PROCESSING.equals(name))
+ return secureProcessing;
+ throw new ParserConfigurationException(name);
+ }
+
+ static final class JAXPBuilder
+ extends DocumentBuilder
+ implements ErrorHandler
+ {
+
+ private Consumer consumer;
+ private XMLReader producer;
+ private DomImpl impl;
+
+ JAXPBuilder(XMLReader parser, JAXPFactory factory)
+ throws ParserConfigurationException
+ {
+ impl = new DomImpl();
+
+ // set up consumer side
+ try
+ {
+ consumer = new Consumer();
+ }
+ catch (SAXException e)
+ {
+ throw new ParserConfigurationException(e.getMessage());
+ }
+
+ // JAXP defaults: true, noise nodes are good (bleech)
+ consumer.setHidingReferences(factory.isExpandEntityReferences());
+ consumer.setHidingComments(factory.isIgnoringComments());
+ consumer.setHidingWhitespace(factory.isIgnoringElementContentWhitespace());
+ consumer.setHidingCDATA(factory.isCoalescing());
+
+ // set up producer side
+ producer = parser;
+ producer.setContentHandler(consumer.getContentHandler());
+ producer.setDTDHandler(consumer.getDTDHandler());
+
+ try
+ {
+ String id;
+
+ // if validating, report validity errors, and default
+ // to treating them as fatal
+ if (factory.isValidating ())
+ {
+ producer.setFeature(FEATURE + "validation", true);
+ producer.setErrorHandler(this);
+ }
+
+ // always save prefix info, maybe do namespace processing
+ producer.setFeature(FEATURE + "namespace-prefixes", true);
+ producer.setFeature(FEATURE + "namespaces",
+ factory.isNamespaceAware());
+
+ // set important handlers
+ id = PROPERTY + "lexical-handler";
+ producer.setProperty(id, consumer.getProperty(id));
+
+ id = PROPERTY + "declaration-handler";
+ producer.setProperty(id, consumer.getProperty(id));
+
+ }
+ catch (SAXException e)
+ {
+ throw new ParserConfigurationException(e.getMessage());
+ }
+ }
+
+ public Document parse(InputSource source)
+ throws SAXException, IOException
+ {
+ producer.parse(source);
+ Document doc = consumer.getDocument();
+ // TODO inputEncoding
+ doc.setDocumentURI(source.getSystemId());
+ return doc;
+ }
+
+ public boolean isNamespaceAware()
+ {
+ try
+ {
+ return producer.getFeature(FEATURE + "namespaces");
+ }
+ catch (SAXException e)
+ {
+ // "can't happen"
+ throw new RuntimeException(e.getMessage());
+ }
+ }
+
+ public boolean isValidating()
+ {
+ try
+ {
+ return producer.getFeature(FEATURE + "validation");
+ }
+ catch (SAXException e)
+ {
+ // "can't happen"
+ throw new RuntimeException(e.getMessage());
+ }
+ }
+
+ public void setEntityResolver(EntityResolver resolver)
+ {
+ producer.setEntityResolver(resolver);
+ }
+
+ public void setErrorHandler(ErrorHandler handler)
+ {
+ producer.setErrorHandler(handler);
+ consumer.setErrorHandler(handler);
+ }
+
+ public DOMImplementation getDOMImplementation()
+ {
+ return impl;
+ }
+
+ public Document newDocument()
+ {
+ return new DomDocument();
+ }
+
+ // implementation of error handler that's used when validating
+ public void fatalError(SAXParseException e)
+ throws SAXException
+ {
+ throw e;
+ }
+
+ public void error(SAXParseException e)
+ throws SAXException
+ {
+ throw e;
+ }
+
+ public void warning(SAXParseException e)
+ throws SAXException
+ {
+ /* ignore */
+ }
+
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/dom/html2/DomHTMLAnchorElement.java b/libjava/classpath/gnu/xml/dom/html2/DomHTMLAnchorElement.java
new file mode 100644
index 000000000..4448dd580
--- /dev/null
+++ b/libjava/classpath/gnu/xml/dom/html2/DomHTMLAnchorElement.java
@@ -0,0 +1,188 @@
+/* DomHTMLAnchorElement.java --
+ Copyright (C) 2005 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.dom.html2;
+
+import org.w3c.dom.html2.HTMLAnchorElement;
+
+/**
+ * An HTML 'A' element node.
+ *
+ * @author Chris Burdess
+ */
+public class DomHTMLAnchorElement
+ extends DomHTMLElement
+ implements HTMLAnchorElement
+{
+
+ protected DomHTMLAnchorElement(DomHTMLDocument owner, String namespaceURI,
+ String name)
+ {
+ super(owner, namespaceURI, name);
+ }
+
+ public String getAccessKey()
+ {
+ return getHTMLAttribute("accesskey");
+ }
+
+ public void setAccessKey(String accessKey)
+ {
+ setHTMLAttribute("accesskey", accessKey);
+ }
+
+ public String getCharset()
+ {
+ return getHTMLAttribute("charset");
+ }
+
+ public void setCharset(String charset)
+ {
+ setHTMLAttribute("charset", charset);
+ }
+
+ public String getCoords()
+ {
+ return getHTMLAttribute("coords");
+ }
+
+ public void setCoords(String coords)
+ {
+ setHTMLAttribute("coords", coords);
+ }
+
+ public String getHref()
+ {
+ return getHTMLAttribute("href");
+ }
+
+ public void setHref(String href)
+ {
+ setHTMLAttribute("href", href);
+ }
+
+ public String getHreflang()
+ {
+ return getHTMLAttribute("hreflang");
+ }
+
+ public void setHreflang(String hreflang)
+ {
+ setHTMLAttribute("hreflang", hreflang);
+ }
+
+ public String getName()
+ {
+ return getHTMLAttribute("name");
+ }
+
+ public void setName(String name)
+ {
+ setHTMLAttribute("name", name);
+ }
+
+ public String getRel()
+ {
+ return getHTMLAttribute("rel");
+ }
+
+ public void setRel(String rel)
+ {
+ setHTMLAttribute("rel", rel);
+ }
+
+ public String getRev()
+ {
+ return getHTMLAttribute("rev");
+ }
+
+ public void setRev(String rev)
+ {
+ setHTMLAttribute("rev", rev);
+ }
+
+ public String getShape()
+ {
+ return getHTMLAttribute("shape");
+ }
+
+ public void setShape(String shape)
+ {
+ setHTMLAttribute("shape", shape);
+ }
+
+ public int getTabIndex()
+ {
+ return getIntHTMLAttribute("tabindex");
+ }
+
+ public void setTabIndex(int tabIndex)
+ {
+ setIntHTMLAttribute("tabindex", tabIndex);
+ }
+
+ public String getTarget()
+ {
+ return getHTMLAttribute("target");
+ }
+
+ public void setTarget(String target)
+ {
+ setHTMLAttribute("target", target);
+ }
+
+ public String getType()
+ {
+ return getHTMLAttribute("type");
+ }
+
+ public void setType(String type)
+ {
+ setHTMLAttribute("type", type);
+ }
+
+ public void blur()
+ {
+ dispatchUIEvent("blur");
+ }
+
+ public void focus()
+ {
+ dispatchUIEvent("focus");
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/dom/html2/DomHTMLAppletElement.java b/libjava/classpath/gnu/xml/dom/html2/DomHTMLAppletElement.java
new file mode 100644
index 000000000..429fb0038
--- /dev/null
+++ b/libjava/classpath/gnu/xml/dom/html2/DomHTMLAppletElement.java
@@ -0,0 +1,187 @@
+/* DomHTMLAppletElement.java --
+ Copyright (C) 2005 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.dom.html2;
+
+import org.w3c.dom.html2.HTMLAppletElement;
+
+/**
+ * An HTML 'APPLET' element node.
+ *
+ * @author Chris Burdess
+ */
+public class DomHTMLAppletElement
+ extends DomHTMLElement
+ implements HTMLAppletElement
+{
+
+ protected DomHTMLAppletElement(DomHTMLDocument owner, String namespaceURI,
+ String name)
+ {
+ super(owner, namespaceURI, name);
+ }
+
+ public String getAlign()
+ {
+ return getHTMLAttribute("align");
+ }
+
+ public void setAlign(String align)
+ {
+ setHTMLAttribute("align", align);
+ }
+
+ public String getCls()
+ {
+ return getHTMLAttribute("class");
+ }
+
+ public void setCls(String cls)
+ {
+ setHTMLAttribute("class", cls);
+ }
+
+ public String getSrc()
+ {
+ return getHTMLAttribute("src");
+ }
+
+ public void setSrc(String src)
+ {
+ setHTMLAttribute("src", src);
+ }
+
+ public String getAlt()
+ {
+ return getHTMLAttribute("alt");
+ }
+
+ public void setAlt(String alt)
+ {
+ setHTMLAttribute("alt", alt);
+ }
+
+ public String getArchive()
+ {
+ return getHTMLAttribute("archive");
+ }
+
+ public void setArchive(String archive)
+ {
+ setHTMLAttribute("archive", archive);
+ }
+
+ public String getCode()
+ {
+ return getHTMLAttribute("code");
+ }
+
+ public void setCode(String code)
+ {
+ setHTMLAttribute("code", code);
+ }
+
+ public String getCodeBase()
+ {
+ return getHTMLAttribute("codebase");
+ }
+
+ public void setCodeBase(String codeBase)
+ {
+ setHTMLAttribute("codebase", codeBase);
+ }
+
+ public String getHeight()
+ {
+ return getHTMLAttribute("height");
+ }
+
+ public void setHeight(String height)
+ {
+ setHTMLAttribute("height", height);
+ }
+
+ public int getHspace()
+ {
+ return getIntHTMLAttribute("hspace");
+ }
+
+ public void setHspace(int hspace)
+ {
+ setIntHTMLAttribute("hspace", hspace);
+ }
+
+ public String getName()
+ {
+ return getHTMLAttribute("name");
+ }
+
+ public void setName(String name)
+ {
+ setHTMLAttribute("name", name);
+ }
+
+ public String getObject()
+ {
+ return getHTMLAttribute("object");
+ }
+
+ public void setObject(String object)
+ {
+ setHTMLAttribute("object", object);
+ }
+
+ public int getVspace()
+ {
+ return getIntHTMLAttribute("vspace");
+ }
+
+ public void setVspace(int vspace)
+ {
+ setIntHTMLAttribute("vspace", vspace);
+ }
+
+ public String getWidth()
+ {
+ return getHTMLAttribute("width");
+ }
+
+ public void setWidth(String width)
+ {
+ setHTMLAttribute("width", width);
+ }
+}
diff --git a/libjava/classpath/gnu/xml/dom/html2/DomHTMLAreaElement.java b/libjava/classpath/gnu/xml/dom/html2/DomHTMLAreaElement.java
new file mode 100644
index 000000000..728b3fdfc
--- /dev/null
+++ b/libjava/classpath/gnu/xml/dom/html2/DomHTMLAreaElement.java
@@ -0,0 +1,138 @@
+/* DomHTMLAreaElement.java --
+ Copyright (C) 2005 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.dom.html2;
+
+import org.w3c.dom.html2.HTMLAreaElement;
+
+/**
+ * An HTML 'AREA' element node.
+ *
+ * @author Chris Burdess
+ */
+public class DomHTMLAreaElement
+ extends DomHTMLElement
+ implements HTMLAreaElement
+{
+
+ protected DomHTMLAreaElement(DomHTMLDocument owner, String namespaceURI,
+ String name)
+ {
+ super(owner, namespaceURI, name);
+ }
+
+ public String getAccessKey()
+ {
+ return getHTMLAttribute("accesskey");
+ }
+
+ public void setAccessKey(String accessKey)
+ {
+ setHTMLAttribute("accesskey", accessKey);
+ }
+
+ public String getAlt()
+ {
+ return getHTMLAttribute("alt");
+ }
+
+ public void setAlt(String alt)
+ {
+ setHTMLAttribute("alt", alt);
+ }
+
+ public String getCoords()
+ {
+ return getHTMLAttribute("coords");
+ }
+
+ public void setCoords(String coords)
+ {
+ setHTMLAttribute("coords", coords);
+ }
+
+ public String getHref()
+ {
+ return getHTMLAttribute("href");
+ }
+
+ public void setHref(String href)
+ {
+ setHTMLAttribute("href", href);
+ }
+
+ public boolean getNoHref()
+ {
+ return getBooleanHTMLAttribute("nohref");
+ }
+
+ public void setNoHref(boolean nohref)
+ {
+ setBooleanHTMLAttribute("nohref", nohref);
+ }
+
+ public String getShape()
+ {
+ return getHTMLAttribute("shape");
+ }
+
+ public void setShape(String shape)
+ {
+ setHTMLAttribute("shape", shape);
+ }
+
+ public int getTabIndex()
+ {
+ return getIntHTMLAttribute("tabindex");
+ }
+
+ public void setTabIndex(int tabIndex)
+ {
+ setIntHTMLAttribute("tabindex", tabIndex);
+ }
+
+ public String getTarget()
+ {
+ return getHTMLAttribute("target");
+ }
+
+ public void setTarget(String target)
+ {
+ setHTMLAttribute("target", target);
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/dom/html2/DomHTMLBRElement.java b/libjava/classpath/gnu/xml/dom/html2/DomHTMLBRElement.java
new file mode 100644
index 000000000..5ad49c549
--- /dev/null
+++ b/libjava/classpath/gnu/xml/dom/html2/DomHTMLBRElement.java
@@ -0,0 +1,68 @@
+/* DomHTMLBRElement.java --
+ Copyright (C) 2005 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.dom.html2;
+
+import org.w3c.dom.html2.HTMLBRElement;
+
+/**
+ * An HTML 'BR' element node.
+ *
+ * @author Chris Burdess
+ */
+public class DomHTMLBRElement
+ extends DomHTMLElement
+ implements HTMLBRElement
+{
+
+ protected DomHTMLBRElement(DomHTMLDocument owner, String namespaceURI,
+ String name)
+ {
+ super(owner, namespaceURI, name);
+ }
+
+ public String getClear()
+ {
+ return getHTMLAttribute("clear");
+ }
+
+ public void setClear(String clear)
+ {
+ setHTMLAttribute("clear", clear);
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/dom/html2/DomHTMLBaseElement.java b/libjava/classpath/gnu/xml/dom/html2/DomHTMLBaseElement.java
new file mode 100644
index 000000000..99a0993ba
--- /dev/null
+++ b/libjava/classpath/gnu/xml/dom/html2/DomHTMLBaseElement.java
@@ -0,0 +1,78 @@
+/* DomHTMLBaseElement.java --
+ Copyright (C) 2005 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.dom.html2;
+
+import org.w3c.dom.html2.HTMLBaseElement;
+
+/**
+ * An HTML 'BASE' element node.
+ *
+ * @author Chris Burdess
+ */
+public class DomHTMLBaseElement
+ extends DomHTMLElement
+ implements HTMLBaseElement
+{
+
+ protected DomHTMLBaseElement(DomHTMLDocument owner, String namespaceURI,
+ String name)
+ {
+ super(owner, namespaceURI, name);
+ }
+
+ public String getHref()
+ {
+ return getHTMLAttribute("href");
+ }
+
+ public void setHref(String href)
+ {
+ setHTMLAttribute("href", href);
+ }
+
+ public String getTarget()
+ {
+ return getHTMLAttribute("target");
+ }
+
+ public void setTarget(String target)
+ {
+ setHTMLAttribute("target", target);
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/dom/html2/DomHTMLBaseFontElement.java b/libjava/classpath/gnu/xml/dom/html2/DomHTMLBaseFontElement.java
new file mode 100644
index 000000000..73ec96dbd
--- /dev/null
+++ b/libjava/classpath/gnu/xml/dom/html2/DomHTMLBaseFontElement.java
@@ -0,0 +1,88 @@
+/* DomHTMLBaseFontElement.java --
+ Copyright (C) 2005 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.dom.html2;
+
+import org.w3c.dom.html2.HTMLBaseFontElement;
+
+/**
+ * An HTML 'BASEFONT' element node.
+ *
+ * @author Chris Burdess
+ */
+public class DomHTMLBaseFontElement
+ extends DomHTMLElement
+ implements HTMLBaseFontElement
+{
+
+ protected DomHTMLBaseFontElement(DomHTMLDocument owner, String namespaceURI,
+ String name)
+ {
+ super(owner, namespaceURI, name);
+ }
+
+ public String getColor()
+ {
+ return getHTMLAttribute("color");
+ }
+
+ public void setColor(String color)
+ {
+ setHTMLAttribute("color", color);
+ }
+
+ public String getFace()
+ {
+ return getHTMLAttribute("face");
+ }
+
+ public void setFace(String face)
+ {
+ setHTMLAttribute("face", face);
+ }
+
+ public int getSize()
+ {
+ return getIntHTMLAttribute("size");
+ }
+
+ public void setSize(int size)
+ {
+ setIntHTMLAttribute("size", size);
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/dom/html2/DomHTMLBodyElement.java b/libjava/classpath/gnu/xml/dom/html2/DomHTMLBodyElement.java
new file mode 100644
index 000000000..d4528625b
--- /dev/null
+++ b/libjava/classpath/gnu/xml/dom/html2/DomHTMLBodyElement.java
@@ -0,0 +1,118 @@
+/* DomHTMLBodyElement.java --
+ Copyright (C) 2005 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.dom.html2;
+
+import org.w3c.dom.html2.HTMLBodyElement;
+
+/**
+ * An HTML 'BODY' element node.
+ *
+ * @author Chris Burdess
+ */
+public class DomHTMLBodyElement
+ extends DomHTMLElement
+ implements HTMLBodyElement
+{
+
+ protected DomHTMLBodyElement(DomHTMLDocument owner, String namespaceURI,
+ String name)
+ {
+ super(owner, namespaceURI, name);
+ }
+
+ public String getALink()
+ {
+ return getHTMLAttribute("alink");
+ }
+
+ public void setALink(String alink)
+ {
+ setHTMLAttribute("alink", alink);
+ }
+
+ public String getBackground()
+ {
+ return getHTMLAttribute("background");
+ }
+
+ public void setBackground(String background)
+ {
+ setHTMLAttribute("background", background);
+ }
+
+ public String getBgColor()
+ {
+ return getHTMLAttribute("bgcolor");
+ }
+
+ public void setBgColor(String bgcolor)
+ {
+ setHTMLAttribute("bgcolor", bgcolor);
+ }
+
+ public String getLink()
+ {
+ return getHTMLAttribute("link");
+ }
+
+ public void setLink(String link)
+ {
+ setHTMLAttribute("link", link);
+ }
+
+ public String getText()
+ {
+ return getHTMLAttribute("text");
+ }
+
+ public void setText(String text)
+ {
+ setHTMLAttribute("text", text);
+ }
+
+ public String getVLink()
+ {
+ return getHTMLAttribute("vlink");
+ }
+
+ public void setVLink(String vlink)
+ {
+ setHTMLAttribute("vlink", vlink);
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/dom/html2/DomHTMLButtonElement.java b/libjava/classpath/gnu/xml/dom/html2/DomHTMLButtonElement.java
new file mode 100644
index 000000000..696169ac2
--- /dev/null
+++ b/libjava/classpath/gnu/xml/dom/html2/DomHTMLButtonElement.java
@@ -0,0 +1,119 @@
+/* DomHTMLButtonElement.java --
+ Copyright (C) 2005 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.dom.html2;
+
+import org.w3c.dom.html2.HTMLButtonElement;
+import org.w3c.dom.html2.HTMLFormElement;
+
+/**
+ * An HTML 'BUTTON' element node.
+ *
+ * @author Chris Burdess
+ */
+public class DomHTMLButtonElement
+ extends DomHTMLElement
+ implements HTMLButtonElement
+{
+
+ protected DomHTMLButtonElement(DomHTMLDocument owner, String namespaceURI,
+ String name)
+ {
+ super(owner, namespaceURI, name);
+ }
+
+ public HTMLFormElement getForm()
+ {
+ return (HTMLFormElement) getParentElement("form");
+ }
+
+ public String getAccessKey()
+ {
+ return getHTMLAttribute("accesskey");
+ }
+
+ public void setAccessKey(String accessKey)
+ {
+ setHTMLAttribute("accesskey", accessKey);
+ }
+
+ public boolean getDisabled()
+ {
+ return getBooleanHTMLAttribute("disabled");
+ }
+
+ public void setDisabled(boolean disabled)
+ {
+ setBooleanHTMLAttribute("disabled", disabled);
+ }
+
+ public String getName()
+ {
+ return getHTMLAttribute("name");
+ }
+
+ public void setName(String name)
+ {
+ setHTMLAttribute("name", name);
+ }
+
+ public int getTabIndex()
+ {
+ return getIntHTMLAttribute("tabindex");
+ }
+
+ public void setTabIndex(int tabIndex)
+ {
+ setIntHTMLAttribute("tabindex", tabIndex);
+ }
+
+ public String getType()
+ {
+ return getHTMLAttribute("type");
+ }
+
+ public String getValue()
+ {
+ return getHTMLAttribute("value");
+ }
+
+ public void setValue(String value)
+ {
+ setHTMLAttribute("value", value);
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/dom/html2/DomHTMLCollection.java b/libjava/classpath/gnu/xml/dom/html2/DomHTMLCollection.java
new file mode 100644
index 000000000..830028753
--- /dev/null
+++ b/libjava/classpath/gnu/xml/dom/html2/DomHTMLCollection.java
@@ -0,0 +1,225 @@
+/* DomHTMLCollection.java --
+ Copyright (C) 2005 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.dom.html2;
+
+import gnu.xml.dom.DomDOMException;
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.LinkedList;
+import java.util.List;
+import org.w3c.dom.DOMException;
+import org.w3c.dom.NamedNodeMap;
+import org.w3c.dom.Node;
+import org.w3c.dom.NodeList;
+import org.w3c.dom.html2.HTMLCollection;
+import org.w3c.dom.html2.HTMLOptionsCollection;
+import org.w3c.dom.traversal.NodeFilter;
+import org.w3c.dom.traversal.NodeIterator;
+
+/**
+ * An HTML element collection.
+ *
+ * @author Chris Burdess
+ */
+class DomHTMLCollection
+ implements HTMLCollection, HTMLOptionsCollection, NodeList, NodeFilter
+{
+
+ final DomHTMLDocument doc;
+ final Node root;
+ List nodeNames;
+ List attributeNames;
+ List results;
+
+ DomHTMLCollection(DomHTMLDocument doc, Node root)
+ {
+ this.doc = doc;
+ this.root = root;
+ }
+
+ // -- Node name and attribute filtering --
+
+ void addNodeName(String name)
+ {
+ if (nodeNames == null)
+ {
+ nodeNames = new LinkedList();
+ }
+ nodeNames.add(name);
+ }
+
+ void addAttributeName(String name)
+ {
+ if (attributeNames == null)
+ {
+ attributeNames = new LinkedList();
+ }
+ attributeNames.add(name);
+ }
+
+ public short acceptNode(Node n)
+ {
+ if (n.getNodeType() != Node.ELEMENT_NODE)
+ {
+ return NodeFilter.FILTER_SKIP;
+ }
+ String localName = n.getLocalName();
+ if (localName == null)
+ {
+ localName = n.getNodeName();
+ }
+ if (nodeNames != null && !acceptName(localName))
+ {
+ return NodeFilter.FILTER_SKIP;
+ }
+ if (attributeNames != null && !acceptAttributes(n.getAttributes()))
+ {
+ return NodeFilter.FILTER_SKIP;
+ }
+ return NodeFilter.FILTER_ACCEPT;
+ }
+
+ private boolean acceptName(String name)
+ {
+ for (Iterator i = nodeNames.iterator(); i.hasNext(); )
+ {
+ String nodeName = (String) i.next();
+ if (nodeName.equalsIgnoreCase(name))
+ {
+ return true;
+ }
+ }
+ return false;
+ }
+
+ private boolean acceptAttributes(NamedNodeMap attrs)
+ {
+ for (Iterator i = attributeNames.iterator(); i.hasNext(); )
+ {
+ String attributeName = (String) i.next();
+ Node attr = getNamedItem(attrs, attributeName);
+ if (attr != null)
+ {
+ // Check that attribute has a non-null value
+ String nodeValue = attr.getNodeValue();
+ if (nodeValue != null && nodeValue.length() > 0)
+ {
+ return true;
+ }
+ }
+ }
+ return false;
+ }
+
+ /**
+ * Case-insensitive version of getNamedItem.
+ */
+ private Node getNamedItem(NamedNodeMap attrs, String name)
+ {
+ int len = attrs.getLength();
+ for (int i = 0; i < len; i++)
+ {
+ Node attr = attrs.item(i);
+ String attrName = attr.getLocalName();
+ if (attrName == null)
+ {
+ attrName = attr.getNodeName();
+ }
+ if (name.equalsIgnoreCase(attrName))
+ {
+ return attr;
+ }
+ }
+ return null;
+ }
+
+ // -- Perform query --
+
+ void evaluate()
+ {
+ NodeIterator i = doc.createNodeIterator(root, NodeFilter.SHOW_ELEMENT,
+ this, true);
+ results = new ArrayList();
+ for (Node node = i.nextNode(); node != null; node = i.nextNode())
+ {
+ results.add(node);
+ }
+ }
+
+ // -- HTMLCollection/NodeList interface --
+
+ public int getLength()
+ {
+ return results.size();
+ }
+
+ public void setLength(int length)
+ {
+ throw new DomDOMException(DOMException.NOT_SUPPORTED_ERR);
+ }
+
+ public Node item(int index)
+ {
+ return (Node) results.get(index);
+ }
+
+ public Node namedItem(String name)
+ {
+ boolean xhtml = false; // FIXME detect XHTML document
+ for (Iterator i = results.iterator(); i.hasNext(); )
+ {
+ Node node = (Node) i.next();
+ NamedNodeMap attrs = node.getAttributes();
+ Node attr = getNamedItem(attrs, "id");
+ if (name.equals(attr.getTextContent()))
+ {
+ return node;
+ }
+ if (!xhtml)
+ {
+ attr = getNamedItem(attrs, "name");
+ if (name.equals(attr.getTextContent()))
+ {
+ return node;
+ }
+ }
+ }
+ return null;
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/dom/html2/DomHTMLDListElement.java b/libjava/classpath/gnu/xml/dom/html2/DomHTMLDListElement.java
new file mode 100644
index 000000000..9acce856a
--- /dev/null
+++ b/libjava/classpath/gnu/xml/dom/html2/DomHTMLDListElement.java
@@ -0,0 +1,68 @@
+/* DomHTMLDListElement.java --
+ Copyright (C) 2005 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.dom.html2;
+
+import org.w3c.dom.html2.HTMLDListElement;
+
+/**
+ * An HTML 'DL' element node.
+ *
+ * @author Chris Burdess
+ */
+public class DomHTMLDListElement
+ extends DomHTMLElement
+ implements HTMLDListElement
+{
+
+ protected DomHTMLDListElement(DomHTMLDocument owner, String namespaceURI,
+ String name)
+ {
+ super(owner, namespaceURI, name);
+ }
+
+ public boolean getCompact()
+ {
+ return getBooleanHTMLAttribute("compact");
+ }
+
+ public void setCompact(boolean compact)
+ {
+ setBooleanHTMLAttribute("compact", compact);
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/dom/html2/DomHTMLDirectoryElement.java b/libjava/classpath/gnu/xml/dom/html2/DomHTMLDirectoryElement.java
new file mode 100644
index 000000000..e0f94a1ed
--- /dev/null
+++ b/libjava/classpath/gnu/xml/dom/html2/DomHTMLDirectoryElement.java
@@ -0,0 +1,68 @@
+/* DomHTMLDirectoryElement.java --
+ Copyright (C) 2005 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.dom.html2;
+
+import org.w3c.dom.html2.HTMLDirectoryElement;
+
+/**
+ * An HTML 'DIR' element node.
+ *
+ * @author Chris Burdess
+ */
+public class DomHTMLDirectoryElement
+ extends DomHTMLElement
+ implements HTMLDirectoryElement
+{
+
+ protected DomHTMLDirectoryElement(DomHTMLDocument owner, String namespaceURI,
+ String name)
+ {
+ super(owner, namespaceURI, name);
+ }
+
+ public boolean getCompact()
+ {
+ return getBooleanHTMLAttribute("compact");
+ }
+
+ public void setCompact(boolean compact)
+ {
+ setBooleanHTMLAttribute("compact", compact);
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/dom/html2/DomHTMLDivElement.java b/libjava/classpath/gnu/xml/dom/html2/DomHTMLDivElement.java
new file mode 100644
index 000000000..66b3ac743
--- /dev/null
+++ b/libjava/classpath/gnu/xml/dom/html2/DomHTMLDivElement.java
@@ -0,0 +1,68 @@
+/* DomHTMLDivElement.java --
+ Copyright (C) 2005 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.dom.html2;
+
+import org.w3c.dom.html2.HTMLDivElement;
+
+/**
+ * An HTML 'DIV' element node.
+ *
+ * @author Chris Burdess
+ */
+public class DomHTMLDivElement
+ extends DomHTMLElement
+ implements HTMLDivElement
+{
+
+ protected DomHTMLDivElement(DomHTMLDocument owner, String namespaceURI,
+ String name)
+ {
+ super(owner, namespaceURI, name);
+ }
+
+ public String getAlign()
+ {
+ return getHTMLAttribute("align");
+ }
+
+ public void setAlign(String align)
+ {
+ setHTMLAttribute("align", align);
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/dom/html2/DomHTMLDocument.java b/libjava/classpath/gnu/xml/dom/html2/DomHTMLDocument.java
new file mode 100644
index 000000000..1afed7beb
--- /dev/null
+++ b/libjava/classpath/gnu/xml/dom/html2/DomHTMLDocument.java
@@ -0,0 +1,426 @@
+/* DomHTMLDocument.java --
+ Copyright (C) 2005 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.dom.html2;
+
+import gnu.xml.dom.DomDocument;
+import gnu.xml.dom.DomDOMException;
+import java.lang.reflect.Constructor;
+import java.net.MalformedURLException;
+import java.net.URL;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Map;
+import java.util.Set;
+import org.w3c.dom.DOMException;
+import org.w3c.dom.Element;
+import org.w3c.dom.Node;
+import org.w3c.dom.NodeList;
+import org.w3c.dom.html2.HTMLCollection;
+import org.w3c.dom.html2.HTMLDocument;
+import org.w3c.dom.html2.HTMLElement;
+
+/**
+ * An HTML document.
+ * This is the factory object used to create HTML elements.
+ *
+ * @author Chris Burdess
+ */
+public class DomHTMLDocument
+ extends DomDocument
+ implements HTMLDocument
+{
+
+ private static final Class[] ELEMENT_PT = new Class[] {
+ DomHTMLDocument.class,
+ String.class,
+ String.class
+ };
+
+ private static Map ELEMENT_CLASSES;
+ static
+ {
+ Map map = new HashMap();
+ map.put("a", DomHTMLAnchorElement.class);
+ map.put("applet", DomHTMLAppletElement.class);
+ map.put("area", DomHTMLAreaElement.class);
+ map.put("base", DomHTMLBaseElement.class);
+ map.put("basefont", DomHTMLBaseFontElement.class);
+ map.put("body", DomHTMLBodyElement.class);
+ map.put("br", DomHTMLBRElement.class);
+ map.put("button", DomHTMLButtonElement.class);
+ map.put("dir", DomHTMLDirectoryElement.class);
+ map.put("div", DomHTMLDivElement.class);
+ map.put("dlist", DomHTMLDListElement.class);
+ map.put("embed", DomHTMLEmbedElement.class);
+ map.put("fieldset", DomHTMLFieldSetElement.class);
+ map.put("font", DomHTMLFontElement.class);
+ map.put("form", DomHTMLFormElement.class);
+ map.put("frame", DomHTMLFrameElement.class);
+ map.put("frameset", DomHTMLFrameSetElement.class);
+ map.put("head", DomHTMLHeadElement.class);
+ map.put("h1", DomHTMLHeadingElement.class);
+ map.put("h2", DomHTMLHeadingElement.class);
+ map.put("h3", DomHTMLHeadingElement.class);
+ map.put("h4", DomHTMLHeadingElement.class);
+ map.put("h5", DomHTMLHeadingElement.class);
+ map.put("h6", DomHTMLHeadingElement.class);
+ map.put("html", DomHTMLHtmlElement.class);
+ map.put("iframe", DomHTMLIFrameElement.class);
+ map.put("img", DomHTMLImageElement.class);
+ map.put("input", DomHTMLInputElement.class);
+ map.put("isindex", DomHTMLIsIndexElement.class);
+ map.put("label", DomHTMLLabelElement.class);
+ map.put("legend", DomHTMLLegendElement.class);
+ map.put("li", DomHTMLLIElement.class);
+ map.put("link", DomHTMLLinkElement.class);
+ map.put("map", DomHTMLMapElement.class);
+ map.put("menu", DomHTMLMenuElement.class);
+ map.put("meta", DomHTMLMetaElement.class);
+ map.put("ins", DomHTMLModElement.class);
+ map.put("del", DomHTMLModElement.class);
+ map.put("object", DomHTMLObjectElement.class);
+ map.put("ol", DomHTMLOListElement.class);
+ map.put("optgroup", DomHTMLOptGroupElement.class);
+ map.put("option", DomHTMLOptionElement.class);
+ map.put("p", DomHTMLParagraphElement.class);
+ map.put("param", DomHTMLParamElement.class);
+ map.put("pre", DomHTMLPreElement.class);
+ map.put("q", DomHTMLQuoteElement.class);
+ map.put("blockquote", DomHTMLQuoteElement.class);
+ map.put("script", DomHTMLScriptElement.class);
+ map.put("select", DomHTMLSelectElement.class);
+ map.put("style", DomHTMLStyleElement.class);
+ map.put("caption", DomHTMLTableCaptionElement.class);
+ map.put("th", DomHTMLTableCellElement.class);
+ map.put("td", DomHTMLTableCellElement.class);
+ map.put("col", DomHTMLTableColElement.class);
+ map.put("colgroup", DomHTMLTableColElement.class);
+ map.put("table", DomHTMLTableElement.class);
+ map.put("tr", DomHTMLTableRowElement.class);
+ map.put("thead", DomHTMLTableSectionElement.class);
+ map.put("tfoot", DomHTMLTableSectionElement.class);
+ map.put("tbody", DomHTMLTableSectionElement.class);
+ map.put("textarea", DomHTMLTextAreaElement.class);
+ map.put("title", DomHTMLTitleElement.class);
+ map.put("ul", DomHTMLUListElement.class);
+ ELEMENT_CLASSES = Collections.unmodifiableMap(map);
+ }
+
+ private static Set HTML_NS_URIS;
+ static
+ {
+ Set set = new HashSet();
+ set.add("http://www.w3.org/TR/html4/strict");
+ set.add("http://www.w3.org/TR/html4/loose");
+ set.add("http://www.w3.org/TR/html4/frameset");
+ set.add("http://www.w3.org/1999/xhtml");
+ set.add("http://www.w3.org/TR/xhtml1/strict");
+ set.add("http://www.w3.org/TR/xhtml1/loose");
+ set.add("http://www.w3.org/TR/xhtml1/frameset");
+ HTML_NS_URIS = Collections.unmodifiableSet(set);
+ }
+
+ /**
+ * Convenience constructor.
+ */
+ public DomHTMLDocument()
+ {
+ this(new DomHTMLImpl());
+ }
+
+ /**
+ * Constructor.
+ * This is called by the DOMImplementation.
+ */
+ public DomHTMLDocument(DomHTMLImpl impl)
+ {
+ super(impl);
+ }
+
+ private Node getChildNodeByName(Node parent, String name)
+ {
+ for (Node ctx = parent.getFirstChild(); ctx != null;
+ ctx = ctx.getNextSibling())
+ {
+ if (name.equalsIgnoreCase(ctx.getNodeName()))
+ {
+ return ctx;
+ }
+ }
+ return null;
+ }
+
+ public String getTitle()
+ {
+ Node html = getDocumentElement();
+ if (html != null)
+ {
+ Node head = getChildNodeByName(html, "head");
+ if (head != null)
+ {
+ Node title = getChildNodeByName(head, "title");
+ if (title != null)
+ {
+ return title.getTextContent();
+ }
+ }
+ }
+ return null;
+ }
+
+ public void setTitle(String title)
+ {
+ Node html = getDocumentElement();
+ if (html == null)
+ {
+ html = createElement("html");
+ appendChild(html);
+ }
+ Node head = getChildNodeByName(html, "head");
+ if (head == null)
+ {
+ head = createElement("head");
+ Node first = html.getFirstChild();
+ if (first != null)
+ {
+ html.insertBefore(first, head);
+ }
+ else
+ {
+ html.appendChild(head);
+ }
+ }
+ Node titleNode = getChildNodeByName(head, "title");
+ if (titleNode == null)
+ {
+ titleNode = createElement("title");
+ Node first = head.getFirstChild();
+ if (first != null)
+ {
+ head.insertBefore(first, titleNode);
+ }
+ else
+ {
+ head.appendChild(titleNode);
+ }
+ }
+ titleNode.setTextContent(title);
+ }
+
+ public String getReferrer()
+ {
+ // TODO getReferrer
+ return null;
+ }
+
+ public String getDomain()
+ {
+ try
+ {
+ URL url = new URL(getDocumentURI());
+ return url.getHost();
+ }
+ catch (MalformedURLException e)
+ {
+ return null;
+ }
+ }
+
+ public String getURL()
+ {
+ return getDocumentURI();
+ }
+
+ public HTMLElement getBody()
+ {
+ Node html = getDocumentElement();
+ if (html != null)
+ {
+ Node body = getChildNodeByName(html, "body");
+ if (body == null)
+ {
+ body = getChildNodeByName(html, "frameset");
+ }
+ return (HTMLElement) body;
+ }
+ return null;
+ }
+
+ public void setBody(HTMLElement body)
+ {
+ Node html = getDocumentElement();
+ if (html == null)
+ {
+ html = createElement("html");
+ appendChild(html);
+ }
+ Node ref = getBody();
+ if (ref == null)
+ {
+ html.appendChild(body);
+ }
+ else
+ {
+ html.replaceChild(body, ref);
+ }
+ }
+
+ public HTMLCollection getImages()
+ {
+ DomHTMLCollection ret = new DomHTMLCollection(this, this);
+ ret.addNodeName("img");
+ ret.evaluate();
+ return ret;
+ }
+
+ public HTMLCollection getApplets()
+ {
+ DomHTMLCollection ret = new DomHTMLCollection(this, this);
+ ret.addNodeName("embed");
+ ret.addNodeName("object");
+ ret.addNodeName("applet");
+ ret.evaluate();
+ return ret;
+ }
+
+ public HTMLCollection getLinks()
+ {
+ DomHTMLCollection ret = new DomHTMLCollection(this, this);
+ ret.addNodeName("area");
+ ret.addNodeName("a");
+ ret.evaluate();
+ return ret;
+ }
+
+ public HTMLCollection getForms()
+ {
+ DomHTMLCollection ret = new DomHTMLCollection(this, this);
+ ret.addNodeName("form");
+ ret.evaluate();
+ return ret;
+ }
+
+ public HTMLCollection getAnchors()
+ {
+ DomHTMLCollection ret = new DomHTMLCollection(this, this);
+ ret.addNodeName("a");
+ ret.addAttributeName("name");
+ ret.evaluate();
+ return ret;
+ }
+
+ public String getCookie()
+ {
+ // TODO getCookie
+ return null;
+ }
+
+ public void setCookie(String cookie)
+ {
+ // TODO setCookie
+ }
+
+ public void open()
+ {
+ // TODO open
+ }
+
+ public void close()
+ {
+ // TODO close
+ }
+
+ public void write(String text)
+ {
+ // TODO write
+ }
+
+ public void writeln(String text)
+ {
+ // TODO write
+ }
+
+ public NodeList getElementsByName(String name)
+ {
+ DomHTMLCollection ret = new DomHTMLCollection(this, this);
+ ret.addNodeName(name);
+ ret.evaluate();
+ return ret;
+ // TODO xhtml: return only form controls (?)
+ }
+
+ public Element createElement(String tagName)
+ {
+ return createElementNS(null, tagName);
+ }
+
+ public Element createElementNS(String uri, String qName)
+ {
+ /* If a non-HTML element, use the default implementation. */
+ if (uri != null && !HTML_NS_URIS.contains(uri))
+ {
+ return super.createElementNS(uri, qName);
+ }
+ String localName = qName.toLowerCase();
+ int ci = qName.indexOf(':');
+ if (ci != -1)
+ {
+ localName = qName.substring(ci + 1);
+ }
+ Class t = (Class) ELEMENT_CLASSES.get(localName);
+ /* If a non-HTML element, use the default implementation. */
+ if (t == null)
+ {
+ return super.createElementNS(uri, qName);
+ }
+ try
+ {
+ Constructor c = t.getDeclaredConstructor(ELEMENT_PT);
+ Object[] args = new Object[] { this, uri, qName };
+ return (Element) c.newInstance(args);
+ }
+ catch (Exception e)
+ {
+ DOMException e2 = new DomDOMException(DOMException.TYPE_MISMATCH_ERR);
+ e2.initCause(e);
+ throw e2;
+ }
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/dom/html2/DomHTMLElement.java b/libjava/classpath/gnu/xml/dom/html2/DomHTMLElement.java
new file mode 100644
index 000000000..deeefec60
--- /dev/null
+++ b/libjava/classpath/gnu/xml/dom/html2/DomHTMLElement.java
@@ -0,0 +1,286 @@
+/* DomHTMLElement.java --
+ Copyright (C) 2005 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.dom.html2;
+
+import gnu.xml.dom.DomDOMException;
+import gnu.xml.dom.DomElement;
+import gnu.xml.dom.DomEvent;
+import org.w3c.dom.DOMException;
+import org.w3c.dom.NamedNodeMap;
+import org.w3c.dom.Node;
+import org.w3c.dom.events.UIEvent;
+import org.w3c.dom.html2.HTMLElement;
+
+/**
+ * Abstract implementation of an HTML element node.
+ *
+ * @author Chris Burdess
+ */
+public abstract class DomHTMLElement
+ extends DomElement
+ implements HTMLElement
+{
+
+ protected DomHTMLElement(DomHTMLDocument owner, String namespaceURI,
+ String name)
+ {
+ super(owner, namespaceURI, name);
+ }
+
+ /**
+ * Returns the value of the specified attribute.
+ * The attribute name is case insensitive.
+ */
+ protected String getHTMLAttribute(String name)
+ {
+ if (hasAttributes())
+ {
+ NamedNodeMap attrs = getAttributes();
+ int len = attrs.getLength();
+ for (int i = 0; i < len; i++)
+ {
+ Node attr = attrs.item(i);
+ String attrName = attr.getLocalName();
+ if (attrName == null)
+ {
+ attrName = attr.getNodeName();
+ }
+ if (attrName.equalsIgnoreCase(name))
+ {
+ return attr.getNodeValue();
+ }
+ }
+ }
+ return "";
+ }
+
+ protected int getIntHTMLAttribute(String name)
+ {
+ String value = getHTMLAttribute(name);
+ if (value == null)
+ {
+ return -1;
+ }
+ try
+ {
+ return Integer.parseInt(value);
+ }
+ catch (NumberFormatException e)
+ {
+ return -1;
+ }
+ }
+
+ protected boolean getBooleanHTMLAttribute(String name)
+ {
+ String value = getHTMLAttribute(name);
+ return value != null;
+ }
+
+ /**
+ * Sets the value of the specified attribute.
+ * The attribute name is case insensitive.
+ */
+ protected void setHTMLAttribute(String name, String value)
+ {
+ Node attr;
+ NamedNodeMap attrs = getAttributes();
+ int len = attrs.getLength();
+ for (int i = 0; i < len; i++)
+ {
+ attr = attrs.item(i);
+ String attrName = attr.getLocalName();
+ if (attrName == null)
+ {
+ attrName = attr.getNodeName();
+ }
+ if (attrName.equalsIgnoreCase(name))
+ {
+ if (value != null)
+ {
+ attr.setNodeValue(value);
+ }
+ else
+ {
+ attrs.removeNamedItem(attr.getNodeName());
+ }
+ return;
+ }
+ }
+ if (value != null)
+ {
+ // Create a new attribute
+ DomHTMLDocument doc = (DomHTMLDocument) getOwnerDocument();
+ // XXX namespace URI for attribute?
+ attr = doc.createAttribute(name);
+ attr.setNodeValue(value);
+ }
+ }
+
+ protected void setIntHTMLAttribute(String name, int value)
+ {
+ setHTMLAttribute(name, Integer.toString(value));
+ }
+
+ protected void setBooleanHTMLAttribute(String name, boolean value)
+ {
+ setHTMLAttribute(name, value ? name : null);
+ }
+
+ /**
+ * Returns the first parent element with the specified name.
+ */
+ protected Node getParentElement(String name)
+ {
+ for (Node parent = getParentNode(); parent != null;
+ parent = parent.getParentNode())
+ {
+ String parentName = parent.getLocalName();
+ if (parentName == null)
+ {
+ parentName = parent.getNodeName();
+ }
+ if (name.equalsIgnoreCase(parentName))
+ {
+ return parent;
+ }
+ }
+ return null;
+ }
+
+ /**
+ * Returns the first child element with the specified name.
+ */
+ protected Node getChildElement(String name)
+ {
+ for (Node child = getFirstChild(); child != null;
+ child = child.getNextSibling())
+ {
+ String childName = child.getLocalName();
+ if (childName == null)
+ {
+ childName = child.getLocalName();
+ }
+ if (name.equalsIgnoreCase(childName))
+ {
+ return child;
+ }
+ }
+ return null;
+ }
+
+ /**
+ * Returns the index of this element among elements of the same name,
+ * relative to its parent.
+ */
+ protected int getIndex()
+ {
+ int index = 0;
+ Node parent = getParentNode();
+ if (parent != null)
+ {
+ for (Node ctx = parent.getFirstChild(); ctx != null;
+ ctx = ctx.getNextSibling())
+ {
+ if (ctx == this)
+ {
+ return index;
+ }
+ index++;
+ }
+ }
+ throw new DomDOMException(DOMException.NOT_FOUND_ERR);
+ }
+
+ protected void dispatchUIEvent(String name)
+ {
+ UIEvent event = new DomEvent.DomUIEvent(name);
+ dispatchEvent(event);
+ }
+
+ public String getId()
+ {
+ return getHTMLAttribute("id");
+ }
+
+ public void setId(String id)
+ {
+ setHTMLAttribute("id", id);
+ }
+
+ public String getTitle()
+ {
+ return getHTMLAttribute("title");
+ }
+
+ public void setTitle(String title)
+ {
+ setHTMLAttribute("title", title);
+ }
+
+ public String getLang()
+ {
+ return getHTMLAttribute("lang");
+ }
+
+ public void setLang(String lang)
+ {
+ setHTMLAttribute("lang", lang);
+ }
+
+ public String getDir()
+ {
+ return getHTMLAttribute("dir");
+ }
+
+ public void setDir(String dir)
+ {
+ setHTMLAttribute("dir", dir);
+ }
+
+ public String getClassName()
+ {
+ return getHTMLAttribute("class");
+ }
+
+ public void setClassName(String className)
+ {
+ setHTMLAttribute("class", className);
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/dom/html2/DomHTMLEmbedElement.java b/libjava/classpath/gnu/xml/dom/html2/DomHTMLEmbedElement.java
new file mode 100644
index 000000000..221131533
--- /dev/null
+++ b/libjava/classpath/gnu/xml/dom/html2/DomHTMLEmbedElement.java
@@ -0,0 +1,129 @@
+/* DomHTMLEmbedElement.java --
+ Copyright (C) 2006 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+
+package gnu.xml.dom.html2;
+
+public class DomHTMLEmbedElement
+ extends DomHTMLAppletElement
+{
+ protected DomHTMLEmbedElement(DomHTMLDocument owner, String namespaceURI,
+ String name)
+ {
+ super(owner, namespaceURI, name);
+ }
+
+ public String getJavaObject()
+ {
+ return getHTMLAttribute("java_object");
+ }
+
+ public void setJavaObject(String object)
+ {
+ setHTMLAttribute("java_object", object);
+ }
+
+ public String getJavaCodeBase()
+ {
+ return getHTMLAttribute("java_codebase");
+ }
+
+ public void setJavaCodeBase(String codeBase)
+ {
+ setHTMLAttribute("java_codebase", codeBase);
+ }
+
+ public String getJavaArchive()
+ {
+ return getHTMLAttribute("java_archive");
+ }
+
+ public void setJavaArchive(String archive)
+ {
+ setHTMLAttribute("java_archive", archive);
+ }
+
+ public void setJavaCode(String code)
+ {
+ setHTMLAttribute("java_code", code);
+ }
+
+ public String getJavaCode()
+ {
+ return getHTMLAttribute("java_code");
+ }
+
+ public void setJavaType(String type)
+ {
+ setHTMLAttribute("java_type", type);
+ }
+
+ public String getJavaType()
+ {
+ return getHTMLAttribute("java_type");
+ }
+
+ public void setType(String type)
+ {
+ setHTMLAttribute("type", type);
+ }
+
+ public String getType()
+ {
+ return getHTMLAttribute("type");
+ }
+
+ public String getPluginsPage()
+ {
+ return getHTMLAttribute("pluginspage");
+ }
+
+ public void setPluginsPage(String pluginspage)
+ {
+ setHTMLAttribute("pluginspage", pluginspage);
+ }
+
+ public String getMayscript()
+ {
+ return getHTMLAttribute("mayscript");
+ }
+
+ public void setMayscript(String mayscript)
+ {
+ setHTMLAttribute("mayscript", mayscript);
+ }
+}
diff --git a/libjava/classpath/gnu/xml/dom/html2/DomHTMLFieldSetElement.java b/libjava/classpath/gnu/xml/dom/html2/DomHTMLFieldSetElement.java
new file mode 100644
index 000000000..252cd3d2b
--- /dev/null
+++ b/libjava/classpath/gnu/xml/dom/html2/DomHTMLFieldSetElement.java
@@ -0,0 +1,64 @@
+/* DomHTMLFieldSetElement.java --
+ Copyright (C) 2005 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.dom.html2;
+
+import org.w3c.dom.html2.HTMLFieldSetElement;
+import org.w3c.dom.html2.HTMLFormElement;
+
+/**
+ * An HTML 'FIELDSET' element node.
+ *
+ * @author Chris Burdess
+ */
+public class DomHTMLFieldSetElement
+ extends DomHTMLElement
+ implements HTMLFieldSetElement
+{
+
+ protected DomHTMLFieldSetElement(DomHTMLDocument owner, String namespaceURI,
+ String name)
+ {
+ super(owner, namespaceURI, name);
+ }
+
+ public HTMLFormElement getForm()
+ {
+ return (HTMLFormElement) getParentElement("form");
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/dom/html2/DomHTMLFontElement.java b/libjava/classpath/gnu/xml/dom/html2/DomHTMLFontElement.java
new file mode 100644
index 000000000..5bfbb6f36
--- /dev/null
+++ b/libjava/classpath/gnu/xml/dom/html2/DomHTMLFontElement.java
@@ -0,0 +1,88 @@
+/* DomHTMLFontElement.java --
+ Copyright (C) 2005 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.dom.html2;
+
+import org.w3c.dom.html2.HTMLFontElement;
+
+/**
+ * An HTML 'FONT' element node.
+ *
+ * @author Chris Burdess
+ */
+public class DomHTMLFontElement
+ extends DomHTMLElement
+ implements HTMLFontElement
+{
+
+ protected DomHTMLFontElement(DomHTMLDocument owner, String namespaceURI,
+ String name)
+ {
+ super(owner, namespaceURI, name);
+ }
+
+ public String getColor()
+ {
+ return getHTMLAttribute("color");
+ }
+
+ public void setColor(String color)
+ {
+ setHTMLAttribute("color", color);
+ }
+
+ public String getFace()
+ {
+ return getHTMLAttribute("face");
+ }
+
+ public void setFace(String face)
+ {
+ setHTMLAttribute("face", face);
+ }
+
+ public String getSize()
+ {
+ return getHTMLAttribute("size");
+ }
+
+ public void setSize(String size)
+ {
+ setHTMLAttribute("size", size);
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/dom/html2/DomHTMLFormElement.java b/libjava/classpath/gnu/xml/dom/html2/DomHTMLFormElement.java
new file mode 100644
index 000000000..9fe2ae802
--- /dev/null
+++ b/libjava/classpath/gnu/xml/dom/html2/DomHTMLFormElement.java
@@ -0,0 +1,149 @@
+/* DomHTMLFormElement.java --
+ Copyright (C) 2005 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.dom.html2;
+
+import org.w3c.dom.html2.HTMLCollection;
+import org.w3c.dom.html2.HTMLFormElement;
+
+/**
+ * An HTML 'FORM' element node.
+ *
+ * @author Chris Burdess
+ */
+public class DomHTMLFormElement
+ extends DomHTMLElement
+ implements HTMLFormElement
+{
+
+ protected DomHTMLFormElement(DomHTMLDocument owner, String namespaceURI,
+ String name)
+ {
+ super(owner, namespaceURI, name);
+ }
+
+ public HTMLCollection getElements()
+ {
+ DomHTMLCollection ret =
+ new DomHTMLCollection((DomHTMLDocument) getOwnerDocument(), this);
+ ret.addNodeName("input");
+ ret.addNodeName("button");
+ ret.addNodeName("select");
+ ret.addNodeName("textarea");
+ ret.addNodeName("isindex");
+ ret.addNodeName("label");
+ ret.addNodeName("option");
+ ret.evaluate();
+ return ret;
+ }
+
+ public int getLength()
+ {
+ return getElements().getLength();
+ }
+
+ public String getName()
+ {
+ return getHTMLAttribute("name");
+ }
+
+ public void setName(String name)
+ {
+ setHTMLAttribute("name", name);
+ }
+
+ public String getAcceptCharset()
+ {
+ return getHTMLAttribute("accept-charset");
+ }
+
+ public void setAcceptCharset(String acceptCharset)
+ {
+ setHTMLAttribute("accept-charset", acceptCharset);
+ }
+
+ public String getAction()
+ {
+ return getHTMLAttribute("action");
+ }
+
+ public void setAction(String action)
+ {
+ setHTMLAttribute("action", action);
+ }
+
+ public String getEnctype()
+ {
+ return getHTMLAttribute("enctype");
+ }
+
+ public void setEnctype(String enctype)
+ {
+ setHTMLAttribute("enctype", enctype);
+ }
+
+ public String getMethod()
+ {
+ return getHTMLAttribute("method");
+ }
+
+ public void setMethod(String method)
+ {
+ setHTMLAttribute("method", method);
+ }
+
+ public String getTarget()
+ {
+ return getHTMLAttribute("target");
+ }
+
+ public void setTarget(String target)
+ {
+ setHTMLAttribute("target", target);
+ }
+
+ public void submit()
+ {
+ dispatchUIEvent("submit");
+ }
+
+ public void reset()
+ {
+ dispatchUIEvent("reset");
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/dom/html2/DomHTMLFrameElement.java b/libjava/classpath/gnu/xml/dom/html2/DomHTMLFrameElement.java
new file mode 100644
index 000000000..2465c4b89
--- /dev/null
+++ b/libjava/classpath/gnu/xml/dom/html2/DomHTMLFrameElement.java
@@ -0,0 +1,145 @@
+/* DomHTMLFrameElement.java --
+ Copyright (C) 2005 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.dom.html2;
+
+import org.w3c.dom.Document;
+import org.w3c.dom.html2.HTMLFrameElement;
+
+/**
+ * An HTML 'FRAME' element node.
+ *
+ * @author Chris Burdess
+ */
+public class DomHTMLFrameElement
+ extends DomHTMLElement
+ implements HTMLFrameElement
+{
+
+ protected DomHTMLFrameElement(DomHTMLDocument owner, String namespaceURI,
+ String name)
+ {
+ super(owner, namespaceURI, name);
+ }
+
+ public String getFrameBorder()
+ {
+ return getHTMLAttribute("frameborder");
+ }
+
+ public void setFrameBorder(String frameBorder)
+ {
+ setHTMLAttribute("frameborder", frameBorder);
+ }
+
+ public String getLongDesc()
+ {
+ return getHTMLAttribute("longdesc");
+ }
+
+ public void setLongDesc(String longDesc)
+ {
+ setHTMLAttribute("longdesc", longDesc);
+ }
+
+ public String getMarginHeight()
+ {
+ return getHTMLAttribute("marginheight");
+ }
+
+ public void setMarginHeight(String marginHeight)
+ {
+ setHTMLAttribute("marginheight", marginHeight);
+ }
+
+ public String getMarginWidth()
+ {
+ return getHTMLAttribute("marginwidth");
+ }
+
+ public void setMarginWidth(String marginWidth)
+ {
+ setHTMLAttribute("marginwidth", marginWidth);
+ }
+
+ public String getName()
+ {
+ return getHTMLAttribute("name");
+ }
+
+ public void setName(String name)
+ {
+ setHTMLAttribute("name", name);
+ }
+
+ public boolean getNoResize()
+ {
+ return getBooleanHTMLAttribute("noresize");
+ }
+
+ public void setNoResize(boolean noResize)
+ {
+ setBooleanHTMLAttribute("noresize", noResize);
+ }
+
+ public String getScrolling()
+ {
+ return getHTMLAttribute("scrolling");
+ }
+
+ public void setScrolling(String scrolling)
+ {
+ setHTMLAttribute("scrolling", scrolling);
+ }
+
+ public String getSrc()
+ {
+ return getHTMLAttribute("src");
+ }
+
+ public void setSrc(String src)
+ {
+ setHTMLAttribute("src", src);
+ }
+
+ public Document getContentDocument()
+ {
+ // TODO getContentDocument
+ return null;
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/dom/html2/DomHTMLFrameSetElement.java b/libjava/classpath/gnu/xml/dom/html2/DomHTMLFrameSetElement.java
new file mode 100644
index 000000000..dae9430fb
--- /dev/null
+++ b/libjava/classpath/gnu/xml/dom/html2/DomHTMLFrameSetElement.java
@@ -0,0 +1,78 @@
+/* DomHTMLFrameSetElement.java --
+ Copyright (C) 2005 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.dom.html2;
+
+import org.w3c.dom.html2.HTMLFrameSetElement;
+
+/**
+ * An HTML 'FRAMESET' element node.
+ *
+ * @author Chris Burdess
+ */
+public class DomHTMLFrameSetElement
+ extends DomHTMLElement
+ implements HTMLFrameSetElement
+{
+
+ protected DomHTMLFrameSetElement(DomHTMLDocument owner, String namespaceURI,
+ String name)
+ {
+ super(owner, namespaceURI, name);
+ }
+
+ public String getCols()
+ {
+ return getHTMLAttribute("cols");
+ }
+
+ public void setCols(String cols)
+ {
+ setHTMLAttribute("cols", cols);
+ }
+
+ public String getRows()
+ {
+ return getHTMLAttribute("rows");
+ }
+
+ public void setRows(String rows)
+ {
+ setHTMLAttribute("rows", rows);
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/dom/html2/DomHTMLHRElement.java b/libjava/classpath/gnu/xml/dom/html2/DomHTMLHRElement.java
new file mode 100644
index 000000000..e27ef5595
--- /dev/null
+++ b/libjava/classpath/gnu/xml/dom/html2/DomHTMLHRElement.java
@@ -0,0 +1,98 @@
+/* DomHTMLHRElement.java --
+ Copyright (C) 2005 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.dom.html2;
+
+import org.w3c.dom.html2.HTMLHRElement;
+
+/**
+ * An HTML 'HR' element node.
+ *
+ * @author Chris Burdess
+ */
+public class DomHTMLHRElement
+ extends DomHTMLElement
+ implements HTMLHRElement
+{
+
+ protected DomHTMLHRElement(DomHTMLDocument owner, String namespaceURI,
+ String name)
+ {
+ super(owner, namespaceURI, name);
+ }
+
+ public String getAlign()
+ {
+ return getHTMLAttribute("align");
+ }
+
+ public void setAlign(String align)
+ {
+ setHTMLAttribute("align", align);
+ }
+
+ public boolean getNoShade()
+ {
+ return getBooleanHTMLAttribute("noshade");
+ }
+
+ public void setNoShade(boolean noShade)
+ {
+ setBooleanHTMLAttribute("noshade", noShade);
+ }
+
+ public String getSize()
+ {
+ return getHTMLAttribute("size");
+ }
+
+ public void setSize(String size)
+ {
+ setHTMLAttribute("size", size);
+ }
+
+ public String getWidth()
+ {
+ return getHTMLAttribute("width");
+ }
+
+ public void setWidth(String width)
+ {
+ setHTMLAttribute("width", width);
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/dom/html2/DomHTMLHeadElement.java b/libjava/classpath/gnu/xml/dom/html2/DomHTMLHeadElement.java
new file mode 100644
index 000000000..4a8ef995b
--- /dev/null
+++ b/libjava/classpath/gnu/xml/dom/html2/DomHTMLHeadElement.java
@@ -0,0 +1,68 @@
+/* DomHTMLHeadElement.java --
+ Copyright (C) 2005 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.dom.html2;
+
+import org.w3c.dom.html2.HTMLHeadElement;
+
+/**
+ * An HTML 'HEAD' element node.
+ *
+ * @author Chris Burdess
+ */
+public class DomHTMLHeadElement
+ extends DomHTMLElement
+ implements HTMLHeadElement
+{
+
+ protected DomHTMLHeadElement(DomHTMLDocument owner, String namespaceURI,
+ String name)
+ {
+ super(owner, namespaceURI, name);
+ }
+
+ public String getProfile()
+ {
+ return getHTMLAttribute("profile");
+ }
+
+ public void setProfile(String profile)
+ {
+ setHTMLAttribute("profile", profile);
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/dom/html2/DomHTMLHeadingElement.java b/libjava/classpath/gnu/xml/dom/html2/DomHTMLHeadingElement.java
new file mode 100644
index 000000000..6e78e9004
--- /dev/null
+++ b/libjava/classpath/gnu/xml/dom/html2/DomHTMLHeadingElement.java
@@ -0,0 +1,68 @@
+/* DomHTMLHeadingElement.java --
+ Copyright (C) 2005 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.dom.html2;
+
+import org.w3c.dom.html2.HTMLHeadingElement;
+
+/**
+ * An HTML 'H1', 'H2', 'H3', 'H4', 'H5', or 'H6' element node.
+ *
+ * @author Chris Burdess
+ */
+public class DomHTMLHeadingElement
+ extends DomHTMLElement
+ implements HTMLHeadingElement
+{
+
+ protected DomHTMLHeadingElement(DomHTMLDocument owner, String namespaceURI,
+ String name)
+ {
+ super(owner, namespaceURI, name);
+ }
+
+ public String getAlign()
+ {
+ return getHTMLAttribute("align");
+ }
+
+ public void setAlign(String align)
+ {
+ setHTMLAttribute("align", align);
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/dom/html2/DomHTMLHtmlElement.java b/libjava/classpath/gnu/xml/dom/html2/DomHTMLHtmlElement.java
new file mode 100644
index 000000000..73f7243ed
--- /dev/null
+++ b/libjava/classpath/gnu/xml/dom/html2/DomHTMLHtmlElement.java
@@ -0,0 +1,68 @@
+/* DomHTMLHtmlElement.java --
+ Copyright (C) 2005 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.dom.html2;
+
+import org.w3c.dom.html2.HTMLHtmlElement;
+
+/**
+ * An HTML 'HTML' top-level element node.
+ *
+ * @author Chris Burdess
+ */
+public class DomHTMLHtmlElement
+ extends DomHTMLElement
+ implements HTMLHtmlElement
+{
+
+ protected DomHTMLHtmlElement(DomHTMLDocument owner, String namespaceURI,
+ String name)
+ {
+ super(owner, namespaceURI, name);
+ }
+
+ public String getVersion()
+ {
+ return getHTMLAttribute("version");
+ }
+
+ public void setVersion(String version)
+ {
+ setHTMLAttribute("version", version);
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/dom/html2/DomHTMLIFrameElement.java b/libjava/classpath/gnu/xml/dom/html2/DomHTMLIFrameElement.java
new file mode 100644
index 000000000..1a58fb73e
--- /dev/null
+++ b/libjava/classpath/gnu/xml/dom/html2/DomHTMLIFrameElement.java
@@ -0,0 +1,165 @@
+/* DomHTMLIFrameElement.java --
+ Copyright (C) 2005 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.dom.html2;
+
+import org.w3c.dom.Document;
+import org.w3c.dom.html2.HTMLIFrameElement;
+
+/**
+ * An HTML 'IFRAME' element node.
+ *
+ * @author Chris Burdess
+ */
+public class DomHTMLIFrameElement
+ extends DomHTMLElement
+ implements HTMLIFrameElement
+{
+
+ protected DomHTMLIFrameElement(DomHTMLDocument owner, String namespaceURI,
+ String name)
+ {
+ super(owner, namespaceURI, name);
+ }
+
+ public String getAlign()
+ {
+ return getHTMLAttribute("align");
+ }
+
+ public void setAlign(String align)
+ {
+ setHTMLAttribute("align", align);
+ }
+
+ public String getFrameBorder()
+ {
+ return getHTMLAttribute("frameborder");
+ }
+
+ public void setFrameBorder(String frameBorder)
+ {
+ setHTMLAttribute("frameborder", frameBorder);
+ }
+
+ public String getHeight()
+ {
+ return getHTMLAttribute("height");
+ }
+
+ public void setHeight(String height)
+ {
+ setHTMLAttribute("height", height);
+ }
+
+ public String getLongDesc()
+ {
+ return getHTMLAttribute("longdesc");
+ }
+
+ public void setLongDesc(String longDesc)
+ {
+ setHTMLAttribute("longdesc", longDesc);
+ }
+
+ public String getMarginHeight()
+ {
+ return getHTMLAttribute("marginheight");
+ }
+
+ public void setMarginHeight(String marginHeight)
+ {
+ setHTMLAttribute("marginheight", marginHeight);
+ }
+
+ public String getMarginWidth()
+ {
+ return getHTMLAttribute("marginwidth");
+ }
+
+ public void setMarginWidth(String marginWidth)
+ {
+ setHTMLAttribute("marginwidth", marginWidth);
+ }
+
+ public String getName()
+ {
+ return getHTMLAttribute("name");
+ }
+
+ public void setName(String name)
+ {
+ setHTMLAttribute("name", name);
+ }
+
+ public String getScrolling()
+ {
+ return getHTMLAttribute("scrolling");
+ }
+
+ public void setScrolling(String scrolling)
+ {
+ setHTMLAttribute("scrolling", scrolling);
+ }
+
+ public String getSrc()
+ {
+ return getHTMLAttribute("src");
+ }
+
+ public void setSrc(String src)
+ {
+ setHTMLAttribute("src", src);
+ }
+
+ public String getWidth()
+ {
+ return getHTMLAttribute("width");
+ }
+
+ public void setWidth(String width)
+ {
+ setHTMLAttribute("width", width);
+ }
+
+ public Document getContentDocument()
+ {
+ // TODO getContentDocument
+ return null;
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/dom/html2/DomHTMLImageElement.java b/libjava/classpath/gnu/xml/dom/html2/DomHTMLImageElement.java
new file mode 100644
index 000000000..c5d294c6f
--- /dev/null
+++ b/libjava/classpath/gnu/xml/dom/html2/DomHTMLImageElement.java
@@ -0,0 +1,178 @@
+/* DomHTMLImageElement.java --
+ Copyright (C) 2005 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.dom.html2;
+
+import org.w3c.dom.html2.HTMLImageElement;
+
+/**
+ * An HTML 'IMG' element node.
+ *
+ * @author Chris Burdess
+ */
+public class DomHTMLImageElement
+ extends DomHTMLElement
+ implements HTMLImageElement
+{
+
+ protected DomHTMLImageElement(DomHTMLDocument owner, String namespaceURI,
+ String name)
+ {
+ super(owner, namespaceURI, name);
+ }
+
+ public String getName()
+ {
+ return getHTMLAttribute("name");
+ }
+
+ public void setName(String name)
+ {
+ setHTMLAttribute("name", name);
+ }
+
+ public String getAlign()
+ {
+ return getHTMLAttribute("align");
+ }
+
+ public void setAlign(String align)
+ {
+ setHTMLAttribute("align", align);
+ }
+
+ public String getAlt()
+ {
+ return getHTMLAttribute("alt");
+ }
+
+ public void setAlt(String alt)
+ {
+ setHTMLAttribute("alt", alt);
+ }
+
+ public String getBorder()
+ {
+ return getHTMLAttribute("border");
+ }
+
+ public void setBorder(String border)
+ {
+ setHTMLAttribute("border", border);
+ }
+
+ public int getHeight()
+ {
+ return getIntHTMLAttribute("height");
+ }
+
+ public void setHeight(int height)
+ {
+ setIntHTMLAttribute("height", height);
+ }
+
+ public int getHspace()
+ {
+ return getIntHTMLAttribute("hspace");
+ }
+
+ public void setHspace(int hspace)
+ {
+ setIntHTMLAttribute("hspace", hspace);
+ }
+
+ public boolean getIsMap()
+ {
+ return getBooleanHTMLAttribute("ismap");
+ }
+
+ public void setIsMap(boolean isMap)
+ {
+ setBooleanHTMLAttribute("ismap", isMap);
+ }
+
+ public String getLongDesc()
+ {
+ return getHTMLAttribute("longdesc");
+ }
+
+ public void setLongDesc(String longDesc)
+ {
+ setHTMLAttribute("longdesc", longDesc);
+ }
+
+ public String getSrc()
+ {
+ return getHTMLAttribute("src");
+ }
+
+ public void setSrc(String src)
+ {
+ setHTMLAttribute("src", src);
+ }
+
+ public String getUseMap()
+ {
+ return getHTMLAttribute("usemap");
+ }
+
+ public void setUseMap(String useMap)
+ {
+ setHTMLAttribute("usemap", useMap);
+ }
+
+ public int getVspace()
+ {
+ return getIntHTMLAttribute("vspace");
+ }
+
+ public void setVspace(int vspace)
+ {
+ setIntHTMLAttribute("vspace", vspace);
+ }
+
+ public int getWidth()
+ {
+ return getIntHTMLAttribute("width");
+ }
+
+ public void setWidth(int width)
+ {
+ setIntHTMLAttribute("width", width);
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/dom/html2/DomHTMLImpl.java b/libjava/classpath/gnu/xml/dom/html2/DomHTMLImpl.java
new file mode 100644
index 000000000..a5faee5ce
--- /dev/null
+++ b/libjava/classpath/gnu/xml/dom/html2/DomHTMLImpl.java
@@ -0,0 +1,66 @@
+/* DomHTMLImpl.java --
+ Copyright (C) 2005 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.dom.html2;
+
+import gnu.xml.dom.DomImpl;
+import org.w3c.dom.Document;
+
+/**
+ * Specialised DOMImplementation for creating HTML documents.
+ *
+ * @author Chris Burdess
+ */
+public class DomHTMLImpl
+ extends DomImpl
+{
+
+ protected Document createDocument()
+ {
+ return new DomHTMLDocument(this);
+ }
+
+ public Object getFeature(String feature, String version)
+ {
+ if (hasFeature(feature, version))
+ {
+ return this;
+ }
+ return null;
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/dom/html2/DomHTMLInputElement.java b/libjava/classpath/gnu/xml/dom/html2/DomHTMLInputElement.java
new file mode 100644
index 000000000..21201889d
--- /dev/null
+++ b/libjava/classpath/gnu/xml/dom/html2/DomHTMLInputElement.java
@@ -0,0 +1,265 @@
+/* DomHTMLInputElement.java --
+ Copyright (C) 2005 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.dom.html2;
+
+import org.w3c.dom.html2.HTMLFormElement;
+import org.w3c.dom.html2.HTMLInputElement;
+
+/**
+ * An HTML 'INPUT' element node.
+ *
+ * @author Chris Burdess
+ */
+public class DomHTMLInputElement
+ extends DomHTMLElement
+ implements HTMLInputElement
+{
+
+ protected String value;
+ protected Boolean checked;
+
+ protected DomHTMLInputElement(DomHTMLDocument owner, String namespaceURI,
+ String name)
+ {
+ super(owner, namespaceURI, name);
+ }
+
+ public String getDefaultValue()
+ {
+ return getHTMLAttribute("value");
+ }
+
+ public void setDefaultValue(String defaultValue)
+ {
+ setHTMLAttribute("value", defaultValue);
+ }
+
+ public boolean getDefaultChecked()
+ {
+ return getBooleanHTMLAttribute("checked");
+ }
+
+ public void setDefaultChecked(boolean defaultChecked)
+ {
+ setBooleanHTMLAttribute("checked", defaultChecked);
+ }
+
+ public HTMLFormElement getForm()
+ {
+ return (HTMLFormElement) getParentElement("form");
+ }
+
+ public String getAccept()
+ {
+ return getHTMLAttribute("accept");
+ }
+
+ public void setAccept(String accept)
+ {
+ setHTMLAttribute("accept", accept);
+ }
+
+ public String getAccessKey()
+ {
+ return getHTMLAttribute("accesskey");
+ }
+
+ public void setAccessKey(String accessKey)
+ {
+ setHTMLAttribute("accesskey", accessKey);
+ }
+
+ public String getAlign()
+ {
+ return getHTMLAttribute("align");
+ }
+
+ public void setAlign(String align)
+ {
+ setHTMLAttribute("align", align);
+ }
+
+ public String getAlt()
+ {
+ return getHTMLAttribute("alt");
+ }
+
+ public void setAlt(String alt)
+ {
+ setHTMLAttribute("alt", alt);
+ }
+
+ public boolean getChecked()
+ {
+ if (checked == null)
+ {
+ checked = Boolean.valueOf(getDefaultChecked());
+ }
+ return checked.booleanValue();
+ }
+
+ public void setChecked(boolean checked)
+ {
+ this.checked = Boolean.valueOf(checked);
+ }
+
+ public boolean getDisabled()
+ {
+ return getBooleanHTMLAttribute("disabled");
+ }
+
+ public void setDisabled(boolean disabled)
+ {
+ setBooleanHTMLAttribute("disabled", disabled);
+ }
+
+ public int getMaxLength()
+ {
+ return getIntHTMLAttribute("maxLength");
+ }
+
+ public void setMaxLength(int maxLength)
+ {
+ setIntHTMLAttribute("maxLength", maxLength);
+ }
+
+ public String getName()
+ {
+ return getHTMLAttribute("name");
+ }
+
+ public void setName(String name)
+ {
+ setHTMLAttribute("name", name);
+ }
+
+ public boolean getReadOnly()
+ {
+ return getBooleanHTMLAttribute("readonly");
+ }
+
+ public void setReadOnly(boolean readOnly)
+ {
+ setBooleanHTMLAttribute("readonly", readOnly);
+ }
+
+ public int getSize()
+ {
+ return getIntHTMLAttribute("size");
+ }
+
+ public void setSize(int size)
+ {
+ setIntHTMLAttribute("size", size);
+ }
+
+ public String getSrc()
+ {
+ return getHTMLAttribute("src");
+ }
+
+ public void setSrc(String src)
+ {
+ setHTMLAttribute("src", src);
+ }
+
+ public int getTabIndex()
+ {
+ return getIntHTMLAttribute("tabindex");
+ }
+
+ public void setTabIndex(int tabIndex)
+ {
+ setIntHTMLAttribute("tabindex", tabIndex);
+ }
+
+ public String getType()
+ {
+ return getHTMLAttribute("type");
+ }
+
+ public void setType(String type)
+ {
+ setHTMLAttribute("type", type);
+ }
+
+ public String getUseMap()
+ {
+ return getHTMLAttribute("usemap");
+ }
+
+ public void setUseMap(String useMap)
+ {
+ setHTMLAttribute("usemap", useMap);
+ }
+
+ public String getValue()
+ {
+ if (value == null)
+ {
+ value = getDefaultValue();
+ }
+ return value;
+ }
+
+ public void setValue(String value)
+ {
+ this.value = value;
+ }
+
+ public void blur()
+ {
+ dispatchUIEvent("blur");
+ }
+
+ public void focus()
+ {
+ dispatchUIEvent("focus");
+ }
+
+ public void select()
+ {
+ dispatchUIEvent("select");
+ }
+
+ public void click()
+ {
+ dispatchUIEvent("click");
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/dom/html2/DomHTMLIsIndexElement.java b/libjava/classpath/gnu/xml/dom/html2/DomHTMLIsIndexElement.java
new file mode 100644
index 000000000..8578cb6c0
--- /dev/null
+++ b/libjava/classpath/gnu/xml/dom/html2/DomHTMLIsIndexElement.java
@@ -0,0 +1,74 @@
+/* DomHTMLIsIndexElement.java --
+ Copyright (C) 2005 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.dom.html2;
+
+import org.w3c.dom.html2.HTMLFormElement;
+import org.w3c.dom.html2.HTMLIsIndexElement;
+
+/**
+ * An HTML 'ISINDEX' element node.
+ *
+ * @author Chris Burdess
+ */
+public class DomHTMLIsIndexElement
+ extends DomHTMLElement
+ implements HTMLIsIndexElement
+{
+
+ protected DomHTMLIsIndexElement(DomHTMLDocument owner, String namespaceURI,
+ String name)
+ {
+ super(owner, namespaceURI, name);
+ }
+
+ public HTMLFormElement getForm()
+ {
+ return (HTMLFormElement) getParentElement("form");
+ }
+
+ public String getPrompt()
+ {
+ return getHTMLAttribute("prompt");
+ }
+
+ public void setPrompt(String prompt)
+ {
+ setHTMLAttribute("prompt", prompt);
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/dom/html2/DomHTMLLIElement.java b/libjava/classpath/gnu/xml/dom/html2/DomHTMLLIElement.java
new file mode 100644
index 000000000..cab1412dd
--- /dev/null
+++ b/libjava/classpath/gnu/xml/dom/html2/DomHTMLLIElement.java
@@ -0,0 +1,78 @@
+/* DomHTMLLIElement.java --
+ Copyright (C) 2005 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.dom.html2;
+
+import org.w3c.dom.html2.HTMLLIElement;
+
+/**
+ * An HTML 'LI' element node.
+ *
+ * @author Chris Burdess
+ */
+public class DomHTMLLIElement
+ extends DomHTMLElement
+ implements HTMLLIElement
+{
+
+ protected DomHTMLLIElement(DomHTMLDocument owner, String namespaceURI,
+ String name)
+ {
+ super(owner, namespaceURI, name);
+ }
+
+ public String getType()
+ {
+ return getHTMLAttribute("type");
+ }
+
+ public void setType(String type)
+ {
+ setHTMLAttribute("type", type);
+ }
+
+ public int getValue()
+ {
+ return getIntHTMLAttribute("value");
+ }
+
+ public void setValue(int value)
+ {
+ setIntHTMLAttribute("value", value);
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/dom/html2/DomHTMLLabelElement.java b/libjava/classpath/gnu/xml/dom/html2/DomHTMLLabelElement.java
new file mode 100644
index 000000000..96cfbaab5
--- /dev/null
+++ b/libjava/classpath/gnu/xml/dom/html2/DomHTMLLabelElement.java
@@ -0,0 +1,84 @@
+/* DomHTMLLabelElement.java --
+ Copyright (C) 2005 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.dom.html2;
+
+import org.w3c.dom.html2.HTMLFormElement;
+import org.w3c.dom.html2.HTMLLabelElement;
+
+/**
+ * An HTML 'LABEL' element node.
+ *
+ * @author Chris Burdess
+ */
+public class DomHTMLLabelElement
+ extends DomHTMLElement
+ implements HTMLLabelElement
+{
+
+ protected DomHTMLLabelElement(DomHTMLDocument owner, String namespaceURI,
+ String name)
+ {
+ super(owner, namespaceURI, name);
+ }
+
+ public HTMLFormElement getForm()
+ {
+ return (HTMLFormElement) getParentElement("form");
+ }
+
+ public String getAccessKey()
+ {
+ return getHTMLAttribute("accesskey");
+ }
+
+ public void setAccessKey(String accessKey)
+ {
+ setHTMLAttribute("accesskey", accessKey);
+ }
+
+ public String getHtmlFor()
+ {
+ return getHTMLAttribute("for");
+ }
+
+ public void setHtmlFor(String htmlFor)
+ {
+ setHTMLAttribute("for", htmlFor);
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/dom/html2/DomHTMLLegendElement.java b/libjava/classpath/gnu/xml/dom/html2/DomHTMLLegendElement.java
new file mode 100644
index 000000000..045861cae
--- /dev/null
+++ b/libjava/classpath/gnu/xml/dom/html2/DomHTMLLegendElement.java
@@ -0,0 +1,84 @@
+/* DomHTMLLegendElement.java --
+ Copyright (C) 2005 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.dom.html2;
+
+import org.w3c.dom.html2.HTMLFormElement;
+import org.w3c.dom.html2.HTMLLegendElement;
+
+/**
+ * An HTML 'LEGEND' element node.
+ *
+ * @author Chris Burdess
+ */
+public class DomHTMLLegendElement
+ extends DomHTMLElement
+ implements HTMLLegendElement
+{
+
+ protected DomHTMLLegendElement(DomHTMLDocument owner, String namespaceURI,
+ String name)
+ {
+ super(owner, namespaceURI, name);
+ }
+
+ public HTMLFormElement getForm()
+ {
+ return (HTMLFormElement) getParentElement("form");
+ }
+
+ public String getAccessKey()
+ {
+ return getHTMLAttribute("accesskey");
+ }
+
+ public void setAccessKey(String accessKey)
+ {
+ setHTMLAttribute("accesskey", accessKey);
+ }
+
+ public String getAlign()
+ {
+ return getHTMLAttribute("align");
+ }
+
+ public void setAlign(String align)
+ {
+ setHTMLAttribute("align", align);
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/dom/html2/DomHTMLLinkElement.java b/libjava/classpath/gnu/xml/dom/html2/DomHTMLLinkElement.java
new file mode 100644
index 000000000..292ec9305
--- /dev/null
+++ b/libjava/classpath/gnu/xml/dom/html2/DomHTMLLinkElement.java
@@ -0,0 +1,148 @@
+/* DomHTMLLinkElement.java --
+ Copyright (C) 2005 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.dom.html2;
+
+import org.w3c.dom.html2.HTMLLinkElement;
+
+/**
+ * An HTML 'LINK' element node.
+ *
+ * @author Chris Burdess
+ */
+public class DomHTMLLinkElement
+ extends DomHTMLElement
+ implements HTMLLinkElement
+{
+
+ protected DomHTMLLinkElement(DomHTMLDocument owner, String namespaceURI,
+ String name)
+ {
+ super(owner, namespaceURI, name);
+ }
+
+ public boolean getDisabled()
+ {
+ return getBooleanHTMLAttribute("disabled");
+ }
+
+ public void setDisabled(boolean disabled)
+ {
+ setBooleanHTMLAttribute("disabled", disabled);
+ }
+
+ public String getCharset()
+ {
+ return getHTMLAttribute("charset");
+ }
+
+ public void setCharset(String charset)
+ {
+ setHTMLAttribute("charset", charset);
+ }
+
+ public String getHref()
+ {
+ return getHTMLAttribute("href");
+ }
+
+ public void setHref(String href)
+ {
+ setHTMLAttribute("href", href);
+ }
+
+ public String getHreflang()
+ {
+ return getHTMLAttribute("hreflang");
+ }
+
+ public void setHreflang(String hreflang)
+ {
+ setHTMLAttribute("hreflang", hreflang);
+ }
+
+ public String getMedia()
+ {
+ return getHTMLAttribute("media");
+ }
+
+ public void setMedia(String media)
+ {
+ setHTMLAttribute("media", media);
+ }
+
+ public String getRel()
+ {
+ return getHTMLAttribute("rel");
+ }
+
+ public void setRel(String rel)
+ {
+ setHTMLAttribute("rel", rel);
+ }
+
+ public String getRev()
+ {
+ return getHTMLAttribute("rev");
+ }
+
+ public void setRev(String rev)
+ {
+ setHTMLAttribute("rev", rev);
+ }
+
+ public String getTarget()
+ {
+ return getHTMLAttribute("target");
+ }
+
+ public void setTarget(String target)
+ {
+ setHTMLAttribute("target", target);
+ }
+
+ public String getType()
+ {
+ return getHTMLAttribute("type");
+ }
+
+ public void setType(String type)
+ {
+ setHTMLAttribute("type", type);
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/dom/html2/DomHTMLMapElement.java b/libjava/classpath/gnu/xml/dom/html2/DomHTMLMapElement.java
new file mode 100644
index 000000000..4497b00e2
--- /dev/null
+++ b/libjava/classpath/gnu/xml/dom/html2/DomHTMLMapElement.java
@@ -0,0 +1,78 @@
+/* DomHTMLMapElement.java --
+ Copyright (C) 2005 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.dom.html2;
+
+import org.w3c.dom.html2.HTMLCollection;
+import org.w3c.dom.html2.HTMLMapElement;
+
+/**
+ * An HTML 'MAP' element node.
+ *
+ * @author Chris Burdess
+ */
+public class DomHTMLMapElement
+ extends DomHTMLElement
+ implements HTMLMapElement
+{
+
+ protected DomHTMLMapElement(DomHTMLDocument owner, String namespaceURI,
+ String name)
+ {
+ super(owner, namespaceURI, name);
+ }
+
+ public HTMLCollection getAreas()
+ {
+ DomHTMLCollection ret =
+ new DomHTMLCollection((DomHTMLDocument) getOwnerDocument(), this);
+ ret.addNodeName("area");
+ ret.evaluate();
+ return ret;
+ }
+
+ public String getName()
+ {
+ return getHTMLAttribute("name");
+ }
+
+ public void setName(String name)
+ {
+ setHTMLAttribute("name", name);
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/dom/html2/DomHTMLMenuElement.java b/libjava/classpath/gnu/xml/dom/html2/DomHTMLMenuElement.java
new file mode 100644
index 000000000..2a6ff08f1
--- /dev/null
+++ b/libjava/classpath/gnu/xml/dom/html2/DomHTMLMenuElement.java
@@ -0,0 +1,68 @@
+/* DomHTMLMenuElement.java --
+ Copyright (C) 2005 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.dom.html2;
+
+import org.w3c.dom.html2.HTMLMenuElement;
+
+/**
+ * An HTML 'MENU' element node.
+ *
+ * @author Chris Burdess
+ */
+public class DomHTMLMenuElement
+ extends DomHTMLElement
+ implements HTMLMenuElement
+{
+
+ protected DomHTMLMenuElement(DomHTMLDocument owner, String namespaceURI,
+ String name)
+ {
+ super(owner, namespaceURI, name);
+ }
+
+ public boolean getCompact()
+ {
+ return getBooleanHTMLAttribute("compact");
+ }
+
+ public void setCompact(boolean compact)
+ {
+ setBooleanHTMLAttribute("compact", compact);
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/dom/html2/DomHTMLMetaElement.java b/libjava/classpath/gnu/xml/dom/html2/DomHTMLMetaElement.java
new file mode 100644
index 000000000..e555a4273
--- /dev/null
+++ b/libjava/classpath/gnu/xml/dom/html2/DomHTMLMetaElement.java
@@ -0,0 +1,98 @@
+/* DomHTMLMetaElement.java --
+ Copyright (C) 2005 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.dom.html2;
+
+import org.w3c.dom.html2.HTMLMetaElement;
+
+/**
+ * An HTML 'META' element node.
+ *
+ * @author Chris Burdess
+ */
+public class DomHTMLMetaElement
+ extends DomHTMLElement
+ implements HTMLMetaElement
+{
+
+ protected DomHTMLMetaElement(DomHTMLDocument owner, String namespaceURI,
+ String name)
+ {
+ super(owner, namespaceURI, name);
+ }
+
+ public String getContent()
+ {
+ return getHTMLAttribute("content");
+ }
+
+ public void setContent(String content)
+ {
+ setHTMLAttribute("content", content);
+ }
+
+ public String getHttpEquiv()
+ {
+ return getHTMLAttribute("http-equiv");
+ }
+
+ public void setHttpEquiv(String httpEquiv)
+ {
+ setHTMLAttribute("http-equiv", httpEquiv);
+ }
+
+ public String getName()
+ {
+ return getHTMLAttribute("name");
+ }
+
+ public void setName(String name)
+ {
+ setHTMLAttribute("name", name);
+ }
+
+ public String getScheme()
+ {
+ return getHTMLAttribute("scheme");
+ }
+
+ public void setScheme(String scheme)
+ {
+ setHTMLAttribute("scheme", scheme);
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/dom/html2/DomHTMLModElement.java b/libjava/classpath/gnu/xml/dom/html2/DomHTMLModElement.java
new file mode 100644
index 000000000..8c158f48a
--- /dev/null
+++ b/libjava/classpath/gnu/xml/dom/html2/DomHTMLModElement.java
@@ -0,0 +1,78 @@
+/* DomHTMLModElement.java --
+ Copyright (C) 2005 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.dom.html2;
+
+import org.w3c.dom.html2.HTMLModElement;
+
+/**
+ * An HTML 'INS' or 'DEL' element node.
+ *
+ * @author Chris Burdess
+ */
+public class DomHTMLModElement
+ extends DomHTMLElement
+ implements HTMLModElement
+{
+
+ protected DomHTMLModElement(DomHTMLDocument owner, String namespaceURI,
+ String name)
+ {
+ super(owner, namespaceURI, name);
+ }
+
+ public String getCite()
+ {
+ return getHTMLAttribute("cite");
+ }
+
+ public void setCite(String cite)
+ {
+ setHTMLAttribute("cite", cite);
+ }
+
+ public String getDateTime()
+ {
+ return getHTMLAttribute("datetime");
+ }
+
+ public void setDateTime(String dateTime)
+ {
+ setHTMLAttribute("datetime", dateTime);
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/dom/html2/DomHTMLOListElement.java b/libjava/classpath/gnu/xml/dom/html2/DomHTMLOListElement.java
new file mode 100644
index 000000000..54f7fd46b
--- /dev/null
+++ b/libjava/classpath/gnu/xml/dom/html2/DomHTMLOListElement.java
@@ -0,0 +1,88 @@
+/* DomHTMLOListElement.java --
+ Copyright (C) 2005 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.dom.html2;
+
+import org.w3c.dom.html2.HTMLOListElement;
+
+/**
+ * An HTML 'OL' element node.
+ *
+ * @author Chris Burdess
+ */
+public class DomHTMLOListElement
+ extends DomHTMLElement
+ implements HTMLOListElement
+{
+
+ protected DomHTMLOListElement(DomHTMLDocument owner, String namespaceURI,
+ String name)
+ {
+ super(owner, namespaceURI, name);
+ }
+
+ public boolean getCompact()
+ {
+ return getBooleanHTMLAttribute("compact");
+ }
+
+ public void setCompact(boolean compact)
+ {
+ setBooleanHTMLAttribute("compact", compact);
+ }
+
+ public int getStart()
+ {
+ return getIntHTMLAttribute("start");
+ }
+
+ public void setStart(int start)
+ {
+ setIntHTMLAttribute("start", start);
+ }
+
+ public String getType()
+ {
+ return getHTMLAttribute("type");
+ }
+
+ public void setType(String type)
+ {
+ setHTMLAttribute("type", type);
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/dom/html2/DomHTMLObjectElement.java b/libjava/classpath/gnu/xml/dom/html2/DomHTMLObjectElement.java
new file mode 100644
index 000000000..2ce5f45c3
--- /dev/null
+++ b/libjava/classpath/gnu/xml/dom/html2/DomHTMLObjectElement.java
@@ -0,0 +1,320 @@
+/* DomHTMLObjectElement.java --
+ Copyright (C) 2005 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.dom.html2;
+
+import org.w3c.dom.Document;
+import org.w3c.dom.html2.HTMLFormElement;
+import org.w3c.dom.html2.HTMLObjectElement;
+
+/**
+ * An HTML 'OBJECT' element node.
+ *
+ * @author Chris Burdess
+ */
+public class DomHTMLObjectElement
+ extends DomHTMLElement
+ implements HTMLObjectElement
+{
+
+ protected DomHTMLObjectElement(DomHTMLDocument owner, String namespaceURI,
+ String name)
+ {
+ super(owner, namespaceURI, name);
+ }
+
+ public HTMLFormElement getForm()
+ {
+ return (HTMLFormElement) getParentElement("form");
+ }
+
+ public String getCode()
+ {
+ return getHTMLAttribute("code");
+ }
+
+ public void setCode(String code)
+ {
+ setHTMLAttribute("code", code);
+ }
+
+ public String getJavaCode()
+ {
+ return getHTMLAttribute("java_code");
+ }
+
+ public void setJavaCode(String code)
+ {
+ setHTMLAttribute("java_code", code);
+ }
+
+ public String getObject()
+ {
+ return getHTMLAttribute("object");
+ }
+
+ public void setObject(String obj)
+ {
+ setHTMLAttribute("object", obj);
+ }
+
+ public String getJavaObject()
+ {
+ return getHTMLAttribute("java_object");
+ }
+
+ public void setJavaObject(String obj)
+ {
+ setHTMLAttribute("java_object", obj);
+ }
+
+ public String getAlign()
+ {
+ return getHTMLAttribute("align");
+ }
+
+ public void setAlign(String align)
+ {
+ setHTMLAttribute("align", align);
+ }
+
+ public String getArchive()
+ {
+ return getHTMLAttribute("archive");
+ }
+
+ public void setArchive(String archive)
+ {
+ setHTMLAttribute("archive", archive);
+ }
+
+ public String getJavaArchive()
+ {
+ return getHTMLAttribute("java_archive");
+ }
+
+ public void setJavaArchive(String archive)
+ {
+ setHTMLAttribute("java_archive", archive);
+ }
+
+ public String getBorder()
+ {
+ return getHTMLAttribute("border");
+ }
+
+ public void setBorder(String border)
+ {
+ setHTMLAttribute("border", border);
+ }
+
+ public String getCodeBase()
+ {
+ return getHTMLAttribute("codebase");
+ }
+
+ public void setCodeBase(String codeBase)
+ {
+ setHTMLAttribute("codebase", codeBase);
+ }
+
+ public String getJavaCodeBase()
+ {
+ return getHTMLAttribute("java_codebase");
+ }
+
+ public void setJavaCodeBase(String codeBase)
+ {
+ setHTMLAttribute("java_codebase", codeBase);
+ }
+
+ public String getCodeType()
+ {
+ return getHTMLAttribute("codetype");
+ }
+
+ public void setCodeType(String codeType)
+ {
+ setHTMLAttribute("codetype", codeType);
+ }
+
+ public String getData()
+ {
+ return getHTMLAttribute("data");
+ }
+
+ public void setData(String data)
+ {
+ setHTMLAttribute("data", data);
+ }
+
+ public boolean getDeclare()
+ {
+ return getBooleanHTMLAttribute("declare");
+ }
+
+ public void setDeclare(boolean declare)
+ {
+ setBooleanHTMLAttribute("declare", declare);
+ }
+
+ public String getHeight()
+ {
+ return getHTMLAttribute("height");
+ }
+
+ public void setHeight(String height)
+ {
+ setHTMLAttribute("height", height);
+ }
+
+ public int getHspace()
+ {
+ return getIntHTMLAttribute("hspace");
+ }
+
+ public void setHspace(int hspace)
+ {
+ setIntHTMLAttribute("hspace", hspace);
+ }
+
+ public String getName()
+ {
+ return getHTMLAttribute("name");
+ }
+
+ public void setName(String name)
+ {
+ setHTMLAttribute("name", name);
+ }
+
+ public String getStandby()
+ {
+ return getHTMLAttribute("standby");
+ }
+
+ public void setStandby(String standby)
+ {
+ setHTMLAttribute("standby", standby);
+ }
+
+ public int getTabIndex()
+ {
+ return getIntHTMLAttribute("tabindex");
+ }
+
+ public void setTabIndex(int tabIndex)
+ {
+ setIntHTMLAttribute("tabindex", tabIndex);
+ }
+
+ public String getType()
+ {
+ return getHTMLAttribute("type");
+ }
+
+ public void setType(String type)
+ {
+ setHTMLAttribute("type", type);
+ }
+
+ public String getJavaType()
+ {
+ return getHTMLAttribute("java_type");
+ }
+
+ public void setJavaType(String type)
+ {
+ setHTMLAttribute("java_type", type);
+ }
+
+ public String getUseMap()
+ {
+ return getHTMLAttribute("usemap");
+ }
+
+ public void setUseMap(String useMap)
+ {
+ setHTMLAttribute("usemap", useMap);
+ }
+
+ public int getVspace()
+ {
+ return getIntHTMLAttribute("vspace");
+ }
+
+ public void setVspace(int vspace)
+ {
+ setIntHTMLAttribute("vspace", vspace);
+ }
+
+ public String getWidth()
+ {
+ return getHTMLAttribute("width");
+ }
+
+ public void setWidth(String width)
+ {
+ setHTMLAttribute("width", width);
+ }
+
+ public Document getContentDocument()
+ {
+ // TODO getContentDocument
+ return null;
+ }
+
+ public void setMayscript(String may)
+ {
+ setHTMLAttribute("mayscript", may);
+ }
+
+ public String getMayscript()
+ {
+ return getHTMLAttribute("mayscript");
+ }
+
+ public void setScriptable(String scr)
+ {
+ setHTMLAttribute("scriptable", scr);
+ }
+
+ public String getScriptable()
+ {
+ return getHTMLAttribute("scriptable");
+ }
+}
diff --git a/libjava/classpath/gnu/xml/dom/html2/DomHTMLOptGroupElement.java b/libjava/classpath/gnu/xml/dom/html2/DomHTMLOptGroupElement.java
new file mode 100644
index 000000000..acb948484
--- /dev/null
+++ b/libjava/classpath/gnu/xml/dom/html2/DomHTMLOptGroupElement.java
@@ -0,0 +1,78 @@
+/* DomHTMLOptGroupElement.java --
+ Copyright (C) 2005 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.dom.html2;
+
+import org.w3c.dom.html2.HTMLOptGroupElement;
+
+/**
+ * An HTML 'OPTGROUP' element node.
+ *
+ * @author Chris Burdess
+ */
+public class DomHTMLOptGroupElement
+ extends DomHTMLElement
+ implements HTMLOptGroupElement
+{
+
+ protected DomHTMLOptGroupElement(DomHTMLDocument owner, String namespaceURI,
+ String name)
+ {
+ super(owner, namespaceURI, name);
+ }
+
+ public boolean getDisabled()
+ {
+ return getBooleanHTMLAttribute("disabled");
+ }
+
+ public void setDisabled(boolean disabled)
+ {
+ setBooleanHTMLAttribute("disabled", disabled);
+ }
+
+ public String getLabel()
+ {
+ return getHTMLAttribute("label");
+ }
+
+ public void setLabel(String label)
+ {
+ setHTMLAttribute("label", label);
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/dom/html2/DomHTMLOptionElement.java b/libjava/classpath/gnu/xml/dom/html2/DomHTMLOptionElement.java
new file mode 100644
index 000000000..f775c138a
--- /dev/null
+++ b/libjava/classpath/gnu/xml/dom/html2/DomHTMLOptionElement.java
@@ -0,0 +1,130 @@
+/* DomHTMLOptionElement.java --
+ Copyright (C) 2005 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.dom.html2;
+
+import org.w3c.dom.html2.HTMLFormElement;
+import org.w3c.dom.html2.HTMLOptionElement;
+
+/**
+ * An HTML 'OPTION' element node.
+ *
+ * @author Chris Burdess
+ */
+public class DomHTMLOptionElement
+ extends DomHTMLElement
+ implements HTMLOptionElement
+{
+
+ protected Boolean selected;
+
+ protected DomHTMLOptionElement(DomHTMLDocument owner, String namespaceURI,
+ String name)
+ {
+ super(owner, namespaceURI, name);
+ }
+
+ public HTMLFormElement getForm()
+ {
+ return (HTMLFormElement) getParentElement("form");
+ }
+
+ public boolean getDefaultSelected()
+ {
+ return getBooleanHTMLAttribute("selected");
+ }
+
+ public void setDefaultSelected(boolean defaultSelected)
+ {
+ setBooleanHTMLAttribute("selected", defaultSelected);
+ }
+
+ public String getText()
+ {
+ return getTextContent();
+ }
+
+ public int getIndex()
+ {
+ return super.getIndex();
+ }
+
+ public boolean getDisabled()
+ {
+ return getBooleanHTMLAttribute("disabled");
+ }
+
+ public void setDisabled(boolean disabled)
+ {
+ setBooleanHTMLAttribute("disabled", disabled);
+ }
+
+ public String getLabel()
+ {
+ return getHTMLAttribute("label");
+ }
+
+ public void setLabel(String label)
+ {
+ setHTMLAttribute("label", label);
+ }
+
+ public boolean getSelected()
+ {
+ if (selected == null)
+ {
+ selected = Boolean.valueOf(getDefaultSelected());
+ }
+ return selected.booleanValue();
+ }
+
+ public void setSelected(boolean selected)
+ {
+ this.selected = Boolean.valueOf(selected);
+ }
+
+ public String getValue()
+ {
+ return getHTMLAttribute("value");
+ }
+
+ public void setValue(String value)
+ {
+ setHTMLAttribute("value", value);
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/dom/html2/DomHTMLParagraphElement.java b/libjava/classpath/gnu/xml/dom/html2/DomHTMLParagraphElement.java
new file mode 100644
index 000000000..4c867fdc1
--- /dev/null
+++ b/libjava/classpath/gnu/xml/dom/html2/DomHTMLParagraphElement.java
@@ -0,0 +1,68 @@
+/* DomHTMLParagraphElement.java --
+ Copyright (C) 2005 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.dom.html2;
+
+import org.w3c.dom.html2.HTMLParagraphElement;
+
+/**
+ * An HTML 'P' element node.
+ *
+ * @author Chris Burdess
+ */
+public class DomHTMLParagraphElement
+ extends DomHTMLElement
+ implements HTMLParagraphElement
+{
+
+ protected DomHTMLParagraphElement(DomHTMLDocument owner, String namespaceURI,
+ String name)
+ {
+ super(owner, namespaceURI, name);
+ }
+
+ public String getAlign()
+ {
+ return getHTMLAttribute("align");
+ }
+
+ public void setAlign(String align)
+ {
+ setHTMLAttribute("align", align);
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/dom/html2/DomHTMLParamElement.java b/libjava/classpath/gnu/xml/dom/html2/DomHTMLParamElement.java
new file mode 100644
index 000000000..cdd74ad4a
--- /dev/null
+++ b/libjava/classpath/gnu/xml/dom/html2/DomHTMLParamElement.java
@@ -0,0 +1,98 @@
+/* DomHTMLParamElement.java --
+ Copyright (C) 2005 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.dom.html2;
+
+import org.w3c.dom.html2.HTMLParamElement;
+
+/**
+ * An HTML 'PARAM' element node.
+ *
+ * @author Chris Burdess
+ */
+public class DomHTMLParamElement
+ extends DomHTMLElement
+ implements HTMLParamElement
+{
+
+ protected DomHTMLParamElement(DomHTMLDocument owner, String namespaceURI,
+ String name)
+ {
+ super(owner, namespaceURI, name);
+ }
+
+ public String getName()
+ {
+ return getHTMLAttribute("name");
+ }
+
+ public void setName(String name)
+ {
+ setHTMLAttribute("name", name);
+ }
+
+ public String getType()
+ {
+ return getHTMLAttribute("type");
+ }
+
+ public void setType(String type)
+ {
+ setHTMLAttribute("type", type);
+ }
+
+ public String getValue()
+ {
+ return getHTMLAttribute("value");
+ }
+
+ public void setValue(String value)
+ {
+ setHTMLAttribute("value", value);
+ }
+
+ public String getValueType()
+ {
+ return getHTMLAttribute("valuetype");
+ }
+
+ public void setValueType(String valueType)
+ {
+ setHTMLAttribute("valuetype", valueType);
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/dom/html2/DomHTMLParser.java b/libjava/classpath/gnu/xml/dom/html2/DomHTMLParser.java
new file mode 100644
index 000000000..88656d2d3
--- /dev/null
+++ b/libjava/classpath/gnu/xml/dom/html2/DomHTMLParser.java
@@ -0,0 +1,264 @@
+/* DomHTMLParser.java --
+ Copyright (C) 2005 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+
+package gnu.xml.dom.html2;
+
+import java.io.IOException;
+import java.io.Reader;
+
+import java.util.Enumeration;
+import java.util.Iterator;
+import java.util.LinkedList;
+
+import javax.swing.text.AttributeSet;
+import javax.swing.text.html.HTML;
+import javax.swing.text.html.parser.DTD;
+import javax.swing.text.html.parser.TagElement;
+
+import org.w3c.dom.NamedNodeMap;
+import org.w3c.dom.Node;
+import org.w3c.dom.html2.HTMLDocument;
+
+/**
+ * This parser reads HTML from the given stream and stores into
+ * {@link HTMLDocument}. The HTML tag becomes the {@link Node}.
+ * The tag attributes become the node attributes. The text inside
+ * HTML tag is inserted as one or several text nodes. The nested
+ * HTML tags are inserted as child nodes.
+ *
+ * If the strict tree structure, closing the tag means closing all
+ * nested tags. To work around this, this parser closes the nested
+ * tags and immediately reopens them after the closed tag.
+ * In this way, <b><i>c</b>d
+ * is parsed as <b><i>c</i></b><i>d
.
+ *
+ * @author Audrius Meskauskas (AudriusA@Bioinformatics.org)
+ */
+public class DomHTMLParser
+ extends gnu.javax.swing.text.html.parser.support.Parser
+{
+ /**
+ * The target where HTML document will be inserted.
+ */
+ protected DomHTMLDocument document;
+
+ /**
+ * The subsequently created new nodes will be inserted as the
+ * childs of this cursor.
+ */
+ protected Node cursor;
+
+ /**
+ * Create parser using the given DTD.
+ *
+ * @param dtd the DTD (for example,
+ * {@link gnu.javax.swing.text.html.parser.HTML_401F}).
+ */
+ public DomHTMLParser(DTD dtd)
+ {
+ super(dtd);
+ }
+
+ /**
+ * Parse SGML insertion ( <! ... > ).
+ * Currently just treats it as comment.
+ */
+ public boolean parseMarkupDeclarations(StringBuffer strBuff)
+ throws java.io.IOException
+ {
+ Node c = document.createComment(strBuff.toString());
+ cursor.appendChild(c);
+ return false;
+ }
+
+ /**
+ * Read the document, present in the given stream, and
+ * return the corresponding {@link HTMLDocument}.
+ *
+ * @param input a stream to read from.
+ * @return a document, reflecting the structure of the provided HTML
+ * text.
+ *
+ * @throws IOException if the reader throws one.
+ */
+ public HTMLDocument parseDocument(Reader input)
+ throws IOException
+ {
+ try
+ {
+ document = new DomHTMLDocument();
+ document.setCheckWellformedness(false);
+ document.setCheckingCharacters(false);
+
+ cursor = document;
+
+ parse(input);
+
+ DomHTMLDocument h = document;
+ document = null;
+ return h;
+ }
+ catch (Exception ex)
+ {
+ ex.printStackTrace();
+ throw new IOException("Exception: " + ex.getMessage());
+ }
+ }
+
+ /**
+ * Create a new node.
+ * @param name the name of node, case insensitive.
+ * @return the created node.
+ */
+ protected Node createNode(String name)
+ {
+ Node new_node = document.createElement(name.toLowerCase());
+ AttributeSet hatts = getAttributes();
+ NamedNodeMap natts = new_node.getAttributes();
+
+ Enumeration enumeration = hatts.getAttributeNames();
+ Object key;
+ Node attribute;
+
+ while (hatts != null)
+ {
+ while (enumeration.hasMoreElements())
+ {
+ key = enumeration.nextElement();
+ attribute = document.createAttribute(key.toString());
+ attribute.setNodeValue(hatts.getAttribute(key).toString());
+ natts.setNamedItem(attribute);
+ }
+
+ // The default values are stored in a parent node.
+ hatts = hatts.getResolveParent();
+ }
+
+ return new_node;
+ }
+
+ /**
+ * Handle comment by inserting the comment node.
+ * @param text the comment text.
+ */
+ protected void handleComment(char[] text)
+ {
+ Node c = document.createComment(new String(text));
+ cursor.appendChild(c);
+ }
+
+ /**
+ * Handle the tag with no content.
+ * @param tag the tag to handle.
+ */
+ protected void handleEmptyTag(TagElement tag)
+ {
+ String name = tag.getHTMLTag().toString();
+
+ if (name.equalsIgnoreCase("#pcdata"))
+ return;
+
+ Node c = createNode(name);
+ cursor.appendChild(c);
+ }
+
+ /**
+ * Close the given tag. Close and reopen all nested tags.
+ * @param tag the tag to close.
+ */
+ protected void handleEndTag(TagElement tag)
+ {
+ String name = tag.getHTMLTag().toString();
+ String nname = cursor.getNodeName();
+
+ // Closing the current tag.
+ if (nname != null && nname.equalsIgnoreCase(name))
+ {
+ cursor = cursor.getParentNode();
+ }
+ else
+ {
+ Node nCursor = cursor.getParentNode();
+
+ // Remember the opened nodes.
+ LinkedList open = new LinkedList();
+ Node close = cursor;
+ while (close != null && !close.getNodeName().equalsIgnoreCase(name))
+ {
+ if (close != document)
+ open.addFirst(close);
+ close = close.getParentNode();
+ }
+ if (close == null)
+ cursor = document;
+ else
+ cursor = close.getParentNode();
+
+ // Insert the copies of the opened nodes.
+ Iterator iter = open.iterator();
+ while (iter.hasNext())
+ {
+ Node item = (Node) iter.next();
+ cursor.appendChild(item);
+ cursor = item;
+ }
+ }
+ }
+
+ /**
+ * Handle the start tag by inserting the HTML element.
+ * @param tag the tag to handle.
+ */
+ protected void handleStartTag(TagElement tag)
+ {
+ HTML.Tag h = tag.getHTMLTag();
+ Node c = createNode(h.toString());
+ cursor.appendChild(c);
+ cursor = c;
+ }
+
+ /**
+ * Handle text by inserting the text node.
+ * @param text the text to insert.
+ */
+ protected void handleText(char[] text)
+ {
+ Node c = document.createTextNode(text, 0, text.length);
+ cursor.appendChild(c);
+ }
+}
diff --git a/libjava/classpath/gnu/xml/dom/html2/DomHTMLPreElement.java b/libjava/classpath/gnu/xml/dom/html2/DomHTMLPreElement.java
new file mode 100644
index 000000000..9a51aaeeb
--- /dev/null
+++ b/libjava/classpath/gnu/xml/dom/html2/DomHTMLPreElement.java
@@ -0,0 +1,68 @@
+/* DomHTMLPreElement.java --
+ Copyright (C) 2005 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.dom.html2;
+
+import org.w3c.dom.html2.HTMLPreElement;
+
+/**
+ * An HTML 'PRE' element node.
+ *
+ * @author Chris Burdess
+ */
+public class DomHTMLPreElement
+ extends DomHTMLElement
+ implements HTMLPreElement
+{
+
+ protected DomHTMLPreElement(DomHTMLDocument owner, String namespaceURI,
+ String name)
+ {
+ super(owner, namespaceURI, name);
+ }
+
+ public int getWidth()
+ {
+ return getIntHTMLAttribute("width");
+ }
+
+ public void setWidth(int width)
+ {
+ setIntHTMLAttribute("width", width);
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/dom/html2/DomHTMLQuoteElement.java b/libjava/classpath/gnu/xml/dom/html2/DomHTMLQuoteElement.java
new file mode 100644
index 000000000..811aed58b
--- /dev/null
+++ b/libjava/classpath/gnu/xml/dom/html2/DomHTMLQuoteElement.java
@@ -0,0 +1,68 @@
+/* DomHTMLQuoteElement.java --
+ Copyright (C) 2005 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.dom.html2;
+
+import org.w3c.dom.html2.HTMLQuoteElement;
+
+/**
+ * An HTML 'Q' or 'BLOCKQUOTE' element node.
+ *
+ * @author Chris Burdess
+ */
+public class DomHTMLQuoteElement
+ extends DomHTMLElement
+ implements HTMLQuoteElement
+{
+
+ protected DomHTMLQuoteElement(DomHTMLDocument owner, String namespaceURI,
+ String name)
+ {
+ super(owner, namespaceURI, name);
+ }
+
+ public String getCite()
+ {
+ return getHTMLAttribute("cite");
+ }
+
+ public void setCite(String cite)
+ {
+ setHTMLAttribute("cite", cite);
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/dom/html2/DomHTMLScriptElement.java b/libjava/classpath/gnu/xml/dom/html2/DomHTMLScriptElement.java
new file mode 100644
index 000000000..1bd7b3417
--- /dev/null
+++ b/libjava/classpath/gnu/xml/dom/html2/DomHTMLScriptElement.java
@@ -0,0 +1,128 @@
+/* DomHTMLScriptElement.java --
+ Copyright (C) 2005 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.dom.html2;
+
+import org.w3c.dom.html2.HTMLScriptElement;
+
+/**
+ * An HTML 'SCRIPT' element node.
+ *
+ * @author Chris Burdess
+ */
+public class DomHTMLScriptElement
+ extends DomHTMLElement
+ implements HTMLScriptElement
+{
+
+ protected DomHTMLScriptElement(DomHTMLDocument owner, String namespaceURI,
+ String name)
+ {
+ super(owner, namespaceURI, name);
+ }
+
+ public String getText()
+ {
+ return getTextContent();
+ }
+
+ public void setText(String text)
+ {
+ setTextContent(text);
+ }
+
+ public String getHtmlFor()
+ {
+ return getHTMLAttribute("for");
+ }
+
+ public void setHtmlFor(String htmlFor)
+ {
+ setHTMLAttribute("for", htmlFor);
+ }
+
+ public String getEvent()
+ {
+ return getHTMLAttribute("event");
+ }
+
+ public void setEvent(String event)
+ {
+ setHTMLAttribute("event", event);
+ }
+
+ public String getCharset()
+ {
+ return getHTMLAttribute("charset");
+ }
+
+ public void setCharset(String charset)
+ {
+ setHTMLAttribute("charset", charset);
+ }
+
+ public boolean getDefer()
+ {
+ return getBooleanHTMLAttribute("defer");
+ }
+
+ public void setDefer(boolean defer)
+ {
+ setBooleanHTMLAttribute("defer", defer);
+ }
+
+ public String getSrc()
+ {
+ return getHTMLAttribute("src");
+ }
+
+ public void setSrc(String src)
+ {
+ setHTMLAttribute("src", src);
+ }
+
+ public String getType()
+ {
+ return getHTMLAttribute("type");
+ }
+
+ public void setType(String type)
+ {
+ setHTMLAttribute("type", type);
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/dom/html2/DomHTMLSelectElement.java b/libjava/classpath/gnu/xml/dom/html2/DomHTMLSelectElement.java
new file mode 100644
index 000000000..fc1debbd6
--- /dev/null
+++ b/libjava/classpath/gnu/xml/dom/html2/DomHTMLSelectElement.java
@@ -0,0 +1,210 @@
+/* DomHTMLSelectElement.java --
+ Copyright (C) 2005 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.dom.html2;
+
+import gnu.xml.dom.DomDOMException;
+import org.w3c.dom.DOMException;
+import org.w3c.dom.html2.HTMLElement;
+import org.w3c.dom.html2.HTMLFormElement;
+import org.w3c.dom.html2.HTMLOptionElement;
+import org.w3c.dom.html2.HTMLOptionsCollection;
+import org.w3c.dom.html2.HTMLSelectElement;
+
+/**
+ * An HTML 'SELECT' element node.
+ *
+ * @author Chris Burdess
+ */
+public class DomHTMLSelectElement
+ extends DomHTMLElement
+ implements HTMLSelectElement
+{
+
+ protected DomHTMLSelectElement(DomHTMLDocument owner, String namespaceURI,
+ String name)
+ {
+ super(owner, namespaceURI, name);
+ }
+
+ public String getType()
+ {
+ return getHTMLAttribute("type");
+ }
+
+ public int getSelectedIndex()
+ {
+ HTMLOptionsCollection options = getOptions();
+ int len = options.getLength();
+ for (int i = 0; i < len; i++)
+ {
+ HTMLOptionElement option = (HTMLOptionElement) options.item(i);
+ if (option.getSelected())
+ {
+ return i;
+ }
+ }
+ return -1;
+ }
+
+ public void setSelectedIndex(int selectedIndex)
+ {
+ HTMLOptionsCollection options = getOptions();
+ int len = options.getLength();
+ if (selectedIndex < 0 || selectedIndex >= len)
+ {
+ throw new DomDOMException(DOMException.INDEX_SIZE_ERR);
+ }
+ for (int i = 0; i < len; i++)
+ {
+ HTMLOptionElement option = (HTMLOptionElement) options.item(i);
+ option.setSelected(i == selectedIndex);
+ }
+ }
+
+ public String getValue()
+ {
+ return getHTMLAttribute("value");
+ }
+
+ public void setValue(String value)
+ {
+ setHTMLAttribute("value", value);
+ }
+
+ public int getLength()
+ {
+ return getIntHTMLAttribute("length");
+ }
+
+ public void setLength(int length)
+ {
+ setIntHTMLAttribute("length", length);
+ }
+
+ public HTMLFormElement getForm()
+ {
+ return (HTMLFormElement) getParentElement("form");
+ }
+
+ public HTMLOptionsCollection getOptions()
+ {
+ DomHTMLCollection ret =
+ new DomHTMLCollection((DomHTMLDocument) getOwnerDocument(), this);
+ ret.addNodeName("option");
+ ret.evaluate();
+ return ret;
+ }
+
+ public boolean getDisabled()
+ {
+ return getBooleanHTMLAttribute("disabled");
+ }
+
+ public void setDisabled(boolean disabled)
+ {
+ setBooleanHTMLAttribute("disabled", disabled);
+ }
+
+ public boolean getMultiple()
+ {
+ return getBooleanHTMLAttribute("multiple");
+ }
+
+ public void setMultiple(boolean multiple)
+ {
+ setBooleanHTMLAttribute("multiple", multiple);
+ }
+
+ public String getName()
+ {
+ return getHTMLAttribute("name");
+ }
+
+ public void setName(String name)
+ {
+ setHTMLAttribute("name", name);
+ }
+
+ public int getSize()
+ {
+ return getIntHTMLAttribute("size");
+ }
+
+ public void setSize(int size)
+ {
+ setIntHTMLAttribute("size", size);
+ }
+
+ public int getTabIndex()
+ {
+ return getIntHTMLAttribute("tabindex");
+ }
+
+ public void setTabIndex(int tabIndex)
+ {
+ setIntHTMLAttribute("tabindex", tabIndex);
+ }
+
+ public void add(HTMLElement element, HTMLElement before)
+ {
+ insertBefore(before, element);
+ }
+
+ public void remove(int index)
+ {
+ HTMLOptionsCollection options = getOptions();
+ int len = options.getLength();
+ if (index < 0 || index >= len)
+ {
+ throw new DomDOMException(DOMException.INDEX_SIZE_ERR);
+ }
+ HTMLOptionElement option = (HTMLOptionElement) options.item(index);
+ option.getParentNode().removeChild(option);
+ }
+
+ public void blur()
+ {
+ dispatchUIEvent("blur");
+ }
+
+ public void focus()
+ {
+ dispatchUIEvent("focus");
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/dom/html2/DomHTMLStyleElement.java b/libjava/classpath/gnu/xml/dom/html2/DomHTMLStyleElement.java
new file mode 100644
index 000000000..78cef3b18
--- /dev/null
+++ b/libjava/classpath/gnu/xml/dom/html2/DomHTMLStyleElement.java
@@ -0,0 +1,88 @@
+/* DomHTMLStyleElement.java --
+ Copyright (C) 2005 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.dom.html2;
+
+import org.w3c.dom.html2.HTMLStyleElement;
+
+/**
+ * An HTML 'STYLE' element node.
+ *
+ * @author Chris Burdess
+ */
+public class DomHTMLStyleElement
+ extends DomHTMLElement
+ implements HTMLStyleElement
+{
+
+ protected DomHTMLStyleElement(DomHTMLDocument owner, String namespaceURI,
+ String name)
+ {
+ super(owner, namespaceURI, name);
+ }
+
+ public boolean getDisabled()
+ {
+ return getBooleanHTMLAttribute("disabled");
+ }
+
+ public void setDisabled(boolean disabled)
+ {
+ setBooleanHTMLAttribute("disabled", disabled);
+ }
+
+ public String getMedia()
+ {
+ return getHTMLAttribute("media");
+ }
+
+ public void setMedia(String media)
+ {
+ setHTMLAttribute("media", media);
+ }
+
+ public String getType()
+ {
+ return getHTMLAttribute("type");
+ }
+
+ public void setType(String type)
+ {
+ setHTMLAttribute("type", type);
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/dom/html2/DomHTMLTableCaptionElement.java b/libjava/classpath/gnu/xml/dom/html2/DomHTMLTableCaptionElement.java
new file mode 100644
index 000000000..2133b1412
--- /dev/null
+++ b/libjava/classpath/gnu/xml/dom/html2/DomHTMLTableCaptionElement.java
@@ -0,0 +1,69 @@
+/* DomHTMLTableCaptionElement.java --
+ Copyright (C) 2005 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.dom.html2;
+
+import org.w3c.dom.html2.HTMLTableCaptionElement;
+
+/**
+ * An HTML 'CAPTION' element node.
+ *
+ * @author Chris Burdess
+ */
+public class DomHTMLTableCaptionElement
+ extends DomHTMLElement
+ implements HTMLTableCaptionElement
+{
+
+ protected DomHTMLTableCaptionElement(DomHTMLDocument owner,
+ String namespaceURI,
+ String name)
+ {
+ super(owner, namespaceURI, name);
+ }
+
+ public String getAlign()
+ {
+ return getHTMLAttribute("align");
+ }
+
+ public void setAlign(String align)
+ {
+ setHTMLAttribute("align", align);
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/dom/html2/DomHTMLTableCellElement.java b/libjava/classpath/gnu/xml/dom/html2/DomHTMLTableCellElement.java
new file mode 100644
index 000000000..350f0bf42
--- /dev/null
+++ b/libjava/classpath/gnu/xml/dom/html2/DomHTMLTableCellElement.java
@@ -0,0 +1,204 @@
+/* DomHTMLTableCellElement.java --
+ Copyright (C) 2005 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.dom.html2;
+
+import org.w3c.dom.html2.HTMLTableCellElement;
+
+/**
+ * An HTML 'TH' or 'TD' element node.
+ *
+ * @author Chris Burdess
+ */
+public class DomHTMLTableCellElement
+ extends DomHTMLElement
+ implements HTMLTableCellElement
+{
+
+ protected DomHTMLTableCellElement(DomHTMLDocument owner,
+ String namespaceURI,
+ String name)
+ {
+ super(owner, namespaceURI, name);
+ }
+
+ public int getCellIndex()
+ {
+ return getIndex();
+ }
+
+ public String getAbbr()
+ {
+ return getHTMLAttribute("abbr");
+ }
+
+ public void setAbbr(String abbr)
+ {
+ setHTMLAttribute("abbr", abbr);
+ }
+
+ public String getAlign()
+ {
+ return getHTMLAttribute("align");
+ }
+
+ public void setAlign(String align)
+ {
+ setHTMLAttribute("align", align);
+ }
+
+ public String getAxis()
+ {
+ return getHTMLAttribute("axis");
+ }
+
+ public void setAxis(String axis)
+ {
+ setHTMLAttribute("axis", axis);
+ }
+
+ public String getBgColor()
+ {
+ return getHTMLAttribute("bgcolor");
+ }
+
+ public void setBgColor(String bgColor)
+ {
+ setHTMLAttribute("bgcolor", bgColor);
+ }
+
+ public String getCh()
+ {
+ return getHTMLAttribute("char");
+ }
+
+ public void setCh(String ch)
+ {
+ setHTMLAttribute("char", ch);
+ }
+
+ public String getChOff()
+ {
+ return getHTMLAttribute("charoff");
+ }
+
+ public void setChOff(String chOff)
+ {
+ setHTMLAttribute("charoff", chOff);
+ }
+
+ public int getColSpan()
+ {
+ return getIntHTMLAttribute("colspan");
+ }
+
+ public void setColSpan(int colSpan)
+ {
+ setIntHTMLAttribute("colspan", colSpan);
+ }
+
+ public String getHeaders()
+ {
+ return getHTMLAttribute("headers");
+ }
+
+ public void setHeaders(String headers)
+ {
+ setHTMLAttribute("headers", headers);
+ }
+
+ public String getHeight()
+ {
+ return getHTMLAttribute("height");
+ }
+
+ public void setHeight(String height)
+ {
+ setHTMLAttribute("height", height);
+ }
+
+ public boolean getNoWrap()
+ {
+ return getBooleanHTMLAttribute("nowrap");
+ }
+
+ public void setNoWrap(boolean noWrap)
+ {
+ setBooleanHTMLAttribute("nowrap", noWrap);
+ }
+
+ public int getRowSpan()
+ {
+ return getIntHTMLAttribute("rowspan");
+ }
+
+ public void setRowSpan(int rowSpan)
+ {
+ setIntHTMLAttribute("rowspan", rowSpan);
+ }
+
+ public String getScope()
+ {
+ return getHTMLAttribute("scope");
+ }
+
+ public void setScope(String scope)
+ {
+ setHTMLAttribute("scope", scope);
+ }
+
+ public String getVAlign()
+ {
+ return getHTMLAttribute("valign");
+ }
+
+ public void setVAlign(String vAlign)
+ {
+ setHTMLAttribute("valign", vAlign);
+ }
+
+ public String getWidth()
+ {
+ return getHTMLAttribute("width");
+ }
+
+ public void setWidth(String width)
+ {
+ setHTMLAttribute("width", width);
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/dom/html2/DomHTMLTableColElement.java b/libjava/classpath/gnu/xml/dom/html2/DomHTMLTableColElement.java
new file mode 100644
index 000000000..c7cef31eb
--- /dev/null
+++ b/libjava/classpath/gnu/xml/dom/html2/DomHTMLTableColElement.java
@@ -0,0 +1,119 @@
+/* DomHTMLTableColElement.java --
+ Copyright (C) 2005 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.dom.html2;
+
+import org.w3c.dom.html2.HTMLTableColElement;
+
+/**
+ * An HTML 'COL' or 'COLGROUP' element node.
+ *
+ * @author Chris Burdess
+ */
+public class DomHTMLTableColElement
+ extends DomHTMLElement
+ implements HTMLTableColElement
+{
+
+ protected DomHTMLTableColElement(DomHTMLDocument owner,
+ String namespaceURI,
+ String name)
+ {
+ super(owner, namespaceURI, name);
+ }
+
+ public String getAlign()
+ {
+ return getHTMLAttribute("align");
+ }
+
+ public void setAlign(String align)
+ {
+ setHTMLAttribute("align", align);
+ }
+
+ public String getCh()
+ {
+ return getHTMLAttribute("char");
+ }
+
+ public void setCh(String ch)
+ {
+ setHTMLAttribute("char", ch);
+ }
+
+ public String getChOff()
+ {
+ return getHTMLAttribute("charoff");
+ }
+
+ public void setChOff(String chOff)
+ {
+ setHTMLAttribute("charoff", chOff);
+ }
+
+ public int getSpan()
+ {
+ return getIntHTMLAttribute("span");
+ }
+
+ public void setSpan(int span)
+ {
+ setIntHTMLAttribute("span", span);
+ }
+
+ public String getVAlign()
+ {
+ return getHTMLAttribute("valign");
+ }
+
+ public void setVAlign(String vAlign)
+ {
+ setHTMLAttribute("valign", vAlign);
+ }
+
+ public String getWidth()
+ {
+ return getHTMLAttribute("width");
+ }
+
+ public void setWidth(String width)
+ {
+ setHTMLAttribute("width", width);
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/dom/html2/DomHTMLTableElement.java b/libjava/classpath/gnu/xml/dom/html2/DomHTMLTableElement.java
new file mode 100644
index 000000000..ade463ef7
--- /dev/null
+++ b/libjava/classpath/gnu/xml/dom/html2/DomHTMLTableElement.java
@@ -0,0 +1,397 @@
+/* DomHTMLTableElement.java --
+ Copyright (C) 2005 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.dom.html2;
+
+import gnu.xml.dom.DomDOMException;
+import org.w3c.dom.DOMException;
+import org.w3c.dom.Node;
+import org.w3c.dom.html2.HTMLCollection;
+import org.w3c.dom.html2.HTMLElement;
+import org.w3c.dom.html2.HTMLTableCaptionElement;
+import org.w3c.dom.html2.HTMLTableElement;
+import org.w3c.dom.html2.HTMLTableSectionElement;
+
+/**
+ * An HTML 'TABLE' element node.
+ *
+ * @author Chris Burdess
+ */
+public class DomHTMLTableElement
+ extends DomHTMLElement
+ implements HTMLTableElement
+{
+
+ protected DomHTMLTableElement(DomHTMLDocument owner, String namespaceURI,
+ String name)
+ {
+ super(owner, namespaceURI, name);
+ }
+
+ public HTMLTableCaptionElement getCaption()
+ {
+ return (HTMLTableCaptionElement) getChildElement("caption");
+ }
+
+ public void setCaption(HTMLTableCaptionElement caption)
+ {
+ HTMLTableCaptionElement ref = getCaption();
+ if (ref == null)
+ {
+ appendChild(caption);
+ }
+ else
+ {
+ replaceChild(caption, ref);
+ }
+ }
+
+ public HTMLTableSectionElement getTHead()
+ {
+ return (HTMLTableSectionElement) getChildElement("thead");
+ }
+
+ public void setTHead(HTMLTableSectionElement tHead)
+ {
+ HTMLTableSectionElement ref = getTHead();
+ if (ref == null)
+ {
+ appendChild(tHead);
+ }
+ else
+ {
+ replaceChild(tHead, ref);
+ }
+ }
+
+ public HTMLTableSectionElement getTFoot()
+ {
+ return (HTMLTableSectionElement) getChildElement("tfoot");
+ }
+
+ public void setTFoot(HTMLTableSectionElement tFoot)
+ {
+ HTMLTableSectionElement ref = getTFoot();
+ if (ref == null)
+ {
+ appendChild(tFoot);
+ }
+ else
+ {
+ replaceChild(tFoot, ref);
+ }
+ }
+
+ public HTMLCollection getRows()
+ {
+ DomHTMLCollection ret =
+ new DomHTMLCollection((DomHTMLDocument) getOwnerDocument(), this);
+ ret.addNodeName("tr");
+ ret.evaluate();
+ return ret;
+ }
+
+ public HTMLCollection getTBodies()
+ {
+ DomHTMLCollection ret =
+ new DomHTMLCollection((DomHTMLDocument) getOwnerDocument(), this);
+ ret.addNodeName("tbody");
+ ret.evaluate();
+ return ret;
+ }
+
+ public String getAlign()
+ {
+ return getHTMLAttribute("align");
+ }
+
+ public void setAlign(String align)
+ {
+ setHTMLAttribute("align", align);
+ }
+
+ public String getBgColor()
+ {
+ return getHTMLAttribute("bgcolor");
+ }
+
+ public void setBgColor(String bgColor)
+ {
+ setHTMLAttribute("bgcolor", bgColor);
+ }
+
+ public String getBorder()
+ {
+ return getHTMLAttribute("border");
+ }
+
+ public void setBorder(String border)
+ {
+ setHTMLAttribute("border", border);
+ }
+
+ public String getCellPadding()
+ {
+ return getHTMLAttribute("cellpadding");
+ }
+
+ public void setCellPadding(String cellPadding)
+ {
+ setHTMLAttribute("cellpadding", cellPadding);
+ }
+
+ public String getCellSpacing()
+ {
+ return getHTMLAttribute("cellspacing");
+ }
+
+ public void setCellSpacing(String cellSpacing)
+ {
+ setHTMLAttribute("cellspacing", cellSpacing);
+ }
+
+ public String getFrame()
+ {
+ return getHTMLAttribute("frame");
+ }
+
+ public void setFrame(String frame)
+ {
+ setHTMLAttribute("frame", frame);
+ }
+
+ public String getRules()
+ {
+ return getHTMLAttribute("rules");
+ }
+
+ public void setRules(String rules)
+ {
+ setHTMLAttribute("rules", rules);
+ }
+
+ public String getSummary()
+ {
+ return getHTMLAttribute("summary");
+ }
+
+ public void setSummary(String summary)
+ {
+ setHTMLAttribute("summary", summary);
+ }
+
+ public String getWidth()
+ {
+ return getHTMLAttribute("width");
+ }
+
+ public void setWidth(String width)
+ {
+ setHTMLAttribute("width", width);
+ }
+
+ public HTMLElement createTHead()
+ {
+ HTMLTableSectionElement ref = getTHead();
+ if (ref == null)
+ {
+ return (HTMLElement) getOwnerDocument().createElement("thead");
+ }
+ else
+ {
+ return ref;
+ }
+ }
+
+ public void deleteTHead()
+ {
+ HTMLTableSectionElement ref = getTHead();
+ if (ref != null)
+ {
+ removeChild(ref);
+ }
+ }
+
+ public HTMLElement createTFoot()
+ {
+ HTMLTableSectionElement ref = getTFoot();
+ if (ref == null)
+ {
+ return (HTMLElement) getOwnerDocument().createElement("tfoot");
+ }
+ else
+ {
+ return ref;
+ }
+ }
+
+ public void deleteTFoot()
+ {
+ HTMLTableSectionElement ref = getTFoot();
+ if (ref != null)
+ {
+ removeChild(ref);
+ }
+ }
+
+ public HTMLElement createCaption()
+ {
+ HTMLTableCaptionElement ref = getCaption();
+ if (ref == null)
+ {
+ return (HTMLElement) getOwnerDocument().createElement("caption");
+ }
+ else
+ {
+ return ref;
+ }
+ }
+
+ public void deleteCaption()
+ {
+ HTMLTableCaptionElement ref = getCaption();
+ if (ref != null)
+ {
+ removeChild(ref);
+ }
+ }
+
+ public HTMLElement insertRow(int index)
+ {
+ Node ref = getRow(index);
+ Node row = getOwnerDocument().createElement("tr");
+ if (ref == null)
+ {
+ Node tbody = getChildElement("tbody");
+ if (tbody == null)
+ {
+ tbody = getOwnerDocument().createElement("tfoot");
+ appendChild(tbody);
+ }
+ tbody.appendChild(row);
+ }
+ else
+ {
+ ref.getParentNode().insertBefore(row, ref);
+ }
+ return (HTMLElement) row;
+ }
+
+ public void deleteRow(int index)
+ {
+ Node ref = getRow(index);
+ if (ref == null)
+ {
+ throw new DomDOMException(DOMException.INDEX_SIZE_ERR);
+ }
+ ref.getParentNode().removeChild(ref);
+ }
+
+ Node getRow(final int index)
+ {
+ int i = 0;
+ Node thead = getChildElement("thead");
+ if (thead != null)
+ {
+ for (Node ctx = thead.getFirstChild(); ctx != null;
+ ctx = ctx.getNextSibling())
+ {
+ String ctxName = ctx.getLocalName();
+ if (ctxName == null)
+ {
+ ctxName = ctx.getNodeName();
+ }
+ if (!"tr".equalsIgnoreCase(ctxName))
+ {
+ continue;
+ }
+ if (index == i)
+ {
+ return ctx;
+ }
+ i++;
+ }
+ }
+ Node tbody = getChildElement("tbody");
+ if (tbody == null)
+ {
+ tbody = this;
+ }
+ for (Node ctx = tbody.getFirstChild(); ctx != null;
+ ctx = ctx.getNextSibling())
+ {
+ String ctxName = ctx.getLocalName();
+ if (ctxName == null)
+ {
+ ctxName = ctx.getNodeName();
+ }
+ if (!"tr".equalsIgnoreCase(ctxName))
+ {
+ continue;
+ }
+ if (index == i)
+ {
+ return ctx;
+ }
+ i++;
+ }
+ Node tfoot = getChildElement("tfoot");
+ if (tfoot != null)
+ {
+ for (Node ctx = tfoot.getFirstChild(); ctx != null;
+ ctx = ctx.getNextSibling())
+ {
+ String ctxName = ctx.getLocalName();
+ if (ctxName == null)
+ {
+ ctxName = ctx.getNodeName();
+ }
+ if (!"tr".equalsIgnoreCase(ctxName))
+ {
+ continue;
+ }
+ if (index == i)
+ {
+ return ctx;
+ }
+ i++;
+ }
+ }
+ return null;
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/dom/html2/DomHTMLTableRowElement.java b/libjava/classpath/gnu/xml/dom/html2/DomHTMLTableRowElement.java
new file mode 100644
index 000000000..9943585f4
--- /dev/null
+++ b/libjava/classpath/gnu/xml/dom/html2/DomHTMLTableRowElement.java
@@ -0,0 +1,228 @@
+/* DomHTMLTableRowElement.java --
+ Copyright (C) 2005 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.dom.html2;
+
+import gnu.xml.dom.DomDOMException;
+import org.w3c.dom.DOMException;
+import org.w3c.dom.Node;
+import org.w3c.dom.html2.HTMLCollection;
+import org.w3c.dom.html2.HTMLElement;
+import org.w3c.dom.html2.HTMLTableRowElement;
+
+/**
+ * An HTML 'TR' element node.
+ *
+ * @author Chris Burdess
+ */
+public class DomHTMLTableRowElement
+ extends DomHTMLElement
+ implements HTMLTableRowElement
+{
+
+ protected DomHTMLTableRowElement(DomHTMLDocument owner,
+ String namespaceURI,
+ String name)
+ {
+ super(owner, namespaceURI, name);
+ }
+
+ public int getRowIndex()
+ {
+ return getIndex();
+ }
+
+ public int getSectionRowIndex()
+ {
+ int index = 0;
+ DomHTMLElement parent = (DomHTMLElement) getParentElement("table");
+ if (parent != null)
+ {
+ Node thead = parent.getChildElement("thead");
+ if (thead != null)
+ {
+ for (Node ctx = thead.getFirstChild(); ctx != null;
+ ctx = ctx.getNextSibling())
+ {
+ if (ctx == this)
+ {
+ return index;
+ }
+ index++;
+ }
+ }
+ Node tbody = parent.getChildElement("tbody");
+ if (tbody != null)
+ {
+ for (Node ctx = tbody.getFirstChild(); ctx != null;
+ ctx = ctx.getNextSibling())
+ {
+ if (ctx == this)
+ {
+ return index;
+ }
+ index++;
+ }
+ }
+ Node tfoot = parent.getChildElement("tfoot");
+ if (tfoot != null)
+ {
+ for (Node ctx = tfoot.getFirstChild(); ctx != null;
+ ctx = ctx.getNextSibling())
+ {
+ if (ctx == this)
+ {
+ return index;
+ }
+ index++;
+ }
+ }
+ }
+ throw new DomDOMException(DOMException.NOT_FOUND_ERR);
+ }
+
+ public HTMLCollection getCells()
+ {
+ DomHTMLCollection ret =
+ new DomHTMLCollection((DomHTMLDocument) getOwnerDocument(), this);
+ ret.addNodeName("th");
+ ret.addNodeName("td");
+ ret.evaluate();
+ return ret;
+ }
+
+ public String getAlign()
+ {
+ return getHTMLAttribute("align");
+ }
+
+ public void setAlign(String align)
+ {
+ setHTMLAttribute("align", align);
+ }
+
+ public String getBgColor()
+ {
+ return getHTMLAttribute("bgcolor");
+ }
+
+ public void setBgColor(String bgColor)
+ {
+ setHTMLAttribute("bgcolor", bgColor);
+ }
+
+ public String getCh()
+ {
+ return getHTMLAttribute("char");
+ }
+
+ public void setCh(String ch)
+ {
+ setHTMLAttribute("char", ch);
+ }
+
+ public String getChOff()
+ {
+ return getHTMLAttribute("charoff");
+ }
+
+ public void setChOff(String chOff)
+ {
+ setHTMLAttribute("charoff", chOff);
+ }
+
+ public String getVAlign()
+ {
+ return getHTMLAttribute("valign");
+ }
+
+ public void setVAlign(String vAlign)
+ {
+ setHTMLAttribute("valign", vAlign);
+ }
+
+ public HTMLElement insertCell(int index)
+ {
+ Node ref = getCell(index);
+ Node cell = getOwnerDocument().createElement("td");
+ if (ref == null)
+ {
+ appendChild(cell);
+ }
+ else
+ {
+ insertBefore(cell, ref);
+ }
+ return (HTMLElement) cell;
+ }
+
+ public void deleteCell(int index)
+ {
+ Node ref = getCell(index);
+ if (ref == null)
+ {
+ throw new DomDOMException(DOMException.INDEX_SIZE_ERR);
+ }
+ removeChild(ref);
+ }
+
+ Node getCell(final int index)
+ {
+ int i = 0;
+ for (Node ctx = getFirstChild(); ctx != null;
+ ctx = ctx.getNextSibling())
+ {
+ String name = ctx.getLocalName();
+ if (name == null)
+ {
+ name = ctx.getNodeName();
+ }
+ if (!"td".equalsIgnoreCase(name) &&
+ !"th".equalsIgnoreCase(name))
+ {
+ continue;
+ }
+ if (index == i)
+ {
+ return ctx;
+ }
+ i++;
+ }
+ return null;
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/dom/html2/DomHTMLTableSectionElement.java b/libjava/classpath/gnu/xml/dom/html2/DomHTMLTableSectionElement.java
new file mode 100644
index 000000000..389eb591b
--- /dev/null
+++ b/libjava/classpath/gnu/xml/dom/html2/DomHTMLTableSectionElement.java
@@ -0,0 +1,162 @@
+/* DomHTMLTableSectionElement.java --
+ Copyright (C) 2005 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.dom.html2;
+
+import gnu.xml.dom.DomDOMException;
+import org.w3c.dom.DOMException;
+import org.w3c.dom.Node;
+import org.w3c.dom.html2.HTMLCollection;
+import org.w3c.dom.html2.HTMLElement;
+import org.w3c.dom.html2.HTMLTableSectionElement;
+
+/**
+ * An HTML 'THEAD', 'TFOOT', or 'TBODY' element node.
+ *
+ * @author Chris Burdess
+ */
+public class DomHTMLTableSectionElement
+ extends DomHTMLElement
+ implements HTMLTableSectionElement
+{
+
+ protected DomHTMLTableSectionElement(DomHTMLDocument owner,
+ String namespaceURI,
+ String name)
+ {
+ super(owner, namespaceURI, name);
+ }
+
+ public String getAlign()
+ {
+ return getHTMLAttribute("align");
+ }
+
+ public void setAlign(String align)
+ {
+ setHTMLAttribute("align", align);
+ }
+
+ public String getCh()
+ {
+ return getHTMLAttribute("char");
+ }
+
+ public void setCh(String ch)
+ {
+ setHTMLAttribute("char", ch);
+ }
+
+ public String getChOff()
+ {
+ return getHTMLAttribute("charoff");
+ }
+
+ public void setChOff(String chOff)
+ {
+ setHTMLAttribute("charoff", chOff);
+ }
+
+ public String getVAlign()
+ {
+ return getHTMLAttribute("valign");
+ }
+
+ public void setVAlign(String vAlign)
+ {
+ setHTMLAttribute("valign", vAlign);
+ }
+
+ public HTMLCollection getRows()
+ {
+ DomHTMLCollection ret =
+ new DomHTMLCollection((DomHTMLDocument) getOwnerDocument(), this);
+ ret.addNodeName("tr");
+ ret.evaluate();
+ return ret;
+ }
+
+ public HTMLElement insertRow(int index)
+ {
+ Node ref = getRow(index);
+ Node row = getOwnerDocument().createElement("tr");
+ if (ref == null)
+ {
+ appendChild(row);
+ }
+ else
+ {
+ insertBefore(row, ref);
+ }
+ return (HTMLElement) row;
+ }
+
+ public void deleteRow(int index)
+ {
+ Node ref = getRow(index);
+ if (ref == null)
+ {
+ throw new DomDOMException(DOMException.INDEX_SIZE_ERR);
+ }
+ removeChild(ref);
+ }
+
+ Node getRow(final int index)
+ {
+ int i = 0;
+ for (Node ctx = getFirstChild(); ctx != null;
+ ctx = ctx.getNextSibling())
+ {
+ String name = ctx.getLocalName();
+ if (name == null)
+ {
+ name = ctx.getNodeName();
+ }
+ if (!"tr".equalsIgnoreCase(name))
+ {
+ continue;
+ }
+ if (index == i)
+ {
+ return ctx;
+ }
+ i++;
+ }
+ return null;
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/dom/html2/DomHTMLTextAreaElement.java b/libjava/classpath/gnu/xml/dom/html2/DomHTMLTextAreaElement.java
new file mode 100644
index 000000000..9acfab134
--- /dev/null
+++ b/libjava/classpath/gnu/xml/dom/html2/DomHTMLTextAreaElement.java
@@ -0,0 +1,181 @@
+/* DomHTMLTextAreaElement.java --
+ Copyright (C) 2005 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.dom.html2;
+
+import org.w3c.dom.html2.HTMLFormElement;
+import org.w3c.dom.html2.HTMLTextAreaElement;
+
+/**
+ * An HTML 'TEXTAREA' element node.
+ *
+ * @author Chris Burdess
+ */
+public class DomHTMLTextAreaElement
+ extends DomHTMLElement
+ implements HTMLTextAreaElement
+{
+
+ protected String value;
+
+ protected DomHTMLTextAreaElement(DomHTMLDocument owner,
+ String namespaceURI,
+ String name)
+ {
+ super(owner, namespaceURI, name);
+ }
+
+ public String getDefaultValue()
+ {
+ return getHTMLAttribute("value");
+ }
+
+ public void setDefaultValue(String defaultValue)
+ {
+ setHTMLAttribute("value", defaultValue);
+ }
+
+ public HTMLFormElement getForm()
+ {
+ return (HTMLFormElement) getParentElement("form");
+ }
+
+ public String getAccessKey()
+ {
+ return getHTMLAttribute("accesskey");
+ }
+
+ public void setAccessKey(String accessKey)
+ {
+ setHTMLAttribute("accesskey", accessKey);
+ }
+
+ public int getCols()
+ {
+ return getIntHTMLAttribute("cols");
+ }
+
+ public void setCols(int cols)
+ {
+ setIntHTMLAttribute("cols", cols);
+ }
+
+ public boolean getDisabled()
+ {
+ return getBooleanHTMLAttribute("disabled");
+ }
+
+ public void setDisabled(boolean disabled)
+ {
+ setBooleanHTMLAttribute("disabled", disabled);
+ }
+
+ public String getName()
+ {
+ return getHTMLAttribute("name");
+ }
+
+ public void setName(String name)
+ {
+ setHTMLAttribute("name", name);
+ }
+
+ public boolean getReadOnly()
+ {
+ return getBooleanHTMLAttribute("readOnly");
+ }
+
+ public void setReadOnly(boolean readOnly)
+ {
+ setBooleanHTMLAttribute("readonly", readOnly);
+ }
+
+ public int getRows()
+ {
+ return getIntHTMLAttribute("rows");
+ }
+
+ public void setRows(int rows)
+ {
+ setIntHTMLAttribute("rows", rows);
+ }
+
+ public int getTabIndex()
+ {
+ return getIntHTMLAttribute("tabindex");
+ }
+
+ public void setTabIndex(int tabIndex)
+ {
+ setIntHTMLAttribute("tabindex", tabIndex);
+ }
+
+ public String getType()
+ {
+ return "textarea";
+ }
+
+ public String getValue()
+ {
+ if (value == null)
+ {
+ value = getDefaultValue();
+ }
+ return value;
+ }
+
+ public void setValue(String value)
+ {
+ this.value = value;
+ }
+
+ public void blur()
+ {
+ dispatchUIEvent("blur");
+ }
+
+ public void focus()
+ {
+ dispatchUIEvent("focus");
+ }
+
+ public void select()
+ {
+ dispatchUIEvent("select");
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/dom/html2/DomHTMLTitleElement.java b/libjava/classpath/gnu/xml/dom/html2/DomHTMLTitleElement.java
new file mode 100644
index 000000000..4f581061b
--- /dev/null
+++ b/libjava/classpath/gnu/xml/dom/html2/DomHTMLTitleElement.java
@@ -0,0 +1,68 @@
+/* DomHTMLTitleElement.java --
+ Copyright (C) 2005 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.dom.html2;
+
+import org.w3c.dom.html2.HTMLTitleElement;
+
+/**
+ * An HTML 'TITLE' element node.
+ *
+ * @author Chris Burdess
+ */
+public class DomHTMLTitleElement
+ extends DomHTMLElement
+ implements HTMLTitleElement
+{
+
+ protected DomHTMLTitleElement(DomHTMLDocument owner, String namespaceURI,
+ String name)
+ {
+ super(owner, namespaceURI, name);
+ }
+
+ public String getText()
+ {
+ return getTextContent();
+ }
+
+ public void setText(String text)
+ {
+ setTextContent(text);
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/dom/html2/DomHTMLUListElement.java b/libjava/classpath/gnu/xml/dom/html2/DomHTMLUListElement.java
new file mode 100644
index 000000000..39cdce7d3
--- /dev/null
+++ b/libjava/classpath/gnu/xml/dom/html2/DomHTMLUListElement.java
@@ -0,0 +1,78 @@
+/* DomHTMLUListElement.java --
+ Copyright (C) 2005 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.dom.html2;
+
+import org.w3c.dom.html2.HTMLUListElement;
+
+/**
+ * An HTML 'UL' element node.
+ *
+ * @author Chris Burdess
+ */
+public class DomHTMLUListElement
+ extends DomHTMLElement
+ implements HTMLUListElement
+{
+
+ protected DomHTMLUListElement(DomHTMLDocument owner, String namespaceURI,
+ String name)
+ {
+ super(owner, namespaceURI, name);
+ }
+
+ public boolean getCompact()
+ {
+ return getBooleanHTMLAttribute("compact");
+ }
+
+ public void setCompact(boolean compact)
+ {
+ setBooleanHTMLAttribute("compact", compact);
+ }
+
+ public String getType()
+ {
+ return getHTMLAttribute("type");
+ }
+
+ public void setType(String type)
+ {
+ setHTMLAttribute("type", type);
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/dom/ls/DomLSException.java b/libjava/classpath/gnu/xml/dom/ls/DomLSException.java
new file mode 100644
index 000000000..31efc845f
--- /dev/null
+++ b/libjava/classpath/gnu/xml/dom/ls/DomLSException.java
@@ -0,0 +1,57 @@
+/* DomLSException.java --
+ Copyright (C) 2004 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.dom.ls;
+
+import org.w3c.dom.ls.LSException;
+
+/**
+ * A DOM LS exception incorporating a cause.
+ *
+ * @author Chris Burdess
+ */
+public class DomLSException
+ extends LSException
+{
+
+ public DomLSException(short type, Exception cause)
+ {
+ super(type, (cause == null) ? null : cause.getMessage());
+ initCause(cause);
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/dom/ls/DomLSInput.java b/libjava/classpath/gnu/xml/dom/ls/DomLSInput.java
new file mode 100644
index 000000000..39b17694f
--- /dev/null
+++ b/libjava/classpath/gnu/xml/dom/ls/DomLSInput.java
@@ -0,0 +1,159 @@
+/* DomLSInput.java --
+ Copyright (C) 1999,2000,2001 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.dom.ls;
+
+import gnu.java.lang.CPStringBuilder;
+
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.io.IOException;
+import java.io.Reader;
+import java.io.StringReader;
+import org.w3c.dom.ls.LSInput;
+
+/**
+ * Specification of XML input to parse.
+ *
+ * @author Chris Burdess
+ */
+public class DomLSInput
+ implements LSInput
+{
+
+ private InputStream in;
+ private String systemId;
+ private String publicId;
+ private String baseURI;
+ private String encoding;
+ private boolean certifiedText;
+
+ public Reader getCharacterStream()
+ {
+ return new InputStreamReader(in);
+ }
+
+ public void setCharacterStream(Reader characterStream)
+ {
+ in = new ReaderInputStream(characterStream);
+ }
+
+ public InputStream getByteStream()
+ {
+ return in;
+ }
+
+ public void setByteStream(InputStream byteStream)
+ {
+ in = byteStream;
+ }
+
+ public String getStringData()
+ {
+ CPStringBuilder acc = new CPStringBuilder();
+ Reader reader = getCharacterStream();
+ try
+ {
+ char[] buf = new char[4096];
+ for (int len = reader.read(buf); len != -1; len = reader.read(buf))
+ {
+ acc.append(buf, 0, len);
+ }
+ }
+ catch (IOException e)
+ {
+ return null; // ?
+ }
+ return acc.toString();
+ }
+
+ public void setStringData(String stringData)
+ {
+ in = new ReaderInputStream(new StringReader(stringData));
+ }
+
+ public String getSystemId()
+ {
+ return systemId;
+ }
+
+ public void setSystemId(String systemId)
+ {
+ this.systemId = systemId;
+ }
+
+ public String getPublicId()
+ {
+ return publicId;
+ }
+
+ public void setPublicId(String publicId)
+ {
+ this.publicId = publicId;
+ }
+
+ public String getBaseURI()
+ {
+ return baseURI;
+ }
+
+ public void setBaseURI(String baseURI)
+ {
+ this.baseURI = baseURI;
+ }
+
+ public String getEncoding()
+ {
+ return encoding;
+ }
+
+ public void setEncoding(String encoding)
+ {
+ this.encoding = encoding;
+ }
+
+ public boolean getCertifiedText()
+ {
+ return certifiedText;
+ }
+
+ public void setCertifiedText(boolean certifiedText)
+ {
+ this.certifiedText = certifiedText;
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/dom/ls/DomLSOutput.java b/libjava/classpath/gnu/xml/dom/ls/DomLSOutput.java
new file mode 100644
index 000000000..e8bec2ec1
--- /dev/null
+++ b/libjava/classpath/gnu/xml/dom/ls/DomLSOutput.java
@@ -0,0 +1,98 @@
+/* DomLSOutput.java --
+ Copyright (C) 1999,2000,2001 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.dom.ls;
+
+import java.io.OutputStream;
+import java.io.OutputStreamWriter;
+import java.io.Writer;
+import org.w3c.dom.ls.LSOutput;
+
+/**
+ * Specification of XML output to produce.
+ *
+ * @author Chris Burdess
+ */
+public class DomLSOutput
+ implements LSOutput
+{
+
+ private OutputStream out;
+ private String systemId;
+ private String encoding;
+
+ public Writer getCharacterStream()
+ {
+ return new OutputStreamWriter(out);
+ }
+
+ public void setCharacterStream(Writer characterStream)
+ {
+ out = new WriterOutputStream(characterStream);
+ }
+
+ public OutputStream getByteStream()
+ {
+ return out;
+ }
+
+ public void setByteStream(OutputStream out)
+ {
+ this.out = out;
+ }
+
+ public String getSystemId()
+ {
+ return systemId;
+ }
+
+ public void setSystemId(String systemId)
+ {
+ this.systemId = systemId;
+ }
+
+ public String getEncoding()
+ {
+ return encoding;
+ }
+
+ public void setEncoding(String encoding)
+ {
+ this.encoding = encoding;
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/dom/ls/DomLSParser.java b/libjava/classpath/gnu/xml/dom/ls/DomLSParser.java
new file mode 100644
index 000000000..99db79d64
--- /dev/null
+++ b/libjava/classpath/gnu/xml/dom/ls/DomLSParser.java
@@ -0,0 +1,567 @@
+/* DomLSParser.java --
+ Copyright (C) 1999,2000,2001,2007 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.dom.ls;
+
+import java.io.File;
+import java.io.InputStream;
+import java.io.IOException;
+import java.net.MalformedURLException;
+import java.net.URL;
+import java.util.Arrays;
+import java.util.List;
+import javax.xml.parsers.ParserConfigurationException;
+import javax.xml.parsers.SAXParser;
+import javax.xml.parsers.SAXParserFactory;
+import org.w3c.dom.Document;
+import org.w3c.dom.DOMConfiguration;
+import org.w3c.dom.DOMException;
+import org.w3c.dom.DOMStringList;
+import org.w3c.dom.Node;
+import org.w3c.dom.ls.DOMImplementationLS;
+import org.w3c.dom.ls.LSException;
+import org.w3c.dom.ls.LSInput;
+import org.w3c.dom.ls.LSParser;
+import org.w3c.dom.ls.LSParserFilter;
+import org.xml.sax.EntityResolver;
+import org.xml.sax.ErrorHandler;
+import org.xml.sax.InputSource;
+import org.xml.sax.SAXException;
+import org.xml.sax.SAXNotRecognizedException;
+import org.xml.sax.SAXParseException;
+import org.xml.sax.XMLReader;
+import gnu.xml.dom.DomDocument;
+import gnu.xml.dom.DomDOMException;
+
+/**
+ * Parser implementation for GNU DOM.
+ *
+ * @author Chris Burdess
+ */
+public class DomLSParser
+ implements LSParser, DOMConfiguration, DOMStringList, ErrorHandler
+{
+
+ private static final List SUPPORTED_PARAMETERS
+ = Arrays.asList(new String[] { "cdata-sections",
+ "comments",
+ "element-content-whitespace",
+ "namespaces",
+ "expand-entity-references",
+ "coalescing",
+ "validating",
+ "xinclude-aware",
+ "entity-resolver",
+ "error-handler" });
+
+ private LSParserFilter filter;
+ private final boolean async;
+ private String schemaType;
+ private SAXEventSink eventSink;
+ private SAXParserFactory factory;
+ private XMLReader reader;
+
+ private boolean namespaceAware = true;
+ private boolean ignoreWhitespace;
+ private boolean expandEntityReferences;
+ private boolean ignoreComments;
+ private boolean coalescing;
+ private boolean validating;
+ private boolean xIncludeAware;
+ private EntityResolver entityResolver;
+ private ErrorHandler errorHandler;
+
+ public DomLSParser(short mode, String schemaType)
+ throws DOMException
+ {
+ switch (mode)
+ {
+ case DOMImplementationLS.MODE_ASYNCHRONOUS:
+ async = true;
+ break;
+ case DOMImplementationLS.MODE_SYNCHRONOUS:
+ async = false;
+ break;
+ default:
+ throw new DomDOMException(DOMException.NOT_SUPPORTED_ERR);
+ }
+ // TODO schemaType
+ this.schemaType = schemaType;
+ factory = SAXParserFactory.newInstance();
+ }
+
+ // -- LSParser --
+
+ public DOMConfiguration getDomConfig()
+ {
+ return this;
+ }
+
+ public LSParserFilter getFilter()
+ {
+ return filter;
+ }
+
+ public void setFilter(LSParserFilter filter)
+ {
+ this.filter = filter;
+ }
+
+ public boolean getAsync()
+ {
+ return async;
+ }
+
+ public boolean getBusy()
+ {
+ return eventSink != null;
+ }
+
+ public Document parse(LSInput input)
+ throws DOMException, LSException
+ {
+ if (async)
+ {
+ return doParse(input);
+ }
+ else
+ {
+ synchronized (this)
+ {
+ return doParse(input);
+ }
+ }
+ }
+
+ public Document parseURI(String uri)
+ throws DOMException, LSException
+ {
+ LSInput input = new DomLSInput();
+ input.setSystemId(uri);
+ return parse(input);
+ }
+
+ public Node parseWithContext(LSInput input, Node context, short action)
+ throws DOMException, LSException
+ {
+ Document doc = (context.getNodeType() == Node.DOCUMENT_NODE) ?
+ (Document) context : context.getOwnerDocument();
+ input.setBaseURI(doc.getDocumentURI());
+ // TODO use namespaces defined on context node
+ Document ret = parse(input);
+ Node root = ret.getDocumentElement();
+ root = doc.adoptNode(root);
+ switch (action)
+ {
+ case ACTION_APPEND_AS_CHILDREN:
+ context.appendChild(root);
+ break;
+ case ACTION_REPLACE_CHILDREN:
+ Node c1 = context.getFirstChild();
+ while (c1 != null)
+ {
+ Node next = c1.getNextSibling();
+ context.removeChild(c1);
+ c1 = next;
+ }
+ context.appendChild(root);
+ break;
+ case ACTION_INSERT_BEFORE:
+ Node p1 = context.getParentNode();
+ p1.insertBefore(root, context);
+ break;
+ case ACTION_INSERT_AFTER:
+ Node p2 = context.getParentNode();
+ Node r1 = context.getNextSibling();
+ if (r1 == null)
+ {
+ p2.appendChild(root);
+ }
+ else
+ {
+ p2.insertBefore(root, r1);
+ }
+ break;
+ case ACTION_REPLACE:
+ Node p3 = context.getParentNode();
+ Node r2 = context.getNextSibling();
+ p3.removeChild(context);
+ if (r2 == null)
+ {
+ p3.appendChild(root);
+ }
+ else
+ {
+ p3.insertBefore(root, r2);
+ }
+ break;
+ }
+ return root;
+ }
+
+ public void abort()
+ {
+ if (eventSink != null)
+ {
+ eventSink.interrupt();
+ }
+ }
+
+ private Document doParse(LSInput input)
+ throws DOMException, LSException
+ {
+ // create event sink
+ if (eventSink != null)
+ {
+ throw new LSException(LSException.PARSE_ERR, "parse in progress");
+ }
+ InputSource source = getInputSource(input);
+ eventSink = (filter == null) ? new SAXEventSink() :
+ new FilteredSAXEventSink(filter);
+ // configure sink
+ eventSink.setNamespaceAware(namespaceAware);
+ eventSink.ignoreWhitespace = ignoreWhitespace;
+ eventSink.expandEntityReferences = expandEntityReferences;
+ eventSink.ignoreComments = ignoreComments;
+ eventSink.coalescing = coalescing;
+ // get and configure reader
+ XMLReader reader = getXMLReader();
+ eventSink.reader = reader;
+ try
+ {
+ reader.setContentHandler(eventSink);
+ reader.setDTDHandler(eventSink);
+ reader.setProperty("http://xml.org/sax/properties/lexical-handler",
+ eventSink);
+ reader.setProperty("http://xml.org/sax/properties/declaration-handler",
+ eventSink);
+ reader.setFeature("http://xml.org/sax/features/namespaces",
+ namespaceAware);
+ reader.setFeature("http://xml.org/sax/features/namespace-prefixes",
+ true);
+ reader.setFeature("http://xml.org/sax/features/validation",
+ validating);
+ try
+ {
+ reader.setFeature("http://gnu.org/sax/features/coalescing",
+ coalescing);
+ }
+ catch (SAXNotRecognizedException e)
+ {
+ // ignore
+ }
+ try
+ {
+ reader.setFeature("http://xml.org/sax/features/use-attributes2",
+ true);
+ }
+ catch (SAXNotRecognizedException e)
+ {
+ // ignore
+ }
+ try
+ {
+ reader.setFeature("http://xml.org/sax/features/external-general-entities",
+ true);
+ }
+ catch (SAXNotRecognizedException e)
+ {
+ // ignore
+ }
+ reader.setEntityResolver(entityResolver);
+ reader.setErrorHandler(errorHandler);
+ // parse
+ reader.parse(source);
+ }
+ catch (DOMException e)
+ {
+ reader = null;
+ eventSink = null;
+ throw e;
+ }
+ catch (SAXException e)
+ {
+ reader = null;
+ eventSink = null;
+ throw new DomLSException(LSException.PARSE_ERR, e);
+ }
+ catch (IOException e)
+ {
+ reader = null;
+ eventSink = null;
+ throw new DomLSException(LSException.PARSE_ERR, e);
+ }
+ // return document
+ Document ret = eventSink.doc;
+ String systemId = input.getSystemId();
+ if (systemId != null && ret instanceof DomDocument)
+ {
+ ((DomDocument) ret).setDocumentURI(systemId);
+ }
+ eventSink = null;
+ return ret;
+ }
+
+ private XMLReader getXMLReader()
+ throws LSException
+ {
+ if (reader == null)
+ {
+ factory.setNamespaceAware(namespaceAware);
+ factory.setValidating(validating);
+ factory.setXIncludeAware(xIncludeAware);
+ try
+ {
+ SAXParser parser = factory.newSAXParser();
+ reader = parser.getXMLReader();
+ }
+ catch (ParserConfigurationException e)
+ {
+ throw new DomLSException(LSException.PARSE_ERR, e);
+ }
+ catch (SAXException e)
+ {
+ throw new DomLSException(LSException.PARSE_ERR, e);
+ }
+ }
+ return reader;
+ }
+
+ private InputSource getInputSource(LSInput input)
+ throws LSException
+ {
+ InputSource source = null;
+ String systemId = input.getSystemId();
+ InputStream in = input.getByteStream();
+ if (in != null)
+ {
+ source = new InputSource(in);
+ source.setSystemId(systemId);
+ }
+ if (source == null)
+ {
+ URL url = null;
+ String base = input.getBaseURI();
+ try
+ {
+ try
+ {
+ URL baseURL = (base == null) ? null : new URL(base);
+ url = (baseURL == null) ? new URL(systemId) :
+ new URL(baseURL, systemId);
+ }
+ catch (MalformedURLException e)
+ {
+ File baseFile = (base == null) ? null : new File(base);
+ url = (baseFile == null) ? new File(systemId).toURL() :
+ new File(baseFile, systemId).toURL();
+ }
+ in = url.openStream();
+ systemId = url.toString();
+ source = new InputSource(in);
+ source.setSystemId(systemId);
+ }
+ catch (IOException e)
+ {
+ throw new DomLSException(LSException.PARSE_ERR, e);
+ }
+ }
+ return source;
+ }
+
+ // -- DOMConfiguration --
+
+ public void setParameter(String name, Object value)
+ throws DOMException
+ {
+ name = name.toLowerCase();
+ if ("cdata-sections".equals(name))
+ {
+ coalescing = !((Boolean) value).booleanValue();
+ }
+ else if ("comments".equals(name))
+ {
+ ignoreComments = !((Boolean) value).booleanValue();
+ }
+ else if ("element-content-whitespace".equals(name))
+ {
+ ignoreWhitespace = !((Boolean) value).booleanValue();
+ }
+ else if ("namespaces".equals(name))
+ {
+ namespaceAware = ((Boolean) value).booleanValue();
+ }
+ else if ("expand-entity-references".equals(name))
+ {
+ expandEntityReferences = ((Boolean) value).booleanValue();
+ }
+ else if ("coalescing".equals(name))
+ {
+ coalescing = ((Boolean) value).booleanValue();
+ }
+ else if ("validating".equals(name))
+ {
+ validating = ((Boolean) value).booleanValue();
+ }
+ else if ("xinclude-aware".equals(name))
+ {
+ xIncludeAware = ((Boolean) value).booleanValue();
+ }
+ else if ("entity-resolver".equals(name))
+ {
+ entityResolver = (EntityResolver) value;
+ }
+ else if ("error-handler".equals(name))
+ {
+ errorHandler = (ErrorHandler) value;
+ }
+ else
+ {
+ throw new DomDOMException(DOMException.NOT_SUPPORTED_ERR);
+ }
+ // invalidate reader, a new one will be created
+ reader = null;
+ }
+
+ public Object getParameter(String name)
+ throws DOMException
+ {
+ name = name.toLowerCase();
+ if ("cdata-sections".equals(name))
+ {
+ return coalescing ? Boolean.FALSE : Boolean.TRUE;
+ }
+ else if ("comments".equals(name))
+ {
+ return ignoreComments ? Boolean.FALSE : Boolean.TRUE;
+ }
+ else if ("element-content-whitespace".equals(name))
+ {
+ return ignoreWhitespace ? Boolean.FALSE : Boolean.TRUE;
+ }
+ else if ("namespaces".equals(name))
+ {
+ return namespaceAware ? Boolean.TRUE : Boolean.FALSE;
+ }
+ else if ("expand-entity-references".equals(name))
+ {
+ return expandEntityReferences ? Boolean.TRUE : Boolean.FALSE;
+ }
+ else if ("coalescing".equals(name))
+ {
+ return coalescing ? Boolean.TRUE : Boolean.FALSE;
+ }
+ else if ("validating".equals(name))
+ {
+ return validating ? Boolean.TRUE : Boolean.FALSE;
+ }
+ else if ("xinclude-aware".equals(name))
+ {
+ return xIncludeAware ? Boolean.TRUE : Boolean.FALSE;
+ }
+ else if ("entity-resolver".equals(name))
+ {
+ return entityResolver;
+ }
+ else if ("error-handler".equals(name))
+ {
+ return errorHandler;
+ }
+ else
+ {
+ throw new DomDOMException(DOMException.NOT_SUPPORTED_ERR);
+ }
+ }
+
+ public boolean canSetParameter(String name, Object value)
+ {
+ return contains(name);
+ }
+
+ public DOMStringList getParameterNames()
+ {
+ return this;
+ }
+
+ // -- DOMStringList --
+
+ public String item(int i)
+ {
+ return (String) SUPPORTED_PARAMETERS.get(i);
+ }
+
+ public int getLength()
+ {
+ return SUPPORTED_PARAMETERS.size();
+ }
+
+ public boolean contains(String str)
+ {
+ return SUPPORTED_PARAMETERS.contains(str);
+ }
+
+ // -- ErrorHandler --
+
+ public void warning(SAXParseException e)
+ throws SAXException
+ {
+ if (errorHandler != null)
+ {
+ errorHandler.warning(e);
+ }
+ }
+
+ public void error(SAXParseException e)
+ throws SAXException
+ {
+ if (errorHandler != null)
+ {
+ errorHandler.error(e);
+ }
+ }
+
+ public void fatalError(SAXParseException e)
+ throws SAXException
+ {
+ if (errorHandler != null)
+ {
+ errorHandler.fatalError(e);
+ }
+ abort();
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/dom/ls/DomLSSerializer.java b/libjava/classpath/gnu/xml/dom/ls/DomLSSerializer.java
new file mode 100644
index 000000000..c282b0b9f
--- /dev/null
+++ b/libjava/classpath/gnu/xml/dom/ls/DomLSSerializer.java
@@ -0,0 +1,353 @@
+/* DomLSSerializer.java --
+ Copyright (C) 1999,2000,2001 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.dom.ls;
+
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.OutputStream;
+import java.io.StringWriter;
+import java.io.Writer;
+import java.net.HttpURLConnection;
+import java.net.MalformedURLException;
+import java.net.URL;
+import java.net.URLConnection;
+import java.util.Arrays;
+import java.util.List;
+import org.w3c.dom.DOMConfiguration;
+import org.w3c.dom.DOMException;
+import org.w3c.dom.DOMStringList;
+import org.w3c.dom.Node;
+import org.w3c.dom.ls.LSException;
+import org.w3c.dom.ls.LSOutput;
+import org.w3c.dom.ls.LSSerializer;
+import org.w3c.dom.ls.LSSerializerFilter;
+import org.w3c.dom.traversal.NodeFilter;
+import gnu.xml.dom.DomDOMException;
+import gnu.xml.transform.StreamSerializer;
+
+/**
+ * Serialize a DOM node to a stream.
+ *
+ * @author Chris Burdess
+ */
+public class DomLSSerializer
+ extends StreamSerializer
+ implements LSSerializer, DOMConfiguration, DOMStringList
+{
+
+ private static final List SUPPORTED_PARAMETERS =
+ Arrays.asList(new String[] {"discard-default-content",
+ "xml-declaration"});
+
+ private LSSerializerFilter filter;
+ private StreamSerializer serializer;
+
+ public DomLSSerializer()
+ {
+ super();
+ discardDefaultContent = true;
+ }
+
+ // -- LSSerializer --
+
+ public DOMConfiguration getDomConfig()
+ {
+ return this;
+ }
+
+ public String getNewLine()
+ {
+ return eol;
+ }
+
+ public void setNewLine(String newLine)
+ {
+ if (newLine == null)
+ {
+ newLine = System.getProperty("line.separator");
+ }
+ eol = newLine;
+ }
+
+ public LSSerializerFilter getFilter()
+ {
+ return filter;
+ }
+
+ public void setFilter(LSSerializerFilter filter)
+ {
+ this.filter = filter;
+ }
+
+ public boolean write(Node node, LSOutput output)
+ throws LSException
+ {
+ OutputStream out = output.getByteStream();
+ try
+ {
+ if (out == null)
+ {
+ String systemId = output.getSystemId();
+ try
+ {
+ URL url = new URL(systemId);
+ URLConnection connection = url.openConnection();
+ connection.setDoOutput(true);
+ if (connection instanceof HttpURLConnection)
+ {
+ ((HttpURLConnection) connection).setRequestMethod("PUT");
+ }
+ out = connection.getOutputStream();
+ }
+ catch (MalformedURLException e)
+ {
+ File file = new File(systemId);
+ out = new FileOutputStream(file);
+ }
+ }
+ serialize(node, out);
+ out.flush();
+ return true;
+ }
+ catch (IOException e)
+ {
+ throw new DomLSException(LSException.SERIALIZE_ERR, e);
+ }
+ }
+
+ public boolean writeToURI(Node node, String uri)
+ throws LSException
+ {
+ LSOutput output = new DomLSOutput();
+ output.setSystemId(uri);
+ return write(node, output);
+ }
+
+ public String writeToString(Node node)
+ throws DOMException, LSException
+ {
+ Writer writer = new StringWriter();
+ LSOutput output = new DomLSOutput();
+ output.setCharacterStream(writer);
+ write(node, output);
+ return writer.toString();
+ }
+
+ public void serialize(Node node, OutputStream out)
+ throws IOException
+ {
+ if (filter == null)
+ {
+ super.serialize(node, out);
+ }
+ else
+ {
+ int wts = filter.getWhatToShow();
+ if (wts != NodeFilter.SHOW_ALL)
+ {
+ switch (node.getNodeType())
+ {
+ case Node.ATTRIBUTE_NODE:
+ if ((wts & NodeFilter.SHOW_ATTRIBUTE) == 0)
+ {
+ super.serialize(node, out);
+ return;
+ }
+ break;
+ case Node.TEXT_NODE:
+ if ((wts & NodeFilter.SHOW_TEXT) == 0)
+ {
+ super.serialize(node, out);
+ return;
+ }
+ break;
+ case Node.ELEMENT_NODE:
+ if ((wts & NodeFilter.SHOW_ELEMENT) == 0)
+ {
+ super.serialize(node, out);
+ return;
+ }
+ break;
+ case Node.CDATA_SECTION_NODE:
+ if ((wts & NodeFilter.SHOW_CDATA_SECTION) == 0)
+ {
+ super.serialize(node, out);
+ return;
+ }
+ break;
+ case Node.COMMENT_NODE:
+ if ((wts & NodeFilter.SHOW_COMMENT) == 0)
+ {
+ super.serialize(node, out);
+ return;
+ }
+ break;
+ case Node.DOCUMENT_NODE:
+ if ((wts & NodeFilter.SHOW_DOCUMENT) == 0)
+ {
+ super.serialize(node, out);
+ return;
+ }
+ break;
+ case Node.DOCUMENT_TYPE_NODE:
+ if ((wts & NodeFilter.SHOW_DOCUMENT_TYPE) == 0)
+ {
+ super.serialize(node, out);
+ return;
+ }
+ break;
+ case Node.PROCESSING_INSTRUCTION_NODE:
+ if ((wts & NodeFilter.SHOW_PROCESSING_INSTRUCTION) == 0)
+ {
+ super.serialize(node, out);
+ return;
+ }
+ break;
+ case Node.DOCUMENT_FRAGMENT_NODE:
+ if ((wts & NodeFilter.SHOW_DOCUMENT_FRAGMENT) == 0)
+ {
+ super.serialize(node, out);
+ return;
+ }
+ break;
+ case Node.ENTITY_NODE:
+ if ((wts & NodeFilter.SHOW_ENTITY) == 0)
+ {
+ super.serialize(node, out);
+ return;
+ }
+ break;
+ case Node.ENTITY_REFERENCE_NODE:
+ if ((wts & NodeFilter.SHOW_ENTITY_REFERENCE) == 0)
+ {
+ super.serialize(node, out);
+ return;
+ }
+ break;
+ case Node.NOTATION_NODE:
+ if ((wts & NodeFilter.SHOW_NOTATION) == 0)
+ {
+ super.serialize(node, out);
+ return;
+ }
+ break;
+ }
+ }
+ switch (filter.acceptNode(node))
+ {
+ case NodeFilter.FILTER_ACCEPT:
+ super.serialize(node, out);
+ break;
+ case NodeFilter.FILTER_REJECT:
+ break;
+ case NodeFilter.FILTER_SKIP:
+ Node first = node.getFirstChild();
+ if (first != null)
+ {
+ serialize(first, out);
+ }
+ break;
+ }
+ }
+ }
+
+ // -- DOMConfiguration --
+
+ public void setParameter(String name, Object value)
+ throws DOMException
+ {
+ if ("discard-default-content".equals(name))
+ {
+ discardDefaultContent = "true".equals(value.toString());
+ }
+ else if ("xml-declaration".equals(name))
+ {
+ xmlDeclaration = "false".equals(value.toString());
+ }
+ else
+ {
+ throw new DomDOMException(DOMException.NOT_SUPPORTED_ERR);
+ }
+ }
+
+ public Object getParameter(String name)
+ throws DOMException
+ {
+ if ("discard-default-content".equals(name))
+ {
+ return discardDefaultContent ? "true" : "false";
+ }
+ else if ("xml-declaration".equals(name))
+ {
+ return xmlDeclaration ? "true" : "false";
+ }
+ else
+ {
+ throw new DomDOMException(DOMException.NOT_SUPPORTED_ERR);
+ }
+ }
+
+ public boolean canSetParameter(String name, Object value)
+ {
+ return contains(name);
+ }
+
+ public DOMStringList getParameterNames()
+ {
+ return this;
+ }
+
+ // -- DOMStringList --
+
+ public String item(int i)
+ {
+ return (String) SUPPORTED_PARAMETERS.get(i);
+ }
+
+ public int getLength()
+ {
+ return SUPPORTED_PARAMETERS.size();
+ }
+
+ public boolean contains(String str)
+ {
+ return SUPPORTED_PARAMETERS.contains(str);
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/dom/ls/FilteredSAXEventSink.java b/libjava/classpath/gnu/xml/dom/ls/FilteredSAXEventSink.java
new file mode 100644
index 000000000..65c1d37a4
--- /dev/null
+++ b/libjava/classpath/gnu/xml/dom/ls/FilteredSAXEventSink.java
@@ -0,0 +1,353 @@
+/* FilteredSAXEventSink.java --
+ Copyright (C) 1999,2000,2001 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.dom.ls;
+
+import java.util.LinkedList;
+import org.w3c.dom.Attr;
+import org.w3c.dom.Element;
+import org.w3c.dom.Node;
+import org.w3c.dom.Text;
+import org.w3c.dom.ls.LSParserFilter;
+import org.w3c.dom.traversal.NodeFilter;
+import org.xml.sax.Attributes;
+import org.xml.sax.SAXException;
+
+/**
+ * A SAX event sink that calls out to a parser filter in order to decide
+ * whether to insert nodes into the tree.
+ *
+ * @author Chris Burdess
+ */
+class FilteredSAXEventSink
+ extends SAXEventSink
+{
+
+ final LSParserFilter filter;
+ final int whatToShow;
+
+ /**
+ * Stack of elements to insert.
+ */
+ LinkedList nodes;
+
+ /**
+ * Corresponding stack of filter decisions about the nodes.
+ */
+ LinkedList decisions;
+
+ /**
+ * True when rejecting child nodes.
+ */
+ boolean rejecting;
+
+ FilteredSAXEventSink(LSParserFilter filter)
+ {
+ this.filter = filter;
+ whatToShow = filter.getWhatToShow();
+ }
+
+ public void startDocument()
+ throws SAXException
+ {
+ if (interrupted)
+ {
+ return;
+ }
+ nodes = new LinkedList();
+ decisions = new LinkedList();
+
+ super.startDocument();
+ }
+
+ public void endDocument()
+ throws SAXException
+ {
+ if (interrupted)
+ {
+ return;
+ }
+ super.endDocument();
+
+ switch (getDecision(ctx, false))
+ {
+ case LSParserFilter.FILTER_REJECT:
+ ctx = null;
+ doc = null;
+ break;
+ }
+
+ nodes = null;
+ decisions = null;
+ }
+
+ public void startElement(String uri, String localName, String qName,
+ Attributes atts)
+ throws SAXException
+ {
+ if (rejecting || interrupted)
+ {
+ return;
+ }
+ Element element = createElement(uri, localName, qName, atts);
+ ctx = element;
+
+ short decision = getDecision(element, true);
+ nodes.addLast(element);
+ decisions.addLast(new Short(decision));
+
+ switch (decision)
+ {
+ case LSParserFilter.FILTER_REJECT:
+ rejecting = true;
+ break;
+ case LSParserFilter.FILTER_INTERRUPT:
+ interrupted = true;
+ break;
+ }
+ }
+
+ protected Attr createAttr(Attributes atts, int index)
+ {
+ Attr attr = super.createAttr(atts, index);
+ short decision = getDecision(attr, false);
+ switch (decision)
+ {
+ case LSParserFilter.FILTER_REJECT:
+ return null;
+ case LSParserFilter.FILTER_INTERRUPT:
+ interrupted = true;
+ return null;
+ }
+ return attr;
+ }
+
+ public void endElement(String uri, String localName, String qName)
+ throws SAXException
+ {
+ if (rejecting || interrupted)
+ {
+ return;
+ }
+ super.endElement(uri, localName, qName);
+
+ Element element = (Element) nodes.removeLast();
+ Node parent = nodes.isEmpty() ? doc : (Node) nodes.getLast();
+ ctx = parent;
+ short decision = ((Short) decisions.removeLast()).shortValue();
+ switch (decision)
+ {
+ case LSParserFilter.FILTER_SKIP:
+ // Add all children of element to parent
+ for (Node child = element.getFirstChild(); child != null;
+ child = child.getNextSibling())
+ {
+ parent.insertBefore(child, element);
+ }
+ return;
+ case LSParserFilter.FILTER_REJECT:
+ rejecting = false;
+ break;
+ }
+ decision = getDecision(element, false);
+ switch (decision)
+ {
+ case LSParserFilter.FILTER_ACCEPT:
+ parent.appendChild(element);
+ break;
+ case LSParserFilter.FILTER_INTERRUPT:
+ interrupted = true;
+ break;
+ }
+ }
+
+ public void characters(char[] c, int off, int len)
+ throws SAXException
+ {
+ if (rejecting || interrupted)
+ {
+ return;
+ }
+ Text text = createText(c, off, len);
+ short decision = getDecision(text, false);
+ switch (decision)
+ {
+ case LSParserFilter.FILTER_ACCEPT:
+ ctx.appendChild(text);
+ break;
+ case LSParserFilter.FILTER_INTERRUPT:
+ interrupted = true;
+ break;
+ }
+ }
+
+ public void processingInstruction(String target, String data)
+ throws SAXException
+ {
+ if (rejecting || interrupted || inDTD)
+ {
+ return;
+ }
+ Node pi = createProcessingInstruction(target, data);
+ short decision = getDecision(pi, false);
+ switch (decision)
+ {
+ case LSParserFilter.FILTER_ACCEPT:
+ ctx.appendChild(pi);
+ break;
+ case LSParserFilter.FILTER_INTERRUPT:
+ interrupted = true;
+ break;
+ }
+ }
+
+ public void startDTD(String name, String publicId, String systemId)
+ throws SAXException
+ {
+ if (interrupted)
+ {
+ return;
+ }
+ Node doctype = createDocumentType(name, publicId, systemId);
+ ctx = doctype;
+ inDTD = true;
+ nodes.addLast(doctype);
+ decisions.addLast(new Short(LSParserFilter.FILTER_ACCEPT));
+ }
+
+ public void endDTD()
+ throws SAXException
+ {
+ if (interrupted)
+ {
+ return;
+ }
+ Node doctype = (Node) nodes.removeLast();
+ decisions.removeLast();
+ inDTD = false;
+ ctx = doc;
+ short decision = getDecision(doctype, false);
+ switch (decision)
+ {
+ case LSParserFilter.FILTER_ACCEPT:
+ ctx.appendChild(doctype);
+ break;
+ case LSParserFilter.FILTER_INTERRUPT:
+ interrupted = true;
+ break;
+ }
+ }
+
+ public void comment(char[] c, int off, int len)
+ throws SAXException
+ {
+ if (rejecting || interrupted || inDTD)
+ {
+ return;
+ }
+ Node comment = createComment(c, off, len);
+ short decision = getDecision(comment, false);
+ switch (decision)
+ {
+ case LSParserFilter.FILTER_ACCEPT:
+ ctx.appendChild(comment);
+ break;
+ case LSParserFilter.FILTER_INTERRUPT:
+ interrupted = true;
+ break;
+ }
+ }
+
+ // TODO declarations
+
+ short getDecision(Node node, boolean start)
+ {
+ boolean show = (whatToShow == NodeFilter.SHOW_ALL);
+ if (!show)
+ {
+ switch (node.getNodeType())
+ {
+ case Node.ATTRIBUTE_NODE:
+ show = ((whatToShow & NodeFilter.SHOW_ATTRIBUTE) != 0);
+ break;
+ case Node.TEXT_NODE:
+ show = ((whatToShow & NodeFilter.SHOW_TEXT) != 0);
+ break;
+ case Node.CDATA_SECTION_NODE:
+ show = ((whatToShow & NodeFilter.SHOW_CDATA_SECTION) != 0);
+ break;
+ case Node.ELEMENT_NODE:
+ show = ((whatToShow & NodeFilter.SHOW_ELEMENT) != 0);
+ break;
+ case Node.COMMENT_NODE:
+ show = ((whatToShow & NodeFilter.SHOW_COMMENT) != 0);
+ break;
+ case Node.DOCUMENT_NODE:
+ show = ((whatToShow & NodeFilter.SHOW_DOCUMENT) != 0);
+ break;
+ case Node.PROCESSING_INSTRUCTION_NODE:
+ show = ((whatToShow & NodeFilter.SHOW_PROCESSING_INSTRUCTION) != 0);
+ break;
+ case Node.DOCUMENT_FRAGMENT_NODE:
+ show = ((whatToShow & NodeFilter.SHOW_DOCUMENT_FRAGMENT) != 0);
+ break;
+ case Node.DOCUMENT_TYPE_NODE:
+ show = ((whatToShow & NodeFilter.SHOW_DOCUMENT_TYPE) != 0);
+ break;
+ case Node.ENTITY_REFERENCE_NODE:
+ show = ((whatToShow & NodeFilter.SHOW_ENTITY_REFERENCE) != 0);
+ break;
+ case Node.ENTITY_NODE:
+ show = ((whatToShow & NodeFilter.SHOW_ENTITY) != 0);
+ break;
+ case Node.NOTATION_NODE:
+ show = ((whatToShow & NodeFilter.SHOW_NOTATION) != 0);
+ break;
+ }
+ }
+ if (!show)
+ {
+ return LSParserFilter.FILTER_ACCEPT;
+ }
+ if (start)
+ {
+ return filter.startElement((Element) node);
+ }
+ return filter.acceptNode(node);
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/dom/ls/ReaderInputStream.java b/libjava/classpath/gnu/xml/dom/ls/ReaderInputStream.java
new file mode 100644
index 000000000..cf279ab39
--- /dev/null
+++ b/libjava/classpath/gnu/xml/dom/ls/ReaderInputStream.java
@@ -0,0 +1,236 @@
+/* ReaderInputStream.java --
+ Copyright (C) 1999, 2000, 2001, 2004 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.dom.ls;
+
+import java.io.InputStream;
+import java.io.IOException;
+import java.io.Reader;
+
+/**
+ * Character stream wrapper.
+ *
+ * @author Chris Burdess
+ * @author Mark Wielaard
+ */
+public class ReaderInputStream
+ extends InputStream
+{
+
+ private Reader reader;
+ private String encoding;
+
+ // Holds extra spillover data if necessary
+ private byte extra[];
+ private int pos;
+
+ private byte extra_marked[];
+ private int pos_marked;
+
+ public ReaderInputStream(Reader reader)
+ {
+ this.reader = reader;
+ this.encoding = "UTF-8";
+ }
+
+ void setEncoding(String encoding)
+ {
+ this.encoding = encoding;
+ }
+
+ public int read()
+ throws IOException
+ {
+ if (extra != null)
+ {
+ int result = extra[pos];
+ pos++;
+ if (pos >= extra.length)
+ {
+ extra = null;
+ }
+ return result;
+ }
+ return reader.read();
+ }
+
+ public int read(byte[] b)
+ throws IOException
+ {
+ return read(b, 0, b.length);
+ }
+
+ public int read(byte[] b, int off, int len)
+ throws IOException
+ {
+ if (len == 0)
+ {
+ return 0;
+ }
+
+ if (extra != null)
+ {
+ int available = extra.length - pos;
+ int l = available < len ? available : len;
+ System.arraycopy(extra, 0, b, off, l);
+ pos += l;
+ if (pos >= extra.length)
+ {
+ extra = null;
+ }
+ return l;
+ }
+
+ char[] c = new char[len];
+ int l = reader.read(c, 0, len);
+ if (l == -1)
+ {
+ return -1;
+ }
+
+ String s = new String(c, 0, l);
+ byte[] d = s.getBytes(encoding);
+
+ int available = d.length;
+ int more = d.length - len;
+ if (more > 0)
+ {
+ extra = new byte[more];
+ pos = 0;
+ System.arraycopy(d, len, extra, 0, more);
+ available -= more;
+ }
+
+ System.arraycopy(d, 0, b, off, available);
+ return available;
+ }
+
+ public void close()
+ throws IOException
+ {
+ reader.close();
+ }
+
+ public boolean markSupported()
+ {
+ return reader.markSupported();
+ }
+
+ public void mark(int limit)
+ {
+ if (extra != null)
+ {
+ extra_marked = new byte[extra.length];
+ System.arraycopy(extra, 0, extra_marked, 0, extra.length);
+ pos_marked = pos;
+ }
+ else
+ {
+ extra_marked = null;
+ }
+
+ try
+ {
+ // Note that this might be a bit more than asked for.
+ // Because we might also have the extra_marked bytes.
+ // That is fine (and necessary for reset() to work).
+ reader.mark(limit);
+ }
+ catch (IOException ioe)
+ {
+ throw new RuntimeException(ioe);
+ }
+ }
+
+ public void reset()
+ throws IOException
+ {
+ extra = extra_marked;
+ pos = pos_marked;
+ extra_marked = null;
+
+ reader.reset();
+ }
+
+ public long skip(long n)
+ throws IOException
+ {
+ long done = 0;
+ if (extra != null)
+ {
+ int available = extra.length - pos;
+ done = available < n ? available : n;
+ pos += done;
+ if (pos >= extra.length)
+ {
+ extra = null;
+ }
+ }
+
+ n -= done;
+ if (n > 0)
+ {
+ return reader.skip(n) + done;
+ }
+ else
+ {
+ return done;
+ }
+ }
+
+ /**
+ * Returns conservative number of bytes available without blocking.
+ * Actual number of bytes that can be read without blocking might
+ * be (much) bigger.
+ */
+ public int available()
+ throws IOException
+ {
+ if (extra != null)
+ {
+ return pos - extra.length;
+ }
+
+ return reader.ready() ? 1 : 0;
+ }
+
+ public String toString()
+ {
+ return getClass().getName() + "[" + reader + ", " + encoding + "]";
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/dom/ls/SAXEventSink.java b/libjava/classpath/gnu/xml/dom/ls/SAXEventSink.java
new file mode 100644
index 000000000..06333dd7e
--- /dev/null
+++ b/libjava/classpath/gnu/xml/dom/ls/SAXEventSink.java
@@ -0,0 +1,603 @@
+/* SAXEventSink.java --
+ Copyright (C) 1999,2000,2001 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.dom.ls;
+
+import java.util.HashSet;
+import java.util.Iterator;
+import java.util.LinkedList;
+import java.util.List;
+import javax.xml.XMLConstants;
+import org.w3c.dom.Attr;
+import org.w3c.dom.Document;
+import org.w3c.dom.DocumentType;
+import org.w3c.dom.Element;
+import org.w3c.dom.Entity;
+import org.w3c.dom.EntityReference;
+import org.w3c.dom.NamedNodeMap;
+import org.w3c.dom.Node;
+import org.w3c.dom.Text;
+import org.xml.sax.Attributes;
+import org.xml.sax.ContentHandler;
+import org.xml.sax.DTDHandler;
+import org.xml.sax.Locator;
+import org.xml.sax.SAXException;
+import org.xml.sax.SAXNotRecognizedException;
+import org.xml.sax.SAXNotSupportedException;
+import org.xml.sax.XMLReader;
+import org.xml.sax.ext.Attributes2;
+import org.xml.sax.ext.DeclHandler;
+import org.xml.sax.ext.LexicalHandler;
+import org.xml.sax.ext.Locator2;
+import gnu.xml.dom.DomAttr;
+import gnu.xml.dom.DomDocument;
+import gnu.xml.dom.DomDoctype;
+import gnu.xml.dom.DomNode;
+
+/**
+ * A SAX content and lexical handler used to construct a DOM document.
+ *
+ * @author Chris Burdess
+ */
+public class SAXEventSink
+ implements ContentHandler, LexicalHandler, DTDHandler, DeclHandler
+{
+
+ private static final String XMLNS_URI = XMLConstants.XMLNS_ATTRIBUTE_NS_URI;
+ private static final String XMLNS_PREFIX = XMLConstants.XMLNS_ATTRIBUTE;
+ private static final HashSet PREDEFINED_ENTITIES = new HashSet();
+ static
+ {
+ PREDEFINED_ENTITIES.add("amp");
+ PREDEFINED_ENTITIES.add("lt");
+ PREDEFINED_ENTITIES.add("gt");
+ PREDEFINED_ENTITIES.add("quot");
+ PREDEFINED_ENTITIES.add("apos");
+ }
+
+ private boolean namespaceAware;
+ boolean ignoreWhitespace;
+ boolean expandEntityReferences;
+ boolean ignoreComments;
+ boolean coalescing;
+
+ XMLReader reader; // reference back to the parser to get features
+
+ DomDocument doc; // document being constructed
+ Node ctx; // current context (parent node)
+ LinkedList entityCtx; // entity context
+ List pending; // namespace nodes waiting for a declaring element
+ Locator locator;
+ boolean inCDATA;
+ boolean inDTD;
+ boolean interrupted;
+
+ void interrupt()
+ {
+ interrupted = true;
+ }
+
+ public Document getDocument()
+ {
+ return doc;
+ }
+
+ public void setReader(XMLReader reader)
+ {
+ this.reader = reader;
+ }
+
+ // -- ContentHandler2 --
+
+ public void setDocumentLocator(Locator locator)
+ {
+ this.locator = locator;
+ }
+
+ public void setNamespaceAware(boolean namespaceAware)
+ {
+ this.namespaceAware = namespaceAware;
+ }
+
+ public void startDocument()
+ throws SAXException
+ {
+ if (namespaceAware)
+ {
+ pending = new LinkedList();
+ }
+ doc = new DomDocument();
+ doc.setStrictErrorChecking(false);
+ doc.setBuilding(true);
+ doc.setDefaultAttributes(false);
+ ctx = doc;
+
+ final String FEATURES = "http://xml.org/sax/features/";
+ final String PROPERTIES = "http://xml.org/sax/properties/";
+ final String GNU_PROPERTIES = "http://gnu.org/sax/properties/";
+
+ if (reader != null)
+ {
+ boolean standalone = reader.getFeature(FEATURES + "is-standalone");
+ doc.setXmlStandalone(standalone);
+ try
+ {
+ String version = (String) reader.getProperty(PROPERTIES +
+ "document-xml-version");
+ doc.setXmlVersion(version);
+ }
+ catch (SAXNotRecognizedException e)
+ {
+ }
+ catch (SAXNotSupportedException e)
+ {
+ }
+ try
+ {
+ String encoding = (String) reader.getProperty(GNU_PROPERTIES +
+ "document-xml-encoding");
+ doc.setXmlEncoding(encoding);
+ }
+ catch (SAXNotRecognizedException e)
+ {
+ }
+ catch (SAXNotSupportedException e)
+ {
+ }
+ }
+ if (locator != null && locator instanceof Locator2)
+ {
+ String encoding = ((Locator2) locator).getEncoding();
+ doc.setInputEncoding(encoding);
+ }
+ }
+
+ public void endDocument()
+ throws SAXException
+ {
+ doc.setStrictErrorChecking(true);
+ doc.setBuilding(false);
+ doc.setDefaultAttributes(true);
+ DomDoctype doctype = (DomDoctype) doc.getDoctype();
+ if (doctype != null)
+ {
+ doctype.makeReadonly();
+ }
+ ctx = null;
+ locator = null;
+ }
+
+ public void startPrefixMapping(String prefix, String uri)
+ throws SAXException
+ {
+ if (namespaceAware)
+ {
+ String nsName = (prefix != null && prefix.length() > 0) ?
+ XMLNS_PREFIX + ":" + prefix : XMLNS_PREFIX;
+ DomAttr ns = (DomAttr) doc.createAttributeNS(XMLNS_URI, nsName);
+ ns.setNodeValue(uri);
+ if (ctx.getNodeType() == Node.ATTRIBUTE_NODE)
+ {
+ // Add to owner element
+ Node target = ((Attr) ctx).getOwnerElement();
+ target.getAttributes().setNamedItemNS(ns);
+ }
+ else
+ {
+ // Add to pending list; namespace node will be inserted when
+ // element is seen
+ pending.add(ns);
+ }
+ }
+ }
+
+ public void endPrefixMapping(String prefix)
+ throws SAXException
+ {
+ }
+
+ public void startElement(String uri, String localName, String qName,
+ Attributes atts)
+ throws SAXException
+ {
+ if (interrupted)
+ {
+ return;
+ }
+ Element element = createElement(uri, localName, qName, atts);
+ // add element to context
+ ctx.appendChild(element);
+ ctx = element;
+ }
+
+ protected Element createElement(String uri, String localName, String qName,
+ Attributes atts)
+ throws SAXException
+ {
+ // create element node
+ Element element = namespaceAware ?
+ doc.createElementNS(uri, qName) :
+ doc.createElement(qName);
+ NamedNodeMap attrs = element.getAttributes();
+ if (namespaceAware && !pending.isEmpty())
+ {
+ // add pending namespace nodes
+ for (Iterator i = pending.iterator(); i.hasNext(); )
+ {
+ Node ns = (Node) i.next();
+ attrs.setNamedItemNS(ns);
+ }
+ pending.clear();
+ }
+ // add attributes
+ int len = atts.getLength();
+ for (int i = 0; i < len; i++)
+ {
+ // create attribute
+ Attr attr = createAttr(atts, i);
+ if (attr != null)
+ {
+ // add attribute to element
+ if (namespaceAware)
+ {
+ attrs.setNamedItemNS(attr);
+ }
+ else
+ {
+ attrs.setNamedItem(attr);
+ }
+ }
+ }
+ return element;
+ }
+
+ protected Attr createAttr(Attributes atts, int index)
+ {
+ DomAttr attr;
+ if (namespaceAware)
+ {
+ String a_uri = atts.getURI(index);
+ String a_qName = atts.getQName(index);
+ attr = (DomAttr) doc.createAttributeNS(a_uri, a_qName);
+ }
+ else
+ {
+ String a_qName = atts.getQName(index);
+ attr = (DomAttr) doc.createAttribute(a_qName);
+ }
+ attr.setNodeValue(atts.getValue(index));
+ if (atts instanceof Attributes2)
+ {
+ Attributes2 atts2 = (Attributes2) atts;
+ // TODO attr.setDeclared(atts2.isDeclared(index));
+ attr.setSpecified(atts2.isSpecified(index));
+ }
+ return attr;
+ }
+
+ public void endElement(String uri, String localName, String qName)
+ throws SAXException
+ {
+ if (interrupted)
+ {
+ return;
+ }
+ if (namespaceAware)
+ {
+ pending.clear();
+ }
+ ctx = ctx.getParentNode();
+ }
+
+ public void characters(char[] c, int off, int len)
+ throws SAXException
+ {
+ if (interrupted || len < 1)
+ {
+ return;
+ }
+ ctx.appendChild(createText(c, off, len));
+ }
+
+ protected Text createText(char[] c, int off, int len)
+ throws SAXException
+ {
+ Text text = (inCDATA && !coalescing) ?
+ doc.createCDATASection(new String(c, off, len)) :
+ doc.createTextNode(new String(c, off, len));
+ return text;
+ }
+
+ public void ignorableWhitespace(char[] c, int off, int len)
+ throws SAXException
+ {
+ if (interrupted)
+ {
+ return;
+ }
+ if (!ignoreWhitespace)
+ {
+ characters(c, off, len);
+ }
+ }
+
+ public void processingInstruction(String target, String data)
+ throws SAXException
+ {
+ if (interrupted)
+ {
+ return;
+ }
+ Node pi = createProcessingInstruction(target, data);
+ ctx.appendChild(pi);
+ }
+
+ protected Node createProcessingInstruction(String target, String data)
+ {
+ return doc.createProcessingInstruction(target, data);
+ }
+
+ public void skippedEntity(String name)
+ throws SAXException
+ {
+ // This callback is totally pointless
+ }
+
+ // -- LexicalHandler --
+
+ public void startDTD(String name, String publicId, String systemId)
+ throws SAXException
+ {
+ if (interrupted)
+ {
+ return;
+ }
+ Node doctype = createDocumentType(name, publicId, systemId);
+ doc.appendChild(doctype);
+ ctx = doctype;
+ inDTD = true;
+ }
+
+ protected Node createDocumentType(String name, String publicId,
+ String systemId)
+ {
+ return new DomDoctype(doc, name, publicId, systemId);
+ }
+
+ public void endDTD()
+ throws SAXException
+ {
+ if (interrupted)
+ {
+ return;
+ }
+ inDTD = false;
+ ctx = ctx.getParentNode();
+ }
+
+ public void startEntity(String name)
+ throws SAXException
+ {
+ if (interrupted)
+ return;
+ DocumentType doctype = doc.getDoctype();
+ if (doctype == null)
+ {
+ throw new SAXException("SAX parser error: " +
+ "reference to entity in undeclared doctype");
+ }
+ if ("[dtd]".equals(name) || name.charAt(0) == '%')
+ return;
+ if (PREDEFINED_ENTITIES.contains(name))
+ return;
+ // Get entity
+ NamedNodeMap entities = doctype.getEntities();
+ Entity entity = (Entity) entities.getNamedItem(name);
+ if (entity == null)
+ {
+ throw new SAXException("SAX parser error: " +
+ "reference to undeclared entity: " + name);
+ }
+ EntityReference ref = doc.createEntityReference(name);
+ // DomDocument populates with the entity replacement text, remove this
+ Node child = ref.getFirstChild();
+ while (child != null)
+ {
+ Node nextChild = child.getNextSibling();
+ ref.removeChild(child);
+ child = nextChild;
+ }
+ ctx.appendChild(ref);
+ ctx = ref;
+ }
+
+ public void endEntity(String name)
+ throws SAXException
+ {
+ if (interrupted)
+ return;
+ if ("[dtd]".equals(name) || name.charAt(0) == '%')
+ return;
+ if (PREDEFINED_ENTITIES.contains(name))
+ return;
+ // Get entity reference
+ EntityReference ref = (EntityReference) ctx;
+ if (!ref.getNodeName().equals(name))
+ throw new SAXException("expecting end of "+ref.getNodeName()+" entity");
+ ctx = ctx.getParentNode();
+ if (ref instanceof DomNode)
+ ((DomNode) ref).makeReadonly();
+ if (expandEntityReferences)
+ {
+ // Move entity content from reference node onto context
+ Node child = ref.getFirstChild();
+ while (child != null)
+ {
+ Node nextChild = child.getNextSibling();
+ ctx.appendChild(child);
+ child = nextChild;
+ }
+ ctx.removeChild(ref);
+ }
+ }
+
+ public void startCDATA()
+ throws SAXException
+ {
+ inCDATA = true;
+ }
+
+ public void endCDATA()
+ throws SAXException
+ {
+ inCDATA = false;
+ }
+
+ public void comment(char[] c, int off, int len)
+ throws SAXException
+ {
+ if (interrupted)
+ {
+ return;
+ }
+ Node comment = createComment(c, off, len);
+ ctx.appendChild(comment);
+ }
+
+ protected Node createComment(char[] c, int off, int len)
+ {
+ return doc.createComment(new String(c, off, len));
+ }
+
+ // -- DTDHandler --
+
+ public void notationDecl(String name, String publicId, String systemId)
+ throws SAXException
+ {
+ if (interrupted)
+ {
+ return;
+ }
+ if (!inDTD)
+ throw new SAXException("notation decl outside DTD");
+ DomDoctype doctype = (DomDoctype) ctx;
+ doctype.declareNotation(name, publicId, systemId);
+ }
+
+ public void unparsedEntityDecl(String name, String publicId, String systemId,
+ String notationName)
+ throws SAXException
+ {
+ if (interrupted)
+ {
+ return;
+ }
+ if (!inDTD)
+ throw new SAXException("unparsed entity decl outside DTD");
+ DomDoctype doctype = (DomDoctype) ctx;
+ Entity entity = doctype.declareEntity(name, publicId, systemId,
+ notationName);
+ }
+
+ // -- DeclHandler --
+
+ public void elementDecl(String name, String model)
+ throws SAXException
+ {
+ if (interrupted)
+ {
+ return;
+ }
+ if (!inDTD)
+ throw new SAXException("element decl outside DTD");
+ // Ignore fake element declarations generated by ValidationConsumer.
+ // If an element is not really declared in the DTD it will not be
+ // declared in the document model.
+ if (!(ctx instanceof DomDoctype))
+ {
+ return;
+ }
+ DomDoctype doctype = (DomDoctype) ctx;
+ doctype.elementDecl(name, model);
+ }
+
+ public void attributeDecl(String eName, String aName, String type,
+ String mode, String value)
+ throws SAXException
+ {
+ if (interrupted)
+ {
+ return;
+ }
+ if (!inDTD)
+ throw new SAXException("attribute decl outside DTD");
+ DomDoctype doctype = (DomDoctype) ctx;
+ doctype.attributeDecl(eName, aName, type, mode, value);
+ }
+
+ public void internalEntityDecl(String name, String value)
+ throws SAXException
+ {
+ if (interrupted)
+ {
+ return;
+ }
+ if (!inDTD)
+ throw new SAXException("internal entity decl outside DTD");
+ DomDoctype doctype = (DomDoctype) ctx;
+ Entity entity = doctype.declareEntity(name, null, null, null);
+ if (entity != null)
+ {
+ Node text = doc.createTextNode(value);
+ entity.appendChild(text);
+ }
+ }
+
+ public void externalEntityDecl(String name, String publicId, String systemId)
+ throws SAXException
+ {
+ if (interrupted)
+ {
+ return;
+ }
+ if (!inDTD)
+ throw new SAXException("external entity decl outside DTD");
+ DomDoctype doctype = (DomDoctype) ctx;
+ Entity entity = doctype.declareEntity(name, publicId, systemId, null);
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/dom/ls/WriterOutputStream.java b/libjava/classpath/gnu/xml/dom/ls/WriterOutputStream.java
new file mode 100644
index 000000000..f1ae344f4
--- /dev/null
+++ b/libjava/classpath/gnu/xml/dom/ls/WriterOutputStream.java
@@ -0,0 +1,97 @@
+/* WriterOutputStream.java --
+ Copyright (C) 1999,2000,2001 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.dom.ls;
+
+import java.io.IOException;
+import java.io.OutputStream;
+import java.io.Writer;
+
+/**
+ * Character stream wrapper.
+ *
+ * @author Chris Burdess
+ */
+public class WriterOutputStream
+ extends OutputStream
+{
+
+ private Writer writer;
+ private String encoding;
+
+ public WriterOutputStream(Writer writer)
+ {
+ this.writer = writer;
+ this.encoding = "UTF-8";
+ }
+
+ void setEncoding(String encoding)
+ {
+ this.encoding = encoding;
+ }
+
+ public void write(int c)
+ throws IOException
+ {
+ writer.write(c);
+ }
+
+ public void write(byte[] b)
+ throws IOException
+ {
+ write(b, 0, b.length);
+ }
+
+ public void write(byte[] b, int off, int len)
+ throws IOException
+ {
+ writer.write(new String(b, off, len, encoding));
+ }
+
+ public void close()
+ throws IOException
+ {
+ writer.close();
+ }
+
+ public void flush()
+ throws IOException
+ {
+ writer.flush();
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/dom/package.html b/libjava/classpath/gnu/xml/dom/package.html
new file mode 100644
index 000000000..fbc864a4d
--- /dev/null
+++ b/libjava/classpath/gnu/xml/dom/package.html
@@ -0,0 +1,273 @@
+
+
+This is a Free Software DOM Level 3 implementation, supporting these features: +
+Note that while DOM does not specify its behavior in the +face of concurrent access, this implementation does. +Specifically: +
+A number of DOM implementations are available in Java, including +commercial ones from Sun, IBM, Oracle, and DataChannel as well as +noncommercial ones from Docuverse, OpenXML, and Silfide. Why have +another? Some of the goals of this version: +
+ ++This also works with the GNU Compiler for Java (GCJ). GCJ promises +to be quite the environment for programming Java, both directly and from +C++ using the new CNI interfaces (which really use C++, unlike JNI).
+ + +At this writing:
++I ran a profiler a few times and remove some of the performance hotspots, +but it's not tuned. Reporting mutation events, in particular, is +rather costly -- it started at about a 40% penalty for appendNode calls, +I've got it down around 12%, but it'll be hard to shrink it much further. +The overall code size is relatively small, though you may want to be rid of +many of the unused DOM interface classes (HTML, CSS, and so on). +
+ + +Starting with DOM Level 2, you can really see that DOM is constructed +as a bunch of optional modules around a core of either XML or HTML +functionality. Different implementations will support different optional +modules. This implementation provides a set of features that should be +useful if you're not depending on the HTML functionality (lots of convenience +functions that mostly don't buy much except API surface area) and user +interface support. That is, browsers will want more -- but what they +need should be cleanly layered over what's already here.
+ +This DOM implementation supports the "XML" feature set, which basically +gets you four things over the bare core (which you're officially not supposed +to implement except in conjunction with the "XML" or "HTML" feature). In +order of decreasing utility, those four things are:
Events may be one of the more interesting new features in Level 2. +This package provides the core feature set and exposes mutation events. +No gooey events though; if you want that, write a layered implementation!
+ +Three mutation events aren't currently generated:
In addition, certain kinds of attribute modification aren't reported. +A fix is known, but it couldn't report the previous value of the attribute. +More work could fix all of this (as well as reduce the generally high cost +of childful attributes), but that's not been done yet.
+ +Also, note that it is a Bad Thing to have the listener +for a mutation event change the ancestry for the target of that event. +Or to prevent mutation events from bubbling to where they're needed. +Just don't do those, OK?
+ +As an experimental feature (named "USER-Events"), you can provide +your own "user" events. Just name them anything starting with "USER-" +and you're set. Dispatch them through, bubbling, capturing, or what +ever takes your fancy. One important thing you can't currently do is +pass any data (like an object) with those events. Maybe later there +will be a "UserEvent" interface letting you get some substantial use +out of this mechanism even if you're not "inside" of a DOM package.
+ +You can create and send HTML events. Ditto UIEvents. Since DOM +doesn't require a UI, it's the UI's job to send them; perhaps that's +part of your application.
+ +This package may be built without the ability to report mutation +events, gaining a significant speedup in DOM construction time. However, +if that is done then certain other features -- notably node iterators +and getElementsByTagname -- will not be available. + + +
Each DOM node has all you need to walk to everything connected +to that node. Lightweight, efficient utilities are easily layered on +top of just the core APIs.
+ +Traversal APIs are an optional part of DOM Level 2, providing +a not-so-lightweight way to walk over DOM trees, if your application +didn't already have such utilities for use with data represented via +DOM. Implementing this helped debug the (optional) event and mutation +event subsystems, so it's provided here.
+ +At this writing, the "TreeWalker" interface isn't implemented.
+ + + +For what appear to be a combination of historical and "committee +logic" reasons, DOM has a number of features which I strongly advise +you to avoid using in your library and application code. These +include the following types of DOM nodes; see the documentation for the +implementation class for more information:
If you really need to use unparsed entities or notations, use SAX; +it offers better support for all DTD-related functionality. +It also exposes actual +document typing information (such as element content models).
+ +Also, when accessing attribute values, use methods that provide their +values as single strings, rather than those which expose value substructure +(Text and EntityReference nodes). (See the DomAttr +documentation for more information.)
+ +Note that many of these features were provided as partial support for +editor functionality (including the incomplete DTD access). Full editor +functionality requires access to potentially malformed lexical structure, +at the level of unparsed tokens and below. Access at such levels is so +complex that using it in non-editor applications sacrifices all the +benefits of XML; editor aplications need extremely specialized APIs.
+ +(This isn't a slam against DTDs, note; only against the broken support +for them in DOM. Even despite inclusion of some dubious SGML legacy features +such as notations and unparsed entities, +and the ongoing proliferation of alternative schema and validation tools, +DTDs are still the most widely adopted tool +to constrain XML document structure. +Alternative schemes generally focus on data transfer style +applications; open document architectures comparable to +DocBook 4.0 don't yet exist in the schema world. +Feel free to use DTDs; just don't expect DOM to help you.)
+ + + + diff --git a/libjava/classpath/gnu/xml/libxmlj/dom/GnomeAttr.java b/libjava/classpath/gnu/xml/libxmlj/dom/GnomeAttr.java new file mode 100644 index 000000000..da9c62c9d --- /dev/null +++ b/libjava/classpath/gnu/xml/libxmlj/dom/GnomeAttr.java @@ -0,0 +1,117 @@ +/* GnomeAttr.java - + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.libxmlj.dom; + +import gnu.java.lang.CPStringBuilder; + +import org.w3c.dom.Attr; +import org.w3c.dom.DOMException; +import org.w3c.dom.Element; +import org.w3c.dom.Node; +import org.w3c.dom.TypeInfo; + +/** + * A DOM attribute node implemented in libxml2. + * + * @author Chris Burdess + */ +class GnomeAttr + extends GnomeNode + implements Attr +{ + + GnomeAttr(Object id) + { + super(id); + } + + public String getName() + { + return getNodeName(); + } + + public native boolean getSpecified(); + + public native String getValue(); + + public native void setValue(String value) + throws DOMException; + + public Node getParentNode() + { + return null; + } + + public Element getOwnerElement() + { + return (Element) super.getParentNode(); + } + + // DOM Level 3 methods + + public TypeInfo getSchemaTypeInfo() + { + return new GnomeTypeInfo(id); + } + + public boolean isId() + { + if (xmljIsId()) + { + return true; + } + GnomeElement element = (GnomeElement) getOwnerElement(); + return (element != null && + element.userIdAttrs != null && + element.userIdAttrs.contains(this)); + } + + private native boolean xmljIsId(); + + public String toString() + { + CPStringBuilder buffer = new CPStringBuilder(getClass().getName()); + buffer.append("[name="); + buffer.append(getName()); + buffer.append(",value="); + buffer.append(getValue()); + buffer.append("]"); + return buffer.toString(); + } + +} diff --git a/libjava/classpath/gnu/xml/libxmlj/dom/GnomeCDATASection.java b/libjava/classpath/gnu/xml/libxmlj/dom/GnomeCDATASection.java new file mode 100644 index 000000000..3e408e653 --- /dev/null +++ b/libjava/classpath/gnu/xml/libxmlj/dom/GnomeCDATASection.java @@ -0,0 +1,57 @@ +/* GnomeCDATASection.java - + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.libxmlj.dom; + +import org.w3c.dom.CDATASection; + +/** + * A DOM CDATA section node implemented in libxml2. + * + * @author Chris Burdess + */ +class GnomeCDATASection + extends GnomeText + implements CDATASection +{ + + GnomeCDATASection(Object id) + { + super(id); + } + +} diff --git a/libjava/classpath/gnu/xml/libxmlj/dom/GnomeCharacterData.java b/libjava/classpath/gnu/xml/libxmlj/dom/GnomeCharacterData.java new file mode 100644 index 000000000..3f3d6ecb5 --- /dev/null +++ b/libjava/classpath/gnu/xml/libxmlj/dom/GnomeCharacterData.java @@ -0,0 +1,119 @@ +/* GnomeCharacterData.java - + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.libxmlj.dom; + +import gnu.java.lang.CPStringBuilder; + +import org.w3c.dom.CharacterData; +import org.w3c.dom.DOMException; + +/** + * A DOM character data node implemented in libxml2. + * + * @author Chris Burdess + */ +abstract class GnomeCharacterData + extends GnomeNode + implements CharacterData +{ + + GnomeCharacterData(Object id) + { + super(id); + } + + public String getData() + throws DOMException + { + return getNodeValue(); + } + + public void setData(String data) + throws DOMException + { + setNodeValue(data); + } + + public int getLength() + { + return getData().length(); + } + + public String substringData(int offset, int count) + throws DOMException + { + return getData().substring(offset, offset + count); + } + + public void appendData(String arg) + throws DOMException + { + setData(getData() + arg); + } + + public void insertData(int offset, String arg) + throws DOMException + { + String data = getData(); + setData(data.substring(0, offset) + arg + data.substring(offset)); + } + + public void deleteData(int offset, int count) + throws DOMException + { + String data = getData(); + setData(data.substring(0, offset) + data.substring(offset + count)); + } + + public void replaceData(int offset, int count, String arg) + { + String data = getData(); + setData(data.substring(0, offset) + arg + + data.substring(offset + count)); + } + + public String toString() + { + CPStringBuilder buffer = new CPStringBuilder(getClass().getName()); + buffer.append("[data="); + buffer.append(getData()); + buffer.append("]"); + return buffer.toString(); + } + +} diff --git a/libjava/classpath/gnu/xml/libxmlj/dom/GnomeComment.java b/libjava/classpath/gnu/xml/libxmlj/dom/GnomeComment.java new file mode 100644 index 000000000..b6985c4a4 --- /dev/null +++ b/libjava/classpath/gnu/xml/libxmlj/dom/GnomeComment.java @@ -0,0 +1,57 @@ +/* GnomeComment.java - + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.libxmlj.dom; + +import org.w3c.dom.Comment; + +/** + * A DOM comment node implemented in libxml2. + * + * @author Chris Burdess + */ +class GnomeComment + extends GnomeCharacterData + implements Comment +{ + + GnomeComment(Object id) + { + super(id); + } + +} diff --git a/libjava/classpath/gnu/xml/libxmlj/dom/GnomeDOMException.java b/libjava/classpath/gnu/xml/libxmlj/dom/GnomeDOMException.java new file mode 100644 index 000000000..a1af82335 --- /dev/null +++ b/libjava/classpath/gnu/xml/libxmlj/dom/GnomeDOMException.java @@ -0,0 +1,98 @@ +/* GnomeDOMException.java - + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.libxmlj.dom; + +import org.w3c.dom.DOMException; + +class GnomeDOMException + extends DOMException +{ + + GnomeDOMException(short code, String message) + { + super(code, createMessage(code, message)); + } + + private static String createMessage(int code, String message) + { + if (message != null) + { + return message; + } + switch (code) + { + case INDEX_SIZE_ERR: + return "INDEX_SIZE_ERR"; + case DOMSTRING_SIZE_ERR: + return "DOMSTRING_SIZE_ERR"; + case HIERARCHY_REQUEST_ERR: + return "HIERARCHY_REQUEST_ERR"; + case WRONG_DOCUMENT_ERR: + return "WRONG_DOCUMENT_ERR"; + case INVALID_CHARACTER_ERR: + return "INVALID_CHARACTER_ERR"; + case NO_DATA_ALLOWED_ERR: + return "NO_DATA_ALLOWED_ERR"; + case NO_MODIFICATION_ALLOWED_ERR: + return "NO_MODIFICATION_ALLOWED_ERR"; + case NOT_FOUND_ERR: + return "NOT_FOUND_ERR"; + case NOT_SUPPORTED_ERR: + return "NOT_SUPPORTED_ERR"; + case INUSE_ATTRIBUTE_ERR: + return "INUSE_ATTRIBUTE_ERR"; + case INVALID_STATE_ERR: + return "INVALID_STATE_ERR"; + case SYNTAX_ERR: + return "SYNTAX_ERR"; + case INVALID_MODIFICATION_ERR: + return "INVALID_MODIFICATION_ERR"; + case NAMESPACE_ERR: + return "NAMESPACE_ERR"; + case INVALID_ACCESS_ERR: + return "INVALID_ACCESS_ERR"; + case VALIDATION_ERR: + return "VALIDATION_ERR"; + case TYPE_MISMATCH_ERR: + return "TYPE_MISMATCH_ERR"; + default: + return null; + } + } + +} diff --git a/libjava/classpath/gnu/xml/libxmlj/dom/GnomeDOMStringList.java b/libjava/classpath/gnu/xml/libxmlj/dom/GnomeDOMStringList.java new file mode 100644 index 000000000..c415ec782 --- /dev/null +++ b/libjava/classpath/gnu/xml/libxmlj/dom/GnomeDOMStringList.java @@ -0,0 +1,84 @@ +/* GnomeDOMStringList.java - + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.libxmlj.dom; + +import org.w3c.dom.DOMStringList; + +/** + * Implementation of a string list using an array of strings. + * + * @author Chris Burdess + */ +class GnomeDOMStringList + implements DOMStringList +{ + + final String[] values; + + GnomeDOMStringList(String[] values) + { + this.values = values; + } + + public int getLength() + { + return values.length; + } + + public String item(int index) + { + if (index < 0 || index >= values.length) + { + return null; + } + return values[index]; + } + + public boolean contains(String value) + { + for (int i = 0; i < values.length; i++) + { + if (values[i].equalsIgnoreCase(value)) + { + return true; + } + } + return false; + } + +} diff --git a/libjava/classpath/gnu/xml/libxmlj/dom/GnomeDocument.java b/libjava/classpath/gnu/xml/libxmlj/dom/GnomeDocument.java new file mode 100644 index 000000000..0d1b487d8 --- /dev/null +++ b/libjava/classpath/gnu/xml/libxmlj/dom/GnomeDocument.java @@ -0,0 +1,562 @@ +/* GnomeDocument.java - + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.libxmlj.dom; + +import gnu.java.lang.CPStringBuilder; +import gnu.xml.dom.DomNodeIterator; + +import java.util.Iterator; + +import org.w3c.dom.Attr; +import org.w3c.dom.CDATASection; +import org.w3c.dom.Comment; +import org.w3c.dom.Document; +import org.w3c.dom.DocumentFragment; +import org.w3c.dom.DocumentType; +import org.w3c.dom.DOMConfiguration; +import org.w3c.dom.DOMErrorHandler; +import org.w3c.dom.DOMException; +import org.w3c.dom.DOMImplementation; +import org.w3c.dom.DOMStringList; +import org.w3c.dom.Element; +import org.w3c.dom.EntityReference; +import org.w3c.dom.Node; +import org.w3c.dom.NodeList; +import org.w3c.dom.ProcessingInstruction; +import org.w3c.dom.Text; +import org.w3c.dom.UserDataHandler; +import org.w3c.dom.traversal.DocumentTraversal; +import org.w3c.dom.traversal.NodeFilter; +import org.w3c.dom.traversal.NodeIterator; +import org.w3c.dom.traversal.TreeWalker; +import org.w3c.dom.xpath.XPathEvaluator; +import org.w3c.dom.xpath.XPathException; +import org.w3c.dom.xpath.XPathExpression; +import org.w3c.dom.xpath.XPathNSResolver; + +/** + * A DOM document node implemented in libxml2. + * + * @author Chris Burdess + */ +public class GnomeDocument + extends GnomeNode + implements Document, DOMConfiguration, XPathEvaluator, DocumentTraversal +{ + + DOMImplementation dom; + + /** + * Not currently used. + */ + boolean strictErrorChecking; + + /* DOMConfiguration */ + boolean canonicalForm = false; + boolean cdataSections = true; + boolean checkCharacterNormalization = false; + boolean comments = true; + boolean datatypeNormalization = false; + boolean elementContentWhitespace = true; + boolean entities = true; + DOMErrorHandler errorHandler; + boolean namespaces = true; + boolean namespaceDeclarations = true; + boolean normalizeCharacters = false; + boolean splitCdataSections = true; + boolean validate = false; + boolean validateIfSchema = false; + boolean wellFormed = true; + + GnomeDocument(Object id) + { + super(id); + strictErrorChecking = true; + } + + protected void finalize() + { + free(id); + } + + private native void free(Object id); + + public native DocumentType getDoctype(); + + public DOMImplementation getImplementation() + { + return dom; + } + + public native Element getDocumentElement(); + + public Element createElement(String tagName) + throws DOMException + { + return createElementNS(null, tagName); + } + + public native DocumentType createDocumentType(String name, String publicId, + String systemId); + + public native DocumentFragment createDocumentFragment(); + + public native Text createTextNode(String data); + + public native Comment createComment(String data); + + public native CDATASection createCDATASection(String data) + throws DOMException; + + public native ProcessingInstruction createProcessingInstruction(String target, + String data) + throws DOMException; + + public Attr createAttribute(String name) + throws DOMException + { + return createAttributeNS(null, name); + } + + public native EntityReference createEntityReference(String name) + throws DOMException; + + public native NodeList getElementsByTagName(String tagName); + + public Node importNode(Node importedNode, boolean deep) + throws DOMException + { + Node ret = xmljImportNode(importedNode, deep); + if (importedNode instanceof GnomeNode) + { + ((GnomeNode) importedNode) + .notifyUserDataHandlers(UserDataHandler.NODE_IMPORTED, + importedNode, ret); + } + return ret; + } + + private native Node xmljImportNode(Node importedNode, boolean deep) + throws DOMException; + + public native Element createElementNS(String namespaceURI, String + qualifiedName) + throws DOMException; + + public native Attr createAttributeNS(String namespaceURI, String + qualifiedName) + throws DOMException; + + public native NodeList getElementsByTagNameNS(String namespaceURI, + String localName); + + public Element getElementById(String elementId) + { + Element element = xmljGetElementById(elementId); + if (element == null) + { + TreeWalker walker = createTreeWalker(this, NodeFilter.SHOW_ELEMENT, + null, false); + for (Node node = walker.nextNode(); node != null; + node = walker.nextNode()) + { + GnomeElement e = (GnomeElement) node; + if (e.userIdAttrs != null) + { + for (Iterator i = e.userIdAttrs.iterator(); i.hasNext(); ) + { + Attr attr = (Attr) i.next(); + if (attr.getNodeValue().equals(elementId)) + { + return e; + } + } + } + } + } + return element; + } + + private native Element xmljGetElementById(String elementId); + + // DOM Level 3 methods + + public native String getInputEncoding(); + + public native String getXmlEncoding(); + + public native boolean getXmlStandalone(); + + public native void setXmlStandalone(boolean xmlStandalone); + + public native String getXmlVersion(); + + public native void setXmlVersion(String xmlVersion); + + public boolean getStrictErrorChecking() + { + return strictErrorChecking; + } + + public void setStrictErrorChecking(boolean strictErrorChecking) + { + this.strictErrorChecking = strictErrorChecking; + } + + public native String getDocumentURI(); + + public native void setDocumentURI(String documentURI); + + public Node adoptNode(Node source) + throws DOMException + { + if (source == null || !(source instanceof GnomeNode)) + { + return null; + } + Node ret = xmljAdoptNode(source); + if (source instanceof GnomeNode) + { + ((GnomeNode) source). + notifyUserDataHandlers(UserDataHandler.NODE_ADOPTED, + source, ret); + } + return ret; + } + + private native Node xmljAdoptNode(Node source) + throws DOMException; + + public DOMConfiguration getDomConfig() + { + return this; + } + + public void normalizeDocument() + { + normalize(); + } + + public native Node renameNode(Node n, String namespaceURI, + String qualifiedName); + + // -- DOMConfiguration methods -- + + public void setParameter(String name, Object value) + throws DOMException + { + name = name.toLowerCase(); + if ("canonical-form".equals(name)) + { + /* optional + canonicalForm = getBooleanValue(value);*/ + } + else if ("cdata-sections".equals(name)) + { + cdataSections = getBooleanValue(value); + } + else if ("check-character-normalization".equals(name)) + { + /* optional + checkCharacterNormalization = getBooleanValue(value);*/ + } + else if ("comments".equals(name)) + { + comments = getBooleanValue(value); + } + else if ("datatype-normalization".equals(name)) + { + /* optional + datatypeNormalization = getBooleanValue(value);*/ + } + else if ("element-content-whitespace".equals(name)) + { + /* optional + elementContentWhitespace = getBooleanValue(value);*/ + } + else if ("entities".equals(name)) + { + entities = getBooleanValue(value); + } + else if ("error-handler".equals(name)) + { + errorHandler = (DOMErrorHandler) value; + } + else if ("infoset".equals(name)) + { + if (getBooleanValue(value)) + { + validateIfSchema = false; + entities = false; + datatypeNormalization = false; + cdataSections = false; + namespaceDeclarations = true; + wellFormed = true; + elementContentWhitespace = true; + comments = true; + namespaces = true; + } + } + else if ("namespaces".equals(name)) + { + /* optional + namespaces = getBooleanValue(value);*/ + } + else if ("namespace-declarations".equals(name)) + { + namespaceDeclarations = getBooleanValue(value); + } + else if ("normalize-characters".equals(name)) + { + /* optional + normalizeCharacters = getBooleanValue(value);*/ + } + else if ("split-cdata-sections".equals(name)) + { + splitCdataSections = getBooleanValue(value); + } + else if ("validate".equals(name)) + { + /* optional + validate = getBooleanValue(value);*/ + } + else if ("validate-if-schema".equals(name)) + { + /* optional + validateIfSchema = getBooleanValue(value);*/ + } + else if ("well-formed".equals(name)) + { + /* optional + wellFormed = getBooleanValue(value);*/ + } + else + { + throw new GnomeDOMException(DOMException.NOT_FOUND_ERR, name); + } + } + + public Object getParameter(String name) + throws DOMException + { + name = name.toLowerCase(); + if ("canonical-form".equals(name)) + { + return Boolean.valueOf(canonicalForm); + } + else if ("cdata-sections".equals(name)) + { + return Boolean.valueOf(cdataSections); + } + else if ("check-character-normalization".equals(name)) + { + return Boolean.valueOf(checkCharacterNormalization); + } + else if ("comments".equals(name)) + { + return Boolean.valueOf(comments); + } + else if ("datatype-normalization".equals(name)) + { + return Boolean.valueOf(datatypeNormalization); + } + else if ("element-content-whitespace".equals(name)) + { + return Boolean.valueOf(elementContentWhitespace); + } + else if ("entities".equals(name)) + { + return Boolean.valueOf(entities); + } + else if ("error-handler".equals(name)) + { + return errorHandler; + } + else if ("infoset".equals(name)) + { + return Boolean.valueOf(!validateIfSchema && + !entities && + !datatypeNormalization && + !cdataSections && + namespaceDeclarations && + wellFormed && + elementContentWhitespace && + comments && + namespaces); + } + else if ("namespaces".equals(name)) + { + return Boolean.valueOf(namespaces); + } + else if ("namespace-declarations".equals(name)) + { + return Boolean.valueOf(namespaceDeclarations); + } + else if ("normalize-characters".equals(name)) + { + return Boolean.valueOf(normalizeCharacters); + } + else if ("split-cdata-sections".equals(name)) + { + return Boolean.valueOf(splitCdataSections); + } + else if ("validate".equals(name)) + { + return Boolean.valueOf(validate); + } + else if ("validate-if-schema".equals(name)) + { + return Boolean.valueOf(validateIfSchema); + } + else if ("well-formed".equals(name)) + { + return Boolean.valueOf(wellFormed); + } + else + { + throw new GnomeDOMException(DOMException.NOT_FOUND_ERR, name); + } + } + + public boolean canSetParameter(String name, Object value) + { + name = name.toLowerCase(); + if ("error-handler".equals(name)) + { + return (value == null || value instanceof DOMErrorHandler); + } + return ("cdata-sections".equals(name) || + "comments".equals(name) || + "entities".equals(name) || + "namespace-declarations".equals(name) || + "split-cdata-sections".equals(name)); + } + + public DOMStringList getParameterNames() + { + String[] names = new String[] { + "canonical-form", + "cdata-sections", + "check-character-normalization", + "comments", + "datatype-normalization", + "element-content-whitespace", + "entities", + "error-handler", + "infoset", + "namespaces", + "namespace-declarations", + "normalize-characters", + "split-cdata-sections", + "validate", + "validate-if-schema", + "well-formed" + }; + return new GnomeDOMStringList(names); + } + + private boolean getBooleanValue(Object value) + { + if (value instanceof Boolean) + { + return ((Boolean) value).booleanValue(); + } + else if (value instanceof String) + { + return Boolean.valueOf ((String) value).booleanValue(); + } + return false; + } + + // -- XPathEvaluator methods -- + + public XPathExpression createExpression(String expression, + XPathNSResolver resolver) + throws XPathException, DOMException + { + return new GnomeXPathExpression(this, expression, resolver); + } + + public XPathNSResolver createNSResolver(Node nodeResolver) + { + return new GnomeXPathNSResolver(nodeResolver); + } + + public native Object evaluate(String expression, + Node contextNode, + XPathNSResolver resolver, + short type, + Object result) + throws XPathException, DOMException; + + // -- DocumentTraversal methods -- + + public NodeIterator createNodeIterator(Node root, + int whatToShow, + NodeFilter filter, + boolean entityReferenceExpansion) + throws DOMException + { + return new DomNodeIterator(root, whatToShow, filter, + entityReferenceExpansion, false); + } + + public TreeWalker createTreeWalker(Node root, + int whatToShow, + NodeFilter filter, + boolean entityReferenceExpansion) + throws DOMException + { + return new DomNodeIterator(root, whatToShow, filter, + entityReferenceExpansion, true); + } + + // -- Debugging -- + + public String toString() + { + CPStringBuilder buffer = new CPStringBuilder(getClass().getName()); + buffer.append("[version="); + buffer.append(getXmlVersion()); + buffer.append(",standalone="); + buffer.append(getXmlStandalone()); + buffer.append("]"); + return buffer.toString(); + } + +} diff --git a/libjava/classpath/gnu/xml/libxmlj/dom/GnomeDocumentBuilder.java b/libjava/classpath/gnu/xml/libxmlj/dom/GnomeDocumentBuilder.java new file mode 100644 index 000000000..6dea87226 --- /dev/null +++ b/libjava/classpath/gnu/xml/libxmlj/dom/GnomeDocumentBuilder.java @@ -0,0 +1,326 @@ +/* GnomeDocumentBuilder.java - + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.libxmlj.dom; + +import java.io.InputStream; +import java.io.IOException; +import java.net.URL; +import javax.xml.parsers.DocumentBuilder; + +import org.w3c.dom.Document; +import org.w3c.dom.DocumentType; +import org.w3c.dom.DOMImplementation; +import org.xml.sax.EntityResolver; +import org.xml.sax.ErrorHandler; +import org.xml.sax.InputSource; +import org.xml.sax.Locator; +import org.xml.sax.SAXException; +import org.xml.sax.SAXParseException; + +import gnu.xml.libxmlj.util.NamedInputStream; +import gnu.xml.libxmlj.util.StandaloneDocumentType; +import gnu.xml.libxmlj.util.StandaloneLocator; +import gnu.xml.libxmlj.util.XMLJ; + +/** + * A JAXP DOM implementation that uses Gnome libxml2 as the underlying + * parser and node representation. + * + * @author Chris Burdess + */ +public class GnomeDocumentBuilder + extends DocumentBuilder + implements DOMImplementation +{ + + static + { + XMLJ.init(); + } + + // -- DocumentBuilder -- + + private boolean validate; + private boolean coalesce; + private boolean expandEntities; + private EntityResolver entityResolver; + private ErrorHandler errorHandler; + private boolean seenFatalError; + + /** + * Constructs a new validating document builder. + */ + public GnomeDocumentBuilder() + { + this(true, false, false); + } + + /** + * Constructs a new document builder. + * @param validate whether to validate during parsing + * @param coalesce whether to merge CDATA as text nodes + * @param expandEntities whether to expand entity references + */ + public GnomeDocumentBuilder(boolean validate, + boolean coalesce, + boolean expandEntities) + { + this.validate = validate; + this.coalesce = coalesce; + this.expandEntities = expandEntities; + } + + public DOMImplementation getDOMImplementation() + { + return this; + } + + public boolean isNamespaceAware() + { + return true; + } + + public boolean isValidating() + { + return validate; + } + + public Document newDocument() + { + return createDocument(null, null, null); + } + + public Document parse(InputSource input) + throws SAXException, IOException + { + NamedInputStream in = XMLJ.getInputStream(input); + byte[] detectBuffer = in.getDetectBuffer(); + String publicId = input.getPublicId(); + String systemId = input.getSystemId(); + String base = XMLJ.getBaseURI(systemId); + // Handle zero-length document + if (detectBuffer == null) + { + throw new SAXParseException("No document element", publicId, + systemId, 0, 0); + } + seenFatalError = false; + return parseStream(in, + detectBuffer, + publicId, + systemId, + base, + validate, + coalesce, + expandEntities, + true, //entityResolver != null, + errorHandler != null); + } + + private native Document parseStream(InputStream in, + byte[] detectBuffer, + String publicId, + String systemId, + String base, + boolean validate, + boolean coalesce, + boolean expandEntities, + boolean entityResolver, + boolean errorHandler); + + public void setEntityResolver(EntityResolver resolver) + { + entityResolver = resolver; + } + + public void setErrorHandler(ErrorHandler handler) + { + errorHandler = handler; + } + + // -- DOMImplementation -- + + public boolean hasFeature(String name, String version) + { + if (name.length() == 0) + { + return false; + } + name = name.toLowerCase(); + if (name.charAt(0) == '+') + { + name = name.substring(1); + } + + if ("xml".equals(name) || "core".equals(name)) + { + return (version == null || + "".equals(version) || + "1.0".equals(version) || + "2.0".equals(version) || + "3.0".equals(version)); + + } + else if ("ls".equals(name) || "ls-async".equals(name)) + { + // TODO + /* + return (version == null || + "".equals(version) || + "3.0".equals(version)); + */ + return false; + } + else if ("traversal".equals(name)) + { + return (version == null || + "".equals(version) || + "2.0".equals(version)); + } + else if ("xpath".equals(name)) + { + return (version == null || + "".equals(version) || + "3.0".equals(version)); + } + return false; + } + + // DOM Level 3 + + public Object getFeature(String feature, String version) + { + if (hasFeature(feature, version)) + { + return this; + } + return null; + } + + public native Document createDocument(String namespaceURI, + String qualifiedName, + DocumentType doctype); + + public DocumentType createDocumentType(String qualifiedName, + String publicId, + String systemId) + { + return new StandaloneDocumentType(qualifiedName, publicId, systemId); + } + + // Callback hooks from JNI + + private void setDocumentLocator(Object ctx, Object loc) + { + // ignore + } + + private InputStream resolveEntity(String publicId, String systemId, + String base) + throws SAXException, IOException + { + String url = XMLJ.getAbsoluteURI(base, systemId); + InputStream in = null; + if (entityResolver != null) + { + InputSource source = entityResolver.resolveEntity(publicId, url); + if (source != null) + { + in = XMLJ.getInputStream(source); + } + } + if (in == null) + { + in = XMLJ.getInputStream(new URL(url)); + } + return in; + } + + private void warning(String message, + int lineNumber, + int columnNumber, + String publicId, + String systemId) + throws SAXException + { + if (!seenFatalError && errorHandler != null) + { + Locator l = new StandaloneLocator(lineNumber, + columnNumber, + publicId, + systemId); + errorHandler.warning(new SAXParseException(message, l)); + } + } + + private void error(String message, + int lineNumber, + int columnNumber, + String publicId, + String systemId) + throws SAXException + { + if (!seenFatalError && errorHandler != null) + { + Locator l = new StandaloneLocator(lineNumber, + columnNumber, + publicId, + systemId); + errorHandler.error(new SAXParseException(message, l)); + } + } + + private void fatalError(String message, + int lineNumber, + int columnNumber, + String publicId, + String systemId) + throws SAXException + { + if (!seenFatalError && errorHandler != null) + { + seenFatalError = true; + Locator l = new StandaloneLocator(lineNumber, + columnNumber, + publicId, + systemId); + errorHandler.fatalError(new SAXParseException(message, l)); + } + } + +} diff --git a/libjava/classpath/gnu/xml/libxmlj/dom/GnomeDocumentBuilderFactory.java b/libjava/classpath/gnu/xml/libxmlj/dom/GnomeDocumentBuilderFactory.java new file mode 100644 index 000000000..448e12351 --- /dev/null +++ b/libjava/classpath/gnu/xml/libxmlj/dom/GnomeDocumentBuilderFactory.java @@ -0,0 +1,118 @@ +/* GnomeDocumentBuilderFactory.java - + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.libxmlj.dom; + +import javax.xml.XMLConstants; +import javax.xml.parsers.DocumentBuilder; +import javax.xml.parsers.DocumentBuilderFactory; +import javax.xml.parsers.ParserConfigurationException; + +/** + * Factory for JAXP document builders using the libxml2 implementation. + * + * @author Chris Burdess + */ +public class GnomeDocumentBuilderFactory + extends DocumentBuilderFactory +{ + + private boolean secureProcessing; + + public GnomeDocumentBuilderFactory () + { + setNamespaceAware (true); + } + + public Object getAttribute (String name) + { + // TODO + return null; + } + + public DocumentBuilder newDocumentBuilder () + throws ParserConfigurationException + { + /* + if (!isNamespaceAware ()) + { + String msg = "Parser must be namespace-aware"; + throw new ParserConfigurationException (msg); + } + if (isIgnoringComments ()) + { + String msg = "Ignoring comments not supported"; + throw new ParserConfigurationException (msg); + } + if (isIgnoringElementContentWhitespace ()) + { + String msg = "Ignoring element content whitespace not supported"; + throw new ParserConfigurationException (msg); + } + */ + return new GnomeDocumentBuilder (isValidating (), + isCoalescing (), + isExpandEntityReferences ()); + } + + public void setAttribute (String name, Object value) + { + // TODO + } + + public void setFeature(String name, boolean value) + throws ParserConfigurationException + { + if (name == null) + throw new NullPointerException(); + if (XMLConstants.FEATURE_SECURE_PROCESSING.equals(name)) + { + secureProcessing = true; + return; + } + throw new ParserConfigurationException(name); + } + + public boolean getFeature(String name) + throws ParserConfigurationException + { + if (XMLConstants.FEATURE_SECURE_PROCESSING.equals(name)) + return secureProcessing; + throw new ParserConfigurationException(name); + } + +} diff --git a/libjava/classpath/gnu/xml/libxmlj/dom/GnomeDocumentFragment.java b/libjava/classpath/gnu/xml/libxmlj/dom/GnomeDocumentFragment.java new file mode 100644 index 000000000..3623eb6cf --- /dev/null +++ b/libjava/classpath/gnu/xml/libxmlj/dom/GnomeDocumentFragment.java @@ -0,0 +1,57 @@ +/* GnomeDocumentFragment.java - + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.libxmlj.dom; + +import org.w3c.dom.DocumentFragment; + +/** + * A DOM document fragment node implemented in libxml2. + * + * @author Chris Burdess + */ +class GnomeDocumentFragment +extends GnomeNode +implements DocumentFragment +{ + + GnomeDocumentFragment (Object id) + { + super (id); + } + +} diff --git a/libjava/classpath/gnu/xml/libxmlj/dom/GnomeDocumentType.java b/libjava/classpath/gnu/xml/libxmlj/dom/GnomeDocumentType.java new file mode 100644 index 000000000..7649b7339 --- /dev/null +++ b/libjava/classpath/gnu/xml/libxmlj/dom/GnomeDocumentType.java @@ -0,0 +1,98 @@ +/* GnomeDocumentType.java - + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.libxmlj.dom; + +import gnu.java.lang.CPStringBuilder; + +import org.w3c.dom.DocumentType; +import org.w3c.dom.NamedNodeMap; + +/** + * A DOM document type node implemented in libxml2. + * + * @author Chris Burdess + */ +class GnomeDocumentType +extends GnomeNode +implements DocumentType +{ + + GnomeDocumentType (Object id) + { + super (id); + } + + public String getName () + { + return getNodeName (); + } + + public NamedNodeMap getEntities () + { + return new GnomeNamedNodeMap (id, 1); + } + + public NamedNodeMap getNotations () + { + return new GnomeNamedNodeMap (id, 2); + } + + public native String getPublicId (); + + public native String getSystemId (); + + public native String getInternalSubset (); + + public String toString () + { + String publicId = getPublicId (); + CPStringBuilder buffer = new CPStringBuilder (getClass ().getName ()); + buffer.append ("["); + if (publicId != null) + { + buffer.append ("publicId="); + buffer.append (publicId); + buffer.append (","); + } + buffer.append ("systemId="); + buffer.append (getSystemId ()); + buffer.append ("]"); + return buffer.toString (); + } + +} diff --git a/libjava/classpath/gnu/xml/libxmlj/dom/GnomeElement.java b/libjava/classpath/gnu/xml/libxmlj/dom/GnomeElement.java new file mode 100644 index 000000000..19889d663 --- /dev/null +++ b/libjava/classpath/gnu/xml/libxmlj/dom/GnomeElement.java @@ -0,0 +1,184 @@ +/* GnomeElement.java - + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.libxmlj.dom; + +import gnu.java.lang.CPStringBuilder; + +import java.util.HashSet; +import java.util.Set; + +import org.w3c.dom.Attr; +import org.w3c.dom.DOMException; +import org.w3c.dom.Element; +import org.w3c.dom.NodeList; +import org.w3c.dom.TypeInfo; + +/** + * A DOM element node implemented in libxml2. + * + * @author Chris Burdess + */ +class GnomeElement + extends GnomeNode + implements Element +{ + + /** + * User-defined ID attributes. + */ + Set userIdAttrs; + + GnomeElement(Object id) + { + super(id); + } + + public String getTagName() + { + return getNodeName(); + } + + public native String getAttribute(String name); + + public native void setAttribute(String name, String value) + throws DOMException; + + public void removeAttribute(String name) + throws DOMException + { + Attr attr = getAttributeNode(name); + if (attr != null) + { + removeAttributeNode(attr); + } + } + + public native Attr getAttributeNode(String name); + + public native Attr setAttributeNode(Attr newAttr) + throws DOMException; + + public native Attr removeAttributeNode(Attr oldAttr) + throws DOMException; + + public native NodeList getElementsByTagName(String name); + + public native String getAttributeNS(String namespaceURI, String localName); + + public native void setAttributeNS(String namespaceURI, String + qualifiedName, String value) + throws DOMException; + + public void removeAttributeNS(String namespaceURI, String localName) + throws DOMException + { + Attr attr = getAttributeNodeNS(namespaceURI, localName); + if (attr != null) + { + removeAttributeNode(attr); + } + } + + public native Attr getAttributeNodeNS(String namespaceURI, + String localName); + + public native Attr setAttributeNodeNS(Attr newAttr) + throws DOMException; + + public native NodeList getElementsByTagNameNS(String namespaceURI, + String localName); + + public native boolean hasAttribute(String name); + + public native boolean hasAttributeNS(String namespaceURI, + String localName); + + // DOM Level 3 methods + + public TypeInfo getSchemaTypeInfo() + { + return new GnomeTypeInfo(id); + } + + public void setIdAttribute(String name, boolean isId) + { + Attr attr = getAttributeNode(name); + setIdAttributeNode(attr, isId); + } + + public void setIdAttributeNode(Attr attr, boolean isId) + { + if (attr == null)// FIXME || !attr.getOwnerElement().equals(this)) + { + throw new GnomeDOMException(DOMException.NOT_FOUND_ERR, null); + } + if (isId) + { + if (userIdAttrs == null) + { + userIdAttrs = new HashSet(); + } + userIdAttrs.add(attr); + } + else if (userIdAttrs != null) + { + userIdAttrs.remove(attr); + if (userIdAttrs.isEmpty()) + { + userIdAttrs = null; + } + } + } + + public void setIdAttributeNS(String namespaceURI, String localName, + boolean isId) + { + Attr attr = getAttributeNodeNS(namespaceURI, localName); + setIdAttributeNode(attr, isId); + } + + public String toString() + { + CPStringBuilder buffer = new CPStringBuilder(getClass().getName()); + buffer.append("[tagName="); + buffer.append(getTagName()); + buffer.append("]"); + return buffer.toString(); + } + +} diff --git a/libjava/classpath/gnu/xml/libxmlj/dom/GnomeEntity.java b/libjava/classpath/gnu/xml/libxmlj/dom/GnomeEntity.java new file mode 100644 index 000000000..a05217473 --- /dev/null +++ b/libjava/classpath/gnu/xml/libxmlj/dom/GnomeEntity.java @@ -0,0 +1,104 @@ +/* GnomeEntity.java - + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.libxmlj.dom; + +import gnu.java.lang.CPStringBuilder; + +import org.w3c.dom.Entity; + +/** + * A DOM entity node implemented in libxml2. + * + * @author Chris Burdess + */ +class GnomeEntity +extends GnomeNode +implements Entity +{ + + GnomeEntity (Object id) + { + super (id); + } + + public native String getPublicId (); + + public native String getSystemId (); + + public native String getNotationName (); + + // DOM Level 3 methods + + public String getInputEncoding () + { + // TODO + return null; + } + + public String getXmlEncoding () + { + // TODO + return null; + } + + public String getXmlVersion () + { + // TODO + return null; + } + + public String toString () + { + String publicId = getPublicId (); + CPStringBuilder buffer = new CPStringBuilder (getClass ().getName ()); + buffer.append ("["); + if (publicId != null) + { + buffer.append ("publicId="); + buffer.append (publicId); + buffer.append (","); + } + buffer.append ("systemId="); + buffer.append (getSystemId ()); + buffer.append (",notationName="); + buffer.append (getNotationName ()); + buffer.append ("]"); + return buffer.toString (); + } + +} diff --git a/libjava/classpath/gnu/xml/libxmlj/dom/GnomeEntityReference.java b/libjava/classpath/gnu/xml/libxmlj/dom/GnomeEntityReference.java new file mode 100644 index 000000000..aa2fcf1e5 --- /dev/null +++ b/libjava/classpath/gnu/xml/libxmlj/dom/GnomeEntityReference.java @@ -0,0 +1,57 @@ +/* GnomeEntityReference.java - + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.libxmlj.dom; + +import org.w3c.dom.EntityReference; + +/** + * A DOM entity reference node implemented in libxml2. + * + * @author Chris Burdess + */ +class GnomeEntityReference +extends GnomeNode +implements EntityReference +{ + + GnomeEntityReference (Object id) + { + super (id); + } + +} diff --git a/libjava/classpath/gnu/xml/libxmlj/dom/GnomeNamedNodeMap.java b/libjava/classpath/gnu/xml/libxmlj/dom/GnomeNamedNodeMap.java new file mode 100644 index 000000000..b8df4fede --- /dev/null +++ b/libjava/classpath/gnu/xml/libxmlj/dom/GnomeNamedNodeMap.java @@ -0,0 +1,92 @@ +/* GnomeNamedNodeMap.java - + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.libxmlj.dom; + +import org.w3c.dom.DOMException; +import org.w3c.dom.NamedNodeMap; +import org.w3c.dom.Node; + +/** + * A DOM named node map implemented in libxml2. + * + * @author Chris Burdess + */ +class GnomeNamedNodeMap +implements NamedNodeMap +{ + + /** + * The node id. + */ + private final Object id; + + /** + * The map type. + * 0=attributes + * 1=entities + * 2=notations + */ + private final int type; + + GnomeNamedNodeMap (Object id, int type) + { + this.id = id; + this.type = type; + } + + public native Node getNamedItem (String name); + + public native Node setNamedItem (Node arg) + throws DOMException; + + public native Node removeNamedItem (String name) + throws DOMException; + + public native Node item (int index); + + public native int getLength (); + + public native Node getNamedItemNS (String namespaceURI, String localName); + + public native Node setNamedItemNS (Node arg) + throws DOMException; + + public native Node removeNamedItemNS (String namespaceURI, + String localName); + +} diff --git a/libjava/classpath/gnu/xml/libxmlj/dom/GnomeNode.java b/libjava/classpath/gnu/xml/libxmlj/dom/GnomeNode.java new file mode 100644 index 000000000..5874916e5 --- /dev/null +++ b/libjava/classpath/gnu/xml/libxmlj/dom/GnomeNode.java @@ -0,0 +1,501 @@ +/* GnomeNode.java - + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.libxmlj.dom; + +import gnu.java.lang.CPStringBuilder; + +import java.util.HashMap; +import java.util.Iterator; +import java.util.Map; + +import org.w3c.dom.Document; +import org.w3c.dom.DocumentType; +import org.w3c.dom.DOMException; +import org.w3c.dom.NamedNodeMap; +import org.w3c.dom.Node; +import org.w3c.dom.NodeList; +import org.w3c.dom.Text; +import org.w3c.dom.UserDataHandler; + +import gnu.xml.libxmlj.util.StandaloneDocumentType; + +/** + * A DOM node implemented in libxml2. + * + * @author Chris Burdess + */ +class GnomeNode + implements Node, Comparable +{ + + /** + * Maps document pointers to a map of node pointers to node instances. + */ + static Map instances; + + /** + * Retrieves the node instance for the specified node pointer. + * This creates a new instance and adds it to the cache if required. + * @param doc the document pointer + * @param node the node pointer + * @param type the node type + */ + static GnomeNode newInstance(final Object doc, final Object node, + final int type) + { + if (doc == null) + { + throw new NullPointerException("doc"); + } + if (node == null) + { + throw new NullPointerException("node"); + } + if (instances == null) + { + instances = new HashMap(); + } + Map docNodes = (Map) instances.get(doc); + if (docNodes == null) + { + docNodes = new HashMap(1024); // TODO review optimal initial capacity + instances.put(doc, docNodes); + } + GnomeNode nodeInstance = (GnomeNode) docNodes.get(node); + if (nodeInstance != null) + { + return nodeInstance; // Return cached version + } + switch (type) + { + case ELEMENT_NODE: + nodeInstance = new GnomeElement(node); + break; + case ATTRIBUTE_NODE: + nodeInstance = new GnomeAttr(node); + break; + case TEXT_NODE: + nodeInstance = new GnomeText(node); + break; + case CDATA_SECTION_NODE: + nodeInstance = new GnomeCDATASection(node); + break; + case ENTITY_REFERENCE_NODE: + nodeInstance = new GnomeEntityReference(node); + break; + case ENTITY_NODE: + nodeInstance = new GnomeEntity(node); + break; + case PROCESSING_INSTRUCTION_NODE: + nodeInstance = new GnomeProcessingInstruction(node); + break; + case COMMENT_NODE: + nodeInstance = new GnomeComment(node); + break; + case DOCUMENT_NODE: + nodeInstance = new GnomeDocument(node); + break; + case DOCUMENT_TYPE_NODE: + nodeInstance = new GnomeDocumentType(node); + break; + case DOCUMENT_FRAGMENT_NODE: + nodeInstance = new GnomeDocumentFragment(node); + break; + case NOTATION_NODE: + nodeInstance = new GnomeNotation(node); + break; + default: + throw new IllegalArgumentException("Unknown node type: " + type); + } + docNodes.put(node, nodeInstance); + return nodeInstance; + } + + /** + * Frees the specified document. + * This removes all its nodes from the cache. + */ + static void freeDocument(final Object doc) + { + if (instances == null || doc == null) + { + return; + } + instances.remove(doc); + //System.out.println("Freed "+instances.remove(doc)); + } + + /** + * xmlNodePtr + */ + final Object id; + + Map userData; + Map userDataHandlers; + + GnomeNode(final Object id) + { + this.id = id; + } + + public native String getNodeName(); + + public native String getNodeValue() + throws DOMException; + + public native void setNodeValue(String nodeValue) + throws DOMException; + + public native short getNodeType(); + + public native Node getParentNode(); + + public NodeList getChildNodes() + { + return new GnomeNodeList(id); + } + + public native Node getFirstChild(); + + public native Node getLastChild(); + + public native Node getPreviousSibling(); + + public native Node getNextSibling(); + + public NamedNodeMap getAttributes() + { + return new GnomeNamedNodeMap(id, 0); + } + + public native Document getOwnerDocument(); + + public Node insertBefore(Node newChild, Node refChild) + throws DOMException + { + if (newChild instanceof StandaloneDocumentType) + { + DocumentType dt = (DocumentType) newChild; + newChild = ((GnomeDocument) getOwnerDocument()) + .createDocumentType(dt.getName(), dt.getPublicId(), + dt.getSystemId()); + } + if (newChild == null) + { + throw new GnomeDOMException(DOMException.NOT_FOUND_ERR, null); + } + if (!(newChild instanceof GnomeNode)) + { + throw new GnomeDOMException(DOMException.WRONG_DOCUMENT_ERR, null); + } + if (refChild == null || !(refChild instanceof GnomeNode)) + { + throw new GnomeDOMException(DOMException.NOT_FOUND_ERR, null); + } + return xmljInsertBefore(newChild, refChild); + } + + private native Node xmljInsertBefore(Node newChild, Node refChild) + throws DOMException; + + public Node replaceChild(Node newChild, Node oldChild) + throws DOMException + { + if (newChild instanceof StandaloneDocumentType) + { + DocumentType dt = (DocumentType) newChild; + newChild = ((GnomeDocument) getOwnerDocument()) + .createDocumentType(dt.getName(), dt.getPublicId(), + dt.getSystemId()); + } + if (newChild == null) + { + throw new GnomeDOMException(DOMException.NOT_FOUND_ERR, null); + } + if (!(newChild instanceof GnomeNode)) + { + throw new GnomeDOMException(DOMException.WRONG_DOCUMENT_ERR, newChild.toString()); + } + if (oldChild == null || !(oldChild instanceof GnomeNode)) + { + throw new GnomeDOMException(DOMException.NOT_FOUND_ERR, null); + } + return xmljReplaceChild(newChild, oldChild); + } + + private native Node xmljReplaceChild(Node newChild, Node oldChild) + throws DOMException; + + public Node removeChild(Node oldChild) + throws DOMException + { + if (!(oldChild instanceof GnomeNode)) + { + throw new GnomeDOMException(DOMException.WRONG_DOCUMENT_ERR, null); + } + return xmljRemoveChild(oldChild); + } + + private native Node xmljRemoveChild(Node oldChild) + throws DOMException; + + public Node appendChild(Node newChild) + throws DOMException + { + if (newChild instanceof StandaloneDocumentType) + { + DocumentType dt = (DocumentType) newChild; + newChild = ((GnomeDocument) getOwnerDocument()) + .createDocumentType(dt.getName(), dt.getPublicId(), + dt.getSystemId()); + } + if (!(newChild instanceof GnomeNode)) + { + throw new GnomeDOMException(DOMException.WRONG_DOCUMENT_ERR, null); + } + return xmljAppendChild(newChild); + } + + private native Node xmljAppendChild(Node newChild) + throws DOMException; + + public native boolean hasChildNodes(); + + public Node cloneNode(boolean deep) + { + Node ret = xmljCloneNode(deep); + notifyUserDataHandlers(UserDataHandler.NODE_CLONED, this, ret); + return ret; + } + + private native Node xmljCloneNode(boolean deep); + + public native void normalize(); + + public boolean isSupported(String feature, String version) + { + return getOwnerDocument().getImplementation() + .hasFeature(feature, version); + } + + public native String getNamespaceURI(); + + public native String getPrefix(); + + public native void setPrefix(String prefix) + throws DOMException; + + public native String getLocalName(); + + public native boolean hasAttributes(); + + public int hashCode() + { + return id.hashCode(); + } + + public boolean equals(Object other) + { + if (other == this) + { + return true; + } + return (other instanceof GnomeNode && + ((GnomeNode) other).id == id); + } + + // DOM Level 3 methods + + public native String getBaseURI(); + + public short compareDocumentPosition(Node other) + throws DOMException + { + return (short) compareTo(other); + } + + public final int compareTo(Object other) + { + if (other instanceof GnomeNode) + { + return xmljCompareTo(other); + } + return 0; + } + + private native int xmljCompareTo(Object other); + + public String getTextContent() + throws DOMException + { + switch (getNodeType()) + { + case ELEMENT_NODE: + case ATTRIBUTE_NODE: + case ENTITY_NODE: + case ENTITY_REFERENCE_NODE: + case DOCUMENT_FRAGMENT_NODE: + CPStringBuilder buffer = new CPStringBuilder(); + NodeList children = getChildNodes(); + int len = children.getLength(); + for (int i = 0; i < len; i++) + { + Node child = children.item(i); + String textContent = child.getTextContent(); + if (textContent != null) + { + buffer.append(textContent); + } + } + return buffer.toString(); + case TEXT_NODE: + case CDATA_SECTION_NODE: + case COMMENT_NODE: + case PROCESSING_INSTRUCTION_NODE: + return getNodeValue(); + default: + return null; + } + } + + public void setTextContent(String textContent) + throws DOMException + { + switch (getNodeType()) + { + case ENTITY_REFERENCE_NODE: + // entity references are read only + throw new GnomeDOMException(DOMException.NO_MODIFICATION_ALLOWED_ERR, + null); + case ELEMENT_NODE: + case ATTRIBUTE_NODE: + case ENTITY_NODE: + case DOCUMENT_FRAGMENT_NODE: + NodeList children = getChildNodes(); + int len = children.getLength(); + for (int i = 0; i < len; i++) + { + Node child = children.item(i); + removeChild(child); + } + if (textContent != null) + { + Text text = getOwnerDocument().createTextNode(textContent); + appendChild(text); + } + break; + case TEXT_NODE: + case CDATA_SECTION_NODE: + case COMMENT_NODE: + case PROCESSING_INSTRUCTION_NODE: + setNodeValue(textContent); + break; + } + } + + public boolean isSameNode(Node other) + { + return equals(other); + } + + public native String lookupPrefix(String namespaceURI); + + public native boolean isDefaultNamespace(String namespaceURI); + + public native String lookupNamespaceURI(String prefix); + + public native boolean isEqualNode(Node arg); + + public Object getFeature(String feature, String version) + { + return getOwnerDocument().getImplementation() + .getFeature(feature, version); + } + + public Object setUserData(String key, Object data, UserDataHandler handler) + { + // TODO handler + if (userData == null) + { + userData = new HashMap(); + } + if (handler != null) + { + if (userDataHandlers == null) + { + userDataHandlers = new HashMap(); + } + userDataHandlers.put(key, handler); + } + return userData.put(key, data); + } + + public Object getUserData(String key) + { + if (userData == null) + { + return null; + } + return userData.get(key); + } + + void notifyUserDataHandlers(short op, Node src, Node dst) + { + if (userDataHandlers != null) + { + for (Iterator i = userDataHandlers.entrySet().iterator(); i.hasNext(); ) + { + Map.Entry entry = (Map.Entry) i.next(); + String key = (String) entry.getKey(); + UserDataHandler handler = (UserDataHandler) entry.getValue(); + Object data = userData.get(key); + handler.handle(op, key, data, src, dst); + } + } + } + + public String toString() + { + CPStringBuilder buffer = new CPStringBuilder(getClass().getName()); + buffer.append("[nodeName="); + buffer.append(getNodeName()); + buffer.append("]"); + return buffer.toString(); + } + +} diff --git a/libjava/classpath/gnu/xml/libxmlj/dom/GnomeNodeList.java b/libjava/classpath/gnu/xml/libxmlj/dom/GnomeNodeList.java new file mode 100644 index 000000000..51a6d0bfa --- /dev/null +++ b/libjava/classpath/gnu/xml/libxmlj/dom/GnomeNodeList.java @@ -0,0 +1,66 @@ +/* GnomeNodeList.java - + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.libxmlj.dom; + +import org.w3c.dom.Node; +import org.w3c.dom.NodeList; + +/** + * A DOM node list implemented in libxml2. + * + * @author Chris Burdess + */ +class GnomeNodeList +implements NodeList +{ + + /** + * The node id. + */ + private final Object id; + + GnomeNodeList (Object id) + { + this.id = id; + } + + public native Node item (int index); + + public native int getLength (); + +} diff --git a/libjava/classpath/gnu/xml/libxmlj/dom/GnomeNotation.java b/libjava/classpath/gnu/xml/libxmlj/dom/GnomeNotation.java new file mode 100644 index 000000000..50e48e2a3 --- /dev/null +++ b/libjava/classpath/gnu/xml/libxmlj/dom/GnomeNotation.java @@ -0,0 +1,80 @@ +/* GnomeNotation.java - + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.libxmlj.dom; + +import gnu.java.lang.CPStringBuilder; + +import org.w3c.dom.Notation; + +/** + * A DOM notation node implemented in libxml2. + * + * @author Chris Burdess + */ +class GnomeNotation +extends GnomeNode +implements Notation +{ + + GnomeNotation (Object id) + { + super (id); + } + + public native String getPublicId (); + + public native String getSystemId (); + + public String toString () + { + String publicId = getPublicId (); + CPStringBuilder buffer = new CPStringBuilder (getClass ().getName ()); + buffer.append ("["); + if (publicId != null) + { + buffer.append ("publicId="); + buffer.append (publicId); + buffer.append (","); + } + buffer.append ("systemId="); + buffer.append (getSystemId ()); + buffer.append ("]"); + return buffer.toString (); + } + +} diff --git a/libjava/classpath/gnu/xml/libxmlj/dom/GnomeProcessingInstruction.java b/libjava/classpath/gnu/xml/libxmlj/dom/GnomeProcessingInstruction.java new file mode 100644 index 000000000..fad6e108a --- /dev/null +++ b/libjava/classpath/gnu/xml/libxmlj/dom/GnomeProcessingInstruction.java @@ -0,0 +1,79 @@ +/* GnomeProcessingInstruction.java - + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.libxmlj.dom; + +import gnu.java.lang.CPStringBuilder; + +import org.w3c.dom.DOMException; +import org.w3c.dom.ProcessingInstruction; + +/** + * A DOM processing instruction node implemented in libxml2. + * + * @author Chris Burdess + */ +class GnomeProcessingInstruction +extends GnomeNode +implements ProcessingInstruction +{ + + GnomeProcessingInstruction (Object id) + { + super (id); + } + + public String getTarget () + { + return getNodeName (); + } + + public native String getData (); + + public native void setData (String data) + throws DOMException; + + public String toString () + { + CPStringBuilder buffer = new CPStringBuilder (getClass ().getName ()); + buffer.append ("[data="); + buffer.append (getData ()); + buffer.append ("]"); + return buffer.toString (); + } + +} diff --git a/libjava/classpath/gnu/xml/libxmlj/dom/GnomeText.java b/libjava/classpath/gnu/xml/libxmlj/dom/GnomeText.java new file mode 100644 index 000000000..e00be18a9 --- /dev/null +++ b/libjava/classpath/gnu/xml/libxmlj/dom/GnomeText.java @@ -0,0 +1,132 @@ +/* GnomeText.java - + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.libxmlj.dom; + +import gnu.java.lang.CPStringBuilder; + +import org.w3c.dom.DOMException; +import org.w3c.dom.Node; +import org.w3c.dom.Text; + +/** + * A DOM text node implemented in libxml2. + * + * @author Chris Burdess + */ +class GnomeText +extends GnomeCharacterData +implements Text +{ + + GnomeText (Object id) + { + super (id); + } + + public Text splitText (int offset) + throws DOMException + { + String value = getNodeValue (); + String part1 = value.substring (0, offset); + String part2 = value.substring (offset); + Text text = getOwnerDocument ().createTextNode (part1); + getParentNode ().insertBefore (text, this); + setNodeValue (part2); + return text; + } + + // DOM Level 3 + + public boolean isElementContentWhitespace () + { + return getTextContent ().trim ().length () == 0; + } + + public String getWholeText () + { + Node first = this; + Node node = getPreviousSibling (); + while (node != null && node instanceof Text) + { + first = node; + node = node.getPreviousSibling (); + } + CPStringBuilder buf = new CPStringBuilder (first.getNodeValue ()); + node = first.getNextSibling (); + while (node != null && node instanceof Text) + { + buf.append (node.getNodeValue ()); + node = node.getNextSibling (); + } + return buf.toString (); + } + + public Text replaceWholeText (String content) throws DOMException + { + boolean isEmpty = (content == null || content.length () == 0); + if (!isEmpty) + { + setNodeValue (content); + } + + Node first = this; + Node node = getPreviousSibling (); + while (node != null && node instanceof Text) + { + first = node; + node = node.getPreviousSibling (); + } + node = first.getNextSibling (); + Node parent = getParentNode (); + if (first != this || isEmpty) + { + parent.removeChild (first); + } + while (node != null && node instanceof Text) + { + Node tmp = node; + node = node.getNextSibling (); + if (tmp != this || isEmpty) + { + parent.removeChild (tmp); + } + } + return (isEmpty) ? null : this; + } + +} diff --git a/libjava/classpath/gnu/xml/libxmlj/dom/GnomeTypeInfo.java b/libjava/classpath/gnu/xml/libxmlj/dom/GnomeTypeInfo.java new file mode 100644 index 000000000..5517e8a99 --- /dev/null +++ b/libjava/classpath/gnu/xml/libxmlj/dom/GnomeTypeInfo.java @@ -0,0 +1,65 @@ +/* GnomeTypeInfo.java - + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.libxmlj.dom; + +import org.w3c.dom.TypeInfo; + +/** + * Provides XML Schema information about an element or attribute. + * + * @author Chris Burdess + */ +class GnomeTypeInfo implements TypeInfo +{ + + final Object id; + + GnomeTypeInfo(Object id) + { + this.id = id; + } + + public native String getTypeName (); + + public native String getTypeNamespace (); + + public native boolean isDerivedFrom (String typeNamespaceArg, + String typeNameArg, + int derivationMethod); + +} diff --git a/libjava/classpath/gnu/xml/libxmlj/dom/GnomeXPathExpression.java b/libjava/classpath/gnu/xml/libxmlj/dom/GnomeXPathExpression.java new file mode 100644 index 000000000..52f4daa21 --- /dev/null +++ b/libjava/classpath/gnu/xml/libxmlj/dom/GnomeXPathExpression.java @@ -0,0 +1,86 @@ +/* GnomeXPathExpression.java - + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.libxmlj.dom; + +import org.w3c.dom.DOMException; +import org.w3c.dom.Node; +import org.w3c.dom.xpath.XPathException; +import org.w3c.dom.xpath.XPathExpression; +import org.w3c.dom.xpath.XPathNSResolver; + +/** + * A compiled XPath expression implemented in libxml2. + * + * @author Chris Burdess + */ +class GnomeXPathExpression +implements XPathExpression +{ + + /** + * xmlXPathCompExprPtr + */ + final Object expr; + + GnomeXPathExpression (GnomeDocument doc, String expression, + XPathNSResolver resolver) + { + expr = init (expression); + // TODO resolver + } + + protected void finalize () + { + free (expr); + } + + private native Object init (String expression); + + private native void free (Object expr); + + public Object evaluate (Node contextNode, short type, Object result) + throws XPathException, DOMException + { + return doEvaluate (expr, contextNode, type, result); + } + + private native Object doEvaluate (Object expr, Node contextNode, + short type, Object result) + throws XPathException, DOMException; + +} diff --git a/libjava/classpath/gnu/xml/libxmlj/dom/GnomeXPathNSResolver.java b/libjava/classpath/gnu/xml/libxmlj/dom/GnomeXPathNSResolver.java new file mode 100644 index 000000000..039a79a03 --- /dev/null +++ b/libjava/classpath/gnu/xml/libxmlj/dom/GnomeXPathNSResolver.java @@ -0,0 +1,64 @@ +/* GnomeXPathNSResolver.java - + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.libxmlj.dom; + +import org.w3c.dom.Node; +import org.w3c.dom.xpath.XPathNSResolver; + +/** + * XPath namespace URI resolver implemented in libxml2. + * + * @author Chris Burdess + */ +class GnomeXPathNSResolver + implements XPathNSResolver +{ + + Node node; + + GnomeXPathNSResolver(Node node) + { + this.node = node; + } + + public String lookupNamespaceURI(String prefix) + { + return node.lookupNamespaceURI(prefix); + } + +} diff --git a/libjava/classpath/gnu/xml/libxmlj/dom/GnomeXPathNodeList.java b/libjava/classpath/gnu/xml/libxmlj/dom/GnomeXPathNodeList.java new file mode 100644 index 000000000..b8d0a3d0a --- /dev/null +++ b/libjava/classpath/gnu/xml/libxmlj/dom/GnomeXPathNodeList.java @@ -0,0 +1,73 @@ +/* GnomeXPathNodeList.java - + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.libxmlj.dom; + +import org.w3c.dom.Node; +import org.w3c.dom.NodeList; + +/** + * A node list that uses an XPath result object. + * + * @author Chris Burdess + */ +class GnomeXPathNodeList +implements NodeList +{ + + /** + * xmlXPathObjectPtr + */ + final Object obj; + + GnomeXPathNodeList (Object obj) + { + this.obj = obj; + } + + protected void finalize () + { + free(obj); + } + + private native void free (Object obj); + + public native int getLength (); + + public native Node item (int index); + +} diff --git a/libjava/classpath/gnu/xml/libxmlj/dom/GnomeXPathResult.java b/libjava/classpath/gnu/xml/libxmlj/dom/GnomeXPathResult.java new file mode 100644 index 000000000..b2c706f65 --- /dev/null +++ b/libjava/classpath/gnu/xml/libxmlj/dom/GnomeXPathResult.java @@ -0,0 +1,134 @@ +/* GnomeXPathResult.java - + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.libxmlj.dom; + +import gnu.java.lang.CPStringBuilder; + +import org.w3c.dom.DOMException; +import org.w3c.dom.Node; +import org.w3c.dom.xpath.XPathException; +import org.w3c.dom.xpath.XPathResult; + +/** + * An XPath result object implemented in libxml2. + * + * @author Chris Burdess + */ +class GnomeXPathResult +implements XPathResult +{ + + /** + * xmlXPathObjectPtr + */ + final Object obj; + + GnomeXPathResult (Object obj) + { + this.obj = obj; + } + + protected void finalize () + { + free (obj); + } + + private native void free (Object obj); + + public native short getResultType (); + + public native double getNumberValue () + throws XPathException; + + public native String getStringValue () + throws XPathException; + + public native boolean getBooleanValue () + throws XPathException; + + public native Node getSingleNodeValue () + throws XPathException; + + public native boolean getInvalidIteratorState(); + + public native int getSnapshotLength () + throws XPathException; + + public native Node iterateNext () + throws XPathException, DOMException; + + public native Node snapshotItem (int index) + throws XPathException; + + public String toString () + { + short type = getResultType (); + switch (type) + { + case STRING_TYPE: + return getStringValue (); + case NUMBER_TYPE: + return Double.toString (getNumberValue ()); + case BOOLEAN_TYPE: + return Boolean.valueOf (getBooleanValue ()).toString (); + case UNORDERED_NODE_SNAPSHOT_TYPE: + int len = getSnapshotLength (); + switch (len) { + case 0: + return "[no matches]"; + case 1: + return getSingleNodeValue ().toString (); + default: + CPStringBuilder buffer = new CPStringBuilder (); + for (int i = 0; i < len; i++) + { + if (i > 0) + { + buffer.append (','); + } + buffer.append (snapshotItem (i)); + } + return buffer.toString (); + } + default: + return getClass ().getName () + "[type=" + type + ",length=" + + getSnapshotLength () + ']'; + } + } + +} diff --git a/libjava/classpath/gnu/xml/libxmlj/sax/GnomeLocator.java b/libjava/classpath/gnu/xml/libxmlj/sax/GnomeLocator.java new file mode 100644 index 000000000..dba0a72c0 --- /dev/null +++ b/libjava/classpath/gnu/xml/libxmlj/sax/GnomeLocator.java @@ -0,0 +1,99 @@ +/* GnomeLocator.java - + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.libxmlj.sax; + +import org.xml.sax.Locator; + +/** + * SAX Locator implementation that uses libxml2. + * + * @author Chris Burdess + */ +class GnomeLocator +implements Locator +{ + + // An xmlParserCtxtPtr + private final Object ctx; + + // An xmlSAXLocatorPtr + private final Object loc; + + GnomeLocator (Object ctx, Object loc) + { + this.ctx = ctx; + this.loc = loc; + if (ctx == null) + { + throw new NullPointerException ("ctx"); + } + if (loc == null) + { + throw new NullPointerException ("loc"); + } + } + + public String getPublicId () + { + return publicId (ctx, loc); + } + + private native String publicId (Object ctx, Object loc); + + public String getSystemId () + { + return systemId (ctx, loc); + } + + private native String systemId (Object ctx, Object loc); + + public int getLineNumber () + { + return lineNumber (ctx, loc); + } + + private native int lineNumber (Object ctx, Object loc); + + public int getColumnNumber () + { + return columnNumber (ctx, loc); + } + + private native int columnNumber (Object ctx, Object loc); + +} diff --git a/libjava/classpath/gnu/xml/libxmlj/sax/GnomeSAXParser.java b/libjava/classpath/gnu/xml/libxmlj/sax/GnomeSAXParser.java new file mode 100644 index 000000000..86d326b7c --- /dev/null +++ b/libjava/classpath/gnu/xml/libxmlj/sax/GnomeSAXParser.java @@ -0,0 +1,105 @@ +/* GnomeSAXParser.java - + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.libxmlj.sax; + +import javax.xml.parsers.SAXParser; + +import org.xml.sax.Parser; +import org.xml.sax.SAXException; +import org.xml.sax.SAXNotRecognizedException; +import org.xml.sax.SAXNotSupportedException; +import org.xml.sax.XMLReader; + +/** + * JAXP SAX parser implementation that uses libxml2. + * + * @author Chris Burdess + */ +class GnomeSAXParser +extends SAXParser +{ + + private boolean namespaceAware; + private boolean validating; + + /** + * Creates a new SAX parser. + */ + GnomeSAXParser (boolean namespaceAware, boolean validating) + { + this.namespaceAware = namespaceAware; + this.validating = validating; + } + + public Parser getParser () + throws SAXException + { + throw new SAXNotSupportedException ("SAX version 1 not supported"); + } + + public XMLReader getXMLReader () + throws SAXException + { + return new GnomeXMLReader (namespaceAware, validating); + } + + public Object getProperty (String name) + throws SAXNotRecognizedException, SAXNotSupportedException + { + GnomeXMLReader.checkPropertyName (name); + throw new SAXNotSupportedException (name); + } + + public void setProperty (String name, Object value) + throws SAXNotRecognizedException, SAXNotSupportedException + { + GnomeXMLReader.checkPropertyName (name); + throw new SAXNotSupportedException (name); + } + + public boolean isNamespaceAware () + { + return namespaceAware; + } + + public boolean isValidating () + { + return validating; + } + +} diff --git a/libjava/classpath/gnu/xml/libxmlj/sax/GnomeSAXParserFactory.java b/libjava/classpath/gnu/xml/libxmlj/sax/GnomeSAXParserFactory.java new file mode 100644 index 000000000..0c2881240 --- /dev/null +++ b/libjava/classpath/gnu/xml/libxmlj/sax/GnomeSAXParserFactory.java @@ -0,0 +1,92 @@ +/* GnomeSAXParserFactory.java - + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.libxmlj.sax; + +import java.util.Map; +import java.util.HashMap; + +import javax.xml.parsers.SAXParser; +import javax.xml.parsers.SAXParserFactory; +import javax.xml.parsers.ParserConfigurationException; + +import org.xml.sax.SAXException; +import org.xml.sax.SAXNotRecognizedException; +import org.xml.sax.SAXNotSupportedException; + +/** + * JAXP SAX parser factory implementation that uses libxml2. + * + * @author Chris Burdess + */ +public class GnomeSAXParserFactory +extends SAXParserFactory +{ + + private Map features; + + /** + * Creates a new SAX parser factory. + */ + public GnomeSAXParserFactory () + { + features = new HashMap (); + } + + public SAXParser newSAXParser () + throws ParserConfigurationException, SAXException + { + // TODO features + return new GnomeSAXParser (isNamespaceAware (), isValidating ()); + } + + public boolean getFeature (String name) + throws ParserConfigurationException, SAXNotRecognizedException, SAXNotSupportedException + { + GnomeXMLReader.checkFeatureName (name); + Boolean val = (Boolean) features.get (name); + return (val == null) ? false : val.booleanValue (); + } + + public void setFeature (String name, boolean flag) + throws ParserConfigurationException, SAXNotRecognizedException, SAXNotSupportedException + { + GnomeXMLReader.checkFeatureName (name); + features.put (name, flag ? Boolean.TRUE : Boolean.FALSE); + } + +} diff --git a/libjava/classpath/gnu/xml/libxmlj/sax/GnomeXMLReader.java b/libjava/classpath/gnu/xml/libxmlj/sax/GnomeXMLReader.java new file mode 100644 index 000000000..3a34bc934 --- /dev/null +++ b/libjava/classpath/gnu/xml/libxmlj/sax/GnomeXMLReader.java @@ -0,0 +1,1065 @@ +/* GnomeXMLReader.java - + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.libxmlj.sax; + +import java.io.File; +import java.io.FileNotFoundException; +import java.io.InputStream; +import java.io.IOException; +import java.net.MalformedURLException; +import java.net.URL; +import java.util.Arrays; +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; + +import org.xml.sax.Attributes; +import org.xml.sax.ContentHandler; +import org.xml.sax.DTDHandler; +import org.xml.sax.EntityResolver; +import org.xml.sax.ErrorHandler; +import org.xml.sax.InputSource; +import org.xml.sax.Locator; +import org.xml.sax.SAXException; +import org.xml.sax.SAXNotRecognizedException; +import org.xml.sax.SAXNotSupportedException; +import org.xml.sax.SAXParseException; +import org.xml.sax.XMLReader; +import org.xml.sax.ext.DeclHandler; +import org.xml.sax.ext.LexicalHandler; + +import gnu.xml.libxmlj.util.NamedInputStream; +import gnu.xml.libxmlj.util.StandaloneLocator; +import gnu.xml.libxmlj.util.XMLJ; + +/** + * A SAX2 parser that uses libxml2. + * + * @author Chris Burdess + */ +public class GnomeXMLReader +implements XMLReader +{ + + static + { + XMLJ.init (); + } + + private static final String FEATURES_PREFIX = + "http://xml.org/sax/features/"; + + private static final List RECOGNIZED_FEATURES = + Arrays.asList (new String[] + { + "external-general-entities", + "external-parameter-entities", + "is-standalone", + "lexical-handler/parameter-entities", + "namespaces", + "namespace-prefixes", + "resolve-dtd-uris", + "string-interning", + "use-attributes2", + "use-locator2", + "use-entity-resolver2", + "validation" + }); + + private static final String PROPERTIES_PREFIX = + "http://xml.org/sax/properties/"; + + private static final List RECOGNIZED_PROPERTIES = + Arrays.asList (new String[] + { + "declaration-handler", + "dom-node", + "lexical-handler", + "xml-string" + }); + + // Features + + private transient boolean standalone; + private boolean namespaces; + private boolean namespacePrefixes; + private boolean validation; + + // Callback handlers + + private ContentHandler contentHandler; + + private DTDHandler dtdHandler; + + private EntityResolver entityResolver; + + private ErrorHandler errorHandler; + + private DeclHandler declarationHandler; + + private LexicalHandler lexicalHandler; + + private GnomeLocator locator; + + // Namespace helper for handling callbacks + private transient Namespaces ns; + + // If true, do not invoke callback methods except endDocument + private transient boolean seenFatalError; + + private transient boolean seenStartDocument; + + private transient String base; + + public GnomeXMLReader () + { + this (true, true); + } + + public GnomeXMLReader (boolean namespaces, boolean validation) + { + this.namespaces = namespaces; + this.validation = validation; + ns = new Namespaces (); + } + + public ContentHandler getContentHandler () + { + return contentHandler; + } + + public void setContentHandler (ContentHandler handler) + { + contentHandler = handler; + } + + public DTDHandler getDTDHandler () + { + return dtdHandler; + } + + public void setDTDHandler (DTDHandler handler) + { + dtdHandler = handler; + } + + public EntityResolver getEntityResolver () + { + return entityResolver; + } + + public void setEntityResolver (EntityResolver resolver) + { + entityResolver = resolver; + } + + public ErrorHandler getErrorHandler () + { + return errorHandler; + } + + public void setErrorHandler (ErrorHandler handler) + { + errorHandler = handler; + } + + // Features + + public boolean getFeature (String name) + throws SAXNotRecognizedException, SAXNotSupportedException + { + checkFeatureName (name); + String key = name.substring (FEATURES_PREFIX.length ()); + if ("external-general-entities".equals (key)) + { + return validation; // TODO check this + } + else if ("external-parameter-entities".equals (key)) + { + return validation; // TODO check this + } + else if ("is-standalone".equals (key)) + { + return standalone; + } + else if ("namespaces".equals (key)) + { + return namespaces; + } + else if ("namespace-prefixes".equals (key)) + { + return namespacePrefixes; + } + else if ("resolve-dtd-uris".equals (key)) + { + return true; + } + else if ("validation".equals (key)) + { + return validation; + } + else + { + return false; + } + } + + public void setFeature (String name, boolean value) + throws SAXNotRecognizedException, SAXNotSupportedException + { + checkFeatureName (name); + String key = name.substring (FEATURES_PREFIX.length ()); + if ("namespaces".equals (key)) + { + namespaces = value; + } + else if ("namespace-prefixes".equals (key)) + { + namespacePrefixes = value; + } + else if ("validation".equals (key)) + { + validation = value; + } + } + + /** + * Check that the specified feature name is recognized. + */ + static void checkFeatureName (String name) + throws SAXNotRecognizedException + { + if (name == null || !name.startsWith (FEATURES_PREFIX)) + { + throw new SAXNotRecognizedException (name); + } + String key = name.substring (FEATURES_PREFIX.length ()); + if (!RECOGNIZED_FEATURES.contains (key)) + { + throw new SAXNotRecognizedException (name); + } + } + + // Properties + + public Object getProperty (String name) + throws SAXNotRecognizedException, SAXNotSupportedException + { + checkPropertyName (name); + String key = name.substring (PROPERTIES_PREFIX.length ()); + if ("declaration-handler".equals (key)) + { + return getDeclarationHandler (); + } + else if ("lexical-handler".equals (key)) + { + return getLexicalHandler (); + } + else + { + throw new SAXNotSupportedException (name); + } + } + + public void setProperty (String name, Object value) + throws SAXNotRecognizedException, SAXNotSupportedException + { + checkPropertyName (name); + String key = name.substring (PROPERTIES_PREFIX.length ()); + if ("declaration-handler".equals (key)) + { + setDeclarationHandler ((DeclHandler) value); + } + else if ("lexical-handler".equals (key)) + { + setLexicalHandler ((LexicalHandler) value); + } + } + + public DeclHandler getDeclarationHandler () + { + return declarationHandler; + } + + public void setDeclarationHandler (DeclHandler declarationHandler) + { + this.declarationHandler = declarationHandler; + } + + public LexicalHandler getLexicalHandler () + { + return lexicalHandler; + } + + public void setLexicalHandler (LexicalHandler lexicalHandler) + { + this.lexicalHandler = lexicalHandler; + } + + /** + * Check that the specified property name is recognized. + */ + static void checkPropertyName (String name) + throws SAXNotRecognizedException + { + if (!name.startsWith (PROPERTIES_PREFIX)) + { + throw new SAXNotRecognizedException (name); + } + String key = name.substring (PROPERTIES_PREFIX.length ()); + if (!RECOGNIZED_PROPERTIES.contains (key)) + { + throw new SAXNotRecognizedException (name); + } + } + + // Parse + + public void parse (String systemId) + throws IOException, SAXException + { + URL url = null; + try + { + url = new URL (systemId); + } + catch (MalformedURLException e) + { + File file = new File(systemId); + if (!file.exists ()) + { + throw new FileNotFoundException (systemId); + } + String path = file.getAbsolutePath(); + if (File.separatorChar != '/') + { + path = path.replace (File.separatorChar, '/'); + } + if (!path.startsWith ("/")) + { + path = "/" + path; + } + if (!path.endsWith ("/") && file.isDirectory ()) + { + path = path + "/"; + } + url = new URL ("file:" + path); + } + InputSource source = new InputSource(url.toString ()); + source.setByteStream (url.openStream ()); + parse (source); + } + + public synchronized void parse (InputSource input) + throws IOException, SAXException + { + NamedInputStream in = XMLJ.getInputStream (input); + byte[] detectBuffer = in.getDetectBuffer (); + String publicId = input.getPublicId (); + String systemId = input.getSystemId (); + base = XMLJ.getBaseURI (systemId); + // Reset state + standalone = false; + seenFatalError = false; + seenStartDocument = false; + if (systemId != null) + { + int lsi = systemId.lastIndexOf ('/'); + if (lsi != -1) + { + base = systemId.substring (0, lsi + 1); + } + } + // Handle zero-length document + if (detectBuffer == null) + { + startDocument (true); + fatalError ("No document element", 0, 0, publicId, systemId); + endDocument (); + return; + } + // Parse + parseStream(in, + detectBuffer, + publicId, + systemId, + base, + validation, + contentHandler != null, + dtdHandler != null, + entityResolver != null, + errorHandler != null, + declarationHandler != null, + lexicalHandler != null); + in.close (); + } + + native void parseStream (InputStream in, + byte[] detectBuffer, + String publicId, + String systemId, + String base, + boolean validate, + boolean contentHandler, + boolean dtdHandler, + boolean entityResolver, + boolean errorHandler, + boolean declarationHandler, + boolean lexicalHandler) + throws IOException, SAXException; + + String getURI (String prefix) + { + if (!namespaces) + { + return null; + } + return ns.getURI (prefix); + } + + // Callbacks from libxmlj + + private void startDTD (String name, String publicId, String systemId) + throws SAXException + { + if (seenFatalError || lexicalHandler == null) + { + return; + } + try + { + systemId = XMLJ.getAbsoluteURI (base, systemId); + lexicalHandler.startDTD (name, publicId, systemId); + } + catch (Exception e) + { + if (e instanceof SAXException) + { + throw (SAXException) e; + } + else + { + throw new SAXException (e); + } + } + } + + private void externalEntityDecl (String name, String publicId, + String systemId) + throws SAXException + { + if (seenFatalError || declarationHandler == null) + { + return; + } + try + { + systemId = XMLJ.getAbsoluteURI (base, systemId); + declarationHandler.externalEntityDecl (name, publicId, systemId); + } + catch (Exception e) + { + if (e instanceof SAXException) + { + throw (SAXException) e; + } + else + { + throw new SAXException (e); + } + } + } + + private void internalEntityDecl (String name, String value) + throws SAXException + { + if (seenFatalError || declarationHandler == null) + { + return; + } + try + { + declarationHandler.internalEntityDecl (name, value); + } + catch (Exception e) + { + if (e instanceof SAXException) + { + throw (SAXException) e; + } + else + { + throw new SAXException (e); + } + } + } + + private InputStream resolveEntity (String publicId, String systemId) + throws SAXException, IOException + { + if (entityResolver == null) + { + return null; + } + try + { + systemId = XMLJ.getAbsoluteURI (base, systemId); + InputSource source = entityResolver.resolveEntity (publicId, systemId); + return (source == null) ? null : XMLJ.getInputStream (source); + } + catch (Exception e) + { + if (e instanceof SAXException) + { + throw (SAXException) e; + } + else + { + throw new SAXException (e); + } + } + } + + private void notationDecl (String name, String publicId, String systemId) + throws SAXException + { + if (seenFatalError || dtdHandler == null) + { + return; + } + try + { + systemId = XMLJ.getAbsoluteURI (base, systemId); + dtdHandler.notationDecl (name, publicId, systemId); + } + catch (Exception e) + { + if (e instanceof SAXException) + { + throw (SAXException) e; + } + else + { + throw new SAXException (e); + } + } + } + + private void attributeDecl (String eName, String aName, String type, + String mode, String value) + throws SAXException + { + if (seenFatalError || declarationHandler == null) + { + return; + } + try + { + declarationHandler.attributeDecl (eName, aName, type, mode, value); + } + catch (Exception e) + { + if (e instanceof SAXException) + { + throw (SAXException) e; + } + else + { + throw new SAXException (e); + } + } + } + + private void elementDecl (String name, String model) + throws SAXException + { + if (seenFatalError || declarationHandler == null) + { + return; + } + try + { + declarationHandler.elementDecl (name, model); + } + catch (Exception e) + { + if (e instanceof SAXException) + { + throw (SAXException) e; + } + else + { + throw new SAXException (e); + } + } + } + + private void unparsedEntityDecl (String name, String publicId, + String systemId, String notationName) + throws SAXException + { + if (seenFatalError || dtdHandler == null) + { + return; + } + try + { + systemId = XMLJ.getAbsoluteURI (base, systemId); + dtdHandler.unparsedEntityDecl (name, publicId, systemId, + notationName); + } + catch (Exception e) + { + if (e instanceof SAXException) + { + throw (SAXException) e; + } + else + { + throw new SAXException (e); + } + } + } + + private void setDocumentLocator (Object ctx, Object loc) + { + locator = new GnomeLocator (ctx, loc); + if (seenFatalError || contentHandler == null) + { + return; + } + try + { + contentHandler.setDocumentLocator (locator); + } + catch (Exception e) + { + } + } + + private void startDocument (boolean standalone) + throws SAXException + { + this.standalone = standalone; + seenStartDocument = true; + if (contentHandler == null) + { + return; + } + try + { + contentHandler.startDocument (); + } + catch (Exception e) + { + if (e instanceof SAXException) + { + throw (SAXException) e; + } + else + { + throw new SAXException (e); + } + } + } + + private void endDocument () + throws SAXException + { + if (contentHandler == null) + { + return; + } + try + { + contentHandler.endDocument(); + } + catch (Exception e) + { + if (e instanceof SAXException) + { + throw (SAXException) e; + } + else + { + throw new SAXException (e); + } + } + } + + private void startElement(String name, String[] attrs) + throws SAXException + { + if (seenFatalError || contentHandler == null) + { + return; + } + try + { + XMLName xName = new XMLName (this, name); + if (namespaces) + { + // Handle defined namespaces + ns.push (); + int len = (attrs == null) ? 0 : attrs.length; + if (len > 0) + { + ArrayList filtered = new ArrayList (len); + for (int i = 0; i < len; i += 2) + { + String attName = attrs[i]; + String attValue = attrs[i + 1]; + if (attName.equals ("xmlns")) + { + startPrefixMapping ("", attValue); + } + else if (attName.startsWith ("xmlns:")) + { + startPrefixMapping (attName.substring (6), attValue); + } + else + { + filtered.add (attName); + filtered.add (attValue); + } + } + // Remove xmlns attributes + attrs = new String[filtered.size ()]; + filtered.toArray (attrs); + } + } + // Construct attributes + Attributes atts = new StringArrayAttributes (this, attrs); + contentHandler.startElement (xName.uri, xName.localName, xName.qName, + atts); + } + catch (Exception e) + { + if (e instanceof SAXException) + { + throw (SAXException) e; + } + else + { + throw new SAXException (e); + } + } + } + + private void endElement (String name) + throws SAXException + { + if (seenFatalError || contentHandler == null) + { + return; + } + try + { + XMLName xName = new XMLName (this, name); + String uri = (xName.uri == null) ? "" : xName.uri; + contentHandler.endElement (uri, xName.localName, xName.qName); + // Handle undefining namespaces + if (namespaces) + { + for (Iterator i = ns.currentPrefixes (); i.hasNext (); ) + { + endPrefixMapping ((String) i.next ()); + } + ns.pop (); // releases current depth + } + } + catch (Exception e) + { + if (e instanceof SAXException) + { + throw (SAXException) e; + } + else + { + throw new SAXException (e); + } + } + } + + private void startPrefixMapping (String prefix, String uri) + throws SAXException + { + if (seenFatalError || contentHandler == null) + { + return; + } + ns.define (prefix, uri); + contentHandler.startPrefixMapping (prefix, uri); + } + + private void endPrefixMapping (String prefix) + throws SAXException + { + if (seenFatalError || contentHandler == null) + { + return; + } + contentHandler.endPrefixMapping (prefix); + } + + private void characters (String text) + throws SAXException + { + if (seenFatalError || contentHandler == null || text == null) + { + return; + } + try + { + char[] ch = text.toCharArray (); + contentHandler.characters (ch, 0, ch.length); + } + catch (Exception e) + { + if (e instanceof SAXException) + { + throw (SAXException) e; + } + else + { + throw new SAXException (e); + } + } + } + + private void ignorableWhitespace (String text) + throws SAXException + { + if (seenFatalError || contentHandler == null || text == null) + { + return; + } + try + { + char[] ch = text.toCharArray (); + contentHandler.ignorableWhitespace (ch, 0, ch.length); + } + catch (Exception e) + { + if (e instanceof SAXException) + { + throw (SAXException) e; + } + else + { + throw new SAXException (e); + } + } + } + + private void processingInstruction (String target, String data) + throws SAXException + { + if (seenFatalError || contentHandler == null) + { + return; + } + try + { + if (data == null) + { + data = ""; + } + contentHandler.processingInstruction (target, data); + } + catch (Exception e) + { + if (e instanceof SAXException) + { + throw (SAXException) e; + } + else + { + throw new SAXException (e); + } + } + } + + private void comment (String text) + throws SAXException + { + if (seenFatalError || lexicalHandler == null || text == null) + { + return; + } + try + { + char[] ch = text.toCharArray (); + lexicalHandler.comment (ch, 0, ch.length); + } + catch (Exception e) + { + if (e instanceof SAXException) + { + throw (SAXException) e; + } + else + { + throw new SAXException (e); + } + } + } + + private void cdataBlock (String text) + throws SAXException + { + if (seenFatalError || text == null) + { + return; + } + try + { + if (lexicalHandler == null) + { + characters(text); + } + else + { + lexicalHandler.startCDATA(); + characters(text); + lexicalHandler.endCDATA(); + } + } + catch (Exception e) + { + if (e instanceof SAXException) + { + throw (SAXException) e; + } + else + { + throw new SAXException (e); + } + } + } + + private void warning (String message, + int lineNumber, int columnNumber, + String publicId, String systemId) + throws SAXException + { + if (seenFatalError || errorHandler == null) + { + return; + } + try + { + Locator l = new StandaloneLocator (lineNumber, columnNumber, + publicId, systemId); + errorHandler.warning (new SAXParseException (message, l)); + } + catch (Exception e) + { + if (e instanceof SAXException) + { + throw (SAXException) e; + } + else + { + throw new SAXException (e); + } + } + } + + private void error (String message, + int lineNumber, int columnNumber, + String publicId, String systemId) + throws SAXException + { + if (seenFatalError || errorHandler == null) + { + return; + } + try + { + Locator l = new StandaloneLocator (lineNumber, columnNumber, + publicId, systemId); + errorHandler.error (new SAXParseException (message, l)); + } + catch (Exception e) + { + if (e instanceof SAXException) + { + throw (SAXException) e; + } + else + { + throw new SAXException (e); + } + } + } + + private void fatalError (String message, + int lineNumber, int columnNumber, + String publicId, String systemId) + throws SAXException + { + if (seenFatalError || errorHandler == null) + { + return; + } + try + { + if (!seenStartDocument) + { + startDocument (false); + } + seenFatalError = true; + Locator l = new StandaloneLocator (lineNumber, columnNumber, + publicId, systemId); + errorHandler.fatalError (new SAXParseException (message, l)); + } + catch (Exception e) + { + if (e instanceof SAXException) + { + throw (SAXException) e; + } + else + { + throw new SAXException (e); + } + } + } + +} diff --git a/libjava/classpath/gnu/xml/libxmlj/sax/Namespaces.java b/libjava/classpath/gnu/xml/libxmlj/sax/Namespaces.java new file mode 100644 index 000000000..473d02028 --- /dev/null +++ b/libjava/classpath/gnu/xml/libxmlj/sax/Namespaces.java @@ -0,0 +1,122 @@ +/* Namespaces.java - + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.libxmlj.sax; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashMap; +import java.util.Iterator; + +/** + * Helper class for managing namespaces. + * + * @author Chris Burdess + */ +class Namespaces +{ + + ArrayList stack = new ArrayList (); + + /** + * Increments the tree depth. + * This allocates a new potential namespace entry. + */ + void push () + { + stack.add (null); + } + + /** + * Decrements the tree depth. + * This removes namespaces defined at the extremity. + */ + void pop () + { + stack.remove (stack.size() - 1); + } + + /** + * Searches for the namespace URI corresponding to the specified prefix. + */ + String getURI (String prefix) + { + for (int i = stack.size () - 1; i >= 0; i--) + { + HashMap ns = (HashMap) stack.get (i); + if (ns != null && ns.containsKey (prefix)) + { + String ret = (String) ns.get (prefix); + return (ret == null) ? "" : ret; + } + } + return ""; + } + + /** + * Defines the specified prefix-URI mapping at the current depth in the + * tree. + */ + void define (String prefix, String uri) + { + int index = stack.size () - 1; + HashMap ns = (HashMap) stack.get (index); + if (ns == null) + { + ns = new HashMap (); + stack.set (index, ns); + } + ns.put (prefix, uri); + } + + /** + * Returns an iterator over the prefixes defined at the current depth. + */ + Iterator currentPrefixes () + { + HashMap ns = (HashMap) stack.get (stack.size () - 1); + if (ns == null) + { + return Collections.EMPTY_LIST.iterator (); + } + else + { + return ns.keySet ().iterator (); + } + } + +} diff --git a/libjava/classpath/gnu/xml/libxmlj/sax/StringArrayAttributes.java b/libjava/classpath/gnu/xml/libxmlj/sax/StringArrayAttributes.java new file mode 100644 index 000000000..f5a753c56 --- /dev/null +++ b/libjava/classpath/gnu/xml/libxmlj/sax/StringArrayAttributes.java @@ -0,0 +1,170 @@ +/* StringArrayAttributes.java - + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.libxmlj.sax; + +import org.xml.sax.Attributes; + +/** + * An implementation of Attributes that reads values from an array of + * strings, supplied by libxml2. + * Each pair of elements in the array represents a key followed by a value. + * + * @author Chris Burdess + */ +class StringArrayAttributes +implements Attributes +{ + + private int len; + private XMLName[] keys; + private String[] values; + + StringArrayAttributes (GnomeXMLReader parser, String[] pairs) + { + len = (pairs == null) ? 0 : pairs.length / 2; + keys = new XMLName[len]; + values = new String[len]; + for (int i = 0; i < len; i++) + { + int pairIndex = i * 2; + keys[i] = new XMLName (parser, pairs[pairIndex]); + values[i] = pairs[pairIndex + 1]; + } + } + + public int getLength () + { + return len; + } + + public String getURI (int index) + { + if (index < 0 || index >= len) + { + return null; + } + return keys[index].uri; + } + + public String getLocalName (int index) + { + if (index < 0 || index >= len) + { + return null; + } + return keys[index].localName; + } + + public String getQName (int index) + { + if (index < 0 || index >= len) + { + return null; + } + return keys[index].qName; + } + + public String getType (int index) + { + if (index < 0 || index >= len) + { + return null; + } + // TODO can we get this information from libxml2? + return "CDATA"; + } + + public String getValue (int index) + { + if (index < 0 || index >= len) + { + return null; + } + return values[index]; + } + + public int getIndex (String uri, String localName) + { + for (int i = 0; i < len; i++) + { + XMLName key = keys[i]; + if (key.localName.equals (localName)) + { + if ((key.uri == null && uri == null) || + (key.uri != null && key.uri.equals(uri))) + { + return i; + } + } + } + return -1; + } + + public int getIndex (String qName) + { + for (int i = 0; i < len; i++) + { + if (keys[i].qName.equals (qName)) + { + return i; + } + } + return -1; + } + + public String getType (String uri, String localName) + { + return getType (getIndex (uri, localName)); + } + + public String getType (String qName) + { + return getType (getIndex (qName)); + } + + public String getValue (String uri, String localName) + { + return getValue (getIndex (uri, localName)); + } + + public String getValue (String qName) + { + return getValue (getIndex (qName)); + } + +} diff --git a/libjava/classpath/gnu/xml/libxmlj/sax/XMLName.java b/libjava/classpath/gnu/xml/libxmlj/sax/XMLName.java new file mode 100644 index 000000000..b9bdf9159 --- /dev/null +++ b/libjava/classpath/gnu/xml/libxmlj/sax/XMLName.java @@ -0,0 +1,92 @@ +/* XMLName.java - + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.libxmlj.sax; + +/** + * Structure containing the components of an XML element/attribute name. + * + * @author Chris Burdess + */ +class XMLName +{ + + private static final String XML_URI = "http://www.w3.org/XML/1998/namespace"; + + final String uri; + final String localName; + final String qName; + final String prefix; + + XMLName (GnomeXMLReader parser, String qName) + { + this.qName = qName; + int ci = qName.lastIndexOf (':'); + if (ci < 1) + { + localName = qName; + prefix = null; + uri = ""; + } + else + { + localName = qName.substring (ci + 1); + prefix = qName.substring (0, ci); + if ("xml".equals (prefix)) + { + if ("lang".equals (localName) || "space".equals (localName)) + { + uri = XML_URI; + } + else + { + uri = parser.getURI (prefix); + } + } + else + { + uri = parser.getURI (prefix); + } + } + } + + public String toString () + { + return qName; + } + +} diff --git a/libjava/classpath/gnu/xml/libxmlj/transform/ErrorListenerErrorHandler.java b/libjava/classpath/gnu/xml/libxmlj/transform/ErrorListenerErrorHandler.java new file mode 100644 index 000000000..398a0ba2e --- /dev/null +++ b/libjava/classpath/gnu/xml/libxmlj/transform/ErrorListenerErrorHandler.java @@ -0,0 +1,111 @@ +/* ErrorListenerErrorHandler.java - + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.libxmlj.transform; + +import javax.xml.transform.ErrorListener; +import javax.xml.transform.TransformerException; +import org.xml.sax.ErrorHandler; +import org.xml.sax.SAXException; +import org.xml.sax.SAXParseException; + +/** + * Provides a SAX ErrorHandler interface to an ErrorListener. + * + * @author Chris Burdess + */ +class ErrorListenerErrorHandler +implements ErrorHandler +{ + + private ErrorListener listener; + + ErrorListenerErrorHandler (ErrorListener listener) + { + this.listener = listener; + } + + public void warning (SAXParseException e) + throws SAXException + { + try + { + listener.warning (new TransformerException (e)); + } + catch (TransformerException te) + { + throw getSAXException (te); + } + } + + public void error (SAXParseException e) + throws SAXException + { + try + { + listener.error (new TransformerException (e)); + } + catch (TransformerException te) + { + throw getSAXException (te); + } + } + + public void fatalError (SAXParseException e) + throws SAXException + { + try + { + listener.fatalError (new TransformerException (e)); + } + catch (TransformerException te) + { + throw getSAXException (te); + } + } + + private SAXException getSAXException (TransformerException e) + { + Throwable cause = e.getCause (); + if (cause instanceof SAXException) + { + return (SAXException) cause; + } + return new SAXException (e); + } + +} diff --git a/libjava/classpath/gnu/xml/libxmlj/transform/GnomeTransformer.java b/libjava/classpath/gnu/xml/libxmlj/transform/GnomeTransformer.java new file mode 100755 index 000000000..5f31cb898 --- /dev/null +++ b/libjava/classpath/gnu/xml/libxmlj/transform/GnomeTransformer.java @@ -0,0 +1,572 @@ +/* GnomeTransformer.java - + Copyright (C) 2004 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.libxmlj.transform; + +import java.io.InputStream; +import java.io.IOException; +import java.io.OutputStream; + +import java.net.URL; + +import java.util.HashMap; +import java.util.Iterator; +import java.util.Map; +import java.util.Properties; + +import javax.xml.transform.ErrorListener; +import javax.xml.transform.Source; +import javax.xml.transform.SourceLocator; +import javax.xml.transform.Result; +import javax.xml.transform.Templates; +import javax.xml.transform.Transformer; +import javax.xml.transform.TransformerConfigurationException; +import javax.xml.transform.TransformerException; +import javax.xml.transform.URIResolver; + +import javax.xml.transform.dom.DOMSource; +import javax.xml.transform.dom.DOMResult; +import javax.xml.transform.sax.SAXResult; +import javax.xml.transform.stream.StreamSource; +import javax.xml.transform.stream.StreamResult; + +import org.w3c.dom.Node; + +import org.xml.sax.EntityResolver; +import org.xml.sax.ErrorHandler; + +import gnu.xml.libxmlj.dom.GnomeDocument; +import gnu.xml.libxmlj.sax.GnomeXMLReader; +import gnu.xml.libxmlj.util.NamedInputStream; +import gnu.xml.libxmlj.util.StandaloneLocator; +import gnu.xml.libxmlj.util.XMLJ; + +/** + * An implementation of {@link javax.xml.transform.Transformer} which + * performs XSLT transformation usinglibxslt
.
+ *
+ * @author Julian Scheid
+ * @author Chris Burdess
+ */
+public class GnomeTransformer
+ extends Transformer
+ implements Templates
+{
+
+ /**
+ * The parameters added by the user via {@link setParameter()}.
+ */
+ private Map parameters;
+
+ /**
+ * The output properties set by the user.
+ */
+ private Properties outputProperties;
+
+ /**
+ * The URI resolver to use during transformation.
+ */
+ private URIResolver resolver;
+
+ /**
+ * The error listener for transformation errors.
+ */
+ private ErrorListener errorListener;
+
+ /**
+ * Handle to the source stylesheet.
+ * This is a native pointer of type xsltStylesheetPtr.
+ */
+ private Object stylesheet;
+
+ /**
+ * Constructor.
+ * @param source the XSLT stylesheet document source
+ * @param resolver the resolver to use during transformation
+ * @param errorListener the error listener for transformation errors
+ */
+ GnomeTransformer (Source source,
+ URIResolver resolver,
+ ErrorListener errorListener)
+ throws TransformerConfigurationException
+ {
+ this.resolver = resolver;
+ this.errorListener = errorListener;
+ parameters = new HashMap ();
+ outputProperties = new Properties ();
+
+ if (source == null)
+ {
+ stylesheet = newStylesheet ();
+ }
+ else if (source instanceof StreamSource)
+ {
+ try
+ {
+ StreamSource ss = (StreamSource) source;
+ NamedInputStream in = XMLJ.getInputStream (ss);
+ String systemId = ss.getSystemId ();
+ String publicId = ss.getPublicId ();
+ String base = XMLJ.getBaseURI (systemId);
+ byte[] detectBuffer = in.getDetectBuffer ();
+ if (detectBuffer == null)
+ {
+ String msg = "No document element";
+ throw new TransformerConfigurationException (msg);
+ }
+ stylesheet = newStylesheetFromStream (in, detectBuffer, publicId,
+ systemId, base,
+ (resolver != null),
+ (errorListener != null));
+ }
+ catch (IOException e)
+ {
+ throw new TransformerConfigurationException (e);
+ }
+ }
+ else if (source instanceof DOMSource)
+ {
+ DOMSource ds = (DOMSource) source;
+ Node node = ds.getNode ();
+ if (!(node instanceof GnomeDocument))
+ {
+ String msg = "Node is not a GnomeDocument";
+ throw new TransformerConfigurationException (msg);
+ }
+ GnomeDocument doc = (GnomeDocument) node;
+ stylesheet = newStylesheetFromDoc (doc);
+ }
+ else
+ {
+ String msg = "Source type not supported (" + source + ")";
+ throw new TransformerConfigurationException (msg);
+ }
+ }
+
+ /**
+ * Copy constructor.
+ */
+ private GnomeTransformer (Object stylesheet,
+ URIResolver resolver,
+ ErrorListener errorListener,
+ Map parameters,
+ Properties outputProperties)
+ {
+ this.stylesheet = stylesheet;
+ this.resolver = resolver;
+ this.errorListener = errorListener;
+ this.parameters = parameters;
+ this.outputProperties = outputProperties;
+ }
+
+ private native Object newStylesheet ()
+ throws TransformerConfigurationException;
+
+ private native Object newStylesheetFromStream (InputStream in,
+ byte[] detectBuffer,
+ String publicId,
+ String systemId,
+ String base,
+ boolean entityResolver,
+ boolean errorHandler)
+ throws TransformerConfigurationException;
+
+ private native Object newStylesheetFromDoc (GnomeDocument doc)
+ throws TransformerConfigurationException;
+
+ //--- Implementation of javax.xml.transform.Transformer follows.
+
+ // Set, get and clear the parameters to use on transformation
+
+ public synchronized void setParameter (String parameter, Object value)
+ {
+ parameters.put (parameter, value);
+ }
+
+ public synchronized Object getParameter (String name)
+ {
+ return parameters.get (name);
+ }
+
+ public synchronized void clearParameters ()
+ {
+ parameters.clear ();
+ }
+
+ // Set and get the ErrorListener to use on transformation
+
+ public void setErrorListener (ErrorListener listener)
+ {
+ this.errorListener = listener;
+ }
+
+ public ErrorListener getErrorListener ()
+ {
+ return errorListener;
+ }
+
+ // Set and get the URIResolver to use on transformation
+
+ public void setURIResolver (URIResolver resolver)
+ {
+ this.resolver = resolver;
+ }
+
+ public URIResolver getURIResolver ()
+ {
+ return resolver;
+ }
+
+ // Set the output properties to use on transformation; get default
+ // output properties and output properties specified in the
+ // stylesheet or by the user.
+
+ public void setOutputProperties (Properties outputProperties)
+ {
+ // Note: defensive copying
+ this.outputProperties = new Properties (outputProperties);
+ }
+
+ public void setOutputProperty (String name, String value)
+ {
+ outputProperties.setProperty (name, value);
+ }
+
+ public Properties getOutputProperties ()
+ {
+ // Note: defensive copying
+ return new Properties (this.outputProperties);
+ }
+
+ public String getOutputProperty (String name)
+ {
+ return outputProperties.getProperty (name);
+ }
+
+ // -- Templates --
+
+ public Transformer newTransformer ()
+ {
+ return new GnomeTransformer (stylesheet, resolver, errorListener,
+ new HashMap (parameters),
+ new Properties (outputProperties));
+ }
+
+ // -- transform --
+
+ /**
+ * Transforms the given source and writes the result to the
+ * given target.
+ */
+ public void transform (Source source, Result result)
+ throws TransformerException
+ {
+ if (source instanceof StreamSource)
+ {
+ try
+ {
+ StreamSource ss = (StreamSource) source;
+ NamedInputStream in = XMLJ.getInputStream (ss);
+ String publicId = ss.getPublicId ();
+ String systemId = ss.getSystemId ();
+ String base = XMLJ.getBaseURI (systemId);
+ byte[] detectBuffer = in.getDetectBuffer ();
+ if (detectBuffer == null)
+ {
+ throw new TransformerException ("No document element");
+ }
+ if (result instanceof StreamResult)
+ {
+ OutputStream out = XMLJ.getOutputStream ((StreamResult) result);
+ transformStreamToStream (in, detectBuffer, publicId, systemId,
+ base, (resolver != null),
+ (errorListener != null), out);
+ }
+ else if (result instanceof DOMResult)
+ {
+ DOMResult dr = (DOMResult) result;
+ GnomeDocument ret =
+ transformStreamToDoc (in, detectBuffer, publicId, systemId,
+ base, (resolver != null),
+ (errorListener != null));
+ dr.setNode (ret);
+ dr.setSystemId (null);
+ }
+ else if (result instanceof SAXResult)
+ {
+ SAXResult sr = (SAXResult) result;
+ transformStreamToSAX (in, detectBuffer, publicId, systemId,
+ base, (resolver != null),
+ (errorListener != null),
+ getSAXContext (sr));
+ }
+ else
+ {
+ String msg = "Result type not supported (" + result + ")";
+ throw new TransformerConfigurationException (msg);
+ }
+ }
+ catch (IOException e)
+ {
+ throw new TransformerException (e);
+ }
+ }
+ else if (source instanceof DOMSource)
+ {
+ DOMSource ds = (DOMSource) source;
+ Node node = ds.getNode ();
+ if (!(node instanceof GnomeDocument))
+ {
+ String msg = "Node is not a GnomeDocument (" + node + ")";
+ throw new TransformerException (msg);
+ }
+ GnomeDocument doc = (GnomeDocument) node;
+ if (result instanceof StreamResult)
+ {
+ try
+ {
+ OutputStream out = XMLJ.getOutputStream ((StreamResult) result);
+ transformDocToStream (doc, out);
+ }
+ catch (IOException e)
+ {
+ throw new TransformerException (e);
+ }
+ }
+ else if (result instanceof DOMResult)
+ {
+ DOMResult dr = (DOMResult) result;
+ GnomeDocument ret = transformDocToDoc (doc);
+ dr.setNode (ret);
+ dr.setSystemId (null);
+ }
+ else if (result instanceof SAXResult)
+ {
+ SAXResult sr = (SAXResult) result;
+ transformDocToSAX (doc, getSAXContext (sr));
+ }
+ else
+ {
+ String msg = "Result type not supported";
+ throw new TransformerConfigurationException (msg);
+ }
+ }
+ else
+ {
+ String msg = "Source type not supported";
+ throw new TransformerConfigurationException (msg);
+ }
+ }
+
+ private GnomeXMLReader getSAXContext (SAXResult result)
+ {
+ GnomeXMLReader ctx = new GnomeXMLReader ();
+ ctx.setContentHandler (result.getHandler ());
+ ctx.setLexicalHandler (result.getLexicalHandler ());
+ if (errorListener != null)
+ {
+ ErrorHandler errorHandler =
+ new ErrorListenerErrorHandler (errorListener);
+ ctx.setErrorHandler (errorHandler);
+ }
+ if (resolver != null)
+ {
+ EntityResolver entityResolver =
+ new URIResolverEntityResolver (resolver);
+ ctx.setEntityResolver (entityResolver);
+ }
+ return ctx;
+ }
+
+ private native void transformStreamToStream (InputStream in,
+ byte[] detectBuffer,
+ String publicId,
+ String systemId,
+ String base,
+ boolean entityResolver,
+ boolean errorHandler,
+ OutputStream out)
+ throws TransformerException;
+
+ private native GnomeDocument transformStreamToDoc (InputStream in,
+ byte[] detectBuffer,
+ String publicId,
+ String systemId,
+ String base,
+ boolean entityResolver,
+ boolean errorHandler)
+ throws TransformerException;
+
+ private native void transformStreamToSAX (InputStream in,
+ byte[] detectBuffer,
+ String publicId,
+ String systemId,
+ String base,
+ boolean entityResolver,
+ boolean errorHandler,
+ GnomeXMLReader out)
+ throws TransformerException;
+
+ private native void transformDocToStream (GnomeDocument in,
+ OutputStream out)
+ throws TransformerException;
+
+ private native GnomeDocument transformDocToDoc (GnomeDocument in)
+ throws TransformerException;
+
+ private native void transformDocToSAX (GnomeDocument in,
+ GnomeXMLReader out)
+ throws TransformerException;
+
+ /*
+ * Retrieve parameters as a string array.
+ * This is a convenience method called from native code.
+ */
+ private String[] getParameterArray ()
+ {
+ String[] parameterArray = new String[parameters.size () * 2];
+ int index = 0;
+ for (Iterator it = parameters.keySet ().iterator ();
+ it.hasNext ();
+ ++index)
+ {
+ String parameterKey = (String) it.next ();
+ String parameterValue = (String) parameters.get (parameterKey);
+ parameterArray[index * 2 + 0] = parameterKey;
+ parameterArray[index * 2 + 1] =
+ "'" + ((parameterValue != null) ? parameterValue : "") + "'";
+ // FIXME encode parameter value correctly for XPath
+ }
+ return parameterArray;
+ }
+
+ // -- Free xsltStylesheet handle --
+
+ public void finalize ()
+ {
+ if (stylesheet != null)
+ {
+ free ();
+ stylesheet = null;
+ }
+ }
+
+ private native void free ();
+
+ // -- Callbacks --
+
+ private InputStream resolveEntity (String publicId, String systemId)
+ throws TransformerException
+ {
+ if (resolver != null)
+ {
+ systemId = resolver.resolve (null, systemId).getSystemId ();
+ }
+ if (systemId == null)
+ {
+ return null;
+ }
+ try
+ {
+ URL url = new URL (systemId);
+ return XMLJ.getInputStream (url);
+ }
+ catch (IOException e)
+ {
+ throw new TransformerException (e);
+ }
+ }
+
+ private void setDocumentLocator (Object ctx, Object loc)
+ {
+ }
+
+ private void warning (String message,
+ int lineNumber,
+ int columnNumber,
+ String publicId,
+ String systemId)
+ throws TransformerException
+ {
+ if (errorListener == null)
+ {
+ return;
+ }
+ SourceLocator l = new StandaloneLocator (lineNumber,
+ columnNumber,
+ publicId,
+ systemId);
+ errorListener.warning (new TransformerException (message, l));
+ }
+
+ private void error (String message,
+ int lineNumber,
+ int columnNumber,
+ String publicId,
+ String systemId)
+ throws TransformerException
+ {
+ if (errorListener == null)
+ {
+ return;
+ }
+ SourceLocator l = new StandaloneLocator (lineNumber,
+ columnNumber,
+ publicId,
+ systemId);
+ errorListener.error (new TransformerException (message, l));
+ }
+
+ private void fatalError (String message,
+ int lineNumber,
+ int columnNumber,
+ String publicId,
+ String systemId)
+ throws TransformerException
+ {
+ if (errorListener == null)
+ {
+ return;
+ }
+ SourceLocator l = new StandaloneLocator (lineNumber,
+ columnNumber,
+ publicId,
+ systemId);
+ errorListener.fatalError (new TransformerException (message, l));
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/libxmlj/transform/GnomeTransformerFactory.java b/libjava/classpath/gnu/xml/libxmlj/transform/GnomeTransformerFactory.java
new file mode 100755
index 000000000..4a0100a21
--- /dev/null
+++ b/libjava/classpath/gnu/xml/libxmlj/transform/GnomeTransformerFactory.java
@@ -0,0 +1,349 @@
+/* GnomeTransformerFactory.java -
+ Copyright (C) 2004 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.libxmlj.transform;
+
+import java.io.InputStream;
+import java.io.IOException;
+import java.util.HashMap;
+import java.util.Map;
+
+import javax.xml.parsers.FactoryConfigurationError;
+import javax.xml.parsers.ParserConfigurationException;
+import javax.xml.parsers.SAXParser;
+import javax.xml.parsers.SAXParserFactory;
+
+import javax.xml.transform.ErrorListener;
+import javax.xml.transform.Source;
+import javax.xml.transform.Templates;
+import javax.xml.transform.Transformer;
+import javax.xml.transform.TransformerFactory;
+import javax.xml.transform.TransformerConfigurationException;
+import javax.xml.transform.URIResolver;
+
+import javax.xml.transform.dom.DOMResult;
+import javax.xml.transform.dom.DOMSource;
+import javax.xml.transform.sax.SAXSource;
+import javax.xml.transform.stream.StreamResult;
+import javax.xml.transform.stream.StreamSource;
+
+import org.w3c.dom.Document;
+import org.w3c.dom.Node;
+import org.xml.sax.InputSource;
+import org.xml.sax.SAXException;
+import org.xml.sax.XMLReader;
+import org.xml.sax.helpers.DefaultHandler;
+
+import gnu.xml.libxmlj.util.XMLJ;
+
+/**
+ * An implementation of TransformerFactory
producing
+ * Transformer
objects which use libxslt
+ * for transformation.
+ *
+ * @author Julian Scheid
+ * @author Chris Burdess
+ */
+public class GnomeTransformerFactory
+ extends TransformerFactory
+{
+
+ static
+ {
+ XMLJ.init ();
+ }
+
+ /**
+ * URIResolver set by user, or default implementation.
+ */
+ private URIResolver uriResolver;
+
+ /**
+ * ErrorListener set by user, or default implementation.
+ */
+ private ErrorListener errorListener;
+
+ /**
+ * Attributes set by user.
+ */
+ private Map attributes = new HashMap ();
+
+ //--- Implementation of javax.xml.transform.TransformerFactory
+ //--- follows.
+
+ // -- begin getAssociatedStylesheet implementation --
+
+ /**
+ * Returns the stylesheet associated with the specified XML source, or
+ * null
if no associated stylesheet could be found.
+ */
+ public Source getAssociatedStylesheet(Source source, String media,
+ String title, String charset)
+ throws TransformerConfigurationException
+ {
+ String href= null;
+ String base = source.getSystemId();
+ if (source instanceof DOMSource)
+ {
+ Node node = ((DOMSource) source).getNode();
+ Document doc = (node.getNodeType() == Node.DOCUMENT_NODE) ?
+ (Document) node : node.getOwnerDocument();
+ if (base == null)
+ {
+ base = doc.getDocumentURI();
+ }
+ for (node = doc.getFirstChild(); node != null;
+ node = node.getNextSibling())
+ {
+ if (node.getNodeType() == Node.PROCESSING_INSTRUCTION_NODE &&
+ "xml-stylesheet".equals(node.getNodeName()))
+ {
+ String data = node.getNodeValue();
+ if (media != null &&
+ !media.equals(parseParameter(data, "type")))
+ {
+ continue;
+ }
+ if (title != null &&
+ !title.equals(parseParameter(data, "title")))
+ {
+ continue;
+ }
+ href = parseParameter(data, "href");
+ }
+ }
+ }
+ else
+ {
+ InputSource input;
+ XMLReader parser = null;
+ try
+ {
+ if (source instanceof SAXSource)
+ {
+ SAXSource sax = (SAXSource) source;
+ input = sax.getInputSource();
+ parser = sax.getXMLReader();
+ }
+ else
+ {
+ StreamSource stream = (StreamSource) source;
+ InputStream in = stream.getInputStream();
+ input = new InputSource(in);
+ }
+ input.setSystemId(base);
+ if (parser == null)
+ {
+ parser = createXMLReader();
+ }
+ AssociatedStylesheetHandler ash =
+ new AssociatedStylesheetHandler();
+ ash.media = media;
+ ash.title = title;
+ parser.setContentHandler(ash);
+ parser.parse(input);
+ href = ash.href;
+ }
+ catch (SAXException e)
+ {
+ throw new TransformerConfigurationException(e);
+ }
+ catch (IOException e)
+ {
+ throw new TransformerConfigurationException(e);
+ }
+ }
+ if (href == null)
+ {
+ return null;
+ }
+ if (base != null)
+ {
+ base = XMLJ.getBaseURI(base);
+ }
+ href = XMLJ.getAbsoluteURI(base, href);
+ return new StreamSource(href);
+ }
+
+ private XMLReader createXMLReader()
+ throws TransformerConfigurationException
+ {
+ try
+ {
+ SAXParserFactory factory = SAXParserFactory.newInstance();
+ SAXParser parser = factory.newSAXParser();
+ return parser.getXMLReader();
+ }
+ catch (FactoryConfigurationError e)
+ {
+ throw new TransformerConfigurationException(e);
+ }
+ catch (ParserConfigurationException e)
+ {
+ throw new TransformerConfigurationException(e);
+ }
+ catch (SAXException e)
+ {
+ throw new TransformerConfigurationException(e);
+ }
+ }
+
+ class AssociatedStylesheetHandler
+ extends DefaultHandler
+ {
+
+ String media;
+ String title;
+ String href;
+
+ public void processingInstruction(String target, String data)
+ throws SAXException
+ {
+ if ("xml-stylesheet".equals(target))
+ {
+ if (media != null && !media.equals(parseParameter(data, "type")))
+ {
+ return;
+ }
+ if (title != null && !title.equals(parseParameter(data, "title")))
+ {
+ return;
+ }
+ href = parseParameter(data, "href");
+ }
+ }
+
+ }
+
+ String parseParameter(String data, String name)
+ {
+ int start = data.indexOf(name + "=");
+ if (start != -1)
+ {
+ start += name.length() + 2;
+ char delim = data.charAt(start - 1);
+ int end = data.indexOf(delim, start);
+ if (end != -1)
+ {
+ return data.substring(start, end);
+ }
+ }
+ return null;
+ }
+
+ // -- end getAssociatedStylesheet implementation --
+
+ public synchronized void setAttribute (String name, Object value)
+ {
+ this.attributes.put (name, value);
+ }
+
+ public synchronized Object getAttribute (String name)
+ {
+ return attributes.get (name);
+ }
+
+ public void setErrorListener (ErrorListener errorListener)
+ {
+ this.errorListener = errorListener;
+ }
+
+ public ErrorListener getErrorListener ()
+ {
+ return errorListener;
+ }
+
+ public void setURIResolver (URIResolver uriResolver)
+ {
+ this.uriResolver = uriResolver;
+ }
+
+ public URIResolver getURIResolver ()
+ {
+ return uriResolver;
+ }
+
+ public boolean getFeature (String name)
+ {
+ return (StreamSource.FEATURE.equals (name) ||
+ StreamResult.FEATURE.equals (name) ||
+ DOMSource.FEATURE.equals (name) ||
+ DOMResult.FEATURE.equals (name));
+ }
+
+ public void setFeature(String name, boolean value)
+ throws TransformerConfigurationException
+ {
+ throw new TransformerConfigurationException(name);
+ }
+
+ /**
+ * Returns a new instance of class {@link Transformer} for a
+ * null souce.
+ */
+ public Transformer newTransformer ()
+ throws TransformerConfigurationException
+ {
+ return newTransformer (null);
+ }
+
+ /**
+ * Returns a new instance of class {@link Transformer} for
+ * the given souce.
+ */
+ public Transformer newTransformer (Source source)
+ throws TransformerConfigurationException
+ {
+ return new GnomeTransformer (source, uriResolver, errorListener);
+ }
+
+ /**
+ * Returns a new instance of class {@link Templates} for
+ * the given souce.
+ */
+ public Templates newTemplates (Source source)
+ throws TransformerConfigurationException
+ {
+ return new GnomeTransformer (source, uriResolver, errorListener);
+ }
+
+ /**
+ * Perform native cleanup.
+ */
+ public static native void freeLibxsltGlobal ();
+
+}
diff --git a/libjava/classpath/gnu/xml/libxmlj/transform/URIResolverEntityResolver.java b/libjava/classpath/gnu/xml/libxmlj/transform/URIResolverEntityResolver.java
new file mode 100644
index 000000000..0ce71d0ef
--- /dev/null
+++ b/libjava/classpath/gnu/xml/libxmlj/transform/URIResolverEntityResolver.java
@@ -0,0 +1,87 @@
+/* URIResolverEntityResolver.java -
+ Copyright (C) 2004 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.libxmlj.transform;
+
+import java.io.IOException;
+import javax.xml.transform.URIResolver;
+import javax.xml.transform.TransformerException;
+import javax.xml.transform.sax.SAXSource;
+import org.xml.sax.EntityResolver;
+import org.xml.sax.InputSource;
+import org.xml.sax.SAXException;
+
+/**
+ * Provides an EntityResolver interface to a URIResolver.
+ *
+ * @author Chris Burdess
+ */
+class URIResolverEntityResolver
+implements EntityResolver
+{
+
+ private URIResolver resolver;
+
+ URIResolverEntityResolver (URIResolver resolver)
+ {
+ this.resolver = resolver;
+ }
+
+ public InputSource resolveEntity (String publicId, String systemId)
+ throws SAXException, IOException
+ {
+ try
+ {
+ return SAXSource.sourceToInputSource (resolver.resolve (systemId,
+ null));
+ }
+ catch (TransformerException e)
+ {
+ Throwable cause = e.getCause ();
+ if (cause instanceof SAXException)
+ {
+ throw (SAXException) cause;
+ }
+ else if (cause instanceof IOException)
+ {
+ throw (IOException) cause;
+ }
+ throw new SAXException (e);
+ }
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/libxmlj/transform/package.html b/libjava/classpath/gnu/xml/libxmlj/transform/package.html
new file mode 100755
index 000000000..dac1027ff
--- /dev/null
+++ b/libjava/classpath/gnu/xml/libxmlj/transform/package.html
@@ -0,0 +1,14 @@
+
++ A JAXP-compliant wrapper for the XSLT C library for Gnome, also + known as libxslt. Allows to use libxslt via the Java API for XML + processing. +
+ ++ Usage: +
javax.xml.transform.TransformerFactory
+ to gnu.xml.libxmlj.GnomeTransformerFactory
.In effect, this makes a remote procedure call to the URI, with the + * request and response document syntax as chosen by the application. + * Note that all the input events must be seen, and sent to the URI, + * before the first output event can be seen. Clients are delayed + * at least by waiting for the server to respond, constraining concurrency. + * Services can thus be used to synchronize concurrent activities, and + * even to prioritize service among different clients. + * + *
You are advised to avoid restricting yourself to an "RPC" model + * for distributed computation. With a World Wide Web, network latencies + * and failures (e.g. non-availability) + * are significant; adopting a "procedure" model, rather than a workflow + * model where bulk requests are sent and worked on asynchronously, is not + * generally an optimal system-wide architecture. When the messages may + * need authentication, such as with an OpenPGP signature, or when server + * loads don't argue in favor of immediate responses, non-RPC models can + * be advantageous. (So-called "peer to peer" computing models are one + * additional type of model, though too often that term is applied to + * systems that still have a centralized control structure.) + * + *
Be strict in what you send, liberal in what you accept, as + * the Internet tradition goes. Strictly conformant data should never cause + * problems to its receiver; make your request pipeline be very strict, and + * don't compromise on that. Make your response pipeline strict as well, + * but be ready to tolerate specific mild, temporary, and well-documented + * variations from specific communications peers. + * + * @see XmlServlet + * + * @author David Brownell + */ +final public class CallFilter implements EventConsumer +{ + private Requestor req; + private EventConsumer next; + private URL target; + private URLConnection conn; + private ErrorHandler errHandler; + + + /** + * Initializes a call filter so that its inputs are sent to the + * specified URI, and its outputs are sent to the next consumer + * provided. + * + * @exception IOException if the URI isn't accepted as a URL + */ + // constructor used by PipelineFactory + public CallFilter (String uri, EventConsumer next) + throws IOException + { + this.next = next; + req = new Requestor (); + setCallTarget (uri); + } + + /** + * Assigns the URI of the call target to be used. + * Does not affect calls currently being made. + */ + final public void setCallTarget (String uri) + throws IOException + { + target = new URL (uri); + } + + /** + * Assigns the error handler to be used to present most fatal + * errors. + */ + public void setErrorHandler (ErrorHandler handler) + { + req.setErrorHandler (handler); + } + + + /** + * Returns the call target's URI. + */ + final public String getCallTarget () + { + return target.toString (); + } + + /** Returns the content handler currently in use. */ + final public org.xml.sax.ContentHandler getContentHandler () + { + return req; + } + + /** Returns the DTD handler currently in use. */ + final public DTDHandler getDTDHandler () + { + return req; + } + + + /** + * Returns the declaration or lexical handler currently in + * use, or throws an exception for other properties. + */ + final public Object getProperty (String id) + throws SAXNotRecognizedException + { + if (EventFilter.DECL_HANDLER.equals (id)) + return req; + if (EventFilter.LEXICAL_HANDLER.equals (id)) + return req; + throw new SAXNotRecognizedException (id); + } + + + // JDK 1.1 seems to need it to be done this way, sigh + ErrorHandler getErrorHandler () { return errHandler; } + + // + // Takes input and echoes to server as POST input. + // Then sends the POST reply to the next pipeline element. + // + final class Requestor extends XMLWriter + { + Requestor () + { + super ((Writer)null); + } + + public synchronized void startDocument () throws SAXException + { + // Connect to remote object and set up to send it XML text + try { + if (conn != null) + throw new IllegalStateException ("call is being made"); + + conn = target.openConnection (); + conn.setDoOutput (true); + conn.setRequestProperty ("Content-Type", + "application/xml;charset=UTF-8"); + + setWriter (new OutputStreamWriter ( + conn.getOutputStream (), + "UTF8"), "UTF-8"); + + } catch (IOException e) { + fatal ("can't write (POST) to URI: " + target, e); + } + + // NOW base class can safely write that text! + super.startDocument (); + } + + public void endDocument () throws SAXException + { + // + // Finish writing the request (for HTTP, a POST); + // this closes the output stream. + // + super.endDocument (); + + // + // Receive the response. + // Produce events for the next stage. + // + InputSource source; + XMLReader producer; + String encoding; + + try { + + source = new InputSource (conn.getInputStream ()); + +// FIXME if status is anything but success, report it!! It'd be good to +// save the request data just in case we need to deal with a forward. + + encoding = Resolver.getEncoding (conn.getContentType ()); + if (encoding != null) + source.setEncoding (encoding); + + producer = XMLReaderFactory.createXMLReader (); + producer.setErrorHandler (getErrorHandler ()); + EventFilter.bind (producer, next); + producer.parse (source); + conn = null; + + } catch (IOException e) { + fatal ("I/O Exception reading response, " + e.getMessage (), e); + } + } + } +} diff --git a/libjava/classpath/gnu/xml/pipeline/DomConsumer.java b/libjava/classpath/gnu/xml/pipeline/DomConsumer.java new file mode 100644 index 000000000..141f36eca --- /dev/null +++ b/libjava/classpath/gnu/xml/pipeline/DomConsumer.java @@ -0,0 +1,967 @@ +/* DomConsumer.java -- + Copyright (C) 1999,2000,2001 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.pipeline; + +import gnu.xml.util.DomParser; + +import org.xml.sax.Attributes; +import org.xml.sax.ContentHandler; +import org.xml.sax.DTDHandler; +import org.xml.sax.ErrorHandler; +import org.xml.sax.Locator; +import org.xml.sax.SAXException; +import org.xml.sax.SAXNotRecognizedException; +import org.xml.sax.SAXParseException; +import org.xml.sax.ext.DeclHandler; +import org.xml.sax.ext.LexicalHandler; +import org.xml.sax.helpers.AttributesImpl; +import org.w3c.dom.Attr; +import org.w3c.dom.CDATASection; +import org.w3c.dom.CharacterData; +import org.w3c.dom.Document; +import org.w3c.dom.DOMImplementation; +import org.w3c.dom.Element; +import org.w3c.dom.EntityReference; +import org.w3c.dom.Node; +import org.w3c.dom.ProcessingInstruction; +import org.w3c.dom.Text; + +/** + * This consumer builds a DOM Document from its input, acting either as a + * pipeline terminus or as an intermediate buffer. When a document's worth + * of events has been delivered to this consumer, that document is read with + * a {@link DomParser} and sent to the next consumer. It is also available + * as a read-once property. + * + *
The DOM tree is constructed as faithfully as possible. There are some + * complications since a DOM should expose behaviors that can't be implemented + * without API backdoors into that DOM, and because some SAX parsers don't + * report all the information that DOM permits to be exposed. The general + * problem areas involve information from the Document Type Declaration (DTD). + * DOM only represents a limited subset, but has some behaviors that depend + * on much deeper knowledge of a document's DTD. You shouldn't have much to + * worry about unless you change handling of "noise" nodes from its default + * setting (which ignores them all); note if you use JAXP to populate your + * DOM trees, it wants to save "noise" nodes by default. (Such nodes include + * ignorable whitespace, comments, entity references and CDATA boundaries.) + * Otherwise, your + * main worry will be if you use a SAX parser that doesn't flag ignorable + * whitespace unless it's validating (few don't). + * + *
The SAX2 events used as input must contain XML Names for elements + * and attributes, with original prefixes. In SAX2, + * this is optional unless the "namespace-prefixes" parser feature is set. + * Moreover, many application components won't provide completely correct + * structures anyway. Before you convert a DOM to an output document, + * you should plan to postprocess it to create or repair such namespace + * information. The {@link NSFilter} pipeline stage does such work. + * + *
Note: changes late in DOM L2 process made it impractical to + * attempt to create the DocumentType node in any implementation-neutral way, + * much less to populate it (L1 didn't support even creating such nodes). + * To create and populate such a node, subclass the inner + * {@link DomConsumer.Handler} class and teach it about the backdoors into + * whatever DOM implementation you want. It's possible that some revised + * DOM API (L3?) will make this problem solvable again. + * + * @see DomParser + * + * @author David Brownell + */ +public class DomConsumer implements EventConsumer +{ + private Class domImpl; + + private boolean hidingCDATA = true; + private boolean hidingComments = true; + private boolean hidingWhitespace = true; + private boolean hidingReferences = true; + + private Handler handler; + private ErrorHandler errHandler; + + private EventConsumer next; + + // FIXME: this can't be a generic pipeline stage just now, + // since its input became a Class not a String (to be turned + // into a class, using the right class loader) + + + /** + * Configures this pipeline terminus to use the specified implementation + * of DOM when constructing its result value. + * + * @param impl class implementing {@link org.w3c.dom.Document Document} + * which publicly exposes a default constructor + * + * @exception SAXException when there is a problem creating an + * empty DOM document using the specified implementation + */ + public DomConsumer (Class impl) + throws SAXException + { + domImpl = impl; + handler = new Handler (this); + } + + /** + * This is the hook through which a subclass provides a handler + * which knows how to access DOM extensions, specific to some + * implementation, to record additional data in a DOM. + * Treat this as part of construction; don't call it except + * before (or between) parses. + */ + protected void setHandler (Handler h) + { + handler = h; + } + + + private Document emptyDocument () + throws SAXException + { + try { + return (Document) domImpl.newInstance (); + } catch (IllegalAccessException e) { + throw new SAXException ("can't access constructor: " + + e.getMessage ()); + } catch (InstantiationException e) { + throw new SAXException ("can't instantiate Document: " + + e.getMessage ()); + } + } + + + /** + * Configures this consumer as a buffer/filter, using the specified + * DOM implementation when constructing its result value. + * + *
This event consumer acts as a buffer and filter, in that it + * builds a DOM tree and then writes it out when endDocument + * is invoked. Because of the limitations of DOM, much information + * will as a rule not be seen in that replay. To get a full fidelity + * copy of the input event stream, use a {@link TeeConsumer}. + * + * @param impl class implementing {@link org.w3c.dom.Document Document} + * which publicly exposes a default constructor + * @param next receives a "replayed" sequence of parse events when + * the endDocument method is invoked. + * + * @exception SAXException when there is a problem creating an + * empty DOM document using the specified DOM implementation + */ + public DomConsumer (Class impl, EventConsumer n) + throws SAXException + { + this (impl); + next = n; + } + + + /** + * Returns the document constructed from the preceding + * sequence of events. This method should not be + * used again until another sequence of events has been + * given to this EventConsumer. + */ + final public Document getDocument () + { + return handler.clearDocument (); + } + + public void setErrorHandler (ErrorHandler handler) + { + errHandler = handler; + } + + + /** + * Returns true if the consumer is hiding entity references nodes + * (the default), and false if EntityReference nodes should + * instead be created. Such EntityReference nodes will normally be + * empty, unless an implementation arranges to populate them and then + * turn them back into readonly objects. + * + * @see #setHidingReferences + */ + final public boolean isHidingReferences () + { return hidingReferences; } + + /** + * Controls whether the consumer will hide entity expansions, + * or will instead mark them with entity reference nodes. + * + * @see #isHidingReferences + * @param flag False if entity reference nodes will appear + */ + final public void setHidingReferences (boolean flag) + { hidingReferences = flag; } + + + /** + * Returns true if the consumer is hiding comments (the default), + * and false if they should be placed into the output document. + * + * @see #setHidingComments + */ + public final boolean isHidingComments () + { return hidingComments; } + + /** + * Controls whether the consumer is hiding comments. + * + * @see #isHidingComments + */ + public final void setHidingComments (boolean flag) + { hidingComments = flag; } + + + /** + * Returns true if the consumer is hiding ignorable whitespace + * (the default), and false if such whitespace should be placed + * into the output document as children of element nodes. + * + * @see #setHidingWhitespace + */ + public final boolean isHidingWhitespace () + { return hidingWhitespace; } + + /** + * Controls whether the consumer hides ignorable whitespace + * + * @see #isHidingComments + */ + public final void setHidingWhitespace (boolean flag) + { hidingWhitespace = flag; } + + + /** + * Returns true if the consumer is saving CDATA boundaries, or + * false (the default) otherwise. + * + * @see #setHidingCDATA + */ + final public boolean isHidingCDATA () + { return hidingCDATA; } + + /** + * Controls whether the consumer will save CDATA boundaries. + * + * @see #isHidingCDATA + * @param flag True to treat CDATA text differently from other + * text nodes + */ + final public void setHidingCDATA (boolean flag) + { hidingCDATA = flag; } + + + + /** Returns the document handler being used. */ + final public ContentHandler getContentHandler () + { return handler; } + + /** Returns the DTD handler being used. */ + final public DTDHandler getDTDHandler () + { return handler; } + + /** + * Returns the lexical handler being used. + * (DOM construction can't really use declaration handlers.) + */ + final public Object getProperty (String id) + throws SAXNotRecognizedException + { + if ("http://xml.org/sax/properties/lexical-handler".equals (id)) + return handler; + if ("http://xml.org/sax/properties/declaration-handler".equals (id)) + return handler; + throw new SAXNotRecognizedException (id); + } + + EventConsumer getNext () { return next; } + + ErrorHandler getErrorHandler () { return errHandler; } + + /** + * Class used to intercept various parsing events and use them to + * populate a DOM document. Subclasses would typically know and use + * backdoors into specific DOM implementations, used to implement + * DTD-related functionality. + * + *
Note that if this ever throws a DOMException (runtime exception) + * that will indicate a bug in the DOM (e.g. doesn't support something + * per specification) or the parser (e.g. emitted an illegal name, or + * accepted illegal input data).
+ */ + public static class Handler + implements ContentHandler, LexicalHandler, + DTDHandler, DeclHandler + { + protected DomConsumer consumer; + + private DOMImplementation impl; + private Document document; + private boolean isL2; + + private Locator locator; + private Node top; + private boolean inCDATA; + private boolean mergeCDATA; + private boolean inDTD; + private String currentEntity; + + private boolean recreatedAttrs; + private AttributesImpl attributes = new AttributesImpl (); + + /** + * Subclasses may use SAX2 events to provide additional + * behaviors in the resulting DOM. + */ + protected Handler (DomConsumer consumer) + throws SAXException + { + this.consumer = consumer; + document = consumer.emptyDocument (); + impl = document.getImplementation (); + isL2 = impl.hasFeature ("XML", "2.0"); + } + + private void fatal (String message, Exception x) + throws SAXException + { + SAXParseException e; + ErrorHandler errHandler = consumer.getErrorHandler (); + + if (locator == null) + e = new SAXParseException (message, null, null, -1, -1, x); + else + e = new SAXParseException (message, locator, x); + if (errHandler != null) + errHandler.fatalError (e); + throw e; + } + + /** + * Returns and forgets the document produced. If the handler is + * reused, a new document may be created. + */ + Document clearDocument () + { + Document retval = document; + document = null; + locator = null; + return retval; + } + + /** + * Returns the document under construction. + */ + protected Document getDocument () + { return document; } + + /** + * Returns the current node being populated. This is usually + * an Element or Document, but it might be an EntityReference + * node if some implementation-specific code knows how to put + * those into the result tree and later mark them as readonly. + */ + protected Node getTop () + { return top; } + + + // SAX1 + public void setDocumentLocator (Locator locator) + { + this.locator = locator; + } + + // SAX1 + public void startDocument () + throws SAXException + { + if (document == null) + try { + if (isL2) { + // couple to original implementation + document = impl.createDocument (null, "foo", null); + document.removeChild (document.getFirstChild ()); + } else { + document = consumer.emptyDocument (); + } + } catch (Exception e) { + fatal ("DOM create document", e); + } + top = document; + } + + // SAX1 + public void endDocument () + throws SAXException + { + try { + if (consumer.getNext () != null && document != null) { + DomParser parser = new DomParser (document); + + EventFilter.bind (parser, consumer.getNext ()); + parser.parse ("ignored"); + } + } finally { + top = null; + } + } + + // SAX1 + public void processingInstruction (String target, String data) + throws SAXException + { + // we can't create populated entity ref nodes using + // only public DOM APIs (they've got to be readonly) + if (currentEntity != null) + return; + + ProcessingInstruction pi; + + if (isL2 + // && consumer.isUsingNamespaces () + && target.indexOf (':') != -1) + namespaceError ( + "PI target name is namespace nonconformant: " + + target); + if (inDTD) + return; + pi = document.createProcessingInstruction (target, data); + top.appendChild (pi); + } + + /** + * Subclasses may overrride this method to provide a more efficient + * way to construct text nodes. + * Typically, copying the text into a single character array will + * be more efficient than doing that as well as allocating other + * needed for a String, including an internal StringBuffer. + * Those additional memory and CPU costs can be incurred later, + * if ever needed. + * Unfortunately the standard DOM factory APIs encourage those costs + * to be incurred early. + */ + protected Text createText ( + boolean isCDATA, + char ch [], + int start, + int length + ) { + String value = new String (ch, start, length); + + if (isCDATA) + return document.createCDATASection (value); + else + return document.createTextNode (value); + } + + // SAX1 + public void characters (char ch [], int start, int length) + throws SAXException + { + // we can't create populated entity ref nodes using + // only public DOM APIs (they've got to be readonly + // at creation time) + if (currentEntity != null) + return; + + Node lastChild = top.getLastChild (); + + // merge consecutive text or CDATA nodes if appropriate. + if (lastChild instanceof Text) { + if (consumer.isHidingCDATA () + // consecutive Text content ... always merge + || (!inCDATA + && !(lastChild instanceof CDATASection)) + // consecutive CDATASection content ... don't + // merge between sections, only within them + || (inCDATA && mergeCDATA + && lastChild instanceof CDATASection) + ) { + CharacterData last = (CharacterData) lastChild; + String value = new String (ch, start, length); + + last.appendData (value); + return; + } + } + if (inCDATA && !consumer.isHidingCDATA ()) { + top.appendChild (createText (true, ch, start, length)); + mergeCDATA = true; + } else + top.appendChild (createText (false, ch, start, length)); + } + + // SAX2 + public void skippedEntity (String name) + throws SAXException + { + // this callback is useless except to report errors, since + // we can't know if the ref was in content, within an + // attribute, within a declaration ... only one of those + // cases supports more intelligent action than a panic. + fatal ("skipped entity: " + name, null); + } + + // SAX2 + public void startPrefixMapping (String prefix, String uri) + throws SAXException + { + // reconstruct "xmlns" attributes deleted by all + // SAX2 parsers without "namespace-prefixes" = true + if ("".equals (prefix)) + attributes.addAttribute ("", "", "xmlns", + "CDATA", uri); + else + attributes.addAttribute ("", "", "xmlns:" + prefix, + "CDATA", uri); + recreatedAttrs = true; + } + + // SAX2 + public void endPrefixMapping (String prefix) + throws SAXException + { } + + // SAX2 + public void startElement ( + String uri, + String localName, + String qName, + Attributes atts + ) throws SAXException + { + // we can't create populated entity ref nodes using + // only public DOM APIs (they've got to be readonly) + if (currentEntity != null) + return; + + // parser discarded basic information; DOM tree isn't writable + // without massaging to assign prefixes to all nodes. + // the "NSFilter" class does that massaging. + if (qName.length () == 0) + qName = localName; + + + Element element; + int length = atts.getLength (); + + if (!isL2) { + element = document.createElement (qName); + + // first the explicit attributes ... + length = atts.getLength (); + for (int i = 0; i < length; i++) + element.setAttribute (atts.getQName (i), + atts.getValue (i)); + // ... then any recreated ones (DOM deletes duplicates) + if (recreatedAttrs) { + recreatedAttrs = false; + length = attributes.getLength (); + for (int i = 0; i < length; i++) + element.setAttribute (attributes.getQName (i), + attributes.getValue (i)); + attributes.clear (); + } + + top.appendChild (element); + top = element; + return; + } + + // For an L2 DOM when namespace use is enabled, use + // createElementNS/createAttributeNS except when + // (a) it's an element in the default namespace, or + // (b) it's an attribute with no prefix + String namespace; + + if (localName.length () != 0) + namespace = (uri.length () == 0) ? null : uri; + else + namespace = getNamespace (getPrefix (qName), atts); + + if (namespace == null) + element = document.createElement (qName); + else + element = document.createElementNS (namespace, qName); + + populateAttributes (element, atts); + if (recreatedAttrs) { + recreatedAttrs = false; + // ... DOM deletes any duplicates + populateAttributes (element, attributes); + attributes.clear (); + } + + top.appendChild (element); + top = element; + } + + final static String xmlnsURI = "http://www.w3.org/2000/xmlns/"; + + private void populateAttributes (Element element, Attributes attrs) + throws SAXParseException + { + int length = attrs.getLength (); + + for (int i = 0; i < length; i++) { + String type = attrs.getType (i); + String value = attrs.getValue (i); + String name = attrs.getQName (i); + String local = attrs.getLocalName (i); + String uri = attrs.getURI (i); + + // parser discarded basic information, DOM tree isn't writable + if (name.length () == 0) + name = local; + + // all attribute types other than these three may not + // contain scoped names... enumerated attributes get + // reported as NMTOKEN, except for NOTATION values + if (!("CDATA".equals (type) + || "NMTOKEN".equals (type) + || "NMTOKENS".equals (type))) { + if (value.indexOf (':') != -1) { + namespaceError ( + "namespace nonconformant attribute value: " + + "<" + element.getNodeName () + + " " + name + "='" + value + "' ...>"); + } + } + + // xmlns="" is legal (undoes default NS) + // xmlns:foo="" is illegal + String prefix = getPrefix (name); + String namespace; + + if ("xmlns".equals (prefix)) { + if ("".equals (value)) + namespaceError ("illegal null namespace decl, " + name); + namespace = xmlnsURI; + } else if ("xmlns".equals (name)) + namespace = xmlnsURI; + + else if (prefix == null) + namespace = null; + else if (!"".equals(uri) && uri.length () != 0) + namespace = uri; + else + namespace = getNamespace (prefix, attrs); + + if (namespace == null) + element.setAttribute (name, value); + else + element.setAttributeNS (namespace, name, value); + } + } + + private String getPrefix (String name) + { + int temp; + + if ((temp = name.indexOf (':')) > 0) + return name.substring (0, temp); + return null; + } + + // used with SAX1-level parser output + private String getNamespace (String prefix, Attributes attrs) + throws SAXParseException + { + String namespace; + String decl; + + // defaulting + if (prefix == null) { + decl = "xmlns"; + namespace = attrs.getValue (decl); + if ("".equals (namespace)) + return null; + else if (namespace != null) + return namespace; + + // "xmlns" is like a keyword + // ... according to the Namespace REC, but DOM L2 CR2+ + // and Infoset violate that by assigning a namespace. + // that conflict is resolved elsewhere. + } else if ("xmlns".equals (prefix)) + return null; + + // "xml" prefix is fixed + else if ("xml".equals (prefix)) + return "http://www.w3.org/XML/1998/namespace"; + + // otherwise, expect a declaration + else { + decl = "xmlns:" + prefix; + namespace = attrs.getValue (decl); + } + + // if we found a local declaration, great + if (namespace != null) + return namespace; + + + // ELSE ... search up the tree we've been building + for (Node n = top; + n != null && n.getNodeType () != Node.DOCUMENT_NODE; + n = n.getParentNode ()) { + if (n.getNodeType () == Node.ENTITY_REFERENCE_NODE) + continue; + Element e = (Element) n; + Attr attr = e.getAttributeNode (decl); + if (attr != null) + return attr.getNodeValue (); + } + // see above re "xmlns" as keyword + if ("xmlns".equals (decl)) + return null; + + namespaceError ("Undeclared namespace prefix: " + prefix); + return null; + } + + // SAX2 + public void endElement (String uri, String localName, String qName) + throws SAXException + { + // we can't create populated entity ref nodes using + // only public DOM APIs (they've got to be readonly) + if (currentEntity != null) + return; + + top = top.getParentNode (); + } + + // SAX1 (mandatory reporting if validating) + public void ignorableWhitespace (char ch [], int start, int length) + throws SAXException + { + if (consumer.isHidingWhitespace ()) + return; + characters (ch, start, length); + } + + // SAX2 lexical event + public void startCDATA () + throws SAXException + { + inCDATA = true; + // true except for the first fragment of a cdata section + mergeCDATA = false; + } + + // SAX2 lexical event + public void endCDATA () + throws SAXException + { + inCDATA = false; + } + + // SAX2 lexical event + // + // this SAX2 callback merges two unrelated things: + // - Declaration of the root element type ... belongs with + // the other DTD declaration methods, NOT HERE. + // - IDs for the optional external subset ... belongs here + // with other lexical information. + // + // ...and it doesn't include the internal DTD subset, desired + // both to support DOM L2 and to enable "pass through" processing + // + public void startDTD (String name, String publicId, String SystemId) + throws SAXException + { + // need to filter out comments and PIs within the DTD + inDTD = true; + } + + // SAX2 lexical event + public void endDTD () + throws SAXException + { + inDTD = false; + } + + // SAX2 lexical event + public void comment (char ch [], int start, int length) + throws SAXException + { + Node comment; + + // we can't create populated entity ref nodes using + // only public DOM APIs (they've got to be readonly) + if (consumer.isHidingComments () + || inDTD + || currentEntity != null) + return; + comment = document.createComment (new String (ch, start, length)); + top.appendChild (comment); + } + + /** + * May be overridden by subclasses to return true, indicating + * that entity reference nodes can be populated and then made + * read-only. + */ + public boolean canPopulateEntityRefs () + { return false; } + + // SAX2 lexical event + public void startEntity (String name) + throws SAXException + { + // are we ignoring what would be contents of an + // entity ref, since we can't populate it? + if (currentEntity != null) + return; + + // Are we hiding all entity boundaries? + if (consumer.isHidingReferences ()) + return; + + // SAX2 shows parameter entities; DOM hides them + if (name.charAt (0) == '%' || "[dtd]".equals (name)) + return; + + // Since we can't create a populated entity ref node in any + // standard way, we create an unpopulated one. + EntityReference ref = document.createEntityReference (name); + top.appendChild (ref); + top = ref; + + // ... allowing subclasses to populate them + if (!canPopulateEntityRefs ()) + currentEntity = name; + } + + // SAX2 lexical event + public void endEntity (String name) + throws SAXException + { + if (name.charAt (0) == '%' || "[dtd]".equals (name)) + return; + if (name.equals (currentEntity)) + currentEntity = null; + if (!consumer.isHidingReferences ()) + top = top.getParentNode (); + } + + + // SAX1 DTD event + public void notationDecl ( + String name, + String publicId, String SystemId + ) throws SAXException + { + /* IGNORE -- no public DOM API lets us store these + * into the doctype node + */ + } + + // SAX1 DTD event + public void unparsedEntityDecl ( + String name, + String publicId, String SystemId, + String notationName + ) throws SAXException + { + /* IGNORE -- no public DOM API lets us store these + * into the doctype node + */ + } + + // SAX2 declaration event + public void elementDecl (String name, String model) + throws SAXException + { + /* IGNORE -- no content model support in DOM L2 */ + } + + // SAX2 declaration event + public void attributeDecl ( + String eName, + String aName, + String type, + String mode, + String value + ) throws SAXException + { + /* IGNORE -- no attribute model support in DOM L2 */ + } + + // SAX2 declaration event + public void internalEntityDecl (String name, String value) + throws SAXException + { + /* IGNORE -- no public DOM API lets us store these + * into the doctype node + */ + } + + // SAX2 declaration event + public void externalEntityDecl ( + String name, + String publicId, + String SystemId + ) throws SAXException + { + /* IGNORE -- no public DOM API lets us store these + * into the doctype node + */ + } + + // + // These really should offer the option of nonfatal handling, + // like other validity errors, though that would cause major + // chaos in the DOM data structures. DOM is already spec'd + // to treat many of these as fatal, so this is consistent. + // + private void namespaceError (String description) + throws SAXParseException + { + SAXParseException err; + + err = new SAXParseException (description, locator); + throw err; + } + } +} diff --git a/libjava/classpath/gnu/xml/pipeline/EventConsumer.java b/libjava/classpath/gnu/xml/pipeline/EventConsumer.java new file mode 100644 index 000000000..a0a8824f7 --- /dev/null +++ b/libjava/classpath/gnu/xml/pipeline/EventConsumer.java @@ -0,0 +1,95 @@ +/* EventConsumer.java -- + Copyright (C) 1999,2000,2001 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.pipeline; + +import org.xml.sax.*; + + +/** + * Collects the event consumption apparatus of a SAX pipeline stage. + * Consumers which permit some handlers or other characteristics to be + * configured will provide methods to support that configuration. + * + *Two important categories of consumers include filters, which + * process events and pass them on to other consumers, and terminus + * (or terminal) stages, which don't pass events on. Filters are not + * necessarily derived from the {@link EventFilter} class, although that + * class can substantially simplify their construction by automating the + * most common activities. + * + *
Event consumers which follow certain conventions for the signatures + * of their constructors can be automatically assembled into pipelines + * by the {@link PipelineFactory} class. + * + * @author David Brownell + */ +public interface EventConsumer +{ + /** Most stages process these core SAX callbacks. */ + public ContentHandler getContentHandler (); + + /** Few stages will use unparsed entities. */ + public DTDHandler getDTDHandler (); + + /** + * This method works like the SAX2 XMLReader method of the same name, + * and is used to retrieve the optional lexical and declaration handlers + * in a pipeline. + * + * @param id This is a URI identifying the type of property desired. + * @return The value of that property, if it is defined. + * + * @exception SAXNotRecognizedException Thrown if the particular + * pipeline stage does not understand the specified identifier. + */ + public Object getProperty (String id) + throws SAXNotRecognizedException; + + /** + * This method provides a filter stage with a handler that abstracts + * presentation of warnings and both recoverable and fatal errors. + * Most pipeline stages should share a single policy and mechanism + * for such reports, since application components require consistency + * in such activities. Accordingly, typical responses to this method + * invocation involve saving the handler for use; filters will pass + * it on to any other consumers they use. + * + * @param handler encapsulates error handling policy for this stage + */ + public void setErrorHandler (ErrorHandler handler); +} diff --git a/libjava/classpath/gnu/xml/pipeline/EventFilter.java b/libjava/classpath/gnu/xml/pipeline/EventFilter.java new file mode 100644 index 000000000..b3cc2d654 --- /dev/null +++ b/libjava/classpath/gnu/xml/pipeline/EventFilter.java @@ -0,0 +1,796 @@ +/* EventFilter.java -- + Copyright (C) 1999,2000,2001 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.pipeline; + +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; + +import org.xml.sax.*; +import org.xml.sax.ext.*; +import org.xml.sax.helpers.XMLFilterImpl; + +/** + * A customizable event consumer, used to assemble various kinds of filters + * using SAX handlers and an optional second consumer. It can be constructed + * in two ways:
Additionally, SAX handlers may be assigned, which completely replace + * the "upstream" view (through {@link EventConsumer}) of handlers, initially + * null or the "next" consumer provided to the constructor. To make + * it easier to build specialized filter classes, this class implements + * all the standard SAX consumer handlers, and those implementations + * delegate "downstream" to the consumer accessed by {@link #getNext}. + * + *
The simplest way to create a custom a filter class is to create a + * subclass which overrides one or more handler interface methods. The + * constructor for that subclass then registers itself as a handler for + * those interfaces using a call such as setContentHandler(this), + * so the "upstream" view of event delivery is modified from the state + * established in the base class constructor. That way, + * the overridden methods intercept those event callbacks + * as they go "downstream", and + * all other event callbacks will pass events to any next consumer. + * Overridden methods may invoke superclass methods (perhaps after modifying + * parameters) if they wish to delegate such calls. Such subclasses + * should use {@link #getErrorHandler} to report errors using the + * common error reporting mechanism. + * + *
Another important technique is to construct a filter consisting + * of only a few specific types of handler. For example, one could easily + * prune out lexical events or various declarations by providing handlers + * which don't pass those events downstream, or by providing null handlers. + * + *
This may be viewed as the consumer oriented analogue of the SAX2 + * {@link org.xml.sax.helpers.XMLFilterImpl XMLFilterImpl} class. + * Key differences include:
The {@link #chainTo chainTo()} convenience routine supports chaining to + * an XMLFilterImpl, in its role as a limited functionality event + * consumer. Its event producer role ({@link XMLFilter}) is ignored. + * + *
The {@link #bind bind()} routine may be used associate event pipelines + * with any kind of {@link XMLReader} that will produce the events. + * Such pipelines don't necessarily need to have any members which are + * implemented using this class. That routine has some intelligence + * which supports automatic changes to parser feature flags, letting + * event piplines become largely independent of the particular feature + * sets of parsers. + * + * @author David Brownell + */ +public class EventFilter + implements EventConsumer, ContentHandler, DTDHandler, + LexicalHandler, DeclHandler +{ + // SAX handlers + private ContentHandler docHandler, docNext; + private DTDHandler dtdHandler, dtdNext; + private LexicalHandler lexHandler, lexNext; + private DeclHandler declHandler, declNext; + // and ideally, one more for the stuff SAX2 doesn't show + + private Locator locator; + private EventConsumer next; + private ErrorHandler errHandler; + + + /** SAX2 URI prefix for standard feature flags. */ + public static final String FEATURE_URI + = "http://xml.org/sax/features/"; + /** SAX2 URI prefix for standard properties (mostly for handlers). */ + public static final String PROPERTY_URI + = "http://xml.org/sax/properties/"; + + /** SAX2 property identifier for {@link DeclHandler} events */ + public static final String DECL_HANDLER + = PROPERTY_URI + "declaration-handler"; + /** SAX2 property identifier for {@link LexicalHandler} events */ + public static final String LEXICAL_HANDLER + = PROPERTY_URI + "lexical-handler"; + + // + // These class objects will be null if the relevant class isn't linked. + // Small configurations (pJava and some kinds of embedded systems) need + // to facilitate smaller executables. So "instanceof" is undesirable + // when bind() sees if it can remove some stages. + // + // SECURITY NOTE: assuming all these classes are part of the same sealed + // package, there's no problem saving these in the instance of this class + // that's associated with "this" class loader. But that wouldn't be true + // for classes in another package. + // + private static boolean loaded; + private static Class nsClass; + private static Class validClass; + private static Class wfClass; + private static Class xincClass; + + static ClassLoader getClassLoader () + { + Method m = null; + + try { + m = Thread.class.getMethod("getContextClassLoader"); + } catch (NoSuchMethodException e) { + // Assume that we are running JDK 1.1, use the current ClassLoader + return EventFilter.class.getClassLoader(); + } + + try { + return (ClassLoader) m.invoke(Thread.currentThread()); + } catch (IllegalAccessException e) { + // assert(false) + throw new UnknownError(e.getMessage()); + } catch (InvocationTargetException e) { + // assert(e.getTargetException() instanceof SecurityException) + throw new UnknownError(e.getMessage()); + } + } + + static Class loadClass (ClassLoader classLoader, String className) + { + try { + if (classLoader == null) + return Class.forName(className); + else + return classLoader.loadClass(className); + } catch (Exception e) { + return null; + } + } + + static private void loadClasses () + { + ClassLoader loader = getClassLoader (); + + nsClass = loadClass (loader, "gnu.xml.pipeline.NSFilter"); + validClass = loadClass (loader, "gnu.xml.pipeline.ValidationConsumer"); + wfClass = loadClass (loader, "gnu.xml.pipeline.WellFormednessFilter"); + xincClass = loadClass (loader, "gnu.xml.pipeline.XIncludeFilter"); + loaded = true; + } + + + /** + * Binds the standard SAX2 handlers from the specified consumer + * pipeline to the specified producer. These handlers include the core + * {@link ContentHandler} and {@link DTDHandler}, plus the extension + * {@link DeclHandler} and {@link LexicalHandler}. Any additional + * application-specific handlers need to be bound separately. + * The {@link ErrorHandler} is handled differently: the producer's + * error handler is passed through to the consumer pipeline. + * The producer is told to include namespace prefix information if it + * can, since many pipeline stages need that Infoset information to + * work well. + * + *
At the head of the pipeline, certain standard event filters are + * recognized and handled specially. This facilitates construction + * of processing pipelines that work regardless of the capabilities + * of the XMLReader implementation in use; for example, it permits + * validating output of a {@link gnu.xml.util.DomParser}.
Other than that, this method works with any kind of event consumer, + * not just event filters. Note that in all cases, the standard handlers + * are assigned; any previous handler assignments for the handler will + * be overridden. + * + * @param producer will deliver events to the specified consumer + * @param consumer pipeline supplying event handlers to be associated + * with the producer (may not be null) + */ + public static void bind (XMLReader producer, EventConsumer consumer) + { + Class klass = null; + boolean prefixes; + + if (!loaded) + loadClasses (); + + // DOM building, printing, layered validation, and other + // things don't work well when prefix info is discarded. + // Include it by default, whenever possible. + try { + producer.setFeature (FEATURE_URI + "namespace-prefixes", + true); + prefixes = true; + } catch (SAXException e) { + prefixes = false; + } + + // NOTE: This loop doesn't use "instanceof", since that + // would prevent compiling/linking without those classes + // being present. + while (consumer != null) { + klass = consumer.getClass (); + + // we might have already changed this problematic SAX2 default. + if (nsClass != null && nsClass.isAssignableFrom (klass)) { + if (!prefixes) + break; + consumer = ((EventFilter)consumer).getNext (); + + // the parser _might_ do DTD validation by default ... + // if not, maybe we can change this setting. + } else if (validClass != null + && validClass.isAssignableFrom (klass)) { + try { + producer.setFeature (FEATURE_URI + "validation", + true); + consumer = ((ValidationConsumer)consumer).getNext (); + } catch (SAXException e) { + break; + } + + // parsers are required not to have such bugs + } else if (wfClass != null && wfClass.isAssignableFrom (klass)) { + consumer = ((WellFormednessFilter)consumer).getNext (); + + // stop on the first pipeline stage we can't remove + } else + break; + + if (consumer == null) + klass = null; + } + + // the actual setting here doesn't matter as much + // as that producer and consumer agree + if (xincClass != null && klass != null + && xincClass.isAssignableFrom (klass)) + ((XIncludeFilter)consumer).setSavingPrefixes (prefixes); + + // Some SAX parsers can't handle null handlers -- bleech + DefaultHandler2 h = new DefaultHandler2 (); + + if (consumer != null && consumer.getContentHandler () != null) + producer.setContentHandler (consumer.getContentHandler ()); + else + producer.setContentHandler (h); + if (consumer != null && consumer.getDTDHandler () != null) + producer.setDTDHandler (consumer.getDTDHandler ()); + else + producer.setDTDHandler (h); + + try { + Object dh; + + if (consumer != null) + dh = consumer.getProperty (DECL_HANDLER); + else + dh = null; + if (dh == null) + dh = h; + producer.setProperty (DECL_HANDLER, dh); + } catch (Exception e) { /* ignore */ } + try { + Object lh; + + if (consumer != null) + lh = consumer.getProperty (LEXICAL_HANDLER); + else + lh = null; + if (lh == null) + lh = h; + producer.setProperty (LEXICAL_HANDLER, lh); + } catch (Exception e) { /* ignore */ } + + // this binding goes the other way around + if (producer.getErrorHandler () == null) + producer.setErrorHandler (h); + if (consumer != null) + consumer.setErrorHandler (producer.getErrorHandler ()); + } + + /** + * Initializes all handlers to null. + */ + // constructor used by PipelineFactory + public EventFilter () { } + + + /** + * Handlers that are not otherwise set will default to those from + * the specified consumer, making it easy to pass events through. + * If the consumer is null, all handlers are initialzed to null. + */ + // constructor used by PipelineFactory + public EventFilter (EventConsumer consumer) + { + if (consumer == null) + return; + + next = consumer; + + // We delegate through the "xxNext" handlers, and + // report the "xxHandler" ones on our input side. + + // Normally a subclass would both override handler + // methods and register itself as the "xxHandler". + + docHandler = docNext = consumer.getContentHandler (); + dtdHandler = dtdNext = consumer.getDTDHandler (); + try { + declHandler = declNext = (DeclHandler) + consumer.getProperty (DECL_HANDLER); + } catch (SAXException e) { /* leave value null */ } + try { + lexHandler = lexNext = (LexicalHandler) + consumer.getProperty (LEXICAL_HANDLER); + } catch (SAXException e) { /* leave value null */ } + } + + /** + * Treats the XMLFilterImpl as a limited functionality event consumer, + * by arranging to deliver events to it; this lets such classes be + * "wrapped" as pipeline stages. + * + *
Upstream Event Setup: + * If no handlers have been assigned to this EventFilter, then the + * handlers from specified XMLFilterImpl are returned from this + * {@link EventConsumer}: the XMLFilterImpl is just "wrapped". + * Otherwise the specified handlers will be returned. + * + *
Downstream Event Setup: + * Subclasses may chain event delivery to the specified XMLFilterImpl + * by invoking the appropiate superclass methods, + * as if their constructor passed a "next" EventConsumer to the + * constructor for this class. + * If this EventFilter has an ErrorHandler, it is assigned as + * the error handler for the XMLFilterImpl, just as would be + * done for a next stage implementing {@link EventConsumer}. + * + * @param next the next downstream component of the pipeline. + * @exception IllegalStateException if the "next" consumer has + * already been set through the constructor. + */ + public void chainTo (XMLFilterImpl next) + { + if (this.next != null) + throw new IllegalStateException (); + + docNext = next.getContentHandler (); + if (docHandler == null) + docHandler = docNext; + dtdNext = next.getDTDHandler (); + if (dtdHandler == null) + dtdHandler = dtdNext; + + try { + declNext = (DeclHandler) next.getProperty (DECL_HANDLER); + if (declHandler == null) + declHandler = declNext; + } catch (SAXException e) { /* leave value null */ } + try { + lexNext = (LexicalHandler) next.getProperty (LEXICAL_HANDLER); + if (lexHandler == null) + lexHandler = lexNext; + } catch (SAXException e) { /* leave value null */ } + + if (errHandler != null) + next.setErrorHandler (errHandler); + } + + /** + * Records the error handler that should be used by this stage, and + * passes it "downstream" to any subsequent stage. + */ + final public void setErrorHandler (ErrorHandler handler) + { + errHandler = handler; + if (next != null) + next.setErrorHandler (handler); + } + + /** + * Returns the error handler assigned this filter stage, or null + * if no such assigment has been made. + */ + final public ErrorHandler getErrorHandler () + { + return errHandler; + } + + + /** + * Returns the next event consumer in sequence; or null if there + * is no such handler. + */ + final public EventConsumer getNext () + { return next; } + + + /** + * Assigns the content handler to use; a null handler indicates + * that these events will not be forwarded. + * This overrides the previous settting for this handler, which was + * probably pointed to the next consumer by the base class constructor. + */ + final public void setContentHandler (ContentHandler h) + { + docHandler = h; + } + + /** Returns the content handler being used. */ + final public ContentHandler getContentHandler () + { + return docHandler; + } + + /** + * Assigns the DTD handler to use; a null handler indicates + * that these events will not be forwarded. + * This overrides the previous settting for this handler, which was + * probably pointed to the next consumer by the base class constructor. + */ + final public void setDTDHandler (DTDHandler h) + { dtdHandler = h; } + + /** Returns the dtd handler being used. */ + final public DTDHandler getDTDHandler () + { + return dtdHandler; + } + + /** + * Stores the property, normally a handler; a null handler indicates + * that these events will not be forwarded. + * This overrides the previous handler settting, which was probably + * pointed to the next consumer by the base class constructor. + */ + final public void setProperty (String id, Object o) + throws SAXNotRecognizedException, SAXNotSupportedException + { + try { + Object value = getProperty (id); + + if (value == o) + return; + if (DECL_HANDLER.equals (id)) { + declHandler = (DeclHandler) o; + return; + } + if (LEXICAL_HANDLER.equals (id)) { + lexHandler = (LexicalHandler) o; + return; + } + throw new SAXNotSupportedException (id); + + } catch (ClassCastException e) { + throw new SAXNotSupportedException (id); + } + } + + /** Retrieves a property of unknown intent (usually a handler) */ + final public Object getProperty (String id) + throws SAXNotRecognizedException + { + if (DECL_HANDLER.equals (id)) + return declHandler; + if (LEXICAL_HANDLER.equals (id)) + return lexHandler; + + throw new SAXNotRecognizedException (id); + } + + /** + * Returns any locator provided to the next consumer, if this class + * (or a subclass) is handling {@link ContentHandler } events. + */ + public Locator getDocumentLocator () + { return locator; } + + + // CONTENT HANDLER DELEGATIONS + + /** SAX2: passes this callback to the next consumer, if any */ + public void setDocumentLocator (Locator locator) + { + this.locator = locator; + if (docNext != null) + docNext.setDocumentLocator (locator); + } + + /** SAX2: passes this callback to the next consumer, if any */ + public void startDocument () throws SAXException + { + if (docNext != null) + docNext.startDocument (); + } + + /** SAX2: passes this callback to the next consumer, if any */ + public void skippedEntity (String name) throws SAXException + { + if (docNext != null) + docNext.skippedEntity (name); + } + + /** SAX2: passes this callback to the next consumer, if any */ + public void processingInstruction (String target, String data) + throws SAXException + { + if (docNext != null) + docNext.processingInstruction (target, data); + } + + /** SAX2: passes this callback to the next consumer, if any */ + public void characters (char ch [], int start, int length) + throws SAXException + { + if (docNext != null) + docNext.characters (ch, start, length); + } + + /** SAX2: passes this callback to the next consumer, if any */ + public void ignorableWhitespace (char ch [], int start, int length) + throws SAXException + { + if (docNext != null) + docNext.ignorableWhitespace (ch, start, length); + } + + /** SAX2: passes this callback to the next consumer, if any */ + public void startPrefixMapping (String prefix, String uri) + throws SAXException + { + if (docNext != null) + docNext.startPrefixMapping (prefix, uri); + } + + /** SAX2: passes this callback to the next consumer, if any */ + public void startElement ( + String uri, String localName, + String qName, Attributes atts + ) throws SAXException + { + if (docNext != null) + docNext.startElement (uri, localName, qName, atts); + } + + /** SAX2: passes this callback to the next consumer, if any */ + public void endElement (String uri, String localName, String qName) + throws SAXException + { + if (docNext != null) + docNext.endElement (uri, localName, qName); + } + + /** SAX2: passes this callback to the next consumer, if any */ + public void endPrefixMapping (String prefix) throws SAXException + { + if (docNext != null) + docNext.endPrefixMapping (prefix); + } + + /** SAX2: passes this callback to the next consumer, if any */ + public void endDocument () throws SAXException + { + if (docNext != null) + docNext.endDocument (); + locator = null; + } + + + // DTD HANDLER DELEGATIONS + + /** SAX1: passes this callback to the next consumer, if any */ + public void unparsedEntityDecl ( + String name, + String publicId, + String systemId, + String notationName + ) throws SAXException + { + if (dtdNext != null) + dtdNext.unparsedEntityDecl (name, publicId, systemId, notationName); + } + + /** SAX1: passes this callback to the next consumer, if any */ + public void notationDecl (String name, String publicId, String systemId) + throws SAXException + { + if (dtdNext != null) + dtdNext.notationDecl (name, publicId, systemId); + } + + + // LEXICAL HANDLER DELEGATIONS + + /** SAX2: passes this callback to the next consumer, if any */ + public void startDTD (String name, String publicId, String systemId) + throws SAXException + { + if (lexNext != null) + lexNext.startDTD (name, publicId, systemId); + } + + /** SAX2: passes this callback to the next consumer, if any */ + public void endDTD () + throws SAXException + { + if (lexNext != null) + lexNext.endDTD (); + } + + /** SAX2: passes this callback to the next consumer, if any */ + public void comment (char ch [], int start, int length) + throws SAXException + { + if (lexNext != null) + lexNext.comment (ch, start, length); + } + + /** SAX2: passes this callback to the next consumer, if any */ + public void startCDATA () + throws SAXException + { + if (lexNext != null) + lexNext.startCDATA (); + } + + /** SAX2: passes this callback to the next consumer, if any */ + public void endCDATA () + throws SAXException + { + if (lexNext != null) + lexNext.endCDATA (); + } + + /** + * SAX2: passes this callback to the next consumer, if any. + */ + public void startEntity (String name) + throws SAXException + { + if (lexNext != null) + lexNext.startEntity (name); + } + + /** + * SAX2: passes this callback to the next consumer, if any. + */ + public void endEntity (String name) + throws SAXException + { + if (lexNext != null) + lexNext.endEntity (name); + } + + + // DECLARATION HANDLER DELEGATIONS + + + /** SAX2: passes this callback to the next consumer, if any */ + public void elementDecl (String name, String model) + throws SAXException + { + if (declNext != null) + declNext.elementDecl (name, model); + } + + /** SAX2: passes this callback to the next consumer, if any */ + public void attributeDecl (String eName, String aName, + String type, String mode, String value) + throws SAXException + { + if (declNext != null) + declNext.attributeDecl (eName, aName, type, mode, value); + } + + /** SAX2: passes this callback to the next consumer, if any */ + public void externalEntityDecl (String name, + String publicId, String systemId) + throws SAXException + { + if (declNext != null) + declNext.externalEntityDecl (name, publicId, systemId); + } + + /** SAX2: passes this callback to the next consumer, if any */ + public void internalEntityDecl (String name, String value) + throws SAXException + { + if (declNext != null) + declNext.internalEntityDecl (name, value); + } +} diff --git a/libjava/classpath/gnu/xml/pipeline/LinkFilter.java b/libjava/classpath/gnu/xml/pipeline/LinkFilter.java new file mode 100644 index 000000000..e11a5eca6 --- /dev/null +++ b/libjava/classpath/gnu/xml/pipeline/LinkFilter.java @@ -0,0 +1,242 @@ +/* LinkFilter.java -- + Copyright (C) 1999,2000,2001 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.pipeline; + +import java.io.IOException; +import java.net.URL; +import java.util.Enumeration; +import java.util.Vector; + +import org.xml.sax.Attributes; +import org.xml.sax.SAXException; + + +/** + * Pipeline filter to remember XHTML links found in a document, + * so they can later be crawled. Fragments are not counted, and duplicates + * are ignored. Callers are responsible for filtering out URLs they aren't + * interested in. Events are passed through unmodified. + * + *
Input MUST include a setDocumentLocator() call, as it's used to + * resolve relative links in the absence of a "base" element. Input MUST + * also include namespace identifiers, since it is the XHTML namespace + * identifier which is used to identify the relevant elements. + * + *
FIXME: handle xml:base attribute ... in association with + * a stack of base URIs. Similarly, recognize/support XLink data. + * + * @author David Brownell + */ +public class LinkFilter extends EventFilter +{ + // for storing URIs + private Vector vector = new Vector (); + + // struct for "full" link record (tbd) + // these for troubleshooting original source: + // original uri + // uri as resolved (base, relative, etc) + // URI of originating doc + // line # + // original element + attrs (img src, desc, etc) + + // XLink model of the link ... for inter-site pairups ? + + private String baseURI; + + private boolean siteRestricted = false; + + // + // XXX leverage blacklist info (like robots.txt) + // + // XXX constructor w/param ... pipeline for sending link data + // probably XHTML --> XLink, providing info as sketched above + // + + + /** + * Constructs a new event filter, which collects links in private data + * structure for later enumeration. + */ + // constructor used by PipelineFactory + public LinkFilter () + { + super.setContentHandler (this); + } + + + /** + * Constructs a new event filter, which collects links in private data + * structure for later enumeration and passes all events, unmodified, + * to the next consumer. + */ + // constructor used by PipelineFactory + public LinkFilter (EventConsumer next) + { + super (next); + super.setContentHandler (this); + } + + + /** + * Returns an enumeration of the links found since the filter + * was constructed, or since removeAllLinks() was called. + * + * @return enumeration of strings. + */ + public Enumeration getLinks () + { + return vector.elements (); + } + + /** + * Removes records about all links reported to the event + * stream, as if the filter were newly created. + */ + public void removeAllLinks () + { + vector = new Vector (); + } + + + /** + * Collects URIs for (X)HTML content from elements which hold them. + */ + public void startElement ( + String uri, + String localName, + String qName, + Attributes atts + ) throws SAXException + { + String link; + + // Recognize XHTML links. + if ("http://www.w3.org/1999/xhtml".equals (uri)) { + + if ("a".equals (localName) || "base".equals (localName) + || "area".equals (localName)) + link = atts.getValue ("href"); + else if ("iframe".equals (localName) || "frame".equals (localName)) + link = atts.getValue ("src"); + else if ("blockquote".equals (localName) || "q".equals (localName) + || "ins".equals (localName) || "del".equals (localName)) + link = atts.getValue ("cite"); + else + link = null; + link = maybeAddLink (link); + + // "base" modifies designated baseURI + if ("base".equals (localName) && link != null) + baseURI = link; + + if ("iframe".equals (localName) || "img".equals (localName)) + maybeAddLink (atts.getValue ("longdesc")); + } + + super.startElement (uri, localName, qName, atts); + } + + private String maybeAddLink (String link) + { + int index; + + // ignore empty links and fragments inside docs + if (link == null) + return null; + if ((index = link.indexOf ("#")) >= 0) + link = link.substring (0, index); + if (link.equals ("")) + return null; + + try { + // get the real URI + URL base = new URL ((baseURI != null) + ? baseURI + : getDocumentLocator ().getSystemId ()); + URL url = new URL (base, link); + + link = url.toString (); + + // ignore duplicates + if (vector.contains (link)) + return link; + + // other than what "base" does, stick to original site: + if (siteRestricted) { + // don't switch protocols + if (!base.getProtocol ().equals (url.getProtocol ())) + return link; + // don't switch servers + if (base.getHost () != null + && !base.getHost ().equals (url.getHost ())) + return link; + } + + vector.addElement (link); + + return link; + + } catch (IOException e) { + // bad URLs we don't want + } + return null; + } + + /** + * Reports an error if no Locator has been made available. + */ + public void startDocument () + throws SAXException + { + if (getDocumentLocator () == null) + throw new SAXException ("no Locator!"); + } + + /** + * Forgets about any base URI information that may be recorded. + * Applications will often want to call removeAllLinks(), likely + * after examining the links which were reported. + */ + public void endDocument () + throws SAXException + { + baseURI = null; + super.endDocument (); + } +} diff --git a/libjava/classpath/gnu/xml/pipeline/NSFilter.java b/libjava/classpath/gnu/xml/pipeline/NSFilter.java new file mode 100644 index 000000000..0fa4621d3 --- /dev/null +++ b/libjava/classpath/gnu/xml/pipeline/NSFilter.java @@ -0,0 +1,341 @@ +/* NSFilter.java -- + Copyright (C) 1999,2000,2001 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.pipeline; + +import java.util.Enumeration; +import java.util.Stack; + +import org.xml.sax.Attributes; +import org.xml.sax.ErrorHandler; +import org.xml.sax.Locator; +import org.xml.sax.SAXException; +import org.xml.sax.SAXParseException; +import org.xml.sax.helpers.AttributesImpl; +import org.xml.sax.helpers.NamespaceSupport; + +/** + * This filter ensures that element and attribute names are properly prefixed, + * and that such prefixes are declared. Such data is critical for operations + * like writing XML text, and validating against DTDs: names or their prefixes + * may have been discarded, although they are essential to the exchange of + * information using XML. There are various common ways that such data + * gets discarded:
This filter uses a heuristic to choose the prefix to assign to any + * particular name which wasn't already corectly prefixed. The associated + * namespace will be correct, and the prefix will be declared. Original + * structures facilitating text editing, such as conventions about use of + * mnemonic prefix names or the scoping of prefixes, can't always be + * reconstructed after they are discarded, as strongly encouraged by the + * current SAX2 defaults. + * + *
Note that this can't possibly know whether values inside attribute + * value or document content involve prefixed names. If your application + * requires using prefixed names in such locations you'll need to add some + * appropriate logic (perhaps adding additional heuristics in a subclass). + * + * @author David Brownell + */ +public class NSFilter extends EventFilter +{ + private NamespaceSupport nsStack = new NamespaceSupport (); + private Stack elementStack = new Stack (); + + private boolean pushedContext; + private String nsTemp [] = new String [3]; + private AttributesImpl attributes = new AttributesImpl (); + private boolean usedDefault; + + // gensymmed prefixes use this root name + private static final String prefixRoot = "prefix-"; + + + /** + * Passes events through to the specified consumer, after first + * processing them. + * + * @param next the next event consumer to receive events. + */ + // constructor used by PipelineFactory + public NSFilter (EventConsumer next) + { + super (next); + + setContentHandler (this); + } + + private void fatalError (String message) + throws SAXException + { + SAXParseException e; + ErrorHandler handler = getErrorHandler (); + Locator locator = getDocumentLocator (); + + if (locator == null) + e = new SAXParseException (message, null, null, -1, -1); + else + e = new SAXParseException (message, locator); + if (handler != null) + handler.fatalError (e); + throw e; + } + + + public void startDocument () throws SAXException + { + elementStack.removeAllElements (); + nsStack.reset (); + pushedContext = false; + super.startDocument (); + } + + /** + * This call is not passed to the next consumer in the chain. + * Prefix declarations and scopes are only exposed in the form + * of attributes; this callback just records a declaration that + * will be exposed as an attribute. + */ + public void startPrefixMapping (String prefix, String uri) + throws SAXException + { + if (pushedContext == false) { + nsStack.pushContext (); + pushedContext = true; + } + + // this check is awkward, but the paranoia prevents big trouble + for (Enumeration e = nsStack.getDeclaredPrefixes (); + e.hasMoreElements (); + /* NOP */ ) { + String declared = (String) e.nextElement (); + + if (!declared.equals (prefix)) + continue; + if (uri.equals (nsStack.getURI (prefix))) + return; + fatalError ("inconsistent binding for prefix '" + prefix + + "' ... " + uri + " (was " + nsStack.getURI (prefix) + ")"); + } + + if (!nsStack.declarePrefix (prefix, uri)) + fatalError ("illegal prefix declared: " + prefix); + } + + private String fixName (String ns, String l, String name, boolean isAttr) + throws SAXException + { + if ("".equals (name) || name == null) { + name = l; + if ("".equals (name) || name == null) + fatalError ("empty/null name"); + } + + // can we correctly process the name as-is? + // handles "element scope" attribute names here. + if (nsStack.processName (name, nsTemp, isAttr) != null + && nsTemp [0].equals (ns) + ) { + return nsTemp [2]; + } + + // nope, gotta modify the name or declare a default mapping + int temp; + + // get rid of any current prefix + if ((temp = name.indexOf (':')) >= 0) { + name = name.substring (temp + 1); + + // ... maybe that's enough (use/prefer default namespace) ... + if (!isAttr && nsStack.processName (name, nsTemp, false) != null + && nsTemp [0].equals (ns) + ) { + return nsTemp [2]; + } + } + + // must we define and use the default/undefined prefix? + if ("".equals (ns)) { + if (isAttr) + fatalError ("processName bug"); + if (attributes.getIndex ("xmlns") != -1) + fatalError ("need to undefine default NS, but it's bound: " + + attributes.getValue ("xmlns")); + + nsStack.declarePrefix ("", ""); + attributes.addAttribute ("", "", "xmlns", "CDATA", ""); + return name; + } + + // is there at least one non-null prefix we can use? + for (Enumeration e = nsStack.getDeclaredPrefixes (); + e.hasMoreElements (); + /* NOP */) { + String prefix = (String) e.nextElement (); + String uri = nsStack.getURI (prefix); + + if (uri == null || !uri.equals (ns)) + continue; + return prefix + ":" + name; + } + + // no such luck. create a prefix name, declare it, use it. + for (temp = 0; temp >= 0; temp++) { + String prefix = prefixRoot + temp; + + if (nsStack.getURI (prefix) == null) { + nsStack.declarePrefix (prefix, ns); + attributes.addAttribute ("", "", "xmlns:" + prefix, + "CDATA", ns); + return prefix + ":" + name; + } + } + fatalError ("too many prefixes genned"); + // NOTREACHED + return null; + } + + public void startElement ( + String uri, String localName, + String qName, Attributes atts + ) throws SAXException + { + if (!pushedContext) + nsStack.pushContext (); + pushedContext = false; + + // make sure we have all NS declarations handy before we start + int length = atts.getLength (); + + for (int i = 0; i < length; i++) { + String aName = atts.getQName (i); + + if (!aName.startsWith ("xmlns")) + continue; + + String prefix; + + if ("xmlns".equals (aName)) + prefix = ""; + else if (aName.indexOf (':') == 5) + prefix = aName.substring (6); + else // "xmlnsfoo" etc. + continue; + startPrefixMapping (prefix, atts.getValue (i)); + } + + // put namespace decls at the start of our regenned attlist + attributes.clear (); + for (Enumeration e = nsStack.getDeclaredPrefixes (); + e.hasMoreElements (); + /* NOP */) { + String prefix = (String) e.nextElement (); + + attributes.addAttribute ("", "", + ("".equals (prefix) + ? "xmlns" + : "xmlns:" + prefix), + "CDATA", + nsStack.getURI (prefix)); + } + + // name fixups: element, then attributes. + // fixName may declare a new prefix or, for the element, + // redeclare the default (if element name needs it). + qName = fixName (uri, localName, qName, false); + + for (int i = 0; i < length; i++) { + String aName = atts.getQName (i); + String aNS = atts.getURI (i); + String aLocal = atts.getLocalName (i); + String aType = atts.getType (i); + String aValue = atts.getValue (i); + + if (aName.startsWith ("xmlns")) + continue; + aName = fixName (aNS, aLocal, aName, true); + attributes.addAttribute (aNS, aLocal, aName, aType, aValue); + } + + elementStack.push (qName); + + // pass event along, with cleaned-up names and decls. + super.startElement (uri, localName, qName, attributes); + } + + public void endElement (String uri, String localName, String qName) + throws SAXException + { + nsStack.popContext (); + qName = (String) elementStack.pop (); + super.endElement (uri, localName, qName); + } + + /** + * This call is not passed to the next consumer in the chain. + * Prefix declarations and scopes are only exposed in their + * attribute form. + */ + public void endPrefixMapping (String prefix) + throws SAXException + { } + + public void endDocument () throws SAXException + { + elementStack.removeAllElements (); + nsStack.reset (); + super.endDocument (); + } +} diff --git a/libjava/classpath/gnu/xml/pipeline/PipelineFactory.java b/libjava/classpath/gnu/xml/pipeline/PipelineFactory.java new file mode 100644 index 000000000..c2adab021 --- /dev/null +++ b/libjava/classpath/gnu/xml/pipeline/PipelineFactory.java @@ -0,0 +1,723 @@ +/* PipelineFactory.java -- + Copyright (C) 1999,2000,2001 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.pipeline; + +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.OutputStream; +import java.io.OutputStreamWriter; +import java.lang.reflect.Constructor; +import java.util.StringTokenizer; + +import org.xml.sax.*; +import org.xml.sax.ext.*; + + +/** + * This provides static factory methods for creating simple event pipelines. + * These pipelines are specified by strings, suitable for passing on + * command lines or embedding in element attributes. For example, one way + * to write a pipeline that restores namespace syntax, validates (stopping + * the pipeline on validity errors) and then writes valid data to standard + * output is this:
+ * nsfix | validate | write ( stdout )+ * + *
In this syntax, the tokens are always separated by whitespace, and each + * stage of the pipeline may optionally have a parameter (which can be a + * pipeline) in parentheses. Interior stages are called filters, and the + * rightmost end of a pipeline is called a terminus. + * + *
Stages are usually implemented by a single class, which may not be + * able to act as both a filter and a terminus; but any terminus can be + * automatically turned into a filter, through use of a {@link TeeConsumer}. + * The stage identifiers are either class names, or are one of the following + * short identifiers built into this class. (Most of these identifiers are + * no more than aliases for classes.) The built-in identifiers include:
+ +Stage | +Parameter | +Terminus | +Description | +
---|---|---|---|
dom | +none | +yes | +Applications code can access a DOM Document built + from the input event stream. When used as a filter, this buffers + data up to an endDocument call, and then uses a DOM parser + to report everything that has been recorded (which can easily be + less than what was reported to it). | +
nsfix | +none | +no | +This stage ensures that the XML element and attribute + names in its output use namespace prefixes and declarations correctly. + That is, so that they match the "Namespace plus LocalName" naming data + with which each XML element and attribute is already associated. | +
null | +none | +yes | +This stage ignores all input event data. | +
server | +required server URL |
+ no | +Sends its input as XML request to a remote server, + normally a web application server using the HTTP or HTTPS protocols. + The output of this stage is the parsed response from that server. | +
tee | +required first pipeline |
+ no | +This sends its events down two paths; its parameter + is a pipeline descriptor for the first path, and the second path + is the output of this stage. | +
validate | +none | +yes | +This checks for validity errors, and reports them + through its error handler. The input must include declaration events + and some lexical events. | +
wf | +none | +yes | +This class provides some basic "well formedness" + tests on the input event stream, and reports a fatal error if any + of them fail. One example: start/end calls for elements must match. + No SAX parser is permitted to produce malformed output, but other + components can easily do so. | +
write | +required "stdout", "stderr", or filename |
+ yes | +Writes its input to the specified output, as pretty + printed XML text encoded using UTF-8. Input events must be well + formed and "namespace fixed", else the output won't be XML (or possibly + namespace) conformant. The symbolic names represent + System.out and System.err respectively; names must + correspond to files which don't yet exist. | +
xhtml | +required "stdout", "stderr", or filename |
+ yes | +Like write (above), except that XHTML rules + are followed. The XHTML 1.0 Transitional document type is declared, + and only ASCII characters are written (for interoperability). Other + characters are written as entity or character references; the text is + pretty printed. | +
xinclude | +none | +no | +This stage handles XInclude processing. + This is like entity inclusion, except that the included content + is declared in-line rather than in the DTD at the beginning of + a document. + | +
xslt | +required XSLT stylesheet URI |
+ no | +This stage handles XSLT transformation + according to a stylesheet. + The implementation of the transformation may not actually + stream data, although if such an XSLT engine is in use + then that can happen. + | +
Note that {@link EventFilter#bind} can automatically eliminate + * some filters by setting SAX2 parser features appropriately. This means + * that you can routinely put filters like "nsfix", "validate", or "wf" at the + * front of a pipeline (for components that need inputs conditioned to match + * that level of correctness), and know that it won't actually be used unless + * it's absolutely necessary. + * + * @author David Brownell + */ +public class PipelineFactory +{ + /** + * Creates a simple pipeline according to the description string passed in. + */ + public static EventConsumer createPipeline (String description) + throws IOException + { + return createPipeline (description, null); + } + + /** + * Extends an existing pipeline by prepending the filter pipeline to the + * specified consumer. Some pipelines need more customization than can + * be done through this simplified syntax. When they are set up with + * direct API calls, use this method to merge more complex pipeline + * segments with easily configured ones. + */ + public static EventConsumer createPipeline ( + String description, + EventConsumer next + ) throws IOException + { + // tokens are (for now) what's separated by whitespace; + // very easy to parse, but IDs never have spaces. + + StringTokenizer tokenizer; + String tokens []; + + tokenizer = new StringTokenizer (description); + tokens = new String [tokenizer.countTokens ()]; + for (int i = 0; i < tokens.length; i++) + tokens [i] = tokenizer.nextToken (); + + PipelineFactory factory = new PipelineFactory (); + Pipeline pipeline = factory.parsePipeline (tokens, next); + + return pipeline.createPipeline (); + } + + + private PipelineFactory () { /* NYET */ } + + + /** + * Extends an existing pipeline by prepending a pre-tokenized filter + * pipeline to the specified consumer. Tokens are class names (or the + * predefined aliases) left and right parenthesis, and the vertical bar. + */ + public static EventConsumer createPipeline ( + String tokens [], + EventConsumer next + ) throws IOException + { + PipelineFactory factory = new PipelineFactory (); + Pipeline pipeline = factory.parsePipeline (tokens, next); + + return pipeline.createPipeline (); + } + + + private String tokens []; + private int index; + + private Pipeline parsePipeline (String toks [], EventConsumer next) + { + tokens = toks; + index = 0; + + Pipeline retval = parsePipeline (next); + + if (index != toks.length) + throw new ArrayIndexOutOfBoundsException ( + "extra token: " + tokens [index]); + return retval; + } + + // pipeline ::= stage | stage '|' pipeline + private Pipeline parsePipeline (EventConsumer next) + { + Pipeline retval = new Pipeline (parseStage ()); + + // minimal pipelines: "stage" and "... | id" + if (index > (tokens.length - 2) + || !"|".equals (tokens [index]) + ) { + retval.next = next; + return retval; + } + index++; + retval.rest = parsePipeline (next); + return retval; + } + + // stage ::= id | id '(' pipeline ')' + private Stage parseStage () + { + Stage retval = new Stage (tokens [index++]); + + // minimal stages: "id" and "id ( id )" + if (index > (tokens.length - 2) + || !"(".equals (tokens [index]) /*)*/ + ) + return retval; + + index++; + retval.param = parsePipeline (null); + if (index >= tokens.length) + throw new ArrayIndexOutOfBoundsException ( + "missing right paren"); + if (/*(*/ !")".equals (tokens [index++])) + throw new ArrayIndexOutOfBoundsException ( + "required right paren, not: " + tokens [index - 1]); + return retval; + } + + + // + // these classes obey the conventions for constructors, so they're + // only built in to this table of shortnames + // + // - filter (one or two types of arglist) + // * last constructor is 'next' element + // * optional (first) string parameter + // + // - terminus (one or types of arglist) + // * optional (only) string parameter + // + // terminus stages are transformed into filters if needed, by + // creating a "tee". filter stages aren't turned to terminus + // stages though; either eliminate such stages, or add some + // terminus explicitly. + // + private static final String builtinStages [][] = { + { "dom", "gnu.xml.dom.Consumer" }, + { "nsfix", "gnu.xml.pipeline.NSFilter" }, + { "null", "gnu.xml.pipeline.EventFilter" }, + { "server", "gnu.xml.pipeline.CallFilter" }, + { "tee", "gnu.xml.pipeline.TeeConsumer" }, + { "validate", "gnu.xml.pipeline.ValidationConsumer" }, + { "wf", "gnu.xml.pipeline.WellFormednessFilter" }, + { "xinclude", "gnu.xml.pipeline.XIncludeFilter" }, + { "xslt", "gnu.xml.pipeline.XsltFilter" }, + +// XXX want: option for validate, to preload external part of a DTD + + // xhtml, write ... nyet generic-ready + }; + + private static class Stage + { + String id; + Pipeline param; + + Stage (String name) + { id = name; } + + public String toString () + { + if (param == null) + return id; + return id + " ( " + param + " )"; + } + + private void fail (String message) + throws IOException + { + throw new IOException ("in '" + id + + "' stage of pipeline, " + message); + } + + EventConsumer createStage (EventConsumer next) + throws IOException + { + String name = id; + + // most builtins are just class aliases + for (int i = 0; i < builtinStages.length; i++) { + if (id.equals (builtinStages [i][0])) { + name = builtinStages [i][1]; + break; + } + } + + // Save output as XML or XHTML text + if ("write".equals (name) || "xhtml".equals (name)) { + String filename; + boolean isXhtml = "xhtml".equals (name); + OutputStream out = null; + TextConsumer consumer; + + if (param == null) + fail ("parameter is required"); + + filename = param.toString (); + if ("stdout".equals (filename)) + out = System.out; + else if ("stderr".equals (filename)) + out = System.err; + else { + File f = new File (filename); + +/* + if (!f.isAbsolute ()) + fail ("require absolute file paths"); + */ + if (f.exists ()) + fail ("file already exists: " + f.getName ()); + +// XXX this races against the existence test + out = new FileOutputStream (f); + } + + if (!isXhtml) + consumer = new TextConsumer (out); + else + consumer = new TextConsumer ( + new OutputStreamWriter (out, "8859_1"), + true); + + consumer.setPrettyPrinting (true); + if (next == null) + return consumer; + return new TeeConsumer (consumer, next); + + } else { + // + // Here go all the builtins that are just aliases for + // classes, and all stage IDs that started out as such + // class names. The following logic relies on several + // documented conventions for constructor invocation. + // + String msg = null; + + try { + Class klass = Class.forName (name); + Class argTypes [] = null; + Constructor constructor = null; + boolean filter = false; + Object params [] = null; + Object obj = null; + + // do we need a filter stage? + if (next != null) { + // "next" consumer is always passed, with + // or without the optional string param + if (param == null) { + argTypes = new Class [1]; + argTypes [0] = EventConsumer.class; + + params = new Object [1]; + params [0] = next; + + msg = "no-param filter"; + } else { + argTypes = new Class [2]; + argTypes [0] = String.class; + argTypes [1] = EventConsumer.class; + + params = new Object [2]; + params [0] = param.toString (); + params [1] = next; + + msg = "one-param filter"; + } + + + try { + constructor = klass.getConstructor (argTypes); + } catch (NoSuchMethodException e) { + // try creating a filter from a + // terminus and a tee + filter = true; + msg += " built from "; + } + } + + // build from a terminus stage, with or + // without the optional string param + if (constructor == null) { + String tmp; + + if (param == null) { + argTypes = new Class [0]; + params = new Object [0]; + + tmp = "no-param terminus"; + } else { + argTypes = new Class [1]; + argTypes [0] = String.class; + + params = new Object [1]; + params [0] = param.toString (); + + tmp = "one-param terminus"; + } + if (msg == null) + msg = tmp; + else + msg += tmp; + constructor = klass.getConstructor (argTypes); + // NOT creating terminus by dead-ending + // filters ... users should think about + // that one, something's likely wrong + } + + obj = constructor.newInstance (params); + + // return EventConsumers directly, perhaps after + // turning them into a filter + if (obj instanceof EventConsumer) { + if (filter) + return new TeeConsumer ((EventConsumer) obj, next); + return (EventConsumer) obj; + } + + // if it's not a handler, it's an error + // we can wrap handlers in a filter + EventFilter retval = new EventFilter (); + boolean updated = false; + + if (obj instanceof ContentHandler) { + retval.setContentHandler ((ContentHandler) obj); + updated = true; + } + if (obj instanceof DTDHandler) { + retval.setDTDHandler ((DTDHandler) obj); + updated = true; + } + if (obj instanceof LexicalHandler) { + retval.setProperty ( + EventFilter.PROPERTY_URI + "lexical-handler", + obj); + updated = true; + } + if (obj instanceof DeclHandler) { + retval.setProperty ( + EventFilter.PROPERTY_URI + "declaration-handler", + obj); + updated = true; + } + + if (!updated) + fail ("class is neither Consumer nor Handler"); + + if (filter) + return new TeeConsumer (retval, next); + return retval; + + } catch (IOException e) { + throw e; + + } catch (NoSuchMethodException e) { + fail (name + " constructor missing -- " + msg); + + } catch (ClassNotFoundException e) { + fail (name + " class not found"); + + } catch (Exception e) { + // e.printStackTrace (); + fail ("stage not available: " + e.getMessage ()); + } + } + // NOTREACHED + return null; + } + } + + private static class Pipeline + { + Stage stage; + + // rest may be null + Pipeline rest; + EventConsumer next; + + Pipeline (Stage s) + { stage = s; } + + public String toString () + { + if (rest == null && next == null) + return stage.toString (); + if (rest != null) + return stage + " | " + rest; + throw new IllegalArgumentException ("next"); + } + + EventConsumer createPipeline () + throws IOException + { + if (next == null) { + if (rest == null) + next = stage.createStage (null); + else + next = stage.createStage (rest.createPipeline ()); + } + return next; + } + } + +/* + public static void main (String argv []) + { + try { + // three basic terminus cases + createPipeline ("null"); + createPipeline ("validate"); + createPipeline ("write ( stdout )"); + + // four basic filters + createPipeline ("nsfix | write ( stderr )"); + createPipeline ("wf | null"); + createPipeline ("null | null"); + createPipeline ( +"call ( http://www.example.com/services/xml-1a ) | xhtml ( stdout )"); + + // tee junctions + createPipeline ("tee ( validate ) | write ( stdout )"); + createPipeline ("tee ( nsfix | write ( stdout ) ) | validate"); + + // longer pipeline + createPipeline ("nsfix | tee ( validate ) | write ( stdout )"); + createPipeline ( + "null | wf | nsfix | tee ( validate ) | write ( stdout )"); + + // try some parsing error cases + try { + createPipeline ("null ("); // extra token '(' + System.err.println ("** didn't report error"); + } catch (Exception e) { + System.err.println ("== err: " + e.getMessage ()); } + + try { + createPipeline ("nsfix |"); // extra token '|' + System.err.println ("** didn't report error"); + } catch (Exception e) { + System.err.println ("== err: " + e.getMessage ()); } + + try { + createPipeline ("xhtml ( foo"); // missing right paren + System.err.println ("** didn't report error"); + } catch (Exception e) { + System.err.println ("== err: " + e.getMessage ()); } + + try { + createPipeline ("xhtml ( foo bar"); // required right paren + System.err.println ("** didn't report error"); + } catch (Exception e) { + System.err.println ("== err: " + e.getMessage ()); } + + try { + createPipeline ("tee ( nsfix | validate");// missing right paren + System.err.println ("** didn't report error"); + } catch (Exception e) { + System.err.println ("== err: " + e.getMessage ()); } + + // try some construction error cases + + try { + createPipeline ("call"); // missing param + System.err.println ("** didn't report error"); + } catch (Exception e) { + System.err.println ("== err: " + e.getMessage ()); } + try { + createPipeline ("call ( foobar )"); // broken param + System.err.println ("** didn't report error"); + } catch (Exception e) { + System.err.println ("== err: " + e.getMessage ()); } + try { + createPipeline ("nsfix ( foobar )"); // illegal param + System.err.println ("** didn't report error"); + } catch (Exception e) { + System.err.println ("== err: " + e.getMessage ()); } + try { + createPipeline ("null ( foobar )"); // illegal param + System.err.println ("** didn't report error"); + } catch (Exception e) { + System.err.println ("== err: " + e.getMessage ()); } + try { + createPipeline ("wf ( foobar )"); // illegal param + System.err.println ("** didn't report error"); + } catch (Exception e) { + System.err.println ("== err: " + e.getMessage ()); } + try { + createPipeline ("xhtml ( foobar.html )"); + new File ("foobar.html").delete (); + // now supported + } catch (Exception e) { + System.err.println ("** err: " + e.getMessage ()); } + try { + createPipeline ("xhtml"); // missing param + System.err.println ("** didn't report error"); + } catch (Exception e) { + System.err.println ("== err: " + e.getMessage ()); } + try { + createPipeline ("write ( stdout ) | null"); // nonterminal + System.err.println ("** didn't report error"); + } catch (Exception e) { + System.err.println ("== err: " + e.getMessage ()); } + try { + createPipeline ("validate | null"); + // now supported + } catch (Exception e) { + System.err.println ("** err: " + e.getMessage ()); } + try { + createPipeline ("validate ( foo )"); // illegal param + System.err.println ("** didn't report error"); + } catch (Exception e) { + System.err.println ("== err: " + e.getMessage ()); } + try { + createPipeline ("tee"); // missing param + System.err.println ("** didn't report error"); + } catch (Exception e) { + System.err.println ("== err: " + e.getMessage ()); } + try { + // only builtins so far + createPipeline ("com.example.xml.FilterClass"); + System.err.println ("** didn't report error"); + } catch (Exception e) { + System.err.println ("== err: " + e.getMessage ()); } + + } catch (Exception e) { + e.printStackTrace (); + } + } +/**/ + +} diff --git a/libjava/classpath/gnu/xml/pipeline/TeeConsumer.java b/libjava/classpath/gnu/xml/pipeline/TeeConsumer.java new file mode 100644 index 000000000..3ac860575 --- /dev/null +++ b/libjava/classpath/gnu/xml/pipeline/TeeConsumer.java @@ -0,0 +1,417 @@ +/* TeeConsumer.java -- + Copyright (C) 1999,2000,2001 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.pipeline; + +import org.xml.sax.Attributes; +import org.xml.sax.ContentHandler; +import org.xml.sax.DTDHandler; +import org.xml.sax.ErrorHandler; +import org.xml.sax.Locator; +import org.xml.sax.SAXException; +import org.xml.sax.SAXNotRecognizedException; +import org.xml.sax.ext.DeclHandler; +import org.xml.sax.ext.LexicalHandler; + +/** + * Fans its events out to two other consumers, a "tee" filter stage in an + * event pipeline. Networks can be assembled with multiple output points. + * + *
Error handling should be simple if you remember that exceptions + * you throw will cancel later stages in that callback's pipeline, and + * generally the producer will stop if it sees such an exception. You + * may want to protect your pipeline against such backflows, making a + * kind of reverse filter (or valve?) so that certain exceptions thrown by + * your pipeline will caught and handled before the producer sees them. + * Just use a "try/catch" block, rememebering that really important + * cleanup tasks should be in "finally" clauses. + * + *
That issue isn't unique to "tee" consumers, but tee consumers have + * the additional twist that exceptions thrown by the first consumer + * will cause the second consumer not to see the callback (except for + * the endDocument callback, which signals state cleanup). + * + * @author David Brownell + */ +final public class TeeConsumer + implements EventConsumer, + ContentHandler, DTDHandler, + LexicalHandler,DeclHandler +{ + private EventConsumer first, rest; + + // cached to minimize time overhead + private ContentHandler docFirst, docRest; + private DeclHandler declFirst, declRest; + private LexicalHandler lexFirst, lexRest; + + + /** + * Constructs a consumer which sends all its events to the first + * consumer, and then the second one. If the first consumer throws + * an exception, the second one will not see the event which + * caused that exception to be reported. + * + * @param car The first consumer to get the events + * @param cdr The second consumer to get the events + */ + public TeeConsumer (EventConsumer car, EventConsumer cdr) + { + if (car == null || cdr == null) + throw new NullPointerException (); + first = car; + rest = cdr; + + // + // Cache the handlers. + // + docFirst = first.getContentHandler (); + docRest = rest.getContentHandler (); + // DTD handler isn't cached (rarely needed) + + try { + declFirst = null; + declFirst = (DeclHandler) first.getProperty ( + EventFilter.DECL_HANDLER); + } catch (SAXException e) {} + try { + declRest = null; + declRest = (DeclHandler) rest.getProperty ( + EventFilter.DECL_HANDLER); + } catch (SAXException e) {} + + try { + lexFirst = null; + lexFirst = (LexicalHandler) first.getProperty ( + EventFilter.LEXICAL_HANDLER); + } catch (SAXException e) {} + try { + lexRest = null; + lexRest = (LexicalHandler) rest.getProperty ( + EventFilter.LEXICAL_HANDLER); + } catch (SAXException e) {} + } + +/* FIXME + /** + * Constructs a pipeline, and is otherwise a shorthand for the + * two-consumer constructor for this class. + * + * @param first Description of the first pipeline to get events, + * which will be passed to {@link PipelineFactory#createPipeline} + * @param rest The second pipeline to get the events + * / + // constructor used by PipelineFactory + public TeeConsumer (String first, EventConsumer rest) + throws IOException + { + this (PipelineFactory.createPipeline (first), rest); + } +*/ + + /** Returns the first pipeline to get event calls. */ + public EventConsumer getFirst () + { return first; } + + /** Returns the second pipeline to get event calls. */ + public EventConsumer getRest () + { return rest; } + + /** Returns the content handler being used. */ + final public ContentHandler getContentHandler () + { + if (docRest == null) + return docFirst; + if (docFirst == null) + return docRest; + return this; + } + + /** Returns the dtd handler being used. */ + final public DTDHandler getDTDHandler () + { + // not cached (hardly used) + if (rest.getDTDHandler () == null) + return first.getDTDHandler (); + if (first.getDTDHandler () == null) + return rest.getDTDHandler (); + return this; + } + + /** Returns the declaration or lexical handler being used. */ + final public Object getProperty (String id) + throws SAXNotRecognizedException + { + // + // in degenerate cases, we have no work to do. + // + Object firstProp = null, restProp = null; + + try { firstProp = first.getProperty (id); } + catch (SAXNotRecognizedException e) { /* ignore */ } + try { restProp = rest.getProperty (id); } + catch (SAXNotRecognizedException e) { /* ignore */ } + + if (restProp == null) + return firstProp; + if (firstProp == null) + return restProp; + + // + // we've got work to do; handle two builtin cases. + // + if (EventFilter.DECL_HANDLER.equals (id)) + return this; + if (EventFilter.LEXICAL_HANDLER.equals (id)) + return this; + + // + // non-degenerate, handled by both consumers, but we don't know + // how to handle this. + // + throw new SAXNotRecognizedException ("can't tee: " + id); + } + + /** + * Provides the error handler to both subsequent nodes of + * this filter stage. + */ + public void setErrorHandler (ErrorHandler handler) + { + first.setErrorHandler (handler); + rest.setErrorHandler (handler); + } + + + // + // ContentHandler + // + public void setDocumentLocator (Locator locator) + { + // this call is not made by all parsers + docFirst.setDocumentLocator (locator); + docRest.setDocumentLocator (locator); + } + + public void startDocument () + throws SAXException + { + docFirst.startDocument (); + docRest.startDocument (); + } + + public void endDocument () + throws SAXException + { + try { + docFirst.endDocument (); + } finally { + docRest.endDocument (); + } + } + + public void startPrefixMapping (String prefix, String uri) + throws SAXException + { + docFirst.startPrefixMapping (prefix, uri); + docRest.startPrefixMapping (prefix, uri); + } + + public void endPrefixMapping (String prefix) + throws SAXException + { + docFirst.endPrefixMapping (prefix); + docRest.endPrefixMapping (prefix); + } + + public void skippedEntity (String name) + throws SAXException + { + docFirst.skippedEntity (name); + docRest.skippedEntity (name); + } + + public void startElement (String uri, String localName, + String qName, Attributes atts) + throws SAXException + { + docFirst.startElement (uri, localName, qName, atts); + docRest.startElement (uri, localName, qName, atts); + } + + public void endElement (String uri, String localName, String qName) + throws SAXException + { + docFirst.endElement (uri, localName, qName); + docRest.endElement (uri, localName, qName); + } + + public void processingInstruction (String target, String data) + throws SAXException + { + docFirst.processingInstruction (target, data); + docRest.processingInstruction (target, data); + } + + public void characters (char ch [], int start, int length) + throws SAXException + { + docFirst.characters (ch, start, length); + docRest.characters (ch, start, length); + } + + public void ignorableWhitespace (char ch [], int start, int length) + throws SAXException + { + docFirst.ignorableWhitespace (ch, start, length); + docRest.ignorableWhitespace (ch, start, length); + } + + + // + // DTDHandler + // + public void notationDecl (String name, String publicId, String systemId) + throws SAXException + { + DTDHandler l1 = first.getDTDHandler (); + DTDHandler l2 = rest.getDTDHandler (); + + l1.notationDecl (name, publicId, systemId); + l2.notationDecl (name, publicId, systemId); + } + + public void unparsedEntityDecl (String name, + String publicId, String systemId, + String notationName + ) throws SAXException + { + DTDHandler l1 = first.getDTDHandler (); + DTDHandler l2 = rest.getDTDHandler (); + + l1.unparsedEntityDecl (name, publicId, systemId, notationName); + l2.unparsedEntityDecl (name, publicId, systemId, notationName); + } + + + // + // DeclHandler + // + public void attributeDecl (String eName, String aName, + String type, + String mode, String value) + throws SAXException + { + declFirst.attributeDecl (eName, aName, type, mode, value); + declRest.attributeDecl (eName, aName, type, mode, value); + } + + public void elementDecl (String name, String model) + throws SAXException + { + declFirst.elementDecl (name, model); + declRest.elementDecl (name, model); + } + + public void externalEntityDecl (String name, + String publicId, String systemId) + throws SAXException + { + declFirst.externalEntityDecl (name, publicId, systemId); + declRest.externalEntityDecl (name, publicId, systemId); + } + + public void internalEntityDecl (String name, String value) + throws SAXException + { + declFirst.internalEntityDecl (name, value); + declRest.internalEntityDecl (name, value); + } + + + // + // LexicalHandler + // + public void comment (char ch [], int start, int length) + throws SAXException + { + lexFirst.comment (ch, start, length); + lexRest.comment (ch, start, length); + } + + public void startCDATA () + throws SAXException + { + lexFirst.startCDATA (); + lexRest.startCDATA (); + } + + public void endCDATA () + throws SAXException + { + lexFirst.endCDATA (); + lexRest.endCDATA (); + } + + public void startEntity (String name) + throws SAXException + { + lexFirst.startEntity (name); + lexRest.startEntity (name); + } + + public void endEntity (String name) + throws SAXException + { + lexFirst.endEntity (name); + lexRest.endEntity (name); + } + + public void startDTD (String name, String publicId, String systemId) + throws SAXException + { + lexFirst.startDTD (name, publicId, systemId); + lexRest.startDTD (name, publicId, systemId); + } + + public void endDTD () + throws SAXException + { + lexFirst.endDTD (); + lexRest.endDTD (); + } +} diff --git a/libjava/classpath/gnu/xml/pipeline/TextConsumer.java b/libjava/classpath/gnu/xml/pipeline/TextConsumer.java new file mode 100644 index 000000000..13dcfa7f6 --- /dev/null +++ b/libjava/classpath/gnu/xml/pipeline/TextConsumer.java @@ -0,0 +1,117 @@ +/* TextConsumer.java -- + Copyright (C) 1999,2000,2001 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.pipeline; + +import java.io.*; + +import org.xml.sax.*; + +import gnu.xml.util.XMLWriter; + + +/** + * Terminates a pipeline, consuming events to print them as well formed + * XML (or XHTML) text. + * + *
Input must be well formed, and must include XML names (e.g. the + * prefixes and prefix declarations must be present), or the output of + * this class is undefined. + * + * @see NSFilter + * @see WellFormednessFilter + * + * @author David Brownell + */ +public class TextConsumer extends XMLWriter implements EventConsumer +{ + /** + * Constructs an event consumer which echoes its input as text, + * optionally adhering to some basic XHTML formatting options + * which increase interoperability with old (v3) browsers. + * + *
For the best interoperability, when writing as XHTML only + * ASCII characters are emitted; other characters are turned to + * entity or character references as needed, and no XML declaration + * is provided in the document. + */ + public TextConsumer (Writer w, boolean isXhtml) + throws IOException + { + super (w, isXhtml ? "US-ASCII" : null); + setXhtml (isXhtml); + } + + /** + * Constructs a consumer that writes its input as XML text. + * XHTML rules are not followed. + */ + public TextConsumer (Writer w) + throws IOException + { + this (w, false); + } + + /** + * Constructs a consumer that writes its input as XML text, + * encoded in UTF-8. XHTML rules are not followed. + */ + public TextConsumer (OutputStream out) + throws IOException + { + this (new OutputStreamWriter (out, "UTF8"), false); + } + + /** EventConsumer Returns the document handler being used. */ + public ContentHandler getContentHandler () + { return this; } + + /** EventConsumer Returns the dtd handler being used. */ + public DTDHandler getDTDHandler () + { return this; } + + /** XMLReaderRetrieves a property (lexical and decl handlers) */ + public Object getProperty (String propertyId) + throws SAXNotRecognizedException + { + if (EventFilter.LEXICAL_HANDLER.equals (propertyId)) + return this; + if (EventFilter.DECL_HANDLER.equals (propertyId)) + return this; + throw new SAXNotRecognizedException (propertyId); + } +} diff --git a/libjava/classpath/gnu/xml/pipeline/ValidationConsumer.java b/libjava/classpath/gnu/xml/pipeline/ValidationConsumer.java new file mode 100644 index 000000000..0346984d3 --- /dev/null +++ b/libjava/classpath/gnu/xml/pipeline/ValidationConsumer.java @@ -0,0 +1,1928 @@ +/* ValidationConsumer.java -- + Copyright (C) 1999,2000,2001 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.pipeline; + +import java.io.IOException; +import java.io.StringReader; +import java.io.StringWriter; +import java.util.EmptyStackException; +import java.util.Enumeration; +import java.util.Hashtable; +import java.util.Stack; +import java.util.StringTokenizer; +import java.util.Vector; + +import org.xml.sax.Attributes; +import org.xml.sax.EntityResolver; +import org.xml.sax.ErrorHandler; +import org.xml.sax.InputSource; +import org.xml.sax.Locator; +import org.xml.sax.SAXException; +import org.xml.sax.SAXParseException; +import org.xml.sax.XMLReader; +import org.xml.sax.helpers.XMLReaderFactory; + +/** + * This class checks SAX2 events to report validity errors; it works as + * both a filter and a terminus on an event pipeline. It relies on the + * producer of SAX events to:
xmlns*
+ * attributes (rather than omitting either or both). At this writing, the major SAX2 parsers (such as Ælfred2, + * Crimson, and Xerces) meet these requirements, and this validation + * module is used by the optional Ælfred2 validation support. + *
+ * + *Note that because this is a layered validator, it has to duplicate some + * work that the parser is doing; there are also other cost to layering. + * However, because of layering it doesn't need a parser in order + * to work! You can use it with anything that generates SAX events, such + * as an application component that wants to detect invalid content in + * a changed area without validating an entire document, or which wants to + * ensure that it doesn't write invalid data to a communications partner.
+ * + *Also, note that because this is a layered validator, the line numbers + * reported for some errors may seem strange. For example, if an element does + * not permit character content, the validator + * will use the locator provided to it. + * That might reflect the last character of a characters event + * callback, rather than the first non-whitespace character.
+ * + *Current limitations of the validation performed are in roughly three + * categories.
+ * + *The first category represents constraints which demand violations + * of software layering: exposing lexical details, one of the first things + * that application programming interfaces (APIs) hide. These + * invariably relate to XML entity handling, and to historical oddities + * of the XML validation semantics. Curiously, + * recent (Autumn 1999) conformance testing showed that these constraints are + * among those handled worst by existing XML validating parsers. Arguments + * have been made that each of these VCs should be turned into WFCs (most + * of them) or discarded (popular for the standalone declaration); in short, + * that these are bugs in the XML specification (not all via SGML):
The second category of limitations on this validation represent + * constraints associated with information that is not guaranteed to be + * available (or in one case, is guaranteed not to be available, + * through the SAX2 API:
A third category relates to ease of implementation. (Think of this + * as "bugs".) The most notable issue here is character handling. Rather + * than attempting to implement the voluminous character tables in the XML + * specification (Appendix B), Unicode rules are used directly from + * the java.lang.Character class. Recent JVMs have begun to diverge from + * the original specification for that class (Unicode 2.0), meaning that + * different JVMs may handle that aspect of conformance differently. + *
+ * + *Note that for some of the validity errors that SAX2 does not + * expose, a nonvalidating parser is permitted (by the XML specification) + * to report validity errors. When used with a parser that does so for + * the validity constraints mentioned above (or any other SAX2 event + * stream producer that does the same thing), overall conformance is + * substantially improved. + * + * @see gnu.xml.aelfred2.SAXDriver + * @see gnu.xml.aelfred2.XmlReader + * + * @author David Brownell + */ +public final class ValidationConsumer extends EventFilter +{ + // report error if we happen to notice a non-deterministic choice? + // we won't report buggy content models; just buggy instances + private static final boolean warnNonDeterministic = false; + + // for tracking active content models + private String rootName; + private Stack contentStack = new Stack (); + + // flags for "saved DTD" processing + private boolean disableDeclarations; + private boolean disableReset; + + // + // most VCs get tested when we see element start tags. the per-element + // info (including attributes) recorded here duplicates that found inside + // many nonvalidating parsers, hence dual lookups etc ... that's why a + // layered validator isn't going to be as fast as a non-layered one. + // + + // key = element name; value = ElementInfo + private Hashtable elements = new Hashtable (); + + // some VCs relate to ID/IDREF/IDREFS attributes + // key = id; value = boolean true (defd) or false (refd) + private Hashtable ids = new Hashtable (); + + // we just record declared notation and unparsed entity names. + // the implementation here is simple/slow; these features + // are seldom used, one hopes they'll wither away soon + private Vector notations = new Vector (5, 5); + private Vector nDeferred = new Vector (5, 5); + private Vector unparsed = new Vector (5, 5); + private Vector uDeferred = new Vector (5, 5); + + // note: DocBk 3.1.7 XML defines over 2 dozen notations, + // used when defining unparsed entities for graphics + // (and maybe in other places) + + + + /** + * Creates a pipeline terminus which consumes all events passed to + * it; this will report validity errors as if they were fatal errors, + * unless an error handler is assigned. + * + * @see #setErrorHandler + */ + // constructor used by PipelineFactory + // ... and want one taking system ID of an external subset + public ValidationConsumer () + { + this (null); + } + + /** + * Creates a pipeline filter which reports validity errors and then + * passes events on to the next consumer if they were not fatal. + * + * @see #setErrorHandler + */ + // constructor used by PipelineFactory + // ... and want one taking system ID of an external subset + // (which won't send declaration events) + public ValidationConsumer (EventConsumer next) + { + super (next); + + setContentHandler (this); + setDTDHandler (this); + try { setProperty (DECL_HANDLER, this); } + catch (Exception e) { /* "can't happen" */ } + try { setProperty (LEXICAL_HANDLER, this); } + catch (Exception e) { /* "can't happen" */ } + } + + + private static final String fakeRootName + = ":Nobody:in:their_Right.Mind_would:use:this-name:1x:"; + + /** + * Creates a validation consumer which is preloaded with the DTD provided. + * It does this by constructing a document with that DTD, then parsing + * that document and recording its DTD declarations. Then it arranges + * not to modify that information. + * + *
The resulting validation consumer will only validate against + * the specified DTD, regardless of whether some other DTD is found + * in a document being parsed. + * + * @param rootName The name of the required root element; if this is + * null, any root element name will be accepted. + * @param publicId If non-null and there is a non-null systemId, this + * identifier provides an alternate access identifier for the DTD's + * external subset. + * @param systemId If non-null, this is a URI (normally URL) that + * may be used to access the DTD's external subset. + * @param internalSubset If non-null, holds literal markup declarations + * comprising the DTD's internal subset. + * @param resolver If non-null, this will be provided to the parser for + * use when resolving parameter entities (including any external subset). + * @param resolver If non-null, this will be provided to the parser for + * use when resolving parameter entities (including any external subset). + * @param minimalElement If non-null, a minimal valid document. + * + * @exception SAXNotSupportedException If the default SAX parser does + * not support the standard lexical or declaration handlers. + * @exception SAXParseException If the specified DTD has either + * well-formedness or validity errors + * @exception IOException If the specified DTD can't be read for + * some reason + */ + public ValidationConsumer ( + String rootName, + String publicId, + String systemId, + String internalSubset, + EntityResolver resolver, + String minimalDocument + ) throws SAXException, IOException + { + this (null); + + disableReset = true; + if (rootName == null) + rootName = fakeRootName; + + // + // Synthesize document with that DTD; is it possible to do + // better for the declaration of the root element? + // + // NOTE: can't use SAX2 to write internal subsets. + // + StringWriter writer = new StringWriter (); + + writer.write (""); + } + if (internalSubset != null) + writer.write (internalSubset); + writer.write ("\n ]>"); + + if (minimalDocument != null) { + writer.write ("\n"); + writer.write (minimalDocument); + writer.write ("\n"); + } else { + writer.write (" <"); + writer.write (rootName); + writer.write ("/>\n"); + } + minimalDocument = writer.toString (); + + // + // OK, load it + // + XMLReader producer; + + producer = XMLReaderFactory.createXMLReader (); + bind (producer, this); + + if (resolver != null) + producer.setEntityResolver (resolver); + + InputSource in; + + in = new InputSource (new StringReader (minimalDocument)); + producer.parse (in); + + disableDeclarations = true; + if (rootName == fakeRootName) + this.rootName = null; + } + + private void resetState () + { + if (!disableReset) { + rootName = null; + contentStack.removeAllElements (); + elements.clear (); + ids.clear (); + + notations.removeAllElements (); + nDeferred.removeAllElements (); + unparsed.removeAllElements (); + uDeferred.removeAllElements (); + } + } + + + private void warning (String description) + throws SAXException + { + ErrorHandler errHandler = getErrorHandler (); + Locator locator = getDocumentLocator (); + SAXParseException err; + + if (errHandler == null) + return; + + if (locator == null) + err = new SAXParseException (description, null, null, -1, -1); + else + err = new SAXParseException (description, locator); + errHandler.warning (err); + } + + // package private (for ChildrenRecognizer) + private void error (String description) + throws SAXException + { + ErrorHandler errHandler = getErrorHandler (); + Locator locator = getDocumentLocator (); + SAXParseException err; + + if (locator == null) + err = new SAXParseException (description, null, null, -1, -1); + else + err = new SAXParseException (description, locator); + if (errHandler != null) + errHandler.error (err); + else // else we always treat it as fatal! + throw err; + } + + private void fatalError (String description) + throws SAXException + { + ErrorHandler errHandler = getErrorHandler (); + Locator locator = getDocumentLocator (); + SAXParseException err; + + if (locator != null) + err = new SAXParseException (description, locator); + else + err = new SAXParseException (description, null, null, -1, -1); + if (errHandler != null) + errHandler.fatalError (err); + // we always treat this as fatal, regardless of the handler + throw err; + } + + + private static boolean isExtender (char c) + { + // [88] Extender ::= ... + return c == 0x00b7 || c == 0x02d0 || c == 0x02d1 || c == 0x0387 + || c == 0x0640 || c == 0x0e46 || c == 0x0ec6 || c == 0x3005 + || (c >= 0x3031 && c <= 0x3035) + || (c >= 0x309d && c <= 0x309e) + || (c >= 0x30fc && c <= 0x30fe); + } + + + // use augmented Unicode rules, not full XML rules + private boolean isName (String name, String context, String id) + throws SAXException + { + char buf [] = name.toCharArray (); + boolean pass = true; + + if (!Character.isUnicodeIdentifierStart (buf [0]) + && ":_".indexOf (buf [0]) == -1) + pass = false; + else { + int max = buf.length; + for (int i = 1; pass && i < max; i++) { + char c = buf [i]; + if (!Character.isUnicodeIdentifierPart (c) + && ":-_.".indexOf (c) == -1 + && !isExtender (c)) + pass = false; + } + } + + if (!pass) + error ("In " + context + " for " + id + + ", '" + name + "' is not a name"); + return pass; // true == OK + } + + // use augmented Unicode rules, not full XML rules + private boolean isNmtoken (String nmtoken, String context, String id) + throws SAXException + { + char buf [] = nmtoken.toCharArray (); + boolean pass = true; + int max = buf.length; + + // XXX make this share code with isName + + for (int i = 0; pass && i < max; i++) { + char c = buf [i]; + if (!Character.isUnicodeIdentifierPart (c) + && ":-_.".indexOf (c) == -1 + && !isExtender (c)) + pass = false; + } + + if (!pass) + error ("In " + context + " for " + id + + ", '" + nmtoken + "' is not a name token"); + return pass; // true == OK + } + + private void checkEnumeration (String value, String type, String name) + throws SAXException + { + if (!hasMatch (value, type)) + // VC: Enumeration + error ("Value '" + value + + "' for attribute '" + name + + "' is not permitted: " + type); + } + + // used to test enumerated attributes and mixed content models + // package private + static boolean hasMatch (String value, String orList) + { + int len = value.length (); + int max = orList.length () - len; + + for (int start = 0; + (start = orList.indexOf (value, start)) != -1; + start++) { + char c; + + if (start > max) + break; + c = orList.charAt (start - 1); + if (c != '|' && c != '('/*)*/) + continue; + c = orList.charAt (start + len); + if (c != '|' && /*(*/ c != ')') + continue; + return true; + } + return false; + } + + /** + * LexicalHandler Records the declaration of the root + * element, so it can be verified later. + * Passed to the next consumer, unless this one was + * preloaded with a particular DTD. + */ + public void startDTD (String name, String publicId, String systemId) + throws SAXException + { + if (disableDeclarations) + return; + + rootName = name; + super.startDTD (name, publicId, systemId); + } + + /** + * LexicalHandler Verifies that all referenced notations + * and unparsed entities have been declared. + * Passed to the next consumer, unless this one was + * preloaded with a particular DTD. + */ + public void endDTD () + throws SAXException + { + if (disableDeclarations) + return; + + // this is a convenient hook for end-of-dtd checks, but we + // could also trigger it in the first startElement call. + // locator info is more appropriate here though. + + // VC: Notation Declared (NDATA can refer to them before decls, + // as can NOTATION attribute enumerations and defaults) + int length = nDeferred.size (); + for (int i = 0; i < length; i++) { + String notation = (String) nDeferred.elementAt (i); + if (!notations.contains (notation)) { + error ("A declaration referred to notation '" + notation + + "' which was never declared"); + } + } + nDeferred.removeAllElements (); + + // VC: Entity Name (attribute values can refer to them + // before they're declared); VC Attribute Default Legal + length = uDeferred.size (); + for (int i = 0; i < length; i++) { + String entity = (String) uDeferred.elementAt (i); + if (!unparsed.contains (entity)) { + error ("An attribute default referred to entity '" + entity + + "' which was never declared"); + } + } + uDeferred.removeAllElements (); + super.endDTD (); + } + + + // These are interned, so we can rely on "==" to find the type of + // all attributes except enumerations ... + // "(this|or|that|...)" and "NOTATION (this|or|that|...)" + static final String types [] = { + "CDATA", + "ID", "IDREF", "IDREFS", + "NMTOKEN", "NMTOKENS", + "ENTITY", "ENTITIES" + }; + + + /** + * DecllHandler Records attribute declaration for later use + * in validating document content, and checks validity constraints + * that are applicable to attribute declarations. + * Passed to the next consumer, unless this one was + * preloaded with a particular DTD. + */ + public void attributeDecl ( + String eName, + String aName, + String type, + String mode, + String value + ) throws SAXException + { + if (disableDeclarations) + return; + + ElementInfo info = (ElementInfo) elements.get (eName); + AttributeInfo ainfo = new AttributeInfo (); + boolean checkOne = false; + boolean interned = false; + + // cheap interning of type names and #FIXED, #REQUIRED + // for faster startElement (we can use "==") + for (int i = 0; i < types.length; i++) { + if (types [i].equals (type)) { + type = types [i]; + interned = true; + break; + } + } + if ("#FIXED".equals (mode)) + mode = "#FIXED"; + else if ("#REQUIRED".equals (mode)) + mode = "#REQUIRED"; + + ainfo.type = type; + ainfo.mode = mode; + ainfo.value = value; + + // we might not have seen the content model yet + if (info == null) { + info = new ElementInfo (eName); + elements.put (eName, info); + } + if ("ID" == type) { + checkOne = true; + if (!("#REQUIRED" == mode || "#IMPLIED".equals (mode))) { + // VC: ID Attribute Default + error ("ID attribute '" + aName + + "' must be #IMPLIED or #REQUIRED"); + } + + } else if (!interned && type.startsWith ("NOTATION ")) { + checkOne = true; + + // VC: Notation Attributes (notations must be declared) + StringTokenizer tokens = new StringTokenizer ( + type.substring (10, type.lastIndexOf (')')), + "|"); + while (tokens.hasMoreTokens ()) { + String token = tokens.nextToken (); + if (!notations.contains (token)) + nDeferred.addElement (token); + } + } + if (checkOne) { + for (Enumeration e = info.attributes.keys (); + e.hasMoreElements (); + /* NOP */) { + String name; + AttributeInfo ainfo2; + + name = (String) e.nextElement (); + ainfo2 = (AttributeInfo) info.attributes.get (name); + if (type == ainfo2.type || !interned /* NOTATION */) { + // VC: One ID per Element Type + // VC: One Notation per Element TYpe + error ("Element '" + eName + + "' already has an attribute of type " + + (interned ? "NOTATION" : type) + + " ('" + name + + "') so '" + aName + + "' is a validity error"); + } + } + } + + // VC: Attribute Default Legal + if (value != null) { + + if ("CDATA" == type) { + // event source rejected '<' + + } else if ("NMTOKEN" == type) { + // VC: Name Token (is a nmtoken) + isNmtoken (value, "attribute default", aName); + + } else if ("NMTOKENS" == type) { + // VC: Name Token (is a nmtoken; at least one value) + StringTokenizer tokens = new StringTokenizer (value); + if (!tokens.hasMoreTokens ()) + error ("Default for attribute '" + aName + + "' must have at least one name token."); + else do { + String token = tokens.nextToken (); + isNmtoken (token, "attribute default", aName); + } while (tokens.hasMoreTokens ()); + + } else if ("IDREF" == type || "ENTITY" == type) { + // VC: Entity Name (is a name) + // VC: IDREF (is a name) (is declared) + isName (value, "attribute default", aName); + if ("ENTITY" == type && !unparsed.contains (value)) + uDeferred.addElement (value); + + } else if ("IDREFS" == type || "ENTITIES" == type) { + // VC: Entity Name (is a name; at least one value) + // VC: IDREF (is a name; at least one value) + StringTokenizer names = new StringTokenizer (value); + if (!names.hasMoreTokens ()) + error ("Default for attribute '" + aName + + "' must have at least one name."); + else do { + String name = names.nextToken (); + isName (name, "attribute default", aName); + if ("ENTITIES" == type && !unparsed.contains (name)) + uDeferred.addElement (value); + } while (names.hasMoreTokens ()); + + } else if (type.charAt (0) == '(' /*)*/ ) { + // VC: Enumeration (must match) + checkEnumeration (value, type, aName); + + } else if (!interned && checkOne) { /* NOTATION */ + // VC: Notation attributes (must be names) + isName (value, "attribute default", aName); + + // VC: Notation attributes (must be declared) + if (!notations.contains (value)) + nDeferred.addElement (value); + + // VC: Enumeration (must match) + checkEnumeration (value, type, aName); + + } else if ("ID" != type) + throw new RuntimeException ("illegal attribute type: " + type); + } + + if (info.attributes.get (aName) == null) + info.attributes.put (aName, ainfo); + /* + else + warning ("Element '" + eName + + "' already has an attribute named '" + aName + "'"); + */ + + if ("xml:space".equals (aName)) { + if (!("(default|preserve)".equals (type) + || "(preserve|default)".equals (type) + // these next two are arguable; XHTML's DTD doesn't + // deserve errors. After all, it's not like any + // illegal _value_ could pass ... + || "(preserve)".equals (type) + || "(default)".equals (type) + )) + error ( + "xml:space attribute type must be like '(default|preserve)'" + + " not '" + type + "'" + ); + + } + super.attributeDecl (eName, aName, type, mode, value); + } + + /** + * DecllHandler Records the element declaration for later use + * when checking document content, and checks validity constraints that + * apply to element declarations. Passed to the next consumer, unless + * this one was preloaded with a particular DTD. + */ + public void elementDecl (String name, String model) + throws SAXException + { + if (disableDeclarations) + return; + + ElementInfo info = (ElementInfo) elements.get (name); + + // we might have seen an attribute decl already + if (info == null) { + info = new ElementInfo (name); + elements.put (name, info); + } + if (info.model != null) { + // NOTE: not all parsers can report such duplicates. + // VC: Unique Element Type Declaration + error ("Element type '" + name + + "' was already declared."); + } else { + info.model = model; + + // VC: No Duplicate Types (in mixed content models) + if (model.charAt (1) == '#') // (#PCDATA... + info.getRecognizer (this); + } + super.elementDecl (name, model); + } + + /** + * DecllHandler passed to the next consumer, unless this + * one was preloaded with a particular DTD + */ + public void internalEntityDecl (String name, String value) + throws SAXException + { + if (!disableDeclarations) + super.internalEntityDecl (name, value); + } + + /** + * DecllHandler passed to the next consumer, unless this + * one was preloaded with a particular DTD + */ + public void externalEntityDecl (String name, + String publicId, String systemId) + throws SAXException + { + if (!disableDeclarations) + super.externalEntityDecl (name, publicId, systemId); + } + + + /** + * DTDHandler Records the notation name, for checking + * NOTATIONS attribute values and declararations of unparsed + * entities. Passed to the next consumer, unless this one was + * preloaded with a particular DTD. + */ + public void notationDecl (String name, String publicId, String systemId) + throws SAXException + { + if (disableDeclarations) + return; + + notations.addElement (name); + super.notationDecl (name, publicId, systemId); + } + + /** + * DTDHandler Records the entity name, for checking + * ENTITY and ENTITIES attribute values; records the notation + * name if it hasn't yet been declared. Passed to the next consumer, + * unless this one was preloaded with a particular DTD. + */ + public void unparsedEntityDecl ( + String name, + String publicId, + String systemId, + String notationName + ) throws SAXException + { + if (disableDeclarations) + return; + + unparsed.addElement (name); + if (!notations.contains (notationName)) + nDeferred.addElement (notationName); + super.unparsedEntityDecl (name, publicId, systemId, notationName); + } + + + /** + * ContentHandler Ensures that state from any previous parse + * has been deleted. + * Passed to the next consumer. + */ + public void startDocument () + throws SAXException + { + resetState (); + super.startDocument (); + } + + + private static boolean isAsciiLetter (char c) + { + return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'); + } + + + /** + * ContentHandler Reports a fatal exception. Validating + * XML processors may not skip any entities. + */ + public void skippedEntity (String name) + throws SAXException + { + fatalError ("may not skip entities"); + } + + /* + * SAX2 doesn't expand non-PE refs in attribute defaults... + */ + private String expandDefaultRefs (String s) + throws SAXException + { + if (s.indexOf ('&') < 0) + return s; + +// FIXME: handle nn; nn; &name; + String message = "Can't expand refs in attribute default: " + s; + warning (message); + + return s; + } + + /** + * ContentHandler Performs validity checks against element + * (and document) content models, and attribute values. + * Passed to the next consumer. + */ + public void startElement ( + String uri, + String localName, + String qName, + Attributes atts + ) throws SAXException + { + // + // First check content model for the enclosing scope. + // + if (contentStack.isEmpty ()) { + // VC: Root Element Type + if (!qName.equals (rootName)) { + if (rootName == null) + warning ("This document has no DTD, can't be valid"); + else + error ("Root element type '" + qName + + "' was declared to be '" + rootName + "'"); + } + } else { + Recognizer state = (Recognizer) contentStack.peek (); + + if (state != null) { + Recognizer newstate = state.acceptElement (qName); + + if (newstate == null) + error ("Element type '" + qName + + "' in element '" + state.type.name + + "' violates content model " + state.type.model + ); + if (newstate != state) { + contentStack.pop (); + contentStack.push (newstate); + } + } + } + + // + // Then check that this element was declared, and push the + // object used to validate its content model onto our stack. + // + // This is where the recognizer gets created, if needed; if + // it's a "children" (elements) content model, an NDFA is + // created. (One recognizer is used per content type, no + // matter how complex that recognizer is.) + // + ElementInfo info; + + info = (ElementInfo) elements.get (qName); + if (info == null || info.model == null) { + // VC: Element Valid (base clause) + error ("Element type '" + qName + "' was not declared"); + contentStack.push (null); + + // for less diagnostic noise, fake a declaration. + elementDecl (qName, "ANY"); + } else + contentStack.push (info.getRecognizer (this)); + + // + // Then check each attribute present + // + int len; + String aname; + AttributeInfo ainfo; + + if (atts != null) + len = atts.getLength (); + else + len = 0; + + for (int i = 0; i < len; i++) { + aname = atts.getQName (i); + + if (info == null + || (ainfo = (AttributeInfo) info.attributes.get (aname)) + == null) { + // VC: Attribute Value Type + error ("Attribute '" + aname + + "' was not declared for element type " + qName); + continue; + } + + String value = atts.getValue (i); + + // note that "==" for type names and "#FIXED" is correct + // (and fast) since we've interned those literals. + + if ("#FIXED" == ainfo.mode) { + String expanded = expandDefaultRefs (ainfo.value); + + // VC: Fixed Attribute Default + if (!value.equals (expanded)) { + error ("Attribute '" + aname + + "' must match " + expanded + ); + continue; + } + } + + if ("CDATA" == ainfo.type) + continue; + + // + // For all other attribute types, there are various + // rules to follow. + // + + if ("ID" == ainfo.type) { + // VC: ID (must be a name) + if (isName (value, "ID attribute", aname)) { + if (Boolean.TRUE == ids.get (value)) + // VC: ID (appears once) + error ("ID attribute " + aname + + " uses an ID value '" + value + + "' which was already declared."); + else + // any forward refs are no longer problems + ids.put (value, Boolean.TRUE); + } + continue; + } + + if ("IDREF" == ainfo.type) { + // VC: IDREF (value must be a name) + if (isName (value, "IDREF attribute", aname)) { + // VC: IDREF (must match some ID attribute) + if (ids.get (value) == null) + // new -- assume it's a forward ref + ids.put (value, Boolean.FALSE); + } + continue; + } + + if ("IDREFS" == ainfo.type) { + StringTokenizer tokens = new StringTokenizer (value, " "); + + if (!tokens.hasMoreTokens ()) { + // VC: IDREF (one or more values) + error ("IDREFS attribute " + aname + + " must have at least one ID ref"); + } else do { + String id = tokens.nextToken (); + + // VC: IDREF (value must be a name) + if (isName (id, "IDREFS attribute", aname)) { + // VC: IDREF (must match some ID attribute) + if (ids.get (id) == null) + // new -- assume it's a forward ref + ids.put (id, Boolean.FALSE); + } + } while (tokens.hasMoreTokens ()); + continue; + } + + if ("NMTOKEN" == ainfo.type) { + // VC: Name Token (is a name token) + isNmtoken (value, "NMTOKEN attribute", aname); + continue; + } + + if ("NMTOKENS" == ainfo.type) { + StringTokenizer tokens = new StringTokenizer (value, " "); + + if (!tokens.hasMoreTokens ()) { + // VC: Name Token (one or more values) + error ("NMTOKENS attribute " + aname + + " must have at least one name token"); + } else do { + String token = tokens.nextToken (); + + // VC: Name Token (is a name token) + isNmtoken (token, "NMTOKENS attribute", aname); + } while (tokens.hasMoreTokens ()); + continue; + } + + if ("ENTITY" == ainfo.type) { + if (!unparsed.contains (value)) + // VC: Entity Name + error ("Value of attribute '" + aname + + "' refers to unparsed entity '" + value + + "' which was not declared."); + continue; + } + + if ("ENTITIES" == ainfo.type) { + StringTokenizer tokens = new StringTokenizer (value, " "); + + if (!tokens.hasMoreTokens ()) { + // VC: Entity Name (one or more values) + error ("ENTITIES attribute " + aname + + " must have at least one name token"); + } else do { + String entity = tokens.nextToken (); + + if (!unparsed.contains (entity)) + // VC: Entity Name + error ("Value of attribute '" + aname + + "' refers to unparsed entity '" + entity + + "' which was not declared."); + } while (tokens.hasMoreTokens ()); + continue; + } + + // + // check for enumerations last; more expensive + // + if (ainfo.type.charAt (0) == '(' /*)*/ + || ainfo.type.startsWith ("NOTATION ") + ) { + // VC: Enumeration (value must be defined) + checkEnumeration (value, ainfo.type, aname); + continue; + } + } + + // + // Last, check that all #REQUIRED attributes were provided + // + if (info != null) { + Hashtable table = info.attributes; + + if (table.size () != 0) { + Enumeration e = table.keys (); + + // XXX table.keys uses the heap, bleech -- slows things + + while (e.hasMoreElements ()) { + aname = (String) e.nextElement (); + ainfo = (AttributeInfo) table.get (aname); + + // "#REQUIRED" mode was interned in attributeDecl + if ("#REQUIRED" == ainfo.mode + && atts.getValue (aname) == null) { + // VC: Required Attribute + error ("Attribute '" + aname + "' must be specified " + + "for element type " + qName); + } + } + } + } + super.startElement (uri, localName, qName, atts); + } + + /** + * ContentHandler Reports a validity error if the element's content + * model does not permit character data. + * Passed to the next consumer. + */ + public void characters (char ch [], int start, int length) + throws SAXException + { + Recognizer state; + + if (contentStack.empty ()) + state = null; + else + state = (Recognizer) contentStack.peek (); + + // NOTE: if this ever supports with SAX parsers that don't + // report ignorable whitespace as such (only XP?), this class + // needs to morph it into ignorableWhitespace() as needed ... + + if (state != null && !state.acceptCharacters ()) + // VC: Element Valid (clauses three, four -- see recognizer) + error ("Character content not allowed in element " + + state.type.name); + + super.characters (ch, start, length); + } + + + /** + * ContentHandler Reports a validity error if the element's content + * model does not permit end-of-element yet, or a well formedness error + * if there was no matching startElement call. + * Passed to the next consumer. + */ + public void endElement (String uri, String localName, String qName) + throws SAXException + { + try { + Recognizer state = (Recognizer) contentStack.pop (); + + if (state != null && !state.completed ()) + // VC: Element valid (clauses two, three, four; see Recognizer) + error ("Premature end for element '" + + state.type.name + + "', content model " + + state.type.model); + + // could insist on match of start element, but that's + // something the input stream must to guarantee. + + } catch (EmptyStackException e) { + fatalError ("endElement without startElement: " + qName + + ((uri == null) + ? "" + : ( " { '" + uri + "', " + localName + " }"))); + } + super.endElement (uri, localName, qName); + } + + /** + * ContentHandler Checks whether all ID values that were + * referenced have been declared, and releases all resources. + * Passed to the next consumer. + * + * @see #setDocumentLocator + */ + public void endDocument () + throws SAXException + { + for (Enumeration idNames = ids.keys (); + idNames.hasMoreElements (); + /* NOP */) { + String id = (String) idNames.nextElement (); + + if (Boolean.FALSE == ids.get (id)) { + // VC: IDREF (must match ID) + error ("Undeclared ID value '" + id + + "' was referred to by an IDREF/IDREFS attribute"); + } + } + + resetState (); + super.endDocument (); + } + + + /** Holds per-element declarations */ + static private final class ElementInfo + { + String name; + String model; + + // key = attribute name; value = AttributeInfo + Hashtable attributes = new Hashtable (11); + + ElementInfo (String n) { name = n; } + + private Recognizer recognizer; + + // for validating content models: one per type, shared, + // and constructed only on demand ... so unused elements do + // not need to consume resources. + Recognizer getRecognizer (ValidationConsumer consumer) + throws SAXException + { + if (recognizer == null) { + if ("ANY".equals (model)) + recognizer = ANY; + else if ("EMPTY".equals (model)) + recognizer = new EmptyRecognizer (this); + else if ('#' == model.charAt (1)) + // n.b. this constructor does a validity check + recognizer = new MixedRecognizer (this, consumer); + else + recognizer = new ChildrenRecognizer (this, consumer); + } + return recognizer; + } + } + + /** Holds per-attribute declarations */ + static private final class AttributeInfo + { + String type; + String mode; // #REQUIRED, etc (or null) + String value; // or null + } + + + // + // Content model validation + // + + static private final Recognizer ANY = new Recognizer (null); + + + // Base class defines the calls used to validate content, + // and supports the "ANY" content model + static private class Recognizer + { + final ElementInfo type; + + Recognizer (ElementInfo t) { type = t; } + + // return true iff character data is legal here + boolean acceptCharacters () + throws SAXException + // VC: Element Valid (third and fourth clauses) + { return true; } + + // null return = failure + // otherwise, next state (like an FSM) + // prerequisite: tested that name was declared + Recognizer acceptElement (String name) + throws SAXException + // VC: Element Valid (fourth clause) + { return this; } + + // return true iff model is completed, can finish + boolean completed () + throws SAXException + // VC: Element Valid (fourth clause) + { return true; } + + public String toString () + // n.b. "children" is the interesting case! + { return (type == null) ? "ANY" : type.model; } + } + + // "EMPTY" content model -- no characters or elements + private static final class EmptyRecognizer extends Recognizer + { + public EmptyRecognizer (ElementInfo type) + { super (type); } + + // VC: Element Valid (first clause) + boolean acceptCharacters () + { return false; } + + // VC: Element Valid (first clause) + Recognizer acceptElement (String name) + { return null; } + } + + // "Mixed" content model -- ANY, but restricts elements + private static final class MixedRecognizer extends Recognizer + { + private String permitted []; + + // N.B. constructor tests for duplicated element names (VC) + public MixedRecognizer (ElementInfo t, ValidationConsumer v) + throws SAXException + { + super (t); + + // (#PCDATA...)* or (#PCDATA) ==> ... or empty + // with the "..." being "|elname|..." + StringTokenizer tokens = new StringTokenizer ( + t.model.substring (8, t.model.lastIndexOf (')')), + "|"); + Vector vec = new Vector (); + + while (tokens.hasMoreTokens ()) { + String token = tokens.nextToken (); + + if (vec.contains (token)) + v.error ("element " + token + + " is repeated in mixed content model: " + + t.model); + else + vec.addElement (token.intern ()); + } + permitted = new String [vec.size ()]; + for (int i = 0; i < permitted.length; i++) + permitted [i] = (String) vec.elementAt (i); + + // in one large machine-derived DTD sample, most of about + // 250 mixed content models were empty, and 25 had ten or + // more entries. 2 had over a hundred elements. Linear + // search isn't obviously wrong. + } + + // VC: Element Valid (third clause) + Recognizer acceptElement (String name) + { + int length = permitted.length; + + // first pass -- optimistic w.r.t. event source interning + // (and document validity) + for (int i = 0; i < length; i++) + if (permitted [i] == name) + return this; + // second pass -- pessimistic w.r.t. event source interning + for (int i = 0; i < length; i++) + if (permitted [i].equals (name)) + return this; + return null; + } + } + + + // recognizer loop flags, see later + private static final int F_LOOPHEAD = 0x01; + private static final int F_LOOPNEXT = 0x02; + + // for debugging -- used to label/count nodes in toString() + private static int nodeCount; + + /** + * "Children" content model -- these are nodes in NDFA state graphs. + * They work in fixed space. Note that these graphs commonly have + * cycles, handling features such as zero-or-more and one-or-more. + * + *
It's readonly, so only one copy is ever needed. The content model + * stack may have any number of pointers into each graph, when a model + * happens to be needed more than once due to element nesting. Since + * traversing the graph just moves to another node, and never changes + * it, traversals never interfere with each other. + * + *
There is an option to report non-deterministic models. These are + * always XML errors, but ones which are not often reported despite the + * fact that they can lead to different validating parsers giving + * different results for the same input. (The XML spec doesn't require + * them to be reported.) + * + *
FIXME There's currently at least one known bug here, in that + * it's not actually detecting the non-determinism it tries to detect. + * (Of the "optional.xml" test, the once-or-twice-2* tests are all non-D; + * maybe some others.) This may relate to the issue flagged below as + * "should not" happen (but it was), which showed up when patching the + * graph to have one exit node (or more EMPTY nodes). + */ + private static final class ChildrenRecognizer extends Recognizer + implements Cloneable + { + // for reporting non-deterministic content models + // ... a waste of space if we're not reporting those! + // ... along with the 'model' member (in base class) + private ValidationConsumer consumer; + + // for CHOICE nodes -- each component is an arc that + // accepts a different NAME (or is EMPTY indicating + // NDFA termination). + private Recognizer components []; + + // for NAME/SEQUENCE nodes -- accepts that NAME and + // then goes to the next node (CHOICE, NAME, EMPTY). + private String name; + private Recognizer next; + + // loops always point back to a CHOICE node. we mark such choice + // nodes (F_LOOPHEAD) for diagnostics and faster deep cloning. + // We also mark nodes before back pointers (F_LOOPNEXT), to ensure + // termination when we patch sequences and loops. + private int flags; + + + // prevent a needless indirection between 'this' and 'node' + private void copyIn (ChildrenRecognizer node) + { + // model & consumer are already set + components = node.components; + name = node.name; + next = node.next; + flags = node.flags; + } + + // used to construct top level "children" content models, + public ChildrenRecognizer (ElementInfo type, ValidationConsumer vc) + { + this (vc, type); + populate (type.model.toCharArray (), 0); + patchNext (new EmptyRecognizer (type), null); + } + + // used internally; populating is separate + private ChildrenRecognizer (ValidationConsumer vc, ElementInfo type) + { + super (type); + consumer = vc; + } + + + // + // When rewriting some graph nodes we need deep clones in one case; + // mostly shallow clones (what the JVM handles for us) are fine. + // + private ChildrenRecognizer shallowClone () + { + try { + return (ChildrenRecognizer) clone (); + } catch (CloneNotSupportedException e) { + throw new Error ("clone"); + } + } + + private ChildrenRecognizer deepClone () + { + return deepClone (new Hashtable (37)); + } + + private ChildrenRecognizer deepClone (Hashtable table) + { + ChildrenRecognizer retval; + + if ((flags & F_LOOPHEAD) != 0) { + retval = (ChildrenRecognizer) table.get (this); + if (retval != null) + return this; + + retval = shallowClone (); + table.put (this, retval); + } else + retval = shallowClone (); + + if (next != null) { + if (next instanceof ChildrenRecognizer) + retval.next = ((ChildrenRecognizer)next) + .deepClone (table); + else if (!(next instanceof EmptyRecognizer)) + throw new RuntimeException ("deepClone"); + } + + if (components != null) { + retval.components = new Recognizer [components.length]; + for (int i = 0; i < components.length; i++) { + Recognizer temp = components [i]; + + if (temp == null) + retval.components [i] = null; + else if (temp instanceof ChildrenRecognizer) + retval.components [i] = ((ChildrenRecognizer)temp) + .deepClone (table); + else if (!(temp instanceof EmptyRecognizer)) + throw new RuntimeException ("deepClone"); + } + } + + return retval; + } + + // connect subgraphs, first to next (sequencing) + private void patchNext (Recognizer theNext, Hashtable table) + { + // backpointers must not be repatched or followed + if ((flags & F_LOOPNEXT) != 0) + return; + + // XXX this table "shouldn't" be needed, right? + // but some choice nodes looped if it isn't there. + if (table != null && table.get (this) != null) + return; + if (table == null) + table = new Hashtable (); + + // NAME/SEQUENCE + if (name != null) { + if (next == null) + next = theNext; + else if (next instanceof ChildrenRecognizer) { + ((ChildrenRecognizer)next).patchNext (theNext, table); + } else if (!(next instanceof EmptyRecognizer)) + throw new RuntimeException ("patchNext"); + return; + } + + // CHOICE + for (int i = 0; i < components.length; i++) { + if (components [i] == null) + components [i] = theNext; + else if (components [i] instanceof ChildrenRecognizer) { + ((ChildrenRecognizer)components [i]) + .patchNext (theNext, table); + } else if (!(components [i] instanceof EmptyRecognizer)) + throw new RuntimeException ("patchNext"); + } + + if (table != null && (flags & F_LOOPHEAD) != 0) + table.put (this, this); + } + + /** + * Parses a 'children' spec (or recursively 'cp') and makes this + * become a regular graph node. + * + * @return index after this particle + */ + private int populate (char parseBuf [], int startPos) + { + int nextPos = startPos + 1; + char c; + + if (nextPos < 0 || nextPos >= parseBuf.length) + throw new IndexOutOfBoundsException (); + + // Grammar of the string is from the XML spec, but + // with whitespace removed by the SAX parser. + + // children ::= (choice | seq) ('?' | '*' | '+')? + // cp ::= (Name | choice | seq) ('?' | '*' | '+')? + // choice ::= '(' cp ('|' choice)* ')' + // seq ::= '(' cp (',' choice)* ')' + + // interior nodes only + // cp ::= name ... + if (parseBuf [startPos] != '('/*)*/) { + boolean done = false; + do { + switch (c = parseBuf [nextPos]) { + case '?': case '*': case '+': + case '|': case ',': + case /*(*/ ')': + done = true; + continue; + default: + nextPos++; + continue; + } + } while (!done); + name = new String (parseBuf, startPos, nextPos - startPos); + + // interior OR toplevel nodes + // cp ::= choice .. + // cp ::= seq .. + } else { + // collect everything as a separate list, and merge it + // into "this" later if we can (SEQUENCE or singleton) + ChildrenRecognizer first; + + first = new ChildrenRecognizer (consumer, type); + nextPos = first.populate (parseBuf, nextPos); + c = parseBuf [nextPos++]; + + if (c == ',' || c == '|') { + ChildrenRecognizer current = first; + char separator = c; + Vector v = null; + + if (separator == '|') { + v = new Vector (); + v.addElement (first); + } + + do { + ChildrenRecognizer link; + + link = new ChildrenRecognizer (consumer, type); + nextPos = link.populate (parseBuf, nextPos); + + if (separator == ',') { + current.patchNext (link, null); + current = link; + } else + v.addElement (link); + + c = parseBuf [nextPos++]; + } while (c == separator); + + // choice ... collect everything into one array. + if (separator == '|') { + // assert v.size() > 1 + components = new Recognizer [v.size ()]; + for (int i = 0; i < components.length; i++) { + components [i] = (Recognizer) + v.elementAt (i); + } + // assert flags == 0 + + // sequence ... merge into "this" to be smaller. + } else + copyIn (first); + + // treat singletons like one-node sequences. + } else + copyIn (first); + + if (c != /*(*/ ')') + throw new RuntimeException ("corrupt content model"); + } + + // + // Arity is optional, and the root of all fun. We keep the + // FSM state graph simple by only having NAME/SEQUENCE and + // CHOICE nodes (or EMPTY to terminate a model), easily + // evaluated. So we rewrite each node that has arity, using + // those primitives. We create loops here, if needed. + // + if (nextPos < parseBuf.length) { + c = parseBuf [nextPos]; + if (c == '?' || c == '*' || c == '+') { + nextPos++; + + // Rewrite 'zero-or-one' "?" arity to a CHOICE: + // - SEQUENCE (clone, what's next) + // - or, what's next + // Size cost: N --> N + 1 + if (c == '?') { + Recognizer once = shallowClone (); + + components = new Recognizer [2]; + components [0] = once; + // components [1] initted to null + name = null; + next = null; + flags = 0; + + + // Rewrite 'zero-or-more' "*" arity to a CHOICE. + // - LOOP (clone, back to this CHOICE) + // - or, what's next + // Size cost: N --> N + 1 + } else if (c == '*') { + ChildrenRecognizer loop = shallowClone (); + + loop.patchNext (this, null); + loop.flags |= F_LOOPNEXT; + flags = F_LOOPHEAD; + + components = new Recognizer [2]; + components [0] = loop; + // components [1] initted to null + name = null; + next = null; + + + // Rewrite 'one-or-more' "+" arity to a SEQUENCE. + // Basically (a)+ --> ((a),(a)*). + // - this + // - CHOICE + // * LOOP (clone, back to the CHOICE) + // * or, whatever's next + // Size cost: N --> 2N + 1 + } else if (c == '+') { + ChildrenRecognizer loop = deepClone (); + ChildrenRecognizer choice; + + choice = new ChildrenRecognizer (consumer, type); + loop.patchNext (choice, null); + loop.flags |= F_LOOPNEXT; + choice.flags = F_LOOPHEAD; + + choice.components = new Recognizer [2]; + choice.components [0] = loop; + // choice.components [1] initted to null + // choice.name, choice.next initted to null + + patchNext (choice, null); + } + } + } + + return nextPos; + } + + // VC: Element Valid (second clause) + boolean acceptCharacters () + { return false; } + + // VC: Element Valid (second clause) + Recognizer acceptElement (String type) + throws SAXException + { + // NAME/SEQUENCE + if (name != null) { + if (name.equals (type)) + return next; + return null; + } + + // CHOICE ... optionally reporting nondeterminism we + // run across. we won't check out every transition + // for nondeterminism; only the ones we follow. + Recognizer retval = null; + + for (int i = 0; i < components.length; i++) { + Recognizer temp = components [i].acceptElement (type); + + if (temp == null) + continue; + else if (!warnNonDeterministic) + return temp; + else if (retval == null) + retval = temp; + else if (retval != temp) + consumer.error ("Content model " + this.type.model + + " is non-deterministic for " + type); + } + return retval; + } + + // VC: Element Valid (second clause) + boolean completed () + throws SAXException + { + // expecting a specific element + if (name != null) + return false; + + // choice, some sequences + for (int i = 0; i < components.length; i++) { + if (components [i].completed ()) + return true; + } + + return false; + } + +/** / + // FOR DEBUGGING ... flattens the graph for printing. + + public String toString () + { + StringBuffer buf = new StringBuffer (); + + // only one set of loop labels can be generated + // at a time... + synchronized (ANY) { + nodeCount = 0; + + toString (buf, new Hashtable ()); + return buf.toString (); + } + } + + private void toString (StringBuffer buf, Hashtable table) + { + // When we visit a node, label and count it. + // Nodes are never visited/counted more than once. + // For small models labels waste space, but if arity + // mappings were used the savings are substantial. + // (Plus, the output can be more readily understood.) + String temp = (String) table.get (this); + + if (temp != null) { + buf.append ('{'); + buf.append (temp); + buf.append ('}'); + return; + } else { + StringBuffer scratch = new StringBuffer (15); + + if ((flags & F_LOOPHEAD) != 0) + scratch.append ("loop"); + else + scratch.append ("node"); + scratch.append ('-'); + scratch.append (++nodeCount); + temp = scratch.toString (); + + table.put (this, temp); + buf.append ('['); + buf.append (temp); + buf.append (']'); + buf.append (':'); + } + + // NAME/SEQUENCE + if (name != null) { + // n.b. some output encodings turn some name chars into '?' + // e.g. with Japanese names and ASCII output + buf.append (name); + if (components != null) // bug! + buf.append ('$'); + if (next == null) + buf.append (",*"); + else if (next instanceof EmptyRecognizer) // patch-to-next + buf.append (",{}"); + else if (next instanceof ChildrenRecognizer) { + buf.append (','); + ((ChildrenRecognizer)next).toString (buf, table); + } else // bug! + buf.append (",+"); + return; + } + + // CHOICE + buf.append ("<"); + for (int i = 0; i < components.length; i++) { + if (i != 0) + buf.append ("|"); + if (components [i] instanceof EmptyRecognizer) { + buf.append ("{}"); + } else if (components [i] == null) { // patch-to-next + buf.append ('*'); + } else { + ChildrenRecognizer r; + + r = (ChildrenRecognizer) components [i]; + r.toString (buf, table); + } + } + buf.append (">"); + } +/**/ + } +} diff --git a/libjava/classpath/gnu/xml/pipeline/WellFormednessFilter.java b/libjava/classpath/gnu/xml/pipeline/WellFormednessFilter.java new file mode 100644 index 000000000..7a3db6593 --- /dev/null +++ b/libjava/classpath/gnu/xml/pipeline/WellFormednessFilter.java @@ -0,0 +1,363 @@ +/* WellFormednessFilter.java -- + Copyright (C) 1999,2000,2001 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.pipeline; + +import java.util.EmptyStackException; +import java.util.Stack; + +import org.xml.sax.Attributes; +import org.xml.sax.ErrorHandler; +import org.xml.sax.Locator; +import org.xml.sax.SAXException; +import org.xml.sax.SAXParseException; + +/** + * This filter reports fatal exceptions in the case of event streams that + * are not well formed. The rules currently tested include:
Other checks for event stream correctness may be provided in
+ * the future. For example, insisting that
+ * entity boundaries nest correctly,
+ * namespace scopes nest correctly,
+ * namespace values never contain relative URIs,
+ * attributes don't have "<" characters;
+ * and more.
+ *
+ * @author David Brownell
+ */
+public final class WellFormednessFilter extends EventFilter
+{
+ private boolean startedDoc;
+ private Stack elementStack = new Stack ();
+ private boolean startedCDATA;
+ private String dtdState = "before";
+
+
+ /**
+ * Swallows all events after performing well formedness checks.
+ */
+ // constructor used by PipelineFactory
+ public WellFormednessFilter ()
+ { this (null); }
+
+
+ /**
+ * Passes events through to the specified consumer, after first
+ * processing them.
+ */
+ // constructor used by PipelineFactory
+ public WellFormednessFilter (EventConsumer consumer)
+ {
+ super (consumer);
+
+ setContentHandler (this);
+ setDTDHandler (this);
+
+ try {
+ setProperty (LEXICAL_HANDLER, this);
+ } catch (SAXException e) { /* can't happen */ }
+ }
+
+ /**
+ * Resets state as if any preceding event stream was well formed.
+ * Particularly useful if it ended through some sort of error,
+ * and the endDocument call wasn't made.
+ */
+ public void reset ()
+ {
+ startedDoc = false;
+ startedCDATA = false;
+ elementStack.removeAllElements ();
+ }
+
+
+ private SAXParseException getException (String message)
+ {
+ SAXParseException e;
+ Locator locator = getDocumentLocator ();
+
+ if (locator == null)
+ return new SAXParseException (message, null, null, -1, -1);
+ else
+ return new SAXParseException (message, locator);
+ }
+
+ private void fatalError (String message)
+ throws SAXException
+ {
+ SAXParseException e = getException (message);
+ ErrorHandler handler = getErrorHandler ();
+
+ if (handler != null)
+ handler.fatalError (e);
+ throw e;
+ }
+
+ /**
+ * Throws an exception when called after startDocument.
+ *
+ * @param locator the locator, to be used in error reporting or relative
+ * URI resolution.
+ *
+ * @exception IllegalStateException when called after the document
+ * has already been started
+ */
+ public void setDocumentLocator (Locator locator)
+ {
+ if (startedDoc)
+ throw new IllegalStateException (
+ "setDocumentLocator called after startDocument");
+ super.setDocumentLocator (locator);
+ }
+
+ public void startDocument () throws SAXException
+ {
+ if (startedDoc)
+ fatalError ("startDocument called more than once");
+ startedDoc = true;
+ startedCDATA = false;
+ elementStack.removeAllElements ();
+ super.startDocument ();
+ }
+
+ public void startElement (
+ String uri, String localName,
+ String qName, Attributes atts
+ ) throws SAXException
+ {
+ if (!startedDoc)
+ fatalError ("callback outside of document?");
+ if ("inside".equals (dtdState))
+ fatalError ("element inside DTD?");
+ else
+ dtdState = "after";
+ if (startedCDATA)
+ fatalError ("element inside CDATA section");
+ if (qName == null || "".equals (qName))
+ fatalError ("startElement name missing");
+ elementStack.push (qName);
+ super.startElement (uri, localName, qName, atts);
+ }
+
+ public void endElement (String uri, String localName, String qName)
+ throws SAXException
+ {
+ if (!startedDoc)
+ fatalError ("callback outside of document?");
+ if (startedCDATA)
+ fatalError ("element inside CDATA section");
+ if (qName == null || "".equals (qName))
+ fatalError ("endElement name missing");
+
+ try {
+ String top = (String) elementStack.pop ();
+
+ if (!qName.equals (top))
+ fatalError ("<" + top + " ...>..." + qName + ">");
+ // XXX could record/test namespace info
+ } catch (EmptyStackException e) {
+ fatalError ("endElement without startElement: " + qName + ">");
+ }
+ super.endElement (uri, localName, qName);
+ }
+
+ public void endDocument () throws SAXException
+ {
+ if (!startedDoc)
+ fatalError ("callback outside of document?");
+ dtdState = "before";
+ startedDoc = false;
+ super.endDocument ();
+ }
+
+
+ public void startDTD (String root, String publicId, String systemId)
+ throws SAXException
+ {
+ if (!startedDoc)
+ fatalError ("callback outside of document?");
+ if ("before" != dtdState)
+ fatalError ("two DTDs?");
+ if (!elementStack.empty ())
+ fatalError ("DTD must precede root element");
+ dtdState = "inside";
+ super.startDTD (root, publicId, systemId);
+ }
+
+ public void notationDecl (String name, String publicId, String systemId)
+ throws SAXException
+ {
+// FIXME: not all parsers will report startDTD() ...
+// we'd rather insist we're "inside".
+ if ("after" == dtdState)
+ fatalError ("not inside DTD");
+ super.notationDecl (name, publicId, systemId);
+ }
+
+ public void unparsedEntityDecl (String name,
+ String publicId, String systemId, String notationName)
+ throws SAXException
+ {
+// FIXME: not all parsers will report startDTD() ...
+// we'd rather insist we're "inside".
+ if ("after" == dtdState)
+ fatalError ("not inside DTD");
+ super.unparsedEntityDecl (name, publicId, systemId, notationName);
+ }
+
+ // FIXME: add the four DeclHandler calls too
+
+ public void endDTD ()
+ throws SAXException
+ {
+ if (!startedDoc)
+ fatalError ("callback outside of document?");
+ if ("inside" != dtdState)
+ fatalError ("DTD ends without start?");
+ dtdState = "after";
+ super.endDTD ();
+ }
+
+ public void characters (char ch [], int start, int length)
+ throws SAXException
+ {
+ int here = start, end = start + length;
+ if (elementStack.empty ())
+ fatalError ("characters must be in an element");
+ while (here < end) {
+ if (ch [here++] != ']')
+ continue;
+ if (here == end) // potential problem ...
+ continue;
+ if (ch [here++] != ']')
+ continue;
+ if (here == end) // potential problem ...
+ continue;
+ if (ch [here++] == '>')
+ fatalError ("character data can't contain \"]]>\"");
+ }
+ super.characters (ch, start, length);
+ }
+
+ public void ignorableWhitespace (char ch [], int start, int length)
+ throws SAXException
+ {
+ int here = start, end = start + length;
+ if (elementStack.empty ())
+ fatalError ("characters must be in an element");
+ while (here < end) {
+ if (ch [here++] == '\r')
+ fatalError ("whitespace can't contain CR");
+ }
+ super.ignorableWhitespace (ch, start, length);
+ }
+
+ public void processingInstruction (String target, String data)
+ throws SAXException
+ {
+ if (data.indexOf ('\r') > 0)
+ fatalError ("PIs can't contain CR");
+ if (data.indexOf ("?>") > 0)
+ fatalError ("PIs can't contain \"?>\"");
+ }
+
+ public void comment (char ch [], int start, int length)
+ throws SAXException
+ {
+ if (!startedDoc)
+ fatalError ("callback outside of document?");
+ if (startedCDATA)
+ fatalError ("comments can't nest in CDATA");
+ int here = start, end = start + length;
+ while (here < end) {
+ if (ch [here] == '\r')
+ fatalError ("comments can't contain CR");
+ if (ch [here++] != '-')
+ continue;
+ if (here == end)
+ fatalError ("comments can't end with \"--->\"");
+ if (ch [here++] == '-')
+ fatalError ("comments can't contain \"--\"");
+ }
+ super.comment (ch, start, length);
+ }
+
+ public void startCDATA ()
+ throws SAXException
+ {
+ if (!startedDoc)
+ fatalError ("callback outside of document?");
+ if (startedCDATA)
+ fatalError ("CDATA starts can't nest");
+ startedCDATA = true;
+ super.startCDATA ();
+ }
+
+ public void endCDATA ()
+ throws SAXException
+ {
+ if (!startedDoc)
+ fatalError ("callback outside of document?");
+ if (!startedCDATA)
+ fatalError ("CDATA end without start?");
+ startedCDATA = false;
+ super.endCDATA ();
+ }
+}
diff --git a/libjava/classpath/gnu/xml/pipeline/XIncludeFilter.java b/libjava/classpath/gnu/xml/pipeline/XIncludeFilter.java
new file mode 100644
index 000000000..a1445fa0c
--- /dev/null
+++ b/libjava/classpath/gnu/xml/pipeline/XIncludeFilter.java
@@ -0,0 +1,579 @@
+/* XIncludeFilter.java --
+ Copyright (C) 2001,2002 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.pipeline;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.net.URL;
+import java.net.URLConnection;
+import java.util.Hashtable;
+import java.util.Stack;
+import java.util.Vector;
+
+import org.xml.sax.Attributes;
+import org.xml.sax.ErrorHandler;
+import org.xml.sax.InputSource;
+import org.xml.sax.Locator;
+import org.xml.sax.SAXException;
+import org.xml.sax.SAXParseException;
+import org.xml.sax.XMLReader;
+import org.xml.sax.helpers.XMLReaderFactory;
+
+import gnu.xml.util.Resolver;
+
+
+
+/**
+ * Filter to process an XPointer-free subset of
+ * XInclude, supporting its
+ * use as a kind of replacement for parsed general entities.
+ * XInclude works much like the #include
of C/C++ but
+ * works for XML documents as well as unparsed text files.
+ * Restrictions from the 17-Sept-2002 CR draft of XInclude are as follows:
+ *
+ *
XML documents that are included will normally be processed using + * the default SAX namespace rules, meaning that prefix information may + * be discarded. This may be changed with {@link #setSavingPrefixes + * setSavingPrefixes()}. You are strongly advised to do this. + * + *
Note that XInclude allows highly incompatible implementations, which + * are specialized to handle application-specific infoset extensions. Some + * such implementations can be implemented by subclassing this one, but + * they may only be substituted in applications at "user option". + * + *
TBD: "IURI" handling.
+ *
+ * @author David Brownell
+ */
+public class XIncludeFilter extends EventFilter implements Locator
+{
+ private Hashtable extEntities = new Hashtable (5, 5);
+ private int ignoreCount;
+ private Stack uris = new Stack ();
+ private Locator locator;
+ private Vector inclusions = new Vector (5, 5);
+ private boolean savingPrefixes;
+
+ /**
+ */
+ public XIncludeFilter (EventConsumer next)
+ throws SAXException
+ {
+ super (next);
+ setContentHandler (this);
+ // DTDHandler callbacks pass straight through
+ setProperty (DECL_HANDLER, this);
+ setProperty (LEXICAL_HANDLER, this);
+ }
+
+ private void fatal (SAXParseException e) throws SAXException
+ {
+ ErrorHandler eh;
+
+ eh = getErrorHandler ();
+ if (eh != null)
+ eh.fatalError (e);
+ throw e;
+ }
+
+ /**
+ * Passes "this" down the filter chain as a proxy locator.
+ */
+ public void setDocumentLocator (Locator locator)
+ {
+ this.locator = locator;
+ super.setDocumentLocator (this);
+ }
+
+ /** Used for proxy locator; do not call directly. */
+ public String getSystemId ()
+ { return (locator == null) ? null : locator.getSystemId (); }
+ /** Used for proxy locator; do not call directly. */
+ public String getPublicId ()
+ { return (locator == null) ? null : locator.getPublicId (); }
+ /** Used for proxy locator; do not call directly. */
+ public int getLineNumber ()
+ { return (locator == null) ? -1 : locator.getLineNumber (); }
+ /** Used for proxy locator; do not call directly. */
+ public int getColumnNumber ()
+ { return (locator == null) ? -1 : locator.getColumnNumber (); }
+
+ /**
+ * Assigns the flag controlling the setting of the SAX2
+ * namespace-prefixes flag.
+ */
+ public void setSavingPrefixes (boolean flag)
+ { savingPrefixes = flag; }
+
+ /**
+ * Returns the flag controlling the setting of the SAX2
+ * namespace-prefixes flag when parsing included documents.
+ * The default value is the SAX2 default (false), which discards
+ * information that can be useful.
+ */
+ public boolean isSavingPrefixes ()
+ { return savingPrefixes; }
+
+ //
+ // Two mechanisms are interacting here.
+ //
+ // - XML Base implies a stack of base URIs, updated both by
+ // "real entity" boundaries and element boundaries.
+ //
+ // - Active "Real Entities" (for document and general entities,
+ // and by xincluded files) are tracked to prevent circular
+ // inclusions.
+ //
+ private String addMarker (String uri)
+ throws SAXException
+ {
+ if (locator != null && locator.getSystemId () != null)
+ uri = locator.getSystemId ();
+
+ // guard against InputSource objects without system IDs
+ if (uri == null)
+ fatal (new SAXParseException ("Entity URI is unknown", locator));
+
+ try {
+ URL url = new URL (uri);
+
+ uri = url.toString ();
+ if (inclusions.contains (uri))
+ fatal (new SAXParseException (
+ "XInclude, circular inclusion", locator));
+ inclusions.addElement (uri);
+ uris.push (url);
+ } catch (IOException e) {
+ // guard against illegal relative URIs (Xerces)
+ fatal (new SAXParseException ("parser bug: relative URI",
+ locator, e));
+ }
+ return uri;
+ }
+
+ private void pop (String uri)
+ {
+ inclusions.removeElement (uri);
+ uris.pop ();
+ }
+
+ //
+ // Document entity boundaries get both treatments.
+ //
+ public void startDocument () throws SAXException
+ {
+ ignoreCount = 0;
+ addMarker (null);
+ super.startDocument ();
+ }
+
+ public void endDocument () throws SAXException
+ {
+ inclusions.setSize (0);
+ extEntities.clear ();
+ uris.setSize (0);
+ super.endDocument ();
+ }
+
+ //
+ // External general entity boundaries get both treatments.
+ //
+ public void externalEntityDecl (String name,
+ String publicId, String systemId)
+ throws SAXException
+ {
+ if (name.charAt (0) == '%')
+ return;
+ try {
+ URL url = new URL (locator.getSystemId ());
+ systemId = new URL (url, systemId).toString ();
+ } catch (IOException e) {
+ // what could we do?
+ }
+ extEntities.put (name, systemId);
+ }
+
+ public void startEntity (String name)
+ throws SAXException
+ {
+ if (ignoreCount != 0) {
+ ignoreCount++;
+ return;
+ }
+
+ String uri = (String) extEntities.get (name);
+ if (uri != null)
+ addMarker (uri);
+ super.startEntity (name);
+ }
+
+ public void endEntity (String name)
+ throws SAXException
+ {
+ if (ignoreCount != 0) {
+ if (--ignoreCount != 0)
+ return;
+ }
+
+ String uri = (String) extEntities.get (name);
+
+ if (uri != null)
+ pop (uri);
+ super.endEntity (name);
+ }
+
+ //
+ // element boundaries only affect the base URI stack,
+ // unless they're XInclude elements.
+ //
+ public void
+ startElement (String uri, String localName, String qName, Attributes atts)
+ throws SAXException
+ {
+ if (ignoreCount != 0) {
+ ignoreCount++;
+ return;
+ }
+
+ URL baseURI = (URL) uris.peek ();
+ String base;
+
+ base = atts.getValue ("http://www.w3.org/XML/1998/namespace", "base");
+ if (base == null)
+ uris.push (baseURI);
+ else {
+ URL url;
+
+ if (base.indexOf ('#') != -1)
+ fatal (new SAXParseException (
+ "xml:base with fragment: " + base,
+ locator));
+
+ try {
+ baseURI = new URL (baseURI, base);
+ uris.push (baseURI);
+ } catch (Exception e) {
+ fatal (new SAXParseException (
+ "xml:base with illegal uri: " + base,
+ locator, e));
+ }
+ }
+
+ if (!"http://www.w3.org/2001/XInclude".equals (uri)) {
+ super.startElement (uri, localName, qName, atts);
+ return;
+ }
+
+ if ("include".equals (localName)) {
+ String href = atts.getValue ("href");
+ String parse = atts.getValue ("parse");
+ String encoding = atts.getValue ("encoding");
+ URL url = (URL) uris.peek ();
+ SAXParseException x = null;
+
+ if (href == null)
+ fatal (new SAXParseException (
+ "XInclude missing href",
+ locator));
+ if (href.indexOf ('#') != -1)
+ fatal (new SAXParseException (
+ "XInclude with fragment: " + href,
+ locator));
+
+ if (parse == null || "xml".equals (parse))
+ x = xinclude (url, href);
+ else if ("text".equals (parse))
+ x = readText (url, href, encoding);
+ else
+ fatal (new SAXParseException (
+ "unknown XInclude parsing mode: " + parse,
+ locator));
+ if (x == null) {
+ // strip out all child content
+ ignoreCount++;
+ return;
+ }
+
+ // FIXME the 17-Sept-2002 CR of XInclude says we "must"
+ // use xi:fallback elements to handle resource errors,
+ // if they exist.
+ fatal (x);
+
+ } else if ("fallback".equals (localName)) {
+ fatal (new SAXParseException (
+ "illegal top level XInclude 'fallback' element",
+ locator));
+ } else {
+ ErrorHandler eh = getErrorHandler ();
+
+ // CR doesn't say this is an error
+ if (eh != null)
+ eh.warning (new SAXParseException (
+ "unrecognized toplevel XInclude element: " + localName,
+ locator));
+ super.startElement (uri, localName, qName, atts);
+ }
+ }
+
+ public void endElement (String uri, String localName, String qName)
+ throws SAXException
+ {
+ if (ignoreCount != 0) {
+ if (--ignoreCount != 0)
+ return;
+ }
+
+ uris.pop ();
+ if (!("http://www.w3.org/2001/XInclude".equals (uri)
+ && "include".equals (localName)))
+ super.endElement (uri, localName, qName);
+ }
+
+ //
+ // ignore all content within non-empty xi:include elements
+ //
+ public void characters (char ch [], int start, int length)
+ throws SAXException
+ {
+ if (ignoreCount == 0)
+ super.characters (ch, start, length);
+ }
+
+ public void processingInstruction (String target, String value)
+ throws SAXException
+ {
+ if (ignoreCount == 0)
+ super.processingInstruction (target, value);
+ }
+
+ public void ignorableWhitespace (char ch [], int start, int length)
+ throws SAXException
+ {
+ if (ignoreCount == 0)
+ super.ignorableWhitespace (ch, start, length);
+ }
+
+ public void comment (char ch [], int start, int length)
+ throws SAXException
+ {
+ if (ignoreCount == 0)
+ super.comment (ch, start, length);
+ }
+
+ public void startCDATA () throws SAXException
+ {
+ if (ignoreCount == 0)
+ super.startCDATA ();
+ }
+
+ public void endCDATA () throws SAXException
+ {
+ if (ignoreCount == 0)
+ super.endCDATA ();
+ }
+
+ public void startPrefixMapping (String prefix, String uri)
+ throws SAXException
+ {
+ if (ignoreCount == 0)
+ super.startPrefixMapping (prefix, uri);
+ }
+
+ public void endPrefixMapping (String prefix) throws SAXException
+ {
+ if (ignoreCount == 0)
+ super.endPrefixMapping (prefix);
+ }
+
+ public void skippedEntity (String name) throws SAXException
+ {
+ if (ignoreCount == 0)
+ super.skippedEntity (name);
+ }
+
+ // JDK 1.1 seems to need it to be done this way, sigh
+ void setLocator (Locator l) { locator = l; }
+ Locator getLocator () { return locator; }
+
+
+ //
+ // for XIncluded entities, manage the current locator and
+ // filter out events that would be incorrect to report
+ //
+ private class Scrubber extends EventFilter
+ {
+ Scrubber (EventFilter f)
+ throws SAXException
+ {
+ // delegation passes to next in chain
+ super (f);
+
+ // process all content events
+ super.setContentHandler (this);
+ super.setProperty (LEXICAL_HANDLER, this);
+
+ // drop all DTD events
+ super.setDTDHandler (null);
+ super.setProperty (DECL_HANDLER, null);
+ }
+
+ // maintain proxy locator
+ // only one startDocument()/endDocument() pair per event stream
+ public void setDocumentLocator (Locator l)
+ { setLocator (l); }
+ public void startDocument ()
+ { }
+ public void endDocument ()
+ { }
+
+ private void reject (String message) throws SAXException
+ { fatal (new SAXParseException (message, getLocator ())); }
+
+ // only the DTD from the "base document" gets reported
+ public void startDTD (String root, String publicId, String systemId)
+ throws SAXException
+ { reject ("XIncluded DTD: " + systemId); }
+ public void endDTD ()
+ throws SAXException
+ { reject ("XIncluded DTD"); }
+ // ... so this should never happen
+ public void skippedEntity (String name) throws SAXException
+ { reject ("XInclude skipped entity: " + name); }
+
+ // since we rejected DTDs, only builtin entities can be reported
+ }
+
+ // This package exposes a kind of XML processing pipeline, based on sending
+SAX events, which can be used as components of application architectures.
+Pipelines are used to convey streams of processing events from a producer
+to one or more consumers, and to let each consumer control the data seen by
+later consumers.
+
+ There is a PipelineFactory class which
+accepts a syntax describing how to construct some simple pipelines. Strings
+describing such pipelines can be used in command line tools (see the
+DoParse class)
+and in other places that it is
+useful to let processing be easily reconfigured. Pipelines can of course
+be constructed programmatically, providing access to options that the
+factory won't.
+
+ Web applications are supported by making it easy for servlets (or
+non-Java web application components) to be part of a pipeline. They can
+originate XML (or XHTML) data through an InputSource or in
+response to XML messages sent from clients using CallFilter
+pipeline stages. Such facilities are available using the simple syntax
+for pipeline construction.
+
+
+ Pipelines should be simple to understand.
+
+ Many producers will be SAX2 XMLReader objects, and
+will read (pull) data which is then written (pushed) as events.
+Typically these will parse XML text (acquired from
+ In other cases, you will write producers yourself. For example, some
+data structures might know how to write themselves out using one or
+more XML models, expressed as sequences of SAX2 event callbacks.
+An application module might
+itself be a producer, issuing startDocument and endDocument events
+and then asking those data structures to write themselves out to a
+given EventConsumer, or walking data structures (such as JDBC query
+results) and applying its own conversion rules. WAP format XML
+(WBMXL) can be directly converted to producer output.
+
+ SAX2 introduced an "XMLFilter" interface, which is a kind of XMLReader.
+It is most useful in conjunction with its XMLFilterImpl helper class;
+see the EventFilter javadoc
+for information contrasting that XMLFilterImpl approach with the
+relevant parts of this pipeline framework. Briefly, such XMLFilterImpl
+children can be either producers or consumers, and are more limited in
+configuration flexibility. In this framework, the focus of filters is
+on the EventConsumer side; see the section on
+pipe fitting below.
+
+
+ Many consumers will be used to create standard representations of XML
+data. The TextConsumer takes its events
+and writes them as text for a single XML document,
+using an internal XMLWriter.
+The DomConsumer takes its events and uses
+them to create and populate a DOM Document.
+
+ In other cases, you will write consumers yourself. For example,
+you might use a particular unmarshaling filter to produce objects
+that fit your application's requirements, instead of using DOM.
+Such consumers work at the level of XML data models, rather than with
+specific representations such as XML text or a DOM tree. You could
+convert your output directly to WAP format data (WBXML).
+
+
+ Pipelines are composite event consumers, with each stage having
+the opportunity to transform the data before delivering it to any
+subsequent stages.
+
+ The PipelineFactory class
+provides access to much of this functionality through a simple syntax.
+See the table in that class's javadoc describing a number of standard
+components. Direct API calls are still needed for many of the most
+interesting pipeline configurations, including ones leveraging actual
+or logical concurrency.
+
+ Four basic types of pipe fitting are directly supported. These may
+be used to construct complex pipeline networks. Note that filters can be as complex as
+XSLT transforms
+available) on input data, or as simple as removing simple syntax data
+such as ignorable whitespace, comments, and CDATA delimiters.
+Some simple "built-in" filters are part of this package.
+
+
+ If you follow these coding conventions, your classes may be used
+directly (give the full class name) in pipeline descriptions as understood
+by the PipelineFactory. There are four constructors the factory may
+try to use; in order of decreasing numbers of parameters, these are: Of course, classes may support more than one such usage convention;
+if they do, they can automatically be used in multiple modes. If you
+try to use a terminus class as a filter, and that terminus has a constructor
+with the appropriate number of arguments, it is automatically wrapped in
+a "tee" filter.
+
+
+ It can sometimes be hard to see what's happening, when something
+goes wrong. Easily fixed: just snapshot the data. Then you can find
+out where things start to go wrong.
+
+ If you're using pipeline descriptors so that they're easily
+administered, just stick a write ( filename )
+filter into the pipeline at an appropriate point.
+
+ Inside your programs, you can do the same thing directly: perhaps
+by saving a Writer (perhaps a StringWriter) in a variable, using that
+to create a TextConsumer, and making that the first part of a tee --
+splicing that into your pipeline at a convenient location.
+
+ You can also use a DomConsumer to buffer the data, but remember
+that DOM doesn't save all the information that XML provides, so that DOM
+snapshots are relatively low fidelity. They also are substantially more
+expensive in terms of memory than a StringWriter holding similar data.
+
+ Producers in pipelines don't need to start from XML
+data structures, such as text in XML syntax (likely coming
+from some XMLReader that parses XML) or a
+DOM representation (perhaps with a
+DomParser).
+
+ One common type of event producer will instead make
+direct calls to SAX event handlers returned from an
+EventConsumer.
+For example, making ContentHandler.startElement
+calls and matching ContentHandler.endElement calls.
+
+ Applications making such calls can catch certain
+common "syntax errors" by using a
+WellFormednessFilter.
+That filter will detect (and report) erroneous input data
+such as mismatched document, element, or CDATA start/end calls.
+Use such a filter near the head of the pipeline that your
+producer feeds, at least while debugging, to help ensure that
+you're providing legal XML Infoset data.
+
+ You can also arrange to validate data on the fly.
+For DTD validation, you can configure a
+ValidationConsumer
+to work as a filter, using any DTD you choose.
+Other validation schemes can be handled with other
+validation filters.
+
+
diff --git a/libjava/classpath/gnu/xml/stream/AttributeImpl.java b/libjava/classpath/gnu/xml/stream/AttributeImpl.java
new file mode 100644
index 000000000..58a0dbe21
--- /dev/null
+++ b/libjava/classpath/gnu/xml/stream/AttributeImpl.java
@@ -0,0 +1,123 @@
+/* AttributeImpl.java --
+ Copyright (C) 2005 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.stream;
+
+import java.io.IOException;
+import java.io.Writer;
+import javax.xml.namespace.QName;
+import javax.xml.stream.events.Attribute;
+import javax.xml.stream.Location;
+import javax.xml.stream.XMLStreamException;
+
+/**
+ * An attribute event.
+ *
+ * @author Chris Burdess
+ */
+public class AttributeImpl
+ extends XMLEventImpl
+ implements Attribute
+{
+
+ protected final QName name;
+ protected final String value;
+ protected final String type;
+ protected final boolean specified;
+
+ protected AttributeImpl(Location location,
+ QName name, String value, String type,
+ boolean specified)
+ {
+ super(location);
+ this.name = name;
+ this.value = value;
+ this.type = type;
+ this.specified = specified;
+ }
+
+ public int getEventType()
+ {
+ return ATTRIBUTE;
+ }
+
+ public QName getName()
+ {
+ return name;
+ }
+
+ public String getValue()
+ {
+ return value;
+ }
+
+ public String getDTDType()
+ {
+ return type;
+ }
+
+ public boolean isSpecified()
+ {
+ return specified;
+ }
+
+ public void writeAsEncodedUnicode(Writer writer)
+ throws XMLStreamException
+ {
+ try
+ {
+ String prefix = name.getPrefix();
+ if (prefix != null && !"".equals(prefix))
+ {
+ writer.write(prefix);
+ writer.write(':');
+ }
+ writer.write(name.getLocalPart());
+ writer.write('=');
+ writer.write('"');
+ writer.write(encode(value, true));
+ writer.write('"');
+ }
+ catch (IOException e)
+ {
+ XMLStreamException e2 = new XMLStreamException(e.getMessage());
+ e2.initCause(e);
+ throw e2;
+ }
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/stream/BufferedReader.java b/libjava/classpath/gnu/xml/stream/BufferedReader.java
new file mode 100644
index 000000000..dc69fb34b
--- /dev/null
+++ b/libjava/classpath/gnu/xml/stream/BufferedReader.java
@@ -0,0 +1,198 @@
+/* BufferedReader.java --
+ Copyright (C) 2005 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.stream;
+
+import java.io.IOException;
+import java.io.Reader;
+
+/**
+ * A mark-capable buffered reader.
+ *
+ * @author Chris Burdess
+ */
+class BufferedReader
+ extends Reader
+{
+
+ static final int DEFAULT_BUFFER_SIZE = 4096;
+
+ final Reader in;
+ char[] buf;
+ int pos, count, markpos, marklimit, bufferSize;
+
+ BufferedReader(Reader in)
+ {
+ this(in, DEFAULT_BUFFER_SIZE);
+ }
+
+ BufferedReader(Reader in, int bufferSize)
+ {
+ if (bufferSize < 1)
+ throw new IllegalArgumentException();
+ this.in = in;
+ this.bufferSize = bufferSize;
+ buf = new char[bufferSize];
+ pos = count = bufferSize;
+ }
+
+ public void close()
+ throws IOException
+ {
+ buf = null;
+ pos = count = 0;
+ markpos = -1;
+ in.close();
+ }
+
+ public void mark(int readlimit)
+ throws IOException
+ {
+ marklimit = readlimit;
+ markpos = pos;
+ }
+
+ public boolean markSupported()
+ {
+ return true;
+ }
+
+ public int read()
+ throws IOException
+ {
+ if (pos >= count && !refill())
+ return -1;
+ return (int) buf[pos++];
+ }
+
+ public int read(char[] b)
+ throws IOException
+ {
+ return read(b, 0, b.length);
+ }
+
+ public int read(char[] b, int off, int len)
+ throws IOException
+ {
+ if (off < 0 || len < 0 || b.length - off < len)
+ throw new IndexOutOfBoundsException();
+
+ if (len == 0)
+ return 0;
+
+ if (pos >= count && !refill())
+ return -1;
+
+ int ret = Math.min(count - pos, len);
+ System.arraycopy(buf, pos, b, off, ret);
+ pos += ret;
+ off += ret;
+ len -= ret;
+
+ while (len > 0 && refill())
+ {
+ int remain = Math.min(count - pos, len);
+ System.arraycopy(buf, pos, b, off, remain);
+ pos += remain;
+ off += remain;
+ len -= remain;
+ ret += remain;
+ }
+
+ return ret;
+ }
+
+ public void reset()
+ throws IOException
+ {
+ if (markpos == -1)
+ throw new IOException(buf == null ? "Stream closed." : "Invalid mark.");
+ pos = markpos;
+ }
+
+ public long skip(long n)
+ throws IOException
+ {
+ if (buf == null)
+ throw new IOException("Stream closed.");
+ final long origN = n;
+ while (n > 0L)
+ {
+ if (pos >= count && !refill())
+ break;
+ int numread = (int) Math.min((long) (count - pos), n);
+ pos += numread;
+ n -= numread;
+ }
+ return origN - n;
+ }
+
+ private boolean refill()
+ throws IOException
+ {
+ if (buf == null)
+ throw new IOException("Stream closed.");
+
+ int markcount = count - markpos;
+ if (markpos == -1 || markcount >= marklimit)
+ {
+ markpos = -1;
+ pos = count = 0;
+ }
+ else
+ {
+ char[] newbuf = buf;
+ if (markpos < bufferSize)
+ {
+ newbuf = new char[count - markpos + bufferSize];
+ }
+ System.arraycopy(buf, markpos, newbuf, 0, markcount);
+ buf = newbuf;
+ count = markcount;
+ pos -= markpos;
+ markpos = 0;
+ }
+
+ int numread = in.read(buf, count, bufferSize);
+ if (numread <= 0)
+ return false;
+
+ count += numread;
+ return true;
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/stream/CRLFReader.java b/libjava/classpath/gnu/xml/stream/CRLFReader.java
new file mode 100644
index 000000000..7f3cf4d74
--- /dev/null
+++ b/libjava/classpath/gnu/xml/stream/CRLFReader.java
@@ -0,0 +1,180 @@
+/* CRLFReader.java --
+ Copyright (C) 2005,2006 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.stream;
+
+import java.io.BufferedReader;
+import java.io.IOException;
+import java.io.Reader;
+
+/**
+ * Filtered reader that normalizes CRLF pairs into LFs.
+ *
+ * @author Chris Burdess
+ */
+class CRLFReader
+ extends Reader
+{
+
+ /**
+ * The CR octet.
+ */
+ public static final int CR = 13;
+
+ /**
+ * The LF octet.
+ */
+ public static final int LF = 10;
+
+ private boolean doReset;
+
+ protected Reader in;
+
+ /**
+ * Constructor.
+ */
+ protected CRLFReader(Reader in)
+ {
+ if (!in.markSupported())
+ in = new BufferedReader(in);
+ this.in = in;
+ }
+
+ public int read()
+ throws IOException
+ {
+ int c = in.read();
+ if (c == 13) // CR
+ {
+ in.mark(1);
+ int d = in.read();
+ if (d == 10) // LF
+ c = d;
+ else
+ in.reset();
+ }
+ return c;
+ }
+
+ public int read(char[] b)
+ throws IOException
+ {
+ return read(b, 0, b.length);
+ }
+
+ public int read(char[] b, int off, int len)
+ throws IOException
+ {
+ in.mark(len + 1);
+ int l = in.read(b, off, len);
+ if (l > 0)
+ {
+ int i = indexOfCRLF(b, off, l);
+ if (doReset)
+ {
+ in.reset();
+ if (i != -1)
+ {
+ l = in.read(b, off, (i + 1) - off); // read to CR
+ in.read(); // skip LF
+ b[i] = '\n'; // fix CR as LF
+ }
+ else
+ l = in.read(b, off, len); // CR(s) but no LF
+ }
+ }
+ return l;
+ }
+
+ public boolean markSupported()
+ {
+ return in.markSupported();
+ }
+
+ public void mark(int limit)
+ throws IOException
+ {
+ in.mark(limit);
+ }
+
+ public void reset()
+ throws IOException
+ {
+ in.reset();
+ }
+
+ public long skip(long n)
+ throws IOException
+ {
+ return in.skip(n);
+ }
+
+ public void close()
+ throws IOException
+ {
+ in.close();
+ }
+
+ private int indexOfCRLF(char[] b, int off, int len)
+ throws IOException
+ {
+ doReset = false;
+ int end = off + len;
+ int em1 = end - 1;
+ for (int i = off; i < end; i++)
+ {
+ if (b[i] == '\r') // CR
+ {
+ int d;
+ if (i == em1)
+ {
+ d = in.read();
+ doReset = true;
+ }
+ else
+ d = b[i + 1];
+ if (d == '\n') // LF
+ {
+ doReset = true;
+ return i;
+ }
+ }
+ }
+ return -1;
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/stream/CharactersImpl.java b/libjava/classpath/gnu/xml/stream/CharactersImpl.java
new file mode 100644
index 000000000..2107a5a6d
--- /dev/null
+++ b/libjava/classpath/gnu/xml/stream/CharactersImpl.java
@@ -0,0 +1,119 @@
+/* CharactersImpl.java --
+ Copyright (C) 2005 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.stream;
+
+import java.io.IOException;
+import java.io.Writer;
+import javax.xml.stream.Location;
+import javax.xml.stream.XMLStreamException;
+import javax.xml.stream.events.Characters;
+
+/**
+ * A character data (text) event.
+ *
+ * @author Chris Burdess
+ */
+public class CharactersImpl
+ extends XMLEventImpl
+ implements Characters
+{
+
+ protected final String data;
+ protected final boolean whitespace;
+ protected final boolean cdata;
+ protected final boolean ignorableWhitespace;
+
+ protected CharactersImpl(Location location,
+ String data, boolean whitespace, boolean cdata,
+ boolean ignorableWhitespace)
+ {
+ super(location);
+ this.data = data;
+ this.whitespace = whitespace;
+ this.cdata = cdata;
+ this.ignorableWhitespace = ignorableWhitespace;
+ }
+
+ public int getEventType()
+ {
+ return cdata ? CDATA : whitespace ? SPACE : CHARACTERS;
+ }
+
+ public String getData()
+ {
+ return data;
+ }
+
+ public boolean isWhiteSpace()
+ {
+ return whitespace;
+ }
+
+ public boolean isCData()
+ {
+ return cdata;
+ }
+
+ public boolean isIgnorableWhiteSpace()
+ {
+ return ignorableWhitespace;
+ }
+
+ public void writeAsEncodedUnicode(Writer writer)
+ throws XMLStreamException
+ {
+ try
+ {
+ if (cdata)
+ {
+ writer.write("");
+ }
+ else
+ writer.write(encode(data, false));
+ }
+ catch (IOException e)
+ {
+ XMLStreamException e2 = new XMLStreamException(e.getMessage());
+ e2.initCause(e);
+ throw e2;
+ }
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/stream/CommentImpl.java b/libjava/classpath/gnu/xml/stream/CommentImpl.java
new file mode 100644
index 000000000..118ac7a57
--- /dev/null
+++ b/libjava/classpath/gnu/xml/stream/CommentImpl.java
@@ -0,0 +1,91 @@
+/* CommentImpl.java --
+ Copyright (C) 2005 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.stream;
+
+import java.io.IOException;
+import java.io.Writer;
+import javax.xml.stream.Location;
+import javax.xml.stream.XMLStreamException;
+import javax.xml.stream.events.Comment;
+
+/**
+ * A comment event.
+ *
+ * @author Chris Burdess
+ */
+public class CommentImpl
+ extends XMLEventImpl
+ implements Comment
+{
+
+ protected final String text;
+
+ protected CommentImpl(Location location, String text)
+ {
+ super(location);
+ this.text = text;
+ }
+
+ public int getEventType()
+ {
+ return COMMENT;
+ }
+
+ public String getText()
+ {
+ return text;
+ }
+
+ public void writeAsEncodedUnicode(Writer writer)
+ throws XMLStreamException
+ {
+ try
+ {
+ writer.write("");
+ }
+ catch (IOException e)
+ {
+ XMLStreamException e2 = new XMLStreamException(e.getMessage());
+ e2.initCause(e);
+ throw e2;
+ }
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/stream/DTDImpl.java b/libjava/classpath/gnu/xml/stream/DTDImpl.java
new file mode 100644
index 000000000..cf049d362
--- /dev/null
+++ b/libjava/classpath/gnu/xml/stream/DTDImpl.java
@@ -0,0 +1,114 @@
+/* DTDImpl.java --
+ Copyright (C) 2005 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.stream;
+
+import java.io.IOException;
+import java.io.Writer;
+import java.util.List;
+import javax.xml.stream.Location;
+import javax.xml.stream.XMLStreamException;
+import javax.xml.stream.events.DTD;
+
+/**
+ * A DOCTYPE declaration event.
+ *
+ * @author Chris Burdess
+ */
+public class DTDImpl
+ extends XMLEventImpl
+ implements DTD
+{
+
+ protected final String body;
+ protected final Object impl;
+ protected final List notations;
+ protected final List entities;
+
+ protected DTDImpl(Location location,
+ String body, Object impl, List notations, List entities)
+ {
+ super(location);
+ this.body = body;
+ this.impl = impl;
+ this.notations = notations;
+ this.entities = entities;
+ }
+
+ public int getEventType()
+ {
+ return DTD;
+ }
+
+ public String getDocumentTypeDeclaration()
+ {
+ return body;
+ }
+
+ public Object getProcessedDTD()
+ {
+ return impl;
+ }
+
+ public List getNotations()
+ {
+ return notations;
+ }
+
+ public List getEntities()
+ {
+ return entities;
+ }
+
+ public void writeAsEncodedUnicode(Writer writer)
+ throws XMLStreamException
+ {
+ try
+ {
+ writer.write("");
+ }
+ catch (IOException e)
+ {
+ XMLStreamException e2 = new XMLStreamException(e.getMessage());
+ e2.initCause(e);
+ throw e2;
+ }
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/stream/EndDocumentImpl.java b/libjava/classpath/gnu/xml/stream/EndDocumentImpl.java
new file mode 100644
index 000000000..13877c5c9
--- /dev/null
+++ b/libjava/classpath/gnu/xml/stream/EndDocumentImpl.java
@@ -0,0 +1,70 @@
+/* EndDocumentImpl.java --
+ Copyright (C) 2005 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.stream;
+
+import java.io.Writer;
+import javax.xml.stream.Location;
+import javax.xml.stream.XMLStreamException;
+import javax.xml.stream.events.EndDocument;
+
+/**
+ * An end-document event.
+ *
+ * @author Chris Burdess
+ */
+public class EndDocumentImpl
+ extends XMLEventImpl
+ implements EndDocument
+{
+
+ protected EndDocumentImpl(Location location)
+ {
+ super(location);
+ }
+
+ public int getEventType()
+ {
+ return END_DOCUMENT;
+ }
+
+ public void writeAsEncodedUnicode(Writer writer)
+ throws XMLStreamException
+ {
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/stream/EndElementImpl.java b/libjava/classpath/gnu/xml/stream/EndElementImpl.java
new file mode 100644
index 000000000..71f6aa0bc
--- /dev/null
+++ b/libjava/classpath/gnu/xml/stream/EndElementImpl.java
@@ -0,0 +1,107 @@
+/* EndElementImpl.java --
+ Copyright (C) 2005 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.stream;
+
+import java.io.IOException;
+import java.io.Writer;
+import java.util.Iterator;
+import java.util.List;
+import javax.xml.namespace.QName;
+import javax.xml.stream.Location;
+import javax.xml.stream.XMLStreamException;
+import javax.xml.stream.events.EndElement;
+
+/**
+ * An end-element event.
+ *
+ * @author Chris Burdess
+ */
+public class EndElementImpl
+ extends XMLEventImpl
+ implements EndElement
+{
+
+ protected final QName name;
+ protected final List namespaces;
+
+ protected EndElementImpl(Location location, QName name, List namespaces)
+ {
+ super(location);
+ this.name = name;
+ this.namespaces = namespaces;
+ }
+
+ public int getEventType()
+ {
+ return END_ELEMENT;
+ }
+
+ public QName getName()
+ {
+ return name;
+ }
+
+ public Iterator getNamespaces()
+ {
+ return namespaces.iterator();
+ }
+
+ public void writeAsEncodedUnicode(Writer writer)
+ throws XMLStreamException
+ {
+ try
+ {
+ writer.write("");
+ String prefix = name.getPrefix();
+ if (prefix != null && !"".equals(prefix))
+ {
+ writer.write(prefix);
+ writer.write(':');
+ }
+ writer.write(name.getLocalPart());
+ writer.write(">");
+ }
+ catch (IOException e)
+ {
+ XMLStreamException e2 = new XMLStreamException(e.getMessage());
+ e2.initCause(e);
+ throw e2;
+ }
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/stream/EntityDeclarationImpl.java b/libjava/classpath/gnu/xml/stream/EntityDeclarationImpl.java
new file mode 100644
index 000000000..b6e33e8d2
--- /dev/null
+++ b/libjava/classpath/gnu/xml/stream/EntityDeclarationImpl.java
@@ -0,0 +1,163 @@
+/* EntityDeclarationImpl.java --
+ Copyright (C) 2005 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.stream;
+
+import java.io.IOException;
+import java.io.Writer;
+import javax.xml.stream.Location;
+import javax.xml.stream.XMLStreamException;
+import javax.xml.stream.events.EntityDeclaration;
+
+/**
+ * An entity declaration event.
+ *
+ * @author Chris Burdess
+ */
+public class EntityDeclarationImpl
+ extends XMLEventImpl
+ implements EntityDeclaration
+{
+
+ protected final String publicId;
+ protected final String systemId;
+ protected final String name;
+ protected final String notationName;
+ protected final String replacementText;
+ protected final String baseUri;
+
+ protected EntityDeclarationImpl(Location location,
+ String publicId, String systemId,
+ String name, String notationName,
+ String replacementText, String baseUri)
+ {
+ super(location);
+ this.publicId = publicId;
+ this.systemId = systemId;
+ this.name = name;
+ this.notationName = notationName;
+ this.replacementText = replacementText;
+ this.baseUri = baseUri;
+ }
+
+ public int getEventType()
+ {
+ return ENTITY_DECLARATION;
+ }
+
+ public String getPublicId()
+ {
+ return publicId;
+ }
+
+ public String getSystemId()
+ {
+ return systemId;
+ }
+
+ public String getName()
+ {
+ return name;
+ }
+
+ public String getNotationName()
+ {
+ return notationName;
+ }
+
+ public String getReplacementText()
+ {
+ return replacementText;
+ }
+
+ public String getBaseURI()
+ {
+ return baseUri;
+ }
+
+ public void writeAsEncodedUnicode(Writer writer)
+ throws XMLStreamException
+ {
+ try
+ {
+ writer.write("");
+ }
+ catch (IOException e)
+ {
+ XMLStreamException e2 = new XMLStreamException(e.getMessage());
+ e2.initCause(e);
+ throw e2;
+ }
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/stream/EntityReferenceImpl.java b/libjava/classpath/gnu/xml/stream/EntityReferenceImpl.java
new file mode 100644
index 000000000..6f8a11c80
--- /dev/null
+++ b/libjava/classpath/gnu/xml/stream/EntityReferenceImpl.java
@@ -0,0 +1,101 @@
+/* EntityReferenceImpl.java --
+ Copyright (C) 2005,2006 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.stream;
+
+import java.io.IOException;
+import java.io.Writer;
+import javax.xml.stream.Location;
+import javax.xml.stream.XMLStreamException;
+import javax.xml.stream.events.EntityDeclaration;
+import javax.xml.stream.events.EntityReference;
+
+/**
+ * An entity reference event.
+ *
+ * @author Chris Burdess
+ */
+public class EntityReferenceImpl
+ extends XMLEventImpl
+ implements EntityReference
+{
+
+ protected final EntityDeclaration decl;
+ protected final String name;
+
+ protected EntityReferenceImpl(Location location,
+ EntityDeclaration decl,
+ String name)
+ {
+ super(location);
+ this.decl = decl;
+ this.name = name;
+ }
+
+ public int getEventType()
+ {
+ return ENTITY_REFERENCE;
+ }
+
+ public EntityDeclaration getDeclaration()
+ {
+ return decl;
+ }
+
+ public String getName()
+ {
+ return name;
+ }
+
+ public void writeAsEncodedUnicode(Writer writer)
+ throws XMLStreamException
+ {
+ try
+ {
+ writer.write('&');
+ writer.write(name);
+ writer.write(';');
+ }
+ catch (IOException e)
+ {
+ XMLStreamException e2 = new XMLStreamException(e.getMessage());
+ e2.initCause(e);
+ throw e2;
+ }
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/stream/FilteredEventReader.java b/libjava/classpath/gnu/xml/stream/FilteredEventReader.java
new file mode 100644
index 000000000..1ddb469df
--- /dev/null
+++ b/libjava/classpath/gnu/xml/stream/FilteredEventReader.java
@@ -0,0 +1,114 @@
+/* FilteredEventReader.java --
+ Copyright (C) 2005,2006 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.stream;
+
+import javax.xml.stream.EventFilter;
+import javax.xml.stream.XMLEventReader;
+import javax.xml.stream.XMLStreamException;
+import javax.xml.stream.events.XMLEvent;
+import javax.xml.stream.util.EventReaderDelegate;
+
+class FilteredEventReader
+ extends EventReaderDelegate
+{
+
+ final EventFilter filter;
+
+ FilteredEventReader(XMLEventReader reader, EventFilter filter)
+ {
+ super(reader);
+ this.filter = filter;
+ }
+
+ public boolean hasNext()
+ {
+ // XXX ???
+ return super.hasNext();
+ }
+
+ public XMLEvent nextEvent()
+ throws XMLStreamException
+ {
+ XMLEvent ret;
+ do
+ {
+ ret = super.nextEvent();
+ }
+ while (!filter.accept(ret));
+ return ret;
+ }
+
+ public Object next()
+ {
+ try
+ {
+ return nextEvent();
+ }
+ catch (XMLStreamException e)
+ {
+ RuntimeException e2 = new RuntimeException();
+ e2.initCause(e);
+ throw e2;
+ }
+ }
+
+ public XMLEvent peek()
+ throws XMLStreamException
+ {
+ XMLEvent ret;
+ do
+ {
+ ret = super.peek();
+ }
+ while (!filter.accept(ret));
+ return ret;
+ }
+
+ public XMLEvent nextTag()
+ throws XMLStreamException
+ {
+ XMLEvent ret;
+ do
+ {
+ ret = super.nextTag();
+ }
+ while (!filter.accept(ret));
+ return ret;
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/stream/FilteredStreamReader.java b/libjava/classpath/gnu/xml/stream/FilteredStreamReader.java
new file mode 100644
index 000000000..62d96488e
--- /dev/null
+++ b/libjava/classpath/gnu/xml/stream/FilteredStreamReader.java
@@ -0,0 +1,90 @@
+/* FilteredStreamReader.java --
+ Copyright (C) 2005 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.stream;
+
+import javax.xml.stream.StreamFilter;
+import javax.xml.stream.XMLStreamException;
+import javax.xml.stream.XMLStreamReader;
+import javax.xml.stream.util.StreamReaderDelegate;
+
+class FilteredStreamReader
+ extends StreamReaderDelegate
+{
+
+ final XMLStreamReader reader;
+ final StreamFilter filter;
+
+ FilteredStreamReader(XMLStreamReader reader, StreamFilter filter)
+ {
+ super(reader);
+ this.reader = reader;
+ this.filter = filter;
+ }
+
+ public boolean hasNext()
+ throws XMLStreamException
+ {
+ // XXX ???
+ return super.hasNext();
+ }
+
+ public int next()
+ throws XMLStreamException
+ {
+ int ret;
+ do
+ {
+ ret = super.next();
+ }
+ while (!filter.accept(reader));
+ return ret;
+ }
+
+ public int nextTag()
+ throws XMLStreamException
+ {
+ int ret;
+ do
+ {
+ ret = super.nextTag();
+ }
+ while (!filter.accept(reader));
+ return ret;
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/stream/NamespaceImpl.java b/libjava/classpath/gnu/xml/stream/NamespaceImpl.java
new file mode 100644
index 000000000..241f980b6
--- /dev/null
+++ b/libjava/classpath/gnu/xml/stream/NamespaceImpl.java
@@ -0,0 +1,137 @@
+/* NamespaceImpl.java --
+ Copyright (C) 2005 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.stream;
+
+import java.io.IOException;
+import java.io.Writer;
+import javax.xml.namespace.QName;
+import javax.xml.stream.Location;
+import javax.xml.stream.XMLStreamException;
+import javax.xml.stream.events.Namespace;
+
+/**
+ * A namespace declaration event.
+ *
+ * @author Chris Burdess
+ */
+public class NamespaceImpl
+ extends XMLEventImpl
+ implements Namespace
+{
+
+ protected final String prefix;
+ protected final String uri;
+ protected final boolean specified;
+
+ protected NamespaceImpl(Location location, String prefix, String uri,
+ boolean specified)
+ {
+ super(location);
+ this.prefix = prefix;
+ this.uri = uri;
+ this.specified = specified;
+ }
+
+ public int getEventType()
+ {
+ return NAMESPACE;
+ }
+
+ public String getPrefix()
+ {
+ return prefix;
+ }
+
+ public String getNamespaceURI()
+ {
+ return uri;
+ }
+
+ public boolean isSpecified()
+ {
+ return specified;
+ }
+
+ public QName getName()
+ {
+ if (isDefaultNamespaceDeclaration())
+ return new QName("", "xmlns", null);
+ else
+ return new QName("", prefix, "xmlns");
+ }
+
+ public String getDTDType()
+ {
+ return "CDATA";
+ }
+
+ public String getValue()
+ {
+ return uri;
+ }
+
+ public boolean isDefaultNamespaceDeclaration()
+ {
+ return (prefix == null || "".equals(prefix));
+ }
+
+ public void writeAsEncodedUnicode(Writer writer)
+ throws XMLStreamException
+ {
+ try
+ {
+ writer.write("xmlns");
+ if (prefix != null && !"".equals(prefix))
+ {
+ writer.write(':');
+ writer.write(prefix);
+ }
+ writer.write('=');
+ writer.write('"');
+ writer.write(encode(uri, true));
+ writer.write('"');
+ }
+ catch (IOException e)
+ {
+ XMLStreamException e2 = new XMLStreamException(e.getMessage());
+ e2.initCause(e);
+ throw e2;
+ }
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/stream/NotationDeclarationImpl.java b/libjava/classpath/gnu/xml/stream/NotationDeclarationImpl.java
new file mode 100644
index 000000000..a338237ee
--- /dev/null
+++ b/libjava/classpath/gnu/xml/stream/NotationDeclarationImpl.java
@@ -0,0 +1,125 @@
+/* NotationDeclarationImpl.java --
+ Copyright (C) 2005 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.stream;
+
+import java.io.IOException;
+import java.io.Writer;
+import javax.xml.stream.Location;
+import javax.xml.stream.XMLStreamException;
+import javax.xml.stream.events.NotationDeclaration;
+
+/**
+ * A notation declaration event.
+ *
+ * @author Chris Burdess
+ */
+public class NotationDeclarationImpl
+ extends XMLEventImpl
+ implements NotationDeclaration
+{
+
+ protected final String name;
+ protected final String publicId;
+ protected final String systemId;
+
+ protected NotationDeclarationImpl(Location location,
+ String name, String publicId,
+ String systemId)
+ {
+ super(location);
+ this.name = name;
+ this.publicId = publicId;
+ this.systemId = systemId;
+ }
+
+ public int getEventType()
+ {
+ return NOTATION_DECLARATION;
+ }
+
+ public String getName()
+ {
+ return name;
+ }
+
+ public String getPublicId()
+ {
+ return publicId;
+ }
+
+ public String getSystemId()
+ {
+ return systemId;
+ }
+
+ public void writeAsEncodedUnicode(Writer writer)
+ throws XMLStreamException
+ {
+ try
+ {
+ writer.write("');
+ }
+ catch (IOException e)
+ {
+ XMLStreamException e2 = new XMLStreamException(e.getMessage());
+ e2.initCause(e);
+ throw e2;
+ }
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/stream/ProcessingInstructionImpl.java b/libjava/classpath/gnu/xml/stream/ProcessingInstructionImpl.java
new file mode 100644
index 000000000..4ce0badc8
--- /dev/null
+++ b/libjava/classpath/gnu/xml/stream/ProcessingInstructionImpl.java
@@ -0,0 +1,104 @@
+/* ProcessingInstructionImpl.java --
+ Copyright (C) 2005 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.stream;
+
+import java.io.IOException;
+import java.io.Writer;
+import javax.xml.stream.Location;
+import javax.xml.stream.XMLStreamException;
+import javax.xml.stream.events.ProcessingInstruction;
+
+/**
+ * A processing instruction event.
+ *
+ * @author Chris Burdess
+ */
+public class ProcessingInstructionImpl
+ extends XMLEventImpl
+ implements ProcessingInstruction
+{
+
+ protected final String target;
+ protected final String data;
+
+ protected ProcessingInstructionImpl(Location location,
+ String target, String data)
+ {
+ super(location);
+ this.target = target;
+ this.data = data;
+ }
+
+ public int getEventType()
+ {
+ return PROCESSING_INSTRUCTION;
+ }
+
+ public String getTarget()
+ {
+ return target;
+ }
+
+ public String getData()
+ {
+ return data;
+ }
+
+ public void writeAsEncodedUnicode(Writer writer)
+ throws XMLStreamException
+ {
+ try
+ {
+ writer.write("");
+ writer.write(target);
+ if (data != null)
+ {
+ writer.write(' ');
+ writer.write(data);
+ }
+ writer.write("?>");
+ }
+ catch (IOException e)
+ {
+ XMLStreamException e2 = new XMLStreamException(e.getMessage());
+ e2.initCause(e);
+ throw e2;
+ }
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/stream/SAXParser.java b/libjava/classpath/gnu/xml/stream/SAXParser.java
new file mode 100644
index 000000000..b71d98ed3
--- /dev/null
+++ b/libjava/classpath/gnu/xml/stream/SAXParser.java
@@ -0,0 +1,1041 @@
+/* SAXParser.java --
+ Copyright (C) 2005, 2006, 2007 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.stream;
+
+import java.io.InputStream;
+import java.io.IOException;
+import java.io.Reader;
+import java.net.URL;
+import java.util.Iterator;
+import java.util.Map;
+import javax.xml.XMLConstants;
+import javax.xml.namespace.QName;
+import javax.xml.stream.Location;
+import javax.xml.stream.XMLEventReader;
+import javax.xml.stream.XMLReporter;
+import javax.xml.stream.XMLResolver;
+import javax.xml.stream.XMLStreamConstants;
+import javax.xml.stream.XMLStreamException;
+import javax.xml.stream.XMLStreamReader;
+
+import org.xml.sax.ContentHandler;
+import org.xml.sax.DTDHandler;
+import org.xml.sax.EntityResolver;
+import org.xml.sax.ErrorHandler;
+import org.xml.sax.InputSource;
+import org.xml.sax.Parser;
+import org.xml.sax.SAXException;
+import org.xml.sax.SAXNotRecognizedException;
+import org.xml.sax.SAXNotSupportedException;
+import org.xml.sax.SAXParseException;
+import org.xml.sax.XMLReader;
+import org.xml.sax.ext.Attributes2;
+import org.xml.sax.ext.DeclHandler;
+import org.xml.sax.ext.LexicalHandler;
+import org.xml.sax.ext.Locator2;
+
+/**
+ * JAXP SAX parser using an underlying StAX parser.
+ * This parser supports the following additional SAX features and
+ * properties:
+ * Programming Models
+
+
+
+
+
+
+
+
+
+
+
+
+
+ Producers: XMLReader or Custom
+
+org.xml.sax.helpers.XMLReaderFactory
) or a DOM tree
+(using a DomParser
)
+These may be bound to event consumer using a convenience routine,
+EventFilter.bind().
+Once bound, these producers may be given additional documents to
+sent through its pipeline.
+
+ Consume to Standard or Custom Data Representations
+
+Pipe Fitting
+
+
+
+
+
+ Coding Conventions: Filter and Terminus Stages
+
+
+
+
+
+ Debugging Tip: "Tee" Joints can Snapshot Data
+
+ Debugging Tip: Non-XML Producers
+
+
+ *
+ *
+ * @author Chris Burdess
+ */
+public class SAXParser
+ extends javax.xml.parsers.SAXParser
+ implements XMLReader, Attributes2, Locator2, XMLReporter, XMLResolver
+{
+
+ ContentHandler contentHandler;
+ DeclHandler declHandler;
+ DTDHandler dtdHandler;
+ EntityResolver entityResolver;
+ ErrorHandler errorHandler;
+ LexicalHandler lexicalHandler;
+
+ boolean validating = false;
+ boolean namespaceAware = true;
+ boolean xIncludeAware = false;
+ boolean stringInterning = true;
+ boolean coalescing = true;
+ boolean replaceERefs = true;
+ boolean externalEntities = true;
+ boolean supportDTD = true;
+ boolean baseAware = true;
+
+ XMLParser parser;
+ XMLStreamReader reader;
+ String encoding;
+ String xmlVersion;
+ boolean xmlStandalone;
+ String xmlEncoding;
+ String baseURI;
+
+ public SAXParser()
+ {
+ }
+
+ SAXParser(boolean validating, boolean namespaceAware, boolean xIncludeAware)
+ {
+ this.validating = validating;
+ this.namespaceAware = namespaceAware;
+ this.xIncludeAware = xIncludeAware;
+ }
+
+ // -- SAXParser --
+
+ public Parser getParser()
+ throws SAXException
+ {
+ return null;
+ }
+
+ public XMLReader getXMLReader()
+ throws SAXException
+ {
+ return this;
+ }
+
+ public boolean isNamespaceAware()
+ {
+ return namespaceAware;
+ }
+
+ public boolean isValidating()
+ {
+ return validating;
+ }
+
+ public void setProperty(String name, Object value)
+ throws SAXNotRecognizedException, SAXNotSupportedException
+ {
+ if (parser != null)
+ throw new IllegalStateException("parsing in progress");
+ final String FEATURES = "http://xml.org/sax/features/";
+ final String PROPERTIES = "http://xml.org/sax/properties/";
+ final String GNU_FEATURES = "http://gnu.org/sax/features/";
+ if ((FEATURES + "namespaces").equals(name))
+ namespaceAware = Boolean.TRUE.equals(value);
+ else if ((FEATURES + "namespace-prefixes").equals(name))
+ {
+ // NOOP
+ }
+ else if ((FEATURES + "string-interning").equals(name))
+ stringInterning = Boolean.TRUE.equals(value);
+ else if ((FEATURES + "use-attributes2").equals(name))
+ {
+ // NOOP
+ }
+ else if ((FEATURES + "validation").equals(name))
+ validating = Boolean.TRUE.equals(value);
+ else if ((FEATURES + "external-general-entities").equals(name))
+ externalEntities = Boolean.TRUE.equals(value);
+ else if ((FEATURES + "external-parameter-entities").equals(name))
+ externalEntities = Boolean.TRUE.equals(value);
+ else if ((PROPERTIES + "declaration-handler").equals(name))
+ declHandler = (DeclHandler) value;
+ else if ((PROPERTIES + "lexical-handler").equals(name))
+ lexicalHandler = (LexicalHandler) value;
+ else if ((GNU_FEATURES + "xml-base").equals(name))
+ baseAware = Boolean.TRUE.equals(value);
+ else if ((GNU_FEATURES + "coalescing").equals(name))
+ coalescing = Boolean.TRUE.equals(value);
+ else
+ throw new SAXNotSupportedException(name);
+ }
+
+ public Object getProperty(String name)
+ throws SAXNotRecognizedException, SAXNotSupportedException
+ {
+ final String FEATURES = "http://xml.org/sax/features/";
+ final String PROPERTIES = "http://xml.org/sax/properties/";
+ final String GNU_FEATURES = "http://gnu.org/sax/features/";
+ final String GNU_PROPERTIES = "http://gnu.org/sax/properties/";
+ if ((GNU_FEATURES + "base-uri").equals(name))
+ return baseURI;
+ if ((FEATURES + "is-standalone").equals(name))
+ return xmlStandalone ? Boolean.TRUE : Boolean.FALSE;
+ if ((FEATURES + "namespaces").equals(name))
+ return namespaceAware ? Boolean.TRUE : Boolean.FALSE;
+ if ((FEATURES + "namespace-prefixes").equals(name))
+ return Boolean.TRUE;
+ if ((FEATURES + "string-interning").equals(name))
+ return stringInterning ? Boolean.TRUE : Boolean.FALSE;
+ if ((FEATURES + "use-attributes2").equals(name))
+ return Boolean.TRUE;
+ if ((FEATURES + "use-locator2").equals(name))
+ return Boolean.TRUE;
+ if ((FEATURES + "use-entity-resolver2").equals(name))
+ return Boolean.FALSE;
+ if ((FEATURES + "validation").equals(name))
+ return validating ? Boolean.TRUE : Boolean.FALSE;
+ if ((FEATURES + "external-general-entities").equals(name))
+ return externalEntities ? Boolean.TRUE : Boolean.FALSE;
+ if ((FEATURES + "external-parameter-entities").equals(name))
+ return externalEntities ? Boolean.TRUE : Boolean.FALSE;
+ if ((FEATURES + "xml-1.1").equals(name))
+ return Boolean.TRUE;
+ if ((PROPERTIES + "declaration-handler").equals(name))
+ return declHandler;
+ if ((PROPERTIES + "document-xml-version").equals(name))
+ return xmlVersion;
+ if ((PROPERTIES + "lexical-handler").equals(name))
+ return lexicalHandler;
+ if ((GNU_FEATURES + "xml-base").equals(name))
+ return baseAware ? Boolean.TRUE : Boolean.FALSE;
+ if ((GNU_PROPERTIES + "document-xml-encoding").equals(name))
+ return xmlEncoding;
+ throw new SAXNotRecognizedException(name);
+ }
+
+ public boolean isXIncludeAware()
+ {
+ return xIncludeAware;
+ }
+
+ public void reset()
+ {
+ parser = null;
+ encoding = null;
+ xmlVersion = null;
+ xmlStandalone = false;
+ }
+
+ // -- XMLReader --
+
+ public boolean getFeature(String name)
+ throws SAXNotRecognizedException, SAXNotSupportedException
+ {
+ Object ret = getProperty(name);
+ if (ret instanceof Boolean)
+ return ((Boolean) ret).booleanValue();
+ throw new SAXNotSupportedException(name);
+ }
+
+ public void setFeature(String name, boolean value)
+ throws SAXNotRecognizedException, SAXNotSupportedException
+ {
+ setProperty(name, value ? Boolean.TRUE : Boolean.FALSE);
+ }
+
+ public void setEntityResolver(EntityResolver resolver)
+ {
+ entityResolver = resolver;
+ }
+
+ public EntityResolver getEntityResolver()
+ {
+ return entityResolver;
+ }
+
+ public void setDTDHandler(DTDHandler handler)
+ {
+ dtdHandler = handler;
+ }
+
+ public DTDHandler getDTDHandler()
+ {
+ return dtdHandler;
+ }
+
+ public void setContentHandler(ContentHandler handler)
+ {
+ contentHandler = handler;
+ }
+
+ public ContentHandler getContentHandler()
+ {
+ return contentHandler;
+ }
+
+ public void setErrorHandler(ErrorHandler handler)
+ {
+ errorHandler = handler;
+ }
+
+ public ErrorHandler getErrorHandler()
+ {
+ return errorHandler;
+ }
+
+ public synchronized void parse(InputSource input)
+ throws IOException, SAXException
+ {
+ reset();
+ String systemId = input.getSystemId();
+ InputStream in = input.getByteStream();
+ boolean opened = false;
+ if (in != null)
+ parser = new XMLParser(in, systemId,
+ validating,
+ namespaceAware,
+ coalescing,
+ replaceERefs,
+ externalEntities,
+ supportDTD,
+ baseAware,
+ stringInterning,
+ true,
+ this,
+ this);
+ else
+ {
+ Reader r = input.getCharacterStream();
+ if (r != null)
+ parser = new XMLParser(r, systemId,
+ validating,
+ namespaceAware,
+ coalescing,
+ replaceERefs,
+ externalEntities,
+ supportDTD,
+ baseAware,
+ stringInterning,
+ true,
+ this,
+ this);
+ }
+ if (parser == null)
+ {
+ if (systemId == null)
+ throw new SAXException("No stream or system ID specified");
+ systemId = XMLParser.absolutize(null, systemId);
+ in = new URL(systemId).openStream();
+ opened = true;
+ parser = new XMLParser(in, systemId,
+ validating,
+ namespaceAware,
+ coalescing,
+ replaceERefs,
+ externalEntities,
+ supportDTD,
+ baseAware,
+ stringInterning,
+ true,
+ this,
+ this);
+ }
+ reader = parser;
+ baseURI = systemId;
+
+ if (xIncludeAware)
+ reader = new XIncludeFilter(parser, systemId, namespaceAware,
+ validating, true);
+
+ if (contentHandler != null)
+ contentHandler.setDocumentLocator(this);
+ boolean startDocumentDone = false;
+ try
+ {
+ while (parser.hasNext())
+ {
+ int event = parser.next();
+ if (baseAware)
+ baseURI = parser.getXMLBase();
+ switch (event)
+ {
+ case XMLStreamConstants.CHARACTERS:
+ if (contentHandler != null)
+ {
+ char[] b = reader.getTextCharacters();
+ contentHandler.characters(b, 0, b.length);
+ }
+ break;
+ case XMLStreamConstants.SPACE:
+ if (contentHandler != null)
+ {
+ char[] b = reader.getTextCharacters();
+ if (isIgnorableWhitespace(parser, b, false))
+ contentHandler.ignorableWhitespace(b, 0, b.length);
+ else
+ contentHandler.characters(b, 0, b.length);
+ }
+ break;
+ case XMLStreamConstants.CDATA:
+ if (lexicalHandler != null)
+ lexicalHandler.startCDATA();
+ if (contentHandler != null)
+ {
+ char[] b = reader.getTextCharacters();
+ if (isIgnorableWhitespace(parser, b, true))
+ contentHandler.ignorableWhitespace(b, 0, b.length);
+ else
+ contentHandler.characters(b, 0, b.length);
+ }
+ if (lexicalHandler != null)
+ lexicalHandler.endCDATA();
+ break;
+ case XMLStreamConstants.START_ELEMENT:
+ if (contentHandler != null)
+ {
+ QName name = reader.getName();
+ String uri = name.getNamespaceURI();
+ String localName = name.getLocalPart();
+ String prefix = name.getPrefix();
+ String qName = localName;
+ if (!"".equals(prefix))
+ qName = prefix + ":" + localName;
+ if (!namespaceAware)
+ {
+ uri = "";
+ localName = "";
+ }
+ else
+ {
+ int nc = reader.getNamespaceCount();
+ for (int i = 0; i < nc; i++)
+ {
+ String nsuri = reader.getNamespaceURI(i);
+ String nsprefix = reader.getNamespacePrefix(i);
+ if ("xml".equals(nsprefix))
+ continue;
+ contentHandler.startPrefixMapping(nsprefix, nsuri);
+ }
+ }
+ contentHandler.startElement(uri, localName, qName, this);
+ }
+ break;
+ case XMLStreamConstants.END_ELEMENT:
+ if (contentHandler != null)
+ {
+ QName name = reader.getName();
+ String uri = name.getNamespaceURI();
+ String localName = name.getLocalPart();
+ String prefix = name.getPrefix();
+ String qName = localName;
+ if (!"".equals(prefix))
+ qName = prefix + ":" + localName;
+ if (!namespaceAware)
+ {
+ uri = "";
+ localName = "";
+ }
+ contentHandler.endElement(uri, localName, qName);
+ if (namespaceAware)
+ {
+ int nc = reader.getNamespaceCount();
+ for (int i = 0; i < nc; i++)
+ {
+ String nsprefix = reader.getNamespacePrefix(i);
+ if ("xml".equals(nsprefix))
+ continue;
+ contentHandler.endPrefixMapping(nsprefix);
+ }
+ }
+ }
+ break;
+ case XMLStreamConstants.COMMENT:
+ if (lexicalHandler != null)
+ {
+ char[] b = reader.getTextCharacters();
+ lexicalHandler.comment(b, 0, b.length);
+ }
+ break;
+ case XMLStreamConstants.PROCESSING_INSTRUCTION:
+ if (contentHandler != null)
+ {
+ String target = reader.getPITarget();
+ String data = reader.getPIData();
+ if (data == null)
+ data = "";
+ contentHandler.processingInstruction(target, data);
+ }
+ break;
+ case XMLParser.START_ENTITY:
+ if (lexicalHandler != null)
+ {
+ String name = reader.getText();
+ lexicalHandler.startEntity(name);
+ }
+ break;
+ case XMLParser.END_ENTITY:
+ if (lexicalHandler != null)
+ {
+ String name = reader.getText();
+ lexicalHandler.endEntity(name);
+ }
+ break;
+ case XMLStreamConstants.START_DOCUMENT:
+ encoding = reader.getEncoding();
+ xmlVersion = reader.getVersion();
+ xmlStandalone = reader.isStandalone();
+ xmlEncoding = reader.getCharacterEncodingScheme();
+ if (contentHandler != null)
+ contentHandler.startDocument();
+ startDocumentDone = true;
+ break;
+ case XMLStreamConstants.END_DOCUMENT:
+ if (contentHandler != null)
+ contentHandler.endDocument();
+ break;
+ case XMLStreamConstants.DTD:
+ XMLParser.Doctype doctype = parser.doctype;
+ if (lexicalHandler != null)
+ {
+ String rootName = doctype.rootName;
+ String publicId = doctype.publicId;
+ String systemId2 = doctype.systemId;
+ lexicalHandler.startDTD(rootName, publicId, systemId2);
+ }
+ for (Iterator i = doctype.entryIterator(); i.hasNext(); )
+ {
+ String entry = (String) i.next();
+ char c = entry.charAt(0);
+ String name = entry.substring(1);
+ if ('E' == c)
+ {
+ // Element decl
+ if (declHandler != null)
+ {
+ XMLParser.ContentModel model =
+ doctype.getElementModel(name);
+ declHandler.elementDecl(name, model.text);
+ }
+ }
+ else if ('A' == c)
+ {
+ // Attlist decl
+ if (declHandler != null)
+ {
+ for (Iterator j = doctype.attlistIterator(name);
+ j.hasNext(); )
+ {
+ Map.Entry att = (Map.Entry) j.next();
+ String aname = (String) att.getKey();
+ XMLParser.AttributeDecl decl =
+ (XMLParser.AttributeDecl) att.getValue();
+ String type = decl.type;
+ String value = decl.value;
+ String mode = null;
+ switch (decl.valueType)
+ {
+ case XMLParser.ATTRIBUTE_DEFAULT_FIXED:
+ mode = "#FIXED";
+ break;
+ case XMLParser.ATTRIBUTE_DEFAULT_REQUIRED:
+ mode = "#REQUIRED";
+ break;
+ case XMLParser.ATTRIBUTE_DEFAULT_IMPLIED:
+ mode = "#IMPLIED";
+ break;
+ }
+ declHandler.attributeDecl(name, aname,
+ type, mode, value);
+ }
+ }
+ }
+ else if ('e' == c)
+ {
+ // Entity decl
+ Object entity = doctype.getEntity(name);
+ if (entity instanceof String)
+ {
+ if (declHandler != null)
+ declHandler.internalEntityDecl(name,
+ (String) entity);
+ }
+ else
+ {
+ XMLParser.ExternalIds ids =
+ (XMLParser.ExternalIds) entity;
+ if (ids.notationName != null)
+ {
+ if (dtdHandler != null)
+ {
+ String pub = ids.publicId;
+ String url = ids.systemId;
+ String not = ids.notationName;
+ dtdHandler.unparsedEntityDecl(name,
+ pub,
+ url,
+ not);
+ }
+ }
+ else
+ {
+ if (declHandler != null)
+ {
+ String pub = ids.publicId;
+ String url = ids.systemId;
+ declHandler.externalEntityDecl(name,
+ pub,
+ url);
+ }
+ }
+ }
+ }
+ else if ('n' == c)
+ {
+ // Notation decl
+ if (dtdHandler != null)
+ {
+ XMLParser.ExternalIds ids =
+ doctype.getNotation(name);
+ String pub = ids.publicId;
+ String url = ids.systemId;
+ dtdHandler.notationDecl(name, pub, url);
+ }
+ }
+ else if ('c' == c)
+ {
+ // Comment
+ if (lexicalHandler != null)
+ {
+ String comment = doctype.getComment(name);
+ char[] b = comment.toCharArray();
+ lexicalHandler.comment(b, 0, b.length);
+ }
+ }
+ else if ('p' == c)
+ {
+ // Processing instruction
+ if (contentHandler != null)
+ {
+ String[] pi = doctype.getPI(name);
+ String target = pi[0];
+ String data = pi[1];
+ if (data == null)
+ data = "";
+ contentHandler.processingInstruction(target, data);
+ }
+ }
+ }
+ if (lexicalHandler != null)
+ lexicalHandler.endDTD();
+ }
+ }
+ reset();
+ if (opened)
+ in.close();
+ }
+ catch (Exception e)
+ {
+ SAXParseException e2 = new SAXParseException(e.getMessage(), this);
+ e2.initCause(e);
+ try
+ {
+ if (!startDocumentDone && contentHandler != null)
+ contentHandler.startDocument();
+ if (errorHandler != null)
+ errorHandler.fatalError(e2);
+ if (contentHandler != null)
+ contentHandler.endDocument();
+ }
+ catch (SAXException sex)
+ {
+ // Ignored, we will rethrow the original exception.
+ }
+ reset();
+ if (opened)
+ in.close();
+ if (e instanceof SAXException)
+ throw (SAXException) e;
+ if (e instanceof IOException)
+ throw (IOException) e;
+ else
+ throw e2;
+ }
+ }
+
+ /**
+ * Indicates whether the specified characters are ignorable whitespace.
+ */
+ private boolean isIgnorableWhitespace(XMLParser reader, char[] b,
+ boolean testCharacters)
+ throws XMLStreamException
+ {
+ XMLParser.Doctype doctype = reader.doctype;
+ if (doctype == null)
+ return false;
+ String currentElement = reader.getCurrentElement();
+ // check for xml:space
+ int ac = reader.getAttributeCount();
+ for (int i = 0; i < ac; i++)
+ {
+ QName aname = reader.getAttributeName(i);
+ if ("space".equals(aname.getLocalPart()) &&
+ XMLConstants.XML_NS_URI.equals(aname.getNamespaceURI()))
+ {
+ if ("preserve".equals(reader.getAttributeValue(i)))
+ return false;
+ }
+ }
+ XMLParser.ContentModel model = doctype.getElementModel(currentElement);
+ if (model == null || model.type != XMLParser.ContentModel.ELEMENT)
+ return false;
+ if (model.external && xmlStandalone)
+ return false;
+ boolean white = true;
+ if (testCharacters)
+ {
+ for (int i = 0; i < b.length; i++)
+ {
+ if (b[i] != ' ' && b[i] != '\t' && b[i] != '\n' && b[i] != '\r')
+ {
+ white = false;
+ break;
+ }
+ }
+ }
+ return white;
+ }
+
+ public void parse(String systemId)
+ throws IOException, SAXException
+ {
+ parse(new InputSource(systemId));
+ }
+
+ // -- Attributes2 --
+
+ public int getIndex(String qName)
+ {
+ int len = reader.getAttributeCount();
+ for (int i = 0; i < len; i++)
+ {
+ QName q = reader.getAttributeName(i);
+ String localName = q.getLocalPart();
+ String prefix = q.getPrefix();
+ String qn = ("".equals(prefix)) ? localName : prefix + ":" + localName;
+ if (qName.equals(qn))
+ return i;
+ }
+ return -1;
+ }
+
+ public int getIndex(String uri, String localName)
+ {
+ int len = reader.getAttributeCount();
+ for (int i = 0; i < len; i++)
+ {
+ QName q = reader.getAttributeName(i);
+ String ln = q.getLocalPart();
+ String u = q.getNamespaceURI();
+ if (u == null && uri != null)
+ continue;
+ if (u != null && !u.equals(uri))
+ continue;
+ if (ln.equals(localName))
+ return i;
+ }
+ return -1;
+ }
+
+ public int getLength()
+ {
+ return reader.getAttributeCount();
+ }
+
+ public String getLocalName(int index)
+ {
+ return reader.getAttributeLocalName(index);
+ }
+
+ public String getQName(int index)
+ {
+ QName q = reader.getAttributeName(index);
+ String localName = q.getLocalPart();
+ String prefix = q.getPrefix();
+ return ("".equals(prefix)) ? localName : prefix + ":" + localName;
+ }
+
+ public String getType(int index)
+ {
+ String ret = reader.getAttributeType(index);
+ // SAX doesn't permit ENUMERATION?
+ return ("ENUMERATION".equals(ret)) ? "NMTOKEN" : ret;
+ }
+
+ public String getType(String qName)
+ {
+ int index = getIndex(qName);
+ return (index == -1) ? null : getType(index);
+ }
+
+ public String getType(String uri, String localName)
+ {
+ int index = getIndex(uri, localName);
+ return (index == -1) ? null : getType(index);
+ }
+
+ public String getURI(int index)
+ {
+ String ret = reader.getAttributeNamespace(index);
+ return (ret == null) ? "" : ret;
+ }
+
+ public String getValue(int index)
+ {
+ return reader.getAttributeValue(index);
+ }
+
+ public String getValue(String qName)
+ {
+ int index = getIndex(qName);
+ return (index == -1) ? null : getValue(index);
+ }
+
+ public String getValue(String uri, String localName)
+ {
+ int index = getIndex(uri, localName);
+ return (index == -1) ? null : getValue(index);
+ }
+
+ public boolean isDeclared(int index)
+ {
+ return parser.isAttributeDeclared(index);
+ }
+
+ public boolean isDeclared(String qName)
+ {
+ int index = getIndex(qName);
+ return (index == -1) ? false : isDeclared(index);
+ }
+
+ public boolean isDeclared(String uri, String localName)
+ {
+ int index = getIndex(uri, localName);
+ return (index == -1) ? false : isDeclared(index);
+ }
+
+ public boolean isSpecified(int index)
+ {
+ return reader.isAttributeSpecified(index);
+ }
+
+ public boolean isSpecified(String qName)
+ {
+ int index = getIndex(qName);
+ return (index == -1) ? false : isSpecified(index);
+ }
+
+ public boolean isSpecified(String uri, String localName)
+ {
+ int index = getIndex(uri, localName);
+ return (index == -1) ? false : isSpecified(index);
+ }
+
+ // -- Locator2 --
+
+ public int getColumnNumber()
+ {
+ Location l = reader.getLocation();
+ return l.getColumnNumber();
+ }
+
+ public int getLineNumber()
+ {
+ Location l = reader.getLocation();
+ return l.getLineNumber();
+ }
+
+ public String getPublicId()
+ {
+ Location l = reader.getLocation();
+ return l.getPublicId();
+ }
+
+ public String getSystemId()
+ {
+ Location l = reader.getLocation();
+ return l.getSystemId();
+ }
+
+ public String getEncoding()
+ {
+ return encoding;
+ }
+
+ public String getXMLVersion()
+ {
+ return xmlVersion;
+ }
+
+ // -- XMLResolver --
+
+ public Object resolveEntity(String publicId, String systemId,
+ String baseURI, String namespace)
+ throws XMLStreamException
+ {
+ if (entityResolver != null)
+ {
+ try
+ {
+ InputSource input =
+ entityResolver.resolveEntity(publicId, systemId);
+ if (input != null)
+ {
+ InputStream in = input.getByteStream();
+ if (in == null)
+ {
+ String newSystemId = input.getSystemId();
+ if (newSystemId != null && !newSystemId.equals(systemId))
+ in = XMLParser.resolve(newSystemId);
+ }
+ return in;
+ }
+ }
+ catch (SAXException e)
+ {
+ XMLStreamException e2 = new XMLStreamException(e.getMessage());
+ e2.initCause(e);
+ throw e2;
+ }
+ catch (IOException e)
+ {
+ XMLStreamException e2 = new XMLStreamException(e.getMessage());
+ e2.initCause(e);
+ throw e2;
+ }
+ }
+ return null;
+ }
+
+ public XMLEventReader resolveAsXMLEventReader(String uri)
+ throws XMLStreamException
+ {
+ // unused
+ return null;
+ }
+
+ public XMLStreamReader resolveAsXMLStreamReader(String uri)
+ throws XMLStreamException
+ {
+ // unused
+ return null;
+ }
+
+ // -- XMLReporter --
+
+ public void report(String message, String errorType,
+ Object relatedInformation, Location location)
+ throws XMLStreamException
+ {
+ if (errorHandler != null)
+ {
+ try
+ {
+ errorHandler.warning(new SAXParseException(message, this));
+ }
+ catch (SAXException e)
+ {
+ XMLStreamException e2 = new XMLStreamException(e.getMessage());
+ e2.initCause(e);
+ throw e2;
+ }
+ }
+ }
+
+ public static void main(String[] args)
+ throws Exception
+ {
+ boolean validating = false;
+ boolean namespaceAware = false;
+ boolean xIncludeAware = false;
+ boolean expectCallbackClass = false;
+ String callbackClass = null;
+ int pos = 0;
+ while (pos < args.length && (args[pos].startsWith("-") || expectCallbackClass))
+ {
+ if ("-x".equals(args[pos]))
+ xIncludeAware = true;
+ else if ("-v".equals(args[pos]))
+ validating = true;
+ else if ("-n".equals(args[pos]))
+ namespaceAware = true;
+ else if ("-c".equals(args[pos]))
+ expectCallbackClass = true;
+ else if (expectCallbackClass)
+ {
+ callbackClass = args[pos];
+ expectCallbackClass = false;
+ }
+ pos++;
+ }
+ if (pos >= args.length || expectCallbackClass)
+ {
+ System.out.println("Syntax: SAXParser [-n] [-v] [-x] [-c
+ * Features
+ * http://gnu.org/sax/features/xml-base
+ * read/write
+ * Indicates or sets whether XML Base processing is enabled
+ * Properties
+ * http://gnu.org/sax/properties/base-uri
+ * read-only String
+ * Returns the base URI of the current event
+ * http://gnu.org/sax/properties/document-xml-encoding
+ * read-only String
+ * Returns the encoding specified in the XML declaration
+ *
+ *
+ * @see http://www.w3.org/TR/REC-xml/
+ * @see http://www.w3.org/TR/xml11/
+ * @see http://www.w3.org/TR/REC-xml-names
+ * @see http://www.w3.org/TR/xml-names11
+ * @see http://www.w3.org/TR/xmlbase/
+ *
+ * @author Chris Burdess
+ */
+public class XMLParser
+ implements XMLStreamReader, NamespaceContext
+{
+
+ // -- parser state machine states --
+ private static final int INIT = 0; // start state
+ private static final int PROLOG = 1; // in prolog
+ private static final int CONTENT = 2; // in content
+ private static final int EMPTY_ELEMENT = 3; // empty element state
+ private static final int MISC = 4; // in Misc (after root element)
+
+ // -- parameters for parsing literals --
+ private final static int LIT_ENTITY_REF = 2;
+ private final static int LIT_NORMALIZE = 4;
+ private final static int LIT_ATTRIBUTE = 8;
+ private final static int LIT_DISABLE_PE = 16;
+ private final static int LIT_DISABLE_CREF = 32;
+ private final static int LIT_DISABLE_EREF = 64;
+ private final static int LIT_PUBID = 256;
+
+ // -- types of attribute values --
+ final static int ATTRIBUTE_DEFAULT_UNDECLARED = 30;
+ final static int ATTRIBUTE_DEFAULT_SPECIFIED = 31;
+ final static int ATTRIBUTE_DEFAULT_IMPLIED = 32;
+ final static int ATTRIBUTE_DEFAULT_REQUIRED = 33;
+ final static int ATTRIBUTE_DEFAULT_FIXED = 34;
+
+ // -- additional event types --
+ final static int START_ENTITY = 50;
+ final static int END_ENTITY = 51;
+
+ /**
+ * The current input.
+ */
+ private Input input;
+
+ /**
+ * Stack of inputs representing XML general entities.
+ * The input representing the XML input stream or reader is always the
+ * first element in this stack.
+ */
+ private LinkedList inputStack = new LinkedList();
+
+ /**
+ * Stack of start-entity events to be reported.
+ */
+ private LinkedList startEntityStack = new LinkedList();
+
+ /**
+ * Stack of end-entity events to be reported.
+ */
+ private LinkedList endEntityStack = new LinkedList();
+
+ /**
+ * Current parser state within the main state machine.
+ */
+ private int state = INIT;
+
+ /**
+ * The (type of the) current event.
+ */
+ private int event;
+
+ /**
+ * The element name stack. The first element in this stack will be the
+ * root element.
+ */
+ private LinkedList stack = new LinkedList();
+
+ /**
+ * Stack of namespace contexts. These are maps specifying prefix-to-URI
+ * mappings. The first element in this stack is the most recent namespace
+ * context (i.e. the other way around from the element name stack).
+ */
+ private LinkedList namespaces = new LinkedList();
+
+ /**
+ * The base-URI stack. This holds the base URI context for each element.
+ * The first element in this stack is the most recent context (i.e. the
+ * other way around from the element name stack).
+ */
+ private LinkedList bases = new LinkedList();
+
+ /**
+ * The list of attributes for the current element, in the order defined in
+ * the XML stream.
+ */
+ private ArrayList attrs = new ArrayList();
+
+ /**
+ * Buffer for text and character data.
+ */
+ private StringBuffer buf = new StringBuffer();
+
+ /**
+ * Buffer for NMTOKEN strings (markup).
+ */
+ private StringBuffer nmtokenBuf = new StringBuffer();
+
+ /**
+ * Buffer for string literals. (e.g. attribute values)
+ */
+ private StringBuffer literalBuf = new StringBuffer();
+
+ /**
+ * Temporary Unicode character buffer used during character data reads.
+ */
+ private int[] tmpBuf = new int[1024];
+
+ /**
+ * The element content model for the current element.
+ */
+ private ContentModel currentContentModel;
+
+ /**
+ * The validation stack. This holds lists of the elements seen for each
+ * element, in order to determine whether the names and order of these
+ * elements match the content model for the element. The last entry in
+ * this stack represents the current element.
+ */
+ private LinkedList validationStack;
+
+ /**
+ * These sets contain the IDs and the IDREFs seen in the document, to
+ * ensure that IDs are unique and that each IDREF refers to an ID in the
+ * document.
+ */
+ private HashSet ids, idrefs;
+
+ /**
+ * The target and data associated with the current processing instruction
+ * event.
+ */
+ private String piTarget, piData;
+
+ /**
+ * The XML version declared in the XML declaration.
+ */
+ private String xmlVersion;
+
+ /**
+ * The encoding declared in the XML declaration.
+ */
+ private String xmlEncoding;
+
+ /**
+ * The standalone value declared in the XML declaration.
+ */
+ private Boolean xmlStandalone;
+
+ /**
+ * The document type definition.
+ */
+ Doctype doctype;
+
+ /**
+ * State variables for determining parameter-entity expansion.
+ */
+ private boolean expandPE, peIsError;
+
+ /**
+ * Whether this is a validating parser.
+ */
+ private final boolean validating;
+
+ /**
+ * Whether strings representing markup will be interned.
+ */
+ private final boolean stringInterning;
+
+ /**
+ * If true, CDATA sections will be merged with adjacent text nodes into a
+ * single event.
+ */
+ private final boolean coalescing;
+
+ /**
+ * Whether to replace general entity references with their replacement
+ * text automatically during parsing.
+ * Otherwise entity-reference events will be issued.
+ */
+ private final boolean replaceERefs;
+
+ /**
+ * Whether to support external entities.
+ */
+ private final boolean externalEntities;
+
+ /**
+ * Whether to support DTDs.
+ */
+ private final boolean supportDTD;
+
+ /**
+ * Whether to support XML namespaces. If true, namespace information will
+ * be available. Otherwise namespaces will simply be reported as ordinary
+ * attributes.
+ */
+ private final boolean namespaceAware;
+
+ /**
+ * Whether to support XML Base. If true, URIs specified in xml:base
+ * attributes will be honoured when resolving external entities.
+ */
+ private final boolean baseAware;
+
+ /**
+ * Whether to report extended event types (START_ENTITY and END_ENTITY)
+ * in addition to the standard event types. Used by the SAX parser.
+ */
+ private final boolean extendedEventTypes;
+
+ /**
+ * The reporter to receive parsing warnings.
+ */
+ final XMLReporter reporter;
+
+ /**
+ * Callback interface for resolving external entities.
+ */
+ final XMLResolver resolver;
+
+ // -- Constants for testing the next kind of markup event --
+ private static final String TEST_START_ELEMENT = "<";
+ private static final String TEST_END_ELEMENT = "";
+ private static final String TEST_COMMENT = "");
+ }
+ catch (IOException e)
+ {
+ XMLStreamException e2 = new XMLStreamException(e);
+ e2.initCause(e);
+ throw e2;
+ }
+ }
+
+ public void writeProcessingInstruction(String target)
+ throws XMLStreamException
+ {
+ writeProcessingInstruction(target, null);
+ }
+
+ public void writeProcessingInstruction(String target, String data)
+ throws XMLStreamException
+ {
+ try
+ {
+ if (!isName(target) || "xml".equalsIgnoreCase(target))
+ throw new IllegalArgumentException("illegal PITarget: " + target);
+ if (data != null && !isChars(data))
+ throw new IllegalArgumentException("illegal XML character: " + data);
+
+ endStartElement();
+
+ writer.write('<');
+ writer.write('?');
+ writer.write(target);
+ if (data != null)
+ {
+ writer.write(' ');
+ if (hasXML11RestrictedChars)
+ {
+ int[] seq = UnicodeReader.toCodePointArray(data);
+ for (int i = 0; i < seq.length; i++)
+ {
+ int c = seq[i];
+ if (XMLParser.isXML11RestrictedChar(c))
+ writer.write("" + Integer.toHexString(c) + ";");
+ else
+ writer.write(Character.toChars(i));
+ }
+ }
+ else
+ writer.write(data);
+ }
+ writer.write('?');
+ writer.write('>');
+ }
+ catch (IOException e)
+ {
+ XMLStreamException e2 = new XMLStreamException(e);
+ e2.initCause(e);
+ throw e2;
+ }
+ }
+
+ public void writeCData(String data)
+ throws XMLStreamException
+ {
+ try
+ {
+ if (!isChars(data) || hasXML11RestrictedChars)
+ throw new IllegalArgumentException("illegal XML character: " + data);
+ if (data.indexOf("]]") != -1)
+ throw new IllegalArgumentException("illegal CDATA section: " + data);
+
+ endStartElement();
+
+ writer.write("");
+ }
+ catch (IOException e)
+ {
+ XMLStreamException e2 = new XMLStreamException(e);
+ e2.initCause(e);
+ throw e2;
+ }
+ }
+
+ public void writeDTD(String dtd)
+ throws XMLStreamException
+ {
+ try
+ {
+ // XXX: Should we parse the doctypedecl at this point to ensure
+ // wellformedness?
+ writer.write("');
+ }
+ catch (IOException e)
+ {
+ XMLStreamException e2 = new XMLStreamException(e);
+ e2.initCause(e);
+ throw e2;
+ }
+ }
+
+ public void writeEntityRef(String name)
+ throws XMLStreamException
+ {
+ try
+ {
+ if (!isName(name))
+ throw new IllegalArgumentException("illegal Name: " + name);
+
+ endStartElement();
+
+ writer.write('&');
+ writer.write(name);
+ writer.write(';');
+ }
+ catch (IOException e)
+ {
+ XMLStreamException e2 = new XMLStreamException(e);
+ e2.initCause(e);
+ throw e2;
+ }
+ }
+
+ public void writeStartDocument()
+ throws XMLStreamException
+ {
+ writeStartDocument(null, null);
+ }
+
+ public void writeStartDocument(String version)
+ throws XMLStreamException
+ {
+ writeStartDocument(null, version);
+ }
+
+ public void writeStartDocument(String encoding, String version)
+ throws XMLStreamException
+ {
+ if (version == null)
+ version = "1.0";
+ else if ("1.1".equals(version))
+ xml11 = true;
+ encoding = this.encoding; // YES: the parameter must be ignored
+ if (encoding == null)
+ encoding = "UTF-8";
+ if (!"1.0".equals(version) && !"1.1".equals(version))
+ throw new IllegalArgumentException(version);
+ try
+ {
+ writer.write("");
+ writer.write(System.getProperty("line.separator"));
+ }
+ catch (IOException e)
+ {
+ XMLStreamException e2 = new XMLStreamException(e);
+ e2.initCause(e);
+ throw e2;
+ }
+ }
+
+ public void writeCharacters(String text)
+ throws XMLStreamException
+ {
+ if (text == null)
+ return;
+ try
+ {
+ if (!isChars(text))
+ throw new IllegalArgumentException("illegal XML character: " + text);
+
+ endStartElement();
+
+ if (hasXML11RestrictedChars)
+ writeEncodedWithRestrictedChars(text, false);
+ else
+ writeEncoded(text, false);
+ }
+ catch (IOException e)
+ {
+ XMLStreamException e2 = new XMLStreamException(e);
+ e2.initCause(e);
+ throw e2;
+ }
+ }
+
+ public void writeCharacters(char[] text, int start, int len)
+ throws XMLStreamException
+ {
+ writeCharacters(new String(text, start, len));
+ }
+
+ public String getPrefix(String uri)
+ throws XMLStreamException
+ {
+ String prefix = namespaces.getPrefix(uri);
+ if (prefix == null && namespaceContext != null)
+ prefix = namespaceContext.getPrefix(uri);
+ return prefix;
+ }
+
+ public void setPrefix(String prefix, String uri)
+ throws XMLStreamException
+ {
+ try
+ {
+ if (!isURI(uri))
+ throw new IllegalArgumentException("illegal URI: " + uri);
+ if (!isPrefix(prefix))
+ throw new IllegalArgumentException("illegal NCName: " + prefix);
+ }
+ catch (IOException e)
+ {
+ XMLStreamException e2 = new XMLStreamException(e);
+ e2.initCause(e);
+ throw e2;
+ }
+ if (!namespaces.declarePrefix(prefix, uri))
+ throw new XMLStreamException("illegal prefix " + prefix);
+ }
+
+ public void setDefaultNamespace(String uri)
+ throws XMLStreamException
+ {
+ if (!isURI(uri))
+ throw new IllegalArgumentException("illegal URI: " + uri);
+ if (!namespaces.declarePrefix(XMLConstants.DEFAULT_NS_PREFIX, uri))
+ throw new XMLStreamException("illegal default namespace prefix");
+ }
+
+ public void setNamespaceContext(NamespaceContext context)
+ throws XMLStreamException
+ {
+ namespaceContext = context;
+ }
+
+ public NamespaceContext getNamespaceContext()
+ {
+ return namespaceContext;
+ }
+
+ public Object getProperty(String name)
+ throws IllegalArgumentException
+ {
+ throw new IllegalArgumentException(name);
+ }
+
+ /**
+ * Write the specified text, ensuring that the content is suitably encoded
+ * for XML.
+ * @param text the text to write
+ * @param inAttr whether we are in an attribute value
+ */
+ private void writeEncoded(String text, boolean inAttr)
+ throws IOException
+ {
+ char[] chars = text.toCharArray();
+ int start = 0;
+ int end = chars.length;
+ int len = 0;
+ for (int i = start; i < end; i++)
+ {
+ char c = chars[i];
+ if (c == '<' || c == '>' || c == '&')
+ {
+ writer.write(chars, start, len);
+ if (c == '<')
+ writer.write("<");
+ else if (c == '>')
+ writer.write(">");
+ else
+ writer.write("&");
+ start = i + 1;
+ len = 0;
+ }
+ else if (inAttr && (c == '"' || c == '\''))
+ {
+ writer.write(chars, start, len);
+ if (c == '"')
+ writer.write(""");
+ else
+ writer.write("'");
+ start = i + 1;
+ len = 0;
+ }
+ else
+ len++;
+ }
+ if (len > 0)
+ writer.write(chars, start, len);
+ }
+
+ /**
+ * Writes the specified text, in the knowledge that some of the
+ * characters are XML 1.1 restricted characters.
+ */
+ private void writeEncodedWithRestrictedChars(String text, boolean inAttr)
+ throws IOException
+ {
+ int[] seq = UnicodeReader.toCodePointArray(text);
+ for (int i = 0; i < seq.length; i++)
+ {
+ int c = seq[i];
+ switch (c)
+ {
+ case 0x3c: // '<'
+ writer.write("<");
+ break;
+ case 0x3e: // '>'
+ writer.write(">");
+ break;
+ case 0x26: // '&'
+ writer.write("&");
+ break;
+ case 0x22: // '"'
+ if (inAttr)
+ writer.write(""");
+ else
+ writer.write(c);
+ break;
+ case 0x27: // '\''
+ if (inAttr)
+ writer.write("'");
+ else
+ writer.write(c);
+ break;
+ default:
+ if (XMLParser.isXML11RestrictedChar(c))
+ writer.write("" + Integer.toHexString(c) + ";");
+ else
+ {
+ char[] chars = Character.toChars(c);
+ writer.write(chars, 0, chars.length);
+ }
+ }
+ }
+ }
+
+ private boolean isName(String text)
+ throws IOException
+ {
+ if (text == null)
+ return false;
+ int[] seq = UnicodeReader.toCodePointArray(text);
+ if (seq.length < 1)
+ return false;
+ if (!XMLParser.isNameStartCharacter(seq[0], xml11))
+ return false;
+ for (int i = 1; i < seq.length; i++)
+ {
+ if (!XMLParser.isNameCharacter(seq[i], xml11))
+ return false;
+ }
+ return true;
+ }
+
+ private boolean isPrefix(String text)
+ throws IOException
+ {
+ if (XMLConstants.DEFAULT_NS_PREFIX.equals(text)) {
+ return true;
+ }
+ return isNCName(text);
+ }
+
+ private boolean isNCName(String text)
+ throws IOException
+ {
+ if (text == null)
+ return false;
+ int[] seq = UnicodeReader.toCodePointArray(text);
+ if (seq.length < 1)
+ return false;
+ if (!XMLParser.isNameStartCharacter(seq[0], xml11) || seq[0] == 0x3a)
+ return false;
+ for (int i = 1; i < seq.length; i++)
+ {
+ if (!XMLParser.isNameCharacter(seq[i], xml11) || seq[i] == 0x3a)
+ return false;
+ }
+ return true;
+ }
+
+ private boolean isChars(String text)
+ throws IOException
+ {
+ if (text == null)
+ return false;
+ int[] seq = UnicodeReader.toCodePointArray(text);
+ hasXML11RestrictedChars = false;
+ if (xml11)
+ {
+ for (int i = 0; i < seq.length; i++)
+ {
+ if (!XMLParser.isXML11Char(seq[i]))
+ return false;
+ if (XMLParser.isXML11RestrictedChar(seq[i]))
+ hasXML11RestrictedChars = true;
+ }
+ }
+ else
+ {
+ for (int i = 0; i < seq.length; i++)
+ {
+ if (!XMLParser.isChar(seq[i]))
+ return false;
+ }
+ }
+ return true;
+ }
+
+ private boolean isURI(String text)
+ {
+ if (text == null)
+ return false;
+ char[] chars = text.toCharArray();
+ if (chars.length < 1)
+ return false;
+ for (int i = 0; i < chars.length; i++)
+ {
+ if (chars[i] < 0x20 || chars[i] >= 0x7f)
+ return false;
+ }
+ return true;
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/transform/AbstractNumberNode.java b/libjava/classpath/gnu/xml/transform/AbstractNumberNode.java
new file mode 100644
index 000000000..6e478bdc4
--- /dev/null
+++ b/libjava/classpath/gnu/xml/transform/AbstractNumberNode.java
@@ -0,0 +1,330 @@
+/* AbstractNumberNode.java --
+ Copyright (C) 2004 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.transform;
+
+import gnu.java.lang.CPStringBuilder;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+import javax.xml.namespace.QName;
+import javax.xml.transform.TransformerException;
+import org.w3c.dom.Document;
+import org.w3c.dom.DocumentFragment;
+import org.w3c.dom.Node;
+import org.w3c.dom.Text;
+import gnu.xml.xpath.Expr;
+
+/**
+ * A template node representing the XSL
+ * gnu.xml.stream.stringInterning
+ * Boolean
+ * Indicates whether markup strings will be interned
+ * gnu.xml.stream.xmlBase
+ * Boolean
+ * Indicates whether XML Base processing will be performed
+ * gnu.xml.stream.baseURI
+ * String
+ * Returns the base URI of the current event number
instruction.
+ *
+ * @author Chris Burdess
+ */
+abstract class AbstractNumberNode
+ extends TemplateNode
+{
+
+ static final int ALPHABETIC = 0;
+ static final int TRADITIONAL = 1;
+
+ final TemplateNode format;
+ final String lang;
+ final int letterValue;
+ final String groupingSeparator;
+ final int groupingSize;
+
+ AbstractNumberNode(TemplateNode format, String lang,
+ int letterValue, String groupingSeparator,
+ int groupingSize)
+ {
+ this.format = format;
+ this.lang = lang;
+ this.letterValue = letterValue;
+ this.groupingSeparator = groupingSeparator;
+ this.groupingSize = groupingSize;
+ }
+
+ void doApply(Stylesheet stylesheet, QName mode,
+ Node context, int pos, int len,
+ Node parent, Node nextSibling)
+ throws TransformerException
+ {
+ Document doc = (parent instanceof Document) ? (Document) parent :
+ parent.getOwnerDocument();
+ DocumentFragment fragment = doc.createDocumentFragment();
+ format.apply(stylesheet, mode, context, pos, len, fragment, null);
+ String f = Expr._string(context, Collections.singleton(fragment));
+ String value = format(f, compute(stylesheet, context, pos, len));
+ Text text = doc.createTextNode(value);
+ if (nextSibling != null)
+ {
+ parent.insertBefore(text, nextSibling);
+ }
+ else
+ {
+ parent.appendChild(text);
+ }
+ // xsl:number doesn't process children
+ if (next != null)
+ {
+ next.apply(stylesheet, mode,
+ context, pos, len,
+ parent, nextSibling);
+ }
+ }
+
+ String format(String format, int[] number)
+ {
+ if (number.length == 0)
+ {
+ return "";
+ }
+ int start = 0, end = 0, len = format.length(); // region of format
+ // Tokenize
+ List tokens = new ArrayList((number.length * 2) + 1);
+ List types = new ArrayList(tokens.size());
+ while (end < len)
+ {
+ while (end < len && !isAlphanumeric(format.charAt(end)))
+ {
+ end++;
+ }
+ if (end > start)
+ {
+ tokens.add(format.substring(start, end));
+ types.add(Boolean.FALSE);
+ }
+ start = end;
+ while (end < len && isAlphanumeric(format.charAt(end)))
+ {
+ end++;
+ }
+ if (end > start)
+ {
+ tokens.add(format.substring(start, end));
+ types.add(Boolean.TRUE);
+ }
+ start = end;
+ }
+ // Process tokens
+ CPStringBuilder buf = new CPStringBuilder();
+ len = tokens.size();
+ int pos = 0;
+ for (int i = 0; i < len; i++)
+ {
+ String token = (i < 0) ? "." : (String) tokens.get(i);
+ boolean alpha = (i < 0) ? true :
+ ((Boolean) types.get(i)).booleanValue();
+ if (!alpha)
+ {
+ buf.append(token);
+ }
+ else
+ {
+ if (pos < number.length)
+ {
+ format(buf, number[pos++], token);
+ if (((i + 1 == len) || (i + 2 == len)) &&
+ (pos < number.length))
+ {
+ // More numbers than tokens, reuse last token
+ i -= 2;
+ }
+ }
+ if (pos == number.length && i < (len - 2))
+ {
+ // No more numbers. Skip to the end...
+ i = len - 2;
+ if (((Boolean) types.get(i + 1)).booleanValue())
+ {
+ // number formatting token, ignore
+ i++;
+ }
+ }
+ }
+ }
+ //System.err.println("format: '"+format+"' "+asList(number)+" = '"+buf.toString()+"'");
+ return buf.toString();
+ }
+
+ /*List asList(int[] number)
+ {
+ List l = new ArrayList();
+ for (int i = 0; i < number.length; i++)
+ l.add(new Integer(number[i]));
+ return l;
+ }*/
+
+ void format(CPStringBuilder buf, int number, String formatToken)
+ {
+ int len = formatToken.length();
+ char c = formatToken.charAt(len - 1);
+ if (Character.digit(c, 10) == 1)
+ {
+ // Check preceding characters
+ for (int i = len - 2; i >= 0; i--)
+ {
+ if (formatToken.charAt(i) != (c - 1))
+ {
+ format(buf, number, "1");
+ return;
+ }
+ }
+ // Decimal representation
+ String val = Integer.toString(number);
+ for (int d = len - val.length(); d > 0; d--)
+ {
+ buf.append('0');
+ }
+ buf.append(val);
+ }
+ else if ("A".equals(formatToken))
+ {
+ buf.append(alphabetic('@', number));
+ }
+ else if ("a".equals(formatToken))
+ {
+ buf.append(alphabetic('`', number));
+ }
+ else if ("i".equals(formatToken))
+ {
+ buf.append(roman(false, number));
+ }
+ else if ("I".equals(formatToken))
+ {
+ buf.append(roman(true, number));
+ }
+ else
+ {
+ // Unknown numbering sequence
+ format(buf, number, "1");
+ }
+ }
+
+ static final boolean isAlphanumeric(char c)
+ {
+ switch (Character.getType(c))
+ {
+ case Character.DECIMAL_DIGIT_NUMBER: // Nd
+ case Character.LETTER_NUMBER: // Nl
+ case Character.OTHER_NUMBER: // No
+ case Character.UPPERCASE_LETTER: // Lu
+ case Character.LOWERCASE_LETTER: // Ll
+ case Character.TITLECASE_LETTER: // Lt
+ case Character.MODIFIER_LETTER: // Lm
+ case Character.OTHER_LETTER: // Lo
+ return true;
+ default:
+ return false;
+ }
+ }
+
+ static final String alphabetic(char offset, int number)
+ {
+ CPStringBuilder buf = new CPStringBuilder();
+ while (number > 0)
+ {
+ int r = number % 26;
+ number = number / 26;
+ buf.insert(0, (char) (offset + r));
+ }
+ return buf.toString();
+ }
+
+ static final int[] roman_numbers = {1, 5, 10, 50, 100, 500, 1000};
+ static final char[] roman_chars = {'i', 'v', 'x', 'l', 'c', 'd', 'm'};
+
+ static final String roman(boolean upper, int number)
+ {
+ CPStringBuilder buf = new CPStringBuilder();
+ for (int pos = roman_numbers.length - 1; pos >= 0; pos -= 2)
+ {
+ int f = number / roman_numbers[pos];
+ if (f != 0)
+ {
+ number = number % (f * roman_numbers[pos]);
+ }
+ if (f > 4 && f < 9)
+ {
+ buf.append(roman_chars[pos + 1]);
+ f -= 5;
+ }
+ if (f == 4)
+ {
+ buf.append(roman_chars[pos]);
+ buf.append(roman_chars[pos + 1]);
+ }
+ else if (f == 9)
+ {
+ buf.append(roman_chars[pos]);
+ buf.append(roman_chars[pos + 2]);
+ }
+ else
+ {
+ for (; f > 0; f--)
+ {
+ buf.append(roman_chars[pos]);
+ }
+ }
+ }
+ return upper ? buf.toString().toUpperCase() : buf.toString();
+ }
+
+ abstract int[] compute(Stylesheet stylesheet, Node context, int pos, int len)
+ throws TransformerException;
+
+ public boolean references(QName var)
+ {
+ if (format.references(var))
+ {
+ return true;
+ }
+ return super.references(var);
+ }
+
+ public String toString()
+ {
+ CPStringBuilder buf = new CPStringBuilder("number");
+ buf.append('[');
+ buf.append("format=");
+ buf.append(format);
+ buf.append(']');
+ return buf.toString();
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/transform/ApplyImportsNode.java b/libjava/classpath/gnu/xml/transform/ApplyImportsNode.java
new file mode 100644
index 000000000..298c49da4
--- /dev/null
+++ b/libjava/classpath/gnu/xml/transform/ApplyImportsNode.java
@@ -0,0 +1,82 @@
+/* ApplyImportsNode.java --
+ Copyright (C) 2004,2006 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.transform;
+
+import javax.xml.namespace.QName;
+import javax.xml.transform.TransformerException;
+import org.w3c.dom.Node;
+
+/**
+ * A template node representing an XSLT apply-imports
instruction.
+ *
+ * @author Chris Burdess
+ */
+final class ApplyImportsNode
+ extends TemplateNode
+{
+
+ TemplateNode clone(Stylesheet stylesheet)
+ {
+ TemplateNode ret = new ApplyImportsNode();
+ if (children != null)
+ ret.children = children.clone(stylesheet);
+ if (next != null)
+ ret.next = next.clone(stylesheet);
+ return ret;
+ }
+
+ void doApply(Stylesheet stylesheet, QName mode,
+ Node context, int pos, int len,
+ Node parent, Node nextSibling)
+ throws TransformerException
+ {
+ TemplateNode t = stylesheet.getTemplate(mode, context, true);
+ if (t != null)
+ t.apply(stylesheet, mode, context, pos, len,
+ parent, nextSibling);
+ if (next != null)
+ next.apply(stylesheet, mode, context, pos, len,
+ parent, nextSibling);
+ }
+
+ public String toString()
+ {
+ return "apply-imports";
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/transform/ApplyTemplatesNode.java b/libjava/classpath/gnu/xml/transform/ApplyTemplatesNode.java
new file mode 100644
index 000000000..6aa36954a
--- /dev/null
+++ b/libjava/classpath/gnu/xml/transform/ApplyTemplatesNode.java
@@ -0,0 +1,219 @@
+/* ApplyTemplatesNode.java --
+ Copyright (C) 2004,2006 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.transform;
+
+import gnu.java.lang.CPStringBuilder;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.Iterator;
+import java.util.LinkedList;
+import java.util.List;
+import javax.xml.namespace.QName;
+import javax.xml.transform.TransformerException;
+import org.w3c.dom.Node;
+import gnu.xml.xpath.Expr;
+
+/**
+ * A template node representing the XSL apply-templates
+ * instruction.
+ *
+ * @author Chris Burdess
+ */
+final class ApplyTemplatesNode
+ extends TemplateNode
+{
+
+ final Expr select;
+ final QName mode;
+ final List sortKeys;
+ final List withParams;
+ final boolean isDefault;
+
+ ApplyTemplatesNode(Expr select, QName mode,
+ List sortKeys, List withParams, boolean isDefault)
+ {
+ this.select = select;
+ this.mode = mode;
+ this.sortKeys = sortKeys;
+ this.withParams = withParams;
+ this.isDefault = isDefault;
+ }
+
+ TemplateNode clone(Stylesheet stylesheet)
+ {
+ int len = sortKeys != null ? sortKeys.size() : 0;
+ List sortKeys2 = new ArrayList(len);
+ for (int i = 0; i < len; i++)
+ sortKeys2.add(((Key) sortKeys.get(i)).clone(stylesheet));
+ len = withParams != null ? withParams.size() : 0;
+ List withParams2 = new ArrayList(len);
+ for (int i = 0; i < len; i++)
+ withParams2.add(((WithParam) withParams.get(i)).clone(stylesheet));
+ TemplateNode ret = new ApplyTemplatesNode(select.clone(stylesheet),
+ mode, sortKeys2, withParams2,
+ isDefault);
+ if (children != null)
+ ret.children = children.clone(stylesheet);
+ if (next != null)
+ ret.next = next.clone(stylesheet);
+ return ret;
+ }
+
+ void doApply(Stylesheet stylesheet, QName mode,
+ Node context, int pos, int len,
+ Node parent, Node nextSibling)
+ throws TransformerException
+ {
+ Object ret = select.evaluate(context, pos, len);
+ if (ret != null && ret instanceof Collection)
+ {
+ if (withParams != null)
+ {
+ // compute the parameter values
+ LinkedList values = new LinkedList();
+ for (Iterator i = withParams.iterator(); i.hasNext(); )
+ {
+ WithParam p = (WithParam) i.next();
+ Object value = p.getValue(stylesheet, mode, context, pos, len);
+ Object[] pair = new Object[2];
+ pair[0] = p.name;
+ pair[1] = value;
+ values.add(pair);
+ }
+ // push the parameter context
+ stylesheet.bindings.push(Bindings.WITH_PARAM);
+ // set the parameters
+ for (Iterator i = values.iterator(); i.hasNext(); )
+ {
+ Object[] pair = (Object[]) i.next();
+ QName name = (QName) pair[0];
+ Object value = pair[1];
+ stylesheet.bindings.set(name, value, Bindings.WITH_PARAM);
+ }
+ }
+ Collection ns = (Collection) ret;
+ List nodes = new ArrayList(ns);
+ if (sortKeys != null)
+ {
+ for (Iterator i = sortKeys.iterator(); i.hasNext(); )
+ {
+ SortKey sortKey = (SortKey) i.next();
+ sortKey.init(stylesheet, mode, context, pos, len, parent,
+ nextSibling);
+ }
+ Collections.sort(nodes, new XSLComparator(sortKeys));
+ }
+ else
+ Collections.sort(nodes, documentOrderComparator);
+ int l = nodes.size();
+ QName effectiveMode = isDefault ? mode : this.mode;
+ for (int i = 0; i < l; i++)
+ {
+ Node node = (Node) nodes.get(i);
+ TemplateNode t = stylesheet.getTemplate(effectiveMode, node,
+ false);
+ if (t != null)
+ {
+ stylesheet.current = node;
+ t.apply(stylesheet, effectiveMode, node, i + 1, l,
+ parent, nextSibling);
+ }
+ }
+ if (withParams != null)
+ {
+ // pop the variable context
+ stylesheet.bindings.pop(Bindings.WITH_PARAM);
+ }
+ }
+ // apply-templates doesn't have processable children
+ if (next != null)
+ next.apply(stylesheet, mode,
+ context, pos, len,
+ parent, nextSibling);
+ }
+
+ public boolean references(QName var)
+ {
+ if (select != null && select.references(var))
+ return true;
+ if (withParams != null)
+ {
+ for (Iterator i = withParams.iterator(); i.hasNext(); )
+ {
+ if (((WithParam) i.next()).references(var))
+ return true;
+ }
+ }
+ if (sortKeys != null)
+ {
+ for (Iterator i = sortKeys.iterator(); i.hasNext(); )
+ {
+ if (((SortKey) i.next()).references(var))
+ return true;
+ }
+ }
+ return super.references(var);
+ }
+
+ public String toString()
+ {
+ CPStringBuilder buf = new CPStringBuilder("apply-templates");
+ buf.append('[');
+ boolean o = false;
+ if (select != null)
+ {
+ buf.append("select=");
+ buf.append(select);
+ o = true;
+ }
+ if (mode != null)
+ {
+ if (o)
+ {
+ buf.append(',');
+ }
+ buf.append("mode=");
+ buf.append(mode);
+ }
+ buf.append(']');
+ return buf.toString();
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/transform/AttributeNode.java b/libjava/classpath/gnu/xml/transform/AttributeNode.java
new file mode 100644
index 000000000..4c71f6a92
--- /dev/null
+++ b/libjava/classpath/gnu/xml/transform/AttributeNode.java
@@ -0,0 +1,244 @@
+/* AttributeNode.java --
+ Copyright (C) 2004,2006 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.transform;
+
+import gnu.java.lang.CPStringBuilder;
+
+import javax.xml.XMLConstants;
+import javax.xml.namespace.QName;
+import javax.xml.transform.TransformerException;
+import org.w3c.dom.Document;
+import org.w3c.dom.DocumentFragment;
+import org.w3c.dom.Attr;
+import org.w3c.dom.NamedNodeMap;
+import org.w3c.dom.Node;
+import gnu.xml.xpath.Expr;
+
+/**
+ * A template node representing an XSL attribute
instruction.
+ *
+ * @author Chris Burdess
+ */
+final class AttributeNode
+ extends TemplateNode
+{
+
+ final TemplateNode name;
+ final TemplateNode namespace;
+ final Node source;
+
+ AttributeNode(TemplateNode name,
+ TemplateNode namespace, Node source)
+ {
+ this.name = name;
+ this.namespace = namespace;
+ this.source = source;
+ }
+
+ TemplateNode clone(Stylesheet stylesheet)
+ {
+ TemplateNode ret = new AttributeNode(name.clone(stylesheet),
+ (namespace == null) ? null :
+ namespace.clone(stylesheet),
+ source);
+ if (children != null)
+ ret.children = children.clone(stylesheet);
+ if (next != null)
+ ret.next = next.clone(stylesheet);
+ return ret;
+ }
+
+ void doApply(Stylesheet stylesheet, QName mode,
+ Node context, int pos, int len,
+ Node parent, Node nextSibling)
+ throws TransformerException
+ {
+ Document doc = (parent instanceof Document) ? (Document) parent :
+ parent.getOwnerDocument();
+ // Create a document fragment to hold the name
+ DocumentFragment fragment = doc.createDocumentFragment();
+ // Apply name to the fragment
+ name.apply(stylesheet, mode,
+ context, pos, len,
+ fragment, null);
+ // Use XPath string-value of fragment
+ String nameValue = Expr.stringValue(fragment);
+
+ String namespaceValue = null;
+ if (namespace != null)
+ {
+ // Create a document fragment to hold the namespace
+ fragment = doc.createDocumentFragment();
+ // Apply namespace to the fragment
+ namespace.apply(stylesheet, mode,
+ context, pos, len,
+ fragment, null);
+ // Use XPath string-value of fragment
+ namespaceValue = Expr.stringValue(fragment);
+ if (namespaceValue.length() == 0)
+ namespaceValue = null;
+ }
+
+ String prefix = getPrefix(nameValue);
+ if (namespaceValue == null)
+ {
+ if (prefix != null)
+ {
+ if (XMLConstants.XML_NS_PREFIX.equals(prefix))
+ namespaceValue = XMLConstants.XML_NS_URI;
+ else
+ {
+ // Resolve namespace for this prefix
+ namespaceValue = source.lookupNamespaceURI(prefix);
+ }
+ }
+ }
+ else
+ {
+ if (prefix != null)
+ {
+ String ns2 = source.lookupNamespaceURI(prefix);
+ if (ns2 != null && !ns2.equals(namespaceValue))
+ {
+ // prefix clashes, reset it
+ prefix = null;
+ int ci = nameValue.indexOf(':');
+ nameValue = nameValue.substring(ci + 1);
+ }
+ }
+ }
+ if (prefix == null)
+ {
+ // Resolve prefix for this namespace
+ prefix = source.lookupPrefix(namespaceValue);
+ if (prefix != null)
+ nameValue = prefix + ":" + nameValue;
+ else
+ {
+ if (namespaceValue != null)
+ {
+ // Must invent a prefix
+ prefix = inventPrefix(parent);
+ nameValue = prefix + ":" + nameValue;
+ }
+ }
+ }
+ NamedNodeMap attrs = parent.getAttributes();
+ boolean insert = true;
+ if (XMLConstants.XMLNS_ATTRIBUTE_NS_URI.equals(namespaceValue) ||
+ XMLConstants.XMLNS_ATTRIBUTE.equals(nameValue) ||
+ nameValue.startsWith("xmlns:"))
+ {
+ // Namespace declaration, do not output
+ insert = false;
+ }
+ if (prefix != null && namespaceValue == null)
+ {
+ // Not a QName
+ insert = false;
+ }
+ if (parent.getNodeType() == Node.ELEMENT_NODE &&
+ parent.getFirstChild() != null)
+ {
+ // XSLT 7.1.3 Adding an attribute to an element after children have
+ // been added to it is an error
+ insert = false;
+ }
+ if (insert)
+ {
+ // Insert attribute
+ Attr attr = (namespaceValue != null) ?
+ doc.createAttributeNS(namespaceValue, nameValue) :
+ doc.createAttribute(nameValue);
+ if (attrs != null)
+ {
+ if (namespace != null)
+ attrs.setNamedItemNS(attr);
+ else
+ attrs.setNamedItem(attr);
+ }
+ if (children != null)
+ children.apply(stylesheet, mode,
+ context, pos, len,
+ attr, null);
+ }
+ if (next != null)
+ next.apply(stylesheet, mode,
+ context, pos, len,
+ parent, nextSibling);
+ }
+
+ final String getPrefix(String name)
+ {
+ int ci = name.indexOf(':');
+ return (ci == -1) ? null : name.substring(0, ci);
+ }
+
+ final String inventPrefix(Node parent)
+ {
+ String base = "ns";
+ int count = 0;
+ String ret = base + Integer.toString(count);
+ while (parent.lookupNamespaceURI(ret) != null)
+ {
+ count++;
+ ret = base + Integer.toString(count);
+ }
+ return ret;
+ }
+
+ public boolean references(QName var)
+ {
+ if (name != null && name.references(var))
+ return true;
+ if (namespace != null && namespace.references(var))
+ return true;
+ return super.references(var);
+ }
+
+ public String toString()
+ {
+ CPStringBuilder buf = new CPStringBuilder("attribute");
+ buf.append('[');
+ buf.append("name=");
+ buf.append(name);
+ buf.append(']');
+ return buf.toString();
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/transform/AttributeSet.java b/libjava/classpath/gnu/xml/transform/AttributeSet.java
new file mode 100644
index 000000000..ced50f11c
--- /dev/null
+++ b/libjava/classpath/gnu/xml/transform/AttributeSet.java
@@ -0,0 +1,66 @@
+/* AttributeSet.java --
+ Copyright (C) 2004 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.transform;
+
+/**
+ * An attribute-set entry in a stylesheet.
+ *
+ * @author Chris Burdess
+ */
+final class AttributeSet
+{
+
+ final TemplateNode children;
+ final String name;
+ final String uas;
+
+ AttributeSet(TemplateNode children, String name, String uas)
+ {
+ this.children = children;
+ this.name = name;
+ this.uas = uas;
+ }
+
+ AttributeSet clone(Stylesheet stylesheet)
+ {
+ return new AttributeSet((children == null) ? null :
+ children.clone(stylesheet),
+ name, uas);
+ }
+
+}
diff --git a/libjava/classpath/gnu/xml/transform/Bindings.java b/libjava/classpath/gnu/xml/transform/Bindings.java
new file mode 100644
index 000000000..37d45880c
--- /dev/null
+++ b/libjava/classpath/gnu/xml/transform/Bindings.java
@@ -0,0 +1,346 @@
+/* Bindings.java --
+ Copyright (C) 2004 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+package gnu.xml.transform;
+
+import gnu.java.lang.CPStringBuilder;
+
+import java.util.Collection;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Iterator;
+import java.util.LinkedList;
+import java.util.Map;
+import javax.xml.namespace.QName;
+import javax.xml.xpath.XPathVariableResolver;
+import org.w3c.dom.Node;
+
+/**
+ * The set of variable bindings in effect for a stylesheet.
+ *
+ * @author Chris Burdess
+ */
+public class Bindings
+ implements XPathVariableResolver, Cloneable
+{
+
+ static final int VARIABLE = 0;
+ static final int PARAM = 1;
+ static final int WITH_PARAM = 2;
+
+ final Stylesheet stylesheet;
+
+ /**
+ * Global variables.
+ */
+ final LinkedList