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;
12 import org.opendaylight.yangtools.yang.common.QName;
13 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier;
14 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.NodeIdentifierWithPredicates;
15 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.NodeWithValue;
16 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.PathArgument;
17 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
20 * Common XML-related utility methods, which are not specific to a particular
23 final class XmlUtils {
29 public static TypeDefinition<?> resolveBaseTypeFrom(final TypeDefinition<?> type) {
30 TypeDefinition<?> superType = type;
31 while (superType.getBaseType() != null) {
32 superType = superType.getBaseType();
37 static String encodeIdentifier(final RandomPrefix prefixes, final InstanceIdentifier id) {
38 StringBuilder textContent = new StringBuilder();
39 for (PathArgument pathArgument : id.getPathArguments()) {
40 textContent.append('/');
41 textContent.append(prefixes.encodeQName(pathArgument.getNodeType()));
42 if (pathArgument instanceof NodeIdentifierWithPredicates) {
43 Map<QName, Object> predicates = ((NodeIdentifierWithPredicates) pathArgument).getKeyValues();
45 for (QName keyValue : predicates.keySet()) {
46 String predicateValue = String.valueOf(predicates.get(keyValue));
47 textContent.append('[');
48 textContent.append(prefixes.encodeQName(keyValue));
49 textContent.append("='");
50 textContent.append(predicateValue);
51 textContent.append("']");
53 } else if (pathArgument instanceof NodeWithValue) {
54 textContent.append("[.='");
55 textContent.append(((NodeWithValue) pathArgument).getValue());
56 textContent.append("']");
60 return textContent.toString();