1 package org.enhydra.snapper.parsers; 2 3 import java.io.FileInputStream ; 4 import java.util.Collection ; 5 import java.util.Iterator ; 6 import java.util.Map ; 7 import java.util.Set ; 8 9 import org.apache.poi.poifs.filesystem.DocumentInputStream; 10 11 import org.apache.poi.poifs.eventfilesystem.POIFSReader; 12 import org.apache.poi.poifs.eventfilesystem.POIFSReaderEvent; 13 import org.apache.poi.poifs.eventfilesystem.POIFSReaderListener; 14 import java.io.InputStream ; 15 16 public class PowerParser 17 implements org.enhydra.snapper.api.Parser, POIFSReaderListener { 18 private static final int TWO8 = 256; 19 private static final int TWO16 = 65536; 20 private static final int[] SIGNATURE = { 21 159, 15, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 168, 15}; 22 private static final int SPACE = 32; 23 private String fileName; 24 private String parsedText; 25 public String title; 26 private String properties = ""; 27 public Map customproperties; 28 29 30 31 public void parse() { 32 FileInputStream fis = null; 33 try { 34 POIFSReader r = new POIFSReader(); 36 DocumentPOIFSReaderListener docpoi = new DocumentPOIFSReaderListener(); 37 r.registerListener(docpoi, 38 "\005DocumentSummaryInformation"); 39 40 MyPOIFSReaderListener mypoi = new MyPOIFSReaderListener(); 41 r.registerListener(mypoi, 42 "\005SummaryInformation"); 43 44 r.registerListener(this, "PowerPoint Document"); 45 fis = new FileInputStream (fileName); 46 r.read(fis); 47 48 title = mypoi.getTitle(); 49 50 if (docpoi.getCustomProperties() == null) { 51 properties = ""; 52 return; 53 } 54 customproperties = docpoi.getCustomProperties(); 55 createString(customproperties); 56 fis.close(); 57 fis = null; 58 59 } 60 catch (Exception e) { 61 ParserManager.logger.info("***** File could not be parsed: " + fileName); 62 } 64 65 finally { 66 if (fis != null){ 67 try{ 68 fis.close(); 69 } catch (Exception ex){}; 70 fis = null; 71 } 72 } 73 74 } 75 76 public void setFileName(String fileName) { 77 this.fileName = fileName; 78 } 79 80 public String getParsedText() { 81 return parsedText; 82 } 83 84 public String getTitle() { 85 return title; 86 } 87 88 public String getProperties() { 89 return properties; 90 } 91 92 private void createString(Map customproperties) { 93 Collection values = customproperties.values(); 94 Iterator valuesIterator = values.iterator(); 95 properties = ""; 96 Set keys = customproperties.keySet(); 97 Iterator keysIterator = keys.iterator(); 98 99 for (Iterator it = customproperties.entrySet().iterator(); it.hasNext(); ) { 100 Map.Entry entry = (Map.Entry ) it.next(); 101 properties += entry.getKey().toString() + " " + entry.getValue() + " "; 102 } 103 104 } 105 106 107 public String parse(InputStream stream) throws java.io.IOException { 108 StringBuffer content = new StringBuffer (); 109 int letter = 0; 110 int found = 0; 111 boolean separator = true; 112 int zeroCounter = 0; 113 int tobeRead = 0; 114 StringBuffer word = new StringBuffer (); 115 while ( (letter = stream.read()) != -1) { 116 if ( ( (found < SIGNATURE.length) && (letter == SIGNATURE[found])) || 118 found == 6) { 119 found++; 120 } 121 else if ( (found <= SIGNATURE.length) && (letter == SIGNATURE[0])) { 122 found = 1; 123 } 124 else if (found < 14) { 125 found = 0; 126 } 127 else if (found == 14) { 128 found++; 129 tobeRead = letter; 130 } 131 else if (found == 15) { 132 found++; 133 tobeRead += TWO8 * letter; 134 } 135 else if (found == 16) { 136 tobeRead += TWO16 * letter; 137 found++; 138 } 139 else if (found == 17) { 140 found++; 141 } 142 else if (tobeRead > 0) { 143 tobeRead--; 144 if (letter == 13) { 145 word.append('\n'); 146 } 147 else if (letter < 32) { 148 word.append(' '); 149 } 150 else { 151 word.append( (char) letter); 152 } 153 if (tobeRead == 0) { 155 found = 0; 156 word.append('\n'); 157 content.append(word); 159 word = new StringBuffer (); 160 } 161 } 162 } 163 return new String (content); 165 } 166 167 public void processPOIFSReaderEvent(POIFSReaderEvent event) { 168 try { 169 DocumentInputStream input = event.getStream(); 170 parsedText = parse(input); 171 input.close(); 172 input = null; 173 } 174 catch (Exception ex) { 175 ex.printStackTrace(); 176 } 177 } 178 } | Popular Tags |