KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > roblisa > classfinder > JavaToHTMLFilterStream


1 /**
2  * ClassFinder - a javadoc webserver.
3  * Copyright (C) 2003 Rob Nielsen
4  * rob@roblisa.com
5  * http://www.roblisa.com/java/classfinder/
6  *
7  * Released under the GNU GPL - http://www.gnu.org/copyleft/gpl.html
8  */

9 package com.roblisa.classfinder;
10
11 import java.io.*;
12 import java.util.*;
13 /**
14  * Filters java source code into a html file.
15  */

16 public class JavaToHTMLFilterStream extends FilteredInputStream
17 {
18     boolean singleComment=false;
19     boolean multiComment=false;
20     boolean openSlash=false;
21     boolean singleQuote=false;
22     boolean doubleQuote=false;
23     boolean backSlash=false;
24
25     private static final Set keys=new HashSet();
26
27     static
28     {
29         String[] keywords=new String[]{"abstract","boolean","break","byte","case","catch","char","class","continue","default","delegate","do","double","else","extends","false","final","finally","float","for","if","implements","import","instanceof","int","interface","long","native","new","null","package","private","protected","public","return","short","static","super","switch","synchronized","this","throw","throws","transient","true","try","void","volatile","while"};
30         for(int i=0;i<keywords.length;i++)
31             keys.add(keywords[i]);
32     }
33
34     private static final String brackets="(){}[]";
35
36     StringBuffer sb=new StringBuffer();
37     int lb=-1;
38     int llb=-1;
39     String baseurl;
40     Library lib;
41     Set imports;
42     String font=null;
43     boolean linkToDoc;
44
45     public JavaToHTMLFilterStream(InputStream is,LibClass self,Library lib,boolean linkToDoc)
46     {
47         super(is);
48         StringBuffer bu=new StringBuffer();
49         String path=self.getClassPath();
50         int ind=path.indexOf('/');
51         while(ind!=-1)
52         {
53             bu.append("../");
54             ind=path.indexOf('/',ind+1);
55         }
56
57         baseurl=bu.toString();
58         this.lib=lib;
59         write("<html><head><title>Viewing Source of ");
60         write(self.getClassName());
61         write("</title>");
62         write("<LINK REL =\"stylesheet\" TYPE=\"text/css\" HREF=\"");
63         write(baseurl);
64         write("stylesheet.css\" TITLE=\"Style\">");
65         write("</head><body><h2>Viewing Source of <a HREF=\"");
66         write(ClassFinder.DOC_ROOT);
67         String pck=self.getPackage();
68         write(self.getClassPath());
69         write(".html\">");
70         write(self.getClassName());
71         write("</a></h2>");
72         write("<a HREF=\"");
73         write(baseurl);
74         write(self.getSourcePath());
75         write(".java\">");
76         write(self.getSourceName());
77         write(".java</a>");
78         write("<pre>\n");
79         this.linkToDoc=linkToDoc;
80         imports=new HashSet();
81         imports.add("java.lang");
82         imports.add(pck);
83     }
84
85     public static void main(String[] args) throws Exception
86     {
87         // single line comment
88
InputStream is=new JavaToHTMLFilterStream(new BufferedInputStream(new FileInputStream("JavaToHTMLFilterStream.java")),null,null,true);
89         FileOutputStream fos=new FileOutputStream("rob.html");
90         byte[] buf=new byte[1024];
91         try {
92             int n;
93             while ((n = is.read(buf)) > 0) {
94                 fos.write(buf, 0, n);
95             }
96         } finally {
97             is.close();
98             fos.close();
99         }
100     }
101
102     public void filter(int b)
103     {
104         boolean skip=false;
105
106         if (!singleQuote&&!doubleQuote&&!singleComment&&!multiComment)
107         {
108             if (lb=='/'&&b=='*')
109             {
110                 setFont("MultiComment");
111                 multiComment=true;
112             }
113
114             if (b=='/'&&lb=='/')
115             {
116                 setFont("SingleComment");
117                 singleComment=true;
118             }
119         }
120
121         // if we have a slash, don't write it until we see if it is an open comment
122
if (b=='/')
123             skip=true;
124
125         // if the last was a slash, write it now as comments have been written
126
if (lb=='/')
127             write('/');
128
129         if ((singleComment&&b=='\n')||(multiComment&&llb=='*'&&lb=='/'))
130         {
131             singleComment=multiComment=false;
132         }
133
134
135
136         if (!singleComment&&!multiComment)
137         {
138             if (lb!='\\'||(lb=='\\'&&llb=='\\'))
139             {
140                 if (!singleQuote&&!doubleQuote&&b=='"')
141                 {
142                     setFont("DoubleQuote");
143                     doubleQuote=true;
144                 }
145                 else
146                 if (!singleQuote&&!doubleQuote&&b=='\'')
147                 {
148                     setFont("SingleQuote");
149                     singleQuote=true;
150                 }
151                 else
152                 if ((singleQuote&&b=='\'')||(doubleQuote&&b=='"'))
153                 {
154                     write(b);
155                     skip=true;
156                     singleQuote=doubleQuote=false;
157                 }
158             }
159             if (!singleQuote&&!doubleQuote)
160             {
161                 if ((b>='A'&&b<='Z')||(b>='a'&&b<='z')||(b>='0'&&b<='9')||b=='.'||b=='_')
162                 {
163                     sb.append((char)b);
164                     skip=true;
165                 }
166             }
167         }
168         if (!skip)
169         {
170             if (sb.length()>0)
171             {
172                 boolean match=false;
173                 String s=sb.toString();
174                 String cla=null;
175                 int cut=-1;
176
177                 if (lib!=null)
178                 {
179                     boolean docLink=linkToDoc;
180                     if (s.charAt(s.length()-1)=='.')
181                     {
182                         String sub=s.substring(0,s.length()-1);
183                         if (lib.containsPackage(sub))
184                         {
185                             imports.add(sub);
186                             cla=sub.replace('.','/')+"/package-summary";
187                             cut=s.length()-1;
188                             match=true;
189                             docLink=true;
190                         }
191                     }
192                     else
193                     if (lib.containsPackage(s))
194                     {
195                         cla=s.replace('.','/')+"/package-summary";
196                         cut=s.length();
197                         match=true;
198                     }
199                     else
200                     {
201                         int dot=s.length();
202                         for(int i=0;i<3;i++)
203                         {
204                             String s2=s.substring(0,dot);
205                             Object o=lib.lookupClass(s2);
206                             if (o instanceof LibClass)
207                             {
208                                 LibClass c=(LibClass)o;
209                                 cla=c.getClassPath();
210                                 if (dot!=-1)
211                                     imports.add(cla);
212                                 cut=(i==0)?s.length():dot;
213                                 match=true;
214                                 docLink=linkToDoc&&c.hasResource(LibClass.DOC);
215                             }
216                             else
217                             if (o instanceof List)
218                             {
219                                 for(Iterator it=((List)o).iterator();it.hasNext();)
220                                 {
221                                     LibClass c=(LibClass)it.next();
222                                     if (imports.contains(c.getClassName())||imports.contains(c.getPackage()))
223                                     {
224                                         cla=c.getClassPath();
225                                         cut=(i==0)?s.length():dot;
226                                         match=true;
227                                         docLink=linkToDoc&&c.hasResource(LibClass.DOC);
228                                         break;
229                                     }
230                                 }
231                             }
232                             if (i==0)
233                                 dot=s.indexOf('.');
234                             else
235                             if (i==2)
236                                 dot=s.indexOf('.',dot+1);
237
238                             if (dot==-1||match)
239                                 break;
240
241                         }
242                     }
243                     if (match)
244                     {
245                         setFont(null);
246                         write("<a HREF=\"");
247                         write(docLink?ClassFinder.DOC_ROOT:ClassFinder.SRC_ROOT);
248                         write(cla);
249                         write(".html\">");
250                         write(s.substring(0,cut));
251                         write("</a>");
252                         if (cut<s.length())
253                             write(s.substring(cut));
254                     }
255                 }
256                 if (!match)
257                 {
258                     if (keys.contains(s))
259                     {
260                         setFont("Keywords");
261                         write(s);
262                         match=true;
263                     }
264                 }
265                 if (!match)
266                 {
267                     setFont(null);
268                     write(s);
269                 }
270                 sb.setLength(0);
271             }
272             if (!singleQuote&&!doubleQuote&&!singleComment&&!multiComment)
273             {
274                 if (brackets.indexOf(b)!=-1)
275                 {
276                     setFont("Brackets");
277                     write(b);
278                     skip=true;
279                 }
280             }
281
282             if (!skip)
283             {
284                 if (!singleQuote&&!doubleQuote&&!singleComment&&!multiComment&&b!=' '&&b!='\n'&&b!='\r'&&b!='\t')
285                     setFont(null);
286
287                 if (b==-1)
288                 {
289                     write("</pre></body></html>");
290                     setFont(null);
291                 }
292                 else
293                 if (b=='<')
294                     write("&lt;");
295                 else
296                 if (b=='>')
297                     write("&gt;");
298                 else
299                     write(b);
300             }
301         }
302         llb=lb;
303         lb=b;
304     }
305
306     private void setFont(String col)
307     {
308         if (col==font) return;
309         if (font!=null)
310         {
311             write("</font>");
312         }
313         if (col!=null)
314         {
315             write("<font CLASS=\"");
316             write(col);
317             write("\">");
318         }
319         font=col;
320     }
321 }
322
Popular Tags