2 * Copyright (c) 2014 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.binding.data.codec.gen.impl;
10 import org.opendaylight.yangtools.binding.data.codec.util.AugmentableDispatchSerializer;
11 import org.opendaylight.yangtools.binding.data.codec.util.ChoiceDispatchSerializer;
12 import org.opendaylight.yangtools.sal.binding.generator.util.JavassistUtils;
13 import org.opendaylight.yangtools.sal.binding.model.api.GeneratedType;
14 import org.opendaylight.yangtools.yang.binding.DataObject;
15 import org.opendaylight.yangtools.yang.binding.DataObjectSerializerImplementation;
16 import org.opendaylight.yangtools.yang.model.api.AugmentationSchema;
17 import org.opendaylight.yangtools.yang.model.api.ChoiceCaseNode;
18 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
19 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
20 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
21 import org.opendaylight.yangtools.yang.model.api.NotificationDefinition;
24 * Concrete implementation of {@link AbstractStreamWriterGenerator}
25 * which in runtime generates classes implementing {@link DataObjectSerializerImplementation}
26 * interface and are used to serialize Binding {@link DataObject}.
28 * Actual implementation of codecs is done via static methods, which allows
29 * for static wiring of codecs. Choice codec and Augmentable codecs
30 * are static properties of parent codec and stateless implementations
31 * are used ( {@link ChoiceDispatchSerializer}, {@link AugmentableDispatchSerializer},
32 * which uses registry to dispatch to concrete item codec.
35 public class StreamWriterGenerator extends AbstractStreamWriterGenerator {
37 private StreamWriterGenerator(final JavassistUtils utils, final Void ignore) {
42 * Deprecated, use {@link #create(JavassistUtils)} instead.
46 public StreamWriterGenerator(final JavassistUtils utils) {
51 * Create a new instance backed by a specific {@link JavassistUtils} instance.
53 * @param utils JavassistUtils instance to use
54 * @return A new generator
56 public static DataObjectSerializerGenerator create(final JavassistUtils utils) {
57 return new StreamWriterGenerator(utils, null);
60 private static CharSequence getChildSizeFromSchema(final DataNodeContainer node) {
61 return Integer.toString(node.getChildNodes().size());
65 protected DataObjectSerializerSource generateContainerSerializer(final GeneratedType type, final ContainerSchemaNode node) {
67 return new AugmentableDataNodeContainerEmitterSource(this, type, node) {
69 public CharSequence emitStartEvent() {
70 return startContainerNode(classReference(type), getChildSizeFromSchema(node));
76 protected DataObjectSerializerSource generateNotificationSerializer(final GeneratedType type, final NotificationDefinition node) {
78 return new AugmentableDataNodeContainerEmitterSource(this, type, node) {
80 public CharSequence emitStartEvent() {
81 return startContainerNode(classReference(type), getChildSizeFromSchema(node));
87 protected DataObjectSerializerSource generateCaseSerializer(final GeneratedType type, final ChoiceCaseNode node) {
88 return new AugmentableDataNodeContainerEmitterSource(this, type, node) {
90 public CharSequence emitStartEvent() {
91 return startCaseNode(classReference(type),getChildSizeFromSchema(node));
97 protected DataObjectSerializerSource generateUnkeyedListEntrySerializer(final GeneratedType type, final ListSchemaNode node) {
98 return new AugmentableDataNodeContainerEmitterSource(this, type, node) {
101 public CharSequence emitStartEvent() {
102 return startUnkeyedListItem(getChildSizeFromSchema(schemaNode));
108 protected DataObjectSerializerSource generateSerializer(final GeneratedType type, final AugmentationSchema schema) {
109 return new DataNodeContainerSerializerSource(this, type, schema) {
112 public CharSequence emitStartEvent() {
113 return startAugmentationNode(classReference(type));
119 protected DataObjectSerializerSource generateMapEntrySerializer(final GeneratedType type, final ListSchemaNode node) {
120 return new AugmentableDataNodeContainerEmitterSource(this, type, node) {
122 public CharSequence emitStartEvent() {
123 return startMapEntryNode(invoke(INPUT, "getKey"), getChildSizeFromSchema(node));