diff --git a/src/main/java/org/stax/StaxHandler.java b/src/main/java/org/stax/StaxHandler.java index 8537f17..2181b8c 100644 --- a/src/main/java/org/stax/StaxHandler.java +++ b/src/main/java/org/stax/StaxHandler.java @@ -2,6 +2,7 @@ import javax.xml.stream.XMLStreamException; import java.util.function.Consumer; +import java.util.function.Supplier; /** * Like SAX event {@link org.xml.sax.ContentHandler handler} but for StAX. @@ -32,10 +33,10 @@ static StaxHandler pushOrSkip(String expected, StaxHandler child) { }; } static StaxHandler require(String expected, StaxHandler child) { - return ((sr, name) -> { + return (sr, name) -> { sr.require(expected); sr.push(child); - }); + }; } /** @@ -51,6 +52,10 @@ interface StatefulHandler { void start(S state, StaxReader sr, String name) throws XMLStreamException; } + static StaxHandler stateful(String expected, Supplier init, StatefulHandler handler, Consumer end) { + return stateful((sr, name) -> {sr.require(expected); return init.get();}, handler, end); + } + /** *
{@code
 	 *  
diff --git a/src/test/java/org/stax/StaxReaderImplTest.java b/src/test/java/org/stax/StaxReaderImplTest.java
index 6c852bc..837b96a 100644
--- a/src/test/java/org/stax/StaxReaderImplTest.java
+++ b/src/test/java/org/stax/StaxReaderImplTest.java
@@ -8,8 +8,7 @@
 import org.junit.Test;
 
 import static org.junit.Assert.assertEquals;
-import static org.stax.StaxHandler.require;
-import static org.stax.StaxHandler.stateful;
+import static org.stax.StaxHandler.*;
 
 /**
  * Adapted from Practical XML Parsing With Java and StaxMate
@@ -38,10 +37,7 @@ static void handleRootChildElement(Food food, StaxReader sr, String name) {
 				return new Animal(sr.getAttributeValue("name"));
 			}, StaxReaderImplTest::extractAnimal, food.animals::add));
 		} else if ("vegetables".equals(name)) {
-			sr.push(stateful((r, n) -> {
-				sr.require("vegetable");
-				return new Vegetable();
-			}, StaxReaderImplTest::extractVegetable, food.vegetables::add));
+			sr.push(stateful("vegetable", Vegetable::new, StaxReaderImplTest::extractVegetable, food.vegetables::add));
 		}
 	}
 	private static void extractVegetable(Vegetable vegetable, StaxReader sr, String name) throws XMLStreamException {