try {
EventLoop: while (reader.hasNext()) {
XMLEvent event = reader.nextEvent();
if (event.isStartElement()) {
StartElement start = event.asStartElement();
if (isRemovable(start)) {
while (reader.hasNext()) {
XMLEvent next = reader.nextEvent();
if (next.isEndElement() && next.asEndElement().getName().equals(start.getName()))
continue EventLoop;
}
// If this is reached, we finished the document before the tag ended
throw new RuntimeException(String.format("End of file was reached before %s closing tag was found.",
start.getName()));
}
else if (toAdd.containsKey(start.getName().getLocalPart())
&& !toAdd.get(start.getName().getLocalPart()).isEmpty()) {
writer.add(event);
final XMLEventFactory eventFactory = XMLEventFactory.newInstance();
for (final Tag tag : toAdd.get(start.getName().getLocalPart())) {
List<Attribute> attrs = new ArrayList<Attribute>();
for (final AttributeEntry entry : tag) {
attrs.add(eventFactory.createAttribute(entry.getKey(), entry.getValue()));
}
StartElement newStart = eventFactory.createStartElement(QName.valueOf(tag.getName()), attrs.iterator(),
null);
EndElement newEnd = eventFactory.createEndElement(newStart.getName(), null);
writer.add(newStart);
writer.add(newEnd);
}
}