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 com.google.common.base.Preconditions;
13 import java.util.ArrayDeque;
14 import java.util.Deque;
15 import javax.annotation.Nonnull;
16 import javax.xml.namespace.NamespaceContext;
17 import javax.xml.stream.XMLStreamException;
18 import javax.xml.stream.XMLStreamWriter;
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.LeafSchemaNode;
24 import org.opendaylight.yangtools.yang.model.api.Module;
25 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
27 final class XmlStringInstanceIdentifierCodec extends AbstractModuleStringInstanceIdentifierCodec
28 implements XmlCodec<YangInstanceIdentifier> {
30 private static final ThreadLocal<Deque<NamespaceContext>> TL_CONTEXT = new ThreadLocal<>();
32 private final DataSchemaContextTree dataContextTree;
33 private final XmlCodecFactory codecFactory;
34 private final SchemaContext context;
36 XmlStringInstanceIdentifierCodec(final SchemaContext context, final XmlCodecFactory xmlCodecFactory) {
37 this.context = Preconditions.checkNotNull(context);
38 this.dataContextTree = DataSchemaContextTree.from(context);
39 this.codecFactory = Preconditions.checkNotNull(xmlCodecFactory);
43 protected Module moduleForPrefix(@Nonnull final String prefix) {
44 final String prefixedNS = getNamespaceContext().getNamespaceURI(prefix);
45 return context.findModuleByNamespaceAndRevision(URI.create(prefixedNS), null);
49 protected String prefixForNamespace(@Nonnull final URI namespace) {
50 final Module module = context.findModuleByNamespaceAndRevision(namespace, null);
51 return module == null ? null : module.getName();
56 protected DataSchemaContextTree getDataContextTree() {
57 return dataContextTree;
61 protected Object deserializeKeyValue(final DataSchemaNode schemaNode, final String value) {
62 Preconditions.checkNotNull(schemaNode, "schemaNode cannot be null");
63 Preconditions.checkArgument(schemaNode instanceof LeafSchemaNode, "schemaNode must be of type LeafSchemaNode");
64 final XmlCodec<?> objectXmlCodec = codecFactory.codecFor((LeafSchemaNode) schemaNode);
65 return objectXmlCodec.parseValue(getNamespaceContext(), value);
69 public Class<YangInstanceIdentifier> getDataType() {
70 return YangInstanceIdentifier.class;
74 public YangInstanceIdentifier parseValue(final NamespaceContext ctx, final String str) {
75 pushNamespaceContext(ctx);
77 return deserialize(str);
79 popNamespaceContext();
84 public void writeValue(final XMLStreamWriter ctx, final YangInstanceIdentifier value)
85 throws XMLStreamException {
86 ctx.writeCharacters(serialize(value));
89 private static NamespaceContext getNamespaceContext() {
90 return TL_CONTEXT.get().getFirst();
93 private static void popNamespaceContext() {
94 final Deque<NamespaceContext> stack = TL_CONTEXT.get();
96 if (stack.isEmpty()) {
101 private static void pushNamespaceContext(final NamespaceContext context) {
102 Deque<NamespaceContext> stack = TL_CONTEXT.get();
104 stack = new ArrayDeque<>(1);
105 TL_CONTEXT.set(stack);