Update of /cvsroot/commonjava/commonjava-projects/commonjava-diff/cli/src/java/org/commonjava/diff/cli
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv17552/cli/src/java/org/commonjava/diff/cli
Added Files:
Main.java
Log Message:
o Separated Main into subproject called 'cli' to remove requirement for commonjava-console in main diff engine project,
o Removed dependency on FileSizeConstants, and hence commonjava-util
o Copied DescriptiveStats from commonjava-math to . (commonjava-diff) to remove dependency on that lib.
o Re-tested engine, and all's well.
--- NEW FILE: Main.java ---
/* Created on Jan 16, 2004 */
package org.commonjava.diff.cli;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import org.commonjava.console.Command;
import org.commonjava.console.CommandLine;
import org.commonjava.console.CommandLineException;
import org.commonjava.console.option.Arg;
import org.commonjava.console.option.Option;
import org.commonjava.console.option.OptionFormat;
import org.commonjava.console.option.SingleArgOptionalOption;
import org.commonjava.console.option.SingleArgRequiredOption;
/**
* @author jdcasey
*/
public class Main extends Command{
private static final Option OLD = new SingleArgRequiredOption('o', "old", OptionFormat.FILE_FORMAT, "Old file.");
private static final Option NEW = new SingleArgRequiredOption('n', "new", OptionFormat.FILE_FORMAT, "New file.");
private static final Option MAX_LINE_LEN = new SingleArgOptionalOption('l', "maxlength", OptionFormat.NUMBER_FORMAT, "Maximum Line Length.");
private static final Option[] TEMPLATE = {OLD, NEW, MAX_LINE_LEN};
public static final int CODE_OK = 0;
public static final int CODE_OLD_FILE_MISSING = -1;
public static final int CODE_OLD_FILE_ERROR = -2;
public static final int CODE_NEW_FILE_MISSING = -3;
public static final int CODE_NEW_FILE_ERROR = -4;
public static final int CODE_ERROR_COMPUTING_DIFF = -5;
public static void main(String[] args) {
try {
Main main = new Main();
main.setIn(System.in);
main.setOut(System.out);
main.setErr(System.err);
main.parseArgs(args);
main.executeCommand();
}
catch (CommandLineException e) {
e.printStackTrace();
}
}
/**
* @param description
*/
public Main() {
super("Compute the differences between two files.");
addCommandTemplate(TEMPLATE);
}
/* (non-Javadoc)
* @see org.commonjava.console.Command#executeCommand()
*/
public int executeCommand() throws CommandLineException {
CommandLine cmdLine = getCommandLine();
Arg oldArg = (Arg)cmdLine.getOption(OLD.getLongName());
Arg newArg = (Arg)cmdLine.getOption(NEW.getLongName());
Arg lineLen = (Arg)cmdLine.getOption(MAX_LINE_LEN.getLongName());
File old = (File)oldArg.getValue();
File neo = (File)newArg.getValue();
Long maxLen = null;
if(lineLen.hasValue()) {
maxLen = (Long)lineLen.getValue();
}
FileInputStream oldIn = null;
MappedByteBuffer oldMap = null;
try {
oldIn = new FileInputStream(old);
FileChannel oldChannel = oldIn.getChannel();
oldMap = oldChannel.map(FileChannel.MapMode.READ_ONLY, 0, oldChannel.size());
}
catch (FileNotFoundException e) {
e.printStackTrace(getErr());
getErr().flush();
return CODE_OLD_FILE_MISSING;
}
catch (IOException e) {
e.printStackTrace(getErr());
getErr().flush();
return CODE_OLD_FILE_ERROR;
}
FileInputStream newIn = null;
MappedByteBuffer newMap = null;
try {
newIn = new FileInputStream(neo);
FileChannel newChannel = newIn.getChannel();
newMap = newChannel.map(FileChannel.MapMode.READ_ONLY, 0, newChannel.size());
}
catch (FileNotFoundException e) {
e.printStackTrace(getErr());
getErr().flush();
return CODE_NEW_FILE_MISSING;
}
catch (IOException e) {
e.printStackTrace(getErr());
getErr().flush();
return CODE_NEW_FILE_ERROR;
}
DiffOperation[] ops = null;
try {
if (maxLen == null) {
ops = DiffEngine.getInstance().compute(oldMap, newMap);
}
else {
ops = DiffEngine.getInstance().compute(oldMap, newMap, maxLen.intValue());
}
}
catch (IOException e) {
e.printStackTrace(getErr());
getErr().flush();
return CODE_ERROR_COMPUTING_DIFF;
}
return CODE_OK;
}
}
|