2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.yangtools.yang.data.util;
10 import static com.google.common.base.Preconditions.checkArgument;
12 import com.google.common.base.Splitter;
14 import java.util.Iterator;
15 import org.eclipse.jdt.annotation.NonNull;
16 import org.eclipse.jdt.annotation.Nullable;
17 import org.opendaylight.yangtools.concepts.AbstractIllegalArgumentCodec;
18 import org.opendaylight.yangtools.yang.common.QName;
19 import org.opendaylight.yangtools.yang.common.QNameModule;
21 abstract class AbstractNamespaceCodec<T> extends AbstractIllegalArgumentCodec<String, T> {
22 private static final Splitter COLON_SPLITTER = Splitter.on(':');
25 * Return string prefix for a particular namespace, allocating a new one if necessary.
27 * @param namespace Namespace to map
28 * @return Allocated unique prefix, or null if the prefix cannot be mapped.
30 protected abstract @Nullable String prefixForNamespace(@NonNull URI namespace);
33 * Create a QName for a prefix and local name.
35 * @param prefix Prefix for namespace
36 * @param localName local name
38 * @throws IllegalArgumentException if the prefix cannot be resolved
40 protected abstract @Nullable QName createQName(@NonNull String prefix, @NonNull String localName);
42 private static String getIdAndPrefixAsStr(final String pathPart) {
43 int predicateStartIndex = pathPart.indexOf('[');
44 return predicateStartIndex == -1 ? pathPart : pathPart.substring(0, predicateStartIndex);
47 protected final @NonNull StringBuilder appendQName(final StringBuilder sb, final QName qname) {
48 final String prefix = prefixForNamespace(qname.getNamespace());
49 checkArgument(prefix != null, "Failed to map QName %s to prefix", qname);
50 return sb.append(prefix).append(':').append(qname.getLocalName());
54 * Append a QName, potentially taking into account last QNameModule encountered in the serialized path.
56 * @param sb target StringBuilder
57 * @param qname QName to append
58 * @param lastModule last QNameModule encountered, may be null
59 * @return target StringBuilder
61 protected StringBuilder appendQName(final StringBuilder sb, final QName qname,
62 final @Nullable QNameModule lastModule) {
63 // Covers XML and uncompressed JSON codec
64 return appendQName(sb, qname);
67 protected final QName parseQName(final String str) {
68 final String xPathPartTrimmed = getIdAndPrefixAsStr(str).trim();
69 final Iterator<String> it = COLON_SPLITTER.split(xPathPartTrimmed).iterator();
77 final String first = it.next().trim();
78 if (first.isEmpty()) {
82 final String identifier;
85 // It is "prefix:value"
87 identifier = it.next().trim();
92 if (identifier.isEmpty()) {
96 return createQName(prefix, identifier);