1 17 package org.alfresco.repo.content.transform; 18 19 import java.io.InputStream ; 20 import java.io.OutputStream ; 21 import java.util.Map ; 22 23 import org.alfresco.repo.content.MimetypeMap; 24 import org.alfresco.service.cmr.repository.ContentReader; 25 import org.alfresco.service.cmr.repository.ContentWriter; 26 import org.apache.commons.logging.Log; 27 import org.apache.commons.logging.LogFactory; 28 import org.apache.poi.hssf.usermodel.HSSFCell; 29 import org.apache.poi.hssf.usermodel.HSSFRow; 30 import org.apache.poi.hssf.usermodel.HSSFSheet; 31 import org.apache.poi.hssf.usermodel.HSSFWorkbook; 32 33 48 public class PoiHssfContentTransformer extends AbstractContentTransformer 49 { 50 53 private static final String LINE_BREAK = "\r\n"; 54 55 58 public double getReliability(String sourceMimetype, String targetMimetype) 59 { 60 if (!MimetypeMap.MIMETYPE_EXCEL.equals(sourceMimetype) || 61 !MimetypeMap.MIMETYPE_TEXT_PLAIN.equals(targetMimetype)) 62 { 63 return 0.0; 65 } 66 else 67 { 68 return 1.0; 69 } 70 } 71 72 public void transformInternal(ContentReader reader, ContentWriter writer, Map <String , Object > options) 73 throws Exception 74 { 75 InputStream is = reader.getContentInputStream(); 76 OutputStream os = writer.getContentOutputStream(); 77 String encoding = writer.getEncoding(); 78 try 79 { 80 HSSFWorkbook workbook = new HSSFWorkbook(is); 82 int sheetCount = workbook.getNumberOfSheets(); 84 for (int i = 0; i < sheetCount; i++) 86 { 87 HSSFSheet sheet = workbook.getSheetAt(i); 88 String sheetName = workbook.getSheetName(i); 89 writeSheet(os, sheet, encoding); 90 PoiHssfContentTransformer.writeString(os, encoding, LINE_BREAK, false); 92 PoiHssfContentTransformer.writeString(os, encoding, "End of sheet: " + sheetName, true); 93 PoiHssfContentTransformer.writeString(os, encoding, LINE_BREAK, false); 94 PoiHssfContentTransformer.writeString(os, encoding, LINE_BREAK, false); 95 } 96 } 97 finally 98 { 99 if (is != null) 100 { 101 try { is.close(); } catch (Throwable e) {} 102 } 103 if (os != null) 104 { 105 try { os.close(); } catch (Throwable e) {} 106 } 107 } 108 } 109 110 113 private void writeSheet(OutputStream os, HSSFSheet sheet, String encoding) throws Exception 114 { 115 int rows = sheet.getLastRowNum(); 116 for (int i = 0; i <= rows; i++) 118 { 119 HSSFRow row = sheet.getRow(i); 120 if (row != null) 121 { 122 writeRow(os, row, encoding); 123 } 124 if (i < rows) 126 { 127 PoiHssfContentTransformer.writeString(os, encoding, LINE_BREAK, false); 128 } 129 } 130 } 131 132 private void writeRow(OutputStream os, HSSFRow row, String encoding) throws Exception 133 { 134 short firstCellNum = row.getFirstCellNum(); 135 short lastCellNum = row.getLastCellNum(); 136 for (short i = 0; i < firstCellNum; i++) 138 { 139 PoiHssfContentTransformer.writeString(os, encoding, ",", false); } 141 for (short i = 0; i <= lastCellNum; i++) 143 { 144 HSSFCell cell = row.getCell(i); 145 if (cell != null) 146 { 147 StringBuilder sb = new StringBuilder (10); 148 switch (cell.getCellType()) 149 { 150 case HSSFCell.CELL_TYPE_BLANK: 151 break; 153 case HSSFCell.CELL_TYPE_BOOLEAN: 154 sb.append(cell.getBooleanCellValue()); 155 break; 156 case HSSFCell.CELL_TYPE_ERROR: 157 sb.append("ERROR"); 158 break; 159 case HSSFCell.CELL_TYPE_FORMULA: 160 double dataNumber = cell.getNumericCellValue(); 161 if (Double.isNaN(dataNumber)) 162 { 163 sb.append(cell.getStringCellValue()); 165 } 166 else 167 { 168 sb.append(dataNumber); 170 } 171 break; 172 case HSSFCell.CELL_TYPE_NUMERIC: 173 sb.append(cell.getNumericCellValue()); 174 break; 175 case HSSFCell.CELL_TYPE_STRING: 176 sb.append(cell.getStringCellValue()); 177 break; 178 default: 179 throw new RuntimeException ("Unknown HSSF cell type: " + cell); 180 } 181 String data = sb.toString(); 182 PoiHssfContentTransformer.writeString(os, encoding, data, true); 183 } 184 if (i < lastCellNum) 186 { 187 PoiHssfContentTransformer.writeString(os, encoding, ",", false); 188 } 189 } 190 } 191 192 206 public static void writeString(OutputStream os, String encoding, String value, boolean isData) throws Exception 207 { 208 if (value == null) 209 { 210 return; 212 } 213 int dataLength = value.length(); 214 if (dataLength == 0) 215 { 216 return; 218 } 219 220 StringBuilder sb = new StringBuilder (dataLength + 5); for (int i = 0; i < dataLength; i++) 223 { 224 char currentChar = value.charAt(i); 225 if (currentChar == '\"') { 227 sb.append("\""); } 229 sb.append(currentChar); 231 } 232 if (isData) 234 { 235 sb.insert(0, "\""); 236 sb.append("\""); 237 } 238 value = sb.toString(); 240 241 byte[] bytes = null; 242 if (encoding == null) 243 { 244 bytes = value.getBytes(); 246 } 247 else 248 { 249 bytes = value.getBytes(encoding); 250 } 251 os.write(bytes); 253 } 255 } 256 | Popular Tags |