ant-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Bruce Atherton <br...@callenish.com>
Subject Re: Stripping Contents from XML
Date Mon, 28 Nov 2011 20:48:41 GMT
If I understand you correctly, you want to take a file that looks to 
contain the output from an Ant run and extract valid XML from it, is 
that correct?

If so, there are two parts you need in a filterchain. The first is 
something to strip the "    [api] " tags from the beginning of each 
line. This is easily done with the replaceregex token filter something 
like the following:

<tokenfilter>
     <replaceregex pattern="^\s*\[api\]" replace="  " flags="i"/>
</tokenfilter>

See 
http://docs.oracle.com/javase/6/docs/api/java/util/regex/Pattern.html 
for more details on regular expressions.

The second part is trickier. What you need is something that will remove 
all lines above the first occurrence of a String and below a second 
String. That filter is not currently part of Ant, but should be easy 
enough to implement.

Here is an untested version to give you an idea of what you need:

package org.apache.tools.ant.filters;

import org.apache.tools.ant.types.Parameter;

import java.io.IOException;
import java.io.Reader;

/**
  * Filter which includes only lines within a segment defined by a start 
and end string.
  *
  * Example:
  *
  * <pre>&lt;filterreader 
classname=&quot;org.apache.tools.ant.filters.SegmentFilter&quot;&gt;
  * &lt;param type=&quot;start&quot; value=&quot;foo&quot;/&gt;
  * &lt;param type=&quot;end&quot; value=&quot;bar&quot;/&gt;
  * &lt;/filterreader&gt;</pre>
  *
  * This will include only lines that appear after the string foo 
appears <code>foo</code> and
  * before the string <code>bar</code> appears, including those lines 
containing foo and bar.
  *
  */
public final class SegmentFilter
     extends BaseParamFilterReader
     implements ChainableReader {
     /** Parameter name for the words to filter on. */
     private static final String START_KEY = "start";

     /** Parameter name for the words to filter on. */
     private static final String END_KEY = "end";

     /* State of the filter, either before segment, in segment, or after 
segment */
     private enum STATE {BEFORE, DURING, AFTER}
     private STATE currState = STATE.BEFORE;

     /** Strings that define the start and end of the segment. */
     private String start = "";
     private String end = "";

     /**
      * Remaining line to be read from this filter, or <code>null</code> if
      * the next call to <code>read()</code> should read the original stream
      * to find the next matching line.
      */
     private String line = null;

     /**
      * Constructor for "dummy" instances.
      *
      * @see 
org.apache.tools.ant.filters.BaseFilterReader#BaseFilterReader()
      */
     public SegmentFilter() {
         super();
     }

     /**
      * Creates a new filtered reader.
      *
      * @param in A Reader object providing the underlying stream.
      *           Must not be <code>null</code>.
      */
     public SegmentFilter(final Reader in) {
         super(in);
     }

     /**
      * Returns the next character in the filtered stream, only including
      * lines from the original stream which are contained in the segment.
      *
      * @return the next character in the resulting stream, or -1
      * if the end of the resulting stream has been reached
      *
      * @exception java.io.IOException if the underlying stream throws 
an IOException
      * during reading
      */
     public int read() throws IOException {
         if (!getInitialized()) {
             initialize();
             setInitialized(true);
         }

         int ch = -1;

         if (line != null) {
             ch = line.charAt(0);
             if (line.length() == 1) {
                 line = null;
             } else {
                 line = line.substring(1);
             }
         } else {

             for (line = readLine(); line != null; line = readLine()) {
                 if (currState == STATE.BEFORE) {
                     if (this.start.length() > 0 && 
line.contains(this.start)) {
                         currState = STATE.DURING;
                     }
                 }
                 if (currState == STATE.DURING) {
                     if (this.end.length() > 0 && line.contains(this.end)) {
                         currState = STATE.AFTER;
                     }
                     break;
                 }
             }
             if (line != null) {
                 return read();
             }
         }
         return ch;
     }

     public String getStart() {
         return start;
     }

     public void setStart(String start) {
         this.start = start;
     }

     public String getEnd() {
         return end;
     }

     public void setEnd(String end) {
         this.end = end;
     }

     /**
      * Creates a new LineContains using the passed in
      * Reader for instantiation.
      *
      * @param rdr A Reader object providing the underlying stream.
      *            Must not be <code>null</code>.
      *
      * @return a new filter based on this configuration, but filtering
      *         the specified reader
      */
     public Reader chain(final Reader rdr) {
         SegmentFilter newFilter = new SegmentFilter(rdr);
         newFilter.setStart(getStart());
         newFilter.setEnd(getEnd());
         return newFilter;
     }

     /**
      * Parses the parameters to add user-defined start and end strings.
      */
     private void initialize() {
         Parameter[] params = getParameters();
         if (params != null) {
             for (int i = 0; i < params.length; i++) {
                 if (START_KEY.equals(params[i].getType())) {
                     setStart(params[i].getValue());
                 } else if (END_KEY.equals(params[i].getType())) {
                     setEnd(params[i].getValue());
                 }
             }
         }
     }

}


On 27/11/2011 11:18 PM, Vinodh Kumar wrote:
> Hi,
>
> Can I know how to achieve with ant? Basically I need xml tag content from
> [api]<Servers>  to [api]</Servers>  as output
>
> Any hint?
>
> *      [api] DBTime is 20111128005710
>        [api] Invoking api: getServerList, expectedtofail: false
>        [api]<?xml version="1.0" encoding="UTF-8"?>
>        [api]<Servers>
>        [api]<Server Id="homeid" Name="home"
>        [api]         Status="Active" Type="IntegrationAgentServer"/>
>         .......
>        [api]</Servers>*
>
>
> to
>
> * [api]<Servers>
>        [api]<Server Id="homeid" Name="home"
>        [api]         Status="Active" Type="IntegrationAgentServer"/>
>         .......
>        [api]</Servers>*
>
> --Vinodh Kumar
>
>
>


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@ant.apache.org
For additional commands, e-mail: user-help@ant.apache.org


Mime
View raw message