KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * Insito J2EE - Copyright 2003-2004 Finance Active
3  */

4 package net.sourceforge.cvsgrab.web;
5
6 import net.sourceforge.cvsgrab.CVSGrab;
7 import net.sourceforge.cvsgrab.InvalidVersionException;
8 import net.sourceforge.cvsgrab.MarkerNotFoundException;
9 import net.sourceforge.cvsgrab.RemoteFile;
10 import net.sourceforge.cvsgrab.WebBrowser;
11
12 import org.apache.commons.httpclient.URIException;
13 import org.apache.commons.httpclient.util.URIUtil;
14 import org.apache.commons.jxpath.JXPathContext;
15 import org.apache.commons.jxpath.Pointer;
16 import org.w3c.dom.Document JavaDoc;
17
18 import java.util.ArrayList JavaDoc;
19 import java.util.Iterator JavaDoc;
20 import java.util.List JavaDoc;
21 import java.util.Properties JavaDoc;
22
23
24 /**
25  * Support for Chora 2.0 interfaces to a cvs repository
26  *
27  * @author lclaude
28  * @created 30 mars 2004
29  */

30 public class Chora2_0Interface extends ViewCvsInterface {
31
32     private String JavaDoc _browsePath = "cvs.php";
33     
34     public Chora2_0Interface(CVSGrab grabber) {
35         super(grabber);
36         
37         setFilesXpath("//TR[TD/A/IMG/@alt = 'File']");
38         setFileNameXpath("TD[1]/A/@href");
39         setFileVersionXpath("TD[2]/B/A");
40         setDirectoriesXpath("//TR[TD/A/IMG/@alt = 'Directory']");
41         setDirectoryXpath("TD[1]/A/@href");
42         setCheckoutPath("co.php");
43         setWebInterfaceType("cvs.php/");
44     }
45
46     protected String JavaDoc getBrowsePath() {
47         return _browsePath;
48     }
49
50     protected void setBrowsePath(String JavaDoc browsePath) {
51         _browsePath = browsePath;
52     }
53     
54     /**
55      * {@inheritDoc}
56      */

57     public void detect(Document JavaDoc htmlPage) throws MarkerNotFoundException, InvalidVersionException {
58         String JavaDoc rootUrl = WebBrowser.removeFinalSlash(getGrabber().getRootUrl());
59         if (!rootUrl.endsWith(getBrowsePath())) {
60             throw new MarkerNotFoundException("Root url should end with " + getBrowsePath());
61         }
62         JXPathContext context = JXPathContext.newContext(htmlPage);
63         String JavaDoc type = (String JavaDoc) context.getValue("//IMG[contains(@src,'chora.gif')]");
64
65         if (type == null) {
66             throw new MarkerNotFoundException("Expected marker 'chora.gif', found none");
67         }
68         
69         // Version cannot be found exactly
70
setType("Chora 2.x");
71         
72         if (getGrabber().getVersionTag() != null) {
73             throw new InvalidVersionException("Chora 2.0 doesn't support version tags");
74         }
75     }
76
77     /**
78      * {@inheritDoc}
79      */

80     public String JavaDoc getDirectoryUrl(String JavaDoc rootUrl, String JavaDoc directoryName) {
81         try {
82             String JavaDoc url = WebBrowser.forceFinalSlash(rootUrl);
83             url += WebBrowser.forceFinalSlash(quote(directoryName));
84             url = WebBrowser.addQueryParam(url, getQueryParams());
85             return url;
86         } catch (URIException ex) {
87             ex.printStackTrace();
88             throw new RuntimeException JavaDoc("Cannot create URI");
89         }
90     }
91
92     /**
93      * {@inheritDoc}
94      */

95     public String JavaDoc[] getDirectories(Document JavaDoc htmlPage) {
96         JXPathContext context = JXPathContext.newContext(htmlPage);
97         List JavaDoc directories = new ArrayList JavaDoc();
98         Iterator JavaDoc i = context.iteratePointers(getDirectoriesXpath());
99         while (i.hasNext()) {
100             Pointer pointer = (Pointer) i.next();
101             JXPathContext nodeContext = context.getRelativeContext(pointer);
102             String JavaDoc dir = (String JavaDoc) nodeContext.getValue(getDirectoryXpath());
103             dir = WebBrowser.removeFinalSlash(dir);
104             dir = dir.substring(dir.lastIndexOf('/') + 1);
105             directories.add(dir);
106         }
107         return (String JavaDoc[]) directories.toArray(new String JavaDoc[directories.size()]);
108     }
109
110     /**
111      * {@inheritDoc}
112      */

113     public RemoteFile[] getFiles(Document JavaDoc htmlPage) {
114         JXPathContext context = JXPathContext.newContext(htmlPage);
115         List JavaDoc files = new ArrayList JavaDoc();
116         Iterator JavaDoc i = context.iteratePointers(getFilesXpath());
117         while (i.hasNext()) {
118             Pointer pointer = (Pointer) i.next();
119             JXPathContext nodeContext = context.getRelativeContext(pointer);
120             String JavaDoc fileName = (String JavaDoc) nodeContext.getValue(getFileNameXpath());
121             fileName = fileName.substring(fileName.lastIndexOf('/') + 1);
122             String JavaDoc version = (String JavaDoc) nodeContext.getValue(getFileVersionXpath());
123             RemoteFile file = new RemoteFile(fileName, version);
124             //adjustFile(file, nodeContext);
125
files.add(file);
126         }
127         return (RemoteFile[]) files.toArray(new RemoteFile[files.size()]);
128     }
129     
130     /**
131      * {@inheritDoc}
132      */

133     public String JavaDoc getDownloadUrl(RemoteFile file) {
134         try {
135             // http://cvs.php.net/co.php/smarty/INSTALL?r=1.12&p=1
136
String JavaDoc url = WebBrowser.forceFinalSlash(file.getDirectory().getRemoteRepository().getRootUrl());
137             if (getBrowsePath().length() > 0) {
138                 url = url.substring(0, url.length() - getBrowsePath().length() - 1);
139             }
140             url = WebBrowser.forceFinalSlash(url + getCheckoutPath());
141             String JavaDoc dir = file.getDirectory().getDirectoryPath();
142             url += WebBrowser.forceFinalSlash(quote(dir));
143             //if (file.isInAttic()) {
144
// url += "Attic/";
145
//}
146
url += quote(file.getName());
147             url = WebBrowser.addQueryParam(url, "r", file.getVersion());
148             url = WebBrowser.addQueryParam(url, "p", "1");
149             url = WebBrowser.addQueryParam(url, getQueryParams());
150             return url;
151         } catch (URIException ex) {
152             ex.printStackTrace();
153             throw new RuntimeException JavaDoc("Cannot create URI");
154         }
155     }
156
157     /**
158      * Python-style of URIUtil.encodePath
159      *
160      * @param original The string to quote
161      * @return the quoted string
162      */

163     protected String JavaDoc quote(String JavaDoc original) throws URIException {
164         return URIUtil.encodePath(original, "ISO-8859-1");
165     }
166
167     /**
168      * {@inheritDoc}
169      */

170     protected String JavaDoc getVersionMarker() {
171         return null;
172     }
173
174     
175     protected void adjustFile(RemoteFile file, JXPathContext nodeContext) {
176         // do nothing
177
}
178
179     public boolean presetMatch(String JavaDoc rootUrl, String JavaDoc packagePath) {
180         if (rootUrl.indexOf("cvs.php.net") > 0) {
181             setType("Chora 2.0 on php.net");
182             _browsePath = "";
183             return true;
184         }
185         return false;
186     }
187
188     public Properties JavaDoc guessWebProperties(String JavaDoc url) {
189         // Handles cvs.php.net
190
if (url.startsWith("http://cvs.php.net/")) {
191             Properties JavaDoc properties = new Properties JavaDoc();
192             properties.put(CVSGrab.ROOT_URL_OPTION, "http://cvs.php.net/");
193             properties.put(CVSGrab.PACKAGE_PATH_OPTION, url.substring("http://cvs.php.net/".length()));
194             _browsePath = "";
195             return properties;
196         }
197         return super.guessWebProperties(url);
198     }
199
200     
201 }
202
Popular Tags