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 com.google.common.base.Preconditions;
11 import com.google.common.base.Verify;
13 import java.util.Map.Entry;
14 import javax.xml.stream.XMLStreamException;
15 import javax.xml.stream.XMLStreamWriter;
16 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
17 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
18 import org.opendaylight.yangtools.yang.model.api.SchemaNode;
19 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
20 import org.opendaylight.yangtools.yang.model.api.type.LeafrefTypeDefinition;
21 import org.opendaylight.yangtools.yang.model.util.SchemaContextUtil;
23 final class SchemaAwareXMLStreamWriterUtils extends XMLStreamWriterUtils {
24 private final SchemaContext schemaContext;
26 SchemaAwareXMLStreamWriterUtils(final SchemaContext schemaContext) {
27 this.schemaContext = Preconditions.checkNotNull(schemaContext);
31 TypeDefinition<?> getBaseTypeForLeafRef(final SchemaNode schemaNode, final LeafrefTypeDefinition type) {
32 final TypeDefinition<?> ret = SchemaContextUtil.getBaseTypeForLeafRef(type, schemaContext, schemaNode);
33 return Verify.verifyNotNull(ret, "Unable to find base type for leafref node '%s'.", schemaNode.getPath());
37 void writeInstanceIdentifier(final XMLStreamWriter writer, final YangInstanceIdentifier value)
38 throws XMLStreamException {
39 RandomPrefixInstanceIdentifierSerializer iiCodec = new RandomPrefixInstanceIdentifierSerializer(schemaContext);
40 String serializedValue = iiCodec.serialize(value);
42 for (Entry<URI, String> e : iiCodec.getPrefixes()) {
43 writer.writeNamespace(e.getValue(), e.getKey().toString());
46 writer.writeCharacters(serializedValue);