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
9 package org.opendaylight.yangtools.yang.data.codec.xml;
11 import static com.google.common.base.Preconditions.checkArgument;
12 import static java.util.Objects.requireNonNull;
15 import java.util.ArrayDeque;
16 import java.util.Deque;
17 import javax.annotation.Nonnull;
18 import javax.xml.namespace.NamespaceContext;
19 import javax.xml.stream.XMLStreamException;
20 import javax.xml.stream.XMLStreamWriter;
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.LeafSchemaNode;
26 import org.opendaylight.yangtools.yang.model.api.Module;
27 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
29 final class XmlStringInstanceIdentifierCodec extends AbstractModuleStringInstanceIdentifierCodec
30 implements XmlCodec<YangInstanceIdentifier> {
32 private static final ThreadLocal<Deque<NamespaceContext>> TL_CONTEXT = new ThreadLocal<>();
34 private final DataSchemaContextTree dataContextTree;
35 private final XmlCodecFactory codecFactory;
36 private final SchemaContext context;
38 XmlStringInstanceIdentifierCodec(final SchemaContext context, final XmlCodecFactory xmlCodecFactory) {
39 this.context = requireNonNull(context);
40 this.dataContextTree = DataSchemaContextTree.from(context);
41 this.codecFactory = requireNonNull(xmlCodecFactory);
45 protected Module moduleForPrefix(@Nonnull final String prefix) {
46 final String prefixedNS = getNamespaceContext().getNamespaceURI(prefix);
47 return context.findModuleByNamespaceAndRevision(URI.create(prefixedNS), null);
51 protected String prefixForNamespace(@Nonnull final URI namespace) {
52 final Module module = context.findModuleByNamespaceAndRevision(namespace, null);
53 return module == null ? null : module.getName();
58 protected DataSchemaContextTree getDataContextTree() {
59 return dataContextTree;
63 protected Object deserializeKeyValue(final DataSchemaNode schemaNode, final String value) {
64 requireNonNull(schemaNode, "schemaNode cannot be null");
65 checkArgument(schemaNode instanceof LeafSchemaNode, "schemaNode must be of type LeafSchemaNode");
66 final XmlCodec<?> objectXmlCodec = codecFactory.codecFor((LeafSchemaNode) schemaNode);
67 return objectXmlCodec.parseValue(getNamespaceContext(), value);
71 public Class<YangInstanceIdentifier> getDataType() {
72 return YangInstanceIdentifier.class;
76 public YangInstanceIdentifier parseValue(final NamespaceContext ctx, final String str) {
77 pushNamespaceContext(ctx);
79 return deserialize(str);
81 popNamespaceContext();
86 public void writeValue(final XMLStreamWriter ctx, final YangInstanceIdentifier value)
87 throws XMLStreamException {
88 ctx.writeCharacters(serialize(value));
91 private static NamespaceContext getNamespaceContext() {
92 return TL_CONTEXT.get().getFirst();
95 private static void popNamespaceContext() {
96 final Deque<NamespaceContext> stack = TL_CONTEXT.get();
98 if (stack.isEmpty()) {
103 private static void pushNamespaceContext(final NamespaceContext context) {
104 Deque<NamespaceContext> stack = TL_CONTEXT.get();
106 stack = new ArrayDeque<>(1);
107 TL_CONTEXT.set(stack);