Populate model/ hierarchy
[yangtools.git] / yang / yang-xpath-impl / src / main / java / org / opendaylight / yangtools / yang / xpath / impl / Utils.java
1 /*
2  * Copyright (c) 2019 Pantheon Technologies, s.r.o.  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 package org.opendaylight.yangtools.yang.xpath.impl;
9
10 import javax.xml.xpath.XPathExpressionException;
11 import org.eclipse.jdt.annotation.Nullable;
12 import org.opendaylight.yangtools.yang.common.AbstractQName;
13 import org.opendaylight.yangtools.yang.common.QName;
14 import org.opendaylight.yangtools.yang.common.QNameModule;
15 import org.opendaylight.yangtools.yang.common.QualifiedQName;
16 import org.opendaylight.yangtools.yang.common.UnqualifiedQName;
17 import org.opendaylight.yangtools.yang.common.YangNamespaceContext;
18 import org.opendaylight.yangtools.yang.xpath.api.YangLiteralExpr;
19 import org.opendaylight.yangtools.yang.xpath.api.YangQNameExpr;
20 import org.opendaylight.yangtools.yang.xpath.api.YangQNameExpr.Resolved;
21 import org.opendaylight.yangtools.yang.xpath.api.YangQNameExpr.Unresolved;
22
23 /**
24  * Various simplistic utilities shared across classes.
25  */
26 final class Utils {
27     private Utils() {
28
29     }
30
31     static Unresolved interpretAsQName(final YangLiteralExpr expr) throws XPathExpressionException {
32         final String text = expr.getLiteral();
33         final int colon = text.indexOf(':');
34
35         final AbstractQName qname;
36         try {
37             qname = colon != -1 ? QualifiedQName.of(text.substring(0, colon), text.substring(colon + 1))
38                     : UnqualifiedQName.of(text);
39         } catch (IllegalArgumentException e) {
40             throw wrapException(e, "Cannot interpret %s as a QName", expr);
41         }
42
43         return YangQNameExpr.of(qname.intern());
44     }
45
46     static YangQNameExpr interpretAsQName(final YangNamespaceContext namespaceContext, final YangLiteralExpr expr)
47             throws XPathExpressionException {
48         final String text = expr.getLiteral();
49         final int colon = text.indexOf(':');
50         try {
51             if (colon == -1) {
52                 return YangQNameExpr.of(UnqualifiedQName.of(text).intern());
53             }
54
55             return YangQNameExpr.of(namespaceContext.createQName(text.substring(0, colon), text.substring(colon + 1)));
56         } catch (IllegalArgumentException e) {
57             throw wrapException(e, "Cannot interpret %s as a QName", expr);
58         }
59     }
60
61     static Resolved interpretAsQName(final YangNamespaceContext namespaceContext,
62             final QNameModule defaultNamespace, final YangLiteralExpr expr) throws XPathExpressionException {
63         final String text = expr.getLiteral();
64         final int colon = text.indexOf(':');
65         final QName qname;
66
67         try {
68             qname = colon == -1 ? QName.create(defaultNamespace, text).intern()
69                     : namespaceContext.createQName(text.substring(0, colon), text.substring(colon + 1));
70         } catch (IllegalArgumentException e) {
71             throw wrapException(e, "Cannot interpret %s as a QName", expr);
72         }
73
74         return YangQNameExpr.of(qname);
75     }
76
77     static XPathExpressionException wrapException(final @Nullable Throwable cause, final String format,
78             final Object... args) {
79         final XPathExpressionException ret = new XPathExpressionException(String.format(format, args));
80         ret.initCause(cause);
81         return ret;
82     }
83 }