Saturday, March 10, 2012

Editing XML using apache ant target - dtd validation

To acomplish this specific task and automate the xml injections there is a tool called XmlTask which can be download here.

For more information about how to write a ant target or use xmltask you can refer this guide.

This article is not about how to use xmltask actually it is a situation i have to face whilie using this. My machine connects to a proxy server when it connect to internet or other network and when inserting or updating ant task "xmltask" use the dtd declared in <!DOCTYPE >. Actually it tries to download it if it is not in a local machine. So with the proxy i got java.net.ConnectException .


 [xmltask] It looks like you've got a network error. The probable cause  
  [xmltask] is that you're trying to resolve a DTD on the internet although  
  [xmltask] you don't know it! Check your XML for DTDs external to your network  
  [xmltask] and read the Ant documentation for <xmlcatalog>. XMLTask will support  
  [xmltask] usage of <xmlcatalog>. See the following:  
  [xmltask] http://ant.apache.org/manual/CoreTypes/xmlcatalog.html  
  [xmltask] http://www.oopsconsultancy.com/software/xmltask  
  [xmltask] If this isn't the problem, then please report this error to the support  
  [xmltask] mailing list. Thanks!  
  [xmltask] Connection refused  
  [xmltask]   at com.oopsconsultancy.xmltask.ant.XmlTask.documentFromStream(XmlTask.java:372)  
  [xmltask]   at com.oopsconsultancy.xmltask.ant.XmlTask.documentFromFile(XmlTask.java:400)  
  [xmltask]   at com.oopsconsultancy.xmltask.ant.XmlTask.access$000(XmlTask.java:27)  
  [xmltask]   at com.oopsconsultancy.xmltask.ant.XmlTask$InputFile.getDocument(XmlTask.java:216)  
  [xmltask]   at com.oopsconsultancy.xmltask.ant.XmlTask.execute(XmlTask.java:651)  
  [xmltask]   at org.apache.tools.ant.UnknownElement.execute(UnknownElement.java:288)  
  [xmltask]   at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)  
  [xmltask]   at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)  
  [xmltask]   at java.lang.reflect.Method.invoke(Method.java:597)  
  [xmltask]   at org.apache.tools.ant.dispatch.DispatchUtils.execute(DispatchUtils.java:106)  
  [xmltask]   at org.apache.tools.ant.Task.perform(Task.java:348)  
  [xmltask]   at org.apache.tools.ant.Target.execute(Target.java:357)  
  [xmltask]   at org.apache.tools.ant.Target.performTasks(Target.java:385)  
  [xmltask]   at org.apache.tools.ant.Project.executeSortedTargets(Project.java:1337)  
  [xmltask]   at org.apache.tools.ant.Project.executeTarget(Project.java:1306)  
  [xmltask]   at org.apache.tools.ant.helper.DefaultExecutor.executeTargets(DefaultExecutor.java:41)  
  [xmltask]   at org.apache.tools.ant.Project.executeTargets(Project.java:1189)  
  [xmltask]   at org.apache.tools.ant.Main.runBuild(Main.java:758)  
  [xmltask]   at org.apache.tools.ant.Main.startAnt(Main.java:217)  
  [xmltask]   at org.apache.tools.ant.launch.Launcher.run(Launcher.java:257)  
  [xmltask]   at org.apache.tools.ant.launch.Launcher.main(Launcher.java:104)  
  [xmltask] Caused by: java.net.ConnectException: Connection refused  
  [xmltask]   at java.net.PlainSocketImpl.socketConnect(Native Method)  
  [xmltask]   at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:333)  
  [xmltask]   at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:195)  
  [xmltask]   at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:182)  
  [xmltask]   at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:366)  
  [xmltask]   at java.net.Socket.connect(Socket.java:529)  
  [xmltask]   at java.net.Socket.connect(Socket.java:478)  
  [xmltask]   at sun.net.NetworkClient.doConnect(NetworkClient.java:163)  
  [xmltask]   at sun.net.www.http.HttpClient.openServer(HttpClient.java:394)  
  [xmltask]   at sun.net.www.http.HttpClient.openServer(HttpClient.java:529)  
  [xmltask]   at sun.net.www.http.HttpClient.<init>(HttpClient.java:233)  
  [xmltask]   at sun.net.www.http.HttpClient.New(HttpClient.java:306)  
  [xmltask]   at sun.net.www.http.HttpClient.New(HttpClient.java:323)  
  [xmltask]   at sun.net.www.protocol.http.HttpURLConnection.getNewHttpClient(HttpURLConnection.java:860)  
  [xmltask]   at sun.net.www.protocol.http.HttpURLConnection.plainConnect(HttpURLConnection.java:801)  
  [xmltask]   at sun.net.www.protocol.http.HttpURLConnection.connect(HttpURLConnection.java:726)  
  [xmltask]   at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1049)  
  [xmltask]   at org.apache.xerces.impl.XMLEntityManager.setupCurrentEntity(Unknown Source)  
  [xmltask]   at org.apache.xerces.impl.XMLEntityManager.startEntity(Unknown Source)  
  [xmltask]   at org.apache.xerces.impl.XMLEntityManager.startDTDEntity(Unknown Source)  
  [xmltask]   at org.apache.xerces.impl.XMLDTDScannerImpl.setInputSource(Unknown Source)  
  [xmltask]   at org.apache.xerces.impl.XMLDocumentScannerImpl$DTDDispatcher.dispatch(Unknown Source)  
  [xmltask]   at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unknown Source)  
  [xmltask]   at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)  
  [xmltask]   at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)  
  [xmltask]   at org.apache.xerces.parsers.XMLParser.parse(Unknown Source)  
  [xmltask]   at org.apache.xerces.parsers.DOMParser.parse(Unknown Source)  
  [xmltask]   at org.apache.xerces.jaxp.DocumentBuilderImpl.parse(Unknown Source)  
  [xmltask]   at com.oopsconsultancy.xmltask.ant.XmlTask.documentFromStream(XmlTask.java:356)  
  [xmltask]   ... 20 more  
  [xmltask] --- Nested Exception ---  
  [xmltask] java.net.ConnectException: Connection refused  
  [xmltask]   at java.net.PlainSocketImpl.socketConnect(Native Method)  
  [xmltask]   at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:333)  
  [xmltask]   at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:195)  
  [xmltask]   at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:182)  
  [xmltask]   at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:366)  
  [xmltask]   at java.net.Socket.connect(Socket.java:529)  
  [xmltask]   at java.net.Socket.connect(Socket.java:478)  
  [xmltask]   at sun.net.NetworkClient.doConnect(NetworkClient.java:163)  
  [xmltask]   at sun.net.www.http.HttpClient.openServer(HttpClient.java:394)  
  [xmltask]   at sun.net.www.http.HttpClient.openServer(HttpClient.java:529)  
  [xmltask]   at sun.net.www.http.HttpClient.<init>(HttpClient.java:233)  
  [xmltask]   at sun.net.www.http.HttpClient.New(HttpClient.java:306)  
  [xmltask]   at sun.net.www.http.HttpClient.New(HttpClient.java:323)  
  [xmltask]   at sun.net.www.protocol.http.HttpURLConnection.getNewHttpClient(HttpURLConnection.java:860)  
  [xmltask]   at sun.net.www.protocol.http.HttpURLConnection.plainConnect(HttpURLConnection.java:801)  
  [xmltask]   at sun.net.www.protocol.http.HttpURLConnection.connect(HttpURLConnection.java:726)  
  [xmltask]   at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1049)  
  [xmltask]   at org.apache.xerces.impl.XMLEntityManager.setupCurrentEntity(Unknown Source)  
  [xmltask]   at org.apache.xerces.impl.XMLEntityManager.startEntity(Unknown Source)  
  [xmltask]   at org.apache.xerces.impl.XMLEntityManager.startDTDEntity(Unknown Source)  
  [xmltask]   at org.apache.xerces.impl.XMLDTDScannerImpl.setInputSource(Unknown Source)  
  [xmltask]   at org.apache.xerces.impl.XMLDocumentScannerImpl$DTDDispatcher.dispatch(Unknown Source)  
  [xmltask]   at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unknown Source)  
  [xmltask]   at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)  
  [xmltask]   at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)  
  [xmltask]   at org.apache.xerces.parsers.XMLParser.parse(Unknown Source)  
  [xmltask]   at org.apache.xerces.parsers.DOMParser.parse(Unknown Source)  
  [xmltask]   at org.apache.xerces.jaxp.DocumentBuilderImpl.parse(Unknown Source)  
  [xmltask]   at com.oopsconsultancy.xmltask.ant.XmlTask.documentFromStream(XmlTask.java:356)  
  [xmltask]   at com.oopsconsultancy.xmltask.ant.XmlTask.documentFromFile(XmlTask.java:400)  
  [xmltask]   at com.oopsconsultancy.xmltask.ant.XmlTask.access$000(XmlTask.java:27)  
  [xmltask]   at com.oopsconsultancy.xmltask.ant.XmlTask$InputFile.getDocument(XmlTask.java:216)  
  [xmltask]   at com.oopsconsultancy.xmltask.ant.XmlTask.execute(XmlTask.java:651)  
  [xmltask]   at org.apache.tools.ant.UnknownElement.execute(UnknownElement.java:288)  
  [xmltask]   at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)  
  [xmltask]   at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)  
  [xmltask]   at java.lang.reflect.Method.invoke(Method.java:597)  
  [xmltask]   at org.apache.tools.ant.dispatch.DispatchUtils.execute(DispatchUtils.java:106)  
  [xmltask]   at org.apache.tools.ant.Task.perform(Task.java:348)  
  [xmltask]   at org.apache.tools.ant.Target.execute(Target.java:357)  
  [xmltask]   at org.apache.tools.ant.Target.performTasks(Target.java:385)  
  [xmltask]   at org.apache.tools.ant.Project.executeSortedTargets(Project.java:1337)  
  [xmltask]   at org.apache.tools.ant.Project.executeTarget(Project.java:1306)  
  [xmltask]   at org.apache.tools.ant.helper.DefaultExecutor.executeTargets(DefaultExecutor.java:41)  
  [xmltask]   at org.apache.tools.ant.Project.executeTargets(Project.java:1189)  
  [xmltask]   at org.apache.tools.ant.Main.runBuild(Main.java:758)  
  [xmltask]   at org.apache.tools.ant.Main.startAnt(Main.java:217)  
  [xmltask]   at org.apache.tools.ant.launch.Launcher.run(Launcher.java:257)  
  [xmltask]   at org.apache.tools.ant.launch.Launcher.main(Launcher.java:104)  

what i did is as the tutorial dtd section suggested i have define <xmlcatalog> tag and map the local dtd source file to the public id like below example.


 <xmlcatalog id="dtd">  
   <dtd   
    publicId="-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"   
    location="./servlet-2.3.dtd"/>   
 </xmlcatalog>  


and then it gives another error when the edited xml file is parsing by my another program. The error is due to <!DOCTYPE> declaration is missing.
What happen is when we use xmlcatalog to define the local path , and when we insert a xml code to the document and write it back to a file , it elemenates the <!DOCTYPE > declaration in the newly writing file. If we overide the exisiting file we loose it from the only file we have. So to overcome this problem what i did was insert public id and system uril inside the <xmltask> tag itself like below.

 <xmltask source="etc/hibernate.cfg.xml" dest="etc/hibernate.cfg.xml" public="-//Hibernate/Hibernate Configuration DTD 3.0//EN"   
          system="http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">  
                <xmlcatalog id="dtd">  

No comments:

Post a Comment