001/* 002 * (C) Copyright 2006-2007 Nuxeo SA (http://nuxeo.com/) and others. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 * 016 * Contributors: 017 * Nuxeo - initial API and implementation 018 * 019 */ 020package org.nuxeo.ecm.core.convert.plugins.text.extractors; 021 022import java.io.IOException; 023import java.io.InputStream; 024import java.io.Serializable; 025import java.util.Iterator; 026import java.util.Map; 027 028import org.apache.commons.logging.Log; 029import org.apache.commons.logging.LogFactory; 030import org.apache.poi.hssf.usermodel.HSSFCell; 031import org.apache.poi.hssf.usermodel.HSSFRow; 032import org.apache.poi.hssf.usermodel.HSSFSheet; 033import org.apache.poi.hssf.usermodel.HSSFWorkbook; 034import org.apache.poi.poifs.filesystem.POIFSFileSystem; 035import org.apache.poi.ss.usermodel.Row; 036import org.nuxeo.ecm.core.api.Blobs; 037import org.nuxeo.ecm.core.api.blobholder.BlobHolder; 038import org.nuxeo.ecm.core.convert.api.ConversionException; 039import org.nuxeo.ecm.core.convert.cache.SimpleCachableBlobHolder; 040import org.nuxeo.ecm.core.convert.extension.Converter; 041import org.nuxeo.ecm.core.convert.extension.ConverterDescriptor; 042 043public class XL2TextConverter implements Converter { 044 045 private static final Log log = LogFactory.getLog(XL2TextConverter.class); 046 047 private static final String CELL_SEP = " "; 048 049 private static final String ROW_SEP = "\n\n"; 050 051 @Override 052 public BlobHolder convert(BlobHolder blobHolder, Map<String, Serializable> parameters) throws ConversionException { 053 054 StringBuilder sb = new StringBuilder(); 055 try (InputStream stream = blobHolder.getBlob().getStream(); // 056 POIFSFileSystem fs = new POIFSFileSystem(stream); // 057 HSSFWorkbook workbook = new HSSFWorkbook(fs)) { 058 for (int i = 0; i < workbook.getNumberOfSheets(); i++) { 059 HSSFSheet sheet = workbook.getSheetAt(i); 060 Iterator<Row> rows = sheet.rowIterator(); 061 while (rows.hasNext()) { 062 HSSFRow row = (HSSFRow) rows.next(); 063 Iterator<?> cells = row.cellIterator(); 064 while (cells.hasNext()) { 065 HSSFCell cell = (HSSFCell) cells.next(); 066 appendTextFromCell(cell, sb); 067 sb.append(CELL_SEP); 068 } 069 sb.append(ROW_SEP); 070 } 071 } 072 return new SimpleCachableBlobHolder(Blobs.createBlob(sb.toString())); 073 } catch (IOException e) { 074 throw new ConversionException("Error during XL2Text conversion", blobHolder, e); 075 } 076 } 077 078 protected void appendTextFromCell(HSSFCell cell, StringBuilder sb) { 079 String cellValue = null; 080 switch (cell.getCellType()) { 081 case NUMERIC: 082 cellValue = Double.toString(cell.getNumericCellValue()).trim(); 083 break; 084 case STRING: 085 cellValue = cell.getStringCellValue().trim().replaceAll("\n", " "); 086 break; 087 } 088 089 if (cellValue != null && cellValue.length() > 0) { 090 sb.append(cellValue); 091 } 092 } 093 094 @Override 095 public void init(ConverterDescriptor descriptor) { 096 } 097 098}