This discussion is archived
1 2 Previous Next 15 Replies Latest reply: Jun 3, 2010 12:25 AM by 776900 RSS

Transformer serialize problem

776900 Newbie
Currently Being Moderated
Hello

I have a Java application which has a little method to serialize a javax.xml.transform.Transformer into a byte[].

Here is the method:
...  
Object obj = transformer;  
ByteArrayOutputStream bos = new ByteArrayOutputStream() ;  
ObjectOutput out = new ObjectOutputStream(bos);  
out.writeObject(obj);  
out.close();  
  
// Get the bytes of the serialized object  
byte[] buf = bos.toByteArray();  
  
...  
However, I am getting the following error:

java.io.NotSerializableException: net.sf.saxon.Controller
at java.io.ObjectOutputStream.writeObject0(Unknown Source)
at java.io.ObjectOutputStream.writeObject(Unknown Source)
at .processing.transform.cache.CacheTransformers.addTransformer(CacheTransformers.java:62)
....

How can I solve the problem? Is there any other way to serialize a javax.xml.transform.Transformer object?

Thanks
Regards
  • 1. Re: Transformer serialize problem
    jtahlborn Expert
    Currently Being Moderated
    what makes you think a Transformer is/should be Serializable?
  • 2. Re: Transformer serialize problem
    EJP Guru
    Currently Being Moderated
    I have a Java application which has a little method to serialize a javax.xml.transform.Transformer into a byte[].
    Why? What state does a Transformer have that you can't recreate by just making a new one?

    This is pointless.
  • 3. Re: Transformer serialize problem
    776900 Newbie
    Currently Being Moderated
    Hello

    Because I am tried to store a Transformer object in a Cache (that only can store object which can be Serialized). This the reason why I am trying to serialize my Transformer object.

    Thanks
    regards
  • 4. Re: Transformer serialize problem
    EJP Guru
    Currently Being Moderated
    I know you want to serialize it. I asked you why? Telling me where you want to serialize it to doesn't answer that question.

    If you are worried about the performance implications of creating lots of Transformer objects for the same stylesheet, the correct approach is to create a Templates object for teh stylesheet and get a new Transformer from it every time you need one. As per the Javadoc.

    However you haven't said what you're worried about, or what your underlying purpose is, so we are all just guessing here.
  • 5. Re: Transformer serialize problem
    776900 Newbie
    Currently Being Moderated
    Thanks for your reply.

    My main concern is, as you guess, the performance.

    When I create the transformer
        Transformer foTrans = tFactoryXML2FO.newTransformer(streamSource);
      
    where "streamSource" is a StreamSource object with my XSLT, the time taken just for this line is more or less 8 seconds (which its a lot).

    So, to try, reduce the time taken, I have tried several approaches. One of them is using Caching. For caching, I searched over the Internet and I found Apache JSC. I tried it but I got an error :

    "java.io.NotSerializableException: net.sf.saxon.Controller"

    This error happens when I try to write the Transformer object into an ObjectOutput (line ***):

           ....
           ByteArrayOutputStream bos = new ByteArrayOutputStream() ;
           ObjectOutput out = new ObjectOutputStream(bos);
                   
           out.writeObject(transformer); (***)
           out.close();
                  
         // Get the bytes of the serialized object
         byte[] buf = bos.toByteArray();
                  
            cache.put(key, buf);
            ....
    Did you understood my problem?

    Thanks again
  • 6. Re: Transformer serialize problem
    EJP Guru
    Currently Being Moderated
    Yes I do understand your problem. Transformer isn't Serializable. I have also given you the correct solution. The next move here is yours.
  • 7. Re: Transformer serialize problem
    776900 Newbie
    Currently Being Moderated
    I'm going to try with the templates approach but if i'm not wrong I already did it without success.

    Thanks
    Regards
  • 8. Re: Transformer serialize problem
    EJP Guru
    Currently Being Moderated
    Then you must have done it wrong.
  • 9. Re: Transformer serialize problem
    776900 Newbie
    Currently Being Moderated
    I tried using Templates but the performance is the same.
    StreamSource streamSource = new StreamSource(xsltfile.getInputStream());
                   
    SchemaAwareConfiguration conf = new SchemaAwareConfiguration();
    conf.addSchemaSource(new StreamSource(new BufferedInputStream(tu.getBriquesXSDInput())));
    SchemaAwareTransformerFactory tFactoryXML2FO = new SchemaAwareTransformerFactory(conf);
    
    Transformer foTrans = tFactoryXML2FO.newTransformer(streamSource);  => as taken 7297 ms to be executed
    
    OR
    
    Templates temp = tFactoryXML2FO.newTemplates(streamSource);    => as taken 6955 ms to be executed
    Transformer foTrans = temp.newTransformer();
    The method which makes the transformation is on a web service and the transformation will be made only once for each time the web service is executed.
    So if I dont keep in cache the Transformer or the Templates object (I dont see another solution for now), each web service call will take at least 7 seconds (more or less).

    I tried then to keep the Templates object into a Cache and it worked.

    Thanks
    Regards
  • 10. Re: Transformer serialize problem
    EJP Guru
    Currently Being Moderated
    But you only have to create the Templates once per XSLT, then get Transformers when you need them. It is a cache. Use it that way.
  • 11. Re: Transformer serialize problem
    776900 Newbie
    Currently Being Moderated
    Yes, thats what I am doing. The presented code was just to show the time performance...

    Thanks

    regards
  • 12. Re: Transformer serialize problem
    EJP Guru
    Currently Being Moderated
    But it's irrelevant. You are comparing the cost of creating one Templates object as against the cost of creating each Transformer object. 7297ms for one Templates as against 6955 per Transformer. And I did say, several times, to keep the Templates object around for re-use. Serializing it to a Cache probably won't work any better than serializing the Transformer object, but that's not the only way to keep it lying around, is it?
  • 13. Re: Transformer serialize problem
    776900 Newbie
    Currently Being Moderated
    I am storing the Templates object in the Cache without serializing it now.
    If I stored the Transformer object it was different because an error happened when storing the object on the cache (because it was not serializable).
  • 14. Re: Transformer serialize problem
    EJP Guru
    Currently Being Moderated
    I'm aware of that. You don't seem to understand that the Templates object is a cache.
1 2 Previous Next