Adapt to changes in Argeo Connect
[gpl/argeo-suite.git] / org.argeo.suite.workbench.rap / src / org / argeo / suite / workbench / commands / JxlUtils.java
1 package org.argeo.suite.workbench.commands;
2
3 import java.io.IOException;
4 import java.io.InputStream;
5
6 import org.argeo.connect.ConnectException;
7 import org.argeo.eclipse.ui.EclipseUiUtils;
8
9 import jxl.Cell;
10 import jxl.CellType;
11 import jxl.JXLException;
12 import jxl.Sheet;
13 import jxl.Workbook;
14 import jxl.WorkbookSettings;
15
16 /** Centralise useful methods to simplify development with JXL library */
17 class JxlUtils {
18
19 public static boolean isEmptyCell(Sheet sheet, int x, int y) {
20 Cell cell = sheet.getCell(x, y);
21 CellType type = cell.getType();
22 return type == CellType.EMPTY;
23 }
24
25 public static String getStringValue(Sheet sheet, int x, int y) {
26 Cell cell = sheet.getCell(x, y);
27 CellType type = cell.getType();
28 String stringValue = null;
29 if (type == CellType.LABEL || type == CellType.NUMBER)
30 stringValue = cell.getContents();
31 return stringValue;
32 }
33
34 public static String getCompulsoryStringValue(Sheet sheet, int x, int y) {
35 Cell cell = sheet.getCell(x, y);
36 CellType type = cell.getType();
37 String stringValue = null;
38 if (type == CellType.LABEL)
39 stringValue = cell.getContents();
40 else if (type == CellType.NUMBER)
41 stringValue = cell.getContents();
42 if (EclipseUiUtils.isEmpty(stringValue))
43 throw new ConnectException("No name defined at [" + x + "," + y + "], cannot parse indicator file");
44 return stringValue;
45 }
46
47 public static Double getNumberValue(Sheet sheet, int x, int y) {
48 Cell cell = sheet.getCell(x, y);
49 CellType type = cell.getType();
50 if (type == CellType.NUMBER)
51 return new Double(cell.getContents());
52 else if (type == CellType.EMPTY)
53 return null;
54 else
55 throw new ConnectException("Not a number at [" + x + "," + y + "]: " + type.toString());
56 }
57
58 public static Sheet getOnlySheet(InputStream in, String encoding) throws IOException {
59 Workbook wkb = toWorkbook(in, encoding);
60 Sheet sheet = wkb.getSheet(0);
61 return sheet;
62 }
63
64 public static Sheet getSheet(InputStream in, String encoding, int index) throws IOException {
65 Workbook wkb = toWorkbook(in, encoding);
66 return wkb.getSheet(index);
67 }
68
69 public static Workbook toWorkbook(InputStream in, String encoding) throws IOException {
70 try {
71 WorkbookSettings ws = new WorkbookSettings();
72 ws.setEncoding(encoding);
73 return Workbook.getWorkbook(in, ws);
74 } catch (JXLException e) {
75 throw new ConnectException("Unable to open XLS file", e);
76 }
77 }
78
79 // Prevents instantiation
80 private JxlUtils() {
81 }
82 }