|
13 | 13 | * See the License for the specific language governing permissions and
|
14 | 14 | * limitations under the License.
|
15 | 15 | */
|
16 |
| -package org.culturegraph.mf.stream.converter; |
17 |
| - |
18 |
| -import java.util.HashMap; |
19 |
| -import java.util.Map; |
20 |
| -import java.util.regex.Matcher; |
21 |
| -import java.util.regex.Pattern; |
22 |
| - |
23 |
| -import org.culturegraph.mf.framework.DefaultObjectPipe; |
24 |
| -import org.culturegraph.mf.framework.ObjectReceiver; |
25 |
| -import org.culturegraph.mf.framework.annotations.Description; |
26 |
| -import org.culturegraph.mf.framework.annotations.In; |
27 |
| -import org.culturegraph.mf.framework.annotations.Out; |
28 |
| -import org.culturegraph.mf.types.Triple; |
29 |
| -import org.culturegraph.mf.util.StringUtil; |
30 |
| - |
31 |
| - |
32 |
| -/** |
33 |
| - * Builds a {@link String} from a template and an {@link Object}. ${obj} marks |
34 |
| - * the place where the object is to be inserted. |
35 |
| - * |
36 |
| - * @param <T> |
37 |
| - * object type |
38 |
| - * |
39 |
| - * @author Markus Geipel |
40 |
| - * |
41 |
| - */ |
42 |
| -@Description("Builds a String from a template and an Object. Provide template in brackets. ${o} marks the place where the object is to be inserted. " + |
43 |
| - "If the object in an instance of Triple ${s}, ${p} and ${o} are used instead") |
44 |
| -@In(Object.class) |
45 |
| -@Out(String.class) |
46 |
| -public final class ObjectTemplate<T> extends DefaultObjectPipe<T, ObjectReceiver<String>> { |
47 |
| - |
48 |
| - //TODO: make replace more efficient |
49 |
| - private static final Pattern OBJ_PATTERN = Pattern.compile("${o}", Pattern.LITERAL); |
50 |
| - private final Map<String, String> vars = new HashMap<String, String>(); |
51 |
| - private final String template; |
52 |
| - |
53 |
| - public ObjectTemplate(final String template) { |
54 |
| - super(); |
55 |
| - this.template = template; |
56 |
| - } |
57 |
| - |
58 |
| - @Override |
59 |
| - public void process(final T obj) { |
60 |
| - if(obj instanceof Triple){ |
61 |
| - final Triple triple = (Triple)obj; |
62 |
| - vars.put("s", triple.getSubject()); |
63 |
| - vars.put("p", triple.getPredicate()); |
64 |
| - vars.put("o", triple.getObject()); |
65 |
| - getReceiver().process(StringUtil.format(template, vars)); |
66 |
| - }else{ |
67 |
| - final Matcher matcher = OBJ_PATTERN.matcher(template); |
68 |
| - getReceiver().process(matcher.replaceAll(obj.toString())); |
69 |
| - } |
70 |
| - } |
71 |
| -} |
| 16 | +package org.culturegraph.mf.stream.converter; |
| 17 | + |
| 18 | +import java.util.HashMap; |
| 19 | +import java.util.Map; |
| 20 | +import java.util.regex.Matcher; |
| 21 | +import java.util.regex.Pattern; |
| 22 | + |
| 23 | +import org.culturegraph.mf.framework.DefaultObjectPipe; |
| 24 | +import org.culturegraph.mf.framework.ObjectReceiver; |
| 25 | +import org.culturegraph.mf.framework.annotations.Description; |
| 26 | +import org.culturegraph.mf.framework.annotations.In; |
| 27 | +import org.culturegraph.mf.framework.annotations.Out; |
| 28 | +import org.culturegraph.mf.types.Triple; |
| 29 | +import org.culturegraph.mf.util.StringUtil; |
| 30 | + |
| 31 | + |
| 32 | +/** |
| 33 | + * Builds a {@link String} from a template and an {@link Object}. ${o} marks |
| 34 | + * the place where the object is to be inserted. |
| 35 | + * |
| 36 | + * @param <T> |
| 37 | + * object type |
| 38 | + * |
| 39 | + * @author Markus Geipel |
| 40 | + * |
| 41 | + */ |
| 42 | +@Description("Builds a String from a template and an Object. Provide template in brackets. ${o} marks the place where the object is to be inserted. " + |
| 43 | + "If the object in an instance of Triple ${s}, ${p} and ${o} are used instead") |
| 44 | +@In(Object.class) |
| 45 | +@Out(String.class) |
| 46 | +public final class ObjectTemplate<T> extends DefaultObjectPipe<T, ObjectReceiver<String>> { |
| 47 | + |
| 48 | + //TODO: make replace more efficient |
| 49 | + private static final Pattern OBJ_PATTERN = Pattern.compile("${o}", Pattern.LITERAL); |
| 50 | + private final Map<String, String> vars = new HashMap<String, String>(); |
| 51 | + private final String template; |
| 52 | + |
| 53 | + public ObjectTemplate(final String template) { |
| 54 | + super(); |
| 55 | + this.template = template; |
| 56 | + } |
| 57 | + |
| 58 | + @Override |
| 59 | + public void process(final T obj) { |
| 60 | + if(obj instanceof Triple){ |
| 61 | + final Triple triple = (Triple)obj; |
| 62 | + vars.put("s", triple.getSubject()); |
| 63 | + vars.put("p", triple.getPredicate()); |
| 64 | + vars.put("o", triple.getObject()); |
| 65 | + getReceiver().process(StringUtil.format(template, vars)); |
| 66 | + }else{ |
| 67 | + final Matcher matcher = OBJ_PATTERN.matcher(template); |
| 68 | + getReceiver().process(matcher.replaceAll(obj.toString())); |
| 69 | + } |
| 70 | + } |
| 71 | +} |
0 commit comments