Update yang-xpath-api design
[yangtools.git] / yang / yang-xpath-impl / src / main / java / org / opendaylight / yangtools / yang / xpath / impl / LiteralExprUtils.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.opendaylight.yangtools.yang.common.QName;
12 import org.opendaylight.yangtools.yang.common.YangNamespaceContext;
13 import org.opendaylight.yangtools.yang.xpath.api.YangLiteralExpr;
14 import org.opendaylight.yangtools.yang.xpath.api.YangLocationPath;
15 import org.opendaylight.yangtools.yang.xpath.api.YangQNameExpr;
16
17 /**
18  * Utilities for interpreting {@link YangLiteralExpr}s as {@link YangQNameExpr}s and {@link YangLocationPath}s.
19  */
20 final class LiteralExprUtils {
21     private LiteralExprUtils() {
22
23     }
24
25     static YangQNameExpr interpretAsQName(final YangNamespaceContext namespaceContext, final YangLiteralExpr expr)
26             throws XPathExpressionException {
27         final String text = expr.getLiteral();
28         final int colon = text.indexOf(':');
29         final QName qname;
30         if (colon != -1) {
31             try {
32                 qname = namespaceContext.createQName(text.substring(0, colon), text.substring(colon + 1));
33             } catch (IllegalArgumentException e) {
34                 throw new XPathExpressionException(e);
35             }
36         } else {
37             try {
38                 // Deal with UnprefixedNames by interpreting them in implicit namespace
39                 qname = namespaceContext.createQName(expr.getLiteral());
40             } catch (IllegalArgumentException | IllegalStateException e) {
41                 throw new XPathExpressionException(e);
42             }
43         }
44
45         return YangQNameExpr.of(qname);
46     }
47 }