Skip to content

Commit

Permalink
refs #44 書き込む際に正規表現でシートを名を指定する場合、書き込むシートが1つに絞り込めれば、そのシートに書き込むよう修正。
Browse files Browse the repository at this point in the history
  • Loading branch information
mygreen committed Jun 6, 2015
1 parent bb3dd3e commit b7519e8
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,18 @@ public class SheetNotFoundException extends XlsMapperException {
* @param sheetName シート名
*/
public SheetNotFoundException(final String sheetName) {
super(String.format("Cannot find sheet '%s'.", sheetName));
this(sheetName, String.format("Cannot find sheet '%s'.", sheetName));

}

/**
* 指定したシート名が見つからない場合に、そのシート名を指定するコンストラクタ。
* @param sheetName シート名
* @param message メッセージ
* @since 0.5
*/
public SheetNotFoundException(final String sheetName, final String message) {
super(message);
this.sheetName = sheetName;
}

Expand Down
36 changes: 25 additions & 11 deletions src/main/java/com/gh/mygreen/xlsmapper/XlsSaver.java
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ public void save(final InputStream templateXlsIn, final OutputStream xlsOut, fin
saveSheet(xlsSheet[0], beanObj, work);
} catch(SheetNotFoundException e) {
if(config.isIgnoreSheetNotFound()){
logger.warn("skip loading by not-found sheet.", e);
logger.warn("skip saving by not-found sheet.", e);
return;
} else {
throw e;
Expand Down Expand Up @@ -324,12 +324,10 @@ private org.apache.poi.ss.usermodel.Sheet[] findSheet(final Workbook book, final

} else if(sheetAnno.regex().length() > 0) {
// シート名(正規表現)をもとにして、取得する。
final String sheetNameValue;
String sheetNameValue = null;
FieldAdaptor sheetNameField = getSheetNameField(obj, annoReader);
if(sheetNameField != null) {
if(sheetNameField != null && sheetNameField.getValue(obj) != null) {
sheetNameValue = sheetNameField.getValue(obj).toString();
} else {
throw new SheetNotFoundException(sheetAnno.regex());
}

final Pattern pattern = Pattern.compile(sheetAnno.regex());
Expand All @@ -340,18 +338,34 @@ private org.apache.poi.ss.usermodel.Sheet[] findSheet(final Workbook book, final

// オブジェクト中の@XslSheetNameで値が設定されている場合、Excelファイル中の一致するシートを元にする比較する
if(Utils.isNotEmpty(sheetNameValue) && xlsSheet.getSheetName().equals(sheetNameValue)) {
matches.add(xlsSheet);
} else {
matches.add(xlsSheet);
return new org.apache.poi.ss.usermodel.Sheet[]{ xlsSheet };

}

matches.add(xlsSheet);
}
}

if(matches.isEmpty()) {
if(sheetNameValue != null && !matches.isEmpty()) {
// シート名が直接指定の場合
throw new SheetNotFoundException(sheetNameValue);

} else if(matches.isEmpty()) {
throw new SheetNotFoundException(sheetAnno.regex());

} else if(matches.size() == 1) {
// 1つのシートに絞り込めた場合
return new org.apache.poi.ss.usermodel.Sheet[]{ matches.get(0) };

} else {
// 複数のシートがヒットした場合
List<String> names = new ArrayList<>();
for(org.apache.poi.ss.usermodel.Sheet sheet : matches) {
names.add(sheet.getSheetName());
}
throw new SheetNotFoundException(sheetAnno.regex(),
String.format("found multiple sheet : %s.", Utils.join(names, ",")));
}

return matches.toArray(new org.apache.poi.ss.usermodel.Sheet[matches.size()]);
}

throw new AnnotationInvalidException("@XlsSheet requires name or number or regex parameter.", sheetAnno);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,14 @@ public void saveProcess(final Sheet sheet, final Object beansObj, final XlsLabel
final XlsMapperConfig config, final SavingWorkObject work) throws XlsMapperException {

final FindInfo info = findCell(sheet, anno, config);
if(info == null) {
/*
* ラベル用のセルが見つからない場合
* optional=falseの場合は、例外がスローされここには到達しない。
*/
return;
}

Utils.setPosition(info.position.x, info.position.y, beansObj, adaptor.getName());
Utils.setLabel(info.label, beansObj, adaptor.getName());

Expand Down

0 comments on commit b7519e8

Please sign in to comment.