Christopher,
thank you for this email.
I attached ApplyTag.java, taglib.tld and xsl.jar (for impatient).
I'm sure that my solution is not the best solution, but it is very good for
my purpose.
Inside to the code I added comment line to identify my code:
// START BORTONE
....new code...
// STOP BORTONE
I would like to have your feedback (independently from result :-)).
Thanks,
Pier Paolo.
----- Original Message -----
From: "Christopher Benson" <[EMAIL PROTECTED]>
To: <[EMAIL PROTECTED]>
Sent: Monday, May 28, 2001 12:19 PM
Subject: About XSL TagLib future
> Pier,
>
> I can't speak for the project - I'm just a taglibs user. However, if you
> want to share it, please post your code that uses params with the XSL
> taglib. I'd really like to see it, and I'll bet other would too. Thanks.
taglib.tld
/*
* $Id: ApplyTag.java,v 1.1 2000/07/03 19:30:00 craigmcc Exp $
* ====================================================================
*
* The Apache Software License, Version 1.1
*
* Copyright (c) 1999 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 acknowlegement:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowlegement may appear in the software itself,
* if and wherever such third-party acknowlegements normally appear.
*
* 4. The names "The Jakarta Project", "Tomcat", 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 [EMAIL PROTECTED]
*
* 5. Products derived from this software may not be called "Apache"
* nor may "Apache" appear in their names without prior written
* permission of the Apache Group.
*
* 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. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*
* [Additional notices, if required by prior licensing conditions]
*
*/
package org.apache.taglibs.xsl;
import java.io.InputStream;
import java.io.Reader;
import java.io.StringReader;
import java.lang.reflect.Method;
import java.util.Hashtable;
import java.util.Enumeration;
import javax.servlet.ServletContext;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.BodyTagSupport;
import org.apache.xalan.xslt.XSLTInputSource;
import org.apache.xalan.xslt.XSLTProcessor;
import org.apache.xalan.xslt.XSLTProcessorFactory;
import org.apache.xalan.xslt.XSLTResultTarget;
import org.w3c.dom.Node;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
/**
* Apply an XSL stylesheet to an XML data source, rendering the output
* to the writer of our JSP page. This tag uses the Xalan XSLT processor,
* available at <code>http://xml.apache.org</code>.
*
* @author Craig R. McClanahan
* @version $Revision: 1.1 $ $Date: 2000/07/03 19:30:00 $
*/
public class ApplyTag extends BodyTagSupport {
// ------------------------------------------------------------- Properties
/**
* The body content of this tag, if we are using it as the data source.
*/
private String body = null;
// START BORTONE
/**
* The hashtable that will contains the split params attribute of this tag.
*/
private Hashtable paramsTable = null;
// STOP BORTONE
/**
* The name of the XML data bean.
*/
private String nameXml = null;
public String getNameXml() {
return (this.nameXml);
}
public void setNameXml(String nameXml) {
this.nameXml = nameXml;
}
/**
* The name of the XSL stylesheet bean.
*/
private String nameXsl = null;
public String getNameXsl() {
return (this.nameXsl);
}
public void setNameXsl(String nameXsl) {
this.nameXsl = nameXsl;
}
/**
* The property of the XML data bean.
*/
private String propertyXml = null;
public String getPropertyXml() {
return (this.propertyXml);
}
public void setPropertyXml(String propertyXml) {
this.propertyXml = propertyXml;
}
/**
* The property of the XSL stylesheet bean.
*/
private String propertyXsl = null;
public String getPropertyXsl() {
return (this.propertyXsl);
}
public void setPropertyXsl(String propertyXsl) {
this.propertyXsl = propertyXsl;
}
/**
* The XML data resource.
*/
private String xml = null;
public String getXml() {
return (this.xml);
}
public void setXml(String xml) {
this.xml = xml;
}
/**
* The XSL stylesheet resource.
*/
private String xsl = null;
public String getXsl() {
return (this.xsl);
}
public void setXsl(String xsl) {
this.xsl = xsl;
}
// START BORTONE
/**
* The XSL stylesheet params.
*/
private String params = null;
public String getParams() {
return (this.params);
}
public void setParams(String params) {
this.params = params;
}
// STOP BORTONE
// --------------------------------------------------------- Public Methods
/**
* Validate the attributes that were specified for consistency.
* Evaluate the body content of this tag if we will be using it as the
* XML data source; otherwise skip it.
*
* @exception JspException if a JSP error occurs
*/
public int doStartTag() throws JspException {
// Validate the data source attributes
if (nameXml != null) {
if (xml != null) {
throw new JspException("Cannot specify both 'nameXml' and 'xml'");
}
}
else if (propertyXml != null) {
throw new JspException("Cannot specify 'propertyXml' without 'nameXml'");
}
// Validate the stylesheet source attributes
if (nameXsl != null) {
if (xsl != null) {
throw new JspException("Cannot specify both 'nameXsl' and 'xsl'");
}
}
else if (propertyXsl != null) {
throw new JspException("Cannot specify 'propertyXsl' without 'nameXsl'");
}
if ((nameXsl == null) && (xsl == null)) {
throw new JspException("Must specify either 'nameXsl' or 'xsl'");
}
// START BORTONE
// If attribute params exits then it is evaluated
if (params != null) {
loadParams(params);
}
// STOP BORTONE
// Evaluate the tag body only if we need it
if ((nameXml == null) && (xml == null)) {
return (EVAL_BODY_TAG);
}
else {
return (SKIP_BODY);
}
}
/**
* Save the body content that has been processed, but do not iterate.
*
* @exception JspException if a JSP error has occurred
*/
public int doAfterBody() throws JspException {
if (bodyContent == null) {
body = "";
}
else {
body = bodyContent.getString().trim();
}
return (SKIP_BODY);
}
/**
* Finish up by performing the transformation and rendering the output.
*
* @exception JspException if a JSP exception occurs
*/
public int doEndTag() throws JspException {
// Prepare an input source for the data
XSLTInputSource data = null;
if (body != null) {
data = new XSLTInputSource(new StringReader(body));
}
else {
data = getInputSource(nameXml, propertyXml, xml);
}
// Prepare an input source for the stylesheet
XSLTInputSource style = getInputSource(nameXsl, propertyXsl, xsl);
// Prepare an output source for the results
XSLTResultTarget result = new XSLTResultTarget(pageContext.getOut());
// Create an XSLT processor and use it to perform the transformation
XSLTProcessor processor = null;
try {
processor = XSLTProcessorFactory.getProcessor();
// START BORTONE
if (paramsTable != null) {
Enumeration keys = paramsTable.keys();
while (keys.hasMoreElements()) {
String name = (String) keys.nextElement();
String value = (String) paramsTable.get(name);
processor.setStylesheetParam(name, value);
}
}
// STOP BORTONE
processor.process(data, style, result);
}
catch (SAXException e) {
throw new JspException(e.toString());
}
return (EVAL_PAGE);
}
/**
* Release any allocated resources.
*/
public void release() {
this.body = null;
// START BORTONE
this.paramsTable = null;
// STOP BORTONE
}
// -------------------------------------------------------- Private Methods
/**
* Construct and return an XSLTInputSource based on the specified
* parameters.
*
* @param name Name of a bean containing the input source (or has a
* property that returns the input source)
* @param property Name of a property of the specified bean that
* returns the input source
* @param resource Context-relative path to an application resource
* that provides the input source
*
* @exception JspException if a JSP error occurs
*/
private XSLTInputSource getInputSource(String name, String property,
String resource) throws JspException {
// If the resource is specified, use that for the input source
if (resource != null) {
ServletContext context = pageContext.getServletContext();
if (context == null) {
throw new JspException("Cannot find servlet context");
}
InputStream stream = context.getResourceAsStream(resource);
if (stream == null) {
throw new JspException("Missing resource '" + resource + "'");
}
return new XSLTInputSource(stream);
}
// Locate the source object
Object source = null;
Object bean = pageContext.findAttribute(name);
if (bean == null) {
throw new JspException("Missing bean '" + name + "'");
}
if (property == null) {
source = bean;
}
else {
try {
char first = Character.toUpperCase(property.charAt(0));
String methodName = "get" + first + property.substring(1);
Class paramTypes[] = new Class[0];
Method method = bean.getClass().getMethod(methodName,
paramTypes);
source = method.invoke(bean, new Object[0]);
}
catch (Exception e) {
throw new JspException(e.toString());
}
}
// Create an XSLTInputSource for the specified source object
if (source instanceof XSLTInputSource) {
return ((XSLTInputSource) source);
}
else if (source instanceof String) {
return (new XSLTInputSource(new StringReader((String) source)));
}
else if (source instanceof InputSource) {
return (new XSLTInputSource((InputSource) source));
}
else if (source instanceof InputStream) {
return (new XSLTInputSource((InputStream) source));
}
else if (source instanceof Node) {
return (new XSLTInputSource((Node) source));
}
else if (source instanceof Reader) {
return (new XSLTInputSource((Reader) source));
}
else {
throw new JspException("Invalid input source type '"
+ source.getClass().getName() + "'");
}
}
//START BORTONE
/**
* Loads in the Hashtable paramsTable the split params attribute.
*
* @param params Attribute of this tag that contains a string like:
* paramName=paramValue,paramName-N=paramValue-N
*
* @exception JspException if a JSP error occurs
*/
private void loadParams(String params) throws JspException {
String paramName = null;
String paramValue = null;
paramsTable = new Hashtable();
params = params.trim();
while (params.length() > 0) {
int eq_pos = params.indexOf('=');
if (eq_pos <= 1) {
throw new JspException("Syntax error for attribute 'params'. Usage:
param=value,paramN=valueN");
}
else {
paramName = params.substring(0, eq_pos).trim();
}
if (paramName.length() == 0) {
throw new JspException("Syntax error for attribute 'params'. Usage:
param=value,paramN=valueN");
}
int comma_pos = params.indexOf(',', eq_pos);
if (comma_pos == -1) {
paramValue = params.substring(eq_pos + 1).trim();
}
else if (comma_pos <= 1) {
throw new JspException("Syntax error for attribute 'params'. Usage:
param=value,paramN=valueN");
}
else {
paramValue = params.substring(eq_pos + 1, comma_pos).trim();
}
if (paramValue.length() == 0) {
throw new JspException("Syntax error for attribute 'params'. Usage:
param=value,paramN=valueN");
}
if (paramsTable == null) {
paramsTable = new Hashtable();
}
paramsTable.put(paramName, paramValue);
if (comma_pos == -1) {
break;
}
params = params.substring(comma_pos + 1);
}
}
//STOP BORTONE
}
xsl.jar