2 * Copyright (c) 2016 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.yangtools.yang.data.codec.xml;
10 import static java.util.Objects.requireNonNull;
12 import java.util.ArrayDeque;
13 import java.util.Deque;
14 import java.util.Iterator;
15 import javax.xml.namespace.NamespaceContext;
16 import javax.xml.stream.XMLStreamException;
17 import javax.xml.stream.XMLStreamWriter;
18 import org.eclipse.jdt.annotation.NonNull;
19 import org.opendaylight.yangtools.yang.common.XMLNamespace;
20 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
21 import org.opendaylight.yangtools.yang.data.util.AbstractModuleStringInstanceIdentifierCodec;
22 import org.opendaylight.yangtools.yang.data.util.DataSchemaContextTree;
23 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
24 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
25 import org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode;
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;
30 final class XmlStringInstanceIdentifierCodec extends AbstractModuleStringInstanceIdentifierCodec
31 implements XmlCodec<YangInstanceIdentifier> {
33 private static final ThreadLocal<Deque<NamespaceContext>> TL_CONTEXT = new ThreadLocal<>();
35 private final @NonNull DataSchemaContextTree dataContextTree;
36 private final @NonNull XmlCodecFactory codecFactory;
37 private final @NonNull EffectiveModelContext context;
39 XmlStringInstanceIdentifierCodec(final EffectiveModelContext context, final XmlCodecFactory xmlCodecFactory) {
40 this.context = requireNonNull(context);
41 this.dataContextTree = DataSchemaContextTree.from(context);
42 this.codecFactory = requireNonNull(xmlCodecFactory);
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;
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;
59 protected DataSchemaContextTree getDataContextTree() {
60 return dataContextTree;
64 protected Object deserializeKeyValue(final DataSchemaNode schemaNode, final LeafrefResolver resolver,
66 requireNonNull(schemaNode, "schemaNode cannot be null");
67 final XmlCodec<?> objectXmlCodec;
68 if (schemaNode instanceof LeafSchemaNode leafSchemaNode) {
69 objectXmlCodec = codecFactory.codecFor(leafSchemaNode, resolver);
70 } else if (schemaNode instanceof LeafListSchemaNode leafListSchemaNode) {
71 objectXmlCodec = codecFactory.codecFor(leafListSchemaNode, resolver);
73 throw new IllegalArgumentException("schemaNode " + schemaNode
74 + " must be of type LeafSchemaNode or LeafListSchemaNode");
76 return objectXmlCodec.parseValue(getNamespaceContext(), value);
80 public Class<YangInstanceIdentifier> getDataType() {
81 return YangInstanceIdentifier.class;
85 public YangInstanceIdentifier parseValue(final NamespaceContext ctx, final String str) {
86 pushNamespaceContext(ctx);
88 return deserialize(str);
90 popNamespaceContext();
95 public void writeValue(final XMLStreamWriter ctx, final YangInstanceIdentifier value)
96 throws XMLStreamException {
97 ctx.writeCharacters(serialize(value));
100 private static NamespaceContext getNamespaceContext() {
101 return TL_CONTEXT.get().getFirst();
104 private static void popNamespaceContext() {
105 final Deque<NamespaceContext> stack = TL_CONTEXT.get();
107 if (stack.isEmpty()) {
108 TL_CONTEXT.set(null);
112 private static void pushNamespaceContext(final NamespaceContext context) {
113 Deque<NamespaceContext> stack = TL_CONTEXT.get();
115 stack = new ArrayDeque<>(1);
116 TL_CONTEXT.set(stack);