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;
13 import java.util.Map.Entry;
14 import javax.xml.stream.XMLStreamException;
15 import org.eclipse.jdt.annotation.NonNull;
16 import org.opendaylight.yangtools.yang.common.XMLNamespace;
17 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
18 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
19 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContextProvider;
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 EffectiveModelContextProvider {
26 private final @NonNull EffectiveModelContext schemaContext;
28 SchemaAwareXMLStreamWriterUtils(final EffectiveModelContext 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);
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<XMLNamespace, String> e : iiCodec.getPrefixes()) {
46 writer.writeNamespace(e.getValue(), e.getKey().toString());
49 return serializedValue;
53 public EffectiveModelContext getEffectiveModelContext() {