embulk-parser-poi_excel 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.gitignore +12 -0
- data/LICENSE.txt +21 -0
- data/README.md +120 -0
- data/build.gradle +77 -0
- data/classpath/commons-codec-1.9.jar +0 -0
- data/classpath/embulk-parser-poi_excel-0.1.0.jar +0 -0
- data/classpath/embulk-standards-0.7.5.jar +0 -0
- data/classpath/poi-3.13.jar +0 -0
- data/classpath/poi-ooxml-3.13.jar +0 -0
- data/classpath/poi-ooxml-schemas-3.13.jar +0 -0
- data/classpath/stax-api-1.0.1.jar +0 -0
- data/classpath/xmlbeans-2.6.0.jar +0 -0
- data/gradle/wrapper/gradle-wrapper.jar +0 -0
- data/gradle/wrapper/gradle-wrapper.properties +6 -0
- data/gradlew +164 -0
- data/gradlew.bat +90 -0
- data/lib/embulk/guess/poi_excel.rb +61 -0
- data/lib/embulk/parser/poi_excel.rb +3 -0
- data/src/main/java/org/embulk/parser/poi_excel/PoiExcelColumnValueType.java +39 -0
- data/src/main/java/org/embulk/parser/poi_excel/PoiExcelParserPlugin.java +199 -0
- data/src/main/java/org/embulk/parser/poi_excel/visitor/AbstractPoiExcelCellAttributeVisitor.java +133 -0
- data/src/main/java/org/embulk/parser/poi_excel/visitor/PoiExcelCellCommentVisitor.java +68 -0
- data/src/main/java/org/embulk/parser/poi_excel/visitor/PoiExcelCellFontVisitor.java +117 -0
- data/src/main/java/org/embulk/parser/poi_excel/visitor/PoiExcelCellStyleVisitor.java +205 -0
- data/src/main/java/org/embulk/parser/poi_excel/visitor/PoiExcelCellVisitor.java +194 -0
- data/src/main/java/org/embulk/parser/poi_excel/visitor/PoiExcelColorVisitor.java +81 -0
- data/src/main/java/org/embulk/parser/poi_excel/visitor/PoiExcelColumnIndex.java +174 -0
- data/src/main/java/org/embulk/parser/poi_excel/visitor/PoiExcelColumnVisitor.java +146 -0
- data/src/main/java/org/embulk/parser/poi_excel/visitor/PoiExcelVisitorFactory.java +171 -0
- data/src/main/java/org/embulk/parser/poi_excel/visitor/PoiExcelVisitorValue.java +63 -0
- data/src/main/java/org/embulk/parser/poi_excel/visitor/embulk/BooleanCellVisitor.java +54 -0
- data/src/main/java/org/embulk/parser/poi_excel/visitor/embulk/CellVisitor.java +41 -0
- data/src/main/java/org/embulk/parser/poi_excel/visitor/embulk/DoubleCellVisitor.java +54 -0
- data/src/main/java/org/embulk/parser/poi_excel/visitor/embulk/LongCellVisitor.java +54 -0
- data/src/main/java/org/embulk/parser/poi_excel/visitor/embulk/StringCellVisitor.java +63 -0
- data/src/main/java/org/embulk/parser/poi_excel/visitor/embulk/TimestampCellVisitor.java +73 -0
- data/src/test/java/org/embulk/parser/EmbulkPluginTester.java +176 -0
- data/src/test/java/org/embulk/parser/EmbulkTestFileInputPlugin.java +83 -0
- data/src/test/java/org/embulk/parser/EmbulkTestOutputPlugin.java +193 -0
- data/src/test/java/org/embulk/parser/EmbulkTestParserConfig.java +51 -0
- data/src/test/java/org/embulk/parser/poi_excel/TestPoiExcelParserPlugin.java +187 -0
- data/src/test/java/org/embulk/parser/poi_excel/TestPoiExcelParserPlugin_cellComment.java +42 -0
- data/src/test/java/org/embulk/parser/poi_excel/TestPoiExcelParserPlugin_cellFont.java +125 -0
- data/src/test/java/org/embulk/parser/poi_excel/TestPoiExcelParserPlugin_cellStyle.java +132 -0
- data/src/test/java/org/embulk/parser/poi_excel/TestPoiExcelParserPlugin_columnNumber.java +188 -0
- data/src/test/resources/org/embulk/parser/poi_excel/test1.xls +0 -0
- metadata +118 -0
@@ -0,0 +1,199 @@
|
|
1
|
+
package org.embulk.parser.poi_excel;
|
2
|
+
|
3
|
+
import java.io.IOException;
|
4
|
+
import java.util.List;
|
5
|
+
|
6
|
+
import org.apache.poi.EncryptedDocumentException;
|
7
|
+
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
|
8
|
+
import org.apache.poi.ss.usermodel.Row;
|
9
|
+
import org.apache.poi.ss.usermodel.Sheet;
|
10
|
+
import org.apache.poi.ss.usermodel.Workbook;
|
11
|
+
import org.apache.poi.ss.usermodel.WorkbookFactory;
|
12
|
+
import org.embulk.config.Config;
|
13
|
+
import org.embulk.config.ConfigDefault;
|
14
|
+
import org.embulk.config.ConfigSource;
|
15
|
+
import org.embulk.config.Task;
|
16
|
+
import org.embulk.config.TaskSource;
|
17
|
+
import org.embulk.parser.poi_excel.visitor.PoiExcelColumnVisitor;
|
18
|
+
import org.embulk.parser.poi_excel.visitor.PoiExcelVisitorFactory;
|
19
|
+
import org.embulk.parser.poi_excel.visitor.PoiExcelVisitorValue;
|
20
|
+
import org.embulk.spi.Exec;
|
21
|
+
import org.embulk.spi.FileInput;
|
22
|
+
import org.embulk.spi.PageBuilder;
|
23
|
+
import org.embulk.spi.PageOutput;
|
24
|
+
import org.embulk.spi.ParserPlugin;
|
25
|
+
import org.embulk.spi.Schema;
|
26
|
+
import org.embulk.spi.SchemaConfig;
|
27
|
+
import org.embulk.spi.time.TimestampParser;
|
28
|
+
import org.embulk.spi.util.FileInputInputStream;
|
29
|
+
|
30
|
+
import com.google.common.base.Optional;
|
31
|
+
import com.ibm.icu.text.MessageFormat;
|
32
|
+
|
33
|
+
public class PoiExcelParserPlugin implements ParserPlugin {
|
34
|
+
|
35
|
+
public static final String TYPE = "poi_excel";
|
36
|
+
|
37
|
+
public interface PluginTask extends Task, TimestampParser.Task {
|
38
|
+
@Config("sheet")
|
39
|
+
@ConfigDefault("\"Sheet1\"")
|
40
|
+
public String getSheet();
|
41
|
+
|
42
|
+
@Config("skip_header_lines")
|
43
|
+
@ConfigDefault("0")
|
44
|
+
public int getSkipHeaderLines();
|
45
|
+
|
46
|
+
// search merged cell if cellType=BLANK
|
47
|
+
@Config("search_merged_cell")
|
48
|
+
@ConfigDefault("true")
|
49
|
+
public boolean getSearchMergedCell();
|
50
|
+
|
51
|
+
@Config("formula_replace")
|
52
|
+
@ConfigDefault("null")
|
53
|
+
public Optional<List<FormulaReplaceTask>> getFormulaReplace();
|
54
|
+
|
55
|
+
// true: set null if formula error
|
56
|
+
// false: throw exception if formula error
|
57
|
+
@Config("formula_error_null")
|
58
|
+
@ConfigDefault("false")
|
59
|
+
public boolean getFormulaErrorNull();
|
60
|
+
|
61
|
+
// true: set null if cell value error
|
62
|
+
// false: set error code if cell value error
|
63
|
+
@Config("cell_error_null")
|
64
|
+
@ConfigDefault("true")
|
65
|
+
public boolean getCellErrorNull();
|
66
|
+
|
67
|
+
@Config("flush_count")
|
68
|
+
@ConfigDefault("100")
|
69
|
+
public int getFlushCount();
|
70
|
+
|
71
|
+
@Config("columns")
|
72
|
+
public SchemaConfig getColumns();
|
73
|
+
}
|
74
|
+
|
75
|
+
public interface ColumnOptionTask extends Task {
|
76
|
+
|
77
|
+
/**
|
78
|
+
* @see PoiExcelColumnValueType
|
79
|
+
* @return value_type
|
80
|
+
*/
|
81
|
+
@Config("value")
|
82
|
+
@ConfigDefault("\"cell_value\"")
|
83
|
+
public String getValueType();
|
84
|
+
|
85
|
+
public void setValueTypeEnum(PoiExcelColumnValueType valueType);
|
86
|
+
|
87
|
+
public PoiExcelColumnValueType getValueTypeEnum();
|
88
|
+
|
89
|
+
public void setValueTypeSuffix(String suffix);
|
90
|
+
|
91
|
+
public String getValueTypeSuffix();
|
92
|
+
|
93
|
+
// A,B,... or number(1 origin)
|
94
|
+
@Config("column_number")
|
95
|
+
@ConfigDefault("null")
|
96
|
+
public Optional<String> getColumnNumber();
|
97
|
+
|
98
|
+
public void setColumnIndex(int index);
|
99
|
+
|
100
|
+
public int getColumnIndex();
|
101
|
+
|
102
|
+
@Config("search_merged_cell")
|
103
|
+
@ConfigDefault("null")
|
104
|
+
public Optional<Boolean> getSearchMergedCell();
|
105
|
+
|
106
|
+
@Config("formula_replace")
|
107
|
+
@ConfigDefault("null")
|
108
|
+
public Optional<List<FormulaReplaceTask>> getFormulaReplace();
|
109
|
+
|
110
|
+
@Config("formula_error_null")
|
111
|
+
@ConfigDefault("null")
|
112
|
+
public Optional<Boolean> getFormulaErrorNull();
|
113
|
+
|
114
|
+
@Config("cell_error_null")
|
115
|
+
@ConfigDefault("null")
|
116
|
+
public Optional<Boolean> getCellErrorNull();
|
117
|
+
|
118
|
+
// use when value_type=cell_style, cell_font, ...
|
119
|
+
@Config("attribute_name")
|
120
|
+
@ConfigDefault("null")
|
121
|
+
public Optional<List<String>> getAttributeName();
|
122
|
+
}
|
123
|
+
|
124
|
+
public interface FormulaReplaceTask extends Task {
|
125
|
+
|
126
|
+
@Config("regex")
|
127
|
+
public String getRegex();
|
128
|
+
|
129
|
+
// replace string
|
130
|
+
// use variable: "${row}"
|
131
|
+
@Config("to")
|
132
|
+
public String getTo();
|
133
|
+
}
|
134
|
+
|
135
|
+
@Override
|
136
|
+
public void transaction(ConfigSource config, ParserPlugin.Control control) {
|
137
|
+
PluginTask task = config.loadConfig(PluginTask.class);
|
138
|
+
|
139
|
+
Schema schema = task.getColumns().toSchema();
|
140
|
+
|
141
|
+
control.run(task.dump(), schema);
|
142
|
+
}
|
143
|
+
|
144
|
+
@Override
|
145
|
+
public void run(TaskSource taskSource, Schema schema, FileInput input, PageOutput output) {
|
146
|
+
PluginTask task = taskSource.loadTask(PluginTask.class);
|
147
|
+
|
148
|
+
try (FileInputInputStream is = new FileInputInputStream(input)) {
|
149
|
+
while (is.nextFile()) {
|
150
|
+
Workbook workbook;
|
151
|
+
try {
|
152
|
+
workbook = WorkbookFactory.create(is);
|
153
|
+
} catch (IOException | EncryptedDocumentException | InvalidFormatException e) {
|
154
|
+
throw new RuntimeException(e);
|
155
|
+
}
|
156
|
+
|
157
|
+
String sheetName = task.getSheet();
|
158
|
+
Sheet sheet = workbook.getSheet(sheetName);
|
159
|
+
if (sheet == null) {
|
160
|
+
throw new RuntimeException(MessageFormat.format("not found sheet={0}", sheetName));
|
161
|
+
}
|
162
|
+
|
163
|
+
run(task, schema, sheet, output);
|
164
|
+
}
|
165
|
+
}
|
166
|
+
}
|
167
|
+
|
168
|
+
protected void run(PluginTask task, Schema schema, Sheet sheet, PageOutput output) {
|
169
|
+
int skipHeaderLines = task.getSkipHeaderLines();
|
170
|
+
final int flushCount = task.getFlushCount();
|
171
|
+
|
172
|
+
try (final PageBuilder pageBuilder = new PageBuilder(Exec.getBufferAllocator(), schema, output)) {
|
173
|
+
PoiExcelVisitorFactory factory = newPoiExcelVisitorFactory(task, sheet, pageBuilder);
|
174
|
+
PoiExcelColumnVisitor visitor = factory.getPoiExcelColumnVisitor();
|
175
|
+
|
176
|
+
int count = 0;
|
177
|
+
for (final Row row : sheet) {
|
178
|
+
if (row.getRowNum() < skipHeaderLines) {
|
179
|
+
continue;
|
180
|
+
}
|
181
|
+
|
182
|
+
visitor.setRow(row);
|
183
|
+
schema.visitColumns(visitor);
|
184
|
+
pageBuilder.addRecord();
|
185
|
+
|
186
|
+
if (++count >= flushCount) {
|
187
|
+
pageBuilder.flush();
|
188
|
+
count = 0;
|
189
|
+
}
|
190
|
+
}
|
191
|
+
pageBuilder.finish();
|
192
|
+
}
|
193
|
+
}
|
194
|
+
|
195
|
+
protected PoiExcelVisitorFactory newPoiExcelVisitorFactory(PluginTask task, Sheet sheet, PageBuilder pageBuilder) {
|
196
|
+
PoiExcelVisitorValue visitorValue = new PoiExcelVisitorValue(task, sheet, pageBuilder);
|
197
|
+
return new PoiExcelVisitorFactory(visitorValue);
|
198
|
+
}
|
199
|
+
}
|
data/src/main/java/org/embulk/parser/poi_excel/visitor/AbstractPoiExcelCellAttributeVisitor.java
ADDED
@@ -0,0 +1,133 @@
|
|
1
|
+
package org.embulk.parser.poi_excel.visitor;
|
2
|
+
|
3
|
+
import java.text.MessageFormat;
|
4
|
+
import java.util.Collection;
|
5
|
+
import java.util.LinkedHashMap;
|
6
|
+
import java.util.List;
|
7
|
+
import java.util.Map;
|
8
|
+
import java.util.TreeMap;
|
9
|
+
import java.util.TreeSet;
|
10
|
+
|
11
|
+
import org.apache.poi.ss.usermodel.Cell;
|
12
|
+
import org.apache.poi.ss.usermodel.Color;
|
13
|
+
import org.embulk.parser.poi_excel.PoiExcelParserPlugin.ColumnOptionTask;
|
14
|
+
import org.embulk.parser.poi_excel.visitor.embulk.CellVisitor;
|
15
|
+
import org.embulk.spi.Column;
|
16
|
+
import org.embulk.spi.PageBuilder;
|
17
|
+
import org.embulk.spi.type.StringType;
|
18
|
+
|
19
|
+
import com.fasterxml.jackson.core.JsonProcessingException;
|
20
|
+
import com.fasterxml.jackson.databind.ObjectMapper;
|
21
|
+
import com.google.common.base.Optional;
|
22
|
+
|
23
|
+
public abstract class AbstractPoiExcelCellAttributeVisitor<A> {
|
24
|
+
|
25
|
+
protected final PoiExcelVisitorValue visitorValue;
|
26
|
+
protected final PageBuilder pageBuilder;
|
27
|
+
|
28
|
+
public AbstractPoiExcelCellAttributeVisitor(PoiExcelVisitorValue visitorValue) {
|
29
|
+
this.visitorValue = visitorValue;
|
30
|
+
this.pageBuilder = visitorValue.getPageBuilder();
|
31
|
+
}
|
32
|
+
|
33
|
+
public void visit(Column column, ColumnOptionTask option, Cell cell, CellVisitor visitor) {
|
34
|
+
A source = getAttributeSource(column, option, cell);
|
35
|
+
if (source == null) {
|
36
|
+
pageBuilder.setNull(column);
|
37
|
+
return;
|
38
|
+
}
|
39
|
+
|
40
|
+
String suffix = option.getValueTypeSuffix();
|
41
|
+
if (suffix != null) {
|
42
|
+
visitKey(column, option, suffix, cell, source, visitor);
|
43
|
+
} else {
|
44
|
+
visitJson(column, option, cell, source, visitor);
|
45
|
+
}
|
46
|
+
}
|
47
|
+
|
48
|
+
protected abstract A getAttributeSource(Column column, ColumnOptionTask option, Cell cell);
|
49
|
+
|
50
|
+
private void visitKey(Column column, ColumnOptionTask option, String key, Cell cell, A source, CellVisitor visitor) {
|
51
|
+
Object value = getAttributeValue(column, option, cell, source, key);
|
52
|
+
if (value == null) {
|
53
|
+
pageBuilder.setNull(column);
|
54
|
+
} else if (value instanceof String) {
|
55
|
+
visitor.visitCellValueString(column, source, (String) value);
|
56
|
+
} else if (value instanceof Long) {
|
57
|
+
visitor.visitValueLong(column, source, (Long) value);
|
58
|
+
} else if (value instanceof Boolean) {
|
59
|
+
visitor.visitCellValueBoolean(column, source, (Boolean) value);
|
60
|
+
} else if (value instanceof Double) {
|
61
|
+
visitor.visitCellValueNumeric(column, source, (Double) value);
|
62
|
+
} else {
|
63
|
+
throw new IllegalStateException(MessageFormat.format("unsupported conversion. type={0}, value={1}", value
|
64
|
+
.getClass().getName(), value));
|
65
|
+
}
|
66
|
+
}
|
67
|
+
|
68
|
+
private void visitJson(Column column, ColumnOptionTask option, Cell cell, A source, CellVisitor visitor) {
|
69
|
+
Map<String, Object> result;
|
70
|
+
|
71
|
+
Optional<List<String>> nameOption = option.getAttributeName();
|
72
|
+
if (nameOption.isPresent()) {
|
73
|
+
result = new LinkedHashMap<>();
|
74
|
+
|
75
|
+
List<String> list = nameOption.get();
|
76
|
+
for (String key : list) {
|
77
|
+
Object value = getAttributeValue(column, option, cell, source, key);
|
78
|
+
result.put(key, value);
|
79
|
+
}
|
80
|
+
} else {
|
81
|
+
result = new TreeMap<>();
|
82
|
+
|
83
|
+
Collection<String> keys = getAttributeSupplierMap().keySet();
|
84
|
+
for (String key : keys) {
|
85
|
+
if (acceptKey(key)) {
|
86
|
+
Object value = getAttributeValue(column, option, cell, source, key);
|
87
|
+
result.put(key, value);
|
88
|
+
}
|
89
|
+
}
|
90
|
+
}
|
91
|
+
|
92
|
+
String json;
|
93
|
+
try {
|
94
|
+
ObjectMapper mapper = new ObjectMapper();
|
95
|
+
json = mapper.writeValueAsString(result);
|
96
|
+
} catch (JsonProcessingException e) {
|
97
|
+
throw new RuntimeException(e);
|
98
|
+
}
|
99
|
+
|
100
|
+
visitor.visitCellValueString(column, cell, json);
|
101
|
+
}
|
102
|
+
|
103
|
+
protected boolean acceptKey(String key) {
|
104
|
+
return true;
|
105
|
+
}
|
106
|
+
|
107
|
+
private Object getAttributeValue(Column column, ColumnOptionTask option, Cell cell, A source, String key) {
|
108
|
+
Map<String, AttributeSupplier<A>> map = getAttributeSupplierMap();
|
109
|
+
AttributeSupplier<A> supplier = map.get(key.toLowerCase());
|
110
|
+
if (supplier == null) {
|
111
|
+
throw new UnsupportedOperationException(MessageFormat.format(
|
112
|
+
"unsupported attribute name={0}, choose in {1}", key, new TreeSet<>(map.keySet())));
|
113
|
+
}
|
114
|
+
Object value = supplier.get(column, cell, source);
|
115
|
+
|
116
|
+
if (value instanceof Color) {
|
117
|
+
int rgb = PoiExcelColorVisitor.getRGB((Color) value);
|
118
|
+
if (column.getType() instanceof StringType) {
|
119
|
+
value = String.format("%06x", rgb);
|
120
|
+
} else {
|
121
|
+
value = (long) rgb;
|
122
|
+
}
|
123
|
+
}
|
124
|
+
return value;
|
125
|
+
}
|
126
|
+
|
127
|
+
// @FunctionalInterface
|
128
|
+
protected static interface AttributeSupplier<A> {
|
129
|
+
public Object get(Column column, Cell cell, A source);
|
130
|
+
}
|
131
|
+
|
132
|
+
protected abstract Map<String, AttributeSupplier<A>> getAttributeSupplierMap();
|
133
|
+
}
|
@@ -0,0 +1,68 @@
|
|
1
|
+
package org.embulk.parser.poi_excel.visitor;
|
2
|
+
|
3
|
+
import java.util.Collections;
|
4
|
+
import java.util.HashMap;
|
5
|
+
import java.util.Map;
|
6
|
+
|
7
|
+
import org.apache.poi.ss.usermodel.Cell;
|
8
|
+
import org.apache.poi.ss.usermodel.Comment;
|
9
|
+
import org.apache.poi.ss.usermodel.RichTextString;
|
10
|
+
import org.embulk.parser.poi_excel.PoiExcelParserPlugin.ColumnOptionTask;
|
11
|
+
import org.embulk.spi.Column;
|
12
|
+
|
13
|
+
public class PoiExcelCellCommentVisitor extends AbstractPoiExcelCellAttributeVisitor<Comment> {
|
14
|
+
|
15
|
+
public PoiExcelCellCommentVisitor(PoiExcelVisitorValue visitorValue) {
|
16
|
+
super(visitorValue);
|
17
|
+
}
|
18
|
+
|
19
|
+
@Override
|
20
|
+
protected Comment getAttributeSource(Column column, ColumnOptionTask option, Cell cell) {
|
21
|
+
return cell.getCellComment();
|
22
|
+
}
|
23
|
+
|
24
|
+
@Override
|
25
|
+
protected Map<String, AttributeSupplier<Comment>> getAttributeSupplierMap() {
|
26
|
+
return SUPPLIER_MAP;
|
27
|
+
}
|
28
|
+
|
29
|
+
protected static final Map<String, AttributeSupplier<Comment>> SUPPLIER_MAP;
|
30
|
+
static {
|
31
|
+
Map<String, AttributeSupplier<Comment>> map = new HashMap<>(32);
|
32
|
+
map.put("author", new AttributeSupplier<Comment>() {
|
33
|
+
@Override
|
34
|
+
public Object get(Column column, Cell cell, Comment comment) {
|
35
|
+
return comment.getAuthor();
|
36
|
+
}
|
37
|
+
});
|
38
|
+
map.put("column", new AttributeSupplier<Comment>() {
|
39
|
+
@Override
|
40
|
+
public Object get(Column column, Cell cell, Comment comment) {
|
41
|
+
return (long) comment.getColumn();
|
42
|
+
}
|
43
|
+
});
|
44
|
+
map.put("row", new AttributeSupplier<Comment>() {
|
45
|
+
@Override
|
46
|
+
public Object get(Column column, Cell cell, Comment comment) {
|
47
|
+
return (long) comment.getRow();
|
48
|
+
}
|
49
|
+
});
|
50
|
+
map.put("is_visible", new AttributeSupplier<Comment>() {
|
51
|
+
@Override
|
52
|
+
public Object get(Column column, Cell cell, Comment comment) {
|
53
|
+
return comment.isVisible();
|
54
|
+
}
|
55
|
+
});
|
56
|
+
map.put("string", new AttributeSupplier<Comment>() {
|
57
|
+
@Override
|
58
|
+
public Object get(Column column, Cell cell, Comment comment) {
|
59
|
+
RichTextString rich = comment.getString();
|
60
|
+
return rich.getString();
|
61
|
+
}
|
62
|
+
});
|
63
|
+
|
64
|
+
// TODO getClientAnchor
|
65
|
+
|
66
|
+
SUPPLIER_MAP = Collections.unmodifiableMap(map);
|
67
|
+
}
|
68
|
+
}
|
@@ -0,0 +1,117 @@
|
|
1
|
+
package org.embulk.parser.poi_excel.visitor;
|
2
|
+
|
3
|
+
import java.util.Collections;
|
4
|
+
import java.util.HashMap;
|
5
|
+
import java.util.Map;
|
6
|
+
|
7
|
+
import org.apache.poi.ss.usermodel.Cell;
|
8
|
+
import org.apache.poi.ss.usermodel.CellStyle;
|
9
|
+
import org.apache.poi.ss.usermodel.Font;
|
10
|
+
import org.apache.poi.ss.usermodel.Workbook;
|
11
|
+
import org.apache.poi.xssf.usermodel.XSSFFont;
|
12
|
+
import org.embulk.parser.poi_excel.PoiExcelParserPlugin.ColumnOptionTask;
|
13
|
+
import org.embulk.spi.Column;
|
14
|
+
|
15
|
+
public class PoiExcelCellFontVisitor extends AbstractPoiExcelCellAttributeVisitor<Font> {
|
16
|
+
|
17
|
+
public PoiExcelCellFontVisitor(PoiExcelVisitorValue visitorValue) {
|
18
|
+
super(visitorValue);
|
19
|
+
}
|
20
|
+
|
21
|
+
@Override
|
22
|
+
protected Font getAttributeSource(Column column, ColumnOptionTask option, Cell cell) {
|
23
|
+
CellStyle style = cell.getCellStyle();
|
24
|
+
short index = style.getFontIndex();
|
25
|
+
Workbook book = visitorValue.getSheet().getWorkbook();
|
26
|
+
return book.getFontAt(index);
|
27
|
+
}
|
28
|
+
|
29
|
+
@Override
|
30
|
+
protected Map<String, AttributeSupplier<Font>> getAttributeSupplierMap() {
|
31
|
+
return SUPPLIER_MAP;
|
32
|
+
}
|
33
|
+
|
34
|
+
protected static final Map<String, AttributeSupplier<Font>> SUPPLIER_MAP;
|
35
|
+
static {
|
36
|
+
Map<String, AttributeSupplier<Font>> map = new HashMap<>(32);
|
37
|
+
map.put("font_name", new AttributeSupplier<Font>() {
|
38
|
+
@Override
|
39
|
+
public Object get(Column column, Cell cell, Font font) {
|
40
|
+
return font.getFontName();
|
41
|
+
}
|
42
|
+
});
|
43
|
+
map.put("font_height", new AttributeSupplier<Font>() {
|
44
|
+
@Override
|
45
|
+
public Object get(Column column, Cell cell, Font font) {
|
46
|
+
return (long) font.getFontHeight();
|
47
|
+
}
|
48
|
+
});
|
49
|
+
map.put("font_height_in_points", new AttributeSupplier<Font>() {
|
50
|
+
@Override
|
51
|
+
public Object get(Column column, Cell cell, Font font) {
|
52
|
+
return (long) font.getFontHeightInPoints();
|
53
|
+
}
|
54
|
+
});
|
55
|
+
map.put("italic", new AttributeSupplier<Font>() {
|
56
|
+
@Override
|
57
|
+
public Object get(Column column, Cell cell, Font font) {
|
58
|
+
return font.getItalic();
|
59
|
+
}
|
60
|
+
});
|
61
|
+
map.put("strikeout", new AttributeSupplier<Font>() {
|
62
|
+
@Override
|
63
|
+
public Object get(Column column, Cell cell, Font font) {
|
64
|
+
return font.getStrikeout();
|
65
|
+
}
|
66
|
+
});
|
67
|
+
map.put("color", new AttributeSupplier<Font>() {
|
68
|
+
@Override
|
69
|
+
public Object get(Column column, Cell cell, Font font) {
|
70
|
+
if (font instanceof XSSFFont) {
|
71
|
+
return ((XSSFFont) font).getXSSFColor();
|
72
|
+
} else {
|
73
|
+
Workbook book = cell.getSheet().getWorkbook();
|
74
|
+
short color = font.getColor();
|
75
|
+
return PoiExcelColorVisitor.getHssfColor(book, color);
|
76
|
+
}
|
77
|
+
}
|
78
|
+
});
|
79
|
+
map.put("type_offset", new AttributeSupplier<Font>() {
|
80
|
+
@Override
|
81
|
+
public Object get(Column column, Cell cell, Font font) {
|
82
|
+
return (long) font.getTypeOffset();
|
83
|
+
}
|
84
|
+
});
|
85
|
+
map.put("underline", new AttributeSupplier<Font>() {
|
86
|
+
@Override
|
87
|
+
public Object get(Column column, Cell cell, Font font) {
|
88
|
+
return (long) font.getUnderline();
|
89
|
+
}
|
90
|
+
});
|
91
|
+
map.put("char_set", new AttributeSupplier<Font>() {
|
92
|
+
@Override
|
93
|
+
public Object get(Column column, Cell cell, Font font) {
|
94
|
+
return (long) font.getCharSet();
|
95
|
+
}
|
96
|
+
});
|
97
|
+
map.put("index", new AttributeSupplier<Font>() {
|
98
|
+
@Override
|
99
|
+
public Object get(Column column, Cell cell, Font font) {
|
100
|
+
return (long) font.getIndex();
|
101
|
+
}
|
102
|
+
});
|
103
|
+
map.put("boldweight", new AttributeSupplier<Font>() {
|
104
|
+
@Override
|
105
|
+
public Object get(Column column, Cell cell, Font font) {
|
106
|
+
return (long) font.getBoldweight();
|
107
|
+
}
|
108
|
+
});
|
109
|
+
map.put("bold", new AttributeSupplier<Font>() {
|
110
|
+
@Override
|
111
|
+
public Object get(Column column, Cell cell, Font font) {
|
112
|
+
return font.getBold();
|
113
|
+
}
|
114
|
+
});
|
115
|
+
SUPPLIER_MAP = Collections.unmodifiableMap(map);
|
116
|
+
}
|
117
|
+
}
|