source: other-projects/trunk/anttasks/src/org/greenstone/anttasks/DeleteChunkFromFile.java@ 20047

Last change on this file since 20047 was 20047, checked in by oranfry, 15 years ago

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

File size: 5.4 KB
Line 
1/*
2 * RegexSearchReplace.java
3 * A task to delete from a given starting nugget to a given ending nugget or the end of the file
4 *
5 * Copyright (C) 2005 New Zealand Digital Library, http://www.nzdl.org
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 */
21package org.greenstone.anttasks;
22
23import org.apache.tools.ant.*;
24import org.apache.tools.ant.taskdefs.*;
25import java.util.*;
26import java.io.*;
27import java.util.regex.*;
28
29
30public class DeleteChunkFromFile extends Task {
31
32 private File file = null;
33 private Pattern startTag = null;
34 private Pattern endTag = null;
35 private boolean leaveTags = false;
36
37 public static void main( String[] args ) {
38
39 DeleteChunkFromFile dcff = new DeleteChunkFromFile();
40 dcff.setFile(new File (args[0]));
41 dcff.setStartTag(args[1]);
42 dcff.setEndTag(args[2]);
43
44 dcff.execute();
45
46 }
47
48 public void execute() {
49
50 if ( file == null ) {
51 throw new BuildException( "Error - No file specified !!" );
52 }
53
54 if ( startTag == null ) {
55 throw new BuildException( "Error - No startTag specified !!" );
56 }
57
58 System.out.println( "File: " + file );
59 System.out.println( "StartTag: " + startTag );
60 System.out.println( "EndTag: " + endTag );
61 System.out.println( "LeaveTags: " + leaveTags );
62
63 int noReplaces = 0;
64
65 //create the output stream
66 BufferedWriter out = null;
67 File temp = null;
68 try {
69 //create temp file.
70 temp = File.createTempFile("dcff", ".tmp");
71
72 //delete temp file when program exits.
73 temp.deleteOnExit();
74
75 //writer to temp file
76 out = new BufferedWriter( new OutputStreamWriter(new FileOutputStream(temp), "UTF8") );
77
78 } catch (IOException e) {
79 throw new BuildException( "Error - Couldn't create or open the temp file" );
80 }
81
82
83 //create the input stream
84 BufferedReader in = null;
85 try {
86 in = new BufferedReader( new InputStreamReader(new FileInputStream(file), "UTF8") );
87 } catch ( Exception e ) {
88 throw new BuildException( "Error - Couldn't open the specified file" );
89 }
90
91 //pass the file through, searching and replacing
92 String line;
93 boolean hasMoreLines = true;
94 boolean lookingForStartTag = true;
95 String toDefer = null;
96
97 while ( hasMoreLines ) {
98
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 }
109 }
110
111 if ( line == null ) {
112 hasMoreLines = false;
113 } else {
114
115 String toWrite = null;
116 boolean endLine = false;
117 System.out.print("line: " + line );
118 if ( lookingForStartTag ) {
119 Matcher m = startTag.matcher(line);
120 if ( m.find() ) {
121 lookingForStartTag = false;
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)");
129 }
130 } else if ( endTag != null ) {
131 Matcher m = endTag.matcher(line);
132 if ( m.find() ) {
133 lookingForStartTag = true;
134 toDefer = line.substring(leaveTags?m.start():m.end());
135 System.out.println(" (c)");
136 } else {
137 System.out.println(" (d)");
138 }
139 }
140
141 if ( toWrite != null ) {
142 try {
143 out.write(toWrite);
144 if ( endLine ) {
145 out.newLine();
146 }
147 } catch ( Exception e ) {
148 throw new BuildException( "Error - Couldn't write to the temp file" );
149 }
150 }
151 }
152 }
153
154
155 try {
156 //close them both up
157 in.close();
158 out.close();
159 } catch ( Exception e ) {
160 throw new BuildException( "Error - Couldn't close a file" );
161 }
162
163
164 //copy the new file (temp) over the original
165 InputStream i;
166 OutputStream o;
167 try {
168 i = new FileInputStream(temp);
169 o = new FileOutputStream(file);
170
171 } catch ( Exception e ) {
172 throw new BuildException( "Error - Couldn't open the temp file" );
173 }
174
175 try {
176
177 // Transfer bytes from in to out
178 byte[] buf = new byte[1024];
179 int len;
180 while ((len = i.read(buf)) > 0) {
181 o.write(buf, 0, len);
182 }
183
184 //close them up
185 i.close();
186 o.close();
187 } catch ( Exception e ) {
188 throw new BuildException( "Error - Couldn't write to the specified file" );
189 }
190
191 System.out.println( "Finished" );
192 }
193
194
195 public void setFile(File file) {
196 this.file = file;
197 }
198
199 public void setStartTag(String st) {
200 try {
201 this.startTag = Pattern.compile(st);
202 } catch ( PatternSyntaxException pse ) {
203 throw new BuildException( "Invalid start tag!!" );
204 }
205 }
206
207 public void setEndTag(String et) {
208 try {
209 this.endTag = Pattern.compile(et);
210 } catch ( PatternSyntaxException pse ) {
211 throw new BuildException( "Invalid start tag!!" );
212 }
213 }
214
215 public void setLeaveTags( boolean lt ) {
216 this.leaveTags = lt;
217 }
218
219
220}
Note: See TracBrowser for help on using the repository browser.