Improve error message in UnionXmlCodec
[yangtools.git] / yang / yang-data-codec-xml / src / main / java / org / opendaylight / yangtools / yang / data / codec / xml / UnionXmlCodec.java
1 /*
2  * Copyright (c) 2016 Intel Corporation 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.Verify.verify;
11 import static java.util.Objects.requireNonNull;
12
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;
22
23 abstract class UnionXmlCodec<T> implements XmlCodec<T> {
24     private static final class Diverse extends UnionXmlCodec<Object> {
25         Diverse(final List<XmlCodec<?>> codecs) {
26             super(codecs);
27         }
28
29         @Override
30         public Class<Object> getDataType() {
31             return Object.class;
32         }
33     }
34
35     private static final class SingleType<T> extends UnionXmlCodec<T> {
36         private final Class<T> dataClass;
37
38         SingleType(final Class<T> dataClass, final List<XmlCodec<?>> codecs) {
39             super(codecs);
40             this.dataClass = requireNonNull(dataClass);
41         }
42
43         @Override
44         public Class<T> getDataType() {
45             return dataClass;
46         }
47     }
48
49     private static final Logger LOG = LoggerFactory.getLogger(UnionXmlCodec.class);
50
51     private final ImmutableList<XmlCodec<?>> codecs;
52
53     UnionXmlCodec(final List<XmlCodec<?>> codecs) {
54         this.codecs = ImmutableList.copyOf(codecs);
55     }
56
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);
60
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);
67             }
68         }
69
70         LOG.debug("Type {} has single data class {}", type, dataClass);
71         return new SingleType<>(dataClass, codecs);
72     }
73
74     @Override
75     @SuppressWarnings("checkstyle:illegalCatch")
76     public final T parseValue(final NamespaceContext ctx, final String str) {
77         for (XmlCodec<?> codec : codecs) {
78             final Object ret;
79             try {
80                 ret = codec.parseValue(ctx, str);
81             } catch (RuntimeException e) {
82                 LOG.debug("Codec {} did not accept input '{}'", codec, str, e);
83                 continue;
84             }
85
86             return getDataType().cast(ret);
87         }
88
89         throw new IllegalArgumentException("Invalid value \"" + str + "\" for union type.");
90     }
91
92     @Override
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);
98                 continue;
99             }
100
101             @SuppressWarnings("unchecked")
102             final XmlCodec<Object> objCodec = (XmlCodec<Object>) codec;
103             try {
104                 objCodec.writeValue(ctx, value);
105                 return;
106             } catch (RuntimeException e) {
107                 LOG.debug("Codec {} failed to serialize {}", codec, value, e);
108             }
109         }
110
111         throw new IllegalArgumentException("No codec would accept value \"" + value + "\"");
112     }
113 }