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 java.util.Objects.requireNonNull;
13 import com.google.common.annotations.Beta;
14 import java.util.List;
15 import org.opendaylight.yangtools.rfc8528.data.api.MountPointContext;
16 import org.opendaylight.yangtools.rfc8528.data.util.EmptyMountPointContext;
17 import org.opendaylight.yangtools.yang.common.QNameModule;
18 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
19 import org.opendaylight.yangtools.yang.data.impl.codec.AbstractIntegerStringCodec;
20 import org.opendaylight.yangtools.yang.data.impl.codec.BinaryStringCodec;
21 import org.opendaylight.yangtools.yang.data.impl.codec.BitsStringCodec;
22 import org.opendaylight.yangtools.yang.data.impl.codec.BooleanStringCodec;
23 import org.opendaylight.yangtools.yang.data.impl.codec.DecimalStringCodec;
24 import org.opendaylight.yangtools.yang.data.impl.codec.EnumStringCodec;
25 import org.opendaylight.yangtools.yang.data.impl.codec.StringStringCodec;
26 import org.opendaylight.yangtools.yang.data.util.codec.AbstractCodecFactory;
27 import org.opendaylight.yangtools.yang.data.util.codec.SharedCodecCache;
28 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
29 import org.opendaylight.yangtools.yang.model.api.type.BinaryTypeDefinition;
30 import org.opendaylight.yangtools.yang.model.api.type.BitsTypeDefinition;
31 import org.opendaylight.yangtools.yang.model.api.type.BooleanTypeDefinition;
32 import org.opendaylight.yangtools.yang.model.api.type.DecimalTypeDefinition;
33 import org.opendaylight.yangtools.yang.model.api.type.EmptyTypeDefinition;
34 import org.opendaylight.yangtools.yang.model.api.type.EnumTypeDefinition;
35 import org.opendaylight.yangtools.yang.model.api.type.IdentityrefTypeDefinition;
36 import org.opendaylight.yangtools.yang.model.api.type.InstanceIdentifierTypeDefinition;
37 import org.opendaylight.yangtools.yang.model.api.type.Int16TypeDefinition;
38 import org.opendaylight.yangtools.yang.model.api.type.Int32TypeDefinition;
39 import org.opendaylight.yangtools.yang.model.api.type.Int64TypeDefinition;
40 import org.opendaylight.yangtools.yang.model.api.type.Int8TypeDefinition;
41 import org.opendaylight.yangtools.yang.model.api.type.StringTypeDefinition;
42 import org.opendaylight.yangtools.yang.model.api.type.Uint16TypeDefinition;
43 import org.opendaylight.yangtools.yang.model.api.type.Uint32TypeDefinition;
44 import org.opendaylight.yangtools.yang.model.api.type.Uint64TypeDefinition;
45 import org.opendaylight.yangtools.yang.model.api.type.Uint8TypeDefinition;
46 import org.opendaylight.yangtools.yang.model.api.type.UnionTypeDefinition;
47 import org.opendaylight.yangtools.yang.model.api.type.UnknownTypeDefinition;
50 * A thread-safe factory for instantiating {@link XmlCodec}s.
53 public final class XmlCodecFactory extends AbstractCodecFactory<XmlCodec<?>> {
54 private final MountPointContext mountCtx;
56 private XmlCodecFactory(final MountPointContext mountCtx) {
57 super(mountCtx.getEffectiveModelContext(), new SharedCodecCache<>());
58 this.mountCtx = requireNonNull(mountCtx);
61 MountPointContext mountPointContext() {
66 * Instantiate a new codec factory attached to a particular context.
68 * @param context MountPointContext to which the factory should be bound
69 * @return A codec factory instance.
71 public static XmlCodecFactory create(final MountPointContext context) {
72 return new XmlCodecFactory(context);
76 * Instantiate a new codec factory attached to a particular context.
78 * @param context SchemaContext to which the factory should be bound
79 * @return A codec factory instance.
81 public static XmlCodecFactory create(final EffectiveModelContext context) {
82 return create(new EmptyMountPointContext(context));
86 protected XmlCodec<?> binaryCodec(final BinaryTypeDefinition type) {
87 return new QuotedXmlCodec<>(BinaryStringCodec.from(type));
91 protected XmlCodec<?> booleanCodec(final BooleanTypeDefinition type) {
92 return new BooleanXmlCodec(BooleanStringCodec.from(type));
96 protected XmlCodec<?> bitsCodec(final BitsTypeDefinition type) {
97 return new QuotedXmlCodec<>(BitsStringCodec.from(type));
101 protected XmlCodec<?> emptyCodec(final EmptyTypeDefinition type) {
102 return EmptyXmlCodec.INSTANCE;
106 protected XmlCodec<?> enumCodec(final EnumTypeDefinition type) {
107 return new QuotedXmlCodec<>(EnumStringCodec.from(type));
111 protected XmlCodec<?> identityRefCodec(final IdentityrefTypeDefinition type, final QNameModule module) {
112 return new IdentityrefXmlCodec(getEffectiveModelContext(), module);
116 protected XmlCodec<YangInstanceIdentifier> instanceIdentifierCodec(final InstanceIdentifierTypeDefinition type) {
117 return new XmlStringInstanceIdentifierCodec(getEffectiveModelContext(), this);
121 protected XmlCodec<?> int8Codec(final Int8TypeDefinition type) {
122 return new NumberXmlCodec<>(AbstractIntegerStringCodec.from(type));
126 protected XmlCodec<?> int16Codec(final Int16TypeDefinition type) {
127 return new NumberXmlCodec<>(AbstractIntegerStringCodec.from(type));
131 protected XmlCodec<?> int32Codec(final Int32TypeDefinition type) {
132 return new NumberXmlCodec<>(AbstractIntegerStringCodec.from(type));
136 protected XmlCodec<?> int64Codec(final Int64TypeDefinition type) {
137 return new NumberXmlCodec<>(AbstractIntegerStringCodec.from(type));
141 protected XmlCodec<?> decimalCodec(final DecimalTypeDefinition type) {
142 return new NumberXmlCodec<>(DecimalStringCodec.from(type));
146 protected XmlCodec<?> stringCodec(final StringTypeDefinition type) {
147 return new QuotedXmlCodec<>(StringStringCodec.from(type));
151 protected XmlCodec<?> uint8Codec(final Uint8TypeDefinition type) {
152 return new NumberXmlCodec<>(AbstractIntegerStringCodec.from(type));
156 protected XmlCodec<?> uint16Codec(final Uint16TypeDefinition type) {
157 return new NumberXmlCodec<>(AbstractIntegerStringCodec.from(type));
161 protected XmlCodec<?> uint32Codec(final Uint32TypeDefinition type) {
162 return new NumberXmlCodec<>(AbstractIntegerStringCodec.from(type));
166 protected XmlCodec<?> uint64Codec(final Uint64TypeDefinition type) {
167 return new NumberXmlCodec<>(AbstractIntegerStringCodec.from(type));
171 protected XmlCodec<?> unionCodec(final UnionTypeDefinition type, final List<XmlCodec<?>> codecs) {
172 return UnionXmlCodec.create(type, codecs);
176 protected XmlCodec<?> unknownCodec(final UnknownTypeDefinition type) {
177 return NullXmlCodec.INSTANCE;