Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improvements for the WellformednessChecker #116

Merged
merged 4 commits into from
Jul 26, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
* ENTITY_OR_LITERAL = ENTITY | literal
* ENTITY = startEntity, ENTITY_OR_LITERAL*, endEntity)
*
* The {@link WellFormednessChecker} can be used to check if a stream conforms
* The {@link WellformednessChecker} can be used to check if a stream conforms
* to these rules.
*
* @see DefaultStreamReceiver
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,32 +24,32 @@

/**
* Creates a literal for each entry in a map.
*
*
* @author Christoph Böhme
*/
public final class MapToStream extends
DefaultObjectPipe<Map<?, ?>, StreamReceiver> {


private Object idKey = StreamConstants.ID;

public Object getIdKey() {
return idKey;
}

public void setIdKey(final Object idKey) {
this.idKey = idKey;
}

@Override
public void process(final Map<?, ?> map) {
final Object id = map.get(idKey);
if (id == null) {
getReceiver().startRecord(null);
getReceiver().startRecord("");
} else {
getReceiver().startRecord(id.toString());
}
for (Map.Entry<?, ?> entry: map.entrySet()) {
for (final Map.Entry<?, ?> entry: map.entrySet()) {
getReceiver().literal(entry.getKey().toString(), entry.getValue().toString());
}
getReceiver().endRecord();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@

/**
* Reads CG-XML files.
*
*
* @author Christoph Böhme
*
*
*/
@Description("Reads CG-XML files")
@In(XmlReceiver.class)
Expand All @@ -46,7 +46,12 @@ public final class CGXmlHandler extends DefaultXmlPipe<StreamReceiver> {
public void startElement(final String uri, final String localName,
final String qName, final Attributes attributes) {
if (RECORD_TAG.equals(localName)) {
getReceiver().startRecord(attributes.getValue("", ID_ATTR));
final String recordId = attributes.getValue("", ID_ATTR);
if (recordId == null) {
getReceiver().startRecord("");
} else {
getReceiver().startRecord(recordId);
}
} else if (ENTITY_TAG.equals(localName)) {
getReceiver().startEntity(attributes.getValue("", NAME_ATTR));
} else if (LITERAL_TAG.equals(localName)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ private void appendChildren(final StringBuilder builder) {
private boolean strictKeyOrder;
private boolean strictValueOrder;

private final WellFormednessChecker wellFormednessChecker = new WellFormednessChecker();
private final WellformednessChecker wellformednessChecker = new WellformednessChecker();

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

wellFormednessChecker.startRecord(identifier);
wellformednessChecker.startRecord(identifier);

validating = true;

Expand All @@ -224,7 +224,7 @@ public void endRecord() {
throw new ValidationException(VALIDATION_FAILED);
}

wellFormednessChecker.endRecord();
wellformednessChecker.endRecord();

if (!closeGroups()) {
validationFailed = true;
Expand All @@ -240,7 +240,7 @@ public void startEntity(final String name) {
throw new ValidationException(VALIDATION_FAILED);
}

wellFormednessChecker.startEntity(name);
wellformednessChecker.startEntity(name);

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

wellFormednessChecker.endEntity();
wellformednessChecker.endEntity();

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

wellFormednessChecker.literal(name, value);
wellformednessChecker.literal(name, value);

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

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

@Override
public void resetStream() {
wellFormednessChecker.resetStream();
wellformednessChecker.resetStream();

validating = false;
validationFailed = false;
Expand All @@ -308,7 +308,7 @@ public void closeStream() {
throw new ValidationException(VALIDATION_FAILED);
}

wellFormednessChecker.closeStream();
wellformednessChecker.closeStream();

validating = false;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,29 +19,33 @@
import org.culturegraph.mf.framework.StreamReceiver;

/**
* A stream receiver that throws an {@link WellformednessException} if
* the stream event methods are called in an invalid order. Additionally,
* A stream receiver that throws an {@link WellformednessException} if
* the stream event methods are called in an invalid order. Additionally,
* the stream receiver checks that entity and literal names are not null.
*
*
* @see StreamValidator
* @see WellformednessException
*
*
* @author Christoph Böhme
*
*
*/
public final class WellFormednessChecker implements StreamReceiver {

public final class WellformednessChecker implements StreamReceiver {

private static final String ID_MUST_NOT_BE_NULL = "id must not be null";
private static final String NAME_MUST_NOT_BE_NULL = "name must not be null";

private static final String NOT_IN_RECORD = "Not in record";
private static final String NOT_IN_ENTITY = "Not in entity";
private static final String IN_ENTITY = "In entity";
private static final String IN_RECORD = "In record";

private int nestingLevel;

@Override
public void startRecord(final String identifier) {
if (identifier == null) {
throw new WellformednessException(ID_MUST_NOT_BE_NULL);
}
if (nestingLevel > 0) {
throw new WellformednessException(IN_RECORD);
}
Expand All @@ -50,10 +54,10 @@ public void startRecord(final String identifier) {

@Override
public void endRecord() {
if (nestingLevel < 1) {
if (nestingLevel < 1) {
throw new WellformednessException(NOT_IN_RECORD);
} else if (nestingLevel > 1) {
throw new WellformednessException(IN_ENTITY);
throw new WellformednessException(IN_ENTITY);
}
nestingLevel -= 1;
}
Expand Down Expand Up @@ -86,7 +90,7 @@ public void literal(final String name, final String value) {
throw new WellformednessException(NOT_IN_RECORD);
}
}

@Override
public void resetStream() {
nestingLevel = 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import java.util.Map;

import org.culturegraph.mf.exceptions.FormatException;
import org.culturegraph.mf.stream.converter.MapToStream;
import org.culturegraph.mf.stream.sink.EventList;
import org.culturegraph.mf.stream.sink.StreamValidator;
import org.culturegraph.mf.util.StreamConstants;
Expand All @@ -29,9 +28,9 @@

/**
* Test {@link MapToStream}
*
*
* @author Christoph Böhme
*
*
*/
public final class MapToStreamTest {

Expand All @@ -51,16 +50,16 @@ public void testStringStringMap() {
expected.literal(KEYS[1], VALUES[1]);
expected.literal(StreamConstants.ID, RECORD_ID);
expected.endRecord();

final Map<String, String> map = new HashMap<String, String>();
map.put(KEYS[0], VALUES[0]);
map.put(KEYS[1], VALUES[1]);
map.put(StreamConstants.ID, RECORD_ID);

final MapToStream mapToStream = new MapToStream();

Assert.assertEquals(StreamConstants.ID, mapToStream.getIdKey());

checkResults(expected, mapToStream, map);
}

Expand All @@ -73,48 +72,48 @@ public void testIntIntMap() {
expected.literal(KEYS[1], VALUES[1]);
expected.literal(Integer.toString(INT_ID_KEY), RECORD_ID);
expected.endRecord();

final Map<Integer, Integer> map = new HashMap<Integer, Integer>();
map.put(INT_KEYS[0], INT_VALUES[0]);
map.put(INT_KEYS[1], INT_VALUES[1]);
map.put(INT_ID_KEY, INT_RECORD_ID);

final MapToStream mapToStream = new MapToStream();
mapToStream.setIdKey(-1);

Assert.assertEquals(-1, mapToStream.getIdKey());

checkResults(expected, mapToStream, map);
}

@Test
public void testStringIntMap() {
final EventList expected = new EventList();
expected.startRecord(null);
expected.startRecord("");
expected.literal(KEYS[0], VALUES[0]);
expected.literal(KEYS[1], VALUES[1]);
expected.endRecord();

final Map<String, Integer> map = new HashMap<String, Integer>();
map.put(KEYS[0], INT_VALUES[0]);
map.put(KEYS[1], INT_VALUES[1]);

final MapToStream mapToStream = new MapToStream();

checkResults(expected, mapToStream, map);
}

private void checkResults(final EventList expected, final MapToStream mapToStream, final Map<?, ?> map) {

final StreamValidator validator = new StreamValidator(expected.getEvents());

mapToStream.setReceiver(validator);

try {
mapToStream.process(map);
mapToStream.closeStream();
} catch (FormatException e){
} catch (final FormatException e){
Assert.fail(e.toString());
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@
package org.culturegraph.mf.stream.converter.xml;

import org.culturegraph.mf.stream.DataFilePath;
import org.culturegraph.mf.stream.converter.xml.CGXmlHandler;
import org.culturegraph.mf.stream.converter.xml.XmlDecoder;
import org.culturegraph.mf.stream.sink.EventList;
import org.culturegraph.mf.stream.sink.StreamValidator;
import org.culturegraph.mf.stream.source.ResourceOpener;
Expand All @@ -35,27 +33,27 @@ public final class CGXmlReaderTest {
private static final String NUMBER = "Number";
private static final String POSTCODE = "Postcode";
private static final String CITY = "City";

@Test
public void testReadStringStreamReceiver() {
final ResourceOpener opener = new ResourceOpener();
final XmlDecoder saxReader = new XmlDecoder();
final CGXmlHandler cgHandler = new CGXmlHandler();
final EventList writer = new EventList();


opener.setReceiver(saxReader)
.setReceiver(cgHandler)
.setReceiver(writer);

opener.process(DataFilePath.CG_XML);
opener.closeStream();

final StreamValidator validator = new StreamValidator(writer.getEvents());
validator.setStrictRecordOrder(true);
validator.setStrictKeyOrder(true);
validator.setStrictValueOrder(true);

validator.startRecord("1");
validator.literal(NAME, "Thomas Mann");
validator.startEntity(ADDRESS);
Expand All @@ -67,7 +65,7 @@ public void testReadStringStreamReceiver() {
validator.literal(POSTCODE, null);
validator.endEntity();
validator.endRecord();
validator.startRecord(null);
validator.startRecord("");
validator.literal(NAME, "Günter Grass");
validator.startEntity(ADDRESS);
validator.startEntity(STREET);
Expand Down
Loading