View Javadoc
1   /*******************************************************************************
2    *   Gisgraphy Project 
3    * 
4    *   This library is free software; you can redistribute it and/or
5    *   modify it under the terms of the GNU Lesser General Public
6    *   License as published by the Free Software Foundation; either
7    *   version 2.1 of the License, or (at your option) any later version.
8    * 
9    *   This library is distributed in the hope that it will be useful,
10   *   but WITHOUT ANY WARRANTY; without even the implied warranty of
11   *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12   *   Lesser General Public License for more details.
13   * 
14   *   You should have received a copy of the GNU Lesser General Public
15   *   License along with this library; if not, write to the Free Software
16   *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA
17   * 
18   *  Copyright 2008  Gisgraphy project 
19   *  David Masclet <davidmasclet@gisgraphy.com>
20   *  
21   *  
22   *******************************************************************************/
23  package com.gisgraphy.helper;
24  
25  import java.io.BufferedInputStream;
26  import java.io.BufferedReader;
27  import java.io.BufferedWriter;
28  import java.io.DataInputStream;
29  import java.io.File;
30  import java.io.FileInputStream;
31  import java.io.FileWriter;
32  import java.io.IOException;
33  import java.io.InputStream;
34  import java.io.InputStreamReader;
35  
36  /**
37   * 
38   * Create a filter that take a file and remove all the line that contain some
39   * specific string
40   * 
41   * @author <a href="mailto:david.masclet@gisgraphy.com">David Masclet</a>
42   */
43  public class FileLineFilter {
44  
45      private String[] stringToFilter;
46  
47      /**
48       * @param stringToFilter
49       *            All the lines that contains the word or sentence will be
50       *            filtered
51       */
52      public FileLineFilter(String[] stringToFilter) {
53  	this.stringToFilter = stringToFilter;
54      }
55  
56      /**
57       * 
58       * All the lines that contains the word or sentence of the filter will be
59       * deleted, the result file will be written in the destFile
60       * 
61       * @param originalFile
62       *            The file to filter
63       * @param destFile
64       *            The filtered file <br/>
65       *            note : if the destination file does not exists, it will be
66       *            created.
67       */
68      public void filter(File originalFile, File destFile) {
69  	if (originalFile == null) {
70  	    throw new IllegalArgumentException("can not check a null file");
71  	}
72  	if (!originalFile.exists()) {
73  	    throw new IllegalArgumentException("can not check a file that does not exists");
74  	}
75  	if (!originalFile.isFile()) {
76  	    throw new IllegalArgumentException("can only check file, not directory");
77  	}
78  	if (destFile == null) {
79  	    throw new IllegalArgumentException("can not write to a null file");
80  	}
81  	if (!destFile.exists()) {
82  	    try {
83  		if (!destFile.createNewFile()) {
84  		    throw new RuntimeException("Can not create file " + destFile.getAbsolutePath());
85  		}
86  	    } catch (IOException e) {
87  		throw new RuntimeException("Can not create file " + destFile.getAbsolutePath(), e);
88  	    }
89  	}
90  	if (!destFile.isFile()) {
91  	    throw new IllegalArgumentException("can only write to a file, not to a directory ");
92  	}
93  	BufferedWriter out = null;
94  	InputStream stream = null;
95  	DataInputStream in = null;
96  	try {
97  	    stream = new BufferedInputStream(new FileInputStream(originalFile));
98  	    in = new DataInputStream(stream);
99  	    out = new BufferedWriter(new FileWriter(destFile));
100 	    BufferedReader br = new BufferedReader(new InputStreamReader(in));
101 	    String strLine;
102 	    // Read File Line By Line
103 	    while ((strLine = br.readLine()) != null) {
104 		boolean shouldBeFiltered = false;
105 		for (int i = 0; i < stringToFilter.length; i++) {
106 		    if (strLine.contains(stringToFilter[i])) {
107 			shouldBeFiltered = true;
108 			break;
109 		    }
110 		}
111 		if (!shouldBeFiltered) {
112 		    out.write(strLine);
113 		    out.write("\r\n");
114 		}
115 	    }
116 	} catch (Exception e) {// Catch exception if any
117 	    throw new RuntimeException("an exception has occured durind filtering of file " + originalFile.getAbsolutePath() + " to " + destFile.getAbsolutePath(), e);
118 	} finally {
119 	    if (in != null) {
120 		try {
121 		    in.close();
122 		} catch (IOException e) {
123 		}
124 	    }
125 	    if (stream != null) {
126 		try {
127 		    stream.close();
128 		} catch (IOException e) {
129 		}
130 	    }
131 	    if (out != null) {
132 		try {
133 		    out.flush();
134 		    out.close();
135 		} catch (IOException e) {
136 		}
137 	    }
138 	}
139     }
140 
141 }