Separate out correct QName-bearing serialization 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 com.google.common.annotations.VisibleForTesting;
11 import javax.xml.stream.XMLStreamException;
12 import org.eclipse.jdt.annotation.NonNull;
13 import org.opendaylight.yangtools.yang.common.QName;
14 import org.opendaylight.yangtools.yang.common.QNameModule;
15 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
16 import org.opendaylight.yangtools.yang.data.impl.codec.TypeDefinitionAwareCodec;
17 import org.opendaylight.yangtools.yang.model.api.SchemaNode;
18 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
19 import org.opendaylight.yangtools.yang.model.api.type.IdentityrefTypeDefinition;
20 import org.opendaylight.yangtools.yang.model.api.type.InstanceIdentifierTypeDefinition;
21 import org.opendaylight.yangtools.yang.model.api.type.LeafrefTypeDefinition;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
24
25 /**
26  * Utility class for bridging JAXP Stream and YANG Data APIs. Note that the definition of this class
27  * by no means final and subject to change as more functionality is centralized here.
28  */
29 abstract class XMLStreamWriterUtils {
30     private static final Logger LOG = LoggerFactory.getLogger(XMLStreamWriterUtils.class);
31
32     /**
33      * Encode a value into a String in the context of a XML stream writer. This method assumes the start and end of
34      * element is emitted by the caller.
35      *
36      * @param writer XML Stream writer
37      * @param schemaNode Schema node that describes the value
38      * @param type Schema type definition
39      * @param value data value
40      * @param parent module QName owning the leaf definition
41      * @return String characters to be written
42      * @throws XMLStreamException if an encoding problem occurs
43      */
44     String encodeValue(final @NonNull ValueWriter writer,final @NonNull SchemaNode schemaNode,
45             final TypeDefinition<?> type, final @NonNull Object value, final QNameModule parent)
46                     throws XMLStreamException {
47         return type instanceof LeafrefTypeDefinition
48                 ? encodeValue(writer, getBaseTypeForLeafRef(schemaNode, (LeafrefTypeDefinition) type), value, parent)
49                         : encodeValue(writer, type, value, parent);
50     }
51
52     /**
53      * Write a value into a XML stream writer. This method assumes the start and end of element is
54      * emitted by the caller.
55      *
56      * @param writer XML Stream writer
57      * @param type data type. In case of leaf ref this should be the type of leaf being referenced
58      * @param value data value
59      * @param parent optional parameter of a module QName owning the leaf definition
60      * @return String characters to be written
61      * @throws XMLStreamException if an encoding problem occurs
62      */
63     private String encodeValue(final @NonNull ValueWriter writer, final @NonNull TypeDefinition<?> type,
64             final @NonNull Object value, final QNameModule parent) throws XMLStreamException {
65         if (type instanceof IdentityrefTypeDefinition) {
66             return encode(writer, (IdentityrefTypeDefinition) type, value, parent);
67         } else if (type instanceof InstanceIdentifierTypeDefinition) {
68             return encode(writer, (InstanceIdentifierTypeDefinition) type, value);
69         } else {
70             return serialize(type, value);
71         }
72     }
73
74     private static String serialize(final @NonNull TypeDefinition<?> type, final @NonNull Object value) {
75         final TypeDefinitionAwareCodec<Object, ?> codec = TypeDefinitionAwareCodec.from(type);
76         if (codec == null) {
77             LOG.warn("Failed to find codec for {}, falling back to using stream", type);
78             return value.toString();
79         }
80
81         try {
82             return codec.serialize(value);
83         } catch (ClassCastException e) {
84             LOG.warn("Provided node value {} did not have type {} required by mapping. Using stream instead.",
85                 value, type, e);
86             return value.toString();
87         }
88     }
89
90     @VisibleForTesting
91     static String encode(final @NonNull ValueWriter writer, final @NonNull QName qname, final QNameModule parent)
92             throws XMLStreamException {
93         //in case parent is present and same as element namespace write value without namespace
94         if (qname.getNamespace().equals(parent.getNamespace())) {
95             return qname.getLocalName();
96         }
97
98         final String ns = qname.getNamespace().toString();
99         final String prefix = "x";
100         writer.writeNamespace(prefix, ns);
101         return prefix + ':' + qname.getLocalName();
102     }
103
104     private static String encode(final @NonNull ValueWriter writer, final @NonNull IdentityrefTypeDefinition type,
105             final @NonNull Object value, final QNameModule parent) throws XMLStreamException {
106         if (value instanceof QName) {
107             return encode(writer, (QName) value, parent);
108         }
109
110         final QName qname = type.getQName();
111         LOG.debug("Value of {}:{} is not a QName but {}", qname.getNamespace(), qname.getLocalName(), value.getClass());
112         return value.toString();
113     }
114
115     private String encode(final @NonNull ValueWriter writer, final @NonNull InstanceIdentifierTypeDefinition type,
116             final @NonNull Object value) throws XMLStreamException {
117         if (value instanceof YangInstanceIdentifier) {
118             return encodeInstanceIdentifier(writer, (YangInstanceIdentifier)value);
119         }
120
121         final QName qname = type.getQName();
122         LOG.warn("Value of {}:{} is not an InstanceIdentifier but {}", qname.getNamespace(), qname.getLocalName(),
123             value.getClass());
124         return value.toString();
125     }
126
127     abstract @NonNull TypeDefinition<?> getBaseTypeForLeafRef(SchemaNode schemaNode,
128             @NonNull LeafrefTypeDefinition type);
129
130     abstract String encodeInstanceIdentifier(@NonNull ValueWriter writer, YangInstanceIdentifier value)
131             throws XMLStreamException;
132 }