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.impl.codec.xml;
11 import javax.annotation.Nonnull;
12 import org.opendaylight.yangtools.yang.common.QName;
13 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
14 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
15 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeWithValue;
16 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
17 import org.opendaylight.yangtools.yang.data.impl.codec.TypeDefinitionAwareCodec;
18 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
21 * Common XML-related utility methods, which are not specific to a particular
24 public final class XmlUtils {
25 public static final XmlCodecProvider DEFAULT_XML_CODEC_PROVIDER = new XmlCodecProvider() {
27 public TypeDefinitionAwareCodec<Object, ? extends TypeDefinition<?>> codecFor(final TypeDefinition<?> baseType) {
28 return TypeDefinitionAwareCodec.from(baseType);
35 public static TypeDefinition<?> resolveBaseTypeFrom(final @Nonnull TypeDefinition<?> type) {
36 TypeDefinition<?> superType = type;
37 while (superType.getBaseType() != null) {
38 superType = superType.getBaseType();
45 * @deprecated Use {@link RandomPrefixInstanceIdentifierSerializer} instead.
48 static String encodeIdentifier(final RandomPrefix prefixes, final YangInstanceIdentifier id) {
49 StringBuilder textContent = new StringBuilder();
50 for (PathArgument pathArgument : id.getPathArguments()) {
51 textContent.append('/');
53 final QName nt = pathArgument.getNodeType();
54 textContent.append(prefixes.encodePrefix(nt.getNamespace()));
55 textContent.append(':');
56 textContent.append(nt.getLocalName());
58 if (pathArgument instanceof NodeIdentifierWithPredicates) {
59 Map<QName, Object> predicates = ((NodeIdentifierWithPredicates) pathArgument).getKeyValues();
61 for (Map.Entry<QName, Object> entry : predicates.entrySet()) {
62 final QName key = entry.getKey();
63 textContent.append('[');
64 textContent.append(prefixes.encodePrefix(key.getNamespace()));
65 textContent.append(':');
66 textContent.append(key.getLocalName());
67 textContent.append("='");
68 textContent.append(String.valueOf(entry.getValue()));
69 textContent.append("']");
71 } else if (pathArgument instanceof NodeWithValue) {
72 textContent.append("[.='");
73 textContent.append(((NodeWithValue) pathArgument).getValue());
74 textContent.append("']");
78 return textContent.toString();