KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * CVSGrab
3  * Author: Ludovic Claude (ludovicc@users.sourceforge.net)
4  * Distributable under BSD license.
5  * See terms of license at gnu.org.
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 /**
21  * Support for ViewCvs 1.0 interfaces to a cvs repository
22  *
23  * @author <a HREF="mailto:ludovicc@users.sourceforge.net">Ludovic Claude</a>
24  * @version $Revision: 1.15 $ $Date: 2005/06/24 16:38:08 $
25  * @created on 11 oct. 2003
26  */

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

34     public ViewCvs1_0Interface(CVSGrab grabber) {
35         super(grabber);
36         setFileVersionXpath("TD/A/B");
37         setFilesXpath("//TR[TD/A/IMG/@alt = '(file)' or contains(TD/A/IMG/@src, 'text')]");
38         //private String _fileVersionXpath = "TD[A/IMG/@alt != '(graph)'][2]/A/B";
39
setDirectoriesXpath("//TR[TD/A/IMG/@alt = '(dir)' or contains(TD/A/IMG/@src, 'dir')][TD/A/@name != 'Attic']");
40         
41     }
42
43     /**
44      * {@inheritDoc}
45      * @param htmlPage The web page
46      * @throws MarkerNotFoundException if the version marker for the web interface was not found
47      * @throws InvalidVersionException if the version detected is incompatible with the version supported by this web interface.
48      */

49     public void detect(Document JavaDoc htmlPage) throws MarkerNotFoundException, InvalidVersionException {
50         super.detect(htmlPage);
51         _root = getGrabber().getProjectRoot();
52         
53         if (_root == null) {
54             JXPathContext context = JXPathContext.newContext(htmlPage);
55             context.setLenient(true);
56             String JavaDoc href = (String JavaDoc) context.getValue("//A/@href[contains(., 'root=')]");
57             if (href == null) {
58                 CVSGrab.getLog().warn("CVS Root not found, there may be issues if ViewCvs is used with multiple repositories");
59                 CVSGrab.getLog().warn("Use the parameter -cvsRoot <root> to remove this warning");
60             } else {
61                 _root = href.substring(href.indexOf("root=")+ 5);
62                 if (_root.indexOf('#') > 0) {
63                     _root = _root.substring(0, _root.indexOf('#'));
64                 }
65                 if (_root.indexOf('&') > 0) {
66                     _root = _root.substring(0, _root.indexOf('&'));
67                 }
68             }
69         }
70         
71         // Check that CVSGrab is not used on projects hosted on a SVN repository
72
if (getGrabber().getRootUrl() != null && getGrabber().getRootUrl().startsWith("http://cvs.apache.org/viewcvs.cgi/") && _root != null && _root.indexOf("SVN") >= 0) {
73             throw new InvalidVersionException("CVSGrab cannot be used on projects hosted in a SVN repository");
74         }
75     }
76
77     /**
78      * @return the base url to use when trying to auto-detect this type of web interface
79      */

80     public String JavaDoc getBaseUrl() {
81         String JavaDoc url = WebBrowser.forceFinalSlash(getGrabber().getRootUrl());
82         url += getGrabber().getPackagePath();
83         url = WebBrowser.addQueryParam(url, getGrabber().getQueryParams());
84         if (getGrabber().getProjectRoot() != null) {
85             url = WebBrowser.addQueryParam(url, "root", getGrabber().getProjectRoot());
86         }
87         return url;
88     }
89     
90     /**
91      * @return the alternate base url to use when trying to auto-detect this type of web interface
92      */

93     public String JavaDoc getAltBaseUrl() {
94         String JavaDoc url = WebBrowser.forceFinalSlash(getGrabber().getRootUrl());
95         url = WebBrowser.addQueryParam(url, getGrabber().getQueryParams());
96         return url;
97     }
98
99     /**
100      * @param rootUrl
101      * @param directoryName
102      * @return the url to use to access the contents of the repository
103      */

104     public String JavaDoc getDirectoryUrl(String JavaDoc rootUrl, String JavaDoc directoryName) {
105         try {
106             String JavaDoc url = super.getDirectoryUrl(rootUrl, directoryName);
107             if (_root != null) {
108                 url = WebBrowser.addQueryParam(url, "root", quote(_root));
109             }
110             url = WebBrowser.addQueryParam(url, getQueryParams());
111             return url;
112         } catch (URIException ex) {
113             ex.printStackTrace();
114             throw new RuntimeException JavaDoc("Cannot create URI");
115         }
116     }
117     
118     /**
119      * @param file
120      * @return
121      */

122     public String JavaDoc getDownloadUrl( RemoteFile file) {
123         try {
124             // http://cvs.apache.org/viewcvs.cgi/*checkout*/jakarta-regexp/KEYS?rev=1.1
125
String JavaDoc url = super.getDownloadUrl(file);
126             if (_root != null) {
127                 url = WebBrowser.addQueryParam(url, "root", 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     /**
138      * @return
139      */

140     public String JavaDoc getRoot() {
141         return _root;
142     }
143
144     /**
145      * @param root
146      */

147     public void setRoot(String JavaDoc root) {
148         _root = root;
149     }
150
151     protected String JavaDoc getVersionMarker() {
152         return "ViewCVS 1.0";
153     }
154
155     protected void adjustFile(RemoteFile file, JXPathContext nodeContext) {
156         super.adjustFile(file, nodeContext);
157         String JavaDoc href = (String JavaDoc) nodeContext.getValue("TD[1]/A/@href");
158         if (href.indexOf("/Attic/" + file.getName()) >= 0) {
159             file.setInAttic(true);
160         }
161     }
162     
163 }
164
Popular Tags