2 * Copyright (c) 2017 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.codec.xml;
10 import static com.google.common.base.Verify.verifyNotNull;
11 import static java.util.Objects.requireNonNull;
14 import java.util.Map.Entry;
15 import javax.xml.stream.XMLStreamException;
16 import org.eclipse.jdt.annotation.NonNull;
17 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
18 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
19 import org.opendaylight.yangtools.yang.model.api.SchemaContextProvider;
20 import org.opendaylight.yangtools.yang.model.api.SchemaNode;
21 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
22 import org.opendaylight.yangtools.yang.model.api.type.LeafrefTypeDefinition;
23 import org.opendaylight.yangtools.yang.model.util.SchemaContextUtil;
25 final class SchemaAwareXMLStreamWriterUtils extends XMLStreamWriterUtils implements SchemaContextProvider {
26 private final @NonNull SchemaContext schemaContext;
28 SchemaAwareXMLStreamWriterUtils(final SchemaContext schemaContext) {
29 this.schemaContext = requireNonNull(schemaContext);
33 TypeDefinition<?> getBaseTypeForLeafRef(final SchemaNode schemaNode, final LeafrefTypeDefinition type) {
34 final TypeDefinition<?> ret = SchemaContextUtil.getBaseTypeForLeafRef(type, schemaContext, schemaNode);
35 return verifyNotNull(ret, "Unable to find base type for leafref node '%s'.", schemaNode.getPath());
39 String encodeInstanceIdentifier(final ValueWriter writer, final YangInstanceIdentifier value)
40 throws XMLStreamException {
41 RandomPrefixInstanceIdentifierSerializer iiCodec = new RandomPrefixInstanceIdentifierSerializer(schemaContext,
42 writer.getNamespaceContext());
43 String serializedValue = iiCodec.serialize(value);
45 for (Entry<URI, String> e : iiCodec.getPrefixes()) {
46 writer.writeNamespace(e.getValue(), e.getKey().toString());
49 return serializedValue;
53 public SchemaContext getSchemaContext() {