diff options
author | upstream source tree <ports@midipix.org> | 2015-03-15 20:14:05 -0400 |
---|---|---|
committer | upstream source tree <ports@midipix.org> | 2015-03-15 20:14:05 -0400 |
commit | 554fd8c5195424bdbcabf5de30fdc183aba391bd (patch) | |
tree | 976dc5ab7fddf506dadce60ae936f43f58787092 /libjava/classpath/gnu/xml/libxmlj/sax | |
download | cbb-gcc-4.6.4-554fd8c5195424bdbcabf5de30fdc183aba391bd.tar.bz2 cbb-gcc-4.6.4-554fd8c5195424bdbcabf5de30fdc183aba391bd.tar.xz |
obtained gcc-4.6.4.tar.bz2 from upstream website;upstream
verified gcc-4.6.4.tar.bz2.sig;
imported gcc-4.6.4 source tree from verified upstream tarball.
downloading a git-generated archive based on the 'upstream' tag
should provide you with a source tree that is binary identical
to the one extracted from the above tarball.
if you have obtained the source via the command 'git clone',
however, do note that line-endings of files in your working
directory might differ from line-endings of the respective
files in the upstream repository.
Diffstat (limited to 'libjava/classpath/gnu/xml/libxmlj/sax')
7 files changed, 1745 insertions, 0 deletions
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 <a href='mailto:dog@gnu.org'>Chris Burdess</a> + */ +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 <a href='mailto:dog@gnu.org'>Chris Burdess</a> + */ +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 <a href='mailto:dog@gnu.org'>Chris Burdess</a> + */ +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 <a href='mailto:dog@gnu.org'>Chris Burdess</a> + */ +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 <a href='mailto:dog@gnu.org'>Chris Burdess</a> + */ +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 <a href='mailto:dog@gnu.org'>Chris Burdess</a> + */ +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 <a href='mailto:dog@gnu.org'>Chris Burdess</a> + */ +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; + } + +} |