Skip to content

Commit 0b470e5

Browse files
committed
Adds the ability to escape the @-character in Metamorph names
Metamorph uses the @-character to mark data that should be fed back into Metamorph for a second round of processing. However, sometimes one wants to uses an @ in a key name without triggering the feedback loop. (cherry picked from commit 0b882fe) (cherry picked from commit 7e8189b)
1 parent 8cb7a08 commit 0b470e5

File tree

1 file changed

+20
-14
lines changed

1 file changed

+20
-14
lines changed

src/main/java/org/culturegraph/mf/morph/Metamorph.java

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
/**
4040
* Transforms a data stream send via the {@link StreamReceiver} interface. Use
4141
* {@link MorphBuilder} to create an instance based on an xml description
42-
*
42+
*
4343
* @author Markus Michael Geipel
4444
*/
4545
@Description("applies a metamorph transformation to the event stream. Metamorph definition is given in brackets.")
@@ -49,6 +49,7 @@ public final class Metamorph implements StreamPipe<StreamReceiver>, NamedValueRe
4949

5050
public static final String ELSE_KEYWORD = "_else";
5151
public static final char FEEDBACK_CHAR = '@';
52+
public static final char ESCAPE_CHAR = '\\';
5253
public static final String METADATA = "__meta";
5354
public static final String VAR_START = "$[";
5455
public static final String VAR_END = "]";
@@ -82,19 +83,19 @@ public Metamorph(final Reader morphDefReader) {
8283
builder.walk(morphDefReader);
8384
init();
8485
}
85-
86+
8687
public Metamorph(final Reader morphDefReader, final Map<String, String> vars) {
8788
final MorphBuilder builder = new MorphBuilder(this);
8889
builder.walk(morphDefReader, vars);
8990
init();
9091
}
91-
92+
9293
public Metamorph(final InputStream inputStream, final Map<String, String> vars) {
9394
final MorphBuilder builder = new MorphBuilder(this);
9495
builder.walk(inputStream, vars);
9596
init();
9697
}
97-
98+
9899
public Metamorph(final InputStream inputStream) {
99100
final MorphBuilder builder = new MorphBuilder(this);
100101
builder.walk(inputStream);
@@ -106,7 +107,7 @@ public Metamorph(final String morphDef) {
106107
builder.walk(morphDef);
107108
init();
108109
}
109-
110+
110111
public Metamorph(final String morphDef, final Map<String, String> vars) {
111112
final MorphBuilder builder = new MorphBuilder(this);
112113
builder.walk(morphDef, vars);
@@ -164,10 +165,10 @@ public void startRecord(final String identifier) {
164165
@Override
165166
public void endRecord() {
166167

167-
for(FlushListener listener: recordEndListener){
168+
for(final FlushListener listener: recordEndListener){
168169
listener.flush(recordCount, currentEntityCount);
169170
}
170-
171+
171172
outputStreamReceiver.endRecord();
172173
entityCountStack.removeLast();
173174
if (!entityCountStack.isEmpty()) {
@@ -189,7 +190,7 @@ public void startEntity(final String name) {
189190

190191
flattener.startEntity(name);
191192

192-
193+
193194

194195
}
195196

@@ -216,10 +217,10 @@ public void resetStream() {
216217

217218
@Override
218219
public void closeStream() {
219-
for (Closeable closeable : resources) {
220+
for (final Closeable closeable : resources) {
220221
try {
221222
closeable.close();
222-
} catch (IOException e) {
223+
} catch (final IOException e) {
223224
errorHandler.error(e);
224225
}
225226
}
@@ -248,10 +249,10 @@ private List<NamedValueReceiver> findMatchingData(final String path, final List<
248249
* destination
249250
*/
250251
private void send(final String key, final String value, final List<NamedValueReceiver> dataList) {
251-
for (NamedValueReceiver data : dataList) {
252+
for (final NamedValueReceiver data : dataList) {
252253
try {
253254
data.receive(key, value, null, recordCount, currentEntityCount);
254-
} catch (RuntimeException e) {
255+
} catch (final RuntimeException e) {
255256
errorHandler.error(e);
256257
}
257258
}
@@ -287,10 +288,15 @@ public void receive(final String name, final String value, final NamedValueSourc
287288

288289
if (name.length() != 0 && name.charAt(0) == FEEDBACK_CHAR) {
289290
dispatch(name, value, null);
290-
} else {
291-
outputStreamReceiver.literal(name, value);
291+
return;
292292
}
293293

294+
String unescapedName = name;
295+
if(name.length() > 1 && name.charAt(0) == ESCAPE_CHAR
296+
&& (name.charAt(1) == FEEDBACK_CHAR || name.charAt(1) == ESCAPE_CHAR)) {
297+
unescapedName = name.substring(1);
298+
}
299+
outputStreamReceiver.literal(unescapedName, value);
294300
}
295301

296302
@Override

0 commit comments

Comments
 (0)