You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@xalan.apache.org by mo...@apache.org on 2001/05/23 16:57:48 UTC
cvs commit: xml-xalan/java/samples/CompiledEJB README.servlet TransformBean.java TransformHome.java TransformRemote.java TransformServlet.java bottom_frame.html index.html top_frame.html
morten 01/05/23 07:57:48
Added: java/samples/CompiledEJB README.servlet TransformBean.java
TransformHome.java TransformRemote.java
TransformServlet.java bottom_frame.html index.html
top_frame.html
Log:
Added a new demo. This one demonstrates how pre-compiled translets can be
invoked by an EJB (another way of offering compiled XSL transformations as
a web service). Very simple, nothing fancy, but it works.
Revision Changes Path
1.1 xml-xalan/java/samples/CompiledEJB/README.servlet
Index: README.servlet
===================================================================
============================================================
CONTENTS OF THIS DOCUMENT:
o) HOW TO PROVIDE XSL TRANSFORMATIONS AS A WEB SERVICE
o) HOW TO INVOKE TRANSLETS FROM AN ENTERPRISE JAVA BEAN
------------------------------------------------------------
HOW TO PROVIDE XSL TRANSFORMATIONS AS A WEB SERVICE
With XSLTC, XSL transformations can be run from within a
an Enterprise Java Bean (EJB) and exported through a servlet.
This sample code demonstrates how that can be implemented.
The CompiledServlet and CompiledBrazil sample code
demonstrates other aproaches to providing XSL transformations
as a web service.
------------------------------------------------------------
HOW TO INVOKE TRANSLETS FROM AN ENTERPRISE JAVA BEAN
o) Create an EJB that implements SessionBean and has a
single transform() entry point:
public class TransformBean implements SessionBean {
public String transform(String document, String transletName) {
// instanciate translet
// build internal DOM
// run transformation
:
:
}
:
:
}
o) Create this EJB's remote interface (this is the interface
your servlet will use to call the bean's entry point):
public interface TransformRemote extends EJBObject {
public String transform(String document, String transletName)
throws RemoteException;
}
o) Create the EJB's home interface, which your servlet
will use to instanciate the remote interface:
public interface TransformHome extends EJBHome {
TransformRemote create()
throws CreateException, RemoteException;
}
o) Create a servlet that uses the EJB's home interface to
create a remote interface to the EJB, and then calls
the EJB's transform() method through that remote
interface:
public class TransformServlet extends HttpServlet {
public void init(ServletConfig config) {
// look up the EJB's home interface using JNDI
}
public void doGet (HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
// create the remote interface
// pass the parameters from teh request to the EJB
// display results passed back from EJB
}
}
o) Set up your J2EE_CLASSPATH to include JAXP and the XSLTC
runtime jars.
o) Compile your XSL stylesheets and place them either in
your J2EE_CLASSPATH or wrap them in your EJB jar.
o) Deploy your EJB
o) Call the servlet with the necessary parameters (at least
an URI to the source XML document and the name of the
translet class).
------------------------------------------------------------
END OF README
1.1 xml-xalan/java/samples/CompiledEJB/TransformBean.java
Index: TransformBean.java
===================================================================
/*
* @(#)$Id: TransformBean.java,v 1.1 2001/05/23 14:57:42 morten Exp $
*
* The Apache Software License, Version 1.1
*
*
* Copyright (c) 2001 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution,
* if any, must include the following acknowledgment:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowledgment may appear in the software itself,
* if and wherever such third-party acknowledgments normally appear.
*
* 4. The names "Xalan" and "Apache Software Foundation" must
* not be used to endorse or promote products derived from this
* software without prior written permission. For written
* permission, please contact apache@apache.org.
*
* 5. Products derived from this software may not be called "Apache",
* nor may "Apache" appear in their name, without prior written
* permission of the Apache Software Foundation.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation and was
* originally based on software copyright (c) 2001, Sun
* Microsystems., http://www.sun.com. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*
* @author Morten Jorgensen
*
*/
import java.io.*;
import java.text.*;
import java.util.*;
import java.rmi.RemoteException;
import javax.ejb.SessionBean;
import javax.ejb.SessionContext;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.xml.sax.XMLReader;
import org.xml.sax.SAXException;
import org.apache.xalan.xsltc.*;
import org.apache.xalan.xsltc.runtime.*;
import org.apache.xalan.xsltc.dom.*;
public class TransformBean implements SessionBean {
private SessionContext _context = null;
private final static String nullErrorMsg =
"<h1>XSL transformation error</h1>"+
"<p>'null' parameters sent to the XSL transformation bean's "+
"<tt>transform(String document, String translet)</tt> method.</p>";
/**
* Read the input document and build the internal "DOM" tree.
*/
private DOMImpl getDOM(String url, AbstractTranslet translet)
throws Exception {
// Create a SAX parser and get the XMLReader object it uses
final SAXParserFactory factory = SAXParserFactory.newInstance();
final SAXParser parser = factory.newSAXParser();
final XMLReader reader = parser.getXMLReader();
// Set the DOM's builder as the XMLReader's SAX2 content handler
DOMImpl dom = new DOMImpl();
reader.setContentHandler(dom.getBuilder());
// Create a DTD monitor and pass it to the XMLReader object
final DTDMonitor dtdMonitor = new DTDMonitor();
dtdMonitor.handleDTD(reader);
translet.setDTDMonitor(dtdMonitor);
// Parse the input document
reader.parse(url);
return dom;
}
/**
* Generates HTML from a basic error message and an exception
*/
private void errorMsg(PrintWriter out, Exception e, String msg) {
out.println("<h1>Error</h1>");
out.println("<p>"+msg+"</p><br>");
out.println(e.toString());
}
/**
* Main bean entry point
*/
public String transform(String document, String transletName) {
// Initialise the output stream
final StringWriter sout = new StringWriter();
final PrintWriter out = new PrintWriter(sout);
try {
if ((document == null) || (transletName == null)) {
out.println(nullErrorMsg);
}
else {
// Instanciate a translet object (inherits AbstractTranslet)
Class tc = Class.forName(transletName);
AbstractTranslet translet = (AbstractTranslet)tc.newInstance();
// Read input document from the DOM cache
DOMImpl dom = getDOM(document, translet);
// Initialize the (default) SAX output handler
DefaultSAXOutputHandler saxHandler =
new DefaultSAXOutputHandler(out);
// Start the transformation
final long start = System.currentTimeMillis();
translet.transform(dom, new TextOutput(saxHandler));
final long done = System.currentTimeMillis() - start;
out.println("<!-- transformed by XSLTC in "+done+"msecs -->");
}
}
catch (IOException e) {
errorMsg(out, e, "Could not locate source document: "+document);
}
catch (ClassNotFoundException e) {
errorMsg(out, e, "Could not locate the translet class: "+
transletName);
}
catch (SAXException e) {
errorMsg(out, e, "Error parsing document "+document);
}
catch (Exception e) {
errorMsg(out, e, "Impossible state reached.");
}
// Now close up the sink, and return the HTML output in the
// StringWrite object as a string.
out.close();
return sout.toString();
}
/**
*
*/
public void setSessionContext(SessionContext context) {
_context = context;
}
// General EJB entry points
public void ejbCreate() { }
public void ejbRemove() { }
public void ejbActivate() { }
public void ejbPassivate() { }
public void ejbLoad() { }
public void ejbStore() { }
}
1.1 xml-xalan/java/samples/CompiledEJB/TransformHome.java
Index: TransformHome.java
===================================================================
/*
* @(#)$Id: TransformHome.java,v 1.1 2001/05/23 14:57:43 morten Exp $
*
* The Apache Software License, Version 1.1
*
*
* Copyright (c) 2001 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution,
* if any, must include the following acknowledgment:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowledgment may appear in the software itself,
* if and wherever such third-party acknowledgments normally appear.
*
* 4. The names "Xalan" and "Apache Software Foundation" must
* not be used to endorse or promote products derived from this
* software without prior written permission. For written
* permission, please contact apache@apache.org.
*
* 5. Products derived from this software may not be called "Apache",
* nor may "Apache" appear in their name, without prior written
* permission of the Apache Software Foundation.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation and was
* originally based on software copyright (c) 2001, Sun
* Microsystems., http://www.sun.com. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*
* @author Morten Jorgensen
*
*/
import java.rmi.RemoteException;
import javax.ejb.CreateException;
import javax.ejb.EJBHome;
/**
* XSL transformation bean home interface
*/
public interface TransformHome extends EJBHome {
TransformRemote create() throws CreateException, RemoteException;
}
1.1 xml-xalan/java/samples/CompiledEJB/TransformRemote.java
Index: TransformRemote.java
===================================================================
/*
* @(#)$Id: TransformRemote.java,v 1.1 2001/05/23 14:57:44 morten Exp $
*
* The Apache Software License, Version 1.1
*
*
* Copyright (c) 2001 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution,
* if any, must include the following acknowledgment:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowledgment may appear in the software itself,
* if and wherever such third-party acknowledgments normally appear.
*
* 4. The names "Xalan" and "Apache Software Foundation" must
* not be used to endorse or promote products derived from this
* software without prior written permission. For written
* permission, please contact apache@apache.org.
*
* 5. Products derived from this software may not be called "Apache",
* nor may "Apache" appear in their name, without prior written
* permission of the Apache Software Foundation.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation and was
* originally based on software copyright (c) 2001, Sun
* Microsystems., http://www.sun.com. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*
* @author Morten Jorgensen
*
*/
import javax.ejb.EJBObject;
import java.rmi.RemoteException;
/**
* XSL transformation bean remote interface
*/
public interface TransformRemote extends EJBObject {
public String transform(String document, String transletName)
throws RemoteException;
}
1.1 xml-xalan/java/samples/CompiledEJB/TransformServlet.java
Index: TransformServlet.java
===================================================================
/*
* @(#)$Id: TransformServlet.java,v 1.1 2001/05/23 14:57:44 morten Exp $
*
* The Apache Software License, Version 1.1
*
*
* Copyright (c) 2001 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution,
* if any, must include the following acknowledgment:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowledgment may appear in the software itself,
* if and wherever such third-party acknowledgments normally appear.
*
* 4. The names "Xalan" and "Apache Software Foundation" must
* not be used to endorse or promote products derived from this
* software without prior written permission. For written
* permission, please contact apache@apache.org.
*
* 5. Products derived from this software may not be called "Apache",
* nor may "Apache" appear in their name, without prior written
* permission of the Apache Software Foundation.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation and was
* originally based on software copyright (c) 2001, Sun
* Microsystems., http://www.sun.com. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*
* @author Morten Jorgensen
*
*/
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import javax.naming.*;
import javax.rmi.PortableRemoteObject;
public class TransformServlet extends HttpServlet {
// Error message used when the XSL transformation bean cannot be created
private final static String createErrorMsg =
"<h1>XSL transformation bean error</h1>"+
"<p>An XSL transformation bean could not be created.</p>";
// Transformer - "more than meets the eye".
private TransformHome transformer;
/**
* Servlet initializer - look up the bean's home interface
*/
public void init(ServletConfig config)
throws ServletException{
try{
InitialContext context = new InitialContext();
Object transformRef = context.lookup("transform");
transformer =
(TransformHome)PortableRemoteObject.narrow(transformRef,
TransformHome.class);
} catch (Exception NamingException) {
NamingException.printStackTrace();
}
}
/**
* Handles "GET" HTTP requests - ie. runs the actual transformation
*/
public void doGet (HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
String document = request.getParameter("document");
String translet = request.getParameter("translet");
response.setContentType("text/html");
PrintWriter out = response.getWriter();
try{
// Get the insult from the bean
TransformRemote xslt = transformer.create();
String result = xslt.transform(document, translet);
out.println(result);
} catch(Exception CreateException){
out.println(createErrorMsg);
}
out.close();
}
public void destroy() {
System.out.println("Destroy");
}
}
1.1 xml-xalan/java/samples/CompiledEJB/bottom_frame.html
Index: bottom_frame.html
===================================================================
<html>
<head></head>
<body>
<backgroundcolor=#ffffff>
</body>
</html>
1.1 xml-xalan/java/samples/CompiledEJB/index.html
Index: index.html
===================================================================
<html>
<head><title>XML Technology Center</title></head>
<backgroundcolor=#ffffff>
<frameset border="0" rows="30%,70%">
<frame src="top_frame.html" NAME="top" scrolling="off">
<frame src="bottom_frame.html" NAME="bottom" scrolling="on">
</frameset>
<noframes>
</noframes>
</html>
1.1 xml-xalan/java/samples/CompiledEJB/top_frame.html
Index: top_frame.html
===================================================================
<html>
<head>
<base target=xtc_menu>
</head>
<body bgcolor=#ffffff>
<center><h1>Server-side XSL transformations</h1><p></center>
<script language="JavaScript">
function getURI() {
var root = "http://gobsheen.ireland/morten/Sun/XTC/demo/plays/";
var menu = document.XMLinput.elements[0];
var play = menu.options[menu.selectedIndex].value;
return(root+play);
}
function getTranslet() {
var menu = document.XMLinput.elements[1];
var translet = menu.options[menu.selectedIndex].value;
return(translet);
}
function setHTMLlocation(translet) {
var uri = getURI();
var translet = getTranslet();
var source = "http://gobsheen:8000/Transform/Transform?"+
"document="+uri+"&translet="+translet;
open(source,"bottom");
}
function setXMLlocation() {
var target = parent.frames.demo_bottom;
var uri = getURI();
open(uri,"bottom");
}
</script>
<form name="XMLinput">
<table>
<tr>
<td>
<b>Source document:</b>
</td>
<td>
<select name="dropdown">
<option value="AsYouLikeIt.xml">As You Like It
<option value="Cymbeline.xml">Cymbeline
<option value="Hamlet.xml">The Tragedy of Hamlet
<option value="HenryV.xml">The Life of Henry V
<option value="HenryVIII.xml">The Famous History of the Life of Henry VIII
<option value="KingJohn.xml">The Life and Death of King John
<option value="KingLear.xml">The Tragedy of King Lear
<option value="KingRichardII.xml">The Tragedy of King Richard II
<option value="MeasureForMeasure.xml">Measure for Measure
<option value="MerchantOfVenice.xml">The Merchant of Venice
<option value="MerryWivesOfWindsor.xml">The Merry Wives of Windsor
<option value="MidsummerNightsDream.xml">A Midsummer Night's Dream
<option value="MuchAdoAboutNothing.xml">Much Ado about Nothing
<option value="PericlesPrinceOfTyre.xml">Pericles, Prince of Tyre
<option value="RomeoAndJuliet.xml">The Tragedy of Romeo and Juliet
<option value="TamingOfTheShrew.xml">The Taming of the Shrew
<option value="TheTempest.xml">The Tempest
<option value="TimonOfAthens.xml">The Life of Timon of Athens
<option value="TragedyOfCoriolanus.xml">The Tragedy of Coriolanus
<option value="TragedyOfJuliusCaesar.xml">The Tragedy of Julius Caesar
<option value="TragedyOfOthello.xml">The Tragedy of Othello, the Moor of Venice
<option value="TroilusAndCresida.xml">The History of Troilus and Cressida
<option value="TwelfthNight.xml">Twelfth Night, or What You Will
<option value="TwoGentlementOfVerona.xml">The Two Gentlemen of Verona
<option value="WintersTale.xml">The Winter's Tale
</select>
</td>
</tr>
<tr>
<td>
<b>Transformation:</b>
</td>
<td>
<select name="dropdown">
<option value="PlayToHTML">Full
<option value="PlayToSpeakers">Speakers
<option value="PlayToIndex">Index
</select>
</td>
</tr>
<tr>
<td>
<b>Method:</b>
</td>
<td>
<input type="button" value="Transform" onClick="setHTMLlocation()">
<input type="button" value="XML source" onClick="setXMLlocation()">
</td>
</tr>
</table>
</form>
</body>
</html>
---------------------------------------------------------------------
To unsubscribe, e-mail: xalan-cvs-unsubscribe@xml.apache.org
For additional commands, e-mail: xalan-cvs-help@xml.apache.org