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.model.api.ChoiceCaseNode;
18 import org.opendaylight.yangtools.yang.model.api.ChoiceSchemaNode;
19 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
20 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
22 public final class ParserStreamUtils {
24 private ParserStreamUtils() {
25 throw new UnsupportedOperationException("Utility class should not be instantiated.");
29 * Returns stack of schema nodes via which it was necessary to pass to get schema node with specified
30 * {@code childName} and {@code namespace}.
32 * @return stack of schema nodes via which it was passed through. If found schema node is direct child then stack
33 * contains only one node. If it is found under choice and case then stack should contains 2*n+1 element
34 * (where n is number of choices through it was passed)
36 public static Deque<DataSchemaNode> findSchemaNodeByNameAndNamespace(final DataSchemaNode dataSchemaNode,
37 final String childName, final URI namespace) {
38 final Deque<DataSchemaNode> result = new ArrayDeque<>();
39 final List<ChoiceSchemaNode> childChoices = new ArrayList<>();
40 DataSchemaNode potentialChildNode = null;
41 if (dataSchemaNode instanceof DataNodeContainer) {
42 for (final DataSchemaNode childNode : ((DataNodeContainer) dataSchemaNode).getChildNodes()) {
43 if (childNode instanceof ChoiceSchemaNode) {
44 childChoices.add((ChoiceSchemaNode) childNode);
46 final QName childQName = childNode.getQName();
48 if (childQName.getLocalName().equals(childName) && childQName.getNamespace().equals(namespace)) {
49 if (potentialChildNode == null
50 || childQName.getRevision().after(potentialChildNode.getQName().getRevision())) {
51 potentialChildNode = childNode;
57 if (potentialChildNode != null) {
58 result.push(potentialChildNode);
62 // try to find data schema node in choice (looking for first match)
63 for (final ChoiceSchemaNode choiceNode : childChoices) {
64 for (final ChoiceCaseNode concreteCase : choiceNode.getCases()) {
65 final Deque<DataSchemaNode> resultFromRecursion = findSchemaNodeByNameAndNamespace(concreteCase,
66 childName, namespace);
67 if (!resultFromRecursion.isEmpty()) {
68 resultFromRecursion.push(concreteCase);
69 resultFromRecursion.push(choiceNode);
70 return resultFromRecursion;