Smooks not picking up the files present outside the src folder.

61 views
Skip to first unread message

Akash Verma

unread,
Jun 25, 2024, 2:23:59 AM6/25/24
to Smooks Users

Hi Claude and Team Members,


I created a function in which I’m trying to convert the interchange XML to EDI. It was all working fine when the EDIFACT-MESSAGES.dfdl.xsd was present in the resources directory. However, when I moved this to the folder outside of the src folder, which we do when we want to save the files outside of the JAR, it’s not able to load the file; I’m getting, file not found error. I tried different ways to pass the file like  {relativePath} or {absolutePath} or file:/{absolutePath} or   file:///{absolutePath}, but none seems to work; I’m getting FileNotFound error. Also, for the paths like file:/{absolutePath} or   file:///{absolutePath}, I see it’s appending the ‘/‘ before the path which is definitely not a valid path but when I’m passing the absolutePath like this ‘/Users/Akash….’ It’s not appending the extra slash.
I need to make it pick the files present outside the src folder, which is inside the ExternalDirectory. Could you please help me here.



Error Messages - 

When passed the relative Path - 
`Caused by: org.smooks.api.SmooksConfigException: Required resource /ExternalDirectory/EDIFACT/EDIFACT-Messages.dfdl.xsd was not found.

Classpath is unknown. Hint: are you sure you have added the right EDIFACT schema pack to the Java classpath?`

When Passed the path like file:/{absolutePath} or   file:///{absolutePath}  -> here it’s adding extra ‘/‘
 - `Caused by: org.smooks.api.SmooksConfigException: Required resource /file:/Users/akashverma/MainProject/json_to_edifact/ExternalDirectory/EDIFACT/EDIFACT-Messages.dfdl.xsd as not found.

Classpath is unknown. Hint: are you sure you have added the right EDIFACT schema pack to the Java classpath?`

When passing the absolute path like this - /Users/akashverma/MainProject/json_to_edifact/ExternalDirectory/EDIFACT/EDIFACT-Messages.dfdl.xsd  getting the same error but it’s not appending the ‘/‘
 - `Caused by: org.smooks.api.SmooksConfigException: Required resource /Users/akashverma/MainProject/json_to_edifact/ExternalDirectory/EDIFACT/EDIFACT-Messages.dfdl.xsd as not found.

Classpath is unknown. Hint: are you sure you have added the right EDIFACT schema pack to the Java classpath?`
 
Code - 

 protected static String convertInterchangeToEdifact(Object interchange, String version, List<String> messageType) {


        ResourceConfig edifactUnparser = new DefaultResourceConfig("*", new Properties());

        edifactUnparser.setResource("");

        edifactUnparser.setParameter("schemaUri", "file:/Users/akashverma/MainProject/json_to_edifact/ExternalDirectory/EDIFACT/EDIFACT-Messages.dfdl.xsd"); // —>didn’t work

       // edifactUnparser.setParameter("schemaUri","ExternalDirectory/EDIFACT/EDIFACT-Messages.dfdl.xsd");

        edifactUnparser.setResourceType("dfdl");  —> didn’t work

        edifactUnparser.setParameter("dataProcessorFactory", "org.smooks.cartridges.edifact.EdifactDataProcessorFactory");

 

        for (String mesaage:messageType) {

            edifactUnparser.setParameter("messageType", mesaage);

        }

 

        // Build pipeline resource config programmatically

        ResourceConfig pipeline = new DefaultResourceConfig("/Interchange", new Properties());

        pipeline.setResource("org.smooks.engine.resource.visitor.smooks.NestedSmooksVisitor");

        pipeline.setParameter("action", "REPLACE");

        pipeline.setParameter("smooksResourceList", "<smooks-resource-list xmlns=\"https://www.smooks.org/xsd/smooks-2.0.xsd\">" + edifactUnparser.toXml() + "</smooks-resource-list>");

 

        String encoding = "UTF-8";

        Smooks smooks = new Smooks(new DefaultApplicationContextBuilder().setClassLoader(JsonToEDITemplateBuilderServiceImpl.class.getClassLoader()).build());

        StringResult stringResult = new StringResult();

        try {

            smooks.addConfiguration(pipeline);

            ExecutionContext executionContext = smooks.createExecutionContext();

            executionContext.setContentEncoding(encoding);

            System.out.println(interchange.toString());

            smooks.filterSource(executionContext, new ByteSource(interchange.toString().getBytes(encoding)), stringResult);

        }catch (Exception e){

            e.printStackTrace();

        }

 

        return stringResult.getResult();

    }

 

And Below is the structure of my package 

MainProject

-> JSON_TO_EDIFACT

-> SRC->MAIN->JAVA->com.json-to-edifact>Template->IMPL-> JsonToEDITemplateBuilderServiceImpl.JAVA

-> ExternalDirectory->EDIFACT->EDIFACT-MESSAGES.dfdl.xsd

 

Also, I found it breaks inside the below function

protected String readVersion(Parameter<String> schemaUriParameter) throws XPathExpressionException, ParserConfigurationException, IOException, SAXException 
{ DocumentBuilder documentBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder(); 
 Document document = documentBuilder.parse(Misc.getRequiredResource((String)schemaUriParameter.getValue()).toString()); // -> breaks here
 XPathFactory factory = XPathFactory.newInstance();
 XPath xpath = factory.newXPath(); 
return (String)xpath.compile("/schema/annotation/appinfo[@source='http://www.ibm.com/dfdl/edi/un/edifact']/text()").evaluate(document, XPathConstants.STRING); }`



Thanks,
Akash.

 

Reshma Kumari

unread,
Jun 27, 2024, 3:00:50 AM6/27/24
to Smooks Users
Hi Claude,
I believe the addition of extra "/" in front of the schemaURI is happening because of following change that got introduced in v2.0.0-RC3. Is there some work around that can be implemented?
RCV2andRcv3.png

Claude

unread,
Jul 2, 2024, 8:02:39 AM7/2/24
to Smooks Users
Hello Akash,

I'd say this is a bug. I'll try to get it fixed by the next release, unless you'd like to provide a patch. Could you create a bug report? Wrt a workaround, you could try extending EdifactDataProcessorFactory so that it doesn't prepend a slash. This should be easy to do since you're programmatically creating the EDIFACT unparser.

Claude
Reply all
Reply to author
Forward
0 new messages