top / index / prev / next / target / source
日記形式でつづる いがぴょんコラム ウェブページです。
尻に火が付きまくり , Javaソースファイルからホワイトスペースを除去(Strip.java)
うひゃあ 忙しいなぁ。
Javaのソースファイルからホワイトスペースを除去するサンプルプログラム。このプログラムはぜんぜんちゃんとしていません。動かすのはお勧めできません (苦笑) いつかまじめな ホワイトスペース除去PGを作りたく思っているのであった。
import java.io.*;
public class Strip {
public static void main(String[] args) {
try {
if (args.length == 0) {
usage();
} else {
new Strip().process(args[0], args[0] + ".st");
}
} catch (EOFException ex) {
;
} catch (IOException ex) {
System.out.println(ex.toString());
ex.printStackTrace();
}
}
public static void usage() {
System.out.println("java Strip SourceFile.java");
}
public void process(String fileIn, String fileOut) throws IOException {
try {
reader =
new BufferedReader(
new InputStreamReader(
new FileInputStream(fileIn)));
writer =
new BufferedWriter(
new OutputStreamWriter(
new FileOutputStream(fileOut)));
getToken();
} finally {
try {
reader.close();
writer.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
private CharArrayWriter writerWord = new CharArrayWriter();
private BufferedReader reader = null;
private BufferedWriter writer = null;
public void getToken() throws IOException {
for (; ;) {
int read = reader.read();
if (read < 0) {
throw new EOFException("End of file.");
}
try {
switch (read) {
case 0x0a:
case 0x0d:
case 0x0c:
flushWord();
processWhiteSpace();
break;
case '\t':
flushWord();
processWhiteSpace();
break;
case ' ':
flushWord();
processWhiteSpace();
break;
case ';':
// line end.
writerWord.write(read);
flushWord();
break;
case '\'':
flushWord();
processChar();
break;
case '"':
flushWord();
processString();
break;
case '{':
flushWord();
writer.write(read);
break;
case '}':
flushWord();
writer.write(read);
break;
case '+':
flushWord();
writer.write(read);
break;
case '-':
flushWord();
writer.write(read);
break;
default:
withoutFlushWord();
writerWord.write(read);
break;
}
} catch (EOFException ex) {
flushWord();
}
}
}
private boolean isPastFlushWord = false;
public void flushWord() throws IOException {
//if (isPastFlushWord) {
//if (writerWord.size() > 0) {
writer.write(' ');
//}
//}
isPastFlushWord = true;
System.out.println("flushWord(): " + writerWord.toString());
writer.write(writerWord.toString());
writer.flush();
writerWord.reset();
}
public void withoutFlushWord() {
//System.out.println("withoutFlushWord()");
isPastFlushWord = false;
}
public void processWhiteSpace() throws IOException {
for (; ;) {
reader.mark(1);
int read = reader.read();
if (read < 0) {
throw new EOFException("End of file.");
}
switch (read) {
case 0x0a:
case 0x0d:
case 0x0c:
case '\t':
case ' ':
break;
default:
reader.reset();
return ;
}
}
}
public void processChar() throws IOException {
writerWord.write('\'');
boolean isPastEscape = false;
for (; ;) {
int read = reader.read();
if (read < 0) {
throw new EOFException("End of file.");
}
switch (read) {
case '\'':
writerWord.write(read);
if (isPastEscape) {
isPastEscape = false;
break;
} else {
return ;
}
case '\\':
// escape
writerWord.write(read);
if (isPastEscape) {
isPastEscape = false;
} else {
isPastEscape = true;
}
break;
default:
isPastEscape = false;
writerWord.write(read);
break;
}
}
}
public void processString() throws IOException {
writerWord.write('"');
boolean isPastEscape = false;
for (; ;) {
int read = reader.read();
if (read < 0) {
throw new EOFException("\"end\" of file.");
}
switch (read) {
case '"':
writerWord.write(read);
if (isPastEscape) {
isPastEscape = false;
break;
} else {
return ;
}
case '\\':
// escape
writerWord.write(read);
if (isPastEscape) {
isPastEscape = false;
} else {
isPastEscape = true;
}
break;
default:
isPastEscape = false;
writerWord.write(read);
break;
}
}
}
}
.