KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > snapper > parsers > PowerParser


1 package org.enhydra.snapper.parsers;
2
3 import java.io.FileInputStream JavaDoc;
4 import java.util.Collection JavaDoc;
5 import java.util.Iterator JavaDoc;
6 import java.util.Map JavaDoc;
7 import java.util.Set JavaDoc;
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 JavaDoc;
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 JavaDoc fileName;
24   private String JavaDoc parsedText;
25   public String JavaDoc title;
26   private String JavaDoc properties = "";
27   public Map JavaDoc customproperties;
28   
29   
30
31   public void parse() {
32       FileInputStream JavaDoc fis = null;
33     try {
34 // summary info
35
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 JavaDoc(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 JavaDoc e) {
61         ParserManager.logger.info("***** File could not be parsed: " + fileName);
62       //e.printStackTrace();
63
}
64     
65     finally {
66         if (fis != null){
67             try{
68             fis.close();
69             } catch (Exception JavaDoc ex){};
70             fis = null;
71         }
72     }
73
74   }
75
76   public void setFileName(String JavaDoc fileName) {
77     this.fileName = fileName;
78   }
79
80   public String JavaDoc getParsedText() {
81     return parsedText;
82   }
83
84   public String JavaDoc getTitle() {
85     return title;
86   }
87
88   public String JavaDoc getProperties() {
89     return properties;
90   }
91
92   private void createString(Map JavaDoc customproperties) {
93     Collection JavaDoc values = customproperties.values();
94     Iterator JavaDoc valuesIterator = values.iterator();
95     properties = "";
96     Set JavaDoc keys = customproperties.keySet();
97     Iterator JavaDoc keysIterator = keys.iterator();
98
99     for (Iterator JavaDoc it = customproperties.entrySet().iterator(); it.hasNext(); ) {
100       Map.Entry JavaDoc entry = (Map.Entry JavaDoc) it.next();
101       properties += entry.getKey().toString() + " " + entry.getValue() + " ";
102     }
103
104   }
105
106   
107   public String JavaDoc parse(InputStream JavaDoc stream) throws java.io.IOException JavaDoc {
108     StringBuffer JavaDoc content = new StringBuffer JavaDoc();
109     int letter = 0;
110     int found = 0;
111     boolean separator = true;
112     int zeroCounter = 0;
113     int tobeRead = 0;
114     StringBuffer JavaDoc word = new StringBuffer JavaDoc();
115     while ( (letter = stream.read()) != -1) {
116       //System.out.println("LFR: " + letter + " " + found + " " + tobeRead);
117
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         //System.out.println("WLL: " + word);
154
if (tobeRead == 0) {
155           found = 0;
156           word.append('\n');
157           //System.out.println("WWW: " + word);
158
content.append(word);
159           word = new StringBuffer JavaDoc();
160         }
161       }
162     }
163     //System.out.println("CNT: " + content);
164
return new String JavaDoc(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 JavaDoc ex) {
175       ex.printStackTrace();
176     }
177   }
178 }
Popular Tags