Cleanup use of Guava library
[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 static com.google.common.base.Preconditions.checkArgument;
11
12 import com.google.common.base.Splitter;
13 import java.net.URI;
14 import java.util.Iterator;
15 import javax.annotation.Nonnull;
16 import javax.annotation.Nullable;
17 import org.opendaylight.yangtools.yang.common.QName;
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         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     protected final QName parseQName(final String str) {
55         final String xPathPartTrimmed = getIdAndPrefixAsStr(str).trim();
56         final Iterator<String> it = COLON_SPLITTER.split(xPathPartTrimmed).iterator();
57
58         // Empty string
59         if (!it.hasNext()) {
60             return null;
61         }
62
63
64         final String first = it.next().trim();
65         if (first.isEmpty()) {
66             return null;
67         }
68
69         final String identifier;
70         final String prefix;
71         if (it.hasNext()) {
72             // It is "prefix:value"
73             prefix = first;
74             identifier = it.next().trim();
75         } else {
76             prefix = "";
77             identifier = first;
78         }
79         if (identifier.isEmpty()) {
80             return null;
81         }
82
83         return createQName(prefix, identifier);
84     }
85 }