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 Chora2_0Interface extends ViewCvsInterface { 31 32 private String _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 getBrowsePath() { 47 return _browsePath; 48 } 49 50 protected void setBrowsePath(String browsePath) { 51 _browsePath = browsePath; 52 } 53 54 57 public void detect(Document htmlPage) throws MarkerNotFoundException, InvalidVersionException { 58 String 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 type = (String ) 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 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 80 public String getDirectoryUrl(String rootUrl, String directoryName) { 81 try { 82 String 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 ("Cannot create URI"); 89 } 90 } 91 92 95 public String [] getDirectories(Document htmlPage) { 96 JXPathContext context = JXPathContext.newContext(htmlPage); 97 List directories = new ArrayList (); 98 Iterator i = context.iteratePointers(getDirectoriesXpath()); 99 while (i.hasNext()) { 100 Pointer pointer = (Pointer) i.next(); 101 JXPathContext nodeContext = context.getRelativeContext(pointer); 102 String dir = (String ) nodeContext.getValue(getDirectoryXpath()); 103 dir = WebBrowser.removeFinalSlash(dir); 104 dir = dir.substring(dir.lastIndexOf('/') + 1); 105 directories.add(dir); 106 } 107 return (String []) directories.toArray(new String [directories.size()]); 108 } 109 110 113 public RemoteFile[] getFiles(Document htmlPage) { 114 JXPathContext context = JXPathContext.newContext(htmlPage); 115 List files = new ArrayList (); 116 Iterator i = context.iteratePointers(getFilesXpath()); 117 while (i.hasNext()) { 118 Pointer pointer = (Pointer) i.next(); 119 JXPathContext nodeContext = context.getRelativeContext(pointer); 120 String fileName = (String ) nodeContext.getValue(getFileNameXpath()); 121 fileName = fileName.substring(fileName.lastIndexOf('/') + 1); 122 String version = (String ) nodeContext.getValue(getFileVersionXpath()); 123 RemoteFile file = new RemoteFile(fileName, version); 124 files.add(file); 126 } 127 return (RemoteFile[]) files.toArray(new RemoteFile[files.size()]); 128 } 129 130 133 public String getDownloadUrl(RemoteFile file) { 134 try { 135 String 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 dir = file.getDirectory().getDirectoryPath(); 142 url += WebBrowser.forceFinalSlash(quote(dir)); 143 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 ("Cannot create URI"); 154 } 155 } 156 157 163 protected String quote(String original) throws URIException { 164 return URIUtil.encodePath(original, "ISO-8859-1"); 165 } 166 167 170 protected String getVersionMarker() { 171 return null; 172 } 173 174 175 protected void adjustFile(RemoteFile file, JXPathContext nodeContext) { 176 } 178 179 public boolean presetMatch(String rootUrl, String 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 guessWebProperties(String url) { 189 if (url.startsWith("http://cvs.php.net/")) { 191 Properties properties = new Properties (); 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 |