diff options
author | tromey <tromey@138bc75d-0d04-0410-961f-82ee72b054a4> | 2005-09-23 19:36:46 +0000 |
---|---|---|
committer | tromey <tromey@138bc75d-0d04-0410-961f-82ee72b054a4> | 2005-09-23 19:36:46 +0000 |
commit | 49792907376493f0939563eb0219b96a48f1ae3b (patch) | |
tree | b2c2abf473309eac532cafbad81b20f3270ff45f /libjava/classpath/gnu/xml | |
parent | 68cf394a99ed232a528346d711e946d4c9a902b5 (diff) | |
download | gcc-49792907376493f0939563eb0219b96a48f1ae3b.tar.gz |
Initial revision
git-svn-id: svn+ssh://gcc.gnu.org/svn/gcc/trunk@104578 138bc75d-0d04-0410-961f-82ee72b054a4
Diffstat (limited to 'libjava/classpath/gnu/xml')
27 files changed, 5217 insertions, 0 deletions
diff --git a/libjava/classpath/gnu/xml/stream/AttributeImpl.java b/libjava/classpath/gnu/xml/stream/AttributeImpl.java new file mode 100644 index 00000000000..501575e56d8 --- /dev/null +++ b/libjava/classpath/gnu/xml/stream/AttributeImpl.java @@ -0,0 +1,124 @@ +/* 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 <a href='mailto:dog@gnu.org'>Chris Burdess</a> + */ +public class AttributeImpl + extends XMLEventImpl + implements Attribute +{ + + protected final QName name; + protected final String value; + protected final QName type; + protected final boolean specified; + + protected AttributeImpl(Location location, + QName name, String value, QName 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 QName 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/CharactersImpl.java b/libjava/classpath/gnu/xml/stream/CharactersImpl.java new file mode 100644 index 00000000000..6df06582b87 --- /dev/null +++ b/libjava/classpath/gnu/xml/stream/CharactersImpl.java @@ -0,0 +1,120 @@ +/* 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 <a href='mailto:dog@gnu.org'>Chris Burdess</a> + */ +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("<![CDATA["); + writer.write(data); + 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 00000000000..5863fb05aec --- /dev/null +++ b/libjava/classpath/gnu/xml/stream/CommentImpl.java @@ -0,0 +1,92 @@ +/* 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 <a href'mailto:dog@gnu.org'>Chris Burdess</a> + */ +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("<!--"); + writer.write(encode(text, false)); + 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 00000000000..8e008aaede1 --- /dev/null +++ b/libjava/classpath/gnu/xml/stream/DTDImpl.java @@ -0,0 +1,115 @@ +/* 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 <a href='mailto:dog@gnu.org'>Chris Burdess</a> + */ +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("<!DOCTYPE "); + writer.write(body); + 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 00000000000..7a5e2049e10 --- /dev/null +++ b/libjava/classpath/gnu/xml/stream/EndDocumentImpl.java @@ -0,0 +1,72 @@ +/* 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.IOException; +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 <a href='mailto:dog@gnu.org'>Chris Burdess</a> + */ +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 00000000000..7b5382ec5f3 --- /dev/null +++ b/libjava/classpath/gnu/xml/stream/EndElementImpl.java @@ -0,0 +1,108 @@ +/* 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 <a href='mailto:dog@gnu.org'>Chris Burdess</a> + */ +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/EndEntityImpl.java b/libjava/classpath/gnu/xml/stream/EndEntityImpl.java new file mode 100644 index 00000000000..fd36ee267d3 --- /dev/null +++ b/libjava/classpath/gnu/xml/stream/EndEntityImpl.java @@ -0,0 +1,80 @@ +/* EndEntityImpl.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.EndEntity; + +/** + * An end-entity event. + * + * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a> + */ +public class EndEntityImpl + extends XMLEventImpl + implements EndEntity +{ + + protected final String name; + + protected EndEntityImpl(Location location, String name) + { + super(location); + this.name = name; + } + + public int getEventType() + { + return END_ENTITY; + } + + public String getName() + { + return name; + } + + public void writeAsEncodedUnicode(Writer writer) + throws XMLStreamException + { + } + +} + diff --git a/libjava/classpath/gnu/xml/stream/EntityDeclarationImpl.java b/libjava/classpath/gnu/xml/stream/EntityDeclarationImpl.java new file mode 100644 index 00000000000..41ec2fb9b0d --- /dev/null +++ b/libjava/classpath/gnu/xml/stream/EntityDeclarationImpl.java @@ -0,0 +1,164 @@ +/* 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 <a href='mailto:dog@gnu.org'>Chris Burdess</a> + */ +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("<!ENTITY "); + writer.write(name); + writer.write(' '); + if (systemId != null) + { + if (publicId != null) + { + writer.write(" PUBLIC "); + writer.write('"'); + writer.write(publicId); + writer.write('"'); + writer.write(' '); + writer.write('"'); + writer.write(systemId); + writer.write('"'); + } + else + { + writer.write(" SYSTEM "); + writer.write('"'); + writer.write(systemId); + writer.write('"'); + } + if (notationName != null) + { + writer.write(" NDATA "); + writer.write(notationName); + } + } + else + { + writer.write('"'); + if (replacementText != null) + writer.write(replacementText); + writer.write('"'); + } + 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 00000000000..4b40bfa526a --- /dev/null +++ b/libjava/classpath/gnu/xml/stream/EntityReferenceImpl.java @@ -0,0 +1,132 @@ +/* EntityReferenceImpl.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; +import javax.xml.stream.events.EntityReference; + +/** + * An entity reference event. + * + * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a> + */ +public class EntityReferenceImpl + extends XMLEventImpl + implements EntityReference +{ + + //protected final EntityDeclaration decl; + protected final String name; + protected final String baseUri; + protected final String publicId; + protected final String systemId; + protected final String replacementText; + + protected EntityReferenceImpl(Location location, + //EntityDeclaration decl, + String name, + String baseUri, String publicId, + String systemId, String replacementText) + { + super(location); + //this.decl = decl; + this.name = name; + this.baseUri = baseUri; + this.publicId = publicId; + this.systemId = systemId; + this.replacementText = replacementText; + } + + public int getEventType() + { + return ENTITY_REFERENCE; + } + + /*public EntityDeclaration getDeclaration() + { + return decl; + }*/ + + public String getName() + { + return name; + } + + public String getBaseUri() + { + return baseUri; + } + + public String getPublicId() + { + return publicId; + } + + public String getSystemId() + { + return systemId; + } + + public String getReplacementText() + { + return replacementText; + } + + 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 00000000000..3bf0f2518b5 --- /dev/null +++ b/libjava/classpath/gnu/xml/stream/FilteredEventReader.java @@ -0,0 +1,102 @@ +/* FilteredEventReader.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.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() + throws XMLStreamException + { + // XXX ??? + return super.hasNext(); + } + + public XMLEvent next() + throws XMLStreamException + { + XMLEvent ret; + do + { + ret = super.next(); + } + while (!filter.accept(ret)); + return ret; + } + + 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 00000000000..1db02f6e30a --- /dev/null +++ b/libjava/classpath/gnu/xml/stream/FilteredStreamReader.java @@ -0,0 +1,91 @@ +/* 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.ReaderDelegate; + +class FilteredStreamReader + extends ReaderDelegate +{ + + 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/LocationImpl.java b/libjava/classpath/gnu/xml/stream/LocationImpl.java new file mode 100644 index 00000000000..1900aeb45c3 --- /dev/null +++ b/libjava/classpath/gnu/xml/stream/LocationImpl.java @@ -0,0 +1,89 @@ +/* LocationImpl.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.Location; + +/** + * Information about the location of an XML event within the underlying + * stream. + * + * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a> + */ +public class LocationImpl + implements Location +{ + + protected final int offset; + + protected final int col; + + protected final int line; + + protected final String systemId; + + protected LocationImpl(int offset, int col, int line, String systemId) + { + this.offset = offset; + this.col = col; + this.line = line; + this.systemId = systemId; + } + + public int getLineNumber() + { + return line; + } + + public int getColumnNumber() + { + return col; + } + + public int getCharacterOffset() + { + return offset; + } + + public String getLocationURI() + { + return systemId; + } + +} + diff --git a/libjava/classpath/gnu/xml/stream/NamespaceImpl.java b/libjava/classpath/gnu/xml/stream/NamespaceImpl.java new file mode 100644 index 00000000000..eeb57be6cac --- /dev/null +++ b/libjava/classpath/gnu/xml/stream/NamespaceImpl.java @@ -0,0 +1,111 @@ +/* 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.stream.Location; +import javax.xml.stream.XMLStreamException; +import javax.xml.stream.events.Namespace; + +/** + * A namespace declaration event. + * + * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a> + */ +public class NamespaceImpl + extends XMLEventImpl + implements Namespace +{ + + protected final String prefix; + protected final String uri; + + protected NamespaceImpl(Location location, String prefix, String uri) + { + super(location); + this.prefix = prefix; + this.uri = uri; + } + + public int getEventType() + { + return NAMESPACE; + } + + public String getPrefix() + { + return prefix; + } + + public String getNamespaceURI() + { + 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 00000000000..2d08599f577 --- /dev/null +++ b/libjava/classpath/gnu/xml/stream/NotationDeclarationImpl.java @@ -0,0 +1,126 @@ +/* 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 <a href='mailto:dog@gnu.org'>Chris Burdess</a> + */ +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("<!NOTATION "); + writer.write(name); + if (publicId != null) + { + writer.write(" PUBLIC "); + writer.write('"'); + writer.write(publicId); + writer.write('"'); + writer.write(' '); + writer.write('"'); + writer.write(systemId); + writer.write('"'); + } + else + { + writer.write(" SYSTEM "); + writer.write('"'); + writer.write(systemId); + writer.write('"'); + } + 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 00000000000..6a5028956ad --- /dev/null +++ b/libjava/classpath/gnu/xml/stream/ProcessingInstructionImpl.java @@ -0,0 +1,105 @@ +/* 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 <a href='mailto:dog@gnu.org'>Chris Burdess</a> + */ +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/StartDocumentImpl.java b/libjava/classpath/gnu/xml/stream/StartDocumentImpl.java new file mode 100644 index 00000000000..dc4251dd9bf --- /dev/null +++ b/libjava/classpath/gnu/xml/stream/StartDocumentImpl.java @@ -0,0 +1,144 @@ +/* StartDocumentImpl.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.StartDocument; + +/** + * A start-document event. + * + * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a> + */ +public class StartDocumentImpl + extends XMLEventImpl + implements StartDocument +{ + + protected final String systemId; + protected final String encoding; + protected final String xmlVersion; + protected final boolean xmlStandalone; + protected final boolean standaloneDeclared; + protected final boolean encodingDeclared; + + protected StartDocumentImpl(Location location, + String systemId, String encoding, + String xmlVersion, boolean xmlStandalone, + boolean standaloneDeclared, + boolean encodingDeclared) + { + super(location); + this.systemId = systemId; + this.encoding = encoding; + this.xmlVersion = xmlVersion; + this.xmlStandalone = xmlStandalone; + this.standaloneDeclared = standaloneDeclared; + this.encodingDeclared = encodingDeclared; + } + + public int getEventType() + { + return START_DOCUMENT; + } + + public String getSystemId() + { + return systemId; + } + + public String getCharacterEncodingScheme() + { + return encoding; + } + + public boolean encodingSet() + { + return encodingDeclared; + } + + public boolean isStandalone() + { + return xmlStandalone; + } + + public boolean standaloneSet() + { + return standaloneDeclared; + } + + public String getVersion() + { + return xmlVersion; + } + + public void writeAsEncodedUnicode(Writer writer) + throws XMLStreamException + { + try + { + writer.write("<?xml version='"); + writer.write(xmlVersion); + writer.write('\''); + if (standaloneDeclared) + { + writer.write(" standalone='"); + writer.write(xmlStandalone ? "yes" : "no"); + writer.write('\''); + } + if (encodingDeclared) + { + writer.write(" encoding='"); + writer.write(encoding); + writer.write('\''); + } + writer.write("?>"); + } + catch (IOException e) + { + XMLStreamException e2 = new XMLStreamException(e.getMessage()); + e2.initCause(e); + throw e2; + } + } + +} + diff --git a/libjava/classpath/gnu/xml/stream/StartElementImpl.java b/libjava/classpath/gnu/xml/stream/StartElementImpl.java new file mode 100644 index 00000000000..48f88656046 --- /dev/null +++ b/libjava/classpath/gnu/xml/stream/StartElementImpl.java @@ -0,0 +1,153 @@ +/* StartElementImpl.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.NamespaceContext; +import javax.xml.namespace.QName; +import javax.xml.stream.Location; +import javax.xml.stream.XMLStreamException; +import javax.xml.stream.events.Attribute; +import javax.xml.stream.events.Namespace; +import javax.xml.stream.events.StartElement; + +/** + * A start-element event. + * + * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a> + */ +public class StartElementImpl + extends XMLEventImpl + implements StartElement +{ + + protected final QName name; + protected final List attributes; + protected final List namespaces; + protected final NamespaceContext namespaceContext; + + protected StartElementImpl(Location location, + QName name, List attributes, List namespaces, + NamespaceContext namespaceContext) + { + super(location); + this.name = name; + this.attributes = attributes; + this.namespaces = namespaces; + this.namespaceContext = namespaceContext; + } + + public int getEventType() + { + return START_ELEMENT; + } + + public QName getName() + { + return name; + } + + public Iterator getAttributes() + { + return attributes.iterator(); + } + + public Iterator getNamespaces() + { + return namespaces.iterator(); + } + + public Attribute getAttributeByName(QName name) + { + for (Iterator i = attributes.iterator(); i.hasNext(); ) + { + Attribute attr = (Attribute) i.next(); + if (name.equals(attr.getName())) + return attr; + } + return null; + } + + public NamespaceContext getNamespaceContext() + { + return namespaceContext; + } + + public String getNamespaceURI(String prefix) + { + return namespaceContext.getNamespaceURI(prefix); + } + + 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()); + for (Iterator i = namespaces.iterator(); i.hasNext(); ) + { + writer.write(' '); + ((Namespace) i.next()).writeAsEncodedUnicode(writer); + } + for (Iterator i = attributes.iterator(); i.hasNext(); ) + { + writer.write(' '); + ((Attribute) i.next()).writeAsEncodedUnicode(writer); + } + writer.write('>'); + } + catch (IOException e) + { + XMLStreamException e2 = new XMLStreamException(e.getMessage()); + e2.initCause(e); + throw e2; + } + } + +} + diff --git a/libjava/classpath/gnu/xml/stream/StartEntityImpl.java b/libjava/classpath/gnu/xml/stream/StartEntityImpl.java new file mode 100644 index 00000000000..6e4ca257a2c --- /dev/null +++ b/libjava/classpath/gnu/xml/stream/StartEntityImpl.java @@ -0,0 +1,80 @@ +/* StartEntityImpl.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.StartEntity; + +/** + * A start-entity event. + * + * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a> + */ +public class StartEntityImpl + extends XMLEventImpl + implements StartEntity +{ + + protected final String name; + + protected StartEntityImpl(Location location, String name) + { + super(location); + this.name = name; + } + + public int getEventType() + { + return START_ENTITY; + } + + public String getName() + { + return name; + } + + public void writeAsEncodedUnicode(Writer writer) + throws XMLStreamException + { + } + +} + diff --git a/libjava/classpath/gnu/xml/stream/XMLEventAllocatorImpl.java b/libjava/classpath/gnu/xml/stream/XMLEventAllocatorImpl.java new file mode 100644 index 00000000000..4b21b6c7110 --- /dev/null +++ b/libjava/classpath/gnu/xml/stream/XMLEventAllocatorImpl.java @@ -0,0 +1,204 @@ +/* XMLEventAllocatorImpl.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.util.HashMap; +import java.util.LinkedList; +import java.util.List; +import java.util.Map; +import javax.xml.namespace.QName; +import javax.xml.stream.Location; +import javax.xml.stream.XMLStreamConstants; +import javax.xml.stream.XMLStreamException; +import javax.xml.stream.XMLStreamReader; +import javax.xml.stream.events.EntityDeclaration; +import javax.xml.stream.events.XMLEvent; +import javax.xml.stream.util.XMLEventAllocator; +import javax.xml.stream.util.XMLEventConsumer; + +/** + * Allocator for creating XML events based on a reader state. + * + * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a> + */ +public class XMLEventAllocatorImpl + implements XMLEventAllocator +{ + + protected Map entityDeclarations; + + protected XMLEventAllocatorImpl() + { + entityDeclarations = new HashMap(); + } + + public XMLEvent allocate(XMLStreamReader reader) + throws XMLStreamException + { + String text; + boolean whitespace; + boolean ignorableWhitespace; + int len; + List namespaces; + int eventType = reader.getEventType(); + Location location = reader.getLocation(); + switch (eventType) + { + case XMLStreamConstants.CDATA: + text = reader.getText(); + whitespace = isWhitespace(text); + // TODO ignorableWhitespace + ignorableWhitespace = whitespace && false; + return new CharactersImpl(location, text, + whitespace, true, ignorableWhitespace); + case XMLStreamConstants.CHARACTERS: + text = reader.getText(); + whitespace = false; + // TODO ignorableWhitespace + ignorableWhitespace = whitespace && false; + return new CharactersImpl(location, text, + whitespace, false, ignorableWhitespace); + case XMLStreamConstants.COMMENT: + text = reader.getText(); + return new CommentImpl(location, text); + case XMLStreamConstants.DTD: + text = reader.getText(); + List notations = new LinkedList(); + List entities = new LinkedList(); + // TODO readDTDBody(notations, entities); + return new DTDImpl(location, text, null, notations, entities); + case XMLStreamConstants.END_DOCUMENT: + return new EndDocumentImpl(location); + case XMLStreamConstants.END_ELEMENT: + len = reader.getNamespaceCount(); + namespaces = new LinkedList(); + for (int i = 0; i < len; i++) + namespaces.add(new NamespaceImpl(location, + reader.getNamespacePrefix(i), + reader.getNamespaceURI(i))); + return new EndElementImpl(location, + reader.getName(), + namespaces); + case XMLStreamConstants.ENTITY_REFERENCE: + String name = reader.getLocalName(); + //EntityDeclaration decl = + // (EntityDeclaration) entityDeclarations.get(name); + //return new EntityReferenceImpl(location, decl, name); + return new EntityReferenceImpl(location, name, null, null, null, null); + case XMLStreamConstants.PROCESSING_INSTRUCTION: + return new ProcessingInstructionImpl(location, + reader.getPITarget(), + reader.getPIData()); + case XMLStreamConstants.SPACE: + text = reader.getText(); + whitespace = true; + // TODO ignorableWhitespace + ignorableWhitespace = whitespace && false; + return new CharactersImpl(location, text, + whitespace, false, ignorableWhitespace); + case XMLStreamConstants.START_DOCUMENT: + String systemId = location.getLocationURI(); + String encoding = reader.getCharacterEncodingScheme(); + boolean encodingDeclared = encoding != null; + if (encoding == null) + { + encoding = reader.getEncoding(); + if (encoding == null) + encoding = "UTF-8"; + } + String xmlVersion = reader.getVersion(); + if (xmlVersion == null) + xmlVersion = "1.0"; + boolean xmlStandalone = reader.isStandalone(); + boolean standaloneDeclared = reader.standaloneSet(); + return new StartDocumentImpl(location, + systemId, + encoding, + xmlVersion, + xmlStandalone, + standaloneDeclared, + encodingDeclared); + case XMLStreamConstants.START_ELEMENT: + len = reader.getNamespaceCount(); + namespaces = new LinkedList(); + for (int i = 0; i < len; i++) + namespaces.add(new NamespaceImpl(location, + reader.getNamespacePrefix(i), + reader.getNamespaceURI(i))); + len = reader.getAttributeCount(); + List attributes = new LinkedList(); + for (int i = 0; i < len; i++) + attributes.add(new AttributeImpl(location, + reader.getAttributeQName(i), + reader.getAttributeValue(i), + QName.valueOf(reader.getAttributeType(i)), + reader.isAttributeSpecified(i))); + return new StartElementImpl(location, + reader.getName(), + attributes, namespaces, + reader.getNamespaceContext()); + default: + throw new XMLStreamException("Unknown event type: " + eventType); + } + } + + public void allocate(XMLStreamReader reader, XMLEventConsumer consumer) + throws XMLStreamException + { + consumer.add(allocate(reader)); + } + + public XMLEventAllocator newInstance() + { + return new XMLEventAllocatorImpl(); + } + + protected boolean isWhitespace(String text) + { + int len = text.length(); + for (int i = 0; i < len; i++) + { + char c = text.charAt(i); + if (c != 0x20 && c != 0x09 && c != 0x0a && c != 0x0d) + return false; + } + return true; + } + +} + diff --git a/libjava/classpath/gnu/xml/stream/XMLEventFactoryImpl.java b/libjava/classpath/gnu/xml/stream/XMLEventFactoryImpl.java new file mode 100644 index 00000000000..a839b182c09 --- /dev/null +++ b/libjava/classpath/gnu/xml/stream/XMLEventFactoryImpl.java @@ -0,0 +1,271 @@ +/* XMLEventFactoryImpl.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.util.Collections; +import java.util.Iterator; +import java.util.LinkedList; +import javax.xml.XMLConstants; +import javax.xml.namespace.NamespaceContext; +import javax.xml.namespace.QName; +import javax.xml.stream.Location; +import javax.xml.stream.XMLEventFactory; +import javax.xml.stream.events.Attribute; +import javax.xml.stream.events.Characters; +import javax.xml.stream.events.Comment; +import javax.xml.stream.events.DTD; +import javax.xml.stream.events.EndDocument; +import javax.xml.stream.events.EndElement; +import javax.xml.stream.events.EntityDeclaration; +import javax.xml.stream.events.EntityReference; +import javax.xml.stream.events.Namespace; +import javax.xml.stream.events.ProcessingInstruction; +import javax.xml.stream.events.StartDocument; +import javax.xml.stream.events.StartElement; + +/** + * Factory for XML events. + * + * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a> + */ +public class XMLEventFactoryImpl + extends XMLEventFactory +{ + + protected Location location; + + public void setLocation(Location location) + { + this.location = location; + } + + public Attribute createAttribute(String prefix, String namespaceURI, + String localName, String value) + { + return new AttributeImpl(location, + new QName(namespaceURI, localName, prefix), + value, QName.valueOf("CDATA"), true); + } + + public Attribute createAttribute(String localName, String value) + { + return new AttributeImpl(location, + new QName(localName), + value, QName.valueOf("CDATA"), true); + } + + public Attribute createAttribute(QName name, String value) + { + return new AttributeImpl(location, name, value, + QName.valueOf("CDATA"), true); + } + + public Namespace createNamespace(String namespaceURI) + { + return new NamespaceImpl(location, + XMLConstants.DEFAULT_NS_PREFIX, namespaceURI); + } + + public Namespace createNamespace(String prefix, String namespaceUri) + { + return new NamespaceImpl(location, prefix, namespaceUri); + } + + public StartElement createStartElement(QName name, + Iterator attributes, + Iterator namespaces) + { + return new StartElementImpl(location, name, + createLinkedList(attributes), + createLinkedList(namespaces), + null); + } + + public StartElement createStartElement(String prefix, + String namespaceUri, + String localName) + { + return new StartElementImpl(location, + new QName(namespaceUri, localName, prefix), + Collections.EMPTY_LIST, + Collections.EMPTY_LIST, + null); + } + + public StartElement createStartElement(String prefix, + String namespaceUri, + String localName, + Iterator attributes, + Iterator namespaces) + { + return new StartElementImpl(location, + new QName(namespaceUri, localName, prefix), + createLinkedList(attributes), + createLinkedList(namespaces), + null); + } + + public StartElement createStartElement(String prefix, + String namespaceUri, + String localName, + Iterator attributes, + Iterator namespaces, + NamespaceContext context) + { + return new StartElementImpl(location, + new QName(namespaceUri, localName, prefix), + createLinkedList(attributes), + createLinkedList(namespaces), + context); + } + + public EndElement createEndElement(QName name, + Iterator namespaces) + { + return new EndElementImpl(location, name, + createLinkedList(namespaces)); + } + + public EndElement createEndElement(String prefix, + String namespaceUri, + String localName) + { + return new EndElementImpl(location, + new QName(namespaceUri, localName, prefix), + Collections.EMPTY_LIST); + } + + public EndElement createEndElement(String prefix, + String namespaceUri, + String localName, + Iterator namespaces) + { + return new EndElementImpl(location, + new QName(namespaceUri, localName, prefix), + createLinkedList(namespaces)); + } + + public Characters createCharacters(String content) + { + return new CharactersImpl(location, content, false, false, false); + } + + public Characters createCData(String content) + { + return new CharactersImpl(location, content, false, true, false); + } + + public Characters createSpace(String content) + { + return new CharactersImpl(location, content, true, false, false); + } + + public Characters createIgnorableSpace(String content) + { + return new CharactersImpl(location, content, true, false, true); + } + + public StartDocument createStartDocument() + { + return new StartDocumentImpl(location, null, "UTF-8", "1.0", + false, false, false); + } + + public StartDocument createStartDocument(String encoding, + String version, + boolean standalone) + { + return new StartDocumentImpl(location, null, encoding, version, + standalone, true, true); + } + + public StartDocument createStartDocument(String encoding, + String version) + { + return new StartDocumentImpl(location, null, encoding, version, + false, false, true); + } + + public StartDocument createStartDocument(String encoding) + { + return new StartDocumentImpl(location, null, encoding, "1.0", + false, false, true); + } + + public EndDocument createEndDocument() + { + return new EndDocumentImpl(location); + } + + public EntityReference createEntityReference(String name, + //EntityDeclaration declaration) + String replacementText) + { + //return new EntityReferenceImpl(location, declaration, name); + return new EntityReferenceImpl(location, name, null, null, null, + replacementText); + } + + public Comment createComment(String text) + { + return new CommentImpl(location, text); + } + + public ProcessingInstruction createProcessingInstruction(String target, + String data) + { + return new ProcessingInstructionImpl(location, target, data); + } + + public DTD createDTD(String dtd) + { + return new DTDImpl(location, dtd, null, + Collections.EMPTY_LIST, + Collections.EMPTY_LIST); + } + + LinkedList createLinkedList(Iterator i) + { + LinkedList ret = new LinkedList(); + while (i.hasNext()) + ret.add(i.next()); + return ret; + } + +} + diff --git a/libjava/classpath/gnu/xml/stream/XMLEventImpl.java b/libjava/classpath/gnu/xml/stream/XMLEventImpl.java new file mode 100644 index 00000000000..a8b522f88a7 --- /dev/null +++ b/libjava/classpath/gnu/xml/stream/XMLEventImpl.java @@ -0,0 +1,208 @@ +/* XMLEventImpl.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.namespace.QName; +import javax.xml.stream.Location; +import javax.xml.stream.XMLStreamConstants; +import javax.xml.stream.XMLStreamException; +import javax.xml.stream.events.Characters; +import javax.xml.stream.events.EndElement; +import javax.xml.stream.events.StartElement; +import javax.xml.stream.events.XMLEvent; + +/** + * An XML stream event. + * + * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a> + */ +public abstract class XMLEventImpl + implements XMLEvent +{ + + protected final Location location; + + protected XMLEventImpl(Location location) + { + this.location = location; + } + + public abstract int getEventType(); + + public Location getLocation() + { + return location; + } + + public boolean isStartElement() + { + return getEventType() == START_ELEMENT; + } + + public boolean isAttribute() + { + return getEventType() == ATTRIBUTE; + } + + public boolean isNamespace() + { + return getEventType() == NAMESPACE; + } + + public boolean isEndElement() + { + return getEventType() == END_ELEMENT; + } + + public boolean isEntityReference() + { + return getEventType() == ENTITY_REFERENCE; + } + + public boolean isProcessingInstruction() + { + return getEventType() == PROCESSING_INSTRUCTION; + } + + public boolean isCharacters() + { + int et = getEventType(); + return et == CHARACTERS || et == CDATA; + } + + public boolean isStartDocument() + { + return getEventType() == START_DOCUMENT; + } + + public boolean isEndDocument() + { + return getEventType() == END_DOCUMENT; + } + + public boolean isStartEntity() + { + return getEventType() == START_ENTITY; + } + + public boolean isEndEntity() + { + return getEventType() == END_ENTITY; + } + + public StartElement asStartElement() + { + return (StartElement) this; + } + + public EndElement asEndElement() + { + return (EndElement) this; + } + + public Characters asCharacters() + { + return (Characters) this; + } + + public QName getSchemaType() + { + return null; + } + + public abstract void writeAsEncodedUnicode(Writer writer) + throws XMLStreamException; + + protected String encode(String text, boolean inAttr) + { + int len = text.length(); + StringBuffer buf = null; + for (int i = 0; i < len; i++) + { + char c = text.charAt(i); + if (c == '<') + { + if (buf == null) + { + buf = new StringBuffer(text.substring(0, i)); + } + buf.append("<"); + } + else if (c == '>') + { + if (buf == null) + { + buf = new StringBuffer(text.substring(0, i)); + } + buf.append(">"); + } + else if (c == '&') + { + if (buf == null) + { + buf = new StringBuffer(text.substring(0, i)); + } + buf.append("&"); + } + else if (c == '\'' && inAttr) + { + if (buf == null) + { + buf = new StringBuffer(text.substring(0, i)); + } + buf.append("'"); + } + else if (c == '"' && inAttr) + { + if (buf == null) + { + buf = new StringBuffer(text.substring(0, i)); + } + buf.append("""); + } + else if (buf != null) + { + buf.append(c); + } + } + return (buf == null) ? text : buf.toString(); + } + +} + diff --git a/libjava/classpath/gnu/xml/stream/XMLEventReaderImpl.java b/libjava/classpath/gnu/xml/stream/XMLEventReaderImpl.java new file mode 100644 index 00000000000..70481d7c407 --- /dev/null +++ b/libjava/classpath/gnu/xml/stream/XMLEventReaderImpl.java @@ -0,0 +1,125 @@ +/* XMLEventReaderImpl.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.XMLEventReader; +import javax.xml.stream.XMLStreamConstants; +import javax.xml.stream.XMLStreamException; +import javax.xml.stream.XMLStreamReader; +import javax.xml.stream.events.XMLEvent; +import javax.xml.stream.util.XMLEventAllocator; + +/** + * Parser using XML events. + * + * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a> + */ +public class XMLEventReaderImpl + implements XMLEventReader +{ + + protected final XMLStreamReader reader; + protected final XMLEventAllocator allocator; + protected final String systemId; + protected XMLEvent peekEvent; + + protected XMLEventReaderImpl(XMLStreamReader reader, + XMLEventAllocator allocator, + String systemId) + { + this.reader = reader; + this.allocator = allocator; + this.systemId = systemId; + } + + public XMLEvent next() + throws XMLStreamException + { + XMLEvent ret = peek(); + peekEvent = null; + return ret; + } + + public boolean hasNext() + throws XMLStreamException + { + return peekEvent != null || reader.hasNext(); + } + + public XMLEvent peek() + throws XMLStreamException + { + if (peekEvent != null) + return peekEvent; + if (!reader.hasNext()) + return null; + reader.next(); + peekEvent = allocator.allocate(reader); + return peekEvent; + } + + public String getElementText() + throws XMLStreamException + { + return reader.getElementText(); + } + + public XMLEvent nextTag() + throws XMLStreamException + { + if (peekEvent != null) + { + int eventType = peekEvent.getEventType(); + if (eventType == XMLStreamConstants.START_ELEMENT || + eventType == XMLStreamConstants.END_ELEMENT) + return peekEvent; + else + peekEvent = null; + } + reader.nextTag(); + return allocator.allocate(reader); + } + + public Object getProperty(String name) + throws IllegalArgumentException + { + return reader.getProperty(name); + } + +} + diff --git a/libjava/classpath/gnu/xml/stream/XMLEventWriterImpl.java b/libjava/classpath/gnu/xml/stream/XMLEventWriterImpl.java new file mode 100644 index 00000000000..45024158da6 --- /dev/null +++ b/libjava/classpath/gnu/xml/stream/XMLEventWriterImpl.java @@ -0,0 +1,191 @@ +/* XMLEventWriterImpl.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.namespace.NamespaceContext; +import javax.xml.namespace.QName; +import javax.xml.stream.XMLEventReader; +import javax.xml.stream.XMLEventWriter; +import javax.xml.stream.XMLStreamConstants; +import javax.xml.stream.XMLStreamException; +import javax.xml.stream.XMLStreamWriter; +import javax.xml.stream.events.Attribute; +import javax.xml.stream.events.Characters; +import javax.xml.stream.events.Comment; +import javax.xml.stream.events.DTD; +import javax.xml.stream.events.Namespace; +import javax.xml.stream.events.ProcessingInstruction; +import javax.xml.stream.events.StartDocument; +import javax.xml.stream.events.StartElement; +import javax.xml.stream.events.XMLEvent; + +/** + * Writer to write events to an underlying XML stream writer. + * + * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a> + */ +public class XMLEventWriterImpl + implements XMLEventWriter +{ + + protected final XMLStreamWriter writer; + + protected XMLEventWriterImpl(XMLStreamWriter writer) + { + this.writer = writer; + } + + public void flush() + throws XMLStreamException + { + writer.flush(); + } + + public void close() + throws XMLStreamException + { + writer.close(); + } + + public void add(XMLEvent event) + throws XMLStreamException + { + QName name; + String uri; + switch (event.getEventType()) + { + case XMLStreamConstants.START_ELEMENT: + StartElement startElement = event.asStartElement(); + name = startElement.getName(); + uri = name.getNamespaceURI(); + if (uri != null && !"".equals(uri)) + writer.writeStartElement(name.getPrefix(), name.getLocalPart(), uri); + else + writer.writeStartElement(name.getLocalPart()); + break; + case XMLStreamConstants.END_ELEMENT: + writer.writeEndElement(); + break; + case XMLStreamConstants.ATTRIBUTE: + Attribute attribute = (Attribute) event; + name = attribute.getName(); + uri = name.getNamespaceURI(); + if (uri != null && !"".equals(uri)) + writer.writeAttribute(name.getPrefix(), uri, name.getLocalPart(), + attribute.getValue()); + else + writer.writeAttribute(name.getLocalPart(), attribute.getValue()); + break; + case XMLStreamConstants.NAMESPACE: + Namespace namespace = (Namespace) event; + uri = namespace.getNamespaceURI(); + writer.writeNamespace(namespace.getPrefix(), uri); + break; + case XMLStreamConstants.PROCESSING_INSTRUCTION: + ProcessingInstruction pi = (ProcessingInstruction) event; + String data = pi.getData(); + if (data == null) + writer.writeProcessingInstruction(pi.getTarget()); + else + writer.writeProcessingInstruction(pi.getTarget(), data); + break; + case XMLStreamConstants.COMMENT: + Comment comment = (Comment) event; + writer.writeComment(comment.getText()); + break; + case XMLStreamConstants.START_DOCUMENT: + StartDocument startDocument = (StartDocument) event; + writer.writeStartDocument(startDocument.getVersion()); + break; + case XMLStreamConstants.END_DOCUMENT: + writer.writeEndDocument(); + break; + case XMLStreamConstants.DTD: + DTD dtd = (DTD) event; + writer.writeDTD(dtd.getDocumentTypeDeclaration()); + break; + case XMLStreamConstants.CHARACTERS: + case XMLStreamConstants.SPACE: + Characters characters = event.asCharacters(); + writer.writeCharacters(characters.getData()); + break; + case XMLStreamConstants.CDATA: + Characters cdata = event.asCharacters(); + writer.writeCData(cdata.getData()); + break; + } + } + + public void add(XMLEventReader reader) + throws XMLStreamException + { + while (reader.hasNext()) + add(reader.next()); + } + + public String getPrefix(String uri) + throws XMLStreamException + { + return writer.getPrefix(uri); + } + + public void setPrefix(String prefix, String uri) + throws XMLStreamException + { + writer.setPrefix(prefix, uri); + } + + public void setDefaultNamespace(String uri) + throws XMLStreamException + { + writer.setDefaultNamespace(uri); + } + + public void setNamespaceContext(NamespaceContext context) + throws XMLStreamException + { + writer.setNamespaceContext(context); + } + + public NamespaceContext getNamespaceContext() + { + return writer.getNamespaceContext(); + } + +} + diff --git a/libjava/classpath/gnu/xml/stream/XMLInputFactoryImpl.java b/libjava/classpath/gnu/xml/stream/XMLInputFactoryImpl.java new file mode 100644 index 00000000000..c99f564b623 --- /dev/null +++ b/libjava/classpath/gnu/xml/stream/XMLInputFactoryImpl.java @@ -0,0 +1,321 @@ +/* XMLInputFactoryImpl.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.File; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.InputStream; +import java.io.IOException; +import java.io.Reader; +import java.net.MalformedURLException; +import java.net.URL; + +import javax.xml.transform.Source; +import javax.xml.transform.stream.StreamSource; +import javax.xml.stream.EventFilter; +import javax.xml.stream.StreamFilter; +import javax.xml.stream.XMLEventReader; +import javax.xml.stream.XMLReporter; +import javax.xml.stream.XMLResolver; +import javax.xml.stream.XMLStreamException; +import javax.xml.stream.XMLStreamReader; +import javax.xml.stream.XMLInputFactory; +import javax.xml.stream.util.XMLEventAllocator; + +/** + * Factory for creating parsers from various kinds of XML source. + * + * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a> + */ +public class XMLInputFactoryImpl + extends XMLInputFactory +{ + + protected XMLResolver resolver; + protected XMLReporter reporter; + protected XMLEventAllocator allocator; + + protected boolean validating; + protected boolean namespaceAware = true; + protected boolean coalescing; + protected boolean replacingEntityReferences = true; + protected boolean externalEntities = true; + protected boolean supportDTD = true; + + public XMLInputFactoryImpl() + { + allocator = new XMLEventAllocatorImpl(); + } + + public XMLStreamReader createXMLStreamReader(Reader reader) + throws XMLStreamException + { + return new XMLStreamReaderImpl(reader, null, null, + resolver, reporter, + validating, namespaceAware, + coalescing, replacingEntityReferences, + externalEntities, supportDTD); + } + + public XMLStreamReader createXMLStreamReader(Source source) + throws XMLStreamException + { + String systemId = source.getSystemId(); + InputStream in = getInputStream(source); + return new XMLStreamReaderImpl(in, null, systemId, + resolver, reporter, + validating, namespaceAware, + coalescing, replacingEntityReferences, + externalEntities, supportDTD); + } + + public XMLStreamReader createXMLStreamReader(InputStream in) + throws XMLStreamException + { + return new XMLStreamReaderImpl(in, null, null, + resolver, reporter, + validating, namespaceAware, + coalescing, replacingEntityReferences, + externalEntities, supportDTD); + } + + public XMLStreamReader createXMLStreamReader(InputStream in, String encoding) + throws XMLStreamException + { + return createXMLStreamReader(in); + } + + public XMLEventReader createXMLEventReader(Reader reader) + throws XMLStreamException + { + XMLStreamReader sr = createXMLStreamReader(reader); + return new XMLEventReaderImpl(sr, allocator, null); + } + + public XMLEventReader createXMLEventReader(XMLStreamReader reader) + throws XMLStreamException + { + return new XMLEventReaderImpl(reader, allocator, null); + } + + public XMLEventReader createXMLEventReader(Source source) + throws XMLStreamException + { + XMLStreamReader sr = createXMLStreamReader(source); + return new XMLEventReaderImpl(sr, allocator, null); + } + + public XMLEventReader createXMLEventReader(InputStream in) + throws XMLStreamException + { + XMLStreamReader sr = createXMLStreamReader(in); + return new XMLEventReaderImpl(sr, allocator, null); + } + + public XMLEventReader createXMLEventReader(InputStream in, String encoding) + throws XMLStreamException + { + XMLStreamReader sr = createXMLStreamReader(in, encoding); + return new XMLEventReaderImpl(sr, allocator, null); + } + + public XMLStreamReader createFilteredReader(XMLStreamReader reader, + StreamFilter filter) + throws XMLStreamException + { + return new FilteredStreamReader(reader, filter); + } + + public XMLEventReader createFilteredReader(XMLEventReader reader, + EventFilter filter) + throws XMLStreamException + { + return new FilteredEventReader(reader, filter); + } + + public XMLResolver getXMLResolver() + { + return resolver; + } + + public void setXMLResolver(XMLResolver resolver) + { + this.resolver = resolver; + } + + public XMLReporter getXMLReporter() + { + return reporter; + } + + public void setXMLReporter(XMLReporter reporter) + { + this.reporter = reporter; + } + + public void setProperty(String name, Object value) + throws IllegalArgumentException + { + if (name.equals(IS_NAMESPACE_AWARE)) + namespaceAware = ((Boolean) value).booleanValue(); + else if (name.equals(IS_VALIDATING)) + validating = ((Boolean) value).booleanValue(); + else if (name.equals(IS_COALESCING)) + coalescing = ((Boolean) value).booleanValue(); + else if (name.equals(IS_REPLACING_ENTITY_REFERENCES)) + replacingEntityReferences = ((Boolean) value).booleanValue(); + else if (name.equals(IS_SUPPORTING_EXTERNAL_ENTITIES)) + externalEntities = ((Boolean) value).booleanValue(); + else if (name.equals(SUPPORT_DTD)) + supportDTD = ((Boolean) value).booleanValue(); + else if (name.equals(REPORTER)) + reporter = (XMLReporter) value; + else if (name.equals(RESOLVER)) + resolver = (XMLResolver) value; + else if (name.equals(ALLOCATOR)) + allocator = (XMLEventAllocator) value; + else + throw new IllegalArgumentException(name); + } + + public Object getProperty(String name) + throws IllegalArgumentException + { + if (name.equals(IS_NAMESPACE_AWARE)) + return namespaceAware ? Boolean.TRUE : Boolean.FALSE; + if (name.equals(IS_VALIDATING)) + return validating ? Boolean.TRUE : Boolean.FALSE; + if (name.equals(IS_COALESCING)) + return coalescing ? Boolean.TRUE : Boolean.FALSE; + if (name.equals(IS_REPLACING_ENTITY_REFERENCES)) + return replacingEntityReferences ? Boolean.TRUE : Boolean.FALSE; + if (name.equals(IS_SUPPORTING_EXTERNAL_ENTITIES)) + return externalEntities ? Boolean.TRUE : Boolean.FALSE; + if (name.equals(SUPPORT_DTD)) + return supportDTD ? Boolean.TRUE : Boolean.FALSE; + if (name.equals(REPORTER)) + return reporter; + if (name.equals(RESOLVER)) + return resolver; + if (name.equals(ALLOCATOR)) + return allocator; + throw new IllegalArgumentException(name); + } + + public boolean isPropertySupported(String name) + { + return name.equals(IS_NAMESPACE_AWARE) || + name.equals(IS_VALIDATING) || + name.equals(IS_COALESCING) || + name.equals(IS_REPLACING_ENTITY_REFERENCES) || + name.equals(IS_SUPPORTING_EXTERNAL_ENTITIES) || + name.equals(SUPPORT_DTD) || + name.equals(REPORTER) || + name.equals(RESOLVER) || + name.equals(ALLOCATOR); + } + + public void setEventAllocator(XMLEventAllocator allocator) + { + this.allocator = allocator; + } + + public XMLEventAllocator getEventAllocator() + { + return allocator; + } + + public void setCoalescing(boolean coalescing) + { + this.coalescing = coalescing; + } + + public boolean isCoalescing() + { + return coalescing; + } + + protected InputStream getInputStream(Source source) + throws XMLStreamException + { + InputStream in = null; + if (source instanceof StreamSource) + { + StreamSource streamSource = (StreamSource) source; + in = streamSource.getInputStream(); + } + if (in == null) + { + String systemId = source.getSystemId(); + try + { + URL url = new URL(systemId); + try + { + in = url.openStream(); + } + catch (IOException e2) + { + XMLStreamException e3 = new XMLStreamException(e2); + e3.initCause(e2); + throw e3; + } + } + catch (MalformedURLException e) + { + // Fall back to relative file + if (File.separatorChar != '/') + systemId = systemId.replace('/', File.separatorChar); + try + { + in = new FileInputStream(systemId); + } + catch (FileNotFoundException e2) + { + XMLStreamException e3 = new XMLStreamException(e2); + e3.initCause(e2); + throw e3; + } + } + } + return in; + } + +} + diff --git a/libjava/classpath/gnu/xml/stream/XMLOutputFactoryImpl.java b/libjava/classpath/gnu/xml/stream/XMLOutputFactoryImpl.java new file mode 100644 index 00000000000..25f520416e2 --- /dev/null +++ b/libjava/classpath/gnu/xml/stream/XMLOutputFactoryImpl.java @@ -0,0 +1,153 @@ +/* XMLOutputFactoryImpl.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.OutputStream; +import java.io.OutputStreamWriter; +import java.io.Writer; +import java.io.UnsupportedEncodingException; + +import javax.xml.stream.XMLEventWriter; +import javax.xml.stream.XMLOutputFactory; +import javax.xml.stream.XMLStreamException; +import javax.xml.stream.XMLStreamWriter; + +/** + * Standard output factory. + * + * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a> + */ +public class XMLOutputFactoryImpl + extends XMLOutputFactory +{ + + protected boolean prefixDefaulting = false; + + public XMLOutputFactoryImpl() + { + } + + public XMLStreamWriter createXMLStreamWriter(Writer stream) + throws XMLStreamException + { + // XXX try to determine character encoding of writer? + return new XMLStreamWriterImpl(stream, null, prefixDefaulting); + } + + public XMLStreamWriter createXMLStreamWriter(OutputStream stream) + throws XMLStreamException + { + return createXMLStreamWriter(stream, "UTF-8"); + } + + public XMLStreamWriter createXMLStreamWriter(OutputStream stream, + String encoding) + throws XMLStreamException + { + if (encoding == null) + encoding = "UTF-8"; + try + { + Writer writer = new OutputStreamWriter(stream, encoding); + return new XMLStreamWriterImpl(writer, encoding, prefixDefaulting); + } + catch (UnsupportedEncodingException e) + { + XMLStreamException e2 = new XMLStreamException(e); + e2.initCause(e); + throw e2; + } + } + + public XMLEventWriter createXMLEventWriter(OutputStream stream) + throws XMLStreamException + { + XMLStreamWriter writer = createXMLStreamWriter(stream); + return new XMLEventWriterImpl(writer); + } + + public XMLEventWriter createXMLEventWriter(OutputStream stream, + String encoding) + throws XMLStreamException + { + XMLStreamWriter writer = createXMLStreamWriter(stream, encoding); + return new XMLEventWriterImpl(writer); + } + + public XMLEventWriter createXMLEventWriter(Writer stream) + throws XMLStreamException + { + XMLStreamWriter writer = createXMLStreamWriter(stream); + return new XMLEventWriterImpl(writer); + } + + public void setProperty(String name, Object value) + throws IllegalArgumentException + { + if (IS_PREFIX_DEFAULTING.equals(name)) + prefixDefaulting = ((Boolean) value).booleanValue(); + throw new IllegalArgumentException(name); + } + + public Object getProperty(String name) + throws IllegalArgumentException + { + if (IS_PREFIX_DEFAULTING.equals(name)) + return new Boolean(prefixDefaulting); + throw new IllegalArgumentException(name); + } + + public boolean isPropertySupported(String name) + { + if (IS_PREFIX_DEFAULTING.equals(name)) + return true; + return false; + } + + public boolean isPrefixDefaulting() + { + return prefixDefaulting; + } + + public void setPrefixDefaulting(boolean value) + { + prefixDefaulting = value; + } + +} + diff --git a/libjava/classpath/gnu/xml/stream/XMLStreamReaderImpl.java b/libjava/classpath/gnu/xml/stream/XMLStreamReaderImpl.java new file mode 100644 index 00000000000..568d800ae0d --- /dev/null +++ b/libjava/classpath/gnu/xml/stream/XMLStreamReaderImpl.java @@ -0,0 +1,1037 @@ +/* XMLStreamReaderImpl.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.InputStream; +import java.io.IOException; +import java.io.Reader; +import java.util.Enumeration; +import java.util.HashMap; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.Map; +import javax.xml.namespace.NamespaceContext; +import javax.xml.namespace.QName; +import javax.xml.parsers.ParserConfigurationException; +import javax.xml.parsers.SAXParser; +import javax.xml.parsers.SAXParserFactory; +import javax.xml.stream.Location; +import javax.xml.stream.XMLResolver; +import javax.xml.stream.XMLReporter; +import javax.xml.stream.XMLStreamConstants; +import javax.xml.stream.XMLStreamException; +import javax.xml.stream.XMLStreamReader; + +import javax.xml.stream.events.Attribute; +import javax.xml.stream.events.Characters; +import javax.xml.stream.events.Comment; +import javax.xml.stream.events.DTD; +import javax.xml.stream.events.EndDocument; +import javax.xml.stream.events.EndElement; +import javax.xml.stream.events.EndEntity; +import javax.xml.stream.events.EntityDeclaration; +import javax.xml.stream.events.EntityReference; +import javax.xml.stream.events.Namespace; +import javax.xml.stream.events.NotationDeclaration; +import javax.xml.stream.events.ProcessingInstruction; +import javax.xml.stream.events.StartDocument; +import javax.xml.stream.events.StartElement; +import javax.xml.stream.events.StartEntity; +import javax.xml.stream.events.XMLEvent; + +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.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; +import org.xml.sax.helpers.NamespaceSupport; + +/** + * An XML parser. + * + * This implementation uses SAX to create a series of events in memory, + * and then iterates over this series. This has the advantage of being simple + * and unifying the existing XML parsing code. However, it is quite + * memory-inefficient and obviously won't cope with streams of arbitrary + * length. + * + * A future task could be to write a real, progressive/incremental + * implementation of this class. In that case we should consider making that + * the default XML parser implementation and using a SAX wrapper to it to + * provide the GNU SAX implementation. + * + * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a> + */ +public class XMLStreamReaderImpl + implements XMLStreamReader, NamespaceContext +{ + + private LinkedList events; + private XMLEvent currentEvent; + private int eventType; + private NamespaceSupport namespaces; + + protected String publicId; + protected String systemId; + + protected XMLResolver resolver; + protected XMLReporter reporter; + protected boolean validating; + protected boolean namespaceAware; + protected boolean coalescing; + protected boolean replacingEntityReferences; + protected boolean externalEntities; + protected boolean supportDTD; + + protected XMLStreamReaderImpl(InputStream in, + String publicId, + String systemId, + XMLResolver resolver, + XMLReporter reporter, + boolean validating, + boolean namespaceAware, + boolean coalescing, + boolean replacingEntityReferences, + boolean externalEntities, + boolean supportDTD) + throws XMLStreamException + { + //this.in = in; + this.publicId = publicId; + this.systemId = systemId; + this.resolver = resolver; + this.reporter = reporter; + this.validating = validating; + this.namespaceAware = namespaceAware; + this.coalescing = coalescing; + this.replacingEntityReferences = replacingEntityReferences; + this.externalEntities = externalEntities; + this.supportDTD = supportDTD; + namespaces = new NamespaceSupport(); + events = new LinkedList(); + + // Configure the SAX parser and perform the parse + try + { + SAXParserFactory f = SAXParserFactory.newInstance(); + f.setNamespaceAware(namespaceAware); + f.setValidating(validating); + SAXParser p = f.newSAXParser(); + XMLReader r = p.getXMLReader(); + CallbackHandler ch = this.new CallbackHandler(r); + r.setFeature("http://xml.org/sax/features/external-general-entities", + externalEntities); + r.setFeature("http://xml.org/sax/features/namespaces", + namespaceAware); + r.setContentHandler(ch); + r.setDTDHandler(ch); + r.setEntityResolver(ch); + r.setErrorHandler(ch); + r.setProperty("http://xml.org/sax/properties/lexical-handler", + ch); + InputSource source = new InputSource(in); + source.setSystemId(systemId); + r.parse(source); + } + catch (SAXException e) + { + events.add(e); + } + catch (IOException e) + { + events.add(e); + } + catch (ParserConfigurationException e) + { + XMLStreamException e2 = new XMLStreamException(e); + e2.initCause(e); + throw e2; + } + } + + protected XMLStreamReaderImpl(Reader reader, + String publicId, + String systemId, + XMLResolver resolver, + XMLReporter reporter, + boolean validating, + boolean namespaceAware, + boolean coalescing, + boolean replacingEntityReferences, + boolean externalEntities, + boolean supportDTD) + throws XMLStreamException + { + //this.reader = reader; + this.publicId = publicId; + this.systemId = systemId; + this.resolver = resolver; + this.reporter = reporter; + this.validating = validating; + this.namespaceAware = namespaceAware; + this.coalescing = coalescing; + this.replacingEntityReferences = replacingEntityReferences; + this.externalEntities = externalEntities; + this.supportDTD = supportDTD; + namespaces = new NamespaceSupport(); + events = new LinkedList(); + + // Configure the SAX parser and perform the parse + try + { + SAXParserFactory f = SAXParserFactory.newInstance(); + f.setNamespaceAware(namespaceAware); + f.setValidating(validating); + SAXParser p = f.newSAXParser(); + XMLReader r = p.getXMLReader(); + CallbackHandler ch = this.new CallbackHandler(r); + r.setFeature("http://xml.org/sax/features/external-general-entities", + externalEntities); + r.setFeature("http://xml.org/sax/features/namespaces", + namespaceAware); + r.setContentHandler(ch); + r.setDTDHandler(ch); + r.setEntityResolver(ch); + r.setErrorHandler(ch); + r.setProperty("http://xml.org/sax/properties/lexical-handler", + ch); + InputSource source = new InputSource(reader); + source.setSystemId(systemId); + r.parse(source); + } + catch (SAXException e) + { + events.add(e); + } + catch (IOException e) + { + events.add(e); + } + catch (ParserConfigurationException e) + { + XMLStreamException e2 = new XMLStreamException(e); + e2.initCause(e); + throw e2; + } + } + + public Object getProperty(String name) + throws IllegalArgumentException + { + throw new IllegalArgumentException(name); + } + + public int next() + throws XMLStreamException + { + if (events.isEmpty()) + throw new XMLStreamException("EOF"); + Object event = events.removeFirst(); + if (event instanceof Exception) + { + Exception e = (Exception) event; + XMLStreamException e2 = new XMLStreamException(e); + e2.initCause(e); + throw e2; + } + currentEvent = (XMLEvent) event; + eventType = currentEvent.getEventType(); + return eventType; + } + + public void require(int type, String namespaceURI, String localName) + throws XMLStreamException + { + // TODO + throw new UnsupportedOperationException(); + } + + public String getElementText() + throws XMLStreamException + { + // TODO + throw new UnsupportedOperationException(); + } + + public int nextTag() + throws XMLStreamException + { + int ret; + do + { + ret = next(); + } + while (ret != XMLStreamConstants.START_ELEMENT && + ret != XMLStreamConstants.END_ELEMENT); + return ret; + } + + public boolean hasNext() + throws XMLStreamException + { + return !events.isEmpty(); + } + + public void close() + throws XMLStreamException + { + } + + public String getNamespaceURI(String prefix) + { + return namespaces.getURI(prefix); + } + + public String getPrefix(String namespaceURI) + { + return namespaces.getPrefix(namespaceURI); + } + + public Iterator getPrefixes(String namespaceURI) + { + LinkedList acc = new LinkedList(); + for (Enumeration e = namespaces.getPrefixes(namespaceURI); + e.hasMoreElements(); ) + acc.add(e.nextElement()); + return acc.iterator(); + } + + public boolean isStartElement() + { + return eventType == START_ELEMENT; + } + + public boolean isEndElement() + { + return eventType == END_ELEMENT; + } + + public boolean isCharacters() + { + return eventType == CHARACTERS || eventType == CDATA; + } + + public boolean isWhiteSpace() + { + return eventType == SPACE; + } + + public String getAttributeValue(String namespaceURI, String localName) + { + StartElement se = (StartElement) currentEvent; + for (Iterator i = se.getAttributes(); i.hasNext(); ) + { + Attribute attr = (Attribute) i.next(); + QName name = attr.getName(); + if (namespaceURI != null && + !namespaceURI.equals(name.getNamespaceURI())) + continue; + if (!localName.equals(name.getLocalPart())) + continue; + return attr.getValue(); + } + return null; + } + + public int getAttributeCount() + { + StartElement se = (StartElement) currentEvent; + int count = 0; + for (Iterator i = se.getAttributes(); i.hasNext(); ) + { + i.next(); + count++; + } + return count; + } + + public QName getAttributeQName(int index) + { + StartElement se = (StartElement) currentEvent; + int count = 0; + for (Iterator i = se.getAttributes(); i.hasNext(); ) + { + Attribute attr = (Attribute) i.next(); + if (index == count) + return attr.getName(); + count++; + } + return null; + } + + public String getAttributeNamespace(int index) + { + QName name = getAttributeQName(index); + return (name == null) ? null : name.getNamespaceURI(); + } + + public String getAttributeName(int index) + { + QName name = getAttributeQName(index); + return (name == null) ? null : name.getLocalPart(); + } + + public String getAttributePrefix(int index) + { + QName name = getAttributeQName(index); + return (name == null) ? null : name.getPrefix(); + } + + public String getAttributeType(int index) + { + StartElement se = (StartElement) currentEvent; + int count = 0; + for (Iterator i = se.getAttributes(); i.hasNext(); ) + { + Attribute attr = (Attribute) i.next(); + if (index == count) + { + QName type = attr.getDTDType(); + return (type == null) ? "CDATA" : type.toString(); + } + count++; + } + return null; + } + + public String getAttributeValue(int index) + { + StartElement se = (StartElement) currentEvent; + int count = 0; + for (Iterator i = se.getAttributes(); i.hasNext(); ) + { + Attribute attr = (Attribute) i.next(); + if (index == count) + return attr.getValue(); + count++; + } + return null; + } + + public boolean isAttributeSpecified(int index) + { + StartElement se = (StartElement) currentEvent; + int count = 0; + for (Iterator i = se.getAttributes(); i.hasNext(); ) + { + Attribute attr = (Attribute) i.next(); + if (index == count) + return attr.isSpecified(); + count++; + } + return false; + } + + public int getNamespaceCount() + { + Iterator i = null; + switch (eventType) + { + case XMLStreamConstants.START_ELEMENT: + i = ((StartElement) currentEvent).getNamespaces(); + break; + case XMLStreamConstants.END_ELEMENT: + i = ((EndElement) currentEvent).getNamespaces(); + break; + default: + throw new IllegalStateException(); + } + int count = 0; + while (i.hasNext()) + { + i.next(); + count++; + } + return count; + } + + public String getNamespacePrefix(int index) + { + Iterator i = null; + switch (eventType) + { + case XMLStreamConstants.START_ELEMENT: + i = ((StartElement) currentEvent).getNamespaces(); + break; + case XMLStreamConstants.END_ELEMENT: + i = ((EndElement) currentEvent).getNamespaces(); + break; + default: + throw new IllegalStateException(); + } + int count = 0; + while (i.hasNext()) + { + Namespace ns = (Namespace) i.next(); + if (index == count) + return ns.getPrefix(); + count++; + } + return null; + } + + public String getNamespaceURI(int index) + { + Iterator i = null; + switch (eventType) + { + case XMLStreamConstants.START_ELEMENT: + i = ((StartElement) currentEvent).getNamespaces(); + break; + case XMLStreamConstants.END_ELEMENT: + i = ((EndElement) currentEvent).getNamespaces(); + break; + default: + throw new IllegalStateException(); + } + int count = 0; + while (i.hasNext()) + { + Namespace ns = (Namespace) i.next(); + if (index == count) + return ns.getNamespaceURI(); + count++; + } + return null; + } + + public NamespaceContext getNamespaceContext() + { + return this; + } + + public int getEventType() + { + return eventType; + } + + public String getText() + { + switch (eventType) + { + case XMLStreamConstants.CHARACTERS: + case XMLStreamConstants.CDATA: + case XMLStreamConstants.SPACE: + return ((Characters) currentEvent).getData(); + case XMLStreamConstants.COMMENT: + return ((Comment) currentEvent).getText(); + case XMLStreamConstants.ENTITY_REFERENCE: + return ((EntityReference) currentEvent).getReplacementText(); + case XMLStreamConstants.DTD: + return ((DTD) currentEvent).getDocumentTypeDeclaration(); + } + return null; + } + + public char[] getTextCharacters() + { + String text = getText(); + return (text == null) ? null : text.toCharArray(); + } + + public int getTextCharacters(int sourceStart, char[] target, + int targetStart, int length) + throws XMLStreamException + { + char[] source = getTextCharacters(); + int len = Math.min(source.length, length); + System.arraycopy(source, sourceStart, target, targetStart, len); + return len; + } + + public int getTextStart() + { + return 0; + } + + public int getTextLength() + { + String text = getText(); + return (text == null) ? 0 : text.length(); + } + + public String getEncoding() + { + // XXX SAX doesn't provide this + return null; + } + + public boolean hasText() + { + return eventType == CHARACTERS || eventType == DTD || + eventType == SPACE || eventType == ENTITY_REFERENCE || + eventType == COMMENT || eventType == DTD; + } + + public Location getLocation() + { + return currentEvent.getLocation(); + } + + public QName getName() + { + switch (eventType) + { + case XMLStreamConstants.START_ELEMENT: + return ((StartElement) currentEvent).getName(); + case XMLStreamConstants.END_ELEMENT: + return ((EndElement) currentEvent).getName(); + case XMLStreamConstants.ATTRIBUTE: + return ((Attribute) currentEvent).getName(); + } + return null; + } + + public String getLocalName() + { + QName name = getName(); + return (name == null) ? null : name.getLocalPart(); + } + + public boolean hasName() + { + return getName() != null; + } + + public String getNamespaceURI() + { + QName name = getName(); + return (name == null) ? null : name.getNamespaceURI(); + } + + public String getPrefix() + { + QName name = getName(); + return (name == null) ? null : name.getPrefix(); + } + + public String getVersion() + { + StartDocument sd = (StartDocument) currentEvent; + return sd.getVersion(); + } + + public boolean isStandalone() + { + StartDocument sd = (StartDocument) currentEvent; + return sd.isStandalone(); + } + + public boolean standaloneSet() + { + StartDocument sd = (StartDocument) currentEvent; + return sd.standaloneSet(); + } + + public String getCharacterEncodingScheme() + { + StartDocument sd = (StartDocument) currentEvent; + return sd.getCharacterEncodingScheme(); + } + + public String getPITarget() + { + ProcessingInstruction pi = (ProcessingInstruction) currentEvent; + return pi.getTarget(); + } + + public String getPIData() + { + ProcessingInstruction pi = (ProcessingInstruction) currentEvent; + return pi.getData(); + } + + /** + * This class is used to construct the event series from SAX callbacks. + */ + class CallbackHandler + implements ContentHandler, DTDHandler, LexicalHandler, + DeclHandler, EntityResolver, ErrorHandler + { + + XMLReader reader; + Locator locator; + Location location; + private boolean inCDATA; + private LinkedList namespaces = new LinkedList(); + private LinkedList notations; + private LinkedList entities; + + CallbackHandler(XMLReader reader) + { + this.reader = reader; + } + + public void setDocumentLocator(Locator locator) + { + this.locator = locator; + location = new LocationImpl(-1, + locator.getColumnNumber(), + locator.getLineNumber(), + locator.getSystemId()); + } + + public void startDocument() + throws SAXException + { + String version = (locator instanceof Locator2) ? + ((Locator2) locator).getXMLVersion() : null; + String encoding = (locator instanceof Locator2) ? + ((Locator2) locator).getEncoding() : null; + boolean standalone = + reader.getFeature("http://xml.org/sax/features/is-standalone"); + boolean standaloneDeclared = standalone; + boolean encodingDeclared = (encoding != null); + events.add(new StartDocumentImpl(location, + location.getLocationURI(), + encoding, + version, + standalone, + standaloneDeclared, + encodingDeclared)); + } + + public void endDocument() + throws SAXException + { + events.add(new EndDocumentImpl(location)); + } + + public void startPrefixMapping(String prefix, String uri) + throws SAXException + { + namespaces.add(new NamespaceImpl(location, prefix, uri)); + } + + public void endPrefixMapping(String prefix) + throws SAXException + { + } + + public void startElement(String namespaceURI, String localName, + String qName, Attributes atts) + throws SAXException + { + LinkedList ns = namespaces; + namespaces = new LinkedList(); + int ci = qName.indexOf(':'); + String prefix = null; + localName = qName; + if (ci != -1) + { + prefix = qName.substring(0, ci); + localName = qName.substring(ci + 1); + } + QName name = new QName(namespaceURI, localName, prefix); + LinkedList attrs = new LinkedList(); + StartElementImpl se = new StartElementImpl(location, name, + attrs, ns, null); + events.add(se); + // Add namespaces + //for (Iterator i = ns.iterator(); i.hasNext(); ) + // events.add(i.next()); + // Add attributes + int len = atts.getLength(); + for (int i = 0; i < len; i++) + { + String attURI = atts.getURI(i); + String attQName = atts.getQName(i); + String value = atts.getValue(i); + QName type = QName.valueOf(atts.getType(i)); + boolean specified = (atts instanceof Attributes2) && + ((Attributes2) atts).isSpecified(i); + ci = attQName.indexOf(':'); + String attPrefix = null; + String attLocalName = attQName; + if (ci != -1) + { + attPrefix = attQName.substring(0, ci); + attLocalName = attQName.substring(ci + 1); + } + if ("xmlns".equals(attPrefix) || "xmlns".equals(attQName)) + continue; + QName attrName = new QName(attURI, attLocalName, attPrefix); + AttributeImpl attr = new AttributeImpl(location, attrName, + value, type, specified); + attrs.add(attr); + //events.add(attr); + } + } + + public void endElement(String namespaceURI, String localName, + String qName) + throws SAXException + { + int ci = qName.indexOf(':'); + String prefix = null; + localName = qName; + if (ci != -1) + { + prefix = qName.substring(0, ci); + localName = qName.substring(ci + 1); + } + QName name = new QName(namespaceURI, localName, prefix); + events.add(new EndElementImpl(location, name, new LinkedList())); + // TODO namespaces out of scope + } + + public void characters(char[] ch, int start, int length) + throws SAXException + { + boolean whitespace = isWhitespace(ch, start, length); + events.add(new CharactersImpl(location, new String(ch, start, length), + whitespace, inCDATA, false)); + } + + public void ignorableWhitespace(char[] ch, int start, int length) + throws SAXException + { + boolean whitespace = isWhitespace(ch, start, length); + events.add(new CharactersImpl(location, new String(ch, start, length), + whitespace, inCDATA, true)); + } + + boolean isWhitespace(char[] ch, int start, int len) + { + int end = start + len; + for (int i = start; i < end; i++) + { + char c = ch[i]; + if (c != ' ' && c != '\t' && c != '\n' && c != '\r') + return false; + } + return true; + } + + public void processingInstruction(String target, String data) + throws SAXException + { + events.add(new ProcessingInstructionImpl(location, target, data)); + } + + public void skippedEntity(String name) + throws SAXException + { + } + + public void startDTD(String name, String publicId, String systemId) + throws SAXException + { + notations = new LinkedList(); + entities = new LinkedList(); + events.add(new DTDImpl(location, null, null, notations, entities)); + } + + public void endDTD() + throws SAXException + { + } + + public void startEntity(String name) + throws SAXException + { + events.add(new StartEntityImpl(location, name)); + } + + public void endEntity(String name) + throws SAXException + { + events.add(new EndEntityImpl(location, name)); + } + + public void startCDATA() + throws SAXException + { + inCDATA = true; + } + + public void endCDATA() + throws SAXException + { + inCDATA = false; + } + + public void comment(char[] ch, int start, int length) + throws SAXException + { + events.add(new CommentImpl(location, new String(ch, start, length))); + } + + public void notationDecl(String name, String publicId, String systemId) + throws SAXException + { + Object n = new NotationDeclarationImpl(location, name, publicId, + systemId); + notations.add(n); + //events.add(n); + } + + public void unparsedEntityDecl(String name, String publicId, + String systemId, String notationName) + throws SAXException + { + Object e = new EntityDeclarationImpl(location, publicId, systemId, + name, notationName, + null, null); + entities.add(e); + //events.add(e); + } + + public void elementDecl(String name, String model) + throws SAXException + { + } + + public void attributeDecl(String eName, String aName, String type, + String valueDefault, String value) + throws SAXException + { + } + + public void internalEntityDecl(String name, String value) + throws SAXException + { + Object e = new EntityDeclarationImpl(location, null, null, + name, null, value, null); + entities.add(e); + //events.add(e); + } + + public void externalEntityDecl(String name, String publicId, + String systemId) + throws SAXException + { + Object e = new EntityDeclarationImpl(location, publicId, systemId, + name, null, null, null); + entities.add(e); + //events.add(e); + } + + public void warning(SAXParseException e) + throws SAXException + { + if (reporter != null) + { + try + { + reporter.report(e.getMessage(), "warning", e, location); + } + catch (XMLStreamException e2) + { + SAXException e3 = new SAXException(e2.getMessage()); + e3.initCause(e2); + throw e3; + } + } + } + + public void error(SAXParseException e) + throws SAXException + { + if (reporter != null) + { + try + { + reporter.report(e.getMessage(), "error", e, location); + } + catch (XMLStreamException e2) + { + SAXException e3 = new SAXException(e2.getMessage()); + e3.initCause(e2); + throw e3; + } + } + } + + public void fatalError(SAXParseException e) + throws SAXException + { + if (reporter != null) + { + try + { + reporter.report(e.getMessage(), "fatal-error", e, location); + } + catch (XMLStreamException e2) + { + SAXException e3 = new SAXException(e2.getMessage()); + e3.initCause(e2); + throw e3; + } + } + } + + public InputSource resolveEntity(String publicId, String systemId) + throws SAXException, IOException + { + if (resolver != null) + { + try + { + InputStream in = resolver.resolve(systemId); + if (in != null) + { + InputSource ret = new InputSource(in); + ret.setPublicId(publicId); + ret.setSystemId(systemId); + return ret; + } + } + catch (XMLStreamException e) + { + SAXException e2 = new SAXException(e.getMessage()); + e2.initCause(e); + throw e2; + } + } + return null; + } + + } + +} + diff --git a/libjava/classpath/gnu/xml/stream/XMLStreamWriterImpl.java b/libjava/classpath/gnu/xml/stream/XMLStreamWriterImpl.java new file mode 100644 index 00000000000..9ac0abe2712 --- /dev/null +++ b/libjava/classpath/gnu/xml/stream/XMLStreamWriterImpl.java @@ -0,0 +1,699 @@ +/* XMLStreamWriterImpl.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.LinkedList; + +import javax.xml.XMLConstants; +import javax.xml.namespace.NamespaceContext; +import javax.xml.stream.XMLStreamException; +import javax.xml.stream.XMLStreamWriter; + +import org.xml.sax.helpers.NamespaceSupport; + +/** + * Simple XML stream writer. + * + * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a> + */ +public class XMLStreamWriterImpl + implements XMLStreamWriter +{ + + protected final Writer writer; + protected final String encoding; + protected final boolean prefixDefaulting; + protected NamespaceContext namespaceContext; + + private LinkedList elements; + private boolean inStartElement; + private boolean emptyElement; + private NamespaceSupport namespaces; + + protected XMLStreamWriterImpl(Writer writer, String encoding, + boolean prefixDefaulting) + { + this.writer = writer; + this.encoding = encoding; + this.prefixDefaulting = prefixDefaulting; + elements = new LinkedList(); + namespaces = new NamespaceSupport(); + } + + private void endStartElement() + throws IOException + { + if (!inStartElement) + return; + if (emptyElement) + { + writer.write('/'); + elements.removeLast(); + namespaces.popContext(); + emptyElement = false; + } + writer.write('>'); + inStartElement = false; + } + + public void writeStartElement(String localName) + throws XMLStreamException + { + try + { + endStartElement(); + namespaces.pushContext(); + + writer.write('<'); + writer.write(localName); + + elements.addLast(new String[] { null, localName }); + inStartElement = true; + } + catch (IOException e) + { + XMLStreamException e2 = new XMLStreamException(e); + e2.initCause(e); + throw e2; + } + } + + public void writeStartElement(String namespaceURI, String localName) + throws XMLStreamException + { + try + { + endStartElement(); + namespaces.pushContext(); + + String prefix = getPrefix(namespaceURI); + boolean isDeclared = (prefix != null); + if (!isDeclared) + { + if (prefixDefaulting) + prefix = XMLConstants.DEFAULT_NS_PREFIX; + else + throw new XMLStreamException("namespace " + namespaceURI + + " has not been declared"); + } + writer.write('<'); + if (!"".equals(prefix)) + { + writer.write(prefix); + writer.write(':'); + } + writer.write(localName); + if (prefixDefaulting && !isDeclared) + { + writeNamespace(prefix, namespaceURI); + } + + elements.addLast(new String[] { prefix, localName }); + inStartElement = true; + } + catch (IOException e) + { + XMLStreamException e2 = new XMLStreamException(e); + e2.initCause(e); + throw e2; + } + } + + public void writeStartElement(String prefix, String localName, + String namespaceURI) + throws XMLStreamException + { + try + { + endStartElement(); + namespaces.pushContext(); + + String currentPrefix = getPrefix(namespaceURI); + boolean isCurrent = prefix.equals(currentPrefix); + writer.write('<'); + if (!"".equals(prefix)) + { + writer.write(prefix); + writer.write(':'); + } + writer.write(localName); + if (prefixDefaulting && !isCurrent) + { + writeNamespace(prefix, namespaceURI); + } + + elements.addLast(new String[] { prefix, localName }); + inStartElement = true; + } + catch (IOException e) + { + XMLStreamException e2 = new XMLStreamException(e); + e2.initCause(e); + throw e2; + } + } + + public void writeEmptyElement(String namespaceURI, String localName) + throws XMLStreamException + { + writeStartElement(namespaceURI, localName); + emptyElement = true; + } + + public void writeEmptyElement(String prefix, String localName, + String namespaceURI) + throws XMLStreamException + { + writeStartElement(prefix, localName, namespaceURI); + emptyElement = true; + } + + public void writeEmptyElement(String localName) + throws XMLStreamException + { + writeStartElement(localName); + emptyElement = true; + } + + public void writeEndElement() + throws XMLStreamException + { + try + { + endStartElement(); + String[] element = (String[]) elements.removeLast(); + namespaces.popContext(); + + writer.write('<'); + writer.write('/'); + if (element[0] != null && !"".equals(element[0])) + { + writer.write(element[0]); + writer.write(':'); + } + writer.write(element[1]); + writer.write('>'); + } + catch (IOException e) + { + XMLStreamException e2 = new XMLStreamException(e); + e2.initCause(e); + throw e2; + } + } + + public void writeEndDocument() + throws XMLStreamException + { + while (!elements.isEmpty()) + writeEndElement(); + } + + public void close() + throws XMLStreamException + { + flush(); + } + + public void flush() + throws XMLStreamException + { + try + { + writer.flush(); + } + catch (IOException e) + { + XMLStreamException e2 = new XMLStreamException(e); + e2.initCause(e); + throw e2; + } + } + + public void writeAttribute(String localName, String value) + throws XMLStreamException + { + if (!inStartElement) + throw new IllegalStateException(); + try + { + writer.write(' '); + writer.write(localName); + writer.write('='); + writer.write('"'); + writeEncoded(value, true); + writer.write('"'); + } + catch (IOException e) + { + XMLStreamException e2 = new XMLStreamException(e); + e2.initCause(e); + throw e2; + } + } + + public void writeAttribute(String prefix, String namespaceURI, + String localName, String value) + throws XMLStreamException + { + if (!inStartElement) + throw new IllegalStateException(); + try + { + String currentPrefix = getPrefix(namespaceURI); + if (currentPrefix == null) + { + if (prefixDefaulting) + writeNamespace(prefix, namespaceURI); + else + throw new XMLStreamException("namespace " + namespaceURI + + " is not bound"); + } + else if (!currentPrefix.equals(prefix)) + throw new XMLStreamException("namespace " + namespaceURI + + " is bound to prefix " + + currentPrefix); + writer.write(' '); + if (!"".equals(prefix)) + { + writer.write(prefix); + writer.write(':'); + } + writer.write(localName); + writer.write('='); + writer.write('"'); + writeEncoded(value, true); + writer.write('"'); + } + catch (IOException e) + { + XMLStreamException e2 = new XMLStreamException(e); + e2.initCause(e); + throw e2; + } + } + + public void writeAttribute(String namespaceURI, String localName, + String value) + throws XMLStreamException + { + if (!inStartElement) + throw new IllegalStateException(); + try + { + String prefix = getPrefix(namespaceURI); + if (prefix == null) + { + if (prefixDefaulting) + { + prefix = XMLConstants.DEFAULT_NS_PREFIX; + writeNamespace(prefix, namespaceURI); + } + else + throw new XMLStreamException("namespace " + namespaceURI + + " is not bound"); + } + writer.write(' '); + if (!"".equals(prefix)) + { + writer.write(prefix); + writer.write(':'); + } + writer.write(localName); + writer.write('='); + writer.write('"'); + writeEncoded(value, true); + writer.write('"'); + } + catch (IOException e) + { + XMLStreamException e2 = new XMLStreamException(e); + e2.initCause(e); + throw e2; + } + } + + public void writeNamespace(String prefix, String namespaceURI) + throws XMLStreamException + { + if (!inStartElement) + throw new IllegalStateException(); + try + { + if (prefix == null) + prefix = XMLConstants.DEFAULT_NS_PREFIX; + + setPrefix(prefix, namespaceURI); + + writer.write(' '); + writer.write("xmlns"); + if (!XMLConstants.DEFAULT_NS_PREFIX.equals(prefix)) + { + writer.write(':'); + writer.write(prefix); + } + writer.write('='); + writer.write('"'); + writer.write(namespaceURI); + writer.write('"'); + } + catch (IOException e) + { + XMLStreamException e2 = new XMLStreamException(e); + e2.initCause(e); + throw e2; + } + } + + public void writeDefaultNamespace(String namespaceURI) + throws XMLStreamException + { + writeNamespace(XMLConstants.DEFAULT_NS_PREFIX, namespaceURI); + } + + public void writeComment(String data) + throws XMLStreamException + { + try + { + endStartElement(); + + if (data != null && data.indexOf("--") != -1) + throw new IllegalArgumentException(data); + + writer.write("<!--"); + if (data != null) + writer.write(data); + writer.write("-->"); + } + 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 + { + endStartElement(); + + writer.write('<'); + writer.write('?'); + writer.write(target); + if (data != null) + { + writer.write(' '); + 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 + { + endStartElement(); + + if (data.indexOf("]]") != -1) + throw new IllegalArgumentException(data); + + writer.write("<![CDATA["); + writer.write(data); + writer.write("]]>"); + } + catch (IOException e) + { + XMLStreamException e2 = new XMLStreamException(e); + e2.initCause(e); + throw e2; + } + } + + public void writeDTD(String dtd) + throws XMLStreamException + { + try + { + writer.write("<!DOCTYPE "); + writer.write(dtd); + writer.write('>'); + } + catch (IOException e) + { + XMLStreamException e2 = new XMLStreamException(e); + e2.initCause(e); + throw e2; + } + } + + public void writeEntityRef(String name) + throws XMLStreamException + { + try + { + 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"; + 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("<?xml version=\""); + writer.write(version); + writer.write("\" encoding=\""); + writer.write(encoding); + 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 + { + try + { + endStartElement(); + + if (text != null) + 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 + { + try + { + endStartElement(); + + int end = start + len; + len = 0; + for (int i = start; i < end; i++) + { + char c = text[i]; + if (c == '<' || c == '>' || c == '&') + { + writer.write(text, start, len); + if (c == '<') + writer.write("<"); + else if (c == '>') + writer.write(">"); + else + writer.write("&"); + start = i + 1; + len = 0; + } + else + len++; + } + if (len > 0) + writer.write(text, start, len); + } + catch (IOException e) + { + XMLStreamException e2 = new XMLStreamException(e); + e2.initCause(e); + throw e2; + } + } + + 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 + { + if (!namespaces.declarePrefix(prefix, uri)) + throw new XMLStreamException("illegal prefix " + prefix); + } + + public void setDefaultNamespace(String uri) + throws XMLStreamException + { + 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); + } + + 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); + } + +} + |