KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.enhydra.snapper.parsers;
2 import java.io.*;
3 import java.util.zip.*;
4
5 import org.enhydra.snapper.api.Parser;
6 import org.enhydra.snapper.utils.ReadWriteTextFile;
7
8 public class OoToText implements org.enhydra.snapper.api.Parser
9 {
10     final int BUFFER = 2048;
11     String JavaDoc content = "content.xml";
12     String JavaDoc meta = "meta.xml";
13     String JavaDoc tempFile;
14     String JavaDoc metaFile = "";
15     String JavaDoc title, parsedText, fileName;
16     
17     public String JavaDoc parse(InputStream is) throws java.io.IOException JavaDoc{ return "";}
18
19     public OoToText(String JavaDoc fileName,
20             String JavaDoc tempFile)
21    {
22        this.fileName = fileName;
23        this.tempFile = tempFile;
24       // this.metaFile = metaFile;
25
}
26
27     public void parse(){
28         // 1st convert the file to uncompressed xml
29

30         FileOutputStream fos = null;
31         BufferedOutputStream dest;
32         FileInputStream fis = null;
33         ZipInputStream zis = null;
34         try{
35             fis = new FileInputStream(fileName);
36             zis = new ZipInputStream(new BufferedInputStream(fis));
37         }
38         catch (FileNotFoundException fnfe) {
39             try{
40                 ParserManager.logger.debug("File not found!");
41             } catch (Exception JavaDoc ex) {
42                 System.out.println("File not found!");
43                 }
44         }
45         int count;
46         String JavaDoc entryName = "";
47         byte[] data = new byte[BUFFER];
48         try
49         {
50             ZipEntry entry;
51             while ((entry = zis.getNextEntry()) != null)
52             {
53                 entryName = entry.getName();
54                 if (entryName.endsWith(content))
55                 {
56                     fos = new FileOutputStream(tempFile);
57                     dest = new BufferedOutputStream(fos, BUFFER);
58                     while ((count = zis.read(data, 0, BUFFER)) != -1)
59                     {
60                         dest.write(data, 0, count);
61                     }
62
63                     dest.flush();
64                     dest.close();
65                     parsedText = ReadWriteTextFile.getContents(new File(this.tempFile));
66                     //if (new File(this.tempFile).exists())
67
// (new File(this.tempFile)).delete();
68

69                 }
70                 /*else if (entryName.endsWith(meta))
71                 {
72                     fos = new FileOutputStream(metaFile);
73                     dest = new BufferedOutputStream(fos, BUFFER);
74                     while ((count = zis.read(data, 0, BUFFER)) != -1)
75                     {
76                         dest.write(data, 0, count);
77                     }
78                     
79                     
80                     dest.flush();
81                     dest.close();
82                 }*/

83                 else
84                 {
85                     //System.out.println("zip entry:" + entryName);
86
}
87             }
88             fos.close();
89             fos = null;
90             fis.close();
91             fis = null;
92             zis.close();
93             tempFile = null;
94         }
95         catch (Exception JavaDoc eZ)
96         {
97             try{
98                 ParserManager.logger.debug("File could not be parsed: " + fileName);
99             } catch (Exception JavaDoc ex) {
100                 System.out.println("***** File could not be parsed: " + fileName);
101                 }
102         }
103         finally
104         {
105             // debug output?
106
}
107     }
108     
109       public void setFileName(String JavaDoc fileName) {
110         this.fileName = fileName;
111       }
112
113       public String JavaDoc getParsedText() {
114         return parsedText;
115       }
116       
117       public String JavaDoc getTitle() {
118         return title;
119       }
120 }
121
Popular Tags