use of com.helger.xml.microdom.util.ChildrenProviderElementWithName in project ph-commons by phax.
the class TreeXMLConverter method fillXMLAsTreeWithID.
public static <KEYTYPE, DATATYPE, ITEMTYPE extends ITreeItemWithID<KEYTYPE, DATATYPE, ITEMTYPE>> void fillXMLAsTreeWithID(@Nonnull final IMicroElement aElement, @Nonnull final Function<? super String, ? extends KEYTYPE> aIDConverter, @Nonnull final IConverterMicroNodeToTreeItem<? extends DATATYPE> aDataConverter, @Nonnull final BasicTreeWithID<KEYTYPE, DATATYPE, ITEMTYPE> aTree) {
final String sNamespaceURI = aDataConverter.getNamespaceURI();
final NonBlockingStack<ITEMTYPE> aParents = new NonBlockingStack<>();
aParents.push(aTree.getRootItem());
MicroVisitor.visit(aElement, new ChildrenProviderElementWithName(sNamespaceURI, ELEMENT_ITEM), new DefaultHierarchyVisitorCallback<IMicroElement>() {
@Override
@Nonnull
public EHierarchyVisitorReturn onItemBeforeChildren(@Nullable final IMicroElement eItem) {
if (eItem != null) {
final KEYTYPE aTreeItemID = aIDConverter.apply(eItem.getAttributeValue(ATTR_ID));
final IMicroElement eData = eItem.getFirstChildElement(sNamespaceURI, ELEMENT_DATA);
final DATATYPE aTreeItemValue = aDataConverter.getAsDataValue(eData);
final ITEMTYPE aTreeItem = aParents.peek().createChildItem(aTreeItemID, aTreeItemValue);
aParents.push(aTreeItem);
}
return EHierarchyVisitorReturn.CONTINUE;
}
@Override
@Nonnull
public EHierarchyVisitorReturn onItemAfterChildren(@Nullable final IMicroElement aItem) {
if (aItem != null)
aParents.pop();
return EHierarchyVisitorReturn.CONTINUE;
}
});
}
Aggregations