We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you a podcast all about Citrix Workspace, moving to the cloud, and analytics & intelligence. Episode 2 coming soon!Listen Now

x

XSLT transform error in java

M_SYED
M_SYED asked
on
Medium Priority
437 Views
Last Modified: 2013-11-23
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.


Comment
Watch Question

CERTIFIED EXPERT
Top Expert 2016

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

What class is 'xslTransformer'?

Author

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;
    }
}
Leo EikelmanDirector, IT and Business Development

Commented:
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?

Author

Commented:
hmmmmm.....

Let me check.
I will get back to you when I narrow the bug down a bit........... :- (
CERTIFIED EXPERT
Top Expert 2016

Commented:
Try validating your stylesheet. Try

http://validator.w3.org/

Author

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 .............................
Leo EikelmanDirector, IT and Business Development

Commented:
Are you releasing all of your resources?

You must not be releasing the StreamSource.

check that it is properly released


Leo

Author

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.......
CERTIFIED EXPERT
Top Expert 2016

Commented:
Why should a file that is only being read be locked?
Leo EikelmanDirector, IT and Business Development

Commented:
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

Author

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....................
Director, IT and Business Development
Commented:
glad you solved it

Leo

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.