1 package org.apache.slide.extractor; 2 3 import java.io.InputStream ; 4 import java.util.*; 5 6 import org.apache.poi.hpsf.*; 7 import org.apache.poi.poifs.eventfilesystem.*; 8 import org.apache.slide.util.conf.Configurable; 9 import org.apache.slide.util.conf.Configuration; 10 import org.apache.slide.util.conf.ConfigurationException; 11 12 16 public class OfficeExtractor extends AbstractPropertyExtractor implements Configurable { 17 protected List instructions = new ArrayList(); 18 protected Map propertyMap = new HashMap(); 19 20 public OfficeExtractor(String uri, String contentType, String namespace) { 21 super(uri, contentType, namespace); 22 } 23 24 public Map extract(InputStream content) throws ExtractorException { 25 OfficePropertiesListener listener = new OfficePropertiesListener(); 26 try { 27 POIFSReader r = new POIFSReader(); 28 r.registerListener(listener); 29 r.read(content); 30 } catch (Exception e) { 31 throw new ExtractorException("Exception while extracting properties in OfficeExtractor"); 32 } 33 return listener.getProperties(); 34 } 35 36 class OfficePropertiesListener implements POIFSReaderListener { 37 38 private HashMap properties = new HashMap(); 39 40 public Map getProperties() { 41 return properties; 42 } 43 44 public void processPOIFSReaderEvent(POIFSReaderEvent event) { 45 PropertySet ps = null; 46 try { 47 ps = PropertySetFactory.create(event.getStream()); 48 } catch (NoPropertySetStreamException ex) { 49 return; 50 } catch (Exception ex) { 51 throw new RuntimeException ("Property set stream \"" + event.getPath() + event.getName() + "\": " + ex); 52 } 53 String eventName = event.getName().trim(); 54 final long sectionCount = ps.getSectionCount(); 55 List sections = ps.getSections(); 56 int nr = 0; 57 for (Iterator i = sections.iterator(); i.hasNext();) { 58 Section sec = (Section) i.next(); 59 int propertyCount = sec.getPropertyCount(); 60 Property[] props = sec.getProperties(); 61 for (int i2 = 0; i2 < props.length; i2++) { 62 Property p = props[i2]; 63 int id = p.getID(); 64 long type = p.getType(); 65 Object value = p.getValue(); 66 String key = eventName + "-" + nr + "-" + id; 67 if ( propertyMap.containsKey(key) ) { 68 properties.put(propertyMap.get(key), value); 69 } 70 } 71 } 72 } 73 } 74 75 public void configure(Configuration configuration) throws ConfigurationException { 76 Enumeration instructions = configuration.getConfigurations("instruction"); 77 while (instructions.hasMoreElements()) { 78 Configuration extract = (Configuration)instructions.nextElement(); 79 String property = extract.getAttribute("property"); 80 String id = extract.getAttribute("id"); 81 propertyMap.put(id, property); 82 } 83 } 84 } | Popular Tags |