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.CaseSchemaNode;
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 {
24 private ParserStreamUtils() {
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();
47 if (childQName.getLocalName().equals(childName) && childQName.getNamespace().equals(namespace)
48 && (potentialChildNode == null || Revision.compare(childQName.getRevision(),
49 potentialChildNode.getQName().getRevision()) > 0)) {
50 potentialChildNode = childNode;
55 if (potentialChildNode != null) {
56 result.push(potentialChildNode);
60 // try to find data schema node in choice (looking for first match)
61 for (final ChoiceSchemaNode choiceNode : childChoices) {
62 for (final CaseSchemaNode concreteCase : choiceNode.getCases()) {
63 final Deque<DataSchemaNode> resultFromRecursion = findSchemaNodeByNameAndNamespace(concreteCase,
64 childName, namespace);
65 if (!resultFromRecursion.isEmpty()) {
66 resultFromRecursion.push(concreteCase);
67 resultFromRecursion.push(choiceNode);
68 return resultFromRecursion;