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