KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > xml > xmlc > codegen > VarNames


1 /*
2  * Enhydra Java Application Server Project
3  *
4  * The contents of this file are subject to the Enhydra Public License
5  * Version 1.1 (the "License"); you may not use this file except in
6  * compliance with the License. You may obtain a copy of the License on
7  * the Enhydra web site ( http://www.enhydra.org/ ).
8  *
9  * Software distributed under the License is distributed on an "AS IS"
10  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
11  * the License for the specific terms governing rights and limitations
12  * under the License.
13  *
14  * The Initial Developer of the Enhydra Application Server is Lutris
15  * Technologies, Inc. The Enhydra Application Server and portions created
16  * by Lutris Technologies, Inc. are Copyright Lutris Technologies, Inc.
17  * All Rights Reserved.
18  *
19  * Contributor(s):
20  *
21  * $Id: VarNames.java,v 1.1.1.1 2003/03/10 16:36:19 taweili Exp $
22  */

23
24 package org.enhydra.xml.xmlc.codegen;
25
26 /**
27  * Generate tmp variable names for use in initialization. Keeps a table of
28  * tmp variable names used in initialize of nodes. Since nodes are recursive,
29  * but the initialization code is an inline block of code, we use this to
30  * recycle as many variables as possible, but not clobber ones that are
31  * currently in use.
32  */

33 public class VarNames {
34     /**
35      * Type of variable.
36      */

37     private String JavaDoc type;
38
39     /**
40      * Prefix for variable name.
41      */

42     private String JavaDoc prefix;
43
44     /*
45      * Maximum level we have reached.
46      */

47     private int maxLevel = -1;
48
49     /**
50      * Construct a new object.
51      * @param varType Type for the generated variable names.
52      * @param varPrefix Prefix for the generated variable names.
53      */

54     public VarNames(String JavaDoc varType, String JavaDoc varPrefix) {
55         type = varType;
56         prefix = varPrefix;
57     }
58
59     /**
60      * Get the variable name for the current level.
61      * @param level current level
62      * @return the name of the local variable.
63      */

64     public String JavaDoc getVarName(int level) {
65         if (maxLevel < level) {
66             maxLevel = level;
67         }
68         return prefix + level;
69     }
70
71     /**
72      * Get code to define local variables.
73      * @return Line of java code or null if no code was created.
74      */

75     public String JavaDoc getVarDefs() {
76         if (maxLevel < 0) {
77             return null;
78         }
79         StringBuffer JavaDoc def = new StringBuffer JavaDoc(type + " ");
80         for (int level = 0; level <= maxLevel; level++) {
81             if (level > 0) {
82                 def.append(", ");
83             }
84             def.append(prefix+level);
85         }
86         def.append(";");
87         return def.toString();
88     }
89
90     /**
91      * Insert variable definitions at the beginning of the body of a method.
92      */

93     public void insertVarDefs(JavaCode body) {
94         String JavaDoc defs = getVarDefs();
95         if (defs != null) {
96             body.addVars(defs);
97         }
98     }
99 }
100
Popular Tags