r67529 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r67528‎ | r67529 | r67530 >
Date:13:28, 7 June 2010
Author:daniel
Status:deferred
Tags:
Comment:
better command line handling for input/oputput files
Modified paths:
  • /trunk/WikiWord/WikiWordBuilder/src/main/java/de/brightbyte/wikiword/extract/StreamProcessorApp.java (modified) (history)
  • /trunk/WikiWord/WikiWordBuilder/src/main/java/de/brightbyte/wikiword/extract/WordSenseIndexer.java (modified) (history)

Diff [purge]

Index: trunk/WikiWord/WikiWordBuilder/src/main/java/de/brightbyte/wikiword/extract/WordSenseIndexer.java
@@ -1,8 +1,6 @@
22 package de.brightbyte.wikiword.extract;
33
4 -import java.io.FileNotFoundException;
54 import java.io.IOException;
6 -import java.io.UnsupportedEncodingException;
75 import java.text.ParseException;
86 import java.util.Collections;
97 import java.util.List;
@@ -11,10 +9,8 @@
1210 import de.brightbyte.data.cursor.DataCursor;
1311 import de.brightbyte.data.cursor.DataSink;
1412 import de.brightbyte.data.measure.Measure;
15 -import de.brightbyte.io.ConsoleIO;
1613 import de.brightbyte.io.LineCursor;
1714 import de.brightbyte.io.LineSink;
18 -import de.brightbyte.io.OutputSink;
1915 import de.brightbyte.text.Chunker;
2016 import de.brightbyte.text.RegularExpressionChunker;
2117 import de.brightbyte.util.PersistenceException;
@@ -46,18 +42,18 @@
4743 }
4844
4945 @Override
50 - protected DataSink<String> openSink() throws PersistenceException {
 46+ protected DataSink<String> openSink(int paramIndex) throws PersistenceException {
5147 try {
52 - return new LineSink(getOutputWriter());
 48+ return new LineSink(getOutputWriter(paramIndex));
5349 } catch (IOException e) {
5450 throw new PersistenceException(e);
5551 }
5652 }
5753
5854 @Override
59 - protected DataCursor<String> openCursor() throws PersistenceException {
 55+ protected DataCursor<String> openCursor(int paramIndex) throws PersistenceException {
6056 try {
61 - return new LineCursor(getInputReader());
 57+ return new LineCursor(getInputReader(paramIndex));
6258 } catch (IOException e) {
6359 throw new PersistenceException(e);
6460 }
Index: trunk/WikiWord/WikiWordBuilder/src/main/java/de/brightbyte/wikiword/extract/StreamProcessorApp.java
@@ -16,7 +16,6 @@
1717 import de.brightbyte.data.cursor.DataCursor;
1818 import de.brightbyte.data.cursor.DataSink;
1919 import de.brightbyte.io.ConsoleIO;
20 -import de.brightbyte.io.LogOutput;
2120 import de.brightbyte.util.PersistenceException;
2221 import de.brightbyte.wikiword.StoreBackedApp;
2322 import de.brightbyte.wikiword.builder.InputFileHelper;
@@ -37,33 +36,44 @@
3837 }
3938
4039
41 - protected File getOutputFile() {
 40+ protected File getOutputFile(int paramIndex) {
4241 if (outputFile==null) {
43 - if (args.getParameterCount()>2) {
44 - outputFile = new File(args.getParameter(2));
 42+ if (args.getParameterCount()>=paramIndex) {
 43+ String f = args.getParameter(paramIndex);
 44+ if (!f.equals("-")) outputFile = new File(f);
4545 }
4646 }
4747 return outputFile;
4848 }
4949
 50+ protected String getInputPath(int paramIndex) {
 51+ if (inputPath==null) {
 52+ if (args.getParameterCount()>=paramIndex) {
 53+ inputPath = args.getParameter(paramIndex);
 54+ }
 55+ }
 56+ return inputPath;
 57+ }
 58+
5059 protected String getOutputFileEncoding() {
5160 return args.getStringOption("outputencoding", "UTF-8");
5261 }
5362
 63+ protected String inputPath;
5464 protected File outputFile;
5565 protected Writer outputWriter;
5666 protected OutputStream outputStream;
5767 private InputStream inputStream;
5868 private Reader inputReader;
5969
60 - protected Writer getOutputWriter() throws FileNotFoundException, UnsupportedEncodingException {
 70+ protected Writer getOutputWriter(int paramIndex) throws FileNotFoundException, UnsupportedEncodingException {
6171 if (outputWriter==null) {
62 - File f = getOutputFile();
 72+ File f = getOutputFile(paramIndex);
6373 if (f==null) {
6474 outputWriter = ConsoleIO.writer;
6575 usingStdout = true;
6676 } else {
67 - OutputStream out = getOutputStream();
 77+ OutputStream out = getOutputStream(paramIndex);
6878 outputWriter = new OutputStreamWriter(out, getOutputFileEncoding());
6979 usingStdout = out == System.out;
7080 }
@@ -76,9 +86,9 @@
7787 return outputWriter;
7888 }
7989
80 - protected OutputStream getOutputStream() throws FileNotFoundException {
 90+ protected OutputStream getOutputStream(int paramIndex) throws FileNotFoundException {
8191 if (outputStream==null) {
82 - File f = getOutputFile();
 92+ File f = getOutputFile(paramIndex);
8393 if (f==null) {
8494 outputStream = System.out;
8595 usingStdout = true;
@@ -92,14 +102,14 @@
93103 return outputStream;
94104 }
95105
96 - protected Reader getInputReader() throws IOException {
 106+ protected Reader getInputReader(int paramIndex) throws IOException {
97107 if (inputReader==null) {
98 - File f = getOutputFile();
99 - if (f==null) {
 108+ String path = getInputPath(paramIndex);
 109+ if (path==null || path.equals("-")) {
100110 inputReader = ConsoleIO.newReader();
101111 usingStdin = true;
102112 } else {
103 - InputStream in = getInputStream();
 113+ InputStream in = getInputStream(paramIndex);
104114 inputReader = new InputStreamReader(in, getOutputFileEncoding());
105115 usingStdin = (in == System.in);
106116 }
@@ -108,15 +118,15 @@
109119 return inputReader;
110120 }
111121
112 - protected InputStream getInputStream() throws IOException {
 122+ protected InputStream getInputStream(int paramIndex) throws IOException {
113123 if (inputStream==null) {
114 - File f = getOutputFile();
115 - if (f==null) {
 124+ String path = getInputPath(paramIndex);
 125+ if (path==null || path.equals("-")) {
116126 inputStream = System.in;
117127 usingStdin = true;
118128 } else {
119 - inputStream = inputHelper.openFile(f);
120 - info("Reading input from "+f);
 129+ inputStream = inputHelper.open(path);
 130+ info("Reading input from "+path);
121131 usingStdin = false;
122132 }
123133 }
@@ -127,20 +137,20 @@
128138 @Override
129139 public void run() throws Exception {
130140 init();
131 - open();
 141+ open(1); // 0 = corpus, 1 = input, 2 = output
132142
133143 runTransfer(cursor);
134144
135145 close();
136146 }
137147
138 - protected void open() throws PersistenceException {
139 - cursor = openCursor();
140 - sink = openSink();
 148+ protected void open(int paramOffset) throws PersistenceException {
 149+ cursor = openCursor(paramOffset);
 150+ sink = openSink(paramOffset+1);
141151 }
142152
143 - protected abstract DataCursor<? extends I> openCursor() throws PersistenceException;
144 - protected abstract DataSink<? super O> openSink() throws PersistenceException;
 153+ protected abstract DataCursor<? extends I> openCursor(int paramIndex) throws PersistenceException;
 154+ protected abstract DataSink<? super O> openSink(int paramIndex) throws PersistenceException;
145155
146156 protected void init() throws Exception {
147157 // noop

Status & tagging log