All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.enhydra.xml.xmlc.codegen.VarNames Maven / Gradle / Ivy

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

package org.enhydra.xml.xmlc.codegen;

/**
 * Generate tmp variable names for use in initialization.  Keeps a table of
 * tmp variable names used in initialize of nodes. Since nodes are recursive,
 * but the initialization code is an inline block of code, we use this to
 * recycle as many variables as possible, but not clobber ones that are
 * currently in use.
 */
public class VarNames {
    /**
     * Type of variable.
     */
    private String type;

    /**
     * Prefix for variable name.
     */
    private String prefix;

    /*
     * Maximum level we have reached.
     */
    private int maxLevel = -1;

    /**
     * Construct a new object.
     * @param varType Type for the generated variable names.
     * @param varPrefix Prefix for the generated variable names.
     */
    public VarNames(String varType, String varPrefix) {
        type = varType;
        prefix = varPrefix;
    }

    /**
     * Get the variable name for the current level.
     * @param level current level
     * @return the name of the local variable.
     */
    public String getVarName(int level) {
        if (maxLevel < level) {
            maxLevel = level;
        }
        return prefix + level;
    }

    /**
     * Get code to define local variables.
     * @return Line of java code or null if no code was created.
     */
    public String getVarDefs() {
        if (maxLevel < 0) {
            return null;
        }
        StringBuffer def = new StringBuffer(type + " ");
        for (int level = 0; level <= maxLevel; level++) {
            if (level > 0) {
                def.append(", ");
            }
            def.append(prefix+level);
        }
        def.append(";");
        return def.toString();
    }

    /**
     * Insert variable definitions at the beginning of the body of a method.
     */
    public void insertVarDefs(JavaCode body) {
        String defs = getVarDefs();
        if (defs != null) {
            body.addVars(defs);
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy