Bug 6859 #3 Binding generator v1 refactoring
[mdsal.git] / binding / mdsal-binding-dom-codec / src / main / java / org / opendaylight / yangtools / binding / data / codec / gen / impl / DataObjectSerializerSource.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.yangtools.binding.data.codec.gen.impl;
9
10 import com.google.common.base.Preconditions;
11 import org.opendaylight.mdsal.binding.generator.api.ClassLoadingStrategy;
12 import org.opendaylight.mdsal.binding.generator.impl.GeneratedClassLoadingStrategy;
13 import org.opendaylight.mdsal.binding.model.api.Type;
14 import org.opendaylight.yangtools.binding.data.codec.gen.spi.AbstractSource;
15 import org.opendaylight.yangtools.yang.binding.BindingStreamEventWriter;
16 import org.opendaylight.yangtools.yang.binding.DataContainer;
17 import org.opendaylight.yangtools.yang.binding.DataObject;
18 import org.opendaylight.yangtools.yang.binding.DataObjectSerializerRegistry;
19
20 abstract class DataObjectSerializerSource extends AbstractSource {
21
22     private static final ClassLoadingStrategy STRATEGY = GeneratedClassLoadingStrategy.getTCCLClassLoadingStrategy();
23
24     protected static final String SERIALIZER = "_serializer";
25     protected static final String STREAM = "_stream";
26     protected static final String ITERATOR = "_iterator";
27     protected static final String CURRENT = "_current";
28     protected static final String REGISTRY = "_registry";
29
30     private final AbstractGenerator generator;
31
32     /**
33      * @param generator Parent generator
34      */
35     DataObjectSerializerSource(final AbstractGenerator generator) {
36         this.generator = Preconditions.checkNotNull(generator);
37     }
38
39     @SuppressWarnings("unchecked")
40     protected Class<? extends DataContainer> loadClass(final Type childType) {
41         try {
42             return (Class<? extends DataContainer>) STRATEGY.loadClass(childType);
43         } catch (final ClassNotFoundException e) {
44             throw new IllegalStateException("Could not load referenced class ", e);
45         }
46     }
47
48     /**
49      * Returns body of static serialize method.
50      *
51      * <ul>
52      * <li> {@link DataObjectSerializerRegistry} - registry of serializers
53      * <li> {@link DataObject} - object to be serialized
54      * <li> {@link BindingStreamEventWriter} - writer to which events should be serialized.
55      * </ul>
56      *
57      * @return Valid javassist code describing static serialization body.
58      */
59     protected abstract CharSequence getSerializerBody();
60
61     protected final CharSequence leafNode(final String localName, final CharSequence value) {
62         return invoke(STREAM, "leafNode", escape(localName), value);
63     }
64
65     protected final CharSequence startLeafSet(final String localName,final CharSequence expected) {
66         return invoke(STREAM, "startLeafSet", escape(localName),expected);
67     }
68
69     protected final CharSequence startOrderedLeafSet(final String localName, final CharSequence expected) {
70         return invoke(STREAM, "startOrderedLeafSet", escape(localName),expected);
71     }
72
73     protected final CharSequence leafSetEntryNode(final CharSequence value) {
74         return invoke(STREAM, "leafSetEntryNode", value);
75     }
76
77     protected final CharSequence startContainerNode(final CharSequence type, final CharSequence expected) {
78         return invoke(STREAM, "startContainerNode", (type),expected);
79     }
80
81     protected final CharSequence escape(final String localName) {
82         return '"' + localName + '"';
83     }
84
85     protected final CharSequence startUnkeyedList(final CharSequence type, final CharSequence expected) {
86         return invoke(STREAM, "startUnkeyedList", (type),expected);
87     }
88
89     protected final CharSequence startUnkeyedListItem(final CharSequence expected) {
90         return invoke(STREAM, "startUnkeyedListItem",expected);
91     }
92
93     protected final CharSequence startMapNode(final CharSequence type,final CharSequence expected) {
94         return invoke(STREAM, "startMapNode", (type),expected);
95     }
96
97     protected final CharSequence startOrderedMapNode(final CharSequence type,final CharSequence expected) {
98         return invoke(STREAM, "startOrderedMapNode", (type),expected);
99     }
100
101     protected final CharSequence startMapEntryNode(final CharSequence key, final CharSequence expected) {
102         return invoke(STREAM,"startMapEntryNode",key,expected);
103     }
104
105     protected final CharSequence startAugmentationNode(final CharSequence key) {
106         return invoke(STREAM,"startAugmentationNode",key);
107     }
108
109     protected final CharSequence startChoiceNode(final CharSequence localName,final CharSequence expected) {
110         return invoke(STREAM, "startChoiceNode", (localName),expected);
111     }
112
113     protected final CharSequence startCaseNode(final CharSequence localName,final CharSequence expected) {
114         return invoke(STREAM, "startCase", (localName),expected);
115     }
116
117     protected final CharSequence anyxmlNode(final String name, final String value) throws IllegalArgumentException {
118         return invoke(STREAM, "anyxmlNode", escape(name),name);
119     }
120
121     protected final CharSequence endNode() {
122         return invoke(STREAM, "endNode");
123     }
124
125     protected final CharSequence forEach(final String iterable,final Type valueType,final CharSequence body) {
126         return forEach(iterable, ITERATOR, valueType.getFullyQualifiedName(), CURRENT, body);
127     }
128
129     protected final CharSequence classReference(final Type type) {
130         return type.getFullyQualifiedName() + ".class";
131     }
132
133     protected final CharSequence staticInvokeEmitter(final Type childType, final String name) {
134         final Class<?> cls;
135         try {
136             cls = STRATEGY.loadClass(childType);
137         } catch (final ClassNotFoundException e) {
138             throw new IllegalStateException("Failed to invoke emitter", e);
139         }
140
141         final String className = this.generator.loadSerializerFor(cls) + ".getInstance()";
142         return invoke(className, AbstractStreamWriterGenerator.SERIALIZE_METHOD_NAME, REGISTRY, name, STREAM);
143     }
144 }