CellLinkCellConverterFactory.java

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

  2. import java.util.Optional;

  3. import org.apache.poi.common.usermodel.HyperlinkType;
  4. import org.apache.poi.ss.usermodel.Cell;
  5. import org.apache.poi.ss.usermodel.CreationHelper;
  6. import org.apache.poi.ss.usermodel.Hyperlink;

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

  20. /**
  21.  * {@link CellLink}型を処理する{@link CellConverter}を作成するためのファクトリクラス。
  22.  *
  23.  * @since 2.0
  24.  * @author T.TSUCHIE
  25.  *
  26.  */
  27. public class CellLinkCellConverterFactory extends CellConverterFactorySupport<CellLink>
  28.         implements CellConverterFactory<CellLink>{
  29.    
  30.     @Override
  31.     public CellLinkCellConverter create(final FieldAccessor field, final Configuration config) {
  32.        
  33.         final CellLinkCellConverter cellConverter = new CellLinkCellConverter(field, config);
  34.         setupCellConverter(cellConverter, field, config);
  35.        
  36.         return cellConverter;
  37.     }
  38.    
  39.     @Override
  40.     protected void setupCustom(final BaseCellConverter<CellLink> cellConverter, final FieldAccessor field,
  41.             Configuration config) {
  42.         // 何もしない
  43.     }
  44.    
  45.     @Override
  46.     protected TextFormatter<CellLink> createTextFormatter(final FieldAccessor field, final Configuration config) {
  47.        
  48.         return new TextFormatter<CellLink>() {
  49.            
  50.             @Override
  51.             public CellLink parse(String text) throws TextParseException {
  52.                 return new CellLink(text, text);
  53.             }
  54.            
  55.             @Override
  56.             public String format(CellLink value) {
  57.                 return String.format("[%s](%s)", value.getLabel(), value.getLink());
  58.             }
  59.         };
  60.     }
  61.    
  62.     public class CellLinkCellConverter extends BaseCellConverter<CellLink> {
  63.        
  64.         private CellLinkCellConverter(final FieldAccessor field, final Configuration config) {
  65.             super(field, config);
  66.            
  67.         }
  68.        
  69.         @Override
  70.         protected CellLink parseCell(final Cell evaluatedCell, final String formattedValue) throws TypeBindException {
  71.            
  72.             final Optional<XlsTrim> trimAnno = getField().getAnnotation(XlsTrim.class);
  73.            
  74.             final Hyperlink hyperlink = POIUtils.getHyperlink(evaluatedCell);
  75.             if(hyperlink != null) {
  76.                 // リンクが設定されているセルは、リンクの内容を値とする
  77.                 final String address = Utils.trim(hyperlink.getAddress(), trimAnno);
  78.                 final String label = Utils.trim(formattedValue, trimAnno);
  79.                
  80.                 return new CellLink(address, label);
  81.                
  82.             } else if(!formattedValue.isEmpty()) {
  83.                 // リンクがないセルは、セルの文字列を値とする
  84.                 return new CellLink(null, formattedValue);
  85.             }
  86.            
  87.             return null;
  88.         }
  89.        
  90.         @Override
  91.         protected boolean isEmptyCell(final String formattedValue, final Cell cell) {
  92.             if(POIUtils.getHyperlink(cell) != null) {
  93.                 return false;
  94.             }
  95.            
  96.             return super.isEmptyCell(formattedValue, cell);
  97.         }
  98.        
  99.         @Override
  100.         protected void setupCell(final Cell cell, final Optional<CellLink> cellValue) throws TypeBindException {
  101.            
  102.             // 既存のハイパーリンクを削除
  103.             // 削除しないと、Excelの見た目上はリンクは変わっているが、データ上は2重にリンクが設定されている。
  104.             cell.removeHyperlink();
  105.            
  106.             if(cellValue.isPresent()) {
  107.                 final CreationHelper helper = cell.getSheet().getWorkbook().getCreationHelper();
  108.                 final HyperlinkType type = POIUtils.judgeLinkType(cellValue.get().getLink());
  109.                 final Hyperlink link = helper.createHyperlink(type);
  110.                 link.setAddress(cellValue.get().getLink());
  111.                 cell.setHyperlink(link);
  112.                
  113.                 cell.setCellValue(cellValue.get().getLabel());
  114.                
  115.             } else {
  116.                 cell.setBlank();
  117.             }
  118.            
  119.         }
  120.        
  121.     }
  122.    
  123. }