Skip to content

Commit

Permalink
Fixed the code to support the current Gist with multifile support (ad…
Browse files Browse the repository at this point in the history
…resses #3113)
  • Loading branch information
egonw committed Jan 18, 2012
1 parent c98ee9e commit 67b3ed5
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 171 deletions.
Expand Up @@ -10,6 +10,8 @@
******************************************************************************/
package net.bioclipse.gist.test;

import java.util.List;

import net.bioclipse.gist.business.IGistManager;

import org.eclipse.core.resources.IFile;
Expand All @@ -28,37 +30,23 @@ public abstract class AbstractGistManagerPluginTest {

protected static IFile file
= net.bioclipse.core.Activator.getVirtualProject()
.getFile( new Path(FILENAME) );
.getFile( new Path(PATH) );

@BeforeClass
public static void startUIPlugin() {
net.bioclipse.ui.Activator.getDefault();
}

@Test
public void downloadIntString() throws Exception {
String target = PATH + FILENAME;
String foo = gist.download(GIST_NUM, target);
Assert.assertEquals(target, foo);
}

@Test
public void downloadIntIfile() {
String target = PATH + FILENAME;
IFile foo = gist.download(GIST_NUM, file);
Assert.assertEquals( target, foo.getLocationURI().getPath() );
}

@Test
public void downloadIntIFileHook() {
throw new RuntimeException( "Not yet implemented" );
}

@Test
public void downloadInt() {
String foo = gist.download(GIST_NUM);
Assert.assertNotNull( foo );
Assert.assertFalse( "".equals( foo ) );
List<IFile> foo = gist.download(GIST_NUM);
Assert.assertNotNull(foo);
Assert.assertNotSame(0, foo.size());
}

@Test
Expand Down
Expand Up @@ -16,23 +16,20 @@
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;

import net.bioclipse.core.ResourcePathTransformer;
import net.bioclipse.core.business.BioclipseException;
import net.bioclipse.jobs.IReturner;
import net.bioclipse.managers.business.IBioclipseManager;

import org.apache.log4j.Logger;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IFolder;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IWorkspace;
import org.eclipse.core.resources.IWorkspaceRoot;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.swt.SWT;
Expand All @@ -45,118 +42,86 @@ public class GistManager implements IBioclipseManager {
private static final Logger logger = Logger.getLogger(GistManager.class);

public final static String GIST_PROJECT = "Gists";

private static String findUnusedFileName( IFolder currentFolder,
String prefix,
String suffix ) {
String fileName = prefix + suffix;
IPath path = currentFolder.getFullPath();
path = path.append( fileName );
IFile file = currentFolder.getFile( path );
int i=0;
while(file.exists()) {
i++;
path = path.removeLastSegments( 1 );
path = path.append( prefix+i+suffix );
IPath copy = path.removeFirstSegments( 1 );
file = currentFolder.getProject().getFile( copy );
}
fileName = path.lastSegment();
return fileName;
}

private IFolder getProjectDirectory(IProgressMonitor monitor)
throws CoreException {
IWorkspace workspace = ResourcesPlugin.getWorkspace();
IWorkspaceRoot root = workspace.getRoot();
IProject project = root.getProject(GIST_PROJECT);
if (!project.exists()) project.create(monitor);
if (!project.isOpen()) project.open(monitor);

return project.getFolder(GIST_PROJECT);
}

public IFile download(int gist, IProgressMonitor monitor)
throws BioclipseException {
IFolder project;
try {
project = getProjectDirectory(monitor);
}
catch ( CoreException e ) {
throw new BioclipseException( "Could not find a project " +
"to save the gist in." );
}
String tName = findUnusedFileName(project, gist + ".0", ".js");
tName = project.getFullPath().toString() + tName;
IFile target = ResourcePathTransformer.getInstance().transform(tName);
return download(gist, target, monitor);
private class GistFile {
String rev;
String filename;
GistFile(String rev, String filename) {
this.rev = rev;
this.filename = filename;
}
}

public IFile download(int gist, IFile target, IProgressMonitor monitor)
public void download(int gist, IReturner<IFile> returner, IProgressMonitor monitor)
throws BioclipseException {
if (monitor == null) {
monitor = new NullProgressMonitor();
}

if (target == null) {
throw new BioclipseException("Cannot save to a NULL file.");
}

monitor.beginTask("Downloading Gist...", 2);

try {
monitor.subTask("Determining Gist revision");
URL gistURL = new URL("https://gist.github.com/" + gist);
URLConnection gistConn = gistURL.openConnection();

String rawURLPattern = "\"/raw/" + gist + "/([0-9[a-f]]+)/gistfile1.txt";
String rawURLPattern = "\"/raw/" + gist + "/([0-9[a-f]]+)/([^\"]+)";
Pattern p = Pattern.compile(rawURLPattern);


// parse the HTML and extract the links to the raw files
List<GistFile> files = new ArrayList<GistFile>();
BufferedReader reader = new BufferedReader(new InputStreamReader(gistConn.getInputStream()));
String line = reader.readLine();
String rev = null;
while (line != null) {
Matcher m = p.matcher(line);
if (m.find()) {
rev = m.group(1);
logger.debug("Found revision: " + rev);
reader.close();
break;
String rev = m.group(1);
String filename = m.group(2);
logger.debug("Found file: " + filename);
System.out.println("Found file: " + filename);
files.add(new GistFile(rev, filename));
}
line = reader.readLine();
}
monitor.worked(1);

if (monitor.isCanceled()) {
return null;
}
if (monitor.isCanceled()) return;

if (rev != null) {
monitor.subTask("Downloading Gist revision: " + rev);
URL rawURL = new URL(
"https://gist.github.com/raw/" + gist + "/" +
rev + "/gistfile1.txt"
);
logger.debug("Downloading Gist as raw from URL: " + rawURL.toString());
URLConnection rawConn = rawURL.openConnection();
if (target.exists()) {
target.setContents(rawConn.getInputStream(), true, false, null);
} else {
target.create(rawConn.getInputStream(), false, null);
}
monitor.worked(1);
// download
if (files.size() > 0) {
for (GistFile file : files) {
String link = "https://gist.github.com/raw/" + gist + "/" + file.rev + "/" + file.filename;
monitor.subTask("Downloading Gist revision: " + link);
URL rawURL = new URL(link);
logger.debug("Downloading Gist as raw from URL: " + rawURL.toString());
System.out.println("Downloading Gist as raw from URL: " + link);
System.out.println("Downloading Gist as raw from URL: " + rawURL.toString());
URLConnection rawConn = rawURL.openConnection();

IFile targetFile = ResourcePathTransformer.getInstance().transform(
GIST_PROJECT + "/" + gist + "_" + file.filename
);
if (targetFile.exists()) {
targetFile.setContents(rawConn.getInputStream(), true, false, null);
} else {
targetFile.create(rawConn.getInputStream(), false, null);
}
returner.partialReturn(targetFile);
if (monitor.isCanceled()) return;
}
monitor.worked(1);
} else {
Display.getDefault().syncExec(
new Runnable() {
public void run(){
MessageBox mb = new MessageBox(new Shell(), SWT.OK);
mb.setText("Gist Download Error");
mb.setMessage("Could not find Gist");
mb.setMessage("Could not find the gist");
mb.open();
}
});
monitor.done();
return null;
return;
}
} catch (PatternSyntaxException exception) {
exception.printStackTrace();
Expand All @@ -174,7 +139,7 @@ public void run(){
finally {
monitor.done();
}
return target;
return;
}

public String getManagerName() {
Expand Down
Expand Up @@ -10,17 +10,18 @@
******************************************************************************/
package net.bioclipse.gist.business;

import java.util.List;

import net.bioclipse.core.PublishedClass;
import net.bioclipse.core.PublishedMethod;
import net.bioclipse.core.Recorded;
import net.bioclipse.core.TestClasses;

import org.eclipse.core.resources.IFile;

import net.bioclipse.jobs.BioclipseJob;
import net.bioclipse.jobs.BioclipseJobUpdateHook;
import net.bioclipse.managers.business.IBioclipseManager;

import org.eclipse.core.resources.IFile;

@PublishedClass("The gist manager is used for downloading gists")
@TestClasses(
"net.bioclipse.gist.test.APITest," +
Expand All @@ -29,29 +30,12 @@
)
public interface IGistManager extends IBioclipseManager {

@Recorded
@PublishedMethod(
params = "int gist, String path",
methodSummary = "Downloads the Gist with the given number to the " +
"given path and returns the target path"
)
public String download( int gist, String path );

public IFile download( int gist,
IFile target );

public BioclipseJob<IFile> download( int gist,
IFile target,
BioclipseJobUpdateHook hook );

@Recorded
@PublishedMethod(
params = "int gist",
methodSummary = "Downloads the Gist with the given number to the " +
"project 'Gists/' and returns the path"
)
public String download(int gist);

public BioclipseJob<IFile> download( int gist,
BioclipseJobUpdateHook hook );
public List<IFile> download(int gist);
public BioclipseJob<IFile> download( int gist, BioclipseJobUpdateHook<List<IFile>> hook );
}

0 comments on commit 67b3ed5

Please sign in to comment.