You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@xalan.apache.org by bu...@apache.org on 2002/03/29 18:39:24 UTC
DO NOT REPLY [Bug 7611] New: -
SAX Events not being processed with style sheet
DO NOT REPLY TO THIS EMAIL, BUT PLEASE POST YOUR BUG
RELATED COMMENTS THROUGH THE WEB INTERFACE AVAILABLE AT
<http://nagoya.apache.org/bugzilla/show_bug.cgi?id=7611>.
ANY REPLY MADE TO THIS MESSAGE WILL NOT BE COLLECTED AND
INSERTED IN THE BUG DATABASE.
http://nagoya.apache.org/bugzilla/show_bug.cgi?id=7611
SAX Events not being processed with style sheet
Summary: SAX Events not being processed with style sheet
Product: XalanJ2
Version: 2.3
Platform: All
OS/Version: All
Status: NEW
Severity: Major
Priority: Other
Component: Xalan
AssignedTo: xalan-dev@xml.apache.org
ReportedBy: ryan.asleson@stpaul.com
I built a custom XML Reader that implements org.xml.sax.XMLReader. The
CustomReader sends SAX events to a tranformer handler, which is supposed to
process the XML with the designated style sheet. The problem: The XML is not
processed with the style sheet, thus the only ouput is the text from the style
sheet. It's much easier to understand when seeing it.
I'm using Xalan 2.3.1. Everything works OK on 2.0.0 and 2.1.0. It starts to
NOT work with 2.2, and also in 2.3.1.
I've tried some work arounds but haven't found any. If there are any please
list them.
I have tried to isolate the problem to make it easy reproduce.
--- STEPS TO REPRODUCE ---
1. See listing 1 below, "TransformerHandlerTEST.java." Compile the file using
Xalan 2.1.0.
2. See listing 2 below, "discussionForumHome.xslt", and place it in the same
directory as TransformerHandlerTEST.class.
3. Change the working directory to the directory where the files reside. Run
the program using "java -classpath .;xalan.jar; TransformerHandlerTEST". The
XML produce will be output to the screen.
4. Repeat Step 3 above, but use "java -classpath .;xalan.jar;
TransformerHandlerTEST x" (Note extra argument) to run the program. This will
cause the XML to be merged with the discussionForumHome.xslt style sheet to
produce HTML. This is how things should work.
5. Now, see how it doesn't work. Repeat the above process, but instead use
Xalan 2.3.1, by compiling and re-running the program (When using 2.3.1, I think
xml-apis.jar needs to be added to the classpath). When the style sheet is
applied, it is not merged with the XML -- only the non-processed HTML is output
to the screen.
---- Listing 1: TransformerHandlerTEST.java ----
import java.io.*;
import java.util.*;
import org.xml.sax.*;
import org.xml.sax.helpers.*;
import javax.xml.transform.*;
import javax.xml.transform.sax.*;
import javax.xml.transform.stream.*;
/**
* A class that implements the SAX2 XMLReader interface. The
* intent of this class is to make it easy for subclasses to act as
* SAX2 XMLReader implementations. This makes it possible, for example, for
* them to emit SAX2 events that can be fed into an XSLT processor for
* transformation.
*
*/
public class TransformerHandlerTEST implements org.xml.sax.XMLReader {
private Map featureMap = new HashMap();
private Map propertyMap = new HashMap();
private EntityResolver entityResolver;
private DTDHandler dtdHandler;
private ContentHandler contentHandler;
private ErrorHandler errorHandler;
public boolean getFeature(String name)
throws SAXNotRecognizedException, SAXNotSupportedException {
Boolean featureValue = (Boolean) this.featureMap.get(name);
return (featureValue == null) ? false
: featureValue.booleanValue();
}
public void setFeature(String name, boolean value)
throws SAXNotRecognizedException, SAXNotSupportedException {
this.featureMap.put(name, new Boolean(value));
}
public Object getProperty(String name)
throws SAXNotRecognizedException, SAXNotSupportedException {
return this.propertyMap.get(name);
}
public void setProperty(String name, Object value)
throws SAXNotRecognizedException, SAXNotSupportedException {
this.propertyMap.put(name, value);
}
public void setEntityResolver(EntityResolver entityResolver) {
this.entityResolver = entityResolver;
}
public EntityResolver getEntityResolver() {
return this.entityResolver;
}
public void setDTDHandler(DTDHandler dtdHandler) {
this.dtdHandler = dtdHandler;
}
public DTDHandler getDTDHandler() {
return this.dtdHandler;
}
public void setContentHandler(ContentHandler contentHandler) {
this.contentHandler = contentHandler;
}
public ContentHandler getContentHandler() {
return this.contentHandler;
}
public void setErrorHandler(ErrorHandler errorHandler) {
this.errorHandler = errorHandler;
}
public ErrorHandler getErrorHandler() {
return this.errorHandler;
}
public void parse(String systemId) throws IOException, SAXException {
parse(new InputSource(systemId));
}
/**
* NOTE: For testing purposes only. Emit hard-coded SAX
* events
*/
public void parse(InputSource input) throws IOException, SAXException {
//******************************************************
//NOTE: input is not used here. Emit hard coded events.
//******************************************************
final AttributesImpl EMPTY_ATTR = new AttributesImpl();
ContentHandler contentHandler = getContentHandler();
if(contentHandler == null) {
return;
}
//Start the document
contentHandler.startDocument();
//Create the document root
contentHandler.startElement("", "", "discussionForumHome", EMPTY_ATTR);
//Create the single message element
AttributesImpl attribute = new AttributesImpl();
attribute.addAttribute("", "", "id", "", "1");
attribute.addAttribute("", "", "name", "", "Java Programming");
contentHandler.startElement("", "", "messageBoard", attribute);
contentHandler.endElement("", "", "messageBoard");
attribute = new AttributesImpl();
attribute.addAttribute("", "", "id", "", "2");
attribute.addAttribute("", "", "name", "", "XML Programming");
contentHandler.startElement("", "", "messageBoard", attribute);
contentHandler.endElement("", "", "messageBoard");
attribute = new AttributesImpl();
attribute.addAttribute("", "", "id", "", "3");
attribute.addAttribute("", "", "name", "", "XSLT Questions");
contentHandler.startElement("", "", "messageBoard", attribute);
contentHandler.endElement("", "", "messageBoard");
contentHandler.endElement("","", "discussionForumHome");
//End the document
contentHandler.endDocument();
}
public static void main(String[] args) throws Exception {
String xsltFileName = null;
//If any argusments are supplied, then use the XSL style sheet to do
//the transformation. If no arguments are supplied, simply output
//the resulting XML
if(args.length > 0) {
xsltFileName = "discussionForumHome.xslt";
}
else {
xsltFileName = null;
}
TransformerFactory transFact = TransformerFactory.newInstance();
if (transFact.getFeature(SAXTransformerFactory.FEATURE)) {
SAXTransformerFactory saxTransFact =
(SAXTransformerFactory) transFact;
TransformerHandler transHand = null;
if (xsltFileName == null) {
transHand = saxTransFact.newTransformerHandler();
} else {
transHand = saxTransFact.newTransformerHandler(
new StreamSource(new File(xsltFileName)));
}
//Set the destination for the XSLT transformation
transHand.setResult(new StreamResult(System.out));
//Create the reader and supply a dummy input source
XMLReader xmlReader = new TransformerHandlerTEST();
InputSource csvInputSrc = new InputSource();
//Attach the XSLT processor to the XMLReader
xmlReader.setContentHandler(transHand);
xmlReader.parse(csvInputSrc);
} else {
System.err.println("SAXTransformerFactory is not supported.");
System.exit(1);
}
}
}
----- End Listing 1 ----
---- Listing 2: discussionForumHome.xslt ----
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html"/>
<xsl:template match="/">
<html>
<head>
<title>Discussion Forum Home Page</title>
</head>
<body>
<h1>Discussion Forum Home Page</h1>
<h3>Please select a message board to view:</h3>
<ul>
<xsl:apply-templates select="discussionForumHome/messageBoard"/>
</ul>
</body>
</html>
</xsl:template>
<xsl:template match="messageBoard">
<li><a href="viewForum?id={@id}"><xsl:value-of select="@name"/></a></li>
</xsl:template>
</xsl:stylesheet>
---- End Listing 2 ----