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 com.google.common.base.Preconditions.checkArgument;
11 import static java.util.Objects.requireNonNull;
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;
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 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);
73 public Class<YangInstanceIdentifier> getDataType() {
74 return YangInstanceIdentifier.class;
78 public YangInstanceIdentifier parseValue(final NamespaceContext ctx, final String str) {
79 pushNamespaceContext(ctx);
81 return deserialize(str);
83 popNamespaceContext();
88 public void writeValue(final XMLStreamWriter ctx, final YangInstanceIdentifier value)
89 throws XMLStreamException {
90 ctx.writeCharacters(serialize(value));
93 private static NamespaceContext getNamespaceContext() {
94 return TL_CONTEXT.get().getFirst();
97 private static void popNamespaceContext() {
98 final Deque<NamespaceContext> stack = TL_CONTEXT.get();
100 if (stack.isEmpty()) {
101 TL_CONTEXT.set(null);
105 private static void pushNamespaceContext(final NamespaceContext context) {
106 Deque<NamespaceContext> stack = TL_CONTEXT.get();
108 stack = new ArrayDeque<>(1);
109 TL_CONTEXT.set(stack);