Search in sources :

Example 1 with ChildrenProviderElementWithName

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;
        }
    });
}
Also used : Nonnull(javax.annotation.Nonnull) ChildrenProviderElementWithName(com.helger.xml.microdom.util.ChildrenProviderElementWithName) IMicroElement(com.helger.xml.microdom.IMicroElement) EHierarchyVisitorReturn(com.helger.commons.hierarchy.visit.EHierarchyVisitorReturn) NonBlockingStack(com.helger.commons.collection.NonBlockingStack)

Aggregations

NonBlockingStack (com.helger.commons.collection.NonBlockingStack)1 EHierarchyVisitorReturn (com.helger.commons.hierarchy.visit.EHierarchyVisitorReturn)1 IMicroElement (com.helger.xml.microdom.IMicroElement)1 ChildrenProviderElementWithName (com.helger.xml.microdom.util.ChildrenProviderElementWithName)1 Nonnull (javax.annotation.Nonnull)1