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 javax.xml.namespace.NamespaceContext;
15 import javax.xml.stream.XMLStreamException;
16 import javax.xml.stream.XMLStreamWriter;
17 import org.eclipse.jdt.annotation.NonNull;
18 import org.opendaylight.yangtools.yang.common.XMLNamespace;
19 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
20 import org.opendaylight.yangtools.yang.data.util.AbstractModuleStringInstanceIdentifierCodec;
21 import org.opendaylight.yangtools.yang.data.util.DataSchemaContextTree;
22 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
23 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
24 import org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode;
25 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
26 import org.opendaylight.yangtools.yang.model.api.Module;
27 import org.opendaylight.yangtools.yang.model.util.LeafrefResolver;
29 final class XmlStringInstanceIdentifierCodec extends AbstractModuleStringInstanceIdentifierCodec
30 implements XmlCodec<YangInstanceIdentifier> {
32 private static final ThreadLocal<Deque<NamespaceContext>> TL_CONTEXT = new ThreadLocal<>();
34 private final @NonNull DataSchemaContextTree dataContextTree;
35 private final @NonNull XmlCodecFactory codecFactory;
36 private final @NonNull EffectiveModelContext context;
38 XmlStringInstanceIdentifierCodec(final EffectiveModelContext context, final XmlCodecFactory codecFactory) {
39 this.context = requireNonNull(context);
40 this.codecFactory = requireNonNull(codecFactory);
41 dataContextTree = DataSchemaContextTree.from(context);
45 protected Module moduleForPrefix(final String prefix) {
46 final var prefixedNS = getNamespaceContext().getNamespaceURI(prefix);
47 final var modules = context.findModules(XMLNamespace.of(prefixedNS)).iterator();
48 return modules.hasNext() ? modules.next() : null;
52 protected String prefixForNamespace(final XMLNamespace namespace) {
53 final var modules = context.findModuleStatements(namespace).iterator();
54 return modules.hasNext() ? modules.next().argument().getLocalName() : null;
58 protected DataSchemaContextTree getDataContextTree() {
59 return dataContextTree;
63 protected Object deserializeKeyValue(final DataSchemaNode schemaNode, final LeafrefResolver resolver,
65 requireNonNull(schemaNode, "schemaNode cannot be null");
66 final XmlCodec<?> objectXmlCodec;
67 if (schemaNode instanceof LeafSchemaNode leafSchemaNode) {
68 objectXmlCodec = codecFactory.codecFor(leafSchemaNode, resolver);
69 } else if (schemaNode instanceof LeafListSchemaNode leafListSchemaNode) {
70 objectXmlCodec = codecFactory.codecFor(leafListSchemaNode, resolver);
72 throw new IllegalArgumentException("schemaNode " + schemaNode
73 + " must be of type LeafSchemaNode or LeafListSchemaNode");
75 return objectXmlCodec.parseValue(getNamespaceContext(), value);
79 public Class<YangInstanceIdentifier> getDataType() {
80 return YangInstanceIdentifier.class;
84 public YangInstanceIdentifier parseValue(final NamespaceContext ctx, final String str) {
85 pushNamespaceContext(ctx);
87 // FIXME: YANGTOOLS-1523: do not trim()
88 return deserialize(str.trim());
90 popNamespaceContext();
95 public void writeValue(final XMLStreamWriter ctx, final YangInstanceIdentifier value) throws XMLStreamException {
96 ctx.writeCharacters(serialize(value));
99 private static NamespaceContext getNamespaceContext() {
100 return TL_CONTEXT.get().getFirst();
103 private static void popNamespaceContext() {
104 final var stack = TL_CONTEXT.get();
106 if (stack.isEmpty()) {
107 TL_CONTEXT.set(null);
111 private static void pushNamespaceContext(final NamespaceContext context) {
112 var stack = TL_CONTEXT.get();
114 stack = new ArrayDeque<>(1);
115 TL_CONTEXT.set(stack);