source: main/trunk/ant-tasks/src/org/greenstone/anttasks/DeleteChunkFromFile.java@ 22480

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

removed debug output

File size: 5.2 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 if ( lookingForStartTag ) {
118 Matcher m = startTag.matcher(line);
119 if ( m.find() ) {
120 lookingForStartTag = false;
121 toWrite = line.substring(0,leaveTags?m.end():m.start());
122 toDefer = line.substring(m.end());
123 } else {
124 toWrite = line;
125 endLine = true;
126 }
127 } else if ( endTag != null ) {
128 Matcher m = endTag.matcher(line);
129 if ( m.find() ) {
130 lookingForStartTag = true;
131 toDefer = line.substring(leaveTags?m.start():m.end());
132 }
133 }
134
135 if ( toWrite != null ) {
136 try {
137 out.write(toWrite);
138 if ( endLine ) {
139 out.newLine();
140 }
141 } catch ( Exception e ) {
142 throw new BuildException( "Error - Couldn't write to the temp file" );
143 }
144 }
145 }
146 }
147
148
149 try {
150 //close them both up
151 in.close();
152 out.close();
153 } catch ( Exception e ) {
154 throw new BuildException( "Error - Couldn't close a file" );
155 }
156
157
158 //copy the new file (temp) over the original
159 InputStream i;
160 OutputStream o;
161 try {
162 i = new FileInputStream(temp);
163 o = new FileOutputStream(file);
164
165 } catch ( Exception e ) {
166 throw new BuildException( "Error - Couldn't open the temp file" );
167 }
168
169 try {
170
171 // Transfer bytes from in to out
172 byte[] buf = new byte[1024];
173 int len;
174 while ((len = i.read(buf)) > 0) {
175 o.write(buf, 0, len);
176 }
177
178 //close them up
179 i.close();
180 o.close();
181 } catch ( Exception e ) {
182 throw new BuildException( "Error - Couldn't write to the specified file" );
183 }
184
185 System.out.println( "Finished" );
186 }
187
188
189 public void setFile(File file) {
190 this.file = file;
191 }
192
193 public void setStartTag(String st) {
194 try {
195 this.startTag = Pattern.compile(st);
196 } catch ( PatternSyntaxException pse ) {
197 throw new BuildException( "Invalid start tag!!" );
198 }
199 }
200
201 public void setEndTag(String et) {
202 try {
203 this.endTag = Pattern.compile(et);
204 } catch ( PatternSyntaxException pse ) {
205 throw new BuildException( "Invalid start tag!!" );
206 }
207 }
208
209 public void setLeaveTags( boolean lt ) {
210 this.leaveTags = lt;
211 }
212
213
214}
Note: See TracBrowser for help on using the repository browser.