diff options
Diffstat (limited to 'libjava/classpath/gnu/xml/transform')
5 files changed, 279 insertions, 5 deletions
diff --git a/libjava/classpath/gnu/xml/transform/SAXTemplatesHandler.java b/libjava/classpath/gnu/xml/transform/SAXTemplatesHandler.java new file mode 100644 index 00000000000..2c5a4686f10 --- /dev/null +++ b/libjava/classpath/gnu/xml/transform/SAXTemplatesHandler.java @@ -0,0 +1,97 @@ +/* SAXTemplatesHandler.java -- + Copyright (C) 2006 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.transform; + +import javax.xml.transform.Templates; +import javax.xml.transform.TransformerConfigurationException; +import javax.xml.transform.dom.DOMSource; +import javax.xml.transform.sax.TemplatesHandler; +import org.w3c.dom.Document; +import gnu.xml.dom.ls.SAXEventSink; + +/** + * A content handler that acts as a sink for SAX parse events, + * constructing an XSL stylesheet. + * Internally, this class simply creates a DOM tree from the events, + * and then parses the DOM into a Templates object. + * + * @author Chris Burdess (dog@gnu.org) + */ +class SAXTemplatesHandler + extends SAXEventSink + implements TemplatesHandler +{ + + final TransformerFactoryImpl factory; + String systemId; + + SAXTemplatesHandler(TransformerFactoryImpl factory) + { + this.factory = factory; + } + + public String getSystemId() + { + return systemId; + } + + public void setSystemId(String systemId) + { + this.systemId = systemId; + } + + public Templates getTemplates() + { + Document doc = getDocument(); + if (doc == null) + throw new IllegalStateException("Parsing of stylesheet incomplete"); + DOMSource ds = new DOMSource(doc, systemId); + try + { + return factory.newTemplates(ds); + } + catch (TransformerConfigurationException e) + { + String msg = "Unable to construct templates from this event stream"; + IllegalStateException e2 = new IllegalStateException(msg); + e2.initCause(e); + throw e2; + } + } + +} diff --git a/libjava/classpath/gnu/xml/transform/SAXTransformerHandler.java b/libjava/classpath/gnu/xml/transform/SAXTransformerHandler.java new file mode 100644 index 00000000000..83cc3754be9 --- /dev/null +++ b/libjava/classpath/gnu/xml/transform/SAXTransformerHandler.java @@ -0,0 +1,111 @@ +/* SAXTransformerHandler.java -- + Copyright (C) 2006 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package gnu.xml.transform; + +import javax.xml.transform.Result; +import javax.xml.transform.Transformer; +import javax.xml.transform.TransformerException; +import javax.xml.transform.dom.DOMSource; +import javax.xml.transform.sax.TransformerHandler; +import org.w3c.dom.Document; +import org.xml.sax.SAXException; +import gnu.xml.dom.ls.SAXEventSink; + +/** + * A SAX event sink that processes an XML source represented as a stream of + * SAX events into a result tree. + * This works by simply buffering all the events into a DOM tree and then + * using this DOM tree as the source of the transformation. + * + * @author Chris Burdess (dog@gnu.org) + */ +class SAXTransformerHandler + extends SAXEventSink + implements TransformerHandler +{ + + final TransformerFactoryImpl factory; + final Transformer transformer; + String systemId; + Result result; + + SAXTransformerHandler(TransformerFactoryImpl factory, Transformer transformer) + { + this.factory = factory; + this.transformer = transformer; + } + + public String getSystemId() + { + return systemId; + } + + public void setSystemId(String systemId) + { + this.systemId = systemId; + } + + public Transformer getTransformer() + { + return transformer; + } + + public void setResult(Result result) + { + this.result = result; + } + + public void endDocument() + throws SAXException + { + super.endDocument(); + try + { + Document doc = getDocument(); + DOMSource ds = new DOMSource(doc, systemId); + transformer.transform(ds, result); + } + catch (TransformerException e) + { + SAXException e2 = new SAXException(e.getMessage()); + e2.initCause(e); + throw e2; + } + } + +} diff --git a/libjava/classpath/gnu/xml/transform/StreamSerializer.java b/libjava/classpath/gnu/xml/transform/StreamSerializer.java index fb8b1a601bc..26bdda8b325 100644 --- a/libjava/classpath/gnu/xml/transform/StreamSerializer.java +++ b/libjava/classpath/gnu/xml/transform/StreamSerializer.java @@ -324,7 +324,8 @@ public class StreamSerializer break; case Node.TEXT_NODE: value = node.getNodeValue(); - if (!"yes".equals(node.getUserData("disable-output-escaping"))) + if (!"yes".equals(node.getUserData("disable-output-escaping")) && + mode != Stylesheet.OUTPUT_TEXT) value = encode(value, false, false); out.write(encodeText(value)); break; diff --git a/libjava/classpath/gnu/xml/transform/TransformerFactoryImpl.java b/libjava/classpath/gnu/xml/transform/TransformerFactoryImpl.java index 16e3363f8c6..1e8fea3e3a6 100644 --- a/libjava/classpath/gnu/xml/transform/TransformerFactoryImpl.java +++ b/libjava/classpath/gnu/xml/transform/TransformerFactoryImpl.java @@ -60,11 +60,15 @@ import javax.xml.transform.dom.DOMResult; import javax.xml.transform.dom.DOMSource; import javax.xml.transform.sax.SAXResult; import javax.xml.transform.sax.SAXSource; +import javax.xml.transform.sax.SAXTransformerFactory; +import javax.xml.transform.sax.TemplatesHandler; +import javax.xml.transform.sax.TransformerHandler; import javax.xml.transform.stream.StreamResult; import javax.xml.transform.stream.StreamSource; import javax.xml.xpath.XPathFactory; import org.w3c.dom.Document; import org.w3c.dom.Node; +import org.xml.sax.XMLFilter; import gnu.xml.dom.DomDocument; /** @@ -73,7 +77,7 @@ import gnu.xml.dom.DomDocument; * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a> */ public class TransformerFactoryImpl - extends TransformerFactory + extends SAXTransformerFactory { final XPathFactory xpathFactory; @@ -316,7 +320,8 @@ public class TransformerFactoryImpl StreamSource.FEATURE.equals(name) || StreamResult.FEATURE.equals(name) || DOMSource.FEATURE.equals(name) || - DOMResult.FEATURE.equals(name)) + DOMResult.FEATURE.equals(name) || + SAXTransformerFactory.FEATURE.equals(name)) { return true; } @@ -346,6 +351,49 @@ public class TransformerFactoryImpl return userListener; } + // -- SAXTransformerFactory -- + + public TemplatesHandler newTemplatesHandler() + throws TransformerConfigurationException + { + return new SAXTemplatesHandler(this); + } + + public TransformerHandler newTransformerHandler() + throws TransformerConfigurationException + { + Transformer transformer = newTransformer(); + return new SAXTransformerHandler(this, transformer); + } + + public TransformerHandler newTransformerHandler(Source source) + throws TransformerConfigurationException + { + Transformer transformer = newTransformer(source); + return new SAXTransformerHandler(this, transformer); + } + + public TransformerHandler newTransformerHandler(Templates templates) + throws TransformerConfigurationException + { + Transformer transformer = templates.newTransformer(); + return new SAXTransformerHandler(this, transformer); + } + + public XMLFilter newXMLFilter(Source source) + throws TransformerConfigurationException + { + throw new UnsupportedOperationException(); + } + + public XMLFilter newXMLFilter(Templates templates) + throws TransformerConfigurationException + { + throw new UnsupportedOperationException(); + } + + // -- SAXTransformerFactory end -- + /** * Syntax: TransformerFactoryImpl [<stylesheet> [<input> [<output>]]] */ diff --git a/libjava/classpath/gnu/xml/transform/XSLURIResolver.java b/libjava/classpath/gnu/xml/transform/XSLURIResolver.java index 6a49caab458..8d7e3fcb485 100644 --- a/libjava/classpath/gnu/xml/transform/XSLURIResolver.java +++ b/libjava/classpath/gnu/xml/transform/XSLURIResolver.java @@ -53,6 +53,7 @@ import javax.xml.transform.Source; import javax.xml.transform.TransformerException; import javax.xml.transform.URIResolver; import javax.xml.transform.dom.DOMSource; +import javax.xml.transform.sax.SAXSource; import javax.xml.transform.stream.StreamSource; import org.w3c.dom.Node; import org.xml.sax.InputSource; @@ -118,10 +119,9 @@ class XSLURIResolver try { - URL url = resolveURL(systemId, base, href); Node node = null; InputStream in = null; - if (source instanceof StreamSource) + if (source != null && source instanceof StreamSource) { StreamSource ss = (StreamSource) source; in = ss.getInputStream(); @@ -134,8 +134,25 @@ class XSLURIResolver } } } + else if (source != null && source instanceof SAXSource) + { + SAXSource ss = (SAXSource) source; + if (ss.getInputSource() != null) + { + in = ss.getInputSource().getByteStream(); + if (in == null) + { + Reader reader = ss.getInputSource().getCharacterStream(); + if (reader != null) + { + in = new ReaderInputStream(reader); + } + } + } + } if (in == null) { + URL url = resolveURL(systemId, base, href); if (url != null) { systemId = url.toString(); |