1

I have a web.xml with around 10 servlets defined with some basic configuration such as below:

    <servlet>
    <servlet-name>dummyServlet</servlet-name>
    <servlet-class>com.abc.Servlet</servlet-class>
    <init-param>
        <param-name>target</param-name>
        <param-value>foo.com</param-value>
    </init-param>
    <init-param>
        <param-name>log</param-name>
        <param-value>true</param-value>
    </init-param>
</servlet>

<servlet-mapping>
    <servlet-name>dummyServlet</servlet-name>
    <url-pattern>/*</url-pattern>
</servlet-mapping>

        <servlet>
    <servlet-name>dummyServlet2</servlet-name>
    <servlet-class>com.abc.Servlet2</servlet-class>
    <init-param>
        <param-name>target</param-name>
        <param-value>foo.com</param-value>
    </init-param>
    <init-param>
        <param-name>log</param-name>
        <param-value>true</param-value>
    </init-param>
</servlet>

<servlet-mapping>
    <servlet-name>dummyServlet2</servlet-name>
    <url-pattern>/dummy</url-pattern>
</servlet-mapping>

        <servlet>
    <servlet-name>actualServlet</servlet-name>
    <servlet-class>com.abc.ActualServlet</servlet-class>
    <init-param>
        <param-name>target</param-name>
        <param-value>foo.com</param-value>
    </init-param>
    <init-param>
        <param-name>log</param-name>
        <param-value>true</param-value>
    </init-param>
</servlet>

<servlet-mapping>
    <servlet-name>dummyServlet</servlet-name>
    <url-pattern>/actual</url-pattern>
</servlet-mapping>

Now, from my ant target, based on some conditions, I need to remove both dummy-servlets along with their mappings but need to retain the actual-servlet and it's mapping. Can someone please suggest what would be the best/easy way to do that? Should I use ant <replace> or xmltask or some other feature? Replacing the entire web.xml is not an option.

  • 1
    IMO, XSLT transformation would be the best way to handle it. – Yitzhak Khabinsky May 31 '21 at 17:52
  • @YitzhakKhabinsky I am not familiar with XSLT. As I have multiple servlets and need to replace few of them based on can you please tell how it can be done? or pls point me to any resource/tuotrial. – Somtirtha Ghosh Jun 01 '21 at 04:43
  • XSLT technology is not new. It is available since 1999. So you should be able to find endless examples how to execute it in your environment. Please edit your question and add samples of (1) input XML, (2) servlets to remove, and (3) output XML. – Yitzhak Khabinsky Jun 01 '21 at 12:32
  • @YitzhakKhabinsky nothing much to edit but did some changes to the question. As I mentioned, there can be multiple servlets (like the dummy-servlets) in the sample and some actual-servlets. I need to remove the dummy-ones and keep the actual one in the web.xml. I spent some time to look for XSLT example but found ones in which we traverse the xml and replace one property or value. – Somtirtha Ghosh Jun 01 '21 at 14:21
  • Your XML is not well-formed. It is missing a root element. Please fix it. I will help you with the actual XSLT. – Yitzhak Khabinsky Jun 01 '21 at 14:44

1 Answers1

1

I took a liberty, and added a root element to the provided XML to make it well-formed.

Please try the following solution via XSLT.

The XSLT is using so called Identity Transform pattern.

You would need to modify two templates for your real XML: and provide a list of dummy Servlets to be removed.

Everything else outside of the dummy Servlets will be kept intact.

Please remember that XML is case sensitive, including actual values.

Input XML

<root>
    <servlet>
        <servlet-name>dummyServlet</servlet-name>
        <servlet-class>com.abc.Servlet</servlet-class>
        <init-param>
            <param-name>target</param-name>
            <param-value>foo.com</param-value>
        </init-param>
        <init-param>
            <param-name>log</param-name>
            <param-value>true</param-value>
        </init-param>
    </servlet>
    <servlet-mapping>
        <servlet-name>dummyServlet</servlet-name>
        <url-pattern>/*</url-pattern>
    </servlet-mapping>
    <servlet>
        <servlet-name>dummyServlet2</servlet-name>
        <servlet-class>com.abc.Servlet2</servlet-class>
        <init-param>
            <param-name>target</param-name>
            <param-value>foo.com</param-value>
        </init-param>
        <init-param>
            <param-name>log</param-name>
            <param-value>true</param-value>
        </init-param>
    </servlet>
    <servlet-mapping>
        <servlet-name>dummyServlet2</servlet-name>
        <url-pattern>/dummy</url-pattern>
    </servlet-mapping>
    <servlet>
        <servlet-name>actualServlet</servlet-name>
        <servlet-class>com.abc.ActualServlet</servlet-class>
        <init-param>
            <param-name>target</param-name>
            <param-value>foo.com</param-value>
        </init-param>
        <init-param>
            <param-name>log</param-name>
            <param-value>true</param-value>
        </init-param>
    </servlet>
    <servlet-mapping>
        <servlet-name>dummyServlet</servlet-name>
        <url-pattern>/actual</url-pattern>
    </servlet-mapping>
</root>

XSLT

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" encoding="utf-8" indent="yes" omit-xml-declaration="yes"/>
    <xsl:strip-space elements="*"/>

    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>

    <xsl:template match="servlet[servlet-name=('dummyServlet','dummyServlet2')]">
    </xsl:template>
        <xsl:template match="servlet-mapping[servlet-name=('dummyServlet','dummyServlet2')]">
    </xsl:template>
</xsl:stylesheet>

Output XML

<root>
  <servlet>
    <servlet-name>actualServlet</servlet-name>
    <servlet-class>com.abc.ActualServlet</servlet-class>
    <init-param>
      <param-name>target</param-name>
      <param-value>foo.com</param-value>
    </init-param>
    <init-param>
      <param-name>log</param-name>
      <param-value>true</param-value>
    </init-param>
  </servlet>
</root>
Yitzhak Khabinsky
  • 18,471
  • 2
  • 15
  • 21
  • Thanks a lot for taking time to provide your solution. But, `` lines are showing throwing Compile errors. I am checking from my side but if it is something you missed, please check. Once again thankyou for the effort. – Somtirtha Ghosh Jun 02 '21 at 04:36
  • FYI I tried a free online tool with sample xml and xslt you provided and it works. Still working on why getting a compile error when I run in my machine – Somtirtha Ghosh Jun 02 '21 at 05:16
  • your xsl transform is stripping all the linebreaks and if I remove `` this line, a line-break is appearing for each line. Is there any way to keep the line-breaks same as in input file.? – Somtirtha Ghosh Jun 08 '21 at 11:12
  • Empty lines have no meaning in XML files. – Yitzhak Khabinsky Jun 08 '21 at 11:48