summaryrefslogtreecommitdiff
path: root/gnu/xml
diff options
context:
space:
mode:
Diffstat (limited to 'gnu/xml')
-rw-r--r--gnu/xml/dom/DomNode.java1
-rw-r--r--gnu/xml/dom/ls/SAXEventSink.java8
-rw-r--r--gnu/xml/transform/SAXTemplatesHandler.java97
-rw-r--r--gnu/xml/transform/SAXTransformerHandler.java111
-rw-r--r--gnu/xml/transform/StreamSerializer.java3
-rw-r--r--gnu/xml/transform/TransformerFactoryImpl.java52
-rw-r--r--gnu/xml/transform/XSLURIResolver.java21
-rw-r--r--gnu/xml/xpath/Expr.java35
8 files changed, 322 insertions, 6 deletions
diff --git a/gnu/xml/dom/DomNode.java b/gnu/xml/dom/DomNode.java
index f0915eb5e..269038aa7 100644
--- a/gnu/xml/dom/DomNode.java
+++ b/gnu/xml/dom/DomNode.java
@@ -668,6 +668,7 @@ public abstract class DomNode
{
insertionEvent(null, child);
}
+ length++;
}
return child;
diff --git a/gnu/xml/dom/ls/SAXEventSink.java b/gnu/xml/dom/ls/SAXEventSink.java
index aad5ac76e..8c5b104b9 100644
--- a/gnu/xml/dom/ls/SAXEventSink.java
+++ b/gnu/xml/dom/ls/SAXEventSink.java
@@ -43,6 +43,7 @@ import java.util.LinkedList;
import java.util.List;
import javax.xml.XMLConstants;
import org.w3c.dom.Attr;
+import org.w3c.dom.Document;
import org.w3c.dom.DocumentType;
import org.w3c.dom.Element;
import org.w3c.dom.Entity;
@@ -72,7 +73,7 @@ import gnu.xml.dom.DomNode;
*
* @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
*/
-class SAXEventSink
+public class SAXEventSink
implements ContentHandler, LexicalHandler, DTDHandler, DeclHandler
{
@@ -110,6 +111,11 @@ class SAXEventSink
interrupted = true;
}
+ protected Document getDocument()
+ {
+ return doc;
+ }
+
// -- ContentHandler2 --
public void setDocumentLocator(Locator locator)
diff --git a/gnu/xml/transform/SAXTemplatesHandler.java b/gnu/xml/transform/SAXTemplatesHandler.java
new file mode 100644
index 000000000..2c5a4686f
--- /dev/null
+++ b/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/gnu/xml/transform/SAXTransformerHandler.java b/gnu/xml/transform/SAXTransformerHandler.java
new file mode 100644
index 000000000..83cc3754b
--- /dev/null
+++ b/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/gnu/xml/transform/StreamSerializer.java b/gnu/xml/transform/StreamSerializer.java
index fb8b1a601..26bdda8b3 100644
--- a/gnu/xml/transform/StreamSerializer.java
+++ b/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/gnu/xml/transform/TransformerFactoryImpl.java b/gnu/xml/transform/TransformerFactoryImpl.java
index 16e3363f8..1e8fea3e3 100644
--- a/gnu/xml/transform/TransformerFactoryImpl.java
+++ b/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/gnu/xml/transform/XSLURIResolver.java b/gnu/xml/transform/XSLURIResolver.java
index 6a49caab4..8d7e3fcb4 100644
--- a/gnu/xml/transform/XSLURIResolver.java
+++ b/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();
diff --git a/gnu/xml/xpath/Expr.java b/gnu/xml/xpath/Expr.java
index 76fd49eef..cafc83b0d 100644
--- a/gnu/xml/xpath/Expr.java
+++ b/gnu/xml/xpath/Expr.java
@@ -59,6 +59,7 @@ import javax.xml.xpath.XPathExpression;
import javax.xml.xpath.XPathExpressionException;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
+import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
@@ -80,6 +81,38 @@ public abstract class Expr
".####################################################",
new DecimalFormatSymbols(Locale.US));
+ static class ExprNodeSet implements NodeList
+ {
+
+ private ArrayList list;
+
+ ExprNodeSet(Collection collection)
+ {
+ if (collection instanceof ArrayList)
+ list = (ArrayList) collection;
+ else
+ list = new ArrayList(collection);
+ }
+
+ public int getLength()
+ {
+ return list.size();
+ }
+
+ public Node item(int index)
+ {
+ try
+ {
+ return (Node) list.get(index);
+ }
+ catch (ArrayIndexOutOfBoundsException e)
+ {
+ return null;
+ }
+ }
+
+ }
+
public Object evaluate(Object item, QName returnType)
throws XPathExpressionException
{
@@ -132,6 +165,8 @@ public abstract class Expr
{
throw new XPathExpressionException("return value is not a node-set");
}
+ if (ret != null)
+ ret = new ExprNodeSet((Collection) ret);
}
}
return ret;