1 27 package org.htmlparser.lexerapplications.tabby; 28 29 import java.io.File ; 30 import java.io.FileFilter ; 31 import java.io.FileInputStream ; 32 import java.io.FileOutputStream ; 33 34 import java.util.regex.Matcher ; 35 import java.util.regex.Pattern ; 36 import java.util.regex.PatternSyntaxException ; 37 38 import org.htmlparser.lexer.Cursor; 39 import org.htmlparser.lexer.Page; 40 41 46 public class Tabby 47 { 48 51 private static final int DEFAULT_TABSTOP = 4; 52 53 56 protected Filter mFilter; 57 58 61 protected int mTabsize; 62 63 66 public Tabby () 67 { 68 mFilter = null; 69 mTabsize = DEFAULT_TABSTOP; 70 } 71 72 77 public Tabby (final String filter) 78 { 79 this (); 80 mFilter = new Filter (filter); 81 } 82 83 88 public Tabby (final String filter, final int tabsize) 89 throws 90 IllegalArgumentException 91 { 92 this (filter); 93 if (0 >= tabsize) 94 throw new IllegalArgumentException ("tab size cannot be negative"); 95 mTabsize = tabsize; 96 } 97 98 102 protected void process (final File file) 103 { 104 File [] files; 105 106 if (file.isDirectory ()) 107 { 108 files = file.listFiles (mFilter); 109 for (int i = 0; i < files.length; i++) 110 process (files[i]); 111 } 112 else 113 edit (file); 114 } 115 116 120 protected void edit (final File file) 121 { 122 FileInputStream in; 123 Page page; 124 Cursor cursor; 125 int position; 126 int expected; 127 boolean modified; 128 char ch; 129 int last; 130 StringBuffer buffer; 131 FileOutputStream out; 132 133 try 134 { 135 in = new FileInputStream (file); 136 buffer = new StringBuffer (in.available ()); 137 try 138 { 139 page = new Page (in, null); 140 cursor = new Cursor (page, 0); 141 position = 0; 142 modified = false; 143 expected = 0; 144 last = -1; 145 while (Page.EOF != (ch = page.getCharacter (cursor))) 146 { 147 if (++expected != cursor.getPosition ()) 148 { 149 modified = true; 150 expected = cursor.getPosition (); 151 } 152 if ('\t' == ch) 153 { 154 do 155 { 156 buffer.append (' '); 157 position++; 158 } 159 while (0 != (position % mTabsize)); 160 modified = true; 161 } 162 else if ('\n' == ch) 163 { 164 if (last + 1 != position) 166 { 167 last = buffer.length () - (position - last - 1); 169 buffer.setLength (last); 170 modified = true; 171 } 172 buffer.append (ch); 173 position = 0; 174 last = -1; 175 } 176 else 177 { 178 buffer.append (ch); 179 if (!Character.isWhitespace (ch)) 180 last = position; 181 position++; 182 } 183 } 184 } 185 finally 186 { 187 in.close (); 188 } 189 if (modified) 190 { 191 System.out.println (file.getAbsolutePath ()); 192 out = new FileOutputStream (file); 193 out.write (buffer.toString ().getBytes (Page.DEFAULT_CHARSET)); 194 out.close (); 195 } 196 } 197 catch (Exception e) 198 { 199 System.out.println (e); 200 } 201 } 202 203 206 class Filter implements FileFilter 207 { 208 211 protected Pattern mExpression; 212 213 223 public Filter (final String expression) 224 throws 225 PatternSyntaxException 226 { 227 if (null == expression) 228 throw new IllegalArgumentException ( 229 "filter expression cannot be null"); 230 mExpression = Pattern.compile (expression); 231 } 232 233 237 243 public boolean accept (final File pathname) 244 { 245 Matcher matcher; 246 boolean ret; 247 248 if (pathname.isDirectory ()) 250 ret = true; 251 else 252 { 253 matcher = mExpression.matcher (pathname.getAbsolutePath ()); 254 ret = matcher.matches (); 255 } 256 257 return (ret); 258 } 259 } 260 261 270 public static void main (final String [] args) 271 { 272 Tabby tabby; 273 File file; 274 275 if (0 == args.length) 276 System.out.println ( 277 "usage: Tabby (<directory>|<file>)" 278 + " [file-match regexp] [tabsize]"); 279 else 280 { 281 if (2 < args.length) 282 tabby = new Tabby (args[1], Integer.parseInt (args[2])); 283 else 284 if (1 < args.length) 285 tabby = new Tabby (args[1]); 286 else 287 tabby = new Tabby (); 288 file = new File (args[0]); 289 tabby.process (file); 290 } 291 } 292 } 293 294 331 | Popular Tags |