KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > cvsgrab > web > ViewCvsInterface


1 /*
2  * CVSGrab
3  * Author: Ludovic Claude (ludovicc@users.sourceforge.net)
4  * Distributable under BSD license.
5  */

6 package net.sourceforge.cvsgrab.web;
7
8 import java.util.ArrayList JavaDoc;
9 import java.util.Iterator JavaDoc;
10 import java.util.List JavaDoc;
11 import java.util.Properties JavaDoc;
12
13 import net.sourceforge.cvsgrab.CVSGrab;
14 import net.sourceforge.cvsgrab.CvsWebInterface;
15 import net.sourceforge.cvsgrab.InvalidVersionException;
16 import net.sourceforge.cvsgrab.MarkerNotFoundException;
17 import net.sourceforge.cvsgrab.RemoteFile;
18 import net.sourceforge.cvsgrab.WebBrowser;
19
20 import org.apache.commons.httpclient.URIException;
21 import org.apache.commons.httpclient.util.URIUtil;
22 import org.apache.commons.jxpath.JXPathContext;
23 import org.apache.commons.jxpath.Pointer;
24 import org.w3c.dom.Document JavaDoc;
25
26 /**
27  * Support for ViewCvs-like interfaces to a cvs repository
28  *
29  * @author <a HREF="mailto:ludovicc@users.sourceforge.net">Ludovic Claude</a>
30  * @version $Revision: 1.17 $ $Date: 2005/06/24 00:04:34 $
31  * @created on 11 oct. 2003
32  */

33 public abstract class ViewCvsInterface extends CvsWebInterface {
34
35     private String JavaDoc _type;
36     private String JavaDoc _filesXpath = "//TR[TD/A/IMG/@alt = '(file)']";
37     private String JavaDoc _fileNameXpath = "TD[1]/A/@name";
38     private String JavaDoc _fileVersionXpath = "TD[A/IMG/@alt != '(graph)'][2]/A/B";
39     private String JavaDoc _directoriesXpath = "//TR[TD/A/IMG/@alt = '(dir)'][TD/A/@name != 'Attic']";
40     private String JavaDoc _directoryXpath = "TD[1]/A/@name";
41     private String JavaDoc _checkoutPath = "*checkout*/";
42     private String JavaDoc _webInterfaceType = "viewcvs";
43     private String JavaDoc _tagParam = "only_with_tag";
44     private String JavaDoc _cvsrootParam = "cvsroot";
45
46     /**
47      * Constructor for ViewCvsInterface
48      *
49      */

50     public ViewCvsInterface(CVSGrab grabber) {
51         super(grabber);
52     }
53
54     /**
55      * Initialize the web interface
56      */

57     public void init() throws Exception JavaDoc {
58         _type = getId();
59     }
60
61     /**
62      * {@inheritDoc}
63      * @param htmlPage The web page
64      * @throws MarkerNotFoundException if the version marker for the web interface was not found
65      * @throws InvalidVersionException if the version detected is incompatible with the version supported by this web interface.
66      */

67     public void detect(Document JavaDoc htmlPage) throws MarkerNotFoundException, InvalidVersionException {
68
69         JXPathContext context = JXPathContext.newContext(htmlPage);
70         Iterator JavaDoc viewCvsTexts = context.iterate("//META[@name = 'generator']/@content[starts-with(.,'ViewCVS')] | //A[@href]/text()[starts-with(.,'ViewCVS')]");
71         _type = null;
72         String JavaDoc viewCvsVersion = null;
73         while (viewCvsTexts.hasNext()) {
74             viewCvsVersion = (String JavaDoc) viewCvsTexts.next();
75             if (viewCvsVersion.startsWith(getVersionMarker())) {
76                 _type = viewCvsVersion;
77                 break;
78             }
79         }
80         if (_type == null) {
81             throw new MarkerNotFoundException("Expected marker " + getVersionMarker() + ", found " + viewCvsVersion);
82         }
83     }
84
85     /**
86      * {@inheritDoc}
87      */

88     public String JavaDoc getId() {
89         String JavaDoc className = getClass().getName();
90         className = className.substring(className.lastIndexOf('.') + 1);
91         className = className.substring(0, className.indexOf("Interface"));
92         return className;
93     }
94
95     /**
96      * {@inheritDoc}
97      */

98     public String JavaDoc getType() {
99         return _type;
100     }
101
102     /**
103      * @return the base url to use when trying to auto-detect this type of web interface
104      */

105     public String JavaDoc getBaseUrl() {
106         String JavaDoc url = WebBrowser.forceFinalSlash(getGrabber().getRootUrl());
107         url += getGrabber().getPackagePath();
108         if (getProjectRoot() != null) {
109             url = WebBrowser.addQueryParam(url, _cvsrootParam, getProjectRoot());
110         }
111         url = WebBrowser.addQueryParam(url, getGrabber().getQueryParams());
112         return url;
113     }
114
115     /**
116      * @param rootUrl
117      * @param directoryName
118      * @return the url to use to access the contents of the repository
119      */

120     public String JavaDoc getDirectoryUrl(String JavaDoc rootUrl, String JavaDoc directoryName) {
121         try {
122             String JavaDoc tag = getVersionTag();
123             String JavaDoc url = WebBrowser.forceFinalSlash(rootUrl);
124             url += WebBrowser.forceFinalSlash(quote(directoryName));
125             if (getProjectRoot() != null) {
126                 url = WebBrowser.addQueryParam(url, _cvsrootParam, getProjectRoot());
127             }
128             url = WebBrowser.addQueryParam(url, _tagParam, tag);
129             url = WebBrowser.addQueryParam(url, getQueryParams());
130             return url;
131         } catch (URIException ex) {
132             ex.printStackTrace();
133             throw new RuntimeException JavaDoc("Cannot create URI");
134         }
135     }
136
137     /**
138      * {@inheritDoc}
139      */

140     public RemoteFile[] getFiles(Document JavaDoc htmlPage) {
141         JXPathContext context = JXPathContext.newContext(htmlPage);
142         List JavaDoc files = new ArrayList JavaDoc();
143         Iterator JavaDoc i = context.iteratePointers(getFilesXpath());
144         while (i.hasNext()) {
145             Pointer pointer = (Pointer) i.next();
146             JXPathContext nodeContext = context.getRelativeContext(pointer);
147             String JavaDoc fileName = (String JavaDoc) nodeContext.getValue(getFileNameXpath());
148             String JavaDoc version = (String JavaDoc) nodeContext.getValue(getFileVersionXpath());
149             RemoteFile file = new RemoteFile(fileName, version);
150             adjustFile(file, nodeContext);
151             files.add(file);
152         }
153         return (RemoteFile[]) files.toArray(new RemoteFile[files.size()]);
154     }
155
156     /**
157      * {@inheritDoc}
158      */

159     public String JavaDoc[] getDirectories(Document JavaDoc htmlPage) {
160         JXPathContext context = JXPathContext.newContext(htmlPage);
161         context.registerNamespace("HTML", "http://www.w3.org/1999/xhtml");
162         context.registerNamespace("", "http://www.w3.org/1999/xhtml");
163         List JavaDoc directories = new ArrayList JavaDoc();
164         Iterator JavaDoc i = context.iteratePointers(getDirectoriesXpath());
165         while (i.hasNext()) {
166             Pointer pointer = (Pointer) i.next();
167             JXPathContext nodeContext = context.getRelativeContext(pointer);
168             try {
169                 String JavaDoc dir = (String JavaDoc) nodeContext.getValue(getDirectoryXpath());
170                 directories.add(dir);
171             } catch (RuntimeException JavaDoc e) {
172                 CVSGrab.getLog().error("Cannot localise directory name in document location " + nodeContext.getPointer("."), e);
173             }
174         }
175         return (String JavaDoc[]) directories.toArray(new String JavaDoc[directories.size()]);
176     }
177
178     public String JavaDoc getDownloadUrl(RemoteFile file) {
179         try {
180             // http://cvs.apache.org/viewcvs.cgi/*checkout*/jakarta-regexp/KEYS?rev=1.1
181
String JavaDoc url = WebBrowser.forceFinalSlash(file.getDirectory().getRemoteRepository().getRootUrl());
182             String JavaDoc dir = file.getDirectory().getDirectoryPath();
183             url += getCheckoutPath();
184             url += WebBrowser.forceFinalSlash(quote(dir));
185             if (file.isInAttic()) {
186                 url += "Attic/";
187             }
188             url += quote(file.getName());
189             if (getProjectRoot() != null) {
190                 url = WebBrowser.addQueryParam(url, _cvsrootParam, getProjectRoot());
191             }
192             url = WebBrowser.addQueryParam(url, "rev", file.getVersion());
193             url = WebBrowser.addQueryParam(url, getQueryParams());
194             return url;
195         } catch (URIException ex) {
196             ex.printStackTrace();
197             throw new RuntimeException JavaDoc("Cannot create URI");
198         }
199     }
200
201     public Properties JavaDoc guessWebProperties(String JavaDoc url) {
202         Properties JavaDoc properties = new Properties JavaDoc();
203         // Simple heuristic for detecting the type of the web interface
204
int keywordPosition = url.toLowerCase().indexOf(_webInterfaceType);
205         if (keywordPosition > 0) {
206             int rootUrlPosition = url.indexOf('/', keywordPosition) + 1;
207             int cgiFolderPos = url.indexOf("cgi/", rootUrlPosition);
208             if (cgiFolderPos > 0) {
209                 rootUrlPosition = cgiFolderPos + 4;
210             }
211             int nextSlashPos = url.indexOf('/', rootUrlPosition) + 1;
212             int magicScriptPos = url.indexOf(".cgi", rootUrlPosition);
213             if (magicScriptPos < 0 ) {
214                 magicScriptPos = url.indexOf(".py", rootUrlPosition);
215             }
216             if (magicScriptPos > 0 && magicScriptPos < nextSlashPos) {
217                 rootUrlPosition = nextSlashPos;
218             }
219             String JavaDoc guessedRootUrl = url.substring(0, rootUrlPosition);
220             String JavaDoc guessedPackagePath = url.substring(rootUrlPosition);
221             String JavaDoc versionTag = null;
222             String JavaDoc cvsroot = null;
223             String JavaDoc query = null;
224             int queryPos = guessedPackagePath.indexOf('?');
225             if (queryPos >= 0) {
226                 query = guessedPackagePath.substring(queryPos + 1);
227                 guessedPackagePath = guessedPackagePath.substring(0, queryPos);
228                 Properties JavaDoc queryItems = WebBrowser.getQueryParams(query);
229                 versionTag = (String JavaDoc) queryItems.remove(_tagParam);
230                 cvsroot = (String JavaDoc) queryItems.remove(_cvsrootParam);
231                 query = WebBrowser.toQueryParams(queryItems);
232             }
233             properties.put(CVSGrab.ROOT_URL_OPTION, guessedRootUrl);
234             properties.put(CVSGrab.PACKAGE_PATH_OPTION, guessedPackagePath);
235             if (versionTag != null && versionTag.trim().length() > 0) {
236                 properties.put(CVSGrab.TAG_OPTION, versionTag);
237             }
238             if (cvsroot != null && cvsroot.trim().length() > 0) {
239                 properties.put(CVSGrab.PROJECT_ROOT_OPTION, cvsroot);
240             }
241             if (query != null && query.trim().length() > 0) {
242                 properties.put(CVSGrab.QUERY_PARAMS_OPTION, query);
243             }
244         }
245         return properties;
246     }
247
248     public String JavaDoc getFilesXpath() {
249         return _filesXpath;
250     }
251
252     public String JavaDoc getFileNameXpath() {
253         return _fileNameXpath;
254     }
255
256     public String JavaDoc getFileVersionXpath() {
257         return _fileVersionXpath;
258     }
259
260     public String JavaDoc getDirectoriesXpath() {
261         return _directoriesXpath;
262     }
263
264     public String JavaDoc getDirectoryXpath() {
265         return _directoryXpath;
266     }
267
268     protected String JavaDoc getCheckoutPath() {
269         return _checkoutPath;
270     }
271
272     protected void setCheckoutPath(String JavaDoc checkoutPath) {
273         _checkoutPath = checkoutPath;
274     }
275
276     public void setDirectoryXpath(String JavaDoc directoryXpath) {
277         _directoryXpath = directoryXpath;
278     }
279
280     public void setDirectoriesXpath(String JavaDoc directoriesXpath) {
281         _directoriesXpath = directoriesXpath;
282     }
283
284     public void setFileVersionXpath(String JavaDoc fileVersionXpath) {
285         _fileVersionXpath = fileVersionXpath;
286     }
287
288     public void setFileNameXpath(String JavaDoc fileNameXpath) {
289         _fileNameXpath = fileNameXpath;
290     }
291
292     public void setFilesXpath(String JavaDoc filesXpath) {
293         _filesXpath = filesXpath;
294     }
295
296     public String JavaDoc getTagParam() {
297         return _tagParam;
298     }
299
300     public void setTagParam(String JavaDoc param) {
301         _tagParam = param;
302     }
303
304     public String JavaDoc getWebInterfaceType() {
305         return _webInterfaceType;
306     }
307
308     protected void setWebInterfaceType(String JavaDoc webInterfaceType) {
309         this._webInterfaceType = webInterfaceType;
310     }
311
312     public String JavaDoc getCvsrootParam() {
313         return _cvsrootParam;
314     }
315
316     public void setCvsrootParam(String JavaDoc cvsrootParam) {
317         _cvsrootParam = cvsrootParam;
318     }
319
320     /**
321      * @param type
322      */

323     protected void setType(String JavaDoc type) {
324         _type = type;
325     }
326
327     protected abstract String JavaDoc getVersionMarker();
328
329     protected void adjustFile(RemoteFile file, JXPathContext nodeContext) {
330         String JavaDoc fileName = file.getName();
331         if (fileName.startsWith("Attic/")) {
332             file.setName(fileName.substring(6));
333             file.setInAttic(true);
334         }
335     }
336
337     /**
338      * Python-style of URIUtil.encodePath
339      *
340      * @param original The string to quote
341      * @return the quoted string
342      */

343     protected String JavaDoc quote(String JavaDoc original) throws URIException {
344         return URIUtil.encodePath(original, "ISO-8859-1");
345     }
346     
347     protected String JavaDoc getProjectRoot() {
348         return getGrabber().getProjectRoot();
349     }
350
351 }
352
Popular Tags