2 * Copyright (c) 2014 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.controller.xml.codec;
10 import com.google.common.annotations.Beta;
11 import com.google.common.base.Preconditions;
13 import java.util.Map.Entry;
14 import javax.annotation.Nonnull;
15 import javax.xml.stream.XMLStreamException;
16 import javax.xml.stream.XMLStreamWriter;
17 import org.opendaylight.yangtools.yang.common.QName;
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.data.impl.codec.xml.XmlCodecProvider;
21 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
22 import org.opendaylight.yangtools.yang.model.api.type.IdentityrefTypeDefinition;
23 import org.opendaylight.yangtools.yang.model.api.type.InstanceIdentifierTypeDefinition;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
28 * Utility class for bridging JAXP Stream and YANG Data APIs. Note that the definition of this class
29 * by no means final and subject to change as more functionality is centralized here.
32 public class XmlStreamUtils {
33 private static final Logger LOG = LoggerFactory.getLogger(XmlStreamUtils.class);
34 private final XmlCodecProvider codecProvider;
36 protected XmlStreamUtils(final XmlCodecProvider codecProvider) {
37 this.codecProvider = Preconditions.checkNotNull(codecProvider);
41 * Create a new instance encapsulating a particular codec provider.
43 * @param codecProvider XML codec provider
44 * @return A new instance
46 public static XmlStreamUtils create(final XmlCodecProvider codecProvider) {
47 return new XmlStreamUtils(codecProvider);
51 * Write an InstanceIdentifier into the output stream. Calling corresponding {@link javax.xml.stream.XMLStreamWriter#writeStartElement(String)}
52 * and {@link javax.xml.stream.XMLStreamWriter#writeEndElement()} is the responsibility of the caller.
54 * @param writer XML Stream writer
55 * @param id InstanceIdentifier
56 * @throws javax.xml.stream.XMLStreamException
58 public static void write(final @Nonnull XMLStreamWriter writer, final @Nonnull YangInstanceIdentifier id) throws XMLStreamException {
59 Preconditions.checkNotNull(writer, "Writer may not be null");
60 Preconditions.checkNotNull(id, "Variable should contain instance of instance identifier and can't be null");
61 LOG.debug("Writing Instance identifier with Random prefix");
62 final RandomPrefix prefixes = new RandomPrefix();
63 final String str = XmlUtils.encodeIdentifier(prefixes, id);
65 for (final Entry<URI, String> e: prefixes.getPrefixes()) {
66 writer.writeNamespace(e.getValue(), e.getKey().toString());
68 if(LOG.isDebugEnabled()) {
69 LOG.debug("Instance identifier with Random prefix is now {}", str);
71 writer.writeCharacters(str);
75 * Write a value into a XML stream writer. This method assumes the start and end of element is
76 * emitted by the caller.
78 * @param writer XML Stream writer
79 * @param type type definitions
80 * @param value object value
81 * @throws javax.xml.stream.XMLStreamException if an encoding problem occurs
83 public void writeValue(final @Nonnull XMLStreamWriter writer, final @Nonnull TypeDefinition<?> type, final Object value) throws XMLStreamException {
85 if(LOG.isDebugEnabled()){
86 LOG.debug("Value of {}:{} is null, not encoding it", type.getQName().getNamespace(), type.getQName().getLocalName());
91 final TypeDefinition<?> baseType = XmlUtils.resolveBaseTypeFrom(type);
92 if (baseType instanceof IdentityrefTypeDefinition) {
93 write(writer, (IdentityrefTypeDefinition) baseType, value);
94 } else if (baseType instanceof InstanceIdentifierTypeDefinition) {
95 write(writer, (InstanceIdentifierTypeDefinition) baseType, value);
97 final TypeDefinitionAwareCodec<Object, ?> codec = codecProvider.codecFor(baseType);
101 text = codec.serialize(value);
102 } catch (final ClassCastException e) {
103 LOG.error("Provided node value {} did not have type {} required by mapping. Using stream instead.", value, baseType, e);
104 text = String.valueOf(value);
107 LOG.error("Failed to find codec for {}, falling back to using stream", baseType);
108 text = String.valueOf(value);
110 writer.writeCharacters(text);
114 private static void write(final @Nonnull XMLStreamWriter writer, final @Nonnull IdentityrefTypeDefinition type, final @Nonnull Object value) throws XMLStreamException {
115 if (value instanceof QName) {
116 final QName qname = (QName) value;
118 writer.writeNamespace("x", qname.getNamespace().toString());
119 writer.writeCharacters("x:" + qname.getLocalName());
121 if(LOG.isDebugEnabled()) {
122 LOG.debug("Value of {}:{} is not a QName but {}", type.getQName().getNamespace(), type.getQName().getLocalName(), value.getClass());
124 writer.writeCharacters(String.valueOf(value));
128 private static void write(final @Nonnull XMLStreamWriter writer, final @Nonnull InstanceIdentifierTypeDefinition type, final @Nonnull Object value) throws XMLStreamException {
129 if (value instanceof YangInstanceIdentifier) {
130 if(LOG.isDebugEnabled()) {
131 LOG.debug("Writing InstanceIdentifier object {}", value);
133 write(writer, (YangInstanceIdentifier)value);
135 if(LOG.isDebugEnabled()) {
136 LOG.debug("Value of {}:{} is not an InstanceIdentifier but {}", type.getQName().getNamespace(), type.getQName().getLocalName(), value.getClass());
138 writer.writeCharacters(String.valueOf(value));