Skip to content

Commit

Permalink
Cleaning up
Browse files Browse the repository at this point in the history
  • Loading branch information
dkfellows committed Oct 24, 2014
1 parent bc6c80b commit 411dda4
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 54 deletions.
Expand Up @@ -25,11 +25,9 @@
import javax.swing.JComponent;

public interface Renderer {
boolean canHandle(String mimeType);

public boolean canHandle(String mimeType);

public JComponent getComponent(Path path) throws RendererException;

public String getType();
JComponent getComponent(Path path) throws RendererException;

String getType();
}
Expand Up @@ -21,13 +21,13 @@
package net.sf.taverna.t2.renderers;

/**
* If a renderer fails for any reason then throw one of these with an appropriate
* message
* If a renderer fails for any reason then throw one of these with an
* appropriate message.
*
* @author Ian Dunlop
*
*/
public class RendererException extends Exception {
private static final long serialVersionUID = 713914849694276998L;

public RendererException() {
}
Expand All @@ -43,5 +43,4 @@ public RendererException(Throwable cause) {
public RendererException(String message, Throwable cause) {
super(message, cause);
}

}
Expand Up @@ -23,28 +23,26 @@
import java.util.List;

/**
*
*
* Manages the collection of {@linkplain Renderer renderers}.
*
* @author David Withers
*/
public interface RendererRegistry {

/**
* Get all of the available renderers for a specific MIME type. If there is
* a problem with one then catch the exception and log the problem but carry
* on since there is probably more than one way to render the data
*
*
* @param path
* @param mimeType
* @return
*/
public List<Renderer> getRenderersForMimeType(String mimeType);
List<Renderer> getRenderersForMimeType(String mimeType);

/**
* Return all the renderers.
*
*
* @return all the renderers
*/
public List<Renderer> getRenderers();

}
List<Renderer> getRenderers();
}
Expand Up @@ -20,65 +20,70 @@
******************************************************************************/
package net.sf.taverna.t2.renderers;

import java.io.File;
import static org.purl.wf4ever.robundle.Bundles.getReference;
import static org.purl.wf4ever.robundle.Bundles.getStringValue;
import static org.purl.wf4ever.robundle.Bundles.isReference;
import static uk.org.taverna.databundle.DataBundles.isValue;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;

import uk.org.taverna.databundle.DataBundles;

/**
*
*
* @author David Withers
*/
public class RendererUtils {

public static long getSizeInBytes(Path path) throws IOException {
if (DataBundles.isValue(path)) {
if (isValue(path))
return Files.size(path);
} else if (DataBundles.isReference(path)) {
URL url = DataBundles.getReference(path).toURL();
String protocol = url.getProtocol();
if ("http".equalsIgnoreCase(protocol) || "https".equalsIgnoreCase(protocol)) {
String contentLength = url.openConnection().getHeaderField("Content-Length");
if (contentLength != null && !contentLength.isEmpty()){
return new Long(contentLength);
}
} else if ("file".equalsIgnoreCase(protocol)) {
return FileUtils.toFile(url).length();
}
if (!isReference(path))
throw new IllegalArgumentException(
"Path is not a value or reference");

URL url = getReference(path).toURL();
switch (url.getProtocol().toLowerCase()) {
case "http":
case "https":
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("HEAD");
conn.connect();
String contentLength = conn.getHeaderField("Content-Length");
conn.disconnect();
if (contentLength != null && !contentLength.isEmpty())
return Long.parseLong(contentLength);
return -1;
case "file":
return FileUtils.toFile(url).length();
default:
return -1;
} else {
throw new IllegalArgumentException("Path is not a value or reference");
}
}

public static String getString(Path path) throws IOException {
if (DataBundles.isValue(path)) {
return DataBundles.getStringValue(path);
} else if (DataBundles.isReference(path)) {
return IOUtils.toString(DataBundles.getReference(path));
} else {
throw new IllegalArgumentException("Path is not a value or reference");
}
if (isValue(path))
return getStringValue(path);
else if (isReference(path))
return IOUtils.toString(getReference(path));
else
throw new IllegalArgumentException(
"Path is not a value or reference");
}

public static InputStream getInputStream(Path path) throws MalformedURLException, IOException {
if (DataBundles.isValue(path)) {
public static InputStream getInputStream(Path path)
throws MalformedURLException, IOException {
if (isValue(path))
return Files.newInputStream(path);
} else if (DataBundles.isReference(path)) {
return DataBundles.getReference(path).toURL().openStream();
} else {
throw new IllegalArgumentException("Path is not a value or reference");
}
else if (isReference(path))
return getReference(path).toURL().openStream();
else
throw new IllegalArgumentException(
"Path is not a value or reference");
}

}

0 comments on commit 411dda4

Please sign in to comment.