Calling java method through schematron throwing error

Having trouble installing Oxygen? Got a bug to report? Post it all here.
roopesh79
Posts: 26
Joined: Fri Jul 29, 2022 9:55 am

Calling java method through schematron throwing error

Post by roopesh79 »

Hi Team,

Could you please share a schematron pattern that call a simple java method by adding a jar file in class path?

Regards
Roopesh
johnwood
Posts: 5
Joined: Sat Apr 29, 2023 8:05 am

Re: Calling java method through schematron throwing error

Post by johnwood »

Here's an example Schematron pattern that calls a Java method by adding a JAR file to the classpath:

Code: Select all

<?xml version="1.0" encoding="UTF-8"?>
<iso:schema xmlns:iso="http://purl.oclc.org/dsdl/schematron" xmlns:jav="http://www.niso.org/schemas/jav" queryBinding="xslt2">
   <iso:pattern name="javaMethod">
      <iso:rule context="//*[local-name()='document']/*[local-name()='section']/*[local-name()='p']">
         <iso:report test="@class = 'java'">
            <iso:text>Calling Java method: </iso:text>
            <jav:java>
                <jav:classpath>
                    <jav:jar>path/to/your/jar/file.jar</jav:jar>
                </jav:classpath>
                <jav:call>
                    <jav:method>com.example.yourclass.yourMethod()</jav:method>
                </jav:call>
            </jav:java>
         </iso:report>
      </iso:rule>
   </iso:pattern>
</iso:schema>
the jav:java element specifies the JAR file to be added to the classpath, and the jav:callwordle unlimited element specifies the Java method to be called. You will need to replace path/to/your/jar/file.jar with the actual path to your JAR file, and com.example.yourclass.yourMethod() with the fully qualified name of the Java class and method you want to call.
Radu
Posts: 9059
Joined: Fri Jul 09, 2004 5:18 pm

Re: Calling java method through schematron throwing error

Post by Radu »

Hi Roopesh,
Here's an example
https://www.oxygenxml.com/InstData/Edit ... nUtil.html
for calling from a class "ro.sync.ecss.dita.extensions.DITAXSLTExtensionFunctionUtil" a static function named "getKeyRefInfo" with two parameters of type string.
Regards,
Radu
Radu Coravu
<oXygen/> XML Editor
http://www.oxygenxml.com
chrispitude
Posts: 907
Joined: Thu May 02, 2019 2:32 pm

Re: Calling java method through schematron throwing error

Post by chrispitude »

Hi Roopesh,

If you want a small testcase that demonstrates the API that Radu mentioned, I can provide it.
roopesh79
Posts: 26
Joined: Fri Jul 29, 2022 9:55 am

Re: Calling java method through schematron throwing error

Post by roopesh79 »

Hi,
Thank you for the detailed information. It is really helpful. Please share one API test case sample also.

Thanks
Roopesh N
chrispitude
Posts: 907
Joined: Thu May 02, 2019 2:32 pm

Re: Calling java method through schematron throwing error

Post by chrispitude »

Hi Roopesh,

Here is a testcase that calls the new keyref APIs introduced in Oxygen v25.1:

api_testcase_for_roopesh.zip
(23.01 KiB) Downloaded 153 times

To try them out,

1. Open the OPENME1.xpr file in Oxygen v25.1.
2. Open the OPENME2.ditamap file in the DITA Maps Manager.
3. Open a topic in the Book A or Book B submap.
4. Hover your pointer over an <xref> element to see the Schematron/API messages as tooltips.
5. Run Validate and Check for Completeness to see the Schematron/API messages.
roopesh79
Posts: 26
Joined: Fri Jul 29, 2022 9:55 am

Re: Calling java method through schematron throwing error

Post by roopesh79 »

Hi Tea,

I have tried the first approach and not getting the actual java response.
<iso:pattern id="javaMethod">
<iso:rule context="ol">
<iso:report test="@outputclass = 'java'">
<jav:java>
<jav:classpath>
<jav:jar>C:\Progfiles\XDocsOxygenAuthor\frameworks\validations\Oxy.jar</jav:jar>
</jav:classpath>
<jav:call>
<jav:method>net.example.Alert_Oxygen.test()</jav:method>
</jav:call>
</jav:java>
</iso:report>
</iso:rule>
</iso:pattern>
I am getting the below message(screenshot) in the oxygen with all the schematron syntax. Not getting the actual java message.
Thanks
Roopesh
Attachments
image.png
image.png (12.85 KiB) Viewed 898 times
Radu
Posts: 9059
Joined: Fri Jul 09, 2004 5:18 pm

Re: Calling java method through schematron throwing error

Post by Radu »

Hi,
The Oxygen forum is a public place, the person who recommended this solution you tried is not part of the Oxygen team and I think his answer is wrong.
Are you applying the Schematron validation on an XML document opened in Oxygen?
Usually this would mean that you have a validation scenario stage which has been configured to apply the Schematron schema on the XML document. And a validation scenario stage can specify a reference to extra JAR libraries to be used for validation:
https://www.oxygenxml.com/doc/versions/ ... xk_bgk_54b
Once you specify in the validation scenario that you want the JAR library (or libraries) to be used with Schematron validation, you can then use the Java classes directly in the Schematron schema in the way in which Chris or I mentioned:

Code: Select all

<sch:schema xmlns:sch="http://purl.oclc.org/dsdl/schematron" queryBinding="xslt2">
    <sch:ns uri="java:package.name.JavaClassName" prefix="myPrefix"/>
    <sch:pattern>
        <sch:rule context="..">
            <sch:report test="myPrefix:staticMethodName(params)'">....
            </sch:report>
        </sch:rule>
    </sch:pattern>
</sch:schema>
But usually in such cases it's best to start with an usecase and more details, for example what Oxygen version you have, what end result do you want to achieve? What does your custom Java extension do? Is it already built? Are you validating the XML documents using Schematron inside Oxygen?

Regards,
Radu
Radu Coravu
<oXygen/> XML Editor
http://www.oxygenxml.com
roopesh79
Posts: 26
Joined: Fri Jul 29, 2022 9:55 am

Re: Calling java method through schematron throwing error

Post by roopesh79 »

Hi Radu,
I am using Oxygen 21.1(planning to upgrade to 23.1). I have configured the custom schematron in the document type association. Now I want to add a jar file in class path and validate the dita elements against some schematron rules which call the java method present in the jar files. My final goal is to call an external API using jar and validate dita files.
Currently I am trying to run a "hello world" kind of java progam with in schematron, but getting a different error (Cannot find a 0-argument function named {java:net.example.Alert_Oxygen}test...). Not sure the jar file is integrated correctly. I have added jar file using the "schema dialogue box" also. Please guide me on this.
image.png
image.png (49.09 KiB) Viewed 893 times
Thanks
Roopesh
Radu
Posts: 9059
Joined: Fri Jul 09, 2004 5:18 pm

Re: Calling java method through schematron throwing error

Post by Radu »

Hi Roopesh,

Here are some screenshots showing that when you configure a validation stage in the validation scenario you have an "Extensions" button which you can use to add references to JAR libraries:
Screenshot 2023-05-05 at 12.06.23.png
Screenshot 2023-05-05 at 12.06.23.png (190.29 KiB) Viewed 886 times
Screenshot 2023-05-05 at 12.07.07.png
Screenshot 2023-05-05 at 12.07.07.png (168.05 KiB) Viewed 886 times
Screenshot 2023-05-05 at 12.07.19.png
Screenshot 2023-05-05 at 12.07.19.png (113.12 KiB) Viewed 886 times
Screenshot 2023-05-05 at 12.07.37.png
Screenshot 2023-05-05 at 12.07.37.png (58.37 KiB) Viewed 886 times
Regards,
Radu
Radu Coravu
<oXygen/> XML Editor
http://www.oxygenxml.com
Post Reply