500015cd37d7d8a6b253bad23630bb0c64eb4c6c
[yangtools.git] / yang / yang-data-util / src / main / java / org / opendaylight / yangtools / yang / data / util / AbstractNamespaceCodec.java
1 /*
2  * Copyright (c) 2014 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 package org.opendaylight.yangtools.yang.data.util;
9
10 import com.google.common.base.Preconditions;
11 import com.google.common.base.Splitter;
12 import java.net.URI;
13 import java.util.Iterator;
14 import javax.annotation.Nonnull;
15 import javax.annotation.Nullable;
16 import org.opendaylight.yangtools.yang.common.QName;
17 import org.opendaylight.yangtools.yang.common.QNameModule;
18
19 abstract class AbstractNamespaceCodec {
20     private static final Splitter COLON_SPLITTER = Splitter.on(':');
21
22     /**
23      * Return string prefix for a particular namespace, allocating a new one if necessary.
24      *
25      * @param namespace Namespace to map
26      * @return Allocated unique prefix, or null if the prefix cannot be mapped.
27      */
28     @Nullable protected abstract String prefixForNamespace(@Nonnull URI namespace);
29
30     /**
31      * Create a QName for a prefix and local name.
32      *
33      * @param prefix Prefix for namespace
34      * @param localName local name
35      * @return QName
36      * @throws IllegalArgumentException if the prefix cannot be resolved
37      */
38     @Nullable protected abstract QName createQName(@Nonnull String prefix, @Nonnull String localName);
39
40     private static String getIdAndPrefixAsStr(final String pathPart) {
41         int predicateStartIndex = pathPart.indexOf('[');
42         return predicateStartIndex == -1 ? pathPart : pathPart.substring(0, predicateStartIndex);
43     }
44
45     protected final StringBuilder appendQName(final StringBuilder sb, final QName qname) {
46         final String prefix = prefixForNamespace(qname.getNamespace());
47         Preconditions.checkArgument(prefix != null, "Failed to map QName {}", qname);
48         sb.append(prefix);
49         sb.append(':');
50         sb.append(qname.getLocalName());
51         return sb;
52     }
53
54     /**
55      * Append a QName, potentially taking into account last QNameModule encountered in the serialized path.
56      *
57      * @param sb target StringBuilder
58      * @param qname QName to append
59      * @param lastModule last QNameModule encountered, may be null
60      * @return target StringBuilder
61      */
62     protected StringBuilder appendQName(final StringBuilder sb, final QName qname,
63             final @Nullable QNameModule lastModule) {
64         // Covers XML and uncompressed JSON codec
65         return appendQName(sb, qname);
66     }
67
68     protected final QName parseQName(final String str) {
69         final String xPathPartTrimmed = getIdAndPrefixAsStr(str).trim();
70         final Iterator<String> it = COLON_SPLITTER.split(xPathPartTrimmed).iterator();
71
72         // Empty string
73         if (!it.hasNext()) {
74             return null;
75         }
76
77
78         final String first = it.next().trim();
79         if (first.isEmpty()) {
80             return null;
81         }
82
83         final String identifier;
84         final String prefix;
85         if (it.hasNext()) {
86             // It is "prefix:value"
87             prefix = first;
88             identifier = it.next().trim();
89         } else {
90             prefix = "";
91             identifier = first;
92         }
93         if (identifier.isEmpty()) {
94             return null;
95         }
96
97         return createQName(prefix, identifier);
98     }
99 }