ANT script : xmlcatalog not reading local dtd - ant

I have XML file named TIBCOUniversalInstaller_TRA_5.10.0.silent as below.I want to replace values in XML file using "replace" target in ant script using xmltask task.
XML File is below:
<?xml version="1.0"?>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
<properties>
<comment>---Universal Installer Silent Installation Properties---</comment>
<!--accept the license agreement-->
<entry key="acceptLicense">true</entry>
<entry key="installationRoot">/opt/tibco</entry>
<entry key="environmentName">TRA</entry>
</properties>
At time of parsing XML file ,since my server can not reach java.sun.com , so i had downloaded properties.dtd on my local machine and using xmlcatalog task i am forcing ant script to read local copy of properties.dtd.Below is my ant script
<xmlcatalog id="dtd">
<dtd publicId="SYSTEM" location="/home/tibco/BW-AUTOMATION-
PROJECT/Environments/properties.dtd"/>
</xmlcatalog>
<xmltask source="${TRASoftwareFolder}/TIBCOUniversalInstaller_TRA_5.10.0.silent" dest="${TRASoftwareFolder}/TIBCOUniversalInstaller_TRA_5.10.0.silent">
<xmlcatalog refid="dtd">
</xmlcatalog>
<replace path="/:properties/:entry/:[#key='installationRoot']/text()"
withText="/home/tibco"/>
</xmltask>
But still at time of parsing XML contents , everytime it is going to http://java.sun.com/dtd/properties.dtd and i get "Connection Refused Error".
When i did debug i see below which i believe can be issue and it is always going to website instead of local dtd file.
DEBUG LOGS:
"No matching catalog entry found, parser will use: 'http://java.sun.com/dtd/properties.dtd'"
I believe it is because i gave "SYSTEM" as value in "publicId" attribute inside dtd element.
Can you please advise what should be correct value for "publicID" attribute for this given dtd so that it matches catalog at the time of parsing.
If there is another way of reading/replacing this XML file please advise.
Thanks

Related

Getting errors in Saxon-HE 9.9.1 when processing DITA: I/O error on DTD

Using Saxon 9.9.1.3J, I am getting an I/O error every time I try to transform a DITA file that has a DTD:
I/O error reported by XML parser processing file:/test.dita: /learningAssessment.dtd (No such file or directory)
This happens even if I force -dtd:off on the command line. Commenting out the DTD in the DITA file does allow it to process.
Interestingly, when I run the same DITA file in oXygen using Saxon-HE 9.8.0.12, it does process correctly. Any idea what might be causing this to behave differently?
Sample DITA file:
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE learningAssessment PUBLIC "-//OASIS//DTD DITA Learning Assessment//EN" "learningAssessment.dtd">
<learningAssessment id="id">
<title>Title</title>
<learningAssessmentbody>
<lcInteraction>
<lcSingleSelect id="lcSingleSelect_agy_fxz_ljb">
<lcQuestion>Question</lcQuestion>
<lcAnswerOptionGroup id="lcAnswerOptionGroup_bgy_fxz_ljb">
<lcAnswerOption>
<lcAnswerContent>A</lcAnswerContent>
</lcAnswerOption>
<lcAnswerOption>
<lcAnswerContent>B</lcAnswerContent>
<lcCorrectResponse/>
</lcAnswerOption>
</lcAnswerOptionGroup>
</lcSingleSelect>
</lcInteraction>
</learningAssessmentbody>
</learningAssessment>
And here's a shell of an XSL that demonstrates the error:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="3.0">
<xsl:output />
<xsl:template match="/">
<xsl:apply-templates/>
</xsl:template>
</xsl:stylesheet>
You can resolve the problem by the following steps:
Download DITA-OT and expand it any folder you like. In my case it is located at D:\DITA-OT\dita-ot-3.3.4.
Set CLASSPATH environment variable to contain saxon9he.jarand xml-resolver-1.2.jar in DITA-OT/lib.
Invoke Saxon by specifying class name net.sf.saxon.Transform and the catalog: paramter that specifies [DITA-OT]/catalog-dita.xml.
Here is execution example command window:
Hope this helps!
My guess is that you have somehow contrived to give the document a base URI of "file:/test.dita: ", including the final space. You haven't shown how you are running the transformation, so we can't tell where this base URI comes from.
The option -dtd:off is a little misleading. It doesn't switch off DTD processing, only DTD-based validation, which is just one aspect of DTD processing. An XSLT processor always needs to ask the XML parser to read the DTD in order to expand any entity references.
(Well, theoretically it could delay reading any external DTD until it finds the first entity reference; but sadly, I don't know of any XML parser that does that.)
I misunderstood how DTDs work. I assumed the public ones were loaded from an HTTP URL, but they need to be local files. Loading the catalog for DITA OT resolved the issue.
transform -s:test.dita -xsl:test.xsl -o:test.html -catalog:/org.oasis-open.dita.v1_2/plugins/org.oasis-open.dita.v1_2/catalog.xml
Where the catalog option points to this file on my local filesystem, which comes from DITA OT

Ant xmlproperty task fails due to validation error

I want to extract an application version from a DITA map file. The ditamap file is valid and looks like this:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE map PUBLIC "-//OASIS//DTD DITA Map//EN" "map.dtd">
<map id="user-manual">
<title><ph keyref="product"/> User Manual</title>
<topicmeta>
<prodinfo>
<prodname><keyword keyref="product"/></prodname>
<vrmlist>
<vrm version="4" release="3" modification="0"/>
</vrmlist>
</prodinfo>
</topicmeta>
<!--
[...]
-->
</map>
The information I want to get is in the <vrm> element.
"Easy peasy," I think to myself. So I use Ant's <xmlproperty> task to just load this XML file.
<project default="test">
<!-- notice #validate -->
<xmlproperty file="path/to/user-manual.ditamap" validate="false"/>
<target name="test">
<echo>${map.topicmeta.prodinfo.vrmlist.vrm(version)}</echo>
</target>
</project>
I don't want it to validate because Ant isn't going to find map.dtd.
Loading the file returns an error:
java.io.FileNotFoundException: /home/user/user-manual/map.dtd (No such file or directory)
If I remove the <!DOCTYPE> declaration or add a nested <xmlcatalog> with the path to the DTD, the file loads and I can use the properties from it.
I tested this with Ant 1.7.1 and 1.9.4. Is this a bug with Ant, or am I misunderstanding how Ant loads XML properties and the purpose of the validate attribute?
How can I make Ant obey my will?
I recommend to not use the <xmlproperty> for this. Please have a look at the docs:
For example, with semantic attribute processing enabled, this XML
property file:
<root>
<properties>
<foo location="bar"/>
<quux>${root.properties.foo}</quux>
</properties>
</root>
is roughly equivalent to the following fragments in a build.xml file:
<property name="root.properties.foo" location="bar"/>
<property name="root.properties.quux" value="${root.properties.foo}"/>
So the name of the properties you set is generated using their paths to the root element, so they rely on the structure of your DITA Map. But many elements in DITA may be set at different positions on your DITA Map. That means, if you move your metadata to another parent element, the property name changes and your build fails. This is probably not, what you want.
I'd recommend to grab those values via XSLT and than set the properties. That way, you could, for example, say, "give me the first occurance of that element with a simple //foo[1] XPath selector. Further on, you have the power of XSLT and XPath to slice values, format dates and so on before setting a property.
Update
You can use the oops consultancy Ant xmltask for that. It is very easy to set a property using <copy>:
<copy path="//critdates/created/#date"
property="document.date"
append="false"/>

What is source data for the report generated by junitreport ant task?

The JUnit official documentation states:
junitreport collects individual xml files generated by the JUnit task using the nested element.
Other part of the same page states:
<junitreport todir="./reports">
<fileset dir="./reports">
<include name="TEST-*.xml"/>
</fileset>
<report format="frames" todir="./report/html"/>
</junitreport>
would generate a TESTS-TestSuites.xml file in the directory reports
and generate the default framed report in the directory report/html.
Let's say, I have file TEST-all.xml generated by junit task in the "reports" directory. I want to use it as data source for junitreport task:
<fileset dir="./reports">
<include name="TEST-all*.xml"/>
</fileset>
I would expect html report based on my data will be generated.
I tried to do it. Empty TESTS-TestSuites.xml file was generated and as a result empty html file.
Two documentation statements I quoted above somehow contradict each other: the first one says it will use already generated files to create a report and the second one says it will generate new file. Can somebody explain how it works? How can I control what data source will be used to generate html report?
Thanks.
Try running ant with -debug. I suspect
<include name="TEST-all*.xml"/>
is not matching any files. Please try with
<fileset dir="./reports">
<include name="TEST-*.xml"/>
</fileset>
Well, I figured out what was the problem: file TEST-all.xml was generated in wrong format. It seems the correct format is:
<testsuite>
<testcase classname="..." name="..." ...>
<properties>
<property name="..."/>
</properties>
</testcase>
</testsuite>
It didn't solve the problem completely though. The html report is still not generated properly:
In spite the fact that file TESTS-TestSuits.xml is not empty now and contains all necessary tests' information, main page of HTML report (index.html) still shows 0 tests.
If I click the link in Tests column (0 in this case) it opens list of tests, but if I try to open individual test in this page, I get "File not found" error.
I suspect the problem is still with the format of TEST-all.xml. I was looking for the description of the format, but found only pieces of information here and there. Any ideas?

How to retrieve specific tag from an xml file in spring batch in java

This is what I need to implement. I need to create a batch which:
1. READ: will read mutiple xml files from a folder
2. PROCESS: extract the values of some tags from one of these xmls (as explained below)and save the extracted data in a DB.
3. WRITE: move the processed xml file as it is to another directory.
There are no repeating tags in the xml.
For example if this is my XML:
<?xml version="1.0" encoding="UTF-8"?>
<report>
<info>
<ssn>5214365214356</ssn>
<name>abc</name>
<age>12</age>
<gender>male</gender>
</info>
<address>
<street>abc</street>
<city>atrdtysaf</city>
<state>abcsvc</state>
<country>USA</country>
</address>
<healthinfo>
<smoking>no</smoking>
<drinking>no</drinking>
</healthinfo>
</report>
I want to extract the values of "ssn, gender and country tags only". Please note that the actual xml would be relatively huge. I am supposed to use StaxEvenItemReader provided by spring batch.

How to validate an xml file in ant?

this is my xml file
<contacts>
<contact>
<firstname>Edwin</firstname>
<lastname>Dankert</lastname>
</contact>
</contacts>
i want to validate it in ant,my target is
<target name="WellFormed">
<xmlvalidate file="contacts.xml"/>
<echo>WELL FORMED</echo>
</target>
C:\work\build\XML_VALIDATION>ant
Buildfile: C:\work\build\XML_VALIDATION\build.xml
<==========================ANT OUTPUT=============================>
WellFormed:
[xmlvalidate] contacts.xml:1:11: Document root element "contacts", must match DOCTYPE root "null".
[xmlvalidate] contacts.xml:1:11: Document is invalid: no grammar found.
BUILD FAILED
C:\work\build\XML_VALIDATION\build.xml:4: C:\work\build\XML_VALIDATION\contacts.xml is not a valid XML document.
i am not checking it against any xsd,can anyone help me in finding what changes i need to insert into the xml.
If you just want to make sure your XML is well-formed, then add the lenient attribute to your xmlvalidate task as follows.
<xmlvalidate file="contacts.xml" lenient="yes"/>
Your build file is not valid, you need to start with
<?xml version="1.0"?>
<project name="name">
<target name="target-name">
...
</target>
</project>
A google search for sample Ant build file will provide all you need to understand build files. This link will help too.

Resources