private List<Content> processFragments(JDOMFactory factory, XMLStreamReader stream, StAXFilter filter) throws JDOMException {
int state = stream.getEventType();
if (START_DOCUMENT != state) {
throw new JDOMException("JDOM requires that XMLStreamReaders " +
"are at their beginning when being processed.");
}
List<Content> ret = new ArrayList<Content>();
int depth = 0;
String text = null;
try {
while (stream.hasNext() && (state = stream.next()) != END_DOCUMENT) {
switch (state) {
case START_DOCUMENT:
throw new JDOMException("Illegal state for XMLStreamReader. Cannot get XML Fragment for state START_DOCUMENT" );
case END_DOCUMENT:
throw new JDOMException("Illegal state for XMLStreamReader. Cannot get XML Fragment for state END_DOCUMENT" );
case END_ELEMENT:
throw new JDOMException("Illegal state for XMLStreamReader. Cannot get XML Fragment for state END_ELEMENT" );
case START_ELEMENT:
final QName qn = stream.getName();
if (filter.includeElement(depth, qn.getLocalPart(),
Namespace.getNamespace(qn.getPrefix(), qn.getNamespaceURI()))) {
ret.add(processPrunableElement(factory, stream, depth, filter));
} else {
final int back = depth;
depth++;
while (depth > back && stream.hasNext()) {
state = stream.next();
if (state == START_ELEMENT) {
depth++;
} else if (state == END_ELEMENT) {
depth--;
}
}
}
break;
case DTD:
if (filter.includeDocType()) {
ret.add(DTDParser.parse(stream.getText(), factory));
}
break;
case CDATA:
if ((text = filter.includeCDATA(depth, stream.getText())) != null) {
ret.add(factory.cdata(text));
}
break;
case SPACE:
case CHARACTERS:
if ((text = filter.includeText(depth, stream.getText())) != null) {
ret.add(factory.text(text));
}
break;
case COMMENT:
if ((text = filter.includeComment(depth, stream.getText())) != null) {
ret.add(factory.comment(text));
}
break;
case ENTITY_REFERENCE:
if (filter.includeEntityRef(depth, stream.getLocalName())) {
ret.add(factory.entityRef(stream.getLocalName()));
}
break;
case PROCESSING_INSTRUCTION:
if (filter.includeProcessingInstruction(depth, stream.getPITarget())) {
ret.add(factory.processingInstruction(
stream.getPITarget(), stream.getPIData()));
}
break;
default:
throw new JDOMException("Unexpected XMLStream event " + stream.getEventType());
}
}
} catch (XMLStreamException e) {
throw new JDOMException("Unable to process fragments from XMLStreamReader.", e);
}
return ret;
}