2 * Copyright (c) 2015 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.Preconditions.checkArgument;
12 import com.google.common.annotations.VisibleForTesting;
13 import javax.xml.stream.XMLStreamException;
14 import javax.xml.stream.XMLStreamWriter;
15 import org.eclipse.jdt.annotation.NonNull;
16 import org.opendaylight.yangtools.yang.common.QName;
17 import org.opendaylight.yangtools.yang.common.QNameModule;
18 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
19 import org.opendaylight.yangtools.yang.data.impl.codec.TypeDefinitionAwareCodec;
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.TypedDataSchemaNode;
23 import org.opendaylight.yangtools.yang.model.api.type.IdentityrefTypeDefinition;
24 import org.opendaylight.yangtools.yang.model.api.type.InstanceIdentifierTypeDefinition;
25 import org.opendaylight.yangtools.yang.model.api.type.LeafrefTypeDefinition;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
30 * Utility class for bridging JAXP Stream and YANG Data APIs. Note that the definition of this class
31 * by no means final and subject to change as more functionality is centralized here.
33 abstract class XMLStreamWriterUtils {
34 private static final Logger LOG = LoggerFactory.getLogger(XMLStreamWriterUtils.class);
37 * Write a value into a XML stream writer. This method assumes the start and end of element is
38 * emitted by the caller.
40 * @param writer XML Stream writer
41 * @param schemaNode Schema node that describes the value
42 * @param value data value
43 * @param parent module QName owning the leaf definition
44 * @throws XMLStreamException if an encoding problem occurs
46 void writeValue(final @NonNull XMLStreamWriter writer, final @NonNull SchemaNode schemaNode,
47 final Object value, final QNameModule parent) throws XMLStreamException {
49 LOG.debug("Value of {}:{} is null, not encoding it", schemaNode.getQName().getNamespace(),
50 schemaNode.getQName().getLocalName());
54 checkArgument(schemaNode instanceof TypedDataSchemaNode,
55 "Unable to write value for node %s, only nodes of type: leaf and leaf-list can be written at this point",
56 schemaNode.getQName());
58 TypeDefinition<?> type = ((TypedDataSchemaNode) schemaNode).getType();
59 if (type instanceof LeafrefTypeDefinition) {
60 type = getBaseTypeForLeafRef(schemaNode, (LeafrefTypeDefinition) type);
63 writeValue(writer, type, value, parent);
67 * Write a value into a XML stream writer. This method assumes the start and end of element is
68 * emitted by the caller.
70 * @param writer XML Stream writer
71 * @param type data type. In case of leaf ref this should be the type of leaf being referenced
72 * @param value data value
73 * @param parent optional parameter of a module QName owning the leaf definition
74 * @throws XMLStreamException if an encoding problem occurs
76 private void writeValue(final @NonNull XMLStreamWriter writer, final @NonNull TypeDefinition<?> type,
77 final Object value, final QNameModule parent) throws XMLStreamException {
79 LOG.debug("Value of {}:{} is null, not encoding it", type.getQName().getNamespace(),
80 type.getQName().getLocalName());
84 if (type instanceof IdentityrefTypeDefinition) {
85 write(writer, (IdentityrefTypeDefinition) type, value, parent);
86 } else if (type instanceof InstanceIdentifierTypeDefinition) {
87 write(writer, (InstanceIdentifierTypeDefinition) type, value);
89 final TypeDefinitionAwareCodec<Object, ?> codec = TypeDefinitionAwareCodec.from(type);
93 text = codec.serialize(value);
94 } catch (ClassCastException e) {
95 LOG.warn("Provided node value {} did not have type {} required by mapping. Using stream instead.",
97 text = String.valueOf(value);
100 LOG.warn("Failed to find codec for {}, falling back to using stream", type);
101 text = String.valueOf(value);
103 writer.writeCharacters(text);
108 static void write(final @NonNull XMLStreamWriter writer, final @NonNull IdentityrefTypeDefinition type,
109 final @NonNull Object value, final QNameModule parent) throws XMLStreamException {
110 if (value instanceof QName) {
111 final QName qname = (QName) value;
113 //in case parent is present and same as element namespace write value without namespace
114 if (qname.getNamespace().equals(parent.getNamespace())) {
115 writer.writeCharacters(qname.getLocalName());
117 final String ns = qname.getNamespace().toString();
118 final String prefix = "x";
119 writer.writeNamespace(prefix, ns);
120 writer.writeCharacters(prefix + ':' + qname.getLocalName());
124 LOG.debug("Value of {}:{} is not a QName but {}", type.getQName().getNamespace(),
125 type.getQName().getLocalName(), value.getClass());
126 writer.writeCharacters(String.valueOf(value));
130 private void write(final @NonNull XMLStreamWriter writer, final @NonNull InstanceIdentifierTypeDefinition type,
131 final @NonNull Object value) throws XMLStreamException {
132 if (value instanceof YangInstanceIdentifier) {
133 writeInstanceIdentifier(writer, (YangInstanceIdentifier)value);
135 LOG.warn("Value of {}:{} is not an InstanceIdentifier but {}", type.getQName().getNamespace(),
136 type.getQName().getLocalName(), value.getClass());
137 writer.writeCharacters(String.valueOf(value));
141 abstract TypeDefinition<?> getBaseTypeForLeafRef(SchemaNode schemaNode, LeafrefTypeDefinition type);
143 abstract void writeInstanceIdentifier(XMLStreamWriter writer, YangInstanceIdentifier value)
144 throws XMLStreamException;