05b0ab3ced1e2173323b07f151b48fc7571cb185
[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 IdentityrefTypeDefinition type,
92             final @NonNull Object value, final QNameModule parent) throws XMLStreamException {
93         if (value instanceof QName) {
94             final QName qname = (QName) value;
95
96             //in case parent is present and same as element namespace write value without namespace
97             if (qname.getNamespace().equals(parent.getNamespace())) {
98                 return qname.getLocalName();
99             }
100
101             final String ns = qname.getNamespace().toString();
102             final String prefix = "x";
103             writer.writeNamespace(prefix, ns);
104             return prefix + ':' + qname.getLocalName();
105         }
106
107         final QName qname = type.getQName();
108         LOG.debug("Value of {}:{} is not a QName but {}", qname.getNamespace(), qname.getLocalName(), value.getClass());
109         return value.toString();
110     }
111
112     private String encode(final @NonNull ValueWriter writer, final @NonNull InstanceIdentifierTypeDefinition type,
113             final @NonNull Object value) throws XMLStreamException {
114         if (value instanceof YangInstanceIdentifier) {
115             return encodeInstanceIdentifier(writer, (YangInstanceIdentifier)value);
116         }
117
118         final QName qname = type.getQName();
119         LOG.warn("Value of {}:{} is not an InstanceIdentifier but {}", qname.getNamespace(), qname.getLocalName(),
120             value.getClass());
121         return value.toString();
122     }
123
124     abstract @NonNull TypeDefinition<?> getBaseTypeForLeafRef(SchemaNode schemaNode,
125             @NonNull LeafrefTypeDefinition type);
126
127     abstract String encodeInstanceIdentifier(@NonNull ValueWriter writer, YangInstanceIdentifier value)
128             throws XMLStreamException;
129 }