Skip to content

Commit b2795c7

Browse files
author
Markus M. Geipel
committed
Merge pull request #116 from cboehme/issue-115-null-record-ids
Improvements for the WellformednessChecker
2 parents 7e8c13a + c7d4009 commit b2795c7

File tree

8 files changed

+228
-62
lines changed

8 files changed

+228
-62
lines changed

src/main/java/org/culturegraph/mf/framework/StreamReceiver.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
* ENTITY_OR_LITERAL = ENTITY | literal
2626
* ENTITY = startEntity, ENTITY_OR_LITERAL*, endEntity)
2727
*
28-
* The {@link WellFormednessChecker} can be used to check if a stream conforms
28+
* The {@link WellformednessChecker} can be used to check if a stream conforms
2929
* to these rules.
3030
*
3131
* @see DefaultStreamReceiver

src/main/java/org/culturegraph/mf/stream/converter/MapToStream.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,32 +24,32 @@
2424

2525
/**
2626
* Creates a literal for each entry in a map.
27-
*
27+
*
2828
* @author Christoph Böhme
2929
*/
3030
public final class MapToStream extends
3131
DefaultObjectPipe<Map<?, ?>, StreamReceiver> {
3232

33-
33+
3434
private Object idKey = StreamConstants.ID;
35-
35+
3636
public Object getIdKey() {
3737
return idKey;
3838
}
39-
39+
4040
public void setIdKey(final Object idKey) {
4141
this.idKey = idKey;
4242
}
43-
43+
4444
@Override
4545
public void process(final Map<?, ?> map) {
4646
final Object id = map.get(idKey);
4747
if (id == null) {
48-
getReceiver().startRecord(null);
48+
getReceiver().startRecord("");
4949
} else {
5050
getReceiver().startRecord(id.toString());
5151
}
52-
for (Map.Entry<?, ?> entry: map.entrySet()) {
52+
for (final Map.Entry<?, ?> entry: map.entrySet()) {
5353
getReceiver().literal(entry.getKey().toString(), entry.getValue().toString());
5454
}
5555
getReceiver().endRecord();

src/main/java/org/culturegraph/mf/stream/converter/xml/CGXmlHandler.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@
2626

2727
/**
2828
* Reads CG-XML files.
29-
*
29+
*
3030
* @author Christoph Böhme
31-
*
31+
*
3232
*/
3333
@Description("Reads CG-XML files")
3434
@In(XmlReceiver.class)
@@ -46,7 +46,12 @@ public final class CGXmlHandler extends DefaultXmlPipe<StreamReceiver> {
4646
public void startElement(final String uri, final String localName,
4747
final String qName, final Attributes attributes) {
4848
if (RECORD_TAG.equals(localName)) {
49-
getReceiver().startRecord(attributes.getValue("", ID_ATTR));
49+
final String recordId = attributes.getValue("", ID_ATTR);
50+
if (recordId == null) {
51+
getReceiver().startRecord("");
52+
} else {
53+
getReceiver().startRecord(recordId);
54+
}
5055
} else if (ENTITY_TAG.equals(localName)) {
5156
getReceiver().startEntity(attributes.getValue("", NAME_ATTR));
5257
} else if (LITERAL_TAG.equals(localName)) {

src/main/java/org/culturegraph/mf/stream/sink/StreamValidator.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ private void appendChildren(final StringBuilder builder) {
156156
private boolean strictKeyOrder;
157157
private boolean strictValueOrder;
158158

159-
private final WellFormednessChecker wellFormednessChecker = new WellFormednessChecker();
159+
private final WellformednessChecker wellformednessChecker = new WellformednessChecker();
160160

161161
public StreamValidator(final List<Event> expectedStream) {
162162
this.eventStream = new EventNode(null, null);
@@ -207,7 +207,7 @@ public void startRecord(final String identifier) {
207207
throw new ValidationException(VALIDATION_FAILED);
208208
}
209209

210-
wellFormednessChecker.startRecord(identifier);
210+
wellformednessChecker.startRecord(identifier);
211211

212212
validating = true;
213213

@@ -224,7 +224,7 @@ public void endRecord() {
224224
throw new ValidationException(VALIDATION_FAILED);
225225
}
226226

227-
wellFormednessChecker.endRecord();
227+
wellformednessChecker.endRecord();
228228

229229
if (!closeGroups()) {
230230
validationFailed = true;
@@ -240,7 +240,7 @@ public void startEntity(final String name) {
240240
throw new ValidationException(VALIDATION_FAILED);
241241
}
242242

243-
wellFormednessChecker.startEntity(name);
243+
wellformednessChecker.startEntity(name);
244244

245245
if (!openGroups(Event.Type.START_ENTITY, name, strictKeyOrder, strictValueOrder)) {
246246
validationFailed = true;
@@ -255,7 +255,7 @@ public void endEntity() {
255255
throw new ValidationException(VALIDATION_FAILED);
256256
}
257257

258-
wellFormednessChecker.endEntity();
258+
wellformednessChecker.endEntity();
259259

260260
if (!closeGroups()) {
261261
validationFailed = true;
@@ -270,7 +270,7 @@ public void literal(final String name, final String value) {
270270
throw new ValidationException(VALIDATION_FAILED);
271271
}
272272

273-
wellFormednessChecker.literal(name, value);
273+
wellformednessChecker.literal(name, value);
274274

275275
final List<EventNode> stackFrame = stack.peek();
276276

@@ -292,7 +292,7 @@ public void literal(final String name, final String value) {
292292

293293
@Override
294294
public void resetStream() {
295-
wellFormednessChecker.resetStream();
295+
wellformednessChecker.resetStream();
296296

297297
validating = false;
298298
validationFailed = false;
@@ -308,7 +308,7 @@ public void closeStream() {
308308
throw new ValidationException(VALIDATION_FAILED);
309309
}
310310

311-
wellFormednessChecker.closeStream();
311+
wellformednessChecker.closeStream();
312312

313313
validating = false;
314314

src/main/java/org/culturegraph/mf/stream/sink/WellFormednessChecker.java renamed to src/main/java/org/culturegraph/mf/stream/sink/WellformednessChecker.java

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,29 +19,33 @@
1919
import org.culturegraph.mf.framework.StreamReceiver;
2020

2121
/**
22-
* A stream receiver that throws an {@link WellformednessException} if
23-
* the stream event methods are called in an invalid order. Additionally,
22+
* A stream receiver that throws an {@link WellformednessException} if
23+
* the stream event methods are called in an invalid order. Additionally,
2424
* the stream receiver checks that entity and literal names are not null.
25-
*
25+
*
2626
* @see StreamValidator
2727
* @see WellformednessException
28-
*
28+
*
2929
* @author Christoph Böhme
30-
*
30+
*
3131
*/
32-
public final class WellFormednessChecker implements StreamReceiver {
33-
32+
public final class WellformednessChecker implements StreamReceiver {
33+
34+
private static final String ID_MUST_NOT_BE_NULL = "id must not be null";
3435
private static final String NAME_MUST_NOT_BE_NULL = "name must not be null";
35-
36+
3637
private static final String NOT_IN_RECORD = "Not in record";
3738
private static final String NOT_IN_ENTITY = "Not in entity";
3839
private static final String IN_ENTITY = "In entity";
3940
private static final String IN_RECORD = "In record";
40-
41+
4142
private int nestingLevel;
42-
43+
4344
@Override
4445
public void startRecord(final String identifier) {
46+
if (identifier == null) {
47+
throw new WellformednessException(ID_MUST_NOT_BE_NULL);
48+
}
4549
if (nestingLevel > 0) {
4650
throw new WellformednessException(IN_RECORD);
4751
}
@@ -50,10 +54,10 @@ public void startRecord(final String identifier) {
5054

5155
@Override
5256
public void endRecord() {
53-
if (nestingLevel < 1) {
57+
if (nestingLevel < 1) {
5458
throw new WellformednessException(NOT_IN_RECORD);
5559
} else if (nestingLevel > 1) {
56-
throw new WellformednessException(IN_ENTITY);
60+
throw new WellformednessException(IN_ENTITY);
5761
}
5862
nestingLevel -= 1;
5963
}
@@ -86,7 +90,7 @@ public void literal(final String name, final String value) {
8690
throw new WellformednessException(NOT_IN_RECORD);
8791
}
8892
}
89-
93+
9094
@Override
9195
public void resetStream() {
9296
nestingLevel = 0;

src/test/java/org/culturegraph/mf/stream/converter/MapToStreamTest.java

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
import java.util.Map;
2020

2121
import org.culturegraph.mf.exceptions.FormatException;
22-
import org.culturegraph.mf.stream.converter.MapToStream;
2322
import org.culturegraph.mf.stream.sink.EventList;
2423
import org.culturegraph.mf.stream.sink.StreamValidator;
2524
import org.culturegraph.mf.util.StreamConstants;
@@ -29,9 +28,9 @@
2928

3029
/**
3130
* Test {@link MapToStream}
32-
*
31+
*
3332
* @author Christoph Böhme
34-
*
33+
*
3534
*/
3635
public final class MapToStreamTest {
3736

@@ -51,16 +50,16 @@ public void testStringStringMap() {
5150
expected.literal(KEYS[1], VALUES[1]);
5251
expected.literal(StreamConstants.ID, RECORD_ID);
5352
expected.endRecord();
54-
53+
5554
final Map<String, String> map = new HashMap<String, String>();
5655
map.put(KEYS[0], VALUES[0]);
5756
map.put(KEYS[1], VALUES[1]);
5857
map.put(StreamConstants.ID, RECORD_ID);
5958

6059
final MapToStream mapToStream = new MapToStream();
61-
60+
6261
Assert.assertEquals(StreamConstants.ID, mapToStream.getIdKey());
63-
62+
6463
checkResults(expected, mapToStream, map);
6564
}
6665

@@ -73,48 +72,48 @@ public void testIntIntMap() {
7372
expected.literal(KEYS[1], VALUES[1]);
7473
expected.literal(Integer.toString(INT_ID_KEY), RECORD_ID);
7574
expected.endRecord();
76-
75+
7776
final Map<Integer, Integer> map = new HashMap<Integer, Integer>();
7877
map.put(INT_KEYS[0], INT_VALUES[0]);
7978
map.put(INT_KEYS[1], INT_VALUES[1]);
8079
map.put(INT_ID_KEY, INT_RECORD_ID);
81-
80+
8281
final MapToStream mapToStream = new MapToStream();
8382
mapToStream.setIdKey(-1);
84-
83+
8584
Assert.assertEquals(-1, mapToStream.getIdKey());
86-
85+
8786
checkResults(expected, mapToStream, map);
8887
}
89-
88+
9089
@Test
9190
public void testStringIntMap() {
9291
final EventList expected = new EventList();
93-
expected.startRecord(null);
92+
expected.startRecord("");
9493
expected.literal(KEYS[0], VALUES[0]);
9594
expected.literal(KEYS[1], VALUES[1]);
9695
expected.endRecord();
97-
96+
9897
final Map<String, Integer> map = new HashMap<String, Integer>();
9998
map.put(KEYS[0], INT_VALUES[0]);
10099
map.put(KEYS[1], INT_VALUES[1]);
101-
100+
102101
final MapToStream mapToStream = new MapToStream();
103-
102+
104103
checkResults(expected, mapToStream, map);
105104
}
106-
105+
107106
private void checkResults(final EventList expected, final MapToStream mapToStream, final Map<?, ?> map) {
108-
107+
109108
final StreamValidator validator = new StreamValidator(expected.getEvents());
110-
109+
111110
mapToStream.setReceiver(validator);
112-
111+
113112
try {
114113
mapToStream.process(map);
115114
mapToStream.closeStream();
116-
} catch (FormatException e){
115+
} catch (final FormatException e){
117116
Assert.fail(e.toString());
118-
}
117+
}
119118
}
120119
}

src/test/java/org/culturegraph/mf/stream/converter/xml/CGXmlReaderTest.java

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@
1616
package org.culturegraph.mf.stream.converter.xml;
1717

1818
import org.culturegraph.mf.stream.DataFilePath;
19-
import org.culturegraph.mf.stream.converter.xml.CGXmlHandler;
20-
import org.culturegraph.mf.stream.converter.xml.XmlDecoder;
2119
import org.culturegraph.mf.stream.sink.EventList;
2220
import org.culturegraph.mf.stream.sink.StreamValidator;
2321
import org.culturegraph.mf.stream.source.ResourceOpener;
@@ -35,27 +33,27 @@ public final class CGXmlReaderTest {
3533
private static final String NUMBER = "Number";
3634
private static final String POSTCODE = "Postcode";
3735
private static final String CITY = "City";
38-
36+
3937
@Test
4038
public void testReadStringStreamReceiver() {
4139
final ResourceOpener opener = new ResourceOpener();
4240
final XmlDecoder saxReader = new XmlDecoder();
4341
final CGXmlHandler cgHandler = new CGXmlHandler();
4442
final EventList writer = new EventList();
45-
46-
43+
44+
4745
opener.setReceiver(saxReader)
4846
.setReceiver(cgHandler)
4947
.setReceiver(writer);
50-
48+
5149
opener.process(DataFilePath.CG_XML);
5250
opener.closeStream();
53-
51+
5452
final StreamValidator validator = new StreamValidator(writer.getEvents());
5553
validator.setStrictRecordOrder(true);
5654
validator.setStrictKeyOrder(true);
5755
validator.setStrictValueOrder(true);
58-
56+
5957
validator.startRecord("1");
6058
validator.literal(NAME, "Thomas Mann");
6159
validator.startEntity(ADDRESS);
@@ -67,7 +65,7 @@ public void testReadStringStreamReceiver() {
6765
validator.literal(POSTCODE, null);
6866
validator.endEntity();
6967
validator.endRecord();
70-
validator.startRecord(null);
68+
validator.startRecord("");
7169
validator.literal(NAME, "Günter Grass");
7270
validator.startEntity(ADDRESS);
7371
validator.startEntity(STREET);

0 commit comments

Comments
 (0)