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 javax.annotation.Nonnull;
14 import javax.xml.namespace.NamespaceContext;
15 import javax.xml.stream.XMLStreamException;
16 import javax.xml.stream.XMLStreamWriter;
17 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
18 import org.opendaylight.yangtools.yang.data.util.AbstractModuleStringInstanceIdentifierCodec;
19 import org.opendaylight.yangtools.yang.data.util.DataSchemaContextTree;
20 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
21 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
22 import org.opendaylight.yangtools.yang.model.api.Module;
23 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
25 final class XmlStringInstanceIdentifierCodec extends AbstractModuleStringInstanceIdentifierCodec
26 implements XmlCodec<YangInstanceIdentifier> {
28 private final DataSchemaContextTree dataContextTree;
29 private final XmlCodecFactory codecFactory;
30 private final SchemaContext context;
31 private final NamespaceContext namespaceContext;
33 XmlStringInstanceIdentifierCodec(final SchemaContext context, final XmlCodecFactory xmlCodecFactory,
34 final NamespaceContext namespaceContext) {
35 this.context = Preconditions.checkNotNull(context);
36 this.dataContextTree = DataSchemaContextTree.from(context);
37 this.codecFactory = Preconditions.checkNotNull(xmlCodecFactory);
38 this.namespaceContext = Preconditions.checkNotNull(namespaceContext);
42 protected Module moduleForPrefix(@Nonnull final String prefix) {
43 final String prefixedNS = namespaceContext.getNamespaceURI(prefix);
44 return context.findModuleByNamespaceAndRevision(URI.create(prefixedNS), null);
48 protected String prefixForNamespace(@Nonnull final URI namespace) {
49 final Module module = context.findModuleByNamespaceAndRevision(namespace, null);
50 return module == null ? null : module.getName();
55 protected DataSchemaContextTree getDataContextTree() {
56 return dataContextTree;
60 protected Object deserializeKeyValue(final DataSchemaNode schemaNode, final String value) {
61 Preconditions.checkNotNull(schemaNode, "schemaNode cannot be null");
62 Preconditions.checkArgument(schemaNode instanceof LeafSchemaNode, "schemaNode must be of type LeafSchemaNode");
63 final XmlCodec<?> objectXmlCodec = codecFactory.codecFor(schemaNode, namespaceContext);
64 return objectXmlCodec.deserialize(value);
68 * Serialize YangInstanceIdentifier with specified XMLStreamWriter.
70 * @param writer XMLStreamWriter
71 * @param value YangInstanceIdentifier
74 public void serializeToWriter(final XMLStreamWriter writer, final YangInstanceIdentifier value)
75 throws XMLStreamException {
76 writer.writeCharacters(serialize(value));