|
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.regex.Matcher; |
19 |
| -import java.util.regex.Pattern; |
20 |
| - |
21 |
| -import org.culturegraph.mf.exceptions.FormatException; |
22 |
| -import org.culturegraph.mf.framework.DefaultObjectPipe; |
23 |
| -import org.culturegraph.mf.framework.StreamReceiver; |
24 |
| -import org.culturegraph.mf.framework.annotations.Description; |
25 |
| -import org.culturegraph.mf.framework.annotations.In; |
26 |
| -import org.culturegraph.mf.framework.annotations.Out; |
27 |
| - |
28 |
| - |
29 |
| -/** |
30 |
| - * Decodes a record stored in CG-Text format. |
31 |
| - * |
32 |
| - * @see CGTextEncoder |
33 |
| - * |
34 |
| - * @author Christoph Böhme |
35 |
| - * |
36 |
| - */ |
37 |
| -@Description("Decodes a record stored in CG-Text format.") |
38 |
| -@In(String.class) |
39 |
| -@Out(StreamReceiver.class) |
40 |
| -public final class CGTextDecoder |
41 |
| - extends DefaultObjectPipe<String, StreamReceiver> { |
42 |
| - |
43 |
| - private static final String UNQUOTED_NAME = "(?:[A-Za-z0-9-_.:]+)"; |
44 |
| - private static final String QUOTED_NAME = "(?:'(?:\\\\'|[^'])*')"; |
45 |
| - private static final String NAME = "(" + UNQUOTED_NAME + "|" + QUOTED_NAME + ")"; |
46 |
| - private static final String GROUP_START = "(?:\\{)"; |
47 |
| - private static final String GROUP_END = "(?:\\})"; |
48 |
| - private static final String CONTENT = "(?:(.*))"; |
49 |
| - private static final String LEADING_WS = "(?:(?:\\A|\\G)\\s*)"; |
50 |
| - private static final String TRAILING_WS = "(?:\\s*$)"; |
51 |
| - private static final String ASSIGNMENT = "(?:\\s*=\\s*)"; |
52 |
| - private static final String LIST_SEP = "(?:(?:\\s*,\\s*)|(?=\\s*\\})|" + TRAILING_WS + ")"; |
53 |
| - |
54 |
| - private static final Pattern RECORD = Pattern.compile( |
55 |
| - LEADING_WS + NAME + ASSIGNMENT + GROUP_START + CONTENT + GROUP_END + TRAILING_WS); |
56 |
| - private static final Pattern ENTITY_START = Pattern.compile( |
57 |
| - LEADING_WS + NAME + ASSIGNMENT + GROUP_START); |
58 |
| - private static final Pattern ENTITY_END = Pattern.compile( |
59 |
| - LEADING_WS + GROUP_END + LIST_SEP); |
60 |
| - private static final Pattern LITERAL = Pattern.compile( |
61 |
| - LEADING_WS + NAME + ASSIGNMENT + NAME + LIST_SEP); |
62 |
| - |
63 |
| - @Override |
64 |
| - public void process(final String str) { |
65 |
| - final Matcher record = RECORD.matcher(str); |
66 |
| - if (!record.matches()) { |
67 |
| - throw new FormatException("expecting only a single record"); |
68 |
| - } |
69 |
| - final String id = unescape(record.group(1)); |
70 |
| - final String contents = record.group(2); |
71 |
| - getReceiver().startRecord(id); |
72 |
| - processList(contents); |
73 |
| - getReceiver().endRecord(); |
74 |
| - } |
75 |
| - |
76 |
| - private void processList(final String str) { |
77 |
| - final Matcher literal = LITERAL.matcher(str); |
78 |
| - final Matcher entityStart = ENTITY_START.matcher(str); |
79 |
| - final Matcher entityEnd = ENTITY_END.matcher(str); |
80 |
| - int pos = 0; |
81 |
| - while (pos < str.length()) { |
82 |
| - if (literal.find(pos)) { |
83 |
| - final String name = unescape(literal.group(1)); |
84 |
| - final String value = unescape(literal.group(2)); |
85 |
| - getReceiver().literal(name, value); |
86 |
| - pos = literal.end(); |
87 |
| - } else if (entityStart.find(pos)) { |
88 |
| - final String name = unescape(entityStart.group(1)); |
89 |
| - getReceiver().startEntity(name); |
90 |
| - pos = entityStart.end(); |
91 |
| - } else if (entityEnd.find(pos)) { |
92 |
| - getReceiver().endEntity(); |
93 |
| - pos = entityEnd.end(); |
94 |
| - } else { |
95 |
| - throw new FormatException("unexpected format at position: " + pos); |
96 |
| - } |
97 |
| - } |
98 |
| - } |
99 |
| - |
100 |
| - private String unescape(final String str) { |
101 |
| - return str.replaceAll("(^')|('$)", "").replace("\\'", "'").replace("\\\\", "\\"); |
102 |
| - } |
103 |
| - |
104 |
| -} |
| 16 | +package org.culturegraph.mf.stream.converter; |
| 17 | + |
| 18 | +import java.util.regex.Matcher; |
| 19 | +import java.util.regex.Pattern; |
| 20 | + |
| 21 | +import org.culturegraph.mf.exceptions.FormatException; |
| 22 | +import org.culturegraph.mf.framework.DefaultObjectPipe; |
| 23 | +import org.culturegraph.mf.framework.StreamReceiver; |
| 24 | +import org.culturegraph.mf.framework.annotations.Description; |
| 25 | +import org.culturegraph.mf.framework.annotations.In; |
| 26 | +import org.culturegraph.mf.framework.annotations.Out; |
| 27 | + |
| 28 | + |
| 29 | +/** |
| 30 | + * Decodes a record stored in CG-Text format. |
| 31 | + * |
| 32 | + * @see CGTextEncoder |
| 33 | + * |
| 34 | + * @author Christoph Böhme |
| 35 | + * |
| 36 | + */ |
| 37 | +@Description("Decodes a record stored in CG-Text format.") |
| 38 | +@In(String.class) |
| 39 | +@Out(StreamReceiver.class) |
| 40 | +public final class CGTextDecoder |
| 41 | + extends DefaultObjectPipe<String, StreamReceiver> { |
| 42 | + |
| 43 | + private static final String UNQUOTED_NAME = "(?:[A-Za-z0-9-_.:]+)"; |
| 44 | + private static final String QUOTED_NAME = "(?:'(?:\\\\'|[^'])*')"; |
| 45 | + private static final String NAME = "(" + UNQUOTED_NAME + "|" + QUOTED_NAME + ")"; |
| 46 | + private static final String GROUP_START = "(?:\\{)"; |
| 47 | + private static final String GROUP_END = "(?:\\})"; |
| 48 | + private static final String CONTENT = "(?:(.*))"; |
| 49 | + private static final String LEADING_WS = "(?:(?:\\A|\\G)\\s*)"; |
| 50 | + private static final String TRAILING_WS = "(?:\\s*$)"; |
| 51 | + private static final String ASSIGNMENT = "(?:\\s*=\\s*)"; |
| 52 | + private static final String LIST_SEP = "(?:(?:\\s*,\\s*)|(?=\\s*\\})|" + TRAILING_WS + ")"; |
| 53 | + |
| 54 | + private static final Pattern RECORD = Pattern.compile( |
| 55 | + LEADING_WS + NAME + ASSIGNMENT + GROUP_START + CONTENT + GROUP_END + TRAILING_WS); |
| 56 | + private static final Pattern ENTITY_START = Pattern.compile( |
| 57 | + LEADING_WS + NAME + ASSIGNMENT + GROUP_START); |
| 58 | + private static final Pattern ENTITY_END = Pattern.compile( |
| 59 | + LEADING_WS + GROUP_END + LIST_SEP); |
| 60 | + private static final Pattern LITERAL = Pattern.compile( |
| 61 | + LEADING_WS + NAME + ASSIGNMENT + NAME + LIST_SEP); |
| 62 | + |
| 63 | + @Override |
| 64 | + public void process(final String str) { |
| 65 | + assert !isClosed(); |
| 66 | + final Matcher record = RECORD.matcher(str); |
| 67 | + if (!record.matches()) { |
| 68 | + throw new FormatException("expecting only a single record"); |
| 69 | + } |
| 70 | + final String id = unescape(record.group(1)); |
| 71 | + final String contents = record.group(2); |
| 72 | + getReceiver().startRecord(id); |
| 73 | + processList(contents); |
| 74 | + getReceiver().endRecord(); |
| 75 | + } |
| 76 | + |
| 77 | + private void processList(final String str) { |
| 78 | + final Matcher literal = LITERAL.matcher(str); |
| 79 | + final Matcher entityStart = ENTITY_START.matcher(str); |
| 80 | + final Matcher entityEnd = ENTITY_END.matcher(str); |
| 81 | + int pos = 0; |
| 82 | + while (pos < str.length()) { |
| 83 | + if (literal.find(pos)) { |
| 84 | + final String name = unescape(literal.group(1)); |
| 85 | + final String value = unescape(literal.group(2)); |
| 86 | + getReceiver().literal(name, value); |
| 87 | + pos = literal.end(); |
| 88 | + } else if (entityStart.find(pos)) { |
| 89 | + final String name = unescape(entityStart.group(1)); |
| 90 | + getReceiver().startEntity(name); |
| 91 | + pos = entityStart.end(); |
| 92 | + } else if (entityEnd.find(pos)) { |
| 93 | + getReceiver().endEntity(); |
| 94 | + pos = entityEnd.end(); |
| 95 | + } else { |
| 96 | + throw new FormatException("unexpected format at position: " + pos); |
| 97 | + } |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + private String unescape(final String str) { |
| 102 | + return str.replaceAll("(^')|('$)", "").replace("\\'", "'").replace("\\\\", "\\"); |
| 103 | + } |
| 104 | + |
| 105 | +} |
0 commit comments