Reimplement SchemaContextUtil.getBaseTypeForLeafRef()
[yangtools.git] / yang / yang-data-codec-xml / src / main / java / org / opendaylight / yangtools / yang / data / codec / xml / SchemaAwareXMLStreamWriterUtils.java
1 /*
2  * Copyright (c) 2017 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.yangtools.yang.data.codec.xml;
9
10 import static java.util.Objects.requireNonNull;
11
12 import java.util.Map.Entry;
13 import javax.xml.stream.XMLStreamException;
14 import org.eclipse.jdt.annotation.NonNull;
15 import org.opendaylight.yangtools.yang.common.XMLNamespace;
16 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
17 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
18 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContextProvider;
19
20 final class SchemaAwareXMLStreamWriterUtils extends XMLStreamWriterUtils implements EffectiveModelContextProvider {
21     private final @NonNull EffectiveModelContext schemaContext;
22
23     SchemaAwareXMLStreamWriterUtils(final EffectiveModelContext schemaContext) {
24         this.schemaContext = requireNonNull(schemaContext);
25     }
26
27     @Override
28     String encodeInstanceIdentifier(final ValueWriter writer, final YangInstanceIdentifier value)
29             throws XMLStreamException {
30         RandomPrefixInstanceIdentifierSerializer iiCodec = new RandomPrefixInstanceIdentifierSerializer(schemaContext,
31             writer.getNamespaceContext());
32         String serializedValue = iiCodec.serialize(value);
33
34         for (Entry<XMLNamespace, String> e : iiCodec.getPrefixes()) {
35             writer.writeNamespace(e.getValue(), e.getKey().toString());
36         }
37
38         return serializedValue;
39     }
40
41     @Override
42     public EffectiveModelContext getEffectiveModelContext() {
43         return schemaContext;
44     }
45 }