Remove unused XMLStreamWriterUtils.writeAttribute() method
[yangtools.git] / yang / yang-data-codec-xml / src / main / java / org / opendaylight / yangtools / yang / data / codec / xml / XMLStreamWriterUtils.java
1 /*
2  * Copyright (c) 2015 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 com.google.common.base.Preconditions.checkArgument;
11
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;
28
29 /**
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.
32  */
33 abstract class XMLStreamWriterUtils {
34     private static final Logger LOG = LoggerFactory.getLogger(XMLStreamWriterUtils.class);
35
36     /**
37      * Write a value into a XML stream writer. This method assumes the start and end of element is
38      * emitted by the caller.
39      *
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
45      */
46     void writeValue(final @NonNull XMLStreamWriter writer, final @NonNull SchemaNode schemaNode,
47             final Object value, final QNameModule parent) throws XMLStreamException {
48         if (value == null) {
49             LOG.debug("Value of {}:{} is null, not encoding it", schemaNode.getQName().getNamespace(),
50                     schemaNode.getQName().getLocalName());
51             return;
52         }
53
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());
57
58         TypeDefinition<?> type = ((TypedDataSchemaNode) schemaNode).getType();
59         if (type instanceof LeafrefTypeDefinition) {
60             type = getBaseTypeForLeafRef(schemaNode, (LeafrefTypeDefinition) type);
61         }
62
63         writeValue(writer, type, value, parent);
64     }
65
66     /**
67      * Write a value into a XML stream writer. This method assumes the start and end of element is
68      * emitted by the caller.
69      *
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
75      */
76     private void writeValue(final @NonNull XMLStreamWriter writer, final @NonNull TypeDefinition<?> type,
77             final Object value, final QNameModule parent) throws XMLStreamException {
78         if (value == null) {
79             LOG.debug("Value of {}:{} is null, not encoding it", type.getQName().getNamespace(),
80                     type.getQName().getLocalName());
81             return;
82         }
83
84         if (type instanceof IdentityrefTypeDefinition) {
85             write(writer, (IdentityrefTypeDefinition) type, value, parent);
86         } else if (type instanceof InstanceIdentifierTypeDefinition) {
87             write(writer, (InstanceIdentifierTypeDefinition) type, value);
88         } else {
89             final TypeDefinitionAwareCodec<Object, ?> codec = TypeDefinitionAwareCodec.from(type);
90             String text;
91             if (codec != null) {
92                 try {
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.",
96                             value, type, e);
97                     text = String.valueOf(value);
98                 }
99             } else {
100                 LOG.warn("Failed to find codec for {}, falling back to using stream", type);
101                 text = String.valueOf(value);
102             }
103             writer.writeCharacters(text);
104         }
105     }
106
107     @VisibleForTesting
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;
112
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());
116             } else {
117                 final String ns = qname.getNamespace().toString();
118                 final String prefix = "x";
119                 writer.writeNamespace(prefix, ns);
120                 writer.writeCharacters(prefix + ':' + qname.getLocalName());
121             }
122
123         } else {
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));
127         }
128     }
129
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);
134         } else {
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));
138         }
139     }
140
141     abstract TypeDefinition<?> getBaseTypeForLeafRef(SchemaNode schemaNode, LeafrefTypeDefinition type);
142
143     abstract void writeInstanceIdentifier(XMLStreamWriter writer, YangInstanceIdentifier value)
144             throws XMLStreamException;
145 }