1 package org.jahia.services.fileextraction; 2 3 import java.io.ByteArrayInputStream ; 4 import java.io.ByteArrayOutputStream ; 5 import java.io.InputStream ; 6 import java.util.HashMap ; 7 import java.util.Iterator ; 8 import java.util.Map ; 9 10 import org.apache.commons.io.CopyUtils; 11 import org.apache.slide.common.PropertyName; 12 import org.apache.slide.extractor.OfficeExtractor; 13 import org.jahia.services.sites.JahiaSitesSlideService; 14 15 16 public abstract class JahiaOfficeExtractor implements FileExtractor { 17 18 private static org.apache.log4j.Logger logger = 19 org.apache.log4j.Logger.getLogger (JahiaOfficeExtractor.class); 20 21 22 30 public ExtractedDocument getExtractedDocument( 31 String path, 32 long lastModified, 33 InputStream fileStream) 34 throws Exception { 35 return this.getExtractedDocument(path,lastModified,fileStream,null); 36 } 37 38 47 public ExtractedDocument getExtractedDocument( 48 String path, 49 long lastModified, 50 InputStream fileStream, 51 String charSet) 52 throws Exception { 53 54 ByteArrayOutputStream out = new ByteArrayOutputStream (); 56 CopyUtils.copy(fileStream, out); 57 out.flush(); 58 byte[] contents = out.toByteArray(); 59 ExtractedDocument extDoc = this.getPropertiesExtractedDocument(new ByteArrayInputStream (contents)); 60 extDoc.setContent(this.getContentAsString(path,lastModified,new ByteArrayInputStream (contents),charSet)); 61 return extDoc; 62 63 } 64 65 73 public abstract String getContentAsString(String path, 74 long lastModified, 75 InputStream fileStream) 76 throws Exception ; 77 78 85 public abstract String getContentAsString(String path, 86 long lastModified, 87 InputStream fileStream, 88 String charSet) throws Exception ; 89 90 public Map extract(InputStream content){ 91 Map map = null; 92 try { 93 OfficeExtractor officeExtractor = new OfficeExtractor("","",""); 94 officeExtractor.configure(((JahiaSitesSlideService)JahiaSitesSlideService.getInstance()).getConfiguration().getConfiguration("office-property-mapping")); 95 map = officeExtractor.extract(content); 96 } catch ( Throwable t ){ 97 logger.debug("Exception extraction Office properties", t); 98 } 99 100 if ( map == null ){ 101 map = new HashMap (); 102 } 103 return map; 104 } 105 106 114 public ExtractedDocument getPropertiesExtractedDocument(InputStream fileStream) 115 throws Exception { 116 ExtractedDocumentImpl extDoc = new ExtractedDocumentImpl(); 117 Map properties = this.extract(fileStream); 118 Iterator iterator = properties.keySet().iterator(); 119 Object objKey = null; 120 String key = null; 121 PropertyName propName = null; 122 while ( iterator.hasNext() ){ 123 key = null; 124 objKey = iterator.next(); 125 if ( objKey instanceof String ){ 126 key = (String )objKey; 127 extDoc.setProperty(key,properties.get(key)); 128 } else { 129 propName = (PropertyName)objKey; 130 extDoc.setProperty(propName.getName(),properties.get(propName)); 134 } 135 } 137 return extDoc; 138 } 139 140 } 141 | Popular Tags |