Changeset 20047 for other-projects/trunk


Ignore:
Timestamp:
2009-07-21T15:17:02+12:00 (15 years ago)
Author:
oranfry
Message:

the deletechunkfromfile task now supports start and end tags on the same line, with content in between

Location:
other-projects/trunk/anttasks
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • other-projects/trunk/anttasks/build.xml

    r20041 r20047  
    110110        <dcff file="test/dcff3.txt" startTag="fox" leaveTags="true"/>
    111111
    112     </target>
     112        <!-- test 4 -->
     113        <copy file="src/test/dcff.txt" tofile="test/dcff4.txt"/>
     114        <dcff file="test/dcff4.txt" startTag="&lt;!--start--&gt;" endTag="&lt;!--end--&gt;"/>
     115
     116        </target>
    113117
    114118    <target name="test-gpv" depends="test-setup">
  • other-projects/trunk/anttasks/src/org/greenstone/anttasks/DeleteChunkFromFile.java

    r17554 r20047  
    3131
    3232    private File file = null;
    33     private String startTag = null;
    34     private String endTag = null;
     33    private Pattern startTag = null;
     34    private Pattern endTag = null;
    3535    private boolean leaveTags = false;
    3636
     
    9393        boolean hasMoreLines = true;
    9494        boolean lookingForStartTag = true;
     95        String toDefer = null;
     96
    9597        while ( hasMoreLines ) {
    9698
    97             try {
    98                 line = in.readLine();
    99             } catch ( Exception e ) {
    100                 throw new BuildException( "Error - Couldn't read from the specified file" );
     99            //get the next line - either from the file of the leftovers from the last loop around
     100            if ( toDefer != null ) {
     101                line = toDefer;
     102                toDefer = null;
     103            } else {
     104                try {
     105                    line = in.readLine();
     106                } catch ( Exception e ) {
     107                    throw new BuildException( "Error - Couldn't read from the specified file" );
     108                }
    101109            }
    102110
     
    105113            } else {
    106114
    107                 boolean writeLine = lookingForStartTag;
    108 
     115                String toWrite = null;
     116                boolean endLine = false;
     117                System.out.print("line: " + line );
    109118                if ( lookingForStartTag ) {
    110                     if ( line.matches( startTag ) ) {
     119                    Matcher m = startTag.matcher(line);
     120                    if ( m.find() ) {
    111121                        lookingForStartTag = false;
    112                         writeLine = leaveTags;
     122                        toWrite = line.substring(0,leaveTags?m.end():m.start());
     123                        toDefer = line.substring(m.end());
     124                        System.out.println(" (a)");
     125                    } else {
     126                        toWrite = line;
     127                        endLine = true;
     128                        System.out.println(" (b)");
    113129                    }
    114                 } else {
    115                     if ( endTag != null && line.matches( endTag ) ) {
     130                } else if ( endTag != null ) {
     131                    Matcher m = endTag.matcher(line);
     132                    if ( m.find() ) {
    116133                        lookingForStartTag = true;
    117                         writeLine = leaveTags;
     134                        toDefer = line.substring(leaveTags?m.start():m.end());
     135                        System.out.println(" (c)");
     136                    } else {
     137                        System.out.println(" (d)");
    118138                    }
    119139                }
    120140
    121                 if ( writeLine ) {
     141                if ( toWrite != null ) {
    122142                    try {
    123                         out.write(line);
    124                         out.newLine();
     143                        out.write(toWrite);
     144                        if ( endLine ) {
     145                            out.newLine();
     146                        }
    125147                    } catch ( Exception e ) {
    126148                        throw new BuildException( "Error - Couldn't write to the temp file" );
     
    176198
    177199    public void setStartTag(String st) {
    178         this.startTag = st;
     200        try {
     201            this.startTag = Pattern.compile(st);
     202        } catch ( PatternSyntaxException pse ) {
     203            throw new BuildException( "Invalid start tag!!" );
     204        }
    179205    }
    180206
    181207    public void setEndTag(String et) {
    182         this.endTag = et;
     208        try {
     209            this.endTag = Pattern.compile(et);
     210        } catch ( PatternSyntaxException pse ) {
     211            throw new BuildException( "Invalid start tag!!" );
     212        }
    183213    }
    184214
  • other-projects/trunk/anttasks/src/test/dcff.txt

    r17554 r20047  
    1313русскОй язык
    1414äž­æ–‡
     15
     16more text
     17b<!--start-->row<!--end-->n
     18even more text
Note: See TracChangeset for help on using the changeset viewer.