• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 401
  • Last Modified:

XSLT transform error in java

Hi,

I am getting the following exception, any info on when and why it occurs will be great. The exception is

"javax.xml.transform.TransformerException: Did not find the stylesheet root!"

The code that I am using is;

    private String generateEmail(boolean addcommid,
            UserTypeLocalValue recipient, InputStream stream, UserTypeLocalValue sender,
            String subject, Map properties) throws Exception {

            System.out.println("--generateEmail--");

        CommunicationTypeLocalValue initEmailData = new CommunicationTypeLocalValue();

        initEmailData.setStatus(AcuvueEJBConstants.CommunicationStatusCreated);

            CommunicationTypeLocal ctl;
            try
                  {
                  ctl = CommunicationTypeUtil.getLocalHome().create(initEmailData);
                  }
            catch (Exception exception)
                  {
                  exception.printStackTrace ( ) ;
                  throw new Exception ( exception.getMessage ( ) ) ;
                  }

        CommunicationTypeLocalValue emailData = ctl.getCommunicationTypeLocalValue();

        emailData.setCommunicationId(AcuvueEJBConstants.CommunicationStatusCreated);

        emailData.setCreationDate(new Date());

            //System.out.println(recipient.getUserName()+"|||||"+sender.getUserName());

        emailData.setRecipient(recipient.getUserName());
        emailData.setSender(sender.getUserName());

        emailData.setSubject(subject);

        log.info("generateEmail:" + emailData.getCommunicationId());

        if ( addcommid ) {
            properties.put("communication", emailData.getPrimaryKey().toString());
        }

        XsltResult result = xslTransformer.transform(stream, properties);

//            Was removed because the text extraction was faulty and not needed
//      emailData.setTextMessage(result.getTextResult());
        emailData.setTextMessage("");

        emailData.setHtmlMessage(result.getHtmlResult());
//            The folowing is for test purposes only when the getHtmlResult is failing
//            emailData.setHtmlMessage("<html><header></header><body><h1>HTML Content</h1></body></html>");

        emailData.setStatus(AcuvueEJBConstants.CommunicationStatusGenerated);

        ctl.setCommunicationTypeLocalValue(emailData);

        log.debug ( ctl.getCommunicationTypeLocalValue().getPrimaryKey() ) ;

log.info ( "generateEmail: 19" ) ;
        return ctl.getCommunicationTypeLocalValue().getPrimaryKey();
    }

Appreciate your help.


0
M_SYED
Asked:
M_SYED
  • 5
  • 4
  • 3
1 Solution
 
CEHJCommented:
>>XsltResult result = xslTransformer.transform(stream, properties);

What class is 'xslTransformer'?
0
 
M_SYEDAuthor Commented:
Hi,

Its of type,
com.blueprintny.ecpportal.xslt.XslTransformer

The code of this class is;
/*
 * Created on Mar 28, 2005
 */
package com.blueprintny.ecpportal.xslt;

import java.io.InputStream;
import java.io.StringReader;
import java.io.StringWriter;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Result;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;

import org.apache.log4j.Logger;
import org.w3c.dom.Document;
import org.xml.sax.InputSource;

/**
 * Transforms XSL files.
 * @author mparrish
 */
public class XslTransformer {
    private static Logger log = Logger.getLogger(XslTransformer.class);
   
    public XsltResult transform(InputStream stream, Map properties) throws Exception {
        StreamSource source = new StreamSource(stream);

        TransformerFactory tf = TransformerFactory.newInstance();
        Transformer trans = tf.newTransformer(source);

        log.debug("createdTransformer");

        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = factory.newDocumentBuilder();
        Document document = builder.parse(new InputSource(new StringReader(
                "<?xml version=\"1.0\" encoding=\"UTF-8\"?><blank/>")));

        log.debug("createDocument");

        StringWriter sw = new StringWriter();
        Result result = new StreamResult(sw);

        StringWriter sw2 = new StringWriter();
        Result result2 = new StreamResult(sw2);

        log.debug("createResult");

        Source source2 = new DOMSource(document.getDocumentElement());

        Iterator propertiesIter = properties.entrySet().iterator();
       
        while (propertiesIter.hasNext()) {
            Map.Entry entry = (Entry) propertiesIter.next();
            trans.setParameter((String) entry.getKey(), entry.getValue());
        }

        trans.setOutputProperty(OutputKeys.METHOD, "html");

        log.debug("starting transform");
        trans.transform(source2, result);

        trans.setOutputProperty(OutputKeys.METHOD, "text");
        trans.transform(source2, result2);

        XsltResult results = new XsltResult(sw2.toString(), sw.toString());
        log.debug("transformed");
        return results;
    }
}
0
 
Leo EikelmanCommented:
I've seen this happen sometimes if you try to declare two root nodes in your style sheet.

Basically, you have a null style sheet root (I'm sure you already knew this).  It might of not been initialized for some reason.

What line of code causes this problem?
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
M_SYEDAuthor Commented:
hmmmmm.....

Let me check.
I will get back to you when I narrow the bug down a bit........... :- (
0
 
CEHJCommented:
Try validating your stylesheet. Try

http://validator.w3.org/
0
 
M_SYEDAuthor Commented:
Hi Experts,

Am back with more details; The piece of code is
public class XslTransformer {
    private static Logger log = Logger.getLogger(XslTransformer.class);
   
    public XsltResult transform(InputStream stream, Map properties) throws Exception {
        StreamSource source = new StreamSource(stream);
System.out.println("Inside XslTransformer"); // First SYS OUT

        TransformerFactory tf = TransformerFactory.newInstance();
        Transformer trans = tf.newTransformer(source);
System.out.println(" Transformer Object formed"); // Second SYS OUT
        log.debug("createdTransformer");

Now the interesting thing is;
Everything works the first time around and the email is sent.
But when its executed the second time the Exception comes up. The first sys out is printed but the second sys out is not printed instead I get the javax.xml.transform.TransformerException: Did not find the style sheet root

Is there some kind of file handle or obect lock that has to be released.


PLS help .............................
0
 
Leo EikelmanCommented:
Are you releasing all of your resources?

You must not be releasing the StreamSource.

check that it is properly released


Leo
0
 
M_SYEDAuthor Commented:
None of the resources for this class are being released. I have posted the whole code in a prior posting.

I think that I will set the Transformer, TransformerFactory objects to null before returning.......

Lets see.......
0
 
CEHJCommented:
Why should a file that is only being read be locked?
0
 
Leo EikelmanCommented:
Well it could be locked by the class using the source and not releasing it.  But even if this way happening, I think it would release it automatically after the first run was complete.  

Leo
0
 
M_SYEDAuthor Commented:
Hello Experts,

I have solved this problem.
Basicaly the issue was that the "Stream" being used as the base XSLT was initialized outside of the loop, which is perfectly normal.
For the first execution the method call picks up the stream correctly but for subsequent executions it fails to pick it up somehow......... ?
So I have initialized the "Stream" in the loop and everything seems to be working now......... : - )

Dont know why this behaviour is occuring though....................
0
 
Leo EikelmanCommented:
glad you solved it

Leo
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 5
  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now