Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replacing multiple lines of text with Ant

I have an xml file where I need to comment out a whole piece of text with Ant.

There's this Ant task

<replace file="${src.dir}/Version.as" 
         token="@revisionPrana" value="${revision}"/>

that I use to replace words, but in my case I need to replace a whole block like this:

<value>
  <object class="edumatic.backoffice.view.modules.NavigationModuleInfo">
    <property name="url"
     value="edumatic/backoffice/view/modules/support/ExamsNavigationModule.swf"/>
    <property name="icon"
     value="edumatic/backoffice/view/modules/support/assets/book.png" />
    <property name="title" value="Assessments" />
    <property name="pluginID" value="EXAM" />
  </object>
</value>
<value>
  <object class="edumatic.backoffice.view.modules.ContentModuleInfo">
    <property name="url"
     value="edumatic/backoffice/view/modules/support/ExamsContentModule.swf" />
    <property name="pluginID" value="EXAM" />
  </object>
</value>

Into

<!--value>
  <object class="edumatic.backoffice.view.modules.NavigationModuleInfo">
    <property name="url"
     value="edumatic/backoffice/view/modules/support/ExamsNavigationModule.swf"/>
    <property name="icon"
     value="edumatic/backoffice/view/modules/support/assets/book.png" />
    <property name="title" value="Assessments" />
    <property name="pluginID" value="EXAM" />
  </object>
</value>
<value>
  <object class="edumatic.backoffice.view.modules.ContentModuleInfo">
    <property name="url"
     value="edumatic/backoffice/view/modules/support/ExamsContentModule.swf" />
    <property name="pluginID" value="EXAM" />
  </object>
</value-->

So, basically I need to comment out a whole block of XML. Can I do this with a replace task (putting the whole block in the attribute token and value doesn't really work)? Or is there a quick way to read in the xml with ant and delete some nodes and save the xml again?

Searching for and replace it by isn't an option because there are multiple value children and not all of them need to be commented out.

Adding a attribute like isn't an option either because the xml is being parsed by an IOC container (Prana). Maybe prana will ignore the id="1" but it still iss messy, and I don't like messy on the long term.

like image 419
Lieven Cardoen Avatar asked Dec 22 '22 14:12

Lieven Cardoen


2 Answers

If you can identify what is to be replaced through a regular expression, I recommend using the optional task replaceregexp. Here's the doc: http://ant.apache.org/manual/Tasks/replaceregexp.html You can call it twice, one for the start tag and other for the end tag.

The regexp for replacing your can be a bit cumbersome, since you say you do not want to replace all tags, but I think this is the easiest way.

Another option would be to create a custom ant task to do what you want.

like image 186
Miguel Ping Avatar answered Dec 25 '22 02:12

Miguel Ping


If it is an XML file, then you could also call an XSLT transform

like image 44
Mads Hansen Avatar answered Dec 25 '22 02:12

Mads Hansen