Why do you need to use a byte array? A SAX InputSource is quite happy to
accept a StringReader as the input source.
Michael Kay
-----Original Message-----
From: owner-xsl-list(_at_)lists(_dot_)mulberrytech(_dot_)com
[mailto:owner-xsl-list(_at_)lists(_dot_)mulberrytech(_dot_)com] On Behalf Of
W. Eliot Kimber
Sent: 18 April 2003 16:29
To: xsl-list(_at_)lists(_dot_)mulberrytech(_dot_)com
Subject: Re: [xsl] How to: Returning HTML Elements From a
Saxon Extension Function (6.5.x)
W. Eliot Kimber wrote:
W. Eliot Kimber wrote:
My analysis is correct--turns out one of my collegues had already
figured this out and I didn't know it. What I had missed
was the the
"Parse()" method on the Saxon DocumentBuilderImpl class,
which allows
you to pass in a string to be parsed more or less directly.
Here's the
basic pattern:
public NodeInfo generateResultTree(String markup) {
java.io.ByteArrayInputStream bais = new
ByteArrayInputStream(markup.getBytes());
org.xml.sax.InputSource isource = null;
try {
isource = new InputSource(bais);
} catch (org.xml.sax.SAXException e) {
e.printStackTrace();
}
org.w3c.dom.Document root = new
com.icl.saxon.om.DocumentBuilderImpl().parse(isource);
return (NodeInfo)(root.getDocumentElement());
}
I discovered it's not quite so simple because the byte array you get
from String.getBytes() is a UTF-16 byte sequence, but Aelfred (which
Saxon uses in it's DocumentBuilderImpl) only handles UTF-8,
so you have
to convert the Java UTF-16 string to a UTF-8 byte array, which I did
like so:
public NodeInfo generateMarkup(String markupString) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
OutputStreamWriter osw = null;
// NOTE: the following exceptions should never happen,
// so there's no point in trapping them.
try {
osw = new OutputStreamWriter(baos, "UTF-8");
} catch (UnsupportedEncodingException e2) {
e2.printStackTrace();
}
try {
osw.write(markupString);
} catch (IOException e3) {
e3.printStackTrace();
}
try {
osw.flush();
osw.close();
} catch (IOException e4) {
e4.printStackTrace();
}
ByteArrayInputStream bais = new
ByteArrayInputStream(baos.toByteArray());
InputSource isource = new InputSource(bais);
Document root = null;
try {
root = new DocumentBuilderImpl().parse(isource);
} catch (SAXException e1) {
e1.printStackTrace();
}
return (NodeInfo)(root.getDocumentElement());
}
--
W. Eliot Kimber, eliot(_at_)isogen(_dot_)com
Consultant, ISOGEN International
1016 La Posada Dr., Suite 240
Austin, TX 78752 Phone: 512.656.4139
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list