1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32 package com.unitesk.atp.treedl;
33
34 import com.unitesk.atp.treedl.ParameterVariantMatrix.Variant;
35 import com.unitesk.atp.treedl.TDL.*;
36 import com.unitesk.atp.tool.PluginClass;
37
38 /***
39 * @author <A href="mailto:demakov@ispras.ru">Alexey Demakov</A>
40 * @author <A href="mailto:ale.petuhov@gmail.com">Alexander Petuhov</A>
41 * @version $Id: Java5NodeGenerator.java,v 1.21 2005/11/20 20:20:13 all-x Exp $
42 */
43 public class Java5NodeGenerator extends Java5TextGenerator
44 {
45 /***
46 * {@inheritDoc}
47 */
48 public String getVersion()
49 {
50 return PluginClass.getRevision( "$Revision: 1.21 $" ) + "-beta";
51 }
52
53 public void visitBaseModule( BaseModule node )
54 {
55
56 throw new UnsupportedOperationException();
57 }
58
59 public void visitConstTypeDecl( ConstTypeDecl node )
60 {
61 nl();
62 txt("public enum ${name}");
63 nl();
64 block_nl();
65 for( int i = 0
66 ; i < node.sizeFullConstantList()
67 ; i++
68 )
69 {
70 txtif( i > 0, ", " );
71 txtAsIs( node.getFullConstantList( i ).toString() );
72 }
73 nl();
74 unblock_nl();
75 }
76
77 /***
78 * check( int ) for the first attribute of constant type
79 */
80 protected void generateEnumCheck( Field node )
81 {
82 if( ((ConstTypeDecl)((NodeType)node.getType()).getType().getType()).isFlags() )
83 {
84 txt( "public boolean check${Up:name}( ${type} value )" ); nl();
85 block_nl();
86 txt( "return get${Up:name}().containsAll( value );" ); nl();
87 unblock_nl();
88 } else {
89 txt( "public boolean check${Up:name}( ${type} value )" ); nl();
90 block_nl();
91 txt( "return get${Up:name}() == value;" ); nl();
92 unblock_nl();
93 }
94 nl();
95 }
96
97 protected void generateEnumSwitch( OperationDecl operationDecl
98 , int index
99 , ParameterVariantMatrix paramMatrix
100 , Index paramIndex
101 )
102 {
103 ModuleMatrix moduleMatrix = operationDecl.getModuleMatrix();
104 if( index < moduleMatrix.getDim() )
105 {
106 if( moduleMatrix.isNodeType( index ) )
107 {
108 generateEnumSwitch( operationDecl, index + 1, paramMatrix, paramIndex );
109 return;
110 }
111 pushNode( operationDecl.getOptParameterDeclList( moduleMatrix.getIndex( index ) ) );
112 txt( "switch( ${name} )" ); nl();
113 block_nl();
114 Object old_variant = getVariable( "variant" );
115 for( int i = 0; i < paramMatrix.getSize( index ); i++ )
116 {
117 Variant v = paramMatrix.getVariant( index, i );
118 setVariable( "variant", v );
119 txt( "case ${.variant.constant}:" ); nl();
120 block_nl();
121 paramIndex.setIndex( index, i );
122 generateEnumSwitch( operationDecl, index + 1, paramMatrix, paramIndex );
123 unblock_nl();
124 }
125 setVariable( "variant", old_variant );
126 txt( "default:" ); nl(); incIndent();
127 txt( "throw new IllegalArgumentException( \"${name}\" );" ); nl(); popIndent();
128 unblock_nl();
129 popNode();
130 } else {
131 generateCaseCode( operationDecl, paramMatrix, paramIndex );
132 if( operationDecl.getReturnType() == null )
133 {
134 txt( "break;" ); nl();
135 }
136 }
137 }
138
139 public void visitNodeType( NodeType node )
140 {
141 txtif( node.checkCardinality( Cardinality.LIST )
142 , "java.util.List<${type}>"
143 , "${type}"
144 );
145 }
146
147 public void visitPredefinedType( PredefinedType node )
148 {
149 if( JavaLanguageDescription.isPrimitiveType( node ) )
150 {
151 txtif( node.checkCardinality( Cardinality.LIST )
152 , "java.util.List<${ElementObjectType:this}>"
153 , "${PredefinedTypeName:kind}"
154 );
155 } else {
156 txtif( node.checkCardinality( Cardinality.LIST )
157 , "java.util.List<${PredefinedTypeName:kind}>"
158 , "${PredefinedTypeName:kind}"
159 );
160 }
161 }
162
163 public void visitNameType( NameType node )
164 {
165 txtif( node.checkCardinality( Cardinality.LIST )
166 , "java.util.List<"
167
168 + JavaLanguageDescription.normalizeType( node.getName() )
169 + ">"
170 , "${name}"
171 );
172 }
173
174 public void visitTypeRef( TypeRef node )
175 {
176 if( node.getType() == TDL_Module.getRootNodeType() )
177 {
178 txt( "${.Node}" );
179 } else {
180 txtif( node.getModule() != module, "${module.root.name}." );
181
182 ModuleTypeMember type=node.getType();
183 if (type instanceof ConstTypeDecl)
184 {
185 txtif( ((ConstTypeDecl)node.getType()).isFlags()
186 , "java.util.EnumSet<${name}>"
187 , "${name}"
188 );
189 }
190 else
191 {
192 txt( "${name}" );
193 }
194 }
195 }
196 }