14 Replies Latest reply on Feb 8, 2008 10:07 AM by Andreas Watler

    Calling Smooks from Java to do xslt transfomation

    Andreas Watler Newbie

      Hi,

      I'm using JBoss ESB for my degree dissertation.
      For a transformation a have to do, i wanted to use smooks,
      so i can compare diffrent kinds of transfomation tactics.

      I wanted to start with a pure xslt transformation applied at the document root, just to test how to call smooks etc.
      I'm calling Smooks from Java code and not through

      <action>
      configurations,
      because i have multiple output messages for one input message, and as far as I understand the ESB,
      you cant use the actions with this kind of setup.
      As input I want to use dom.Documents, same as output.

      So, my code so far is:
      
      public Document convert(Document inDoc) {
       Document outDoc = null;
       Smooks smooks = null;
       try {
       smooks = new Smooks();
       smooks.addConfigurations("pure-xsl", new URIResourceLocator().getResource("file://c:/pathtToConfig/smooks-config.xml"));
       DOMSource in = new DOMSource(inDoc);
       DOMResult out = new DOMResult();
       smooks.filter(in, out, smooks.createExecutionContext());
       outDoc = (Document)out.getNode();
      
       } catch (IOException e) {
       log.error(e.getMessage());
       } catch (SAXException e) {
       log.error(e.getMessage());
       } catch (Exception e) {
       log.error(e.getMessage());
       }
       return outDoc;
      }
      
      
      


      and my smooks-config.xml:

      
      <?xml version='1.0' encoding='UTF-8'?>
      <smooks-resource-list xmlns="http://www.milyn.org/xsd/smooks-1.0.xsd">
      
       <resource-config selector="$document">
       <resource type="xsl">file://C:/pathToXSLT/univis2cdm_v1.0.xslt</resource>
       <!-- Tryed it with this commented and uncommented -->
       <!--param name="is-xslt-templatelet">false</param-->
       </resource-config>
      
      </smooks-resource-list>
      
      


      When i run this, there are not error messages or something like that,
      but after the filtering, the output is exactly the same as the input, so nothing really happend.
      My questions are now:

      Did i do something wrong? I think everything is OK.
      The XSLT works when I use it outside of Smooks, the pathes are right, no error messages...

      I read in the Smooks Javadoc (Smooks v.1.0) that u can register a Eventhandler for the filter process,
      and there is a BasicExecutionEventListener for this out of the box,
      but it seems this isnt the case in the version used in JBoss ESB.
      Is there some other way i can look into the whole process, to find out whats going wrong?

      Thanks alot for your help, I'm really stuck here, and I'm running out of ideas what to try else.
      And sorry for my bad english :-).

      Andreas

      P.S: I also tryed to apply the xslt with the admin console, but I have a problem there too.
      Everytime i want to save in "Configure New Message Contract" i get a Exception
      (java.util.MissingResourceException: Can't find bundle for base name messages, locale de_DE)

        • 1. Re: Calling Smooks from Java to do xslt transfomation
          Tom Fennelly Master

          Hi Andreas.

          Turn on debug logging. You'll then see which resources are being applied to the message. It may be that you don't have the Smooks Templating Cartridge on the classpath. If this is not available, Smooks Core will not be able to recognise what an "xsl" resource type is and which HandlerFactory to use to apply it.

          JBoss ESB (all versions) use Smooks v0.9 or earlier, so v1.0 features (such as the EventListener stuff you mentioned) are not available on the ESB.

          • 2. Re: Calling Smooks from Java to do xslt transfomation
            Andreas Watler Newbie

            Hi,

            thanks for your reply!
            I tried enabling debug-logging.
            Here is what happens after initializing Smooks and calling filter()
            (These are only those messages I think are related to Smooks):


            [SmooksResourceConfigurationList] Smooks ResourceConfiguration List [default] created.
            [SmooksResourceConfigurationList] Smooks ResourceConfiguration List [installed-cdu-creators.cdrl] created.
            [SmooksResourceConfigurationList] Smooks ResourceConfiguration [Target Profile: [[star]], Selector: [cdu-creator], Selector Namespace URI: [null], Resource: [org.milyn.delivery.JavaContentDeliveryUnitCreator], Num Params: [1]] added to list [installed-cdu-creators.cdrl].
            [SmooksResourceConfigurationList] Smooks ResourceConfiguration List [installed-param-decoders.cdrl] created.
            [SmooksResourceConfigurationList] Smooks ResourceConfiguration [Target Profile: [[star]], Selector: [param-type:string-list], Selector Namespace URI: [null], Resource: [org.milyn.cdr.TokenizedStringParameterDecoder], Num Params: [1]] added to list [installed-param-decoders.cdrl].
            [SmooksResourceConfigurationList] Smooks ResourceConfiguration [Target Profile: [[star]], Selector: [param-type:string-hashset], Selector Namespace URI: [null], Resource: [org.milyn.cdr.TokenizedStringParameterDecoder], Num Params: [1]] added to list [installed-param-decoders.cdrl].
            [SmooksResourceConfigurationList] Smooks ResourceConfiguration [Target Profile: [[star]], Selector: [param-type:properties], Selector Namespace URI: [null], Resource: [org.milyn.cdr.PropertyListParameterDecoder], Num Params: [1]] added to list [installed-param-decoders.cdrl].
            [SmooksResourceConfigurationList] Smooks ResourceConfiguration List [installed-serializers.cdrl] created.
            [SmooksResourceConfigurationList] Smooks ResourceConfiguration [Target Profile: [[star]], Selector: [context-object], Selector Namespace URI: [null], Resource: [org.milyn.delivery.dom.serialize.ContextObjectSerializationUnit], Num Params: [0]] added to list [installed-serializers.cdrl].
            [SmooksResourceConfigurationList] Smooks ResourceConfiguration List [pure-xsl] created.
            [SmooksResourceConfigurationList] Smooks ResourceConfiguration [Target Profile: [[star]], Selector: [$document], Selector Namespace URI: [null], Resource: [file://C:/eclipse_JBoss_WS/ESB_Handle_UnivIS_Files/src/esb/handle/univis/file/univis2cdm_v1.0.xslt], Num Params: [1]] added to list [pure-xsl].

            /// some stuff .....

            [ContentDeliveryConfigImpl] ==================================================================================================
            [ContentDeliveryConfigImpl] Resource configuration (sorted) for profile [org.milyn.profile.Profile
            [ContentDeliveryConfigImpl] 0) cdu-creator
            [ContentDeliveryConfigImpl] " (0) Target Profile: [[star]], Selector: [cdu-creator], Selector Namespace URI: [null], Resource: [org.milyn.delivery.JavaContentDeliveryUnitCreator], Num Params: [1] "
            [ContentDeliveryConfigImpl] 0) param-type:properties
            [ContentDeliveryConfigImpl] " (0) Target Profile: [[star]], Selector: [param-type:properties], Selector Namespace URI: [null], Resource: [org.milyn.cdr.PropertyListParameterDecoder], Num Params: [1] "
            [ContentDeliveryConfigImpl] 0) param-type:string-list
            [ContentDeliveryConfigImpl] " (0) Target Profile: [[star]], Selector: [param-type:string-list], Selector Namespace URI: [null], Resource: [org.milyn.cdr.TokenizedStringParameterDecoder], Num Params: [1] "
            [ContentDeliveryConfigImpl] 0) $document
            [ContentDeliveryConfigImpl] " (0) Target Profile: [[star]], Selector: [$document], Selector Namespace URI: [null], Resource: [file://C:/eclipse_JBoss_WS/ESB_Handle_UnivIS_Files/src/esb/handle/univis/file/univis2cdm_v1.0.xslt], Num Params: [1] "
            [ContentDeliveryConfigImpl] 0) context-object
            [ContentDeliveryConfigImpl] " (0) Target Profile: [[star]], Selector: [context-object], Selector Namespace URI: [null], Resource: [org.milyn.delivery.dom.serialize.ContextObjectSerializationUnit], Num Params: [0] "
            [ContentDeliveryConfigImpl] 0) param-type:string-hashset
            [ContentDeliveryConfigImpl] " (0) Target Profile: [[star]], Selector: [param-type:string-hashset], Selector Namespace URI: [null], Resource: [org.milyn.cdr.TokenizedStringParameterDecoder], Num Params: [1] "
            [ContentDeliveryConfigImpl] ==================================================================================================
            [SmooksDOMFilter] No assembly units configured for device [org.milyn.profile.Profile
            [SmooksDOMFilter] Starting processing phase [org.milyn.profile.Profile


            After this, there are no more messages I can link to Smooks.
            I also tried it with a mini-xslt directly inside the smooks-config.xml (it's called xslt-templatelet I think) that only fetches some data, but it's exactly the same.

            Regarding your classpath tip:
            How exactly would I do that? I tried several things, but none of them worked so I guess i did it wrong.
            But then again, wouldn't there be some error-messages if the problem was here.

            I also tried the tip with removing xalan.jar from $JBOSS_HOME/lib/endorsed/ mentioned here (seemed like a pretty good fit) :
            http://milyn.codehaus.org/Smooks+Trouble+Shooting+Guide
            but again it didn't change a thing.

            So I'm pretty much running out of ideas. Is there anything else I could do?
            Is my code calling Smooks correct so far?

            Thanks again for the help.

            Andreas

            P.S: I'm using jbossesb-server-4.2GA by the way, but used the latest JBossAS when I tried the Admin Console.

            • 3. Re: Calling Smooks from Java to do xslt transfomation
              Tom Fennelly Master

              There should be more Smooks related logging after what you posted there, but going on what you have there, I'm fairly sure the Templating cartridge is not on the CP, which would be odd considering you say you're running this sample under the ESB Server, which registers the HandlerFactory for XSL.

              Have you run any of the transform_* quickstarts? One or more of these use XSL as part of the transformation. If you're not using the ESB (doing it directly ala your 1st post) and you're using Smooks v0.9 (not v1.0), you'll need to manually register the HandlerFactory for XSL (v1.0 automatically picks these up and auto registers them).

              • 4. Re: Calling Smooks from Java to do xslt transfomation
                Andreas Watler Newbie

                Hi,

                I'll try to claryfy how my setup is:
                I'm using an ESB action to pick up a xml-file from a watched folder, so this is my input message.
                I then split that message into multiple dom.Documents and want to convert them now using Smooks before
                I send them out on the ESB. So in a way I am using ESB, but I don't now if the environment at this point is the same when I call Smooks the way I do (and not over actions). I will try those quickstarts, I just thought until now they wouldn't apply to my problem, because they all use the action method to call Smooks.

                Regarding the Classpath: I did put the milyn-smooks-templating-0.3.jar from server\default\deploy\smooks.esb\ into my classpath and also tried to put all the jars of this dir into the server\default\lib\ dir just to be on the save side. I thought that would do the trick. Is there any other way I can reister this cartridge?
                I didn't find something like this mentioned in any of the docs I've read.
                I guess I'm going to look a bit closer into some docs now :-).

                Anyways, thanks so far for your help and patience.

                Regards

                Andreas

                • 5. Re: Calling Smooks from Java to do xslt transfomation
                  Tom Fennelly Master

                  Best thing to do is try the ESB quickstarts, or the Smooks tutorials, but without modifying them beforehand ;-) Get something working before you do anything else.

                  That info is not in any of the docs because people are expected to use the SmooksTransformer action to do this, in which case you shouldn't need to worry about any of that stuff.

                  • 6. Re: Calling Smooks from Java to do xslt transfomation
                    Andreas Watler Newbie

                    It works :-D!

                    I used the

                    TemplatingUtils.registerCDUCreators(smooks);
                    

                    method like mentioned here:

                    http://milyn.codehaus.org/javadoc/v0.9/smooks-cartridges/templating/

                    and now at least something is showing.
                    Right now it seems like the $document selector doesn't work (perhaps a version-thing too?),
                    but I think I'll figure that out.

                    Thanks LOADS! for your help, I was about to give up :-).

                    Regards
                    Andreas

                    • 7. Re: Calling Smooks from Java to do xslt transfomation
                      Andreas Watler Newbie

                      Hi,

                      me again :-).
                      After I fixed the problem from above I thought everything was fine,
                      but I'm still having problems with Smooks.

                      My problem now is, that Smooks doesn't apply the xslt like I think it should.
                      Like mentioned I have a XML as dom.Document as input, and a XSLT that works with the XML
                      when applyed otside of Smooks.

                      My input XML has the structure:

                      <?xml version="1.0" encoding="UTF-8"?>
                      <Root>
                       <..>
                      </Root>
                      


                      and my xslt looks like:

                      <?xml version="1.0" encoding="UTF-8"?>
                      <xsl:stylesheet version="1.0"
                      xmlns:xsl="http://www.w3.org/1999/XSL/Transform"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                      xmlns:xs="http://www.w3.org/2001/XMLSchema"
                      exclude-result-prefixes="xs">
                       <xsl:template match="Root">
                       <...>
                       </xsl:template>
                      </xsl:stylesheet>
                      


                      Inside the smooks-config.xml I have:

                      <resource-config selector="Root">
                      <resource type="xsl">file://pathTo.xslt</resource>
                      


                      After I looked at this tutorial I was pretty sure the transformation would work, because
                      they have a pretty similar setup at the xslt part of the transformation. (It's also the transform_EDI2XML_Groovy_XSLT quickstart I think)

                      http://labs.jboss.com/jbossesb/resources/tutorials/xformation-demos/console-demo-03.html

                      But the transformation doesn't work here the way it does in the tutorial.
                      It seems the transformation always starts to low in the xml-tree.
                      Meaning when I configure "Root" as the selector, the match="Root" template is never hit.
                      Only nodes under the "Root" element are visable for the transformation it seems.
                      I also tried using the $document selector, but it made no difference.
                      I would understand the whole thing if it was just like that with Smooks, but why does the tutorial/quickstart work then?

                      I think I could change the whole xslt so it still works, but I would prefer tho leave it the way it is,
                      so I can use the same xslt in & outside of Smooks, besides not having some extra work :-).
                      Perhaps I just don't get it, but I'am very confused, especially because of the tutorial/quickstart working the way it is.
                      Could it be, that this too is connected to the fact that I'm calling Smooks the way I do?

                      Thanks for the help.
                      Andreas



                      • 8. Re: Calling Smooks from Java to do xslt transfomation
                        Daniel Bevenius Master

                        Hi Andreas,

                        could you try specifying the selector like this:

                        <resource-config selector="$document">
                        

                        An example of this can be found here : http://milyn.codehaus.org/Smooks+Example+-+xslt-namespaces

                        Regards,

                        Daniel

                        • 9. Re: Calling Smooks from Java to do xslt transfomation
                          Tom Fennelly Master

                          Andreas, can you create a JIRA and attach you code to it and one of us will have a look at it. It'll be end of next week before I get to it though. Sorry.

                          • 10. Re: Calling Smooks from Java to do xslt transfomation
                            Tom Fennelly Master

                            Andreas... Daniel was on to me there and he says he'd be glad to take a look at that if you create a JIRA (http://jira.jboss.com/jira/browse/JBESB) and attach your code to it.

                            • 11. Re: Calling Smooks from Java to do xslt transfomation
                              Andreas Watler Newbie

                              Hi,


                              I did try the $document selector, in fact I started with it, but it made no diffrence.

                              I will create a JIRA. Should I label it as bug, feature request or something else?


                              Thanks for all the help so far!

                              Andreas

                              • 12. Re: Calling Smooks from Java to do xslt transfomation
                                Daniel Bevenius Master

                                Hi Andreas,

                                I think you can register the JIRA as a bug and assign it to me (or leave it unassigned) and I'll take it.

                                I'll post back to the forum as soon as I find out what is going on here.

                                Thanks,

                                Daniel

                                • 13. Re: Calling Smooks from Java to do xslt transfomation
                                  Andreas Watler Newbie

                                  Hi,

                                  will do. I'll wrap up a quick example showing the whole thing and create a JIRA.

                                  Thanks alot!

                                  Andreas

                                  • 14. Re: Calling Smooks from Java to do xslt transfomation
                                    Andreas Watler Newbie

                                    Hi,

                                    while setting up the example, I found out that the transformation works fine with dom.Documents made directly from the message, but not the ones I created in my code. So it seemed I'm creating these Documents wrong in some way. As it turned out I used a namespace-uri when creating the Documents, while the original Documents had no NS (perhaps I wanted to enhance the Documents, don't really remember). I thought I read somewhere that Smooks applies the transformation to all namespaces if none is specified, but I might remember that wrong. It's also strange that this only seems to affect the document-element, but I'm not that firm when it comes to namespaces, so I might miss the problem here.

                                    Since I never saw a difference after converting the created Documents back to String, I never figured that this might be the source of the problem. None the less, it seems to work now.
                                    So t seems there is no bug to worry about after all :-).

                                    I'm really very sorry I didn't saw that earlier, and made a fuss about nothing. I want to thank you again for all your help and patience.

                                    Regards
                                    Andreas