KickJava   Java API By Example, From Geeks To Geeks.

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


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

6
7 package net.sourceforge.cvsgrab.web;
8
9 import net.sourceforge.cvsgrab.CVSGrab;
10 import net.sourceforge.cvsgrab.InvalidVersionException;
11 import net.sourceforge.cvsgrab.MarkerNotFoundException;
12 import net.sourceforge.cvsgrab.RemoteFile;
13 import net.sourceforge.cvsgrab.WebBrowser;
14
15 import org.apache.commons.httpclient.URIException;
16 import org.apache.commons.jxpath.JXPathContext;
17 import org.w3c.dom.Document JavaDoc;
18
19 /**
20  * Support for CvsWeb 1.0 interfaces to a cvs repository
21  *
22  * @author <a HREF="mailto:ludovicc@users.sourceforge.net">Ludovic Claude</a>
23  * @version $Revision: 1.8 $ $Date: 2005/06/24 00:04:46 $
24  * @created on 7 dec. 2003
25  */

26 public class CvsWeb1_0Interface extends ViewCvsInterface {
27
28     private String JavaDoc _root;
29     
30     /**
31      * Constructor for CvsWeb1_0Interface
32      */

33     public CvsWeb1_0Interface(CVSGrab grabber) {
34         super(grabber);
35         
36         setFilesXpath("//TR[TD//IMG/@alt = '[TXT]']");
37         setDirectoriesXpath("//TR[TD//IMG/@alt = '[DIR]'][TD/A/@name != 'Attic']");
38         setCheckoutPath("~checkout~/");
39         setWebInterfaceType("cvsweb");
40     }
41
42     /**
43      * {@inheritDoc}
44      * @param htmlPage The web page
45      * @throws MarkerNotFoundException if the version marker for the web interface was not found
46      * @throws InvalidVersionException if the version detected is incompatible with the version supported by this web interface.
47      */

48     public void detect(Document JavaDoc htmlPage) throws MarkerNotFoundException, InvalidVersionException {
49         JXPathContext context = JXPathContext.newContext(htmlPage);
50         context.setLenient(true);
51         // Check that this is CvsWeb
52
String JavaDoc generator = (String JavaDoc) context.getValue("//comment()[starts-with(normalize-space(.),'hennerik CVSweb')]");
53         
54         if (generator == null) {
55             throw new MarkerNotFoundException("Not CvsWeb, did not found comment containing 'hennerik CVSweb'");
56         }
57         
58         setType(generator);
59         
60         _root = getGrabber().getProjectRoot();
61         if (_root == null) {
62             String JavaDoc href = (String JavaDoc) context.getValue("//A/@href[contains(., 'cvsroot=')]");
63             if (href != null) {
64                 _root = href.substring(href.indexOf("cvsroot=") + 8);
65                 if (_root.indexOf('#') > 0) {
66                     _root = _root.substring(0, _root.indexOf('#'));
67                 }
68                 if (_root.indexOf('&') > 0) {
69                     _root = _root.substring(0, _root.indexOf('&'));
70                 }
71             }
72         }
73         
74     }
75         
76     /**
77      * {@inheritDoc}
78      */

79     protected String JavaDoc getVersionMarker() {
80         return null;
81     }
82     
83     /**
84      * @return the base url to use when trying to auto-detect this type of web interface
85      */

86     public String JavaDoc getBaseUrl() {
87         String JavaDoc url = WebBrowser.forceFinalSlash(getGrabber().getRootUrl());
88         url += getGrabber().getPackagePath();
89         url = WebBrowser.addQueryParam(url, getGrabber().getQueryParams());
90         if (getGrabber().getProjectRoot() != null) {
91             url = WebBrowser.addQueryParam(url, "cvsroot", getGrabber().getProjectRoot());
92         }
93         return url;
94     }
95     
96     /**
97      * @return the alternate base url to use when trying to auto-detect this type of web interface
98      */

99     public String JavaDoc getAltBaseUrl() {
100         String JavaDoc url = WebBrowser.forceFinalSlash(getGrabber().getRootUrl());
101         url = WebBrowser.addQueryParam(url, getGrabber().getQueryParams());
102         return url;
103     }
104
105     /**
106      * @return the url to use to access the contents of the repository
107      */

108     public String JavaDoc getDirectoryUrl(String JavaDoc rootUrl, String JavaDoc directoryName) {
109         try {
110             String JavaDoc url = super.getDirectoryUrl(rootUrl, directoryName);
111             if (_root != null) {
112                 url = WebBrowser.addQueryParam(url, "cvsroot", quote(_root));
113             }
114             url = WebBrowser.addQueryParam(url, getQueryParams());
115             return url;
116         } catch (URIException ex) {
117             ex.printStackTrace();
118             throw new RuntimeException JavaDoc("Cannot create URI");
119         }
120     }
121     
122     public String JavaDoc getDownloadUrl( RemoteFile file) {
123         try {
124             // http://cvs.hispalinux.es/cgi-bin/cvsweb/~checkout~/mono/README?rev=1.26&content-type=text/plain&cvsroot=mono
125
String JavaDoc url = super.getDownloadUrl(file);
126             if (_root != null) {
127                 url = WebBrowser.addQueryParam(url, "cvsroot", quote(_root));
128             }
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     public String JavaDoc getRoot() {
138         return _root;
139     }
140
141     public void setRoot(String JavaDoc root) {
142         _root = root;
143     }
144
145 }
146
Popular Tags