• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to replace the string which line before or after a matched string

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Source file:

<category name="org.apache.cxf">
<priority value="WARN" />
</category>


<root>
<priority value="ERROR" />
<appender-ref ref="CONSOLE"/>
<appender-ref ref="FILE"/>
</root>




Target file:

<category name="org.apache.cxf">
<priority value="INFO" />
</category>


<root>
<priority value="INFO" />
<appender-ref ref="CONSOLE"/>
<appender-ref ref="FILE"/>
</root>




Question 1:
My string locator is set on "org.apache.cxf", when it is matched, the value after the line should be changed from "WARN" to "INFO".
So how could i write the ant script?

Question 2:
In the same file, i also want to change the value before the line which string matches "appender-ref ref="CONSOLE""
How can i do?
 
Crespo Chan
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I've googled for a long time, seems xmltask could handle this,

<xmltask source="${env.TOMCAT_HOME}/webapps/services/WEB-INF/classes/log4j.xml"
dest="${env.TOMCAT_HOME}/webapps/services/WEB-INF/classes/log4j.xml">
<replace path="/log4j:configuration/category[2]/priority/value()"
withText="INFO"/>
</xmltask>

but i could not solve the dtd issue

log4j.dtd (A file or directory in the path name does not exist.)

and the xpath issue(skip the dtd issue but remove the dtd line in the xml file)

Caused by: java.lang.ClassNotFoundException: com.sun.org.apache.xpath.internal.XPathAPI
at java.net.URLClassLoader.findClass(URLClassLoader.java:665)



to make it more clear , i post the full xml file(the green string should be subtituted )

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration PUBLIC "-//LOGGER" "log4j.dtd">

<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">

<!-- Appenders -->
<appender name="CONSOLE" class="org.apache.log4j.ConsoleAppender">
<param name="Target" value="System.out" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="[%-5p][%d{ISO8601}][%t] %m%n"/>
</layout>
</appender>

<appender name="FILE" class="org.apache.log4j.DailyRollingFileAppender">
<param name="file" value="/tmp/1.log" />
<param name="append" value="true" />
<param name="encoding" value="UTF-8" />
<param name="DatePattern" value="'.'yyyy-MM-dd"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="[%-5p][%d{ISO8601}][%t]%m%n"/>
</layout>
</appender>

<appender name="CACHEFILE" class="org.apache.log4j.DailyRollingFileAppender">
<param name="file" value="/tmp/2.log" />
<param name="append" value="true" />
<param name="encoding" value="UTF-8" />
<param name="DatePattern" value="'.'yyyy-MM-dd"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="[%-5p][%d{ISO8601}][%t]%m%n"/>
</layout>
</appender>

<!-- Application Loggers -->
<category name="com.documentum.fc.client">
<priority value="ERROR" />
</category>

<category name="org.apache.cxf">
<priority value="WARN" />
</category>

<!-- Root Logger -->
<root>
<priority value="ERROR" />
<appender-ref ref="CONSOLE"/>
<appender-ref ref="FILE"/>
</root>

</log4j:configuration>



Could anyone help me out?
 
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Using XMLTask shoudl work as you found out. Note the xmlcatalog attribute which lets you specify a local DTD if one is not available.

Another option is to use Ant tasks to:
1) Read the file into a property
2) Use the replace task to change the text. (Regular expressions support line breaks.)
3) Echo the property back to a file

The later appraoch would only work if you have some control over the format of the XML.
 
The only taste of success some people get is to take a bite out of you. Or this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic