Skip to content

Commit 95448a9

Browse files
committed
Expose InlineMorph string representation.
E.g., for debugging purposes.
1 parent 2ee0ca4 commit 95448a9

File tree

2 files changed

+74
-4
lines changed

2 files changed

+74
-4
lines changed

metamorph/src/main/java/org/metafacture/metamorph/InlineMorph.java

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,10 @@
5555
*/
5656
public final class InlineMorph {
5757

58+
private static final String XML_BOILERPLATE = "<?xml version='1.1' encoding='UTF-8'?>\n";
59+
private static final String METAMORPH_BOILERPLATE = "<metamorph version='1'\n xmlns='http://www.culturegraph.org/metamorph'>";
60+
private static final String FINISH_METAMORPH_BOILERPLATE = "</metamorph>\n";
61+
5862
private final StringBuilder scriptBuilder = new StringBuilder();
5963

6064
private String systemId;
@@ -126,11 +130,11 @@ private void appendBoilerplate(final String line) {
126130
}
127131

128132
private void appendXmlBoilerplate() {
129-
scriptBuilder.append("<?xml version='1.1' encoding='UTF-8'?>\n");
133+
scriptBuilder.append(XML_BOILERPLATE);
130134
}
131135

132136
private void appendMetamorphBoilerplate() {
133-
scriptBuilder.append("<metamorph version='1'\n xmlns='http://www.culturegraph.org/metamorph'>");
137+
scriptBuilder.append(METAMORPH_BOILERPLATE);
134138
}
135139

136140
/**
@@ -145,18 +149,28 @@ public Metamorph create() {
145149

146150
private void finishBoilerplate() {
147151
if (needFinishMetamorphBoilerplate) {
148-
scriptBuilder.append("</metamorph>\n");
152+
scriptBuilder.append(FINISH_METAMORPH_BOILERPLATE);
149153
needFinishMetamorphBoilerplate = false;
150154
}
151155
}
152156

153157
private InputSource createScriptSource() {
154158
final InputSource scriptSource = new InputSource();
155159
scriptSource.setSystemId(systemId);
156-
scriptSource.setCharacterStream(new StringReader(scriptBuilder.toString()));
160+
scriptSource.setCharacterStream(new StringReader(toString()));
157161
return scriptSource;
158162
}
159163

164+
/**
165+
* Returns the string representation of this Morph script.
166+
*
167+
* @return the Morph script
168+
*/
169+
@Override
170+
public String toString() {
171+
return scriptBuilder.toString() + (needFinishMetamorphBoilerplate ? FINISH_METAMORPH_BOILERPLATE : "");
172+
}
173+
160174
/**
161175
* Creates a {@link Metamorph} instance. The {@code Metamorph} instance will
162176
* be connected to the receiver passed as argument.
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
* Copyright 2021 hbz NRW
3+
*
4+
* Licensed under the Apache License, Version 2.0 the "License";
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.metafacture.metamorph;
18+
19+
import org.junit.Assert;
20+
import org.junit.Test;
21+
22+
import java.util.Arrays;
23+
24+
public final class InlineMorphTest {
25+
26+
private static final String PREFIX = "<?xml version='1.1' encoding='UTF-8'?>\n" +
27+
"<metamorph version='1'\n xmlns='http://www.culturegraph.org/metamorph'>";
28+
29+
private static final String SUFFIX = "\n</metamorph>\n";
30+
31+
@Test
32+
public void shouldProvideEmptyStringRepresentation() {
33+
Assert.assertEquals("", InlineMorph.in(this).toString());
34+
}
35+
36+
@Test
37+
public void shouldProvideScriptStringRepresentation() {
38+
final InlineMorph morph = InlineMorph.in(this);
39+
40+
final String[] script = new String[]{
41+
"<rules>",
42+
" <data source='litA'>",
43+
" <lookup>",
44+
" <entry name='cat' value='mammal' />",
45+
" <entry name='dog' value='mammal' />",
46+
" </lookup>",
47+
" </data>",
48+
"</rules>"
49+
};
50+
51+
Arrays.stream(script).forEach(morph::with);
52+
53+
Assert.assertEquals(PREFIX + String.join("\n", script) + SUFFIX, morph.toString());
54+
}
55+
56+
}

0 commit comments

Comments
 (0)