There must be more code than what you are showing below.
Using the translet should be as simple as:
/**
* Transform XML Data using an XSL stylesheet and send it out
* as the HTTP Servlet Response.
*
* @param Templates xslt XSL Translation template
* @param StringWriter data XML data
* @param PrintWriter out response output
*/
private void transformResponse(Templates xslt, StringReader data,
Writer out) throws ServletException {
StreamSource source = new StreamSource(data);
StreamResult result = new StreamResult(out);
try {
Transformer transformer = xslt.newTransformer();
transformer.transform(source,result);
} catch(TransformerConfigurationException e) {
throw new ServletException("XSL Transformation" +
" location: " + e.getLocationAsString() +
" failure: " + e.getMessageAndLocation());
} catch(Exception e) {
throw new ServletException("XSL Transformation failed",e);
}
}
Where the templates passed in is either for a normal Templates object
or a Templates object for a Translet.
The code snippet I showed was the way AbstractTranslet implements the
reader.parser() method.
Regards,
Glenn
Tom Amiro wrote:
>
> Hi Glenn,
>
> I have been using XSLTC from a JSP with our native API, and
> not TrAX. I've been able to use a string as the XML input as follows:
>
> StringReader inputsr = new StringReader(xmlinput);
> InputSource input = new InputSource(inputsr);
> reader.parse(input);
>
> Tom
>
> Glenn Nielsen wrote:
>
> > I've traced down the problem some more.
> >
> > AbstractTranslet() is throwing a NullPointerException when it tries
> > to do reader.parse("file:"+(new File(xmlDocName).getAbsolutePath()));
> > My Source is not a file, it is created from a StringReader.
> >
> > So it looks like I am in a catch 22. AbstractTranslet() fails on a non
> > file Source in the xsltc 2.1 release and xsltc 2.2 isn't ready yet.
> >
> > BTW, I created an Ant task called <translet> which uses XSLTC to
> > recursively compile stylesheets and add any directory paths as
> > package parts to the generated classes. Think the Ant developers
> > would be interested? Or perhaps including it with Xalan?
> >
> > Regards,
> >
> > Glenn
> >
> > Morten Jorgensen wrote:
> > >
> > > Glenn,
> > >
> > > We are just after finishing an initial implementation of the entire TrAX
> > API
> > > for XSLTC. We decided to separate the TrAX implementation and the native
> > > XSLTC APIs, and this is why the AbstractTranslet class no longer
> > implements
> > > the Transformer interface. All TrAX code is located in the
> > >
> > > org.apache.xalan.xsltc.trax
> > >
> > > package. This is becuase many of our current users choose to use XSLTC's
> > > native API, and we don't want these to have to include all the extra code
> > > for TrAX in their applets/servlets/JSPs what have you.
> > >
> > > But please not that we are in the phase of testing/bugfixing our TrAX
> > code,
> > > and that it is not yet stable code.
> > >
> > > I will look into the System.exit() call and have it removed from the code.
> >
> > >
> > > Thank you for your interest in XSLTC,
> > >
> > > Morten J�rgensen
> > >
> > > Glenn Nielsen wrote:
> > > >
> > > > I am implementing precompiled XSLTC Translets in a Servlet using Xalan
> > 2.1.
> > > >
> > > > I created my own class with implements the Templates interface so that
> > > > the Servlet can instantiate the stylesheet from file if it exists or
> > > > instantiate it from a precompile Translet class. i.e. If the xsl file
> > exists
> > > > it is used, if not the translet is used.
> > > >
> > > > Here is the code for my Templates implementation:
> > > >
> > > >
> > ----------------------------------------------------------------------------
> >
> > > > package net.more.servlets.soapschema;
> > > >
> > > > import javax.xml.transform.Templates;
> > > > import javax.xml.transform.Transformer;
> > > > import javax.xml.transform.TransformerConfigurationException;
> > > > import javax.xml.transform.sax.SAXTransformerFactory;
> > > >
> > > > import org.apache.xalan.xsltc.runtime.AbstractTranslet;
> > > > import java.util.Properties;
> > > >
> > > > /**
> > > > * Implementation of a JAXP1.1 Templates object for Translets.
> > > > */
> > > > public class SchemaTransletTemplate implements Templates {
> > > > private Class _translet = null;
> > > >
> > > > public SchemaTransletTemplate(Class clazz) {
> > > > _translet = clazz;
> > > > }
> > > >
> > > > public Properties getOutputProperties() { /*TBD*/ return null; }
> > > >
> > > > public Transformer newTransformer() throws
> > > > TransformerConfigurationException
> > > > {
> > > > if (_translet == null) {
> > > > throw new TransformerConfigurationException(
> > > > "Error: Null Translet");
> > > > }
> > > > Transformer trans = null;
> > > > try {
> > > > trans = (Transformer)_translet.newInstance();
> > > > } catch(Exception e) {
> > > > throw new TransformerConfigurationException(
> > > > "Error: Could not instantiate Translet: " + e.toString());
> > > > }
> > > > return trans;
> > > > }
> > > > }
> > > >
> > ----------------------------------------------------------------------------
> >
> > > >
> > > > Here is the code which does the transformation using either a template
> > > > instantiated from an XSL file or from a precompiled translet.
> > > >
> > > >
> > ----------------------------------------------------------------------------
> >
> > > > private void transformResponse(Templates xslt, StringReader data,
> > > > Writer out) throws ServletException {
> > > > StreamSource source = new StreamSource(data);
> > > > StreamResult result = new StreamResult(out);
> > > > try {
> > > > Transformer transformer = xslt.newTransformer();
> > > > transformer.transform(source,result);
> > > > } catch(TransformerConfigurationException e) {
> > > > throw new ServletException("XSL Transformation" +
> > > > " location: " + e.getLocationAsString() +
> > > > " failure: " + e.getMessageAndLocation());
> > > > } catch(Exception e) {
> > > > throw new ServletException("XSL Transformation failed",e);
> > > > }
> > > > }
> > > >
> > ----------------------------------------------------------------------------
> >
> > > >
> > > > The above works for a template created at runtime from an XSL stylesheet
> >
> > > > but fails for the Translet. What is really annoying is that when the
> > > > Translet fails it calls System.exit(). This is not considerate
> > behaviour
> > > > when the Translet is running inside a servlet container.
> > > >
> > > > Fortunately I run Tomcat 4 with the Java SecurityManager enabled so that
> >
> > > > the attempt to use System.exit() caused a SecurityException instead of
> > > > terminating Tomcat.
> > > >
> > > > I still don't know whether there is some failure causing the Translet to
> >
> > > > call System.exit() or it just does it.
> > > >
> > > > I have tried to use this code with the 2.2D8 Xalan release, but there
> > > > seem to be some changes to the API for Translets where they no longer
> > > > implement the Transformer interface.
> > > >
> > > > Regards,
> > > >
> > > > Glenn
> > > >
> > > > ----------------------------------------------------------------------
> > > > Glenn Nielsen [EMAIL PROTECTED] | /* Spelin donut madder |
> > > > MOREnet System Programming | * if iz ina coment. |
> > > > Missouri Research and Education Network | */ |
> > > > ----------------------------------------------------------------------
> > >
> > > --
> > >
> > >
> > > E-mail: [EMAIL PROTECTED]
> > > Phone: +353 1 819 9071 / x-19071
> > > --8<-----------------------------------------------------------
> > > C'est l'histoire d'un mec qui tombe d'un immeuble de cinquante
> > > �tages. A chaque �tage, a fur et � mesure de sa chute, il se r�p�te
> > > sans cesse pour se rassurer:
> > > Jusqu'ici tout va bien,
> > > jusqu'ici tout va bien,
> > > jusqu'ici tout va bien...
> > > Mais l'important, c'est pas la chute, c'est l'atterrissage.
> >
> > --
> > ----------------------------------------------------------------------
> > Glenn Nielsen [EMAIL PROTECTED] | /* Spelin donut madder |
> > MOREnet System Programming | * if iz ina coment. |
> > Missouri Research and Education Network | */ |
> > ----------------------------------------------------------------------
--
----------------------------------------------------------------------
Glenn Nielsen [EMAIL PROTECTED] | /* Spelin donut madder |
MOREnet System Programming | * if iz ina coment. |
Missouri Research and Education Network | */ |
----------------------------------------------------------------------