Skip to content

Commit b00d3ba

Browse files
committed
Create ScriptHeaderService
Create a new ScriptHeaderService, a handler of a new ScriptHeader plugin type. The purpose of this new framework is to allow flexible addition of header text to scripts, specified by language.
1 parent e532048 commit b00d3ba

File tree

5 files changed

+249
-0
lines changed

5 files changed

+249
-0
lines changed
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/*
2+
* #%L
3+
* SciJava Common shared library for SciJava software.
4+
* %%
5+
* Copyright (C) 2009 - 2014 Board of Regents of the University of
6+
* Wisconsin-Madison, Broad Institute of MIT and Harvard, and Max Planck
7+
* Institute of Molecular Cell Biology and Genetics.
8+
* %%
9+
* Redistribution and use in source and binary forms, with or without
10+
* modification, are permitted provided that the following conditions are met:
11+
*
12+
* 1. Redistributions of source code must retain the above copyright notice,
13+
* this list of conditions and the following disclaimer.
14+
* 2. Redistributions in binary form must reproduce the above copyright notice,
15+
* this list of conditions and the following disclaimer in the documentation
16+
* and/or other materials provided with the distribution.
17+
*
18+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19+
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21+
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
22+
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23+
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24+
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25+
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26+
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27+
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28+
* POSSIBILITY OF SUCH DAMAGE.
29+
* #L%
30+
*/
31+
32+
package org.scijava.script;
33+
34+
import org.scijava.plugin.AbstractHandlerPlugin;
35+
36+
/**
37+
* Abstract superclass for {@link ScriptHeader} implementations.
38+
*
39+
* @author Mark Hiner
40+
*/
41+
public abstract class AbstractScriptHeader extends
42+
AbstractHandlerPlugin<ScriptLanguage> implements ScriptHeader
43+
{
44+
45+
// -- Typed methods --
46+
47+
@Override
48+
public boolean supports(final ScriptLanguage language) {
49+
return language != null && language.getClass() == handledType();
50+
}
51+
52+
@Override
53+
public Class<ScriptLanguage> getType() {
54+
return ScriptLanguage.class;
55+
}
56+
57+
// -- Internal methods --
58+
59+
protected abstract Class<? extends ScriptLanguage> handledType();
60+
}
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/*
2+
* #%L
3+
* SciJava Common shared library for SciJava software.
4+
* %%
5+
* Copyright (C) 2009 - 2014 Board of Regents of the University of
6+
* Wisconsin-Madison, Broad Institute of MIT and Harvard, and Max Planck
7+
* Institute of Molecular Cell Biology and Genetics.
8+
* %%
9+
* Redistribution and use in source and binary forms, with or without
10+
* modification, are permitted provided that the following conditions are met:
11+
*
12+
* 1. Redistributions of source code must retain the above copyright notice,
13+
* this list of conditions and the following disclaimer.
14+
* 2. Redistributions in binary form must reproduce the above copyright notice,
15+
* this list of conditions and the following disclaimer in the documentation
16+
* and/or other materials provided with the distribution.
17+
*
18+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19+
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21+
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
22+
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23+
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24+
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25+
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26+
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27+
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28+
* POSSIBILITY OF SUCH DAMAGE.
29+
* #L%
30+
*/
31+
32+
package org.scijava.script;
33+
34+
import org.scijava.plugin.AbstractHandlerService;
35+
import org.scijava.plugin.Plugin;
36+
import org.scijava.service.Service;
37+
38+
/**
39+
* Default {@link ScriptHeaderService} implementation.
40+
*
41+
* @author Mark Hiner
42+
*/
43+
@Plugin(type = Service.class)
44+
public class DefaultScriptHeaderService extends
45+
AbstractHandlerService<ScriptLanguage, ScriptHeader> implements
46+
ScriptHeaderService
47+
{
48+
49+
// -- ScriptHeaderService methods --
50+
51+
@Override
52+
public String getHeader(final ScriptLanguage language) {
53+
String header = null;
54+
for (final ScriptHeader scriptHeader : getInstances()) {
55+
if (scriptHeader.supports(language)) {
56+
if (header == null) {
57+
header = "";
58+
}
59+
header += scriptHeader.getHeader() + "\n";
60+
}
61+
}
62+
63+
return header;
64+
}
65+
66+
// -- HandlerService methods --
67+
68+
@Override
69+
public Class<ScriptHeader> getPluginType() {
70+
return ScriptHeader.class;
71+
}
72+
73+
@Override
74+
public Class<ScriptLanguage> getType() {
75+
return ScriptLanguage.class;
76+
}
77+
78+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
* #%L
3+
* SciJava Common shared library for SciJava software.
4+
* %%
5+
* Copyright (C) 2009 - 2014 Board of Regents of the University of
6+
* Wisconsin-Madison, Broad Institute of MIT and Harvard, and Max Planck
7+
* Institute of Molecular Cell Biology and Genetics.
8+
* %%
9+
* Redistribution and use in source and binary forms, with or without
10+
* modification, are permitted provided that the following conditions are met:
11+
*
12+
* 1. Redistributions of source code must retain the above copyright notice,
13+
* this list of conditions and the following disclaimer.
14+
* 2. Redistributions in binary form must reproduce the above copyright notice,
15+
* this list of conditions and the following disclaimer in the documentation
16+
* and/or other materials provided with the distribution.
17+
*
18+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19+
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21+
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
22+
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23+
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24+
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25+
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26+
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27+
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28+
* POSSIBILITY OF SUCH DAMAGE.
29+
* #L%
30+
*/
31+
32+
package org.scijava.script;
33+
34+
import org.scijava.plugin.HandlerPlugin;
35+
36+
/**
37+
* Represents one or more canned lines intended to go at the top of scripts of a
38+
* given language. Typically this includes useful import statements.
39+
*
40+
* @author Mark Hiner
41+
*/
42+
public interface ScriptHeader extends HandlerPlugin<ScriptLanguage> {
43+
44+
/**
45+
* @return The header text for scripts of the supported language.
46+
*/
47+
String getHeader();
48+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
* #%L
3+
* SciJava Common shared library for SciJava software.
4+
* %%
5+
* Copyright (C) 2009 - 2014 Board of Regents of the University of
6+
* Wisconsin-Madison, Broad Institute of MIT and Harvard, and Max Planck
7+
* Institute of Molecular Cell Biology and Genetics.
8+
* %%
9+
* Redistribution and use in source and binary forms, with or without
10+
* modification, are permitted provided that the following conditions are met:
11+
*
12+
* 1. Redistributions of source code must retain the above copyright notice,
13+
* this list of conditions and the following disclaimer.
14+
* 2. Redistributions in binary form must reproduce the above copyright notice,
15+
* this list of conditions and the following disclaimer in the documentation
16+
* and/or other materials provided with the distribution.
17+
*
18+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19+
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21+
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
22+
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23+
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24+
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25+
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26+
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27+
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28+
* POSSIBILITY OF SUCH DAMAGE.
29+
* #L%
30+
*/
31+
32+
package org.scijava.script;
33+
34+
import org.scijava.plugin.HandlerService;
35+
36+
/**
37+
* Interface for a {@link HandlerService} capable of mapping
38+
* {@link ScriptLanguage}s to {@link ScriptHeader}s.
39+
* <p>
40+
* NB: although individual/highest priority handlers can be queried as normal
41+
* via a {@code HandlerService}, the
42+
* {@link #getHeader(ScriptLanguage)} method will combine the headers
43+
* for all available {@code ScriptHeader}s for a given {@code ScriptLanguage}.
44+
* </p>
45+
*
46+
* @author Mark Hiner
47+
*/
48+
public interface ScriptHeaderService extends
49+
HandlerService<ScriptLanguage, ScriptHeader>
50+
{
51+
52+
/**
53+
* Searches for all {@link ScriptHeader}s capable of handling the given
54+
* {@link ScriptLanguage} and combines the result of their
55+
* {@link ScriptHeader#getHeader()} output to a single string.
56+
*
57+
* @param language - Language to look up
58+
* @return The combined header text to insert at the top of a script, or null
59+
* if no {@link ScriptHeader}s were found.
60+
*/
61+
String getHeader(final ScriptLanguage language);
62+
}

src/test/java/org/scijava/ContextCreationTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ public void testFull() {
9090
org.scijava.platform.DefaultPlatformService.class,
9191
org.scijava.plugin.DefaultPluginService.class,
9292
org.scijava.prefs.DefaultPrefService.class,
93+
org.scijava.script.DefaultScriptHeaderService.class,
9394
org.scijava.text.DefaultTextService.class,
9495
org.scijava.thread.DefaultThreadService.class,
9596
org.scijava.tool.DefaultToolService.class,

0 commit comments

Comments
 (0)