1 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 ; 17 18 import java.util.ArrayList ; 19 import java.util.Iterator ; 20 import java.util.List ; 21 import java.util.Properties ; 22 23 24 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 46 public void detect(Document 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 type = "FishEye"; 53 54 context.setLenient(true); 55 String version = (String ) context.getValue("//DIV[@id='footer']/P[A/@href='http://www.cenqua.com/fisheye/']/text()"); 57 58 if (version == null) { 60 version = (String ) 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 73 public String getDirectoryUrl(String rootUrl, String directoryName) { 74 try { 75 String 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 ("Cannot create URI"); 85 } 86 } 87 88 91 public String [] getDirectories(Document htmlPage) { 92 JXPathContext context = JXPathContext.newContext(htmlPage); 93 List directories = new ArrayList (); 94 Iterator i = context.iteratePointers(getDirectoriesXpath()); 95 while (i.hasNext()) { 96 Pointer pointer = (Pointer) i.next(); 97 JXPathContext nodeContext = context.getRelativeContext(pointer); 98 String dir = (String ) nodeContext.getValue(getDirectoryXpath()); 99 dir = WebBrowser.removeFinalSlash(dir); 100 dir = dir.substring(dir.lastIndexOf('/') + 1); 101 directories.add(dir); 102 } 103 return (String []) directories.toArray(new String [directories.size()]); 104 } 105 106 109 public String getDownloadUrl(RemoteFile file) { 110 try { 111 String url = WebBrowser.forceFinalSlash(file.getDirectory().getRemoteRepository().getRootUrl()); 113 url += "~raw,r=" + file.getVersion(); 114 url = WebBrowser.forceFinalSlash(url); 115 String 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 ("Cannot create URI"); 122 } 123 } 124 125 public Properties guessWebProperties(String url) { 126 Properties properties = new Properties (); 127 int keywordPosition = url.toLowerCase().indexOf(getWebInterfaceType()); 129 if (keywordPosition > 0) { 130 String versionTag = null; 131 String 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 guessedRootUrl = url.substring(0, rootUrlPosition); 148 String 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 180 protected String quote(String original) throws URIException { 181 return URIUtil.encodePath(original, "ISO-8859-1"); 182 } 183 184 187 protected String 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 |