Skip to content

Commit 8626a3c

Browse files
committed
Add a ParseService using SJEP
This is a generalization of the code in ScriptInfo#parseAttrs(String).
1 parent cd3a2e4 commit 8626a3c

File tree

5 files changed

+310
-0
lines changed

5 files changed

+310
-0
lines changed
Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
/*
2+
* #%L
3+
* SciJava Common shared library for SciJava software.
4+
* %%
5+
* Copyright (C) 2009 - 2016 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.parse;
33+
34+
import java.util.Collections;
35+
import java.util.LinkedHashMap;
36+
import java.util.List;
37+
import java.util.Map;
38+
39+
import org.scijava.plugin.Plugin;
40+
import org.scijava.service.AbstractService;
41+
import org.scijava.service.Service;
42+
import org.scijava.sjep.Variable;
43+
import org.scijava.sjep.eval.DefaultEvaluator;
44+
import org.scijava.util.ObjectArray;
45+
46+
/**
47+
* Default service for parsing strings.
48+
*
49+
* @author Curtis Rueden
50+
*/
51+
@Plugin(type = Service.class)
52+
public class DefaultParseService extends AbstractService implements
53+
ParseService
54+
{
55+
56+
@Override
57+
public Items parse(final String arg) {
58+
return new ItemsList(arg);
59+
}
60+
61+
// -- Helper classes --
62+
63+
/**
64+
* {@link Items} implementation backed by the
65+
* <a href="https://github.com/scijava/scijava-expression-parser">SciJava
66+
* Expression Parser</a>.
67+
*/
68+
private static class ItemsList extends ObjectArray<Item> implements Items {
69+
70+
public ItemsList(final String arg) {
71+
super(Item.class);
72+
parseItems(arg);
73+
}
74+
75+
@Override
76+
public Map<String, Object> asMap() {
77+
final LinkedHashMap<String, Object> map =
78+
new LinkedHashMap<String, Object>();
79+
for (final Item item : this) {
80+
map.put(item.name(), item.value());
81+
}
82+
return map;
83+
}
84+
85+
@Override
86+
public boolean isMap() {
87+
for (final Item item : this) {
88+
if (item.name() == null) return false;
89+
}
90+
return true;
91+
}
92+
93+
@Override
94+
public boolean isList() {
95+
for (final Item item : this) {
96+
if (item.name() != null) return false;
97+
}
98+
return true;
99+
}
100+
101+
private void parseItems(final String arg) {
102+
final DefaultEvaluator e = new DefaultEvaluator();
103+
final Object result = e.evaluate("(" + arg + ")");
104+
if (result == null) {
105+
throw new IllegalStateException("Error parsing string: '" + arg + "'");
106+
}
107+
final List<?> list;
108+
if (result instanceof List) list = (List<?>) result;
109+
else list = Collections.singletonList(result);
110+
111+
for (final Object o : list) {
112+
final String name;
113+
final Object value;
114+
if (o instanceof Variable) {
115+
final Variable v = (Variable) o;
116+
name = v.getToken();
117+
value = e.value(v);
118+
}
119+
else {
120+
name = null;
121+
value = o;
122+
}
123+
add(new Item() {
124+
125+
@Override
126+
public String name() {
127+
return name;
128+
}
129+
130+
@Override
131+
public Object value() {
132+
return value;
133+
}
134+
135+
});
136+
}
137+
}
138+
139+
}
140+
141+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
* #%L
3+
* SciJava Common shared library for SciJava software.
4+
* %%
5+
* Copyright (C) 2009 - 2016 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.parse;
33+
34+
/**
35+
* An item from the list of parsed items; i.e.: a name/value pair.
36+
* <p>
37+
* NB: It is unfortunate that we cannot use {@code javafx.util.Pair}, but it is:
38+
* A) Java 8; and B) part of JavaFX rather than core Java.
39+
* </p>
40+
*
41+
* @author Curtis Rueden
42+
*/
43+
public interface Item {
44+
45+
/**
46+
* Gets the name of the item, or {@code null} if unnamed (i.e., raw value with
47+
* no equals sign).
48+
*/
49+
String name();
50+
51+
/** Gets the value of the item. */
52+
Object value();
53+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*
2+
* #%L
3+
* SciJava Common shared library for SciJava software.
4+
* %%
5+
* Copyright (C) 2009 - 2016 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.parse;
33+
34+
import java.util.List;
35+
import java.util.Map;
36+
37+
/**
38+
* An ordered list of items, some of which might be key/value pairs, and some of
39+
* which might be raw values.
40+
*
41+
* @author Curtis Rueden
42+
*/
43+
public interface Items extends List<Item> {
44+
45+
/**
46+
* Gets the parsed items as a map. The map will have the same iteration
47+
* order as the original list.
48+
*/
49+
Map<String, Object> asMap();
50+
51+
/** Returns true iff all items are named key/value pairs. */
52+
boolean isMap();
53+
54+
/** Returns true iff there are no named key/value pairs among the items. */
55+
boolean isList();
56+
57+
}
58+
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
* #%L
3+
* SciJava Common shared library for SciJava software.
4+
* %%
5+
* Copyright (C) 2009 - 2016 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.parse;
33+
34+
import org.scijava.service.SciJavaService;
35+
36+
/**
37+
* Interface for service that parses strings.
38+
*
39+
* @author Curtis Rueden
40+
*/
41+
public interface ParseService extends SciJavaService {
42+
43+
/**
44+
* Parses a comma-delimited list of data elements.
45+
* <p>
46+
* Some data elements might be {@code key=value} pairs, while others might be
47+
* raw values (i.e., no equals sign).
48+
* </p>
49+
*
50+
* @param arg The string to parse.
51+
* @return A parsed list of {@link Item}s.
52+
* @throws IllegalArgumentException If the string does not conform to expected
53+
* syntax.
54+
*/
55+
Items parse(String arg);
56+
57+
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ public void testFull() {
102102
org.scijava.module.DefaultModuleService.class,
103103
org.scijava.object.DefaultObjectService.class,
104104
org.scijava.options.DefaultOptionsService.class,
105+
org.scijava.parse.DefaultParseService.class,
105106
org.scijava.platform.DefaultPlatformService.class,
106107
org.scijava.plugin.DefaultPluginService.class,
107108
org.scijava.prefs.DefaultPrefService.class,

0 commit comments

Comments
 (0)