2 * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
9 package org.opendaylight.yangtools.yang.data.util;
12 import java.util.ArrayDeque;
13 import java.util.ArrayList;
14 import java.util.Deque;
15 import java.util.List;
16 import org.opendaylight.yangtools.yang.common.QName;
17 import org.opendaylight.yangtools.yang.common.Revision;
18 import org.opendaylight.yangtools.yang.model.api.ChoiceCaseNode;
19 import org.opendaylight.yangtools.yang.model.api.ChoiceSchemaNode;
20 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
21 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
23 public final class ParserStreamUtils {
25 private ParserStreamUtils() {
26 throw new UnsupportedOperationException("Utility class should not be instantiated.");
30 * Returns stack of schema nodes via which it was necessary to pass to get schema node with specified
31 * {@code childName} and {@code namespace}.
33 * @return stack of schema nodes via which it was passed through. If found schema node is direct child then stack
34 * contains only one node. If it is found under choice and case then stack should contains 2*n+1 element
35 * (where n is number of choices through it was passed)
37 public static Deque<DataSchemaNode> findSchemaNodeByNameAndNamespace(final DataSchemaNode dataSchemaNode,
38 final String childName, final URI namespace) {
39 final Deque<DataSchemaNode> result = new ArrayDeque<>();
40 final List<ChoiceSchemaNode> childChoices = new ArrayList<>();
41 DataSchemaNode potentialChildNode = null;
42 if (dataSchemaNode instanceof DataNodeContainer) {
43 for (final DataSchemaNode childNode : ((DataNodeContainer) dataSchemaNode).getChildNodes()) {
44 if (childNode instanceof ChoiceSchemaNode) {
45 childChoices.add((ChoiceSchemaNode) childNode);
47 final QName childQName = childNode.getQName();
49 if (childQName.getLocalName().equals(childName) && childQName.getNamespace().equals(namespace)) {
50 if (potentialChildNode == null || Revision.compare(childQName.getRevision(),
51 potentialChildNode.getQName().getRevision()) > 0) {
52 potentialChildNode = childNode;
58 if (potentialChildNode != null) {
59 result.push(potentialChildNode);
63 // try to find data schema node in choice (looking for first match)
64 for (final ChoiceSchemaNode choiceNode : childChoices) {
65 for (final ChoiceCaseNode concreteCase : choiceNode.getCases()) {
66 final Deque<DataSchemaNode> resultFromRecursion = findSchemaNodeByNameAndNamespace(concreteCase,
67 childName, namespace);
68 if (!resultFromRecursion.isEmpty()) {
69 resultFromRecursion.push(concreteCase);
70 resultFromRecursion.push(choiceNode);
71 return resultFromRecursion;