Fix format string
[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 %s to prefix", qname);
48         sb.append(prefix).append(':').append(qname.getLocalName());
49         return sb;
50     }
51
52     /**
53      * Append a QName, potentially taking into account last QNameModule encountered in the serialized path.
54      *
55      * @param sb target StringBuilder
56      * @param qname QName to append
57      * @param lastModule last QNameModule encountered, may be null
58      * @return target StringBuilder
59      */
60     protected StringBuilder appendQName(final StringBuilder sb, final QName qname,
61             final @Nullable QNameModule lastModule) {
62         // Covers XML and uncompressed JSON codec
63         return appendQName(sb, qname);
64     }
65
66     protected final QName parseQName(final String str) {
67         final String xPathPartTrimmed = getIdAndPrefixAsStr(str).trim();
68         final Iterator<String> it = COLON_SPLITTER.split(xPathPartTrimmed).iterator();
69
70         // Empty string
71         if (!it.hasNext()) {
72             return null;
73         }
74
75
76         final String first = it.next().trim();
77         if (first.isEmpty()) {
78             return null;
79         }
80
81         final String identifier;
82         final String prefix;
83         if (it.hasNext()) {
84             // It is "prefix:value"
85             prefix = first;
86             identifier = it.next().trim();
87         } else {
88             prefix = "";
89             identifier = first;
90         }
91         if (identifier.isEmpty()) {
92             return null;
93         }
94
95         return createQName(prefix, identifier);
96     }
97 }