Do not pretty-print body class
[yangtools.git] / data / yang-data-util / src / main / java / org / opendaylight / yangtools / yang / data / util / ParserStreamUtils.java
1 /*
2  * Copyright (c) 2016 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.yangtools.yang.data.util;
10
11 import java.util.ArrayDeque;
12 import java.util.ArrayList;
13 import java.util.Deque;
14 import java.util.List;
15 import org.opendaylight.yangtools.yang.common.QName;
16 import org.opendaylight.yangtools.yang.common.Revision;
17 import org.opendaylight.yangtools.yang.common.XMLNamespace;
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;
22
23 public final class ParserStreamUtils {
24     private ParserStreamUtils() {
25         // Hidden on purpose
26     }
27
28     /**
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}.
31      *
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)
35      */
36     public static Deque<DataSchemaNode> findSchemaNodeByNameAndNamespace(final DataSchemaNode dataSchemaNode,
37             final String childName, final XMLNamespace 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);
45                 } else {
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;
51                     }
52                 }
53             }
54         }
55         if (potentialChildNode != null) {
56             result.push(potentialChildNode);
57             return result;
58         }
59
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;
69                 }
70             }
71         }
72         return result;
73     }
74 }