URICellConverterFactory.java

  1. package com.gh.mygreen.xlsmapper.cellconverter.impl;

  2. import java.net.URI;
  3. import java.net.URISyntaxException;
  4. import java.util.Optional;

  5. import org.apache.poi.common.usermodel.HyperlinkType;
  6. import org.apache.poi.ss.usermodel.Cell;
  7. import org.apache.poi.ss.usermodel.CreationHelper;
  8. import org.apache.poi.ss.usermodel.Hyperlink;

  9. import com.gh.mygreen.xlsmapper.Configuration;
  10. import com.gh.mygreen.xlsmapper.annotation.XlsTrim;
  11. import com.gh.mygreen.xlsmapper.cellconverter.BaseCellConverter;
  12. import com.gh.mygreen.xlsmapper.cellconverter.CellConverter;
  13. import com.gh.mygreen.xlsmapper.cellconverter.CellConverterFactory;
  14. import com.gh.mygreen.xlsmapper.cellconverter.CellConverterFactorySupport;
  15. import com.gh.mygreen.xlsmapper.cellconverter.TypeBindException;
  16. import com.gh.mygreen.xlsmapper.fieldaccessor.FieldAccessor;
  17. import com.gh.mygreen.xlsmapper.textformatter.TextFormatter;
  18. import com.gh.mygreen.xlsmapper.textformatter.TextParseException;
  19. import com.gh.mygreen.xlsmapper.util.POIUtils;
  20. import com.gh.mygreen.xlsmapper.util.Utils;

  21. /**
  22.  * {@link URI}型を処理する{@link CellConverter}を作成するためのファクトリクラス。
  23.  *
  24.  * @since 2.0
  25.  * @author T.TSUCHIE
  26.  *
  27.  */
  28. public class URICellConverterFactory extends CellConverterFactorySupport<URI>
  29.         implements CellConverterFactory<URI>{
  30.    
  31.     @Override
  32.     public URICellConverter create(final FieldAccessor field, final Configuration config) {
  33.        
  34.         final URICellConverter cellConverter = new URICellConverter(field, config);
  35.         setupCellConverter(cellConverter, field, config);
  36.        
  37.         return cellConverter;
  38.     }
  39.    
  40.     @Override
  41.     protected void setupCustom(final BaseCellConverter<URI> cellConverter, final FieldAccessor field,
  42.             Configuration config) {
  43.         // 何もしない
  44.     }
  45.    
  46.     @Override
  47.     protected TextFormatter<URI> createTextFormatter(final FieldAccessor field, final Configuration config) {
  48.         return new TextFormatter<URI>() {

  49.             @Override
  50.             public URI parse(String text) throws TextParseException {
  51.                 try {
  52.                     return new URI(text);
  53.                 } catch (URISyntaxException e) {
  54.                     throw new TextParseException(text, URI.class);
  55.                 }
  56.             }
  57.            
  58.             @Override
  59.             public String format(final URI value) {
  60.                 return value.toString();
  61.             }
  62.         };
  63.     }
  64.    
  65.     public class URICellConverter extends BaseCellConverter<URI> {
  66.        
  67.         private URICellConverter(final FieldAccessor field, final Configuration config) {
  68.             super(field, config);
  69.         }
  70.        
  71.         @Override
  72.         protected URI parseCell(final Cell evaluatedCell, final String formattedValue) throws TypeBindException {
  73.            
  74.             final Hyperlink hyperlink = POIUtils.getHyperlink(evaluatedCell);
  75.             if(hyperlink != null) {
  76.                 // リンクが設定されているセルは、リンクの内容を値とする
  77.                 Optional<XlsTrim> trimAnno = getField().getAnnotation(XlsTrim.class);
  78.                 final String address = Utils.trim(hyperlink.getAddress(), trimAnno);
  79.                
  80.                 try {
  81.                     return new URI(address);
  82.                 } catch (URISyntaxException e) {
  83.                     throw newTypeBindExceptionOnParse(e, evaluatedCell, address);
  84.                 }
  85.                
  86.             } else if(!formattedValue.isEmpty()) {
  87.                 // リンクがないセルは、セルの文字列を値とする
  88.                 try {
  89.                     return this.textFormatter.parse(formattedValue);
  90.                    
  91.                 } catch(TextParseException e) {
  92.                     throw newTypeBindExceptionOnParse(e, evaluatedCell, formattedValue);
  93.                 }
  94.             }
  95.            
  96.             return null;
  97.            
  98.         }
  99.        
  100.         @Override
  101.         protected boolean isEmptyCell(final String formattedValue, final Cell cell) {
  102.             if(POIUtils.getHyperlink(cell) != null) {
  103.                 return false;
  104.             }
  105.            
  106.             return super.isEmptyCell(formattedValue, cell);
  107.         }
  108.        
  109.         @Override
  110.         protected void setupCell(final Cell cell, final Optional<URI> cellValue) throws TypeBindException {
  111.            
  112.             // 既存のハイパーリンクを削除
  113.             // 削除しないと、Excelの見た目上はリンクは変わっているが、データ上は2重にリンクが設定されている。
  114.             cell.removeHyperlink();
  115.            
  116.             if(cellValue.isPresent()) {
  117.                 final CreationHelper helper = cell.getSheet().getWorkbook().getCreationHelper();
  118.                 final Hyperlink link = helper.createHyperlink(HyperlinkType.URL);
  119.                 link.setAddress(cellValue.get().toString());
  120.                 cell.setHyperlink(link);
  121.                
  122.                 cell.setCellValue(cellValue.get().toString());
  123.                
  124.             } else {
  125.                 cell.setBlank();
  126.             }
  127.            
  128.         }
  129.        
  130.     }
  131.    
  132. }