package org.opendaylight.controller.yang.model.util; import java.util.ArrayList; import java.util.Iterator; import java.util.List; import java.util.Set; import org.opendaylight.controller.yang.model.api.ContainerSchemaNode; import org.opendaylight.controller.yang.model.api.DataNodeContainer; import org.opendaylight.controller.yang.model.api.DataSchemaNode; import org.opendaylight.controller.yang.model.api.LeafListSchemaNode; import org.opendaylight.controller.yang.model.api.LeafSchemaNode; import org.opendaylight.controller.yang.model.api.ListSchemaNode; public class DataNodeIterator implements Iterator { private final DataNodeContainer container; private final List allLists; private final List allContainers; private final List allLeafs; private final List allLeafLists; private final List allChilds; public DataNodeIterator(final DataNodeContainer container) { if (container == null) { throw new IllegalArgumentException("Data Node Container MUST be specified!"); } this.allContainers = new ArrayList(); this.allLists = new ArrayList(); this.allLeafs = new ArrayList(); this.allLeafLists = new ArrayList(); this.allChilds = new ArrayList(); this.container = container; traverse(this.container); } public List allContainers() { return allContainers; } public List allLists() { return allLists; } public List allLeafs() { return allLeafs; } public List allLeafLists() { return allLeafLists; } private void traverse(final DataNodeContainer dataNode) { if (!containChildDataNodeContainer(dataNode)) { return; } final Set childs = dataNode.getChildNodes(); if (childs != null) { for (DataSchemaNode childNode : childs) { if (childNode.isAugmenting()) { continue; } allChilds.add(childNode); if (childNode instanceof ContainerSchemaNode) { final ContainerSchemaNode container = (ContainerSchemaNode) childNode; allContainers.add(container); traverse(container); } else if (childNode instanceof ListSchemaNode) { final ListSchemaNode list = (ListSchemaNode) childNode; allLists.add(list); traverse(list); } else if (childNode instanceof LeafSchemaNode) { final LeafSchemaNode leaf = (LeafSchemaNode) childNode; allLeafs.add(leaf); } else if (childNode instanceof LeafListSchemaNode) { final LeafListSchemaNode leafList = (LeafListSchemaNode) childNode; allLeafLists.add(leafList); } } } } /** * Returns true if and only if the child node contain at least * one child container schema node or child list schema node, otherwise will * always returns false * * @param container * @return true if and only if the child node contain at least * one child container schema node or child list schema node, * otherwise will always returns false */ private boolean containChildDataNodeContainer( final DataNodeContainer container) { if (container != null) { final Set childs = container.getChildNodes(); if ((childs != null) && (childs.size() > 0)) { for (final DataSchemaNode childNode : childs) { if (childNode instanceof DataNodeContainer) { return true; } } } } return false; } @Override public boolean hasNext() { if (container.getChildNodes() != null) { Set childs = container.getChildNodes(); if ((childs != null) && !childs.isEmpty()) { return childs.iterator().hasNext(); } } return false; } @Override public DataSchemaNode next() { return allChilds.iterator().next(); } @Override public void remove() { throw new UnsupportedOperationException(); } }