View Javadoc

1   /*
2    * Copyright (c) 2001-2005,
3    * RedVerst Group, ISP RAS http://www.ispras.ru
4    * All rights reserved.
5    *
6    * Redistribution and use in source and binary forms, with or without
7    * modification, are permitted provided that the following conditions are met:
8    *
9    * 1. Redistributions of source code must retain the above copyright notice, this
10   *    list of conditions and the following disclaimer.
11   *
12   * 2. Redistributions in binary form must reproduce the above copyright notice,
13   *    this list of conditions and the following disclaimer in the documentation
14   *    and/or other materials provided with the distribution.
15   *
16   * 3. The names "ATP", "TreeDL", "RedVerst", "ISP RAS"
17   *    may not be used to endorse or promote products derived from this software
18   *    without specific prior written permission.
19   *
20   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
21   * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
22   * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23   * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
24   * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
25   * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26   * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
27   * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28   * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
29   * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
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          // processed in visitModule
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                  // remove nested comments
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 }