org.nlab.xml.stream.consumer.XmlConsumer Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of xml-stream-css Show documentation
Show all versions of xml-stream-css Show documentation
Stream Xml using StAX and Css matcher
The newest version!
package org.nlab.xml.stream.consumer;
import javax.xml.stream.XMLStreamException;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.function.Predicate;
import java.util.stream.Stream;
import org.jooq.lambda.fi.util.function.CheckedFunction;
import org.nlab.exception.UncheckedExecutionException;
import org.nlab.xml.stream.XmlStream;
import org.nlab.xml.stream.context.StreamContext;
import org.nlab.xml.stream.matcher.EventMatcher;
import org.nlab.xml.stream.matcher.EventMatchers;
import org.nlab.xml.stream.predicate.XmlPredicate;
public class XmlConsumer {
private final XmlStream stream;
private final List> consumers = new ArrayList<>();
public XmlConsumer(XmlStream stream) {
this.stream = Objects.requireNonNull(stream);
}
public XmlConsumer matchCss(String query, CheckedConsumeAndContinueConsumer consumer) {
addConsumer(EventMatchers.css(query, consumer));
return this;
}
public XmlConsumer matchCss(String query, CheckedFunction consumer) {
addConsumer(EventMatchers.css(query, consumer));
return this;
}
public XmlConsumer matchElements(String[] elements, CheckedFunction consumer) {
addConsumer(EventMatchers.elements(elements, consumer));
return this;
}
public XmlConsumer matchElements(String[] elements, CheckedConsumeAndContinueConsumer consumer) {
addConsumer(EventMatchers.elements(elements, consumer));
return this;
}
public XmlConsumer matchElement(String element, CheckedFunction consumer) {
addConsumer(EventMatchers.element(element, consumer));
return this;
}
public XmlConsumer matchElement(String element, CheckedConsumeAndContinueConsumer consumer) {
addConsumer(EventMatchers.element(element, consumer));
return this;
}
public XmlConsumer matchAttribute(String attribute, CheckedFunction consumer) {
addConsumer(EventMatchers.attribute(attribute, consumer));
return this;
}
public XmlConsumer matchAttribute(String attribute, CheckedConsumeAndContinueConsumer consumer) {
addConsumer(EventMatchers.attribute(attribute, consumer));
return this;
}
public XmlConsumer matchAttributeValue(String attribute, String value, CheckedFunction consumer) {
addConsumer(EventMatchers.attributeValue(attribute, value, consumer));
return this;
}
public XmlConsumer matchAttributeValue(String attribute, String value, CheckedConsumeAndContinueConsumer consumer) {
addConsumer(EventMatchers.attributeValue(attribute, value, consumer));
return this;
}
public XmlConsumer match(Predicate predicate, CheckedFunction consumer) {
addConsumer(new EventMatcher(predicate, consumer));
return this;
}
public XmlConsumer match(Predicate predicate, CheckedConsumeAndContinueConsumer consumer) {
addConsumer(new EventMatcher(predicate, consumer));
return this;
}
public XmlConsumer addConsumer(CheckedFunction consumer) {
consumers.add(consumer);
return this;
}
/**
* Consume the Stream
*
*/
public void consume() {
for (CheckedFunction consumer : consumers) {
if (consumer instanceof EventMatcher
&& ((EventMatcher) consumer).getPredicate() instanceof XmlPredicate
&& ((XmlPredicate) ((EventMatcher) consumer).getPredicate()).requireSibbling()) {
stream.getXmlMatcherStreamReader().requireSibbling();
break;
}
}
try (Stream stream = this.stream) {
stream.forEach(c -> {
for (CheckedFunction consumer : consumers) {
try {
if (!consumer.apply(c)) {
break;
}
} catch (Throwable throwable) {
throw new UncheckedExecutionException(throwable);
}
}
});
}
}
}