Not able to select XML root node having an attribute using XmlTask - ant

I want to insert an element in the XML.
This is an XML file - named web.xml
<web-app>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
</web-app>
This is the ANT task I am using to insert an element in the web-app node.
<taskdef name="xmltask" classname="com.oopsconsultancy.xmltask.ant.XmlTask" classpath="C:/xmltask.jar"/>
<xmltask source="C:\web.xml" dest="C:\web.xml>
<insert path="/web-app">
<![CDATA[
<hello_world id="3">hello world</hello_world>
]]>
</insert>
</xmltask>
Ant task run inserts the hello_world node in the web-app.
The insert (actually root node selection) fails when the root has an attribute.
So the xmltask insert doesn't work when the XML is -
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee">
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
</web-app>
I tried to use insert like this, but no luck -
<insert path="/web-app/[#xmlns='http://xmlns.jcp.org/xml/ns/javaee']">
What is the way to select the root node? Why this is not working, an explanation would help.

This is most likely a problem for XmlTask/xpath when the Xml uses a declared namespace (xmlns= is not just an attribute).
See this other answer: https://stackoverflow.com/a/35778167/366749
I suggest you try:
<taskdef name="xmltask" classname="com.oopsconsultancy.xmltask.ant.XmlTask" classpath="C:/xmltask.jar"/>
<xmltask source="C:\web.xml" dest="C:\web.xml>
<insert path="*[local-name()='web-app']">
<![CDATA[
<hello_world id="3">hello world</hello_world>
]]>
</insert>
</xmltask>

Related

I am using struts2 application but its asking the spring configuration

I am getting the following error when i am trying to run the manning examples separately out of the sand box.
Error as follows:
You might need to add the following to web.xml:
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
Jun 17, 2013 7:17:39 AM org.apache.catalina.core.StandardContext filterStart
SEVERE: Exception starting filter struts2
java.lang.NullPointerException
at com.opensymphony.xwork2.spring.SpringObjectFactory.getClassInstance(SpringObjectFactory.java:188)
at com.opensymphony.xwork2.config.providers.XmlConfigurationProvider.verifyResultType(XmlConfigurationProvider.java:479)
at com.opensymphony.xwork2.config.providers.XmlConfigurationProvider.addResultTypes(XmlConfigurationProvider.java:450)
at com.opensymphony.xwork2.config.providers.XmlConfigurationProvider.addPackage(XmlConfigurationProvider.java:407)
at com.opensymphony.xwork2.config.providers.XmlConfigurationProvider.loadPackages(XmlConfigurationProvider.java:239)
at org.apache.struts2.config.StrutsXmlConfigurationProvider.loadPackages(StrutsXmlConfigurationProvider.java:111)
at com.opensymphony.xwork2.config.impl.DefaultConfiguration.reload(DefaultConfiguration.java:152)
at com.opensymphony.xwork2.config.ConfigurationManager.getConfiguration(ConfigurationManager.java:52)
at org.apache.struts2.dispatcher.Dispatcher.init_PreloadConfiguration(Dispatcher.java:395)
at org.apache.struts2.dispatcher.Dispatcher.init(Dispatcher.java:452)
at org.apache.struts2.dispatcher.FilterDispatcher.init(FilterDispatcher.java:201)
at org.apache.catalina.core.ApplicationFilterConfig.getFilter(ApplicationFilterConfig.java:295)
at org.apache.catalina.core.ApplicationFilterConfig.setFilterDef(ApplicationFilterConfig.java:422)
at org.apache.catalina.core.ApplicationFilterConfig.<init>(ApplicationFilterConfig.java:115)
at org.apache.catalina.core.StandardContext.filterStart(StandardContext.java:4072)
at org.apache.catalina.core.StandardContext.start(StandardContext.java:4726)
at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1057)
at org.apache.catalina.core.StandardHost.start(StandardHost.java:840)
at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1057)
at org.apache.catalina.core.StandardEngine.start(StandardEngine.java:463)
at org.apache.catalina.core.StandardService.start(StandardService.java:525)
at org.apache.catalina.core.StandardServer.start(StandardServer.java:754)
at org.apache.catalina.startup.Catalina.start(Catalina.java:595)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:289)
at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:414)
web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
<init-param>
<param-name>actionPackages</param-name>
<param-value>manning</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<servlet>
<servlet-name>anotherServlet</servlet-name>
<servlet-class>manning.servlet.AnotherServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>anotherServlet</servlet-name>
<url-pattern>/anotherServlet</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
</web-app>
struts.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<!--
For the purposes of the book, we will leave devMode set to true. This
allows several things to occur including provision of debugging level information
on error pages as well as reloading of various resources with each request.
-->
<constant name="struts.devMode" value="true" />
<!--
You can define packages directly in this file, but its probably best
to modularize the configuration files with separate xml files for each
package. Ulitmately its all the same because the include element, seen
below, pulls the included configuration document directly in to the
primary document. The only thing to consider is that a referenced
element must have been declared above the referring element.
-->
<!--
This is the menu action that will allow the reader to see the different
samples actions from the different chapters in a menu format. We declare
it here, in the root namespace, because its not really related to any of the
specific examples or chapters, its just a kind of utility for the whole
application.
-->
<package name="default" namespace="/" extends="struts-default">
<action name="Menu">
<result>/menu/Menu.jsp</result>
</action>
</package>
<include file="manning/chapterTwo/chapterTwo.xml"/>
<include file="manning/chapterThree/chapterThree.xml"/>
<include file="manning/chapterThree/objectBacked/chapterThree.xml"/>
<include file="manning/chapterThree/modelDriven/chapterThree.xml"/>
<include file="manning/chapterFour/chapterFour.xml"/>
<include file="manning/chapterFive/chapterFive.xml"/>
<include file="manning/chapterSix/chapterSix.xml"/>
<include file="manning/chapterSeven/chapterSeven.xml"/>
<include file="manning/chapterEight/chapterEight.xml"/>
<include file="manning/chapterNine/chapterNine.xml"/>
<include file="manning/chapterTen/chapterTen.xml"/>
<include file="manning/chapterEleven/chapterEleven.xml"/>
</struts>
Why i am getting the error. I have removed some configuration regarding spring in web.xml
Change your filter to below
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
And remove struts2-spring-plugin-*.jar from classpath if you have this jar in your classpath and dont want to use Spring integration with Struts2
Please let me know if you still face issue

F# NLog config file

I'm trying to use NLog in an F# console application, I've managed to get it working using a configuration section in App.config however I can't get it working using a stand-alone NLog.config file. My NLog.config file is in the app route, just under App.config and the contents are:
<?xml version="1.0" encoding="utf-8" ?>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" throwExceptions="true">
<targets>
<target name="stdFile" xsi:type="File" fileName="c:/temp/compliant.log"/>
<target name="display" xsi:type="OutputDebugString"/>
</targets>
<rules>
<logger name="compliant.mail.*" minlevel="Debug" writeTo="stdFile,display" />
</rules>
</nlog>
What am I doing wrong?
Also, intellisense isn't working for the xml even though I have included the xsd. :(
In your project, in the Properties for NLog.config, do you have NLog.config marked as "Copy Always"?

JarJar with Ant - how to use a Rule file

I would like to know how to run JarJar with Ant, passing the rules in with an external Rules file.
1) I know I can pass the rules in one by one as below:
<jarjar destfile="B.jar" >
<zipfileset src="A.jar" />
<rule pattern="com.a.**" result="test.b.#1" />
</jarjar>
2) I know I can pass the rules in a file if I run it from the command line:
java -jar jarjar.jar process <rulesFile> <inJar> <outJar>
3) I can use the above command line in an Ant <exec> task. (best workaround)
4) I found some reference to using a <rulesFile> tag in Maven.
The above options are not ideal for what I would like to do.
I want to run JarJar from an Ant task, passing in a rules file.
I have been unable to get any information about this, from any forum, or by mailing the developers of JarJar, so I have decided to answer this question with a workaround that I am using:
Use the DOCTYPE & ENTITY xml entities (as per suggestion on Ant website)
As an example, the below build.xml file includes the contents of another test.txt file inline. I import the text file using the tag include_this (this is my name - you can use any name here):
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE project [
<!ENTITY include_this SYSTEM "test.txt">
]>
<project name="Test" default="build" >
<target name="build" >
&include_this;
</target>
</project>
In this simple example, the contents of the test.txt file is:
<echo>This is a test.</echo>
So I've been using this workaround to replace the rules in the jarjar call. Putting the rules in the test.txt file:
<rule pattern="com.**" result="${project.output}.com.#1" />
<rule pattern="org.**" result="${project.output}.org.#1" />
My jarjar call then becomes:
<!-- jarjar uses the same syntax as the jar task -->
<jarjar destfile="${jarjar.output.dir}/${project.output}.jar" >
<!-- source files -->
<zipfileset src="${jar.output.dir}/${project.output}.jar" />
<!-- refactoring rules -->
&include_this;
</jarjar>

Cant load any page with struts 2 . Issue with web.xml

I am trying to run this example using struts 2. I inserted the libraries in the Lib folder of Web-Inf of a dynamic web project in eclipse.
My Web.xml looks like this now:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
<filter>
<filter-name>struts2</filter-name>
<filter-class>
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
and i created the struts.xml and placed it inside the source folder , the content of which is
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<!-- Configuration for the default package. -->
<package name="default" extends="struts-default">
<action name="UserAction" class="struts.UserAction">
<result name="input">/index.jsp</result>
<result name="success">/result.jsp</result>
</action>
</package>
</struts>
So now when i run any jsp i get the following error in eclipse console
Unable to load configuration. - bean - jar:file:/D:/Users/Don/workspace/.metadata/.plugins/org.eclipse.wst.server.core/tmp1/wtpwebapps/Mscs674-Struts2/WEB-INF/lib/struts2-gxp-plugin-2.3.4.jar!/struts-plugin.xml:8:162
and this on the webpage
HTTP Status 404 -
type Status report
message
description The requested resource () is not available.
Any suggestions ?
The best way i found to create struts 2 application is to use the struts-blank.jar file. It has all the required configurations done for you, and you can remove some example files. It will have all the jars and xml files created for you.
If you still have problems, try doing this now...
first remove all jar (adding all lib is not solution)
Only add necessary libraries
so problem is you are having too many lib
Seems like some jars are missing .. !!
The common dependecies are ::
commons-fileupload-1.2.1.jar
- commons-io-1.3.2.jar
- commons-logging-1.0.4.jar
- commons-logging-api-1.1.jar
- freemarker-2.3.15,jar
- log4j-1.2.14.jar
- ognl-2.7.3.jar
- xwork-core-2.1.6.jar
Check the version number and add jars to classpath.
You do One thing..add javassist-3.11.0.GA.jar file.it will remove your error like
Dispatcher initialization failed java.lang.RuntimeException:
java.lang.reflect.InvocationTargetException ..
I hope this will help you..

How do I specify the files to include in a WAR file?

I need to create a WAR file that just contains static content files (gifs, jpgs, html, js, etc). I have a directory structure that contains all the files and need to create the WAR file via an ANT (1.5.1) build task. Right now I just have this in the ANT task:
<war destfile="${output.file}" webxml="WEB-INF/web.xml" basedir="${basedir}">
<fileset dir="${basedir}/sales" />
</war>
The files I want to include are in C:/basedir/sales and its subdirectories. When I try to run this task I get "A zip file cannot include itself". So clearly putting that fileset in there is not the right way to do it. I am unclear as to what I need to put in the task and in the web.xml file to specify what files to include within the archive.
I think the basedir="${basedir}" is causing you the problems. Also, I think the way you have it written will require that web.xml exist inside WEB-INF dir relative to where you run ant from.
So, try creating /WEB-INF/web.xml as follows:
<?xml version="1.0" encoding="utf-8" ?>
<web-app>
</web-app>
Then try updating /build.xml as follows:
<?xml version="1.0" encoding="UTF-8"?>
<project name="yourproject" basedir="." default="war" xmlns:ivy="antlib:org.apache.ivy.ant">
<target name="war" description="--> build war file">
<war destfile="./mywar.war" webxml="WEB-INF/web.xml">
<fileset dir="C:/basedir/sales" />
</war>
</target>
</project>
Then you should be able to run "ant war" from command line and it should create "mywar.war" in your current directory.

Resources