KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > cvsgrab > web > FishEye1_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 FishEye 0.8, 0.9 and maybe 1.0 interfaces to a cvs repository
26  *
27  * @author lclaude
28  * @date 30 mars 2004
29  */

30 public class FishEye1_0Interface extends ViewCvsInterface {
31
32     public FishEye1_0Interface(CVSGrab grabber) {
33         super(grabber);
34         
35         setFilesXpath("//TABLE[@id='fileTable']//TR[@class='ftFileRow' or @class='ftFileRowDeleted']");
36         setFileNameXpath("TD/A[@class='ftFileName']/text()");
37         setFileVersionXpath("TD/A[@class='ftRev'][@title='view' or @title='history' or @title='download']/text()");
38         setDirectoriesXpath("//DIV[@class='dirPane']/UL/LI/SPAN[@class='folder'][A/SPAN/@class='folderPlain']");
39         setDirectoryXpath("A/SPAN[@class='folderPlain']/text()");
40         setWebInterfaceType("viewrep");
41     }
42
43     /**
44      * {@inheritDoc}
45      */

46     public void detect(Document JavaDoc htmlPage) throws MarkerNotFoundException, InvalidVersionException {
47
48         JXPathContext context = JXPathContext.newContext(htmlPage);
49         context.setLenient(false);
50         context.getValue("//DIV[@id='footer']//A[starts-with(@href,'http://www.cenqua.com/fisheye/')]");
51         
52         String JavaDoc type = "FishEye";
53
54         context.setLenient(true);
55         // Search version on FishEye 0.8
56
String JavaDoc version = (String JavaDoc) context.getValue("//DIV[@id='footer']/P[A/@href='http://www.cenqua.com/fisheye/']/text()");
57
58         // Search version on FishEye 1.0
59
if (version == null) {
60             version = (String JavaDoc) context.getValue("//DIV[@id='footer']/text()[string-length(.) > 0]");
61         }
62         
63         if (version == null) {
64             version = "Unknown version";
65         }
66         setType(type + " " + version);
67
68     }
69
70     /**
71      * {@inheritDoc}
72      */

73     public String JavaDoc getDirectoryUrl(String JavaDoc rootUrl, String JavaDoc directoryName) {
74         try {
75             String JavaDoc url = WebBrowser.forceFinalSlash(rootUrl);
76             if (getVersionTag() != null) {
77                 url += "~br=" + getVersionTag() + "/";
78             }
79             url += WebBrowser.forceFinalSlash(quote(directoryName));
80             url = WebBrowser.addQueryParam(url, getQueryParams());
81             return url;
82         } catch (URIException ex) {
83             ex.printStackTrace();
84             throw new RuntimeException JavaDoc("Cannot create URI");
85         }
86     }
87
88     /**
89      * {@inheritDoc}
90      */

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

109     public String JavaDoc getDownloadUrl(RemoteFile file) {
110         try {
111             // http://cvs.codehaus.org/viewrep/~raw,r=1.12.2.1/picocontainer/java/picocontainer/build.xml
112
String JavaDoc url = WebBrowser.forceFinalSlash(file.getDirectory().getRemoteRepository().getRootUrl());
113             url += "~raw,r=" + file.getVersion();
114             url = WebBrowser.forceFinalSlash(url);
115             String JavaDoc dir = file.getDirectory().getDirectoryPath();
116             url += WebBrowser.forceFinalSlash(quote(dir));
117             url += quote(file.getName());
118             return url;
119         } catch (URIException ex) {
120             ex.printStackTrace();
121             throw new RuntimeException JavaDoc("Cannot create URI");
122         }
123     }
124
125     public Properties JavaDoc guessWebProperties(String JavaDoc url) {
126         Properties JavaDoc properties = new Properties JavaDoc();
127         // Simple heuristic for detecting the type of the web interface
128
int keywordPosition = url.toLowerCase().indexOf(getWebInterfaceType());
129         if (keywordPosition > 0) {
130             String JavaDoc versionTag = null;
131             String JavaDoc query = null;
132             int rootUrlPosition = url.indexOf('/', keywordPosition) + 1;
133             int startPackagePath = rootUrlPosition;
134             if ('~' == url.charAt(rootUrlPosition)) {
135                 startPackagePath = url.indexOf('/', rootUrlPosition) + 1;
136                 int eqPos = url.indexOf('=', rootUrlPosition);
137                 if ("br".equals(url.substring(rootUrlPosition + 1, eqPos))) {
138                     int endVersionTag = startPackagePath - 1;
139                     int commaPos = url.indexOf(',', eqPos);
140                     if (commaPos > 0) {
141                         endVersionTag = commaPos;
142                     }
143                     versionTag = url.substring(eqPos + 1, endVersionTag);
144                 }
145             }
146
147             String JavaDoc guessedRootUrl = url.substring(0, rootUrlPosition);
148             String JavaDoc guessedPackagePath = url.substring(startPackagePath);
149
150             properties.put(CVSGrab.ROOT_URL_OPTION, guessedRootUrl);
151             properties.put(CVSGrab.PACKAGE_PATH_OPTION, guessedPackagePath);
152             if (versionTag != null && versionTag.trim().length() > 0) {
153                 properties.put(CVSGrab.TAG_OPTION, versionTag);
154             }
155             int queryPos = guessedPackagePath.indexOf('?');
156             if (queryPos >= 0) {
157                 query = guessedPackagePath.substring(queryPos + 1);
158                 guessedPackagePath = guessedPackagePath.substring(0, queryPos);
159             } else {
160                 query = "hideDeletedFiles=Y";
161             }
162             properties.put(CVSGrab.ROOT_URL_OPTION, guessedRootUrl);
163             properties.put(CVSGrab.PACKAGE_PATH_OPTION, guessedPackagePath);
164             if (versionTag != null && versionTag.trim().length() > 0) {
165                 properties.put(CVSGrab.TAG_OPTION, versionTag);
166             }
167             if (query != null && query.trim().length() > 0) {
168                 properties.put(CVSGrab.QUERY_PARAMS_OPTION, query);
169             }
170         }
171         return properties;
172     }
173
174     /**
175      * Python-style of URIUtil.encodePath
176      *
177      * @param original The string to quote
178      * @return the quoted string
179      */

180     protected String JavaDoc quote(String JavaDoc original) throws URIException {
181         return URIUtil.encodePath(original, "ISO-8859-1");
182     }
183
184     /**
185      * {@inheritDoc}
186      */

187     protected String JavaDoc getVersionMarker() {
188         return null;
189     }
190
191     protected void adjustFile(RemoteFile file, JXPathContext nodeContext) {
192         if ("ftFileRowDeleted".equals(nodeContext.getValue("@class"))) {
193             file.setInAttic(true);
194         }
195     }
196 }
197
Popular Tags