Skip to content

Commit 5b3af85

Browse files
committed
2 parents ae5f926 + 5f7d518 commit 5b3af85

File tree

4 files changed

+79
-7
lines changed

4 files changed

+79
-7
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
</parent>
1111

1212
<artifactId>scijava-common</artifactId>
13-
<version>2.60.3-SNAPSHOT</version>
13+
<version>2.61.0-SNAPSHOT</version>
1414

1515
<name>SciJava Common</name>
1616
<description>SciJava Common is a shared library for SciJava software. It provides a plugin framework, with an extensible mechanism for service discovery, backed by its own annotation processor, so that plugins can be loaded dynamically. It is used by downstream projects in the SciJava ecosystem, such as ImageJ and SCIFIO.</description>

src/main/java/org/scijava/script/ScriptFinder.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@
3333

3434
import java.io.File;
3535
import java.io.IOException;
36-
import java.io.InputStreamReader;
3736
import java.net.MalformedURLException;
3837
import java.net.URL;
3938
import java.util.Collections;
@@ -191,8 +190,7 @@ private int createInfos(final List<ScriptInfo> scripts, final Set<URL> urls,
191190
urls.add(url);
192191

193192
try {
194-
final ScriptInfo info = new ScriptInfo(getContext(), //
195-
path, new InputStreamReader(url.openStream()));
193+
final ScriptInfo info = new ScriptInfo(getContext(), url, path);
196194

197195
info.setMenuPath(menuPath);
198196

src/main/java/org/scijava/script/ScriptInfo.java

Lines changed: 65 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,11 @@
3535
import java.io.File;
3636
import java.io.FileReader;
3737
import java.io.IOException;
38+
import java.io.InputStreamReader;
3839
import java.io.Reader;
3940
import java.io.StringReader;
41+
import java.net.MalformedURLException;
42+
import java.net.URL;
4043
import java.text.SimpleDateFormat;
4144
import java.util.ArrayList;
4245
import java.util.Date;
@@ -76,6 +79,7 @@ public class ScriptInfo extends AbstractModuleInfo implements Contextual {
7679

7780
private static final int PARAM_CHAR_MAX = 640 * 1024; // should be enough ;-)
7881

82+
private final URL url;
7983
private final String path;
8084
private final String script;
8185

@@ -105,7 +109,7 @@ public class ScriptInfo extends AbstractModuleInfo implements Contextual {
105109
* @param file The script file.
106110
*/
107111
public ScriptInfo(final Context context, final File file) {
108-
this(context, file.getPath());
112+
this(context, null, file.getPath(), null);
109113
}
110114

111115
/**
@@ -116,7 +120,23 @@ public ScriptInfo(final Context context, final File file) {
116120
* @param path Path to the script file.
117121
*/
118122
public ScriptInfo(final Context context, final String path) {
119-
this(context, path, null);
123+
this(context, null, path, null);
124+
}
125+
126+
/**
127+
* Creates a script metadata object which describes a script at the given URL.
128+
*
129+
* @param context The SciJava application context to use when populating
130+
* service inputs.
131+
* @param url URL which references the script.
132+
* @param path Pseudo-path to the script file. This file does not actually
133+
* need to exist, but rather provides a name for the script with file
134+
* extension.
135+
*/
136+
public ScriptInfo(final Context context, final URL url, final String path)
137+
throws IOException
138+
{
139+
this(context, url, path, new InputStreamReader(url.openStream()));
120140
}
121141

122142
/**
@@ -132,9 +152,16 @@ public ScriptInfo(final Context context, final String path) {
132152
*/
133153
public ScriptInfo(final Context context, final String path,
134154
final Reader reader)
155+
{
156+
this(context, null, path, reader);
157+
}
158+
159+
private ScriptInfo(final Context context, final URL url, final String path,
160+
final Reader reader)
135161
{
136162
setContext(context);
137-
this.path = path;
163+
this.url = url(url, path);
164+
this.path = path(url, path);
138165

139166
String contents = null;
140167
if (reader != null) {
@@ -150,6 +177,26 @@ public ScriptInfo(final Context context, final String path,
150177

151178
// -- ScriptInfo methods --
152179

180+
/**
181+
* Gets the URL of the script.
182+
* <p>
183+
* If the actual source of the script is a URL (provided via
184+
* {@link #ScriptInfo(Context, URL, String)}), then this will return it.
185+
* </p>
186+
* <p>
187+
* Alternately, if the path (from {@link #getPath()}) is a real file on disk
188+
* (provided via {@link #ScriptInfo(Context, File)} or
189+
* {@link #ScriptInfo(Context, String)}), then the URL returned here will be a
190+
* {@code file://} one reference to it.
191+
* </p>
192+
* <p>
193+
* Otherwise, this method will return null.
194+
* </p>
195+
*/
196+
public URL getURL() {
197+
return url;
198+
}
199+
153200
/**
154201
* Gets the path to the script on disk.
155202
* <p>
@@ -332,6 +379,21 @@ public String getVersion() {
332379

333380
// -- Helper methods --
334381

382+
private URL url(final URL u, final String p) {
383+
if (u != null) return u;
384+
try {
385+
return new File(p).toURI().toURL();
386+
}
387+
catch (final MalformedURLException exc) {
388+
log.debug("Cannot glean URL from path: " + p, exc);
389+
return null;
390+
}
391+
}
392+
393+
private String path(final URL u, final String p) {
394+
return p == null ? u.getPath() : p;
395+
}
396+
335397
private void parseParam(final String param) throws ScriptException {
336398
final int lParen = param.indexOf("(");
337399
final int rParen = param.lastIndexOf(")");

src/test/java/org/scijava/script/ScriptFinderTest.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
package org.scijava.script;
3333

3434
import static org.junit.Assert.assertEquals;
35+
import static org.junit.Assert.assertTrue;
3536

3637
import java.io.File;
3738
import java.io.IOException;
@@ -114,6 +115,7 @@ public void testFindScripts() {
114115
"Math > Trig > tan", //
115116
};
116117
assertMenuPaths(expected, scripts);
118+
assertURLsMatch(scripts);
117119
}
118120

119121
/**
@@ -148,6 +150,7 @@ public void testMenuPrefixes() {
148150
"Foo > Bar > Math > Trig > tan", //
149151
};
150152
assertMenuPaths(expected, scripts);
153+
assertURLsMatch(scripts);
151154
}
152155

153156
/**
@@ -181,6 +184,7 @@ public void testOverlappingDirectories() {
181184
"Math > Trig > tan", //
182185
};
183186
assertMenuPaths(expected, scripts);
187+
assertURLsMatch(scripts);
184188
}
185189

186190
// -- Helper methods --
@@ -212,6 +216,14 @@ private void assertMenuPaths(final String[] expected,
212216
}
213217
}
214218

219+
private void assertURLsMatch(final ArrayList<ScriptInfo> scripts) {
220+
for (final ScriptInfo info : scripts) {
221+
final String urlPath = info.getURL().getPath();
222+
final String path = info.getPath();
223+
assertTrue(urlPath + " <> " + path, urlPath.endsWith("/" + path));
224+
}
225+
}
226+
215227
// -- Helper classes --
216228

217229
/** "Handles" scripts with .foo extension. */

0 commit comments

Comments
 (0)