Permalink
Browse files

Rename package names from excel specific to general spreadsheet.

  • Loading branch information...
1 parent 7ed38e6 commit 0a1013826646fb452fd39a142748a0d547b19eea @na-ka-na committed Dec 12, 2013
View
@@ -8,5 +8,14 @@
<classpathentry kind="lib" path="lib/ivy/jar/poi-3.9.jar"/>
<classpathentry kind="lib" path="lib/ivy/jar/poi-ooxml-3.9.jar" sourcepath="lib/ivy/source/poi-ooxml-3.9.jar"/>
<classpathentry kind="lib" path="lib/ivy/jar/poi-ooxml-schemas-3.9.jar"/>
+ <classpathentry kind="lib" path="lib/ivy/jar/odfdom-java-0.8.8-incubating.jar"/>
+ <classpathentry kind="lib" path="lib/ivy/jar/simple-odf-0.7-incubating.jar" sourcepath="lib/ivy/source/simple-odf-0.7-incubating.jar">
+ <attributes>
+ <attribute name="javadoc_location" value="jar:platform:/resource/ExcelCompare/lib/ivy/javadoc/simple-odf-0.7-incubating.jar!/"/>
+ </attributes>
+ </classpathentry>
+ <classpathentry kind="lib" path="lib/ivy/jar/taglets-0.8.8-incubating.jar"/>
+ <classpathentry kind="lib" path="lib/ivy/jar/xercesImpl-2.9.1.jar"/>
+ <classpathentry kind="lib" path="lib/ivy/jar/xml-apis-1.3.04.jar"/>
<classpathentry kind="output" path="bin"/>
</classpath>
View
@@ -14,8 +14,8 @@ testcfg.dir = cfg/test
installer.staging = install_stage
# Classes
-mainclass = com.ka.excelcmp.ExcelCompare
-test.mainclass = com.ka.excelcmp.ExcelCompareTest
+mainclass = com.ka.spreadsheet.diff.SpreadSheetDiffer
+test.mainclass = com.ka.spreadsheet.diff.SpreadSheetDifferTest
# Version
full.version = 0.1
View
@@ -5,5 +5,7 @@
<exclude name="xml-apis" />
<exclude name="commons-codec" />
</dependency>
+ <dependency org="org.apache.odftoolkit" name="simple-odf" rev="0.7-incubating" >
+ </dependency>
</dependencies>
</ivy-module>
View
@@ -3,4 +3,4 @@ dir=`readlink -f $0|xargs dirname`
else
dir=`dirname $0`
fi
-java -ea -Xmx512m -cp "$dir/dist/*" com.ka.excelcmp.ExcelCompare "$@"
+java -ea -Xmx512m -cp "$dir/dist/*" com.ka.spreadsheet.diff.SpreadSheetDiffer "$@"
View
@@ -1,5 +1,5 @@
@ECHO OFF
SETLOCAL
set dirname=%~dp0
-java -ea -Xmx512m -cp "%dirname%\dist\*;" com.ka.excelcmp.ExcelCompare %*
+java -ea -Xmx512m -cp "%dirname%\dist\*;" com.ka.spreadsheet.diff.SpreadSheetDiffer %*
ENDLOCAL
@@ -1,8 +1,8 @@
-package com.ka.excelcmp;
+package com.ka.spreadsheet.diff;
-import static com.ka.excelcmp.SpreadSheetUtils.CELL_INTERNAL_TO_USER;
-import static com.ka.excelcmp.SpreadSheetUtils.COL_INTERNAL_TO_USER;
-import static com.ka.excelcmp.SpreadSheetUtils.ROW_INTERNAL_TO_USER;
+import static com.ka.spreadsheet.diff.SpreadSheetUtils.CELL_INTERNAL_TO_USER;
+import static com.ka.spreadsheet.diff.SpreadSheetUtils.COL_INTERNAL_TO_USER;
+import static com.ka.spreadsheet.diff.SpreadSheetUtils.ROW_INTERNAL_TO_USER;
public class CellPos implements Comparable<CellPos>{
@@ -1,4 +1,4 @@
-package com.ka.excelcmp;
+package com.ka.spreadsheet.diff;
public interface DiffReportCallback {
@@ -1,4 +1,4 @@
-package com.ka.excelcmp;
+package com.ka.spreadsheet.diff;
import java.util.Iterator;
@@ -1,11 +1,11 @@
-package com.ka.excelcmp;
+package com.ka.spreadsheet.diff;
import java.util.ArrayList;
import java.util.List;
-import static com.ka.excelcmp.SpreadSheetUtils.CELL_USER_TO_INTERNAL;
-import static com.ka.excelcmp.SpreadSheetUtils.COL_USER_TO_INTERNAL;
-import static com.ka.excelcmp.SpreadSheetUtils.ROW_USER_TO_INTERNAL;
+import static com.ka.spreadsheet.diff.SpreadSheetUtils.CELL_USER_TO_INTERNAL;
+import static com.ka.spreadsheet.diff.SpreadSheetUtils.COL_USER_TO_INTERNAL;
+import static com.ka.spreadsheet.diff.SpreadSheetUtils.ROW_USER_TO_INTERNAL;
public class SheetIgnores {
@@ -1,4 +1,4 @@
-package com.ka.excelcmp;
+package com.ka.spreadsheet.diff;
import java.io.File;
import java.util.HashMap;
import java.util.LinkedHashSet;
@@ -7,6 +7,7 @@
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
+import org.odftoolkit.simple.SpreadsheetDocument;
public class SpreadSheetDiffer {
@@ -52,24 +53,36 @@ static String usage(){
* TODO: Better display of results
*/
- public static void main(String[] args) throws Exception{
+ public static void main(String[] args) {
+ int ret = -1;
+ try {
+ ret = doMain(args);
+ } catch (Exception e) {
+ e.printStackTrace(System.err);
+ }
+ System.exit(ret);
+ }
+
+ public static int doMain(String[] args) throws Exception {
if ((args.length < 2)){
System.out.println(usage());
- return;
+ return -1;
}
final File file1 = new File(args[0]);
final File file2 = new File(args[1]);
- Workbook wb1 = WorkbookFactory.create(file1);
- Workbook wb2 = WorkbookFactory.create(file2);
- SpreadSheetExcel sse1 = new SpreadSheetExcel(wb1);
- SpreadSheetExcel sse2 = new SpreadSheetExcel(wb2);
+ if (!verifyFile(file1) || !verifyFile(file2)) {
+ return -1;
+ }
+
+ ISpreadSheet ss1 = loadSpreadSheet(file1);
+ ISpreadSheet ss2 = loadSpreadSheet(file2);
Map<String,SheetIgnores> sheetIgnores1 = parseSheetIgnores(args, "--ignore1");
Map<String,SheetIgnores> sheetIgnores2 = parseSheetIgnores(args, "--ignore2");
- SpreadSheetIterator wi1 = new SpreadSheetIterator(sse1, sheetIgnores1);
- SpreadSheetIterator wi2 = new SpreadSheetIterator(sse2, sheetIgnores2);
+ SpreadSheetIterator wi1 = new SpreadSheetIterator(ss1, sheetIgnores1);
+ SpreadSheetIterator wi2 = new SpreadSheetIterator(ss2, sheetIgnores2);
DiffReportCallback call = new DiffReportCallback() {
Set<Object> sheets = new LinkedHashSet<Object>();
@@ -131,7 +144,7 @@ private void reportS(String what, Set<Object> sheets, Set<Object> rows, Set<Obje
boolean differ = doDiff(wi1, wi2, call);
- System.exit(differ ? 1 : 0);
+ return differ ? 1 : 0;
}
private static boolean doDiff(SpreadSheetIterator wi1, SpreadSheetIterator wi2, DiffReportCallback call){
@@ -212,4 +225,43 @@ else if ((c1==null) && (c2!=null)){
}
return ret;
}
+
+ private static boolean verifyFile(File file) {
+ if (!file.exists()) {
+ System.err.println("File file: " + file.getAbsolutePath() + " does not exist.");
+ return false;
+ }
+ if (!file.canRead()) {
+ System.err.println("File file: " + file.getAbsolutePath() + " not readable.");
+ return false;
+ }
+ if (!file.isFile()) {
+ System.err.println("File file: " + file.getAbsolutePath() + " is not a file.");
+ return false;
+ }
+ return true;
+ }
+
+ private static ISpreadSheet loadSpreadSheet(File file) throws Exception {
+ // assume file is excel by default
+ Exception excelReadException = null;
+ try {
+ Workbook workbook = WorkbookFactory.create(file);
+ return new SpreadSheetExcel(workbook);
+ } catch (Exception e) {
+ excelReadException = e;
+ }
+ Exception odfReadException = null;
+ try {
+ SpreadsheetDocument spreadsheetDocument = SpreadsheetDocument.loadDocument(file);
+ return new SpreadSheetOdf(spreadsheetDocument);
+ } catch (Exception e) {
+ odfReadException = e;
+ }
+ if (file.getName().matches(".*\\.ods.*")) {
+ throw odfReadException;
+ } else {
+ throw excelReadException;
+ }
+ }
}
@@ -1,4 +1,4 @@
-package com.ka.excelcmp;
+package com.ka.spreadsheet.diff;
import java.util.Iterator;
@@ -1,4 +1,4 @@
-package com.ka.excelcmp;
+package com.ka.spreadsheet.diff;
import java.util.Iterator;
import java.util.Map;
@@ -0,0 +1,146 @@
+package com.ka.spreadsheet.diff;
+
+import java.util.Iterator;
+
+import org.odftoolkit.simple.SpreadsheetDocument;
+import org.odftoolkit.simple.table.Cell;
+import org.odftoolkit.simple.table.Row;
+import org.odftoolkit.simple.table.Table;
+
+public class SpreadSheetOdf implements ISpreadSheet {
+
+ private final SpreadsheetDocument spreadsheetDocument;
+
+ public SpreadSheetOdf(SpreadsheetDocument spreadsheetDocument) {
+ this.spreadsheetDocument = spreadsheetDocument;
+ }
+
+ @Override
+ public Iterator<ISheet> getSheetIterator() {
+ return new Iterator<ISheet>() {
+
+ private int currSheetIdx = 0;
+
+ @Override
+ public boolean hasNext() {
+ return currSheetIdx < spreadsheetDocument.getSheetCount();
+ }
+
+ @Override
+ public ISheet next() {
+ Table table = spreadsheetDocument.getSheetByIndex(currSheetIdx);
+ SheetOdf sheetOdf = new SheetOdf(table, currSheetIdx);
+ currSheetIdx++;
+ return sheetOdf;
+ }
+
+ @Override
+ public void remove() {
+ throw new UnsupportedOperationException();
+ }
+ };
+ }
+}
+
+class SheetOdf implements ISheet {
+
+ private final Table table;
+ private final int sheetIdx;
+
+ public SheetOdf(Table table, int sheetIdx) {
+ this.table = table;
+ this.sheetIdx = sheetIdx;
+ }
+
+ @Override
+ public String getName() {
+ return table.getTableName();
+ }
+
+ @Override
+ public int getSheetIndex() {
+ return sheetIdx;
+ }
+
+ @Override
+ public Iterator<IRow> getRowIterator() {
+ final Iterator<Row> rowIterator = table.getRowIterator();
+ return new Iterator<IRow>() {
+
+ @Override
+ public boolean hasNext() {
+ return rowIterator.hasNext();
+ }
+
+ @Override
+ public IRow next() {
+ return new RowOdf(rowIterator.next());
+ }
+
+ @Override
+ public void remove() {
+ throw new UnsupportedOperationException();
+ }
+ };
+ }
+}
+
+class RowOdf implements IRow {
+
+ private final Row row;
+
+ public RowOdf(Row row) {
+ this.row = row;
+ }
+
+ @Override
+ public int getRowIndex() {
+ return row.getRowIndex();
+ }
+
+ @Override
+ public Iterator<ICell> getCellIterator() {
+ return new Iterator<ICell>() {
+ private int currCellIdx = 0;
+
+ @Override
+ public boolean hasNext() {
+ return currCellIdx < row.getCellCount();
+ }
+
+ @Override
+ public ICell next() {
+ return new CellOdf(row.getCellByIndex(currCellIdx++));
+ }
+
+ @Override
+ public void remove() {
+ throw new UnsupportedOperationException();
+ }
+ };
+ }
+}
+
+class CellOdf implements ICell {
+
+ private final Cell cell;
+
+ public CellOdf(Cell cell) {
+ this.cell = cell;
+ }
+
+ @Override
+ public int getRowIndex() {
+ return cell.getRowIndex();
+ }
+
+ @Override
+ public int getColumnIndex() {
+ return cell.getColumnIndex();
+ }
+
+ @Override
+ public String getStringValue() {
+ return cell.getStringValue();
+ }
+}
Oops, something went wrong.

0 comments on commit 0a10138

Please sign in to comment.