KickJava   Java API By Example, From Geeks To Geeks.

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


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.util.*;
12 import java.net.*;
13 import java.io.*;
14
15 public class Mappings
16 {
17     String dir;
18     Map urlMappings=new HashMap();
19     Map zipMappings=new HashMap();
20     long prevLastModified=0;
21
22     public Mappings(String dir)
23     {
24         this.dir=dir;
25     }
26
27     public String mapHTTPtoLocal(String url)
28     {
29
30         if (urlMappings.size()==0)
31             return url;
32         for(Iterator it=urlMappings.keySet().iterator();it.hasNext();)
33         {
34             String check=(String)it.next();
35             if (url.startsWith(check))
36             {
37                 //System.out.print("mapHTTPtoLocal("+url+")=>");
38
url=(String)urlMappings.get(check)+url.substring(check.length());
39                 //System.out.println(url);
40
break;
41             }
42         }
43         return url;
44     }
45
46     public String mapLocalToFile(String path)
47     {
48         if (zipMappings.size()==0)
49             return null;
50         for(Iterator it=zipMappings.keySet().iterator();it.hasNext();)
51         {
52             String check=(String)it.next();
53             if (path.startsWith(check))
54             {
55                 //System.out.print("mapLocalToFile("+path+")=>");
56
String url=(String)zipMappings.get(check)+path.substring(check.length());
57                 //System.out.println(url);
58
return url;
59             }
60         }
61         return null;
62     }
63
64     public boolean process()
65     {
66         urlMappings.clear();
67         zipMappings.clear();
68         long lastModified=process(new File(dir),0);
69         boolean ret=lastModified!=prevLastModified;
70         prevLastModified=lastModified;
71         return ret;
72     }
73
74     public long process(File file,long lastModified)
75     {
76         try
77         {
78             if (file.isDirectory())
79             {
80                 File[] files=file.listFiles();
81                 for(int i=0;i<files.length;i++)
82                     lastModified=process(files[i],lastModified);
83             }
84             else
85             {
86                 String relDir=file.getParent().substring(dir.length()).replace(File.separatorChar,'/')+"/";
87                 if (file.getName().endsWith(".lnk"))
88                 {
89                     lastModified=Math.max(lastModified,file.lastModified());
90                     file=FileParser.parseLink(file);
91                     if (file==null)
92                         return lastModified;
93                 }
94                 String name=file.getName();
95                 String path=file.getPath();
96                 int dot=name.lastIndexOf('.');
97                 String ext=(dot==-1)?"":name.substring(dot+1).toLowerCase();
98                 if (file.isDirectory())
99                 {
100                     lastModified=Math.max(lastModified,file.lastModified());
101                     String root=file.toURI().toURL().toString();
102                     zipMappings.put(relDir,root);
103                     //System.out.println("DirMapping: relDir="+relDir+" root="+root);
104
}
105                 if (ext.equals("zip")||ext.equals("jar"))
106                 {
107                     lastModified=Math.max(lastModified,file.lastModified());
108                     String canon=file.getCanonicalPath().replace('\\','/');
109                     String root="jar:file:"+canon+"!/";
110                     zipMappings.put(relDir,root);
111                     //System.out.println("ZipMapping: relDir="+relDir+" root="+root);
112
}
113                 else
114                 if (ext.equals("url"))
115                 {
116                     List urls=FileParser.getURLs(file);
117                     if (urls.size()!=0)
118                     {
119                         for(Iterator it=urls.iterator();it.hasNext();)
120                         {
121                             String url=(String)it.next();
122                             urlMappings.put(url,relDir);
123                             //System.out.println("UrlMapping: url="+url+" relDir="+relDir);
124
}
125                     }
126
127                 }
128             }
129         }
130         catch(IOException e)
131         {
132             System.out.println(e);
133         }
134         return lastModified;
135     }
136 }
137
Popular Tags