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.openxml4j.exceptions.OpenXML4JException; 031import org.apache.poi.openxml4j.opc.OPCPackage; 032import org.apache.poi.ss.usermodel.Cell; 033import org.apache.poi.ss.usermodel.Row; 034import org.apache.poi.xssf.usermodel.XSSFCell; 035import org.apache.poi.xssf.usermodel.XSSFRow; 036import org.apache.poi.xssf.usermodel.XSSFSheet; 037import org.apache.poi.xssf.usermodel.XSSFWorkbook; 038import org.nuxeo.ecm.core.api.Blob; 039import org.nuxeo.ecm.core.api.Blobs; 040import org.nuxeo.ecm.core.api.blobholder.BlobHolder; 041import org.nuxeo.ecm.core.convert.api.ConversionException; 042import org.nuxeo.ecm.core.convert.cache.SimpleCachableBlobHolder; 043import org.nuxeo.ecm.core.convert.extension.Converter; 044 045public class XLX2TextConverter extends BaseOfficeXMLTextConverter implements Converter { 046 047 private static final Log log = LogFactory.getLog(XLX2TextConverter.class); 048 049 private static final String CELL_SEP = " "; 050 051 private static final String ROW_SEP = "\n"; 052 053 @Override 054 public BlobHolder convert(BlobHolder blobHolder, Map<String, Serializable> parameters) throws ConversionException { 055 056 StringBuilder sb = new StringBuilder(); 057 058 try { 059 Blob blob = blobHolder.getBlob(); 060 061 if (blob.getLength() > maxSize4POI) { 062 return runFallBackConverter(blobHolder, "xl/"); 063 } 064 065 try (InputStream stream = blob.getStream(); // 066 OPCPackage p = OPCPackage.open(stream); // 067 XSSFWorkbook workbook = new XSSFWorkbook(p)) { 068 for (int i = 0; i < workbook.getNumberOfSheets(); i++) { 069 XSSFSheet sheet = workbook.getSheetAt(i); 070 Iterator<Row> rows = sheet.rowIterator(); 071 while (rows.hasNext()) { 072 XSSFRow row = (XSSFRow) rows.next(); 073 Iterator<Cell> cells = row.cellIterator(); 074 while (cells.hasNext()) { 075 XSSFCell cell = (XSSFCell) cells.next(); 076 appendTextFromCell(cell, sb); 077 } 078 sb.append(ROW_SEP); 079 } 080 } 081 } 082 return new SimpleCachableBlobHolder(Blobs.createBlob(sb.toString())); 083 } catch (IOException | OpenXML4JException e) { 084 throw new ConversionException("Error during XLX2Text conversion", blobHolder, e); 085 } 086 } 087 088 protected void appendTextFromCell(XSSFCell cell, StringBuilder sb) { 089 String cellValue = null; 090 switch (cell.getCellType()) { 091 case NUMERIC: 092 cellValue = Double.toString(cell.getNumericCellValue()).trim(); 093 break; 094 case STRING: 095 cellValue = cell.getStringCellValue().trim(); 096 break; 097 } 098 099 if (cellValue != null && cellValue.length() > 0) { 100 sb.append(cellValue).append(CELL_SEP); 101 } 102 } 103 104}