/*
 *  Copyright 2013 Deutsche Nationalbibliothek
 *
 *  Licensed under the Apache License, Version 2.0 the "License";
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package org.culturegraph.mf.stream.sink;

import org.culturegraph.mf.exceptions.WellformednessException;
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, 
 * 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 {
	
	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 (nestingLevel > 0) {
			throw new WellformednessException(IN_RECORD);
		}
		nestingLevel += 1;
	}

	@Override
	public void endRecord() {
		if (nestingLevel < 1) { 
			throw new WellformednessException(NOT_IN_RECORD);
		} else if (nestingLevel > 1) {
			throw new WellformednessException(IN_ENTITY);			
		}
		nestingLevel -= 1;
	}

	@Override
	public void startEntity(final String name) {
		if (name == null) {
			throw new WellformednessException(NAME_MUST_NOT_BE_NULL);
		}
		if (nestingLevel < 1) {
			throw new WellformednessException(NOT_IN_RECORD);
		}
		nestingLevel += 1;
	}

	@Override
	public void endEntity() {
		if (nestingLevel < 2) {
			throw new WellformednessException(NOT_IN_ENTITY);
		}
		nestingLevel -= 1;
	}

	@Override
	public void literal(final String name, final String value) {
		if (name == null) {
			throw new WellformednessException(NAME_MUST_NOT_BE_NULL);
		}
		if (nestingLevel < 1) {
			throw new WellformednessException(NOT_IN_RECORD);
		}
	}
	
	@Override
	public void resetStream() {
		nestingLevel = 0;
	}

	@Override
	public void closeStream() {
		if (nestingLevel > 0) {
			throw new WellformednessException(IN_RECORD);
		}
	}

}

@@ -845,17 +845,16 @@
 tion%7D if
- 
 %0A * the 
@@ -919,17 +919,16 @@
 ionally,
- 
 %0A * the 
@@ -996,17 +996,16 @@
 null.%0A *
- 
 %0A * @see
@@ -1055,17 +1055,16 @@
 ption%0A *
- 
 %0A * @aut
@@ -1085,17 +1085,16 @@
 B%C3%B6hme%0A *
- 
 %0A */%0Apub
@@ -1159,17 +1159,90 @@
 eiver %7B%0A
-%09
+%0A%09private static final String ID_MUST_NOT_BE_NULL = %22id must not be null%22;
 %0A%09privat
@@ -1312,18 +1312,17 @@
  null%22;%0A
-%09
 %0A
+
 %09private
@@ -1545,18 +1545,17 @@
 ecord%22;%0A
-%09
 %0A
+
 %09private
@@ -1573,18 +1573,17 @@
 gLevel;%0A
-%09
 %0A
+
 %09@Overri
@@ -1633,24 +1633,115 @@
 entifier) %7B%0A
+%09%09if (identifier == null) %7B%0A%09%09%09throw new WellformednessException(ID_MUST_NOT_BE_NULL);%0A%09%09%7D%0A
 %09%09if (nestin
@@ -1895,17 +1895,16 @@
 l %3C 1) %7B
- 
 %0A%09%09%09thro
@@ -2030,19 +2030,16 @@
 ENTITY);
-%09%09%09
 %0A%09%09%7D%0A%09%09n
@@ -2704,17 +2704,16 @@
 %0A%09%09%7D%0A%09%7D%0A
-%09
 %0A%09@Overr

Fix issue #115: WellFormednessChecker should check for null in ids.