KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > contineo > core > text > parser > PSParser


1 /*
2  * PSParser.java
3  *
4  * Created on 26. Mai 2003, 14:45
5  */

6
7 package org.contineo.core.text.parser;
8
9 import java.io.BufferedReader JavaDoc;
10 import java.io.File JavaDoc;
11 import java.io.FileInputStream JavaDoc;
12 import java.io.IOException JavaDoc;
13 import java.io.InputStream JavaDoc;
14 import java.io.InputStreamReader JavaDoc;
15
16 import org.apache.log4j.Level;
17 import org.apache.log4j.Logger;
18 import org.contineo.core.LoggingManager;
19 /**
20  *
21  * @author Michael Scholz
22  */

23 public class PSParser implements Parser {
24     
25     /**
26      * @uml.property name="content"
27      */

28     private StringBuffer JavaDoc content = new StringBuffer JavaDoc();
29     
30     /**
31      * @uml.property name="logger"
32      * @uml.associationEnd
33      */

34     private Logger logger;
35     
36     /**
37      * @uml.property name="version"
38      */

39     private String JavaDoc version = "";
40     private BufferedReader JavaDoc reader;
41     
42     /** Creates a new instance of PSParser */
43     public PSParser(File JavaDoc file) {
44         logger = LoggingManager.getLogger(this.getClass());
45         init(file);
46     }
47     
48     protected void init(File JavaDoc file) {
49         try {
50             InputStream JavaDoc in = new FileInputStream JavaDoc(file);
51             reader = new BufferedReader JavaDoc(new InputStreamReader JavaDoc(in));
52             String JavaDoc line = reader.readLine();
53             if (line != null && line.length() >= 3)
54             {
55                 version = line.substring(line.length()-3);
56                 if (version.startsWith("2"))
57                     parse_v2();
58                 if (version.startsWith("3"))
59                     parse_v3();
60             }
61         }
62         catch (Exception JavaDoc ex) {
63             if (logger.isEnabledFor(Level.ERROR))
64                 logger.error((ex.getMessage()));
65         }
66     }
67
68     protected void parse_v2() throws IOException JavaDoc {
69         boolean isComment = false;
70         boolean isText = false;
71         boolean isConnector = false;
72         int ichar = 0;
73         while ((ichar = reader.read()) > 0) {
74             if (isConnector) {
75                 if (ichar < 108)
76                     content.append((char)32);
77                 isConnector = false;
78             }
79             if (ichar == 37)
80                 isComment = true;
81             if (ichar == 10 && isComment)
82                 isComment = false;
83             if (ichar == 41 && isText ) {
84                 isConnector = true;
85                 isText = false;
86             }
87             if (isText)
88                 content.append((char)ichar);
89             if (ichar == 40 && !isComment)
90                 isText = true;
91         }
92         reader.close();
93     }
94     
95     protected void parse_v3() throws IOException JavaDoc {
96         StringBuffer JavaDoc stmt = new StringBuffer JavaDoc();
97         boolean isComment = false;
98         boolean isText = false;
99         boolean isBMP = false;
100         boolean isStore = false;
101         int store = 0;
102         int ichar = 0;
103         while ((ichar = reader.read()) > 0) {
104             if (ichar == 37)
105                 isComment = true;
106             if (ichar == 10 && isComment)
107                 isComment = false;
108             if (ichar == 41 && isText )
109                 isText = false;
110             if (isText && !isBMP)
111                 content.append((char)ichar);
112             if (ichar == 40 && !isComment && !isBMP)
113                 isText = true;
114             if (isStore) {
115                 if (store == 9 || ichar == 32 || ichar == 10) {
116                     isStore = false;
117                     store = 0;
118                     if (stmt.toString().equals("BEGINBITM")) {
119                         isText = false;
120                         isBMP = true;
121                     }
122                     if (stmt.toString().equals("ENDBITMAP"))
123                         isBMP = false;
124                     stmt.delete(0,stmt.length());
125                 }
126                 else {
127                     stmt.append((char)ichar);
128                     store++;
129                 }
130             }
131             if (!isComment && !isStore && (ichar == 66 || ichar == 69)) {
132                 isStore = true;
133                 stmt.append((char)ichar);
134                 store++;
135             }
136         }
137         reader.close();
138     }
139
140     /**
141      *
142      * @uml.property name="content"
143      */

144     public StringBuffer JavaDoc getContent() {
145         return content;
146     }
147
148     /**
149      *
150      * @uml.property name="version"
151      */

152     public String JavaDoc getVersion() {
153         return version;
154     }
155
156     /* (non-Javadoc)
157      * @see org.contineo.core.text.parser.Parser#getAuthor()
158      */

159     public String JavaDoc getAuthor() {
160         return "";
161     }
162
163     /* (non-Javadoc)
164      * @see org.contineo.core.text.parser.Parser#getSourceDate()
165      */

166     public String JavaDoc getSourceDate() {
167         return "";
168     }
169
170     /* (non-Javadoc)
171      * @see org.contineo.core.text.parser.Parser#getKeywords()
172      */

173     public String JavaDoc getKeywords() {
174         return "";
175     }
176
177     /* (non-Javadoc)
178      * @see org.contineo.core.text.parser.Parser#getTitle()
179      */

180     public String JavaDoc getTitle() {
181         return "";
182     }
183 }
184
Popular Tags