2 * Copyright (c) 2016 Intel Corporation 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.Verify.verify;
11 import static java.util.Objects.requireNonNull;
13 import com.google.common.collect.ImmutableList;
14 import java.util.Iterator;
15 import java.util.List;
16 import javax.xml.namespace.NamespaceContext;
17 import javax.xml.stream.XMLStreamException;
18 import javax.xml.stream.XMLStreamWriter;
19 import org.opendaylight.yangtools.yang.model.api.type.UnionTypeDefinition;
20 import org.slf4j.Logger;
21 import org.slf4j.LoggerFactory;
23 abstract class UnionXmlCodec<T> implements XmlCodec<T> {
24 private static final class Diverse extends UnionXmlCodec<Object> {
25 Diverse(final List<XmlCodec<?>> codecs) {
30 public Class<Object> getDataType() {
35 private static final class SingleType<T> extends UnionXmlCodec<T> {
36 private final Class<T> dataClass;
38 SingleType(final Class<T> dataClass, final List<XmlCodec<?>> codecs) {
40 this.dataClass = requireNonNull(dataClass);
44 public Class<T> getDataType() {
49 private static final Logger LOG = LoggerFactory.getLogger(UnionXmlCodec.class);
51 private final ImmutableList<XmlCodec<?>> codecs;
53 UnionXmlCodec(final List<XmlCodec<?>> codecs) {
54 this.codecs = ImmutableList.copyOf(codecs);
57 static UnionXmlCodec<?> create(final UnionTypeDefinition type, final List<XmlCodec<?>> codecs) {
58 final Iterator<XmlCodec<?>> it = codecs.iterator();
59 verify(it.hasNext(), "Union %s has no subtypes", type);
61 Class<?> dataClass = it.next().getDataType();
62 while (it.hasNext()) {
63 final Class<?> next = it.next().getDataType();
64 if (!dataClass.equals(next)) {
65 LOG.debug("Type {} has diverse data classes: {} and {}", type, dataClass, next);
66 return new Diverse(codecs);
70 LOG.debug("Type {} has single data class {}", type, dataClass);
71 return new SingleType<>(dataClass, codecs);
75 @SuppressWarnings("checkstyle:illegalCatch")
76 public final T parseValue(final NamespaceContext ctx, final String str) {
77 for (XmlCodec<?> codec : codecs) {
80 ret = codec.parseValue(ctx, str);
81 } catch (RuntimeException e) {
82 LOG.debug("Codec {} did not accept input '{}'", codec, str, e);
86 return getDataType().cast(ret);
89 throw new IllegalArgumentException("Invalid value \"" + str + "\" for union type.");
93 @SuppressWarnings("checkstyle:illegalCatch")
94 public void writeValue(final XMLStreamWriter ctx, final Object value) throws XMLStreamException {
95 for (XmlCodec<?> codec : codecs) {
96 if (!codec.getDataType().isInstance(value)) {
97 LOG.debug("Codec {} cannot accept input {}, skipping it", codec, value);
101 @SuppressWarnings("unchecked")
102 final XmlCodec<Object> objCodec = (XmlCodec<Object>) codec;
104 objCodec.writeValue(ctx, value);
106 } catch (RuntimeException e) {
107 LOG.debug("Codec {} failed to serialize {}", codec, value, e);
111 throw new IllegalArgumentException("No codec would accept value \"" + value + "\"");