Fix String value parsing/serialization
[yangtools.git] / codec / yang-data-codec-xml / src / main / java / org / opendaylight / yangtools / yang / data / codec / xml / XmlStringInstanceIdentifierCodec.java
1 /*
2  * Copyright (c) 2016 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 static com.google.common.base.Preconditions.checkArgument;
11 import static java.util.Objects.requireNonNull;
12
13 import java.util.ArrayDeque;
14 import java.util.Deque;
15 import java.util.Iterator;
16 import javax.xml.namespace.NamespaceContext;
17 import javax.xml.stream.XMLStreamException;
18 import javax.xml.stream.XMLStreamWriter;
19 import org.eclipse.jdt.annotation.NonNull;
20 import org.opendaylight.yangtools.yang.common.XMLNamespace;
21 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
22 import org.opendaylight.yangtools.yang.data.util.AbstractModuleStringInstanceIdentifierCodec;
23 import org.opendaylight.yangtools.yang.data.util.DataSchemaContextTree;
24 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
25 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
26 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
27 import org.opendaylight.yangtools.yang.model.api.Module;
28 import org.opendaylight.yangtools.yang.model.util.LeafrefResolver;
29
30 final class XmlStringInstanceIdentifierCodec extends AbstractModuleStringInstanceIdentifierCodec
31         implements XmlCodec<YangInstanceIdentifier> {
32
33     private static final ThreadLocal<Deque<NamespaceContext>> TL_CONTEXT = new ThreadLocal<>();
34
35     private final @NonNull DataSchemaContextTree dataContextTree;
36     private final @NonNull XmlCodecFactory codecFactory;
37     private final @NonNull EffectiveModelContext context;
38
39     XmlStringInstanceIdentifierCodec(final EffectiveModelContext context, final XmlCodecFactory xmlCodecFactory) {
40         this.context = requireNonNull(context);
41         this.dataContextTree = DataSchemaContextTree.from(context);
42         this.codecFactory = requireNonNull(xmlCodecFactory);
43     }
44
45     @Override
46     protected Module moduleForPrefix(final String prefix) {
47         final String prefixedNS = getNamespaceContext().getNamespaceURI(prefix);
48         final Iterator<? extends Module> modules = context.findModules(XMLNamespace.of(prefixedNS)).iterator();
49         return modules.hasNext() ? modules.next() : null;
50     }
51
52     @Override
53     protected String prefixForNamespace(final XMLNamespace namespace) {
54         final Iterator<? extends Module> modules = context.findModules(namespace).iterator();
55         return modules.hasNext() ? modules.next().getName() : null;
56     }
57
58     @Override
59     protected DataSchemaContextTree getDataContextTree() {
60         return dataContextTree;
61     }
62
63     @Override
64     protected Object deserializeKeyValue(final DataSchemaNode schemaNode, final LeafrefResolver resolver,
65             final String value) {
66         requireNonNull(schemaNode, "schemaNode cannot be null");
67         checkArgument(schemaNode instanceof LeafSchemaNode, "schemaNode must be of type LeafSchemaNode");
68         final XmlCodec<?> objectXmlCodec = codecFactory.codecFor((LeafSchemaNode) schemaNode, resolver);
69         return objectXmlCodec.parseValue(getNamespaceContext(), value);
70     }
71
72     @Override
73     public Class<YangInstanceIdentifier> getDataType() {
74         return YangInstanceIdentifier.class;
75     }
76
77     @Override
78     public YangInstanceIdentifier parseValue(final NamespaceContext ctx, final String str) {
79         pushNamespaceContext(ctx);
80         try {
81             return deserialize(str);
82         } finally {
83             popNamespaceContext();
84         }
85     }
86
87     @Override
88     public void writeValue(final XMLStreamWriter ctx, final YangInstanceIdentifier value)
89             throws XMLStreamException {
90         ctx.writeCharacters(serialize(value));
91     }
92
93     private static NamespaceContext getNamespaceContext() {
94         return TL_CONTEXT.get().getFirst();
95     }
96
97     private static void popNamespaceContext() {
98         final Deque<NamespaceContext> stack = TL_CONTEXT.get();
99         stack.pop();
100         if (stack.isEmpty()) {
101             TL_CONTEXT.set(null);
102         }
103     }
104
105     private static void pushNamespaceContext(final NamespaceContext context) {
106         Deque<NamespaceContext> stack = TL_CONTEXT.get();
107         if (stack == null) {
108             stack = new ArrayDeque<>(1);
109             TL_CONTEXT.set(stack);
110         }
111         stack.push(context);
112     }
113 }