Skip to content

Commit 294bc96

Browse files
committed
Add text service
This general-purpose, non-image-specific service migrated from ImageJ: https://github.com/imagej/imagej/tree/imagej-2.0.0-beta-7.8/core/core/src/main/java/imagej/text
1 parent 87b9216 commit 294bc96

File tree

5 files changed

+375
-0
lines changed

5 files changed

+375
-0
lines changed
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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.text;
33+
34+
import java.io.File;
35+
36+
import org.scijava.plugin.AbstractHandlerPlugin;
37+
import org.scijava.util.FileUtils;
38+
39+
/**
40+
* Abstract superclass of {@link TextFormat} implementations.
41+
*
42+
* @author Curtis Rueden
43+
*/
44+
public abstract class AbstractTextFormat extends AbstractHandlerPlugin<File>
45+
implements TextFormat
46+
{
47+
48+
// -- Typed methods --
49+
50+
@Override
51+
public boolean supports(final File file) {
52+
for (final String ext : getExtensions()) {
53+
if (FileUtils.getExtension(file).equalsIgnoreCase(ext)) return true;
54+
}
55+
return false;
56+
}
57+
58+
@Override
59+
public Class<File> getType() {
60+
return File.class;
61+
}
62+
63+
}
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
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.text;
33+
34+
import java.io.File;
35+
import java.io.FileInputStream;
36+
import java.io.IOException;
37+
import java.nio.MappedByteBuffer;
38+
import java.nio.channels.FileChannel;
39+
import java.nio.charset.Charset;
40+
41+
import org.scijava.plugin.AbstractHandlerService;
42+
import org.scijava.plugin.Parameter;
43+
import org.scijava.plugin.Plugin;
44+
import org.scijava.plugin.PluginService;
45+
import org.scijava.service.Service;
46+
47+
/**
48+
* Default service for working with text formats.
49+
*
50+
* @author Curtis Rueden
51+
*/
52+
@Plugin(type = Service.class)
53+
public final class DefaultTextService extends
54+
AbstractHandlerService<File, TextFormat> implements TextService
55+
{
56+
57+
@Parameter
58+
private PluginService pluginService;
59+
60+
// -- TextService methods --
61+
62+
@Override
63+
public String open(final File file) throws IOException {
64+
// This routine is from: http://stackoverflow.com/a/326440
65+
final FileInputStream stream = new FileInputStream(file);
66+
try {
67+
final FileChannel fc = stream.getChannel();
68+
final MappedByteBuffer bb =
69+
fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size());
70+
return Charset.defaultCharset().decode(bb).toString();
71+
}
72+
finally {
73+
stream.close();
74+
}
75+
}
76+
77+
@Override
78+
public String asHTML(final File file) throws IOException {
79+
final TextFormat format = getHandler(file);
80+
if (format == null) return null;
81+
return "<html><body>" + format.asHTML(open(file)) + "</body></html>";
82+
}
83+
84+
// -- PTService methods --
85+
86+
@Override
87+
public Class<TextFormat> getPluginType() {
88+
return TextFormat.class;
89+
}
90+
91+
// -- Typed methods --
92+
93+
@Override
94+
public Class<File> getType() {
95+
return File.class;
96+
}
97+
98+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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.text;
33+
34+
import java.io.File;
35+
import java.util.List;
36+
37+
import org.scijava.plugin.HandlerPlugin;
38+
import org.scijava.plugin.Plugin;
39+
40+
/**
41+
* {@code TextFormat} is a plugin that provides handling for a text markup
42+
* language.
43+
* <p>
44+
* Text formats discoverable at runtime must implement this interface and be
45+
* annotated with @{@link Plugin} with attribute {@link Plugin#type()} =
46+
* {@link TextFormat}.class. While it possible to create a text format merely by
47+
* implementing this interface, it is encouraged to instead extend
48+
* {@link AbstractTextFormat}, for convenience.
49+
* </p>
50+
*
51+
* @author Curtis Rueden
52+
* @see Plugin
53+
* @see TextService
54+
*/
55+
public interface TextFormat extends HandlerPlugin<File> {
56+
57+
/** Gets the list of filename extensions for text in this format. */
58+
List<String> getExtensions();
59+
60+
/** Expresses the given text string in HTML format. */
61+
String asHTML(String text);
62+
63+
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
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.text;
33+
34+
import java.io.File;
35+
import java.io.IOException;
36+
import java.util.List;
37+
38+
import org.scijava.plugin.HandlerService;
39+
import org.scijava.service.SciJavaService;
40+
41+
/**
42+
* Interface for service that works with text formats.
43+
*
44+
* @author Curtis Rueden
45+
*/
46+
public interface TextService extends HandlerService<File, TextFormat>,
47+
SciJavaService
48+
{
49+
50+
/** Reads the data from the given file into a string. */
51+
String open(File file) throws IOException;
52+
53+
/** Expresses the given text string as HTML. */
54+
String asHTML(File file) throws IOException;
55+
56+
// NB: Javadoc overrides.
57+
58+
// -- HandlerService methods --
59+
60+
/** Gets the text format which best handles the given file. */
61+
@Override
62+
TextFormat getHandler(File file);
63+
64+
// -- SingletonService methods --
65+
66+
/** Gets the list of available text formats. */
67+
@Override
68+
List<TextFormat> getInstances();
69+
70+
// -- Typed methods --
71+
72+
/** Gets whether the given file contains text data in a supported format. */
73+
@Override
74+
boolean supports(File file);
75+
76+
}
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
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.text.io;
33+
34+
import java.io.File;
35+
import java.io.IOException;
36+
37+
import org.scijava.Priority;
38+
import org.scijava.io.AbstractIOPlugin;
39+
import org.scijava.io.IOPlugin;
40+
import org.scijava.plugin.Parameter;
41+
import org.scijava.plugin.Plugin;
42+
import org.scijava.text.TextService;
43+
44+
/**
45+
* {@link IOPlugin} for text conversion to HTML.
46+
*
47+
* @author Curtis Rueden
48+
* @see TextService
49+
*/
50+
@Plugin(type = IOPlugin.class, priority = Priority.LOW_PRIORITY - 1)
51+
public class TextIOPlugin extends AbstractIOPlugin<String> {
52+
53+
@Parameter(required = false)
54+
private TextService textService;
55+
56+
// -- IOPlugin methods --
57+
58+
@Override
59+
public Class<String> getDataType() {
60+
return String.class;
61+
}
62+
63+
@Override
64+
public boolean supportsOpen(final String source) {
65+
if (textService == null) return false; // no service for opening text files
66+
return textService.supports(new File(source));
67+
}
68+
69+
@Override
70+
public String open(final String source) throws IOException {
71+
if (textService == null) return null; // no service for opening text files
72+
return textService.asHTML(new File(source));
73+
}
74+
75+
}

0 commit comments

Comments
 (0)