/Users/lyon/j4p/src/gui/htmlconverter/JavaStream.java

1    package gui.htmlconverter; 
2     
3    import java.io.*; 
4     
5    public class JavaStream extends BufferedReader 
6            implements JavaText, CText, CplusplusText { 
7     
8        private JavaHtmlString mainText = new JavaHtmlString(); 
9        private JavaHtmlString comments = new JavaHtmlString(); 
10       private JavaHtmlString strings = new JavaHtmlString(); 
11       private JavaHtmlString keywords = new JavaHtmlString(); 
12       private boolean lineNumbers = false; 
13       private boolean justOutputPublic = false; 
14       private DataOutputStream dos; 
15       private static String[] reservedWords = javaReservedWords; 
16    
17       JavaStream(DataInputStream s0, DataOutputStream s1) { 
18           //super(new BufferedInputStream(s0)); 
19           super(new InputStreamReader(s0)); 
20           dos = s1; 
21       } 
22    
23       public void convertToHtml() { 
24           int index; 
25           boolean isCommentedOut = false; 
26           boolean isQuoted = false; 
27           boolean isSingleQuoted = false; 
28           String line; 
29           String str1, str2; 
30           int lineNumber = 1; 
31    
32           try { 
33               while ((line = readLine()) != null) { 
34                   if (justOutputPublic && 
35                           (line.lastIndexOf("public") == -1) 
36                   ) 
37                       continue; 
38                   // only output public classes 
39    
40                   // output a line number for each line. 
41                   if (lineNumbers) 
42                       line = lineNumber + ".\t" + line; 
43                   lineNumber++; 
44    
45                   // for replacing \t with four spaces 
46                   for (int counter = 0; counter < line.length(); counter++) { 
47                       index = line.indexOf("\t", counter); 
48                       if (index < 0) break; 
49                       // if \t is not find then break this loop 
50                       line = line.substring(0, index) + "    " 
51                               + line.substring(index + 1, line.length()); 
52                       // replace \t to four spaces; 
53                       counter = index + 4; 
54                       // update counter 
55                   } 
56    
57                   // for replacing "<" to "&lt;" 
58    
59                   for (int counter = 0; counter < line.length(); counter++) { 
60                       index = line.indexOf("<", counter); 
61                       if (index < 0) break; 
62                       // if < is not find then break this loop 
63                       line = line.substring(0, index) + "&lt;" 
64                               + line.substring(index + 1, line.length()); 
65                       // replace < to &lt; 
66                       counter = index + 4; 
67                       // update counter 
68                   } 
69    
70                   while (line != "") { 
71                       if (isCommentedOut) { 
72                           index = line.indexOf("*/"); 
73                           if (index >= 0) { 
74                               // if "*/" exists then 
75                               isCommentedOut = false; 
76                               dos.writeBytes(line.substring(0, index) + "*/" 
77                                       + comments.postf); 
78                               if (index + 2 < line.length()) 
79                                   line = line.substring(index + 2, line.length()); 
80                               else 
81                                   break; 
82                               // break this loop 
83                           } else { 
84                               dos.writeBytes(line); 
85                               break;      // break this loop and get next line 
86                           } 
87                       } else if (isQuoted) { 
88                           index = line.indexOf("\""); 
89                           if (index == 0 || index > 0 && line.indexOf("\\") != index - 1) { 
90                               // if "\"" exists then 
91                               isQuoted = false; 
92                               dos.writeBytes(line.substring(0, index) + "\"" + strings.postf); 
93                               if (index + 1 < line.length()) 
94                                   line = line.substring(index + 1, line.length()); 
95                               else 
96                                   break;      // break this loop and get next line 
97                           } else if (index > 0 && line.indexOf("\\") == index - 1) { 
98                               dos.writeBytes(line.substring(0, index + 1)); 
99                               line = line.substring(index + 1, line.length()); 
100                          } 
101                      } else if (isSingleQuoted) {        // added this block on 11/4 
102                          index = line.indexOf("'"); 
103                          if (index == 0 || index > 0 && line.indexOf("\\") != index - 1) { 
104                              // if "'" exists then 
105                              isSingleQuoted = false; 
106                              dos.writeBytes(line.substring(0, index) + "'"); 
107                              if (index + 1 < line.length()) 
108                                  line = line.substring(index + 1, line.length()); 
109                              else 
110                                  break;      // break this loop and get next line 
111                          } else if (index > 0 && line.indexOf("\\") == index - 1) { 
112                              dos.writeBytes(line.substring(0, index + 1)); 
113                              line = line.substring(index + 1, line.length()); 
114                          } 
115                      } else { 
116                          int index1 = line.indexOf("/*"); 
117                          int index2 = line.indexOf("//"); 
118                          int index3 = line.indexOf("\""); 
119                          int index4 = line.indexOf("'"); // added on 11/4 
120                          if (index1 < 0) index1 = 99999; 
121                          if (index2 < 0) index2 = 99999; 
122                          if (index3 < 0) index3 = 99999; 
123                          if (index4 < 0) index4 = 99999; // added on 11/4 
124   
125                          // if "/*" is lefter than "//" and "\"" 
126                          if (index1 < index2 && index1 < index3 && index1 < index4) { 
127                              // modified on 11/4 
128                              isCommentedOut = true; 
129                              if (index1 > 0) 
130                                  print(line.substring(0, index1), 0, 0); 
131                              dos.writeBytes(comments.pref + "/*"); 
132                              line = line.substring(index1 + 2, line.length()); 
133                              // if "//" is lefter than "/*" and "\"" 
134                          } else if (index2 < index1 && index2 < index3 && index2 < index4) { 
135                              // modified on 11/4 
136                              if (index2 > 0) 
137                                  print(line.substring(0, index2), 0, 0); 
138                              dos.writeBytes(comments.pref + "//" 
139                                      + line.substring(index2 + 2, line.length()) + comments.postf); 
140                              break;          // break this loop and get next line 
141                              // if "\"" is lefter than "//" and "/*" 
142                          } else if (index3 < index1 && index3 < index2 && index3 < index4) { 
143                              // modified on 11/4 
144                              isQuoted = true; 
145                              if (index3 > 0) 
146                                  print(line.substring(0, index3), 0, 0); 
147                              dos.writeBytes(strings.pref + "\""); 
148                              line = line.substring(index3 + 1, line.length()); 
149                          } else if (index4 < index1 && index4 < index2 && index4 < index3) { 
150                              // added on 11/4 
151                              isSingleQuoted = true; 
152                              if (index4 > 0) 
153                                  print(line.substring(0, index4), 0, 0); 
154                              dos.writeBytes("'"); 
155                              line = line.substring(index4 + 1, line.length()); 
156                          } else { 
157                              print(line, 0, 0); 
158                              break; 
159                          } 
160                      } 
161                  } 
162                  dos.writeBytes("\n"); 
163              } 
164          } catch (IOException e) { 
165          } 
166      } 
167   
168   
169      public void print(String line, int offset, int index) { 
170          try { 
171              if (index >= reservedWords.length) { 
172                  dos.writeBytes(line); 
173                  return; 
174              } 
175   
176              if (offset == 0) 
177                  offset = line.indexOf(reservedWords[index]); 
178   
179              if (offset >= 0) { 
180                  char beforeKeyword = ' '; 
181                  char afterKeyword = ' '; 
182   
183                  if (offset - 1 >= 0) 
184                      beforeKeyword = line.charAt(offset - 1); 
185                  if (offset + reservedWords[index].length() < line.length()) 
186                      afterKeyword = line.charAt(offset + reservedWords[index].length()); 
187   
188                  switch (beforeKeyword) { 
189                      case ' ': 
190                      case '\t': 
191                      case '\r': 
192                      case '(': 
193                      case '{': 
194                      case ')': 
195                      case ':': 
196                      case ';': 
197                      case '.': 
198                      case ',': 
199                          switch (afterKeyword) { 
200                              case ' ': 
201                              case '\t': 
202                              case '\r': 
203                              case '(': 
204                              case '{': 
205                              case ')': 
206                              case ':': 
207                              case ';': 
208                              case '.': 
209                              case ',': 
210                                  String str1 = line.substring(0, offset); 
211                                  String str2 = line.substring(offset + reservedWords[index].length(), 
212                                          line.length()); 
213                                  line = str1 + keywords.pref + reservedWords[index] + keywords.postf + str2; 
214   
215                                  offset = line.indexOf(reservedWords[index], 
216                                          offset + keywords.pref.length() + reservedWords[index].length() 
217                                          + keywords.postf.length()); 
218   
219                                  print(line, offset, index); 
220                                  return; 
221                          } 
222                  } 
223              } 
224              print(line, 0, index + 1); 
225          } catch (IOException e) { 
226          } 
227          return; 
228      } 
229   
230      public JavaHtmlString getMainText() { 
231          return mainText; 
232      } 
233   
234      public void setMainText(JavaHtmlString mainText) { 
235          this.mainText = mainText; 
236      } 
237   
238      public JavaHtmlString getComments() { 
239          return comments; 
240      } 
241   
242      public void setComments(JavaHtmlString comments) { 
243          this.comments = comments; 
244      } 
245   
246      public JavaHtmlString getStrings() { 
247          return strings; 
248      } 
249   
250      public void setStrings(JavaHtmlString strings) { 
251          this.strings = strings; 
252      } 
253   
254      public JavaHtmlString getKeywords() { 
255          return keywords; 
256      } 
257   
258      public void setKeywords(JavaHtmlString keywords) { 
259          this.keywords = keywords; 
260      } 
261   
262      public boolean isLineNumbers() { 
263          return lineNumbers; 
264      } 
265   
266      public void setLineNumbers(boolean lineNumbers) { 
267          this.lineNumbers = lineNumbers; 
268      } 
269   
270      public boolean isJustOutputPublic() { 
271          return justOutputPublic; 
272      } 
273   
274      public void setJustOutputPublic(boolean justOutputPublic) { 
275          this.justOutputPublic = justOutputPublic; 
276      } 
277   
278      public DataOutputStream getDos() { 
279          return dos; 
280      } 
281   
282      public void setDos(DataOutputStream dos) { 
283          this.dos = dos; 
284      } 
285   
286      public static String[] getReservedWords() { 
287          return reservedWords; 
288      } 
289   
290      public static void setReservedWords(String[] reservedWords) { 
291          JavaStream.reservedWords = reservedWords; 
292      } 
293   
294  } 
295