Skip to content

Commit aa7fe2e

Browse files
committed
Fix bug where text formats claim all files
The file extension check was not being invoked, due to multiple inheritance order of precedence.
1 parent 7775856 commit aa7fe2e

File tree

3 files changed

+97
-1
lines changed

3 files changed

+97
-1
lines changed

src/main/java/org/scijava/text/AbstractTextFormat.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,5 +41,14 @@
4141
public abstract class AbstractTextFormat extends AbstractHandlerPlugin<File>
4242
implements TextFormat
4343
{
44-
// NB: No implementation needed.
44+
// -- Typed methods --
45+
46+
@Override
47+
public boolean supports(final File data) {
48+
// NB: This override is necessary, because the default super is
49+
// AbstractHandlerPlugin->AbstractTypedPlugin->TypedPlugin->Typed,
50+
// which fails to invoke the needed TextFormat.super.
51+
// See fiji/fiji#303 and fiji/HDF5_Vibez#18.
52+
return TextFormat.super.supports(data);
53+
}
4554
}

src/main/java/org/scijava/text/TextFormat.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ public interface TextFormat extends HandlerPlugin<File> {
6363

6464
@Override
6565
default boolean supports(final File file) {
66+
if (!HandlerPlugin.super.supports(file)) return false;
6667
for (final String ext : getExtensions()) {
6768
if (FileUtils.getExtension(file).equalsIgnoreCase(ext)) return true;
6869
}
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
/*-
2+
* #%L
3+
* SciJava Common shared library for SciJava software.
4+
* %%
5+
* Copyright (C) 2009 - 2022 SciJava developers.
6+
* %%
7+
* Redistribution and use in source and binary forms, with or without
8+
* modification, are permitted provided that the following conditions are met:
9+
*
10+
* 1. Redistributions of source code must retain the above copyright notice,
11+
* this list of conditions and the following disclaimer.
12+
* 2. Redistributions in binary form must reproduce the above copyright notice,
13+
* this list of conditions and the following disclaimer in the documentation
14+
* and/or other materials provided with the distribution.
15+
*
16+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17+
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19+
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
20+
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21+
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22+
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23+
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24+
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25+
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26+
* POSSIBILITY OF SUCH DAMAGE.
27+
* #L%
28+
*/
29+
package org.scijava.text;
30+
31+
import static org.junit.Assert.assertEquals;
32+
import static org.junit.Assert.assertNotNull;
33+
import static org.junit.Assert.assertNull;
34+
35+
import java.io.File;
36+
import java.util.Arrays;
37+
import java.util.List;
38+
39+
import org.junit.After;
40+
import org.junit.Before;
41+
import org.junit.Test;
42+
import org.scijava.Context;
43+
import org.scijava.plugin.Plugin;
44+
45+
/** Tests {@link TextService}. */
46+
public class TextServiceTest {
47+
48+
private Context ctx;
49+
private TextService textService;
50+
51+
@Before
52+
public void setUp() {
53+
ctx = new Context(TextService.class);
54+
textService = ctx.service(TextService.class);
55+
}
56+
57+
@After
58+
public void tearDown() {
59+
ctx.dispose();
60+
}
61+
62+
@Test
63+
public void testGetHandler() {
64+
final TextFormat fooHandler = textService.getHandler(new File("data.foo"));
65+
assertNotNull(fooHandler);
66+
assertEquals(FooTextFormat.class, fooHandler.getClass());
67+
System.out.println(fooHandler);
68+
69+
final TextFormat barHandler = textService.getHandler(new File("data.bar"));
70+
assertNull(barHandler);
71+
}
72+
73+
@Plugin(type = TextFormat.class)
74+
public static class FooTextFormat extends AbstractTextFormat {
75+
76+
@Override
77+
public List<String> getExtensions() {
78+
return Arrays.asList("foo");
79+
}
80+
81+
@Override
82+
public String asHTML(final String text) {
83+
return "[FOO]" + text + "[/FOO]";
84+
}
85+
}
86+
}

0 commit comments

Comments
 (0)