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

Last change on this file since 17295 was 17295, checked in by oranfry, 16 years ago

added a delete chunk from file task and made rsr nicer with nested elements

File size: 4.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 String startTag = null;
34 private String 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 FileWriter(temp));
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 FileReader( file ) );
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 while ( hasMoreLines ) {
96
97 try {
98 line = in.readLine();
99 } catch ( Exception e ) {
100 throw new BuildException( "Error - Couldn't read from the specified file" );
101 }
102
103 if ( line == null ) {
104 hasMoreLines = false;
105 } else {
106
107 boolean writeLine = lookingForStartTag;
108
109 if ( lookingForStartTag ) {
110 if ( line.matches( startTag ) ) {
111 lookingForStartTag = false;
112 writeLine = leaveTags;
113 }
114 } else {
115 if ( endTag != null && line.matches( endTag ) ) {
116 lookingForStartTag = true;
117 writeLine = leaveTags;
118 }
119 }
120
121 if ( writeLine ) {
122 try {
123 out.write(line);
124 out.newLine();
125 } catch ( Exception e ) {
126 throw new BuildException( "Error - Couldn't write to the temp file" );
127 }
128 }
129 }
130 }
131
132
133 try {
134 //close them both up
135 in.close();
136 out.close();
137 } catch ( Exception e ) {
138 throw new BuildException( "Error - Couldn't close a file" );
139 }
140
141
142 //copy the new file (temp) over the original
143 InputStream i;
144 OutputStream o;
145 try {
146 i = new FileInputStream(temp);
147 o = new FileOutputStream(file);
148
149 } catch ( Exception e ) {
150 throw new BuildException( "Error - Couldn't open the temp file" );
151 }
152
153 try {
154
155 // Transfer bytes from in to out
156 byte[] buf = new byte[1024];
157 int len;
158 while ((len = i.read(buf)) > 0) {
159 o.write(buf, 0, len);
160 }
161
162 //close them up
163 i.close();
164 o.close();
165 } catch ( Exception e ) {
166 throw new BuildException( "Error - Couldn't write to the specified file" );
167 }
168
169 System.out.println( "Finished" );
170 }
171
172
173 public void setFile(File file) {
174 this.file = file;
175 }
176
177 public void setStartTag(String st) {
178 this.startTag = st;
179 }
180
181 public void setEndTag(String et) {
182 this.endTag = et;
183 }
184
185 public void setLeaveTags( boolean lt ) {
186 this.leaveTags = lt;
187 }
188
189
190}
Note: See TracBrowser for help on using the repository browser.