KickJava   Java API By Example, From Geeks To Geeks.

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


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.Set;
13
14 public class URLFilterStream extends FilteredInputStream
15 {
16     private static final String[] links={"overview-summary.html","constant-values.html","deprecated-list.html","overview-tree.html","serialized-form.html","index-all.html","index-files/index-"};
17     boolean isTag=false;
18     boolean inQuote=false;
19     boolean openAnchor=false;
20     boolean openPre=false;
21     StringBuffer sb=new StringBuffer();
22     StringBuffer tb=new StringBuffer();
23     String baseurl;
24     LibClass self;
25     Library lib;
26     int id;
27     boolean summary;
28     String upPath;
29     int dots=0;
30     Mappings mappings;
31
32     public URLFilterStream(InputStream is,String path,LibClass self,Library lib,int id,Mappings mappings)
33     {
34         super(is);
35         StringBuffer bu=new StringBuffer();
36         summary=path.startsWith(ClassFinder.DOC_SUMMARY_ROOT);
37         int ind=path.indexOf('/',summary?13:5);
38         int firstInd=ind;
39         if (summary&&ind!=-1)
40         {
41             ind=path.indexOf('/',ind+1);
42         }
43         while(ind!=-1)
44         {
45             bu.append("../");
46             ind=path.indexOf('/',ind+1);
47         }
48         baseurl=bu.toString();
49         if (summary)
50         {
51             upPath=path.substring(firstInd+1);
52         }
53         this.self=self;
54         this.lib=lib;
55         this.id=id;
56         this.mappings=mappings;
57     }
58
59     public void filter(int b)
60     {
61         boolean addLink=false;
62         if (!isTag&&b=='<')
63         {
64             isTag=true;
65             tb.setLength(0);
66         }
67         else
68         if (isTag&&(!inQuote)&&b=='>')
69         {
70             String tag=tb.toString();
71             if (tb.length()>=2)
72             {
73                 if (tag.regionMatches(true,0,"A HREF=\"",0,8))
74                 {
75                     int q=tag.indexOf('"');
76                     int q2=tag.indexOf('"',q+1);
77                     String link=tag.substring(q+1,q2);
78                     String mapLink=mappings.mapHTTPtoLocal(link);
79                     if (mapLink!=link)
80                     {
81                         tag=tag.substring(0,q+1)+mapLink+tag.substring(q2);
82                     }
83                     else
84                     if (tag.charAt(q+1)!='#'&&tag.regionMatches(q+1,baseurl,0,baseurl.length()))
85                     {
86                         if (summary)
87                         {
88                             boolean linkToDoc=true;
89                             for(int i=0;i<links.length;i++)
90                             {
91                                 if (tag.regionMatches(q+1+baseurl.length(),links[i],0,links[i].length()))
92                                 {
93                                     linkToDoc=false;
94                                     break;
95                                 }
96                             }
97                             if (linkToDoc)
98                             {
99                                 tag=tag.substring(0,q+1)+ClassFinder.DOC_ROOT+tag.substring(q+1+baseurl.length());
100                             }
101                         }
102                         else
103                         if (tag.regionMatches(q+1+baseurl.length(),"../",0,3))
104                         {
105                             char ch='/';
106                             int pos=q+baseurl.length()+4;
107                             while(tag.regionMatches(pos,"../",0,3))
108                             {
109                                 pos+=3;
110                             }
111                             int slash=tag.indexOf('/',pos);
112                             tag=tag.substring(0,q+1)+baseurl+tag.substring(slash+1);
113                         }
114                         else
115                         {
116                             for(int i=0;i<links.length;i++)
117                             {
118                                 if (tag.regionMatches(q+1+baseurl.length(),links[i],0,links[i].length()))
119                                 {
120                                     tag=tag.substring(0,q+1)+ClassFinder.DOC_SUMMARY_ROOT+id+tag.substring(q+baseurl.length());
121                                     break;
122                                 }
123                             }
124                         }
125                     }
126
127                     openAnchor=true;
128                 }
129                 else
130                 if ((tb.charAt(0)=='/'&&(tb.charAt(1)=='a'||tb.charAt(1)=='A')&&(tb.length()==2||tb.charAt(2)==' ')))
131                     openAnchor=false;
132                 if (tag.equalsIgnoreCase("pre"))
133                     openPre=true;
134                 else
135                 if (tag.equalsIgnoreCase("/pre"))
136                     openPre=false;
137                 addLink=tag.equals("TR ALIGN=\"center\" VALIGN=\"top\"");
138             }
139             write(tag);
140             isTag=false;
141             inQuote=false;
142         }
143         if (isTag)
144         {
145             if (b=='"')
146                 inQuote=!inQuote;
147
148             if (tb.length()>0||(b!=' '&&b!='<'))
149             {
150                 tb.append((char)b);
151                 return;
152             }
153         }
154
155         if (!isTag&&!openAnchor)
156         {
157             if (b=='.')
158                 dots++;
159
160             if ((b>='A'&&b<='Z')||(b>='a'&&b<='z')||(b>='0'&&b<='9')||b=='.'||b=='_')
161             {
162                 sb.append((char)b);
163                 return;
164             }
165         }
166         if (sb.length()>0)
167         {
168             boolean match=false;
169             String cla=sb.toString();
170             if (dots>=1)
171             {
172                 Object o=lib.lookupClass(cla);
173                 if (o instanceof LibClass)
174                 {
175                     LibClass c=(LibClass)o;
176                     if (self==null||c!=self)
177                     {
178                         write("<a HReF=\"");
179                         write(baseurl);
180                         write(c.getClassPath());
181                         write(".html\">");
182                         write(openPre&&b!='&'?cla:c.getName());
183                         write("</a>");
184                         match=true;
185                     }
186                 }
187             }
188             else
189             if (summary)
190             {
191                 int mid=-1;
192                 if (id>0&&cla.equals("PREV"))
193                     mid=id-1;
194                 else
195                 if (id<lib.getJavadocRoots().size()-1&&cla.equals("NEXT"))
196                     mid=id+1;
197                 if (mid!=-1)
198                 {
199                     write("<a HrEF=\""+ClassFinder.DOC_SUMMARY_ROOT+mid+'/'+upPath+"\">"+cla+"</a>");
200                     match=true;
201                 }
202             }
203             if (!match)
204                 write(cla);
205
206             sb.setLength(0);
207             dots=0;
208         }
209         write(b);
210         if (addLink)
211         {
212             if (summary)
213             {
214                 write("<td BGCOLOR=\"#EEEEFF\" CLASS=\"NavBarCell1\"> <a HREF=\""+ClassFinder.DOC_ROOT);
215                 write(upPath);
216                 write("\"><font CLASS=\"NavBarFont1\"><b>Up</b></font></a>&nbsp;</td>\n");
217             }
218             if (self!=null&&(self.hasResource(LibClass.SRC)||self.hasResource(LibClass.CLA)))
219             {
220                 write("<td BGCOLOR=\"#EEEEFF\" CLASS=\"NavBarCell1\"> <a HREF=\""+ClassFinder.SRC_ROOT);
221                 write(self.getSourcePath());
222                 write(".html\"><font CLASS=\"NavBarFont1\"><b>Source</b></font></a>&nbsp;</td>\n");
223             }
224             write("<td BGCOLOR=\"#EEEEFF\" CLASS=\"NavBarCell1\"> <a HREF=\"");
225             write("/search?");
226             if (self!=null)
227             {
228                 write("q=");
229                 write(self.getClassName());
230                 write(";&");
231             }
232             write("f=t\" target=\"_top\"><font CLASS=\"NavBarFont1\"><b>Search</b></font></a>&nbsp;</td>\n");
233         }
234     }
235 }
Popular Tags