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

org.enhydra.xml.xmlc.codegen.JavaCode 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: JavaCode.java,v 1.2 2005/01/26 08:29:24 jkjome Exp $
 */

package org.enhydra.xml.xmlc.codegen;


/**
 * Class used to build the code body of a method.  
 */
public final class JavaCode {
    /**
     * Initial size for StringBuffers.
     */
    private static final int INIT_BUFFER_SIZE = 1024;

    /**
     * Optional variable definitions to insert at the beginning
     * of the body.
     */
    private StringIndentWriter fVars;

    /**
     * Accumulated code block.
     */
    private StringIndentWriter fCode = new StringIndentWriter(INIT_BUFFER_SIZE);

    /**
     * Constructor.
     */
    public JavaCode() {
    }
    
    /**
     * Increase indentation level.
     */
    public final void enter() {
        fCode.enter();
    }

    /**
     * Decrease indentation level.
     */
    public final void leave() {
        fCode.leave();
    }

    /**
     * Add an EOLN
     */
    public void addln() {
        fCode.println();
    }

    /**
     * Add of code to the current line
     */
    public void add(String code) {
        fCode.print(code);
    }

    /**
     * Add of a line of code.
     */
    public void addln(String line) {
        fCode.println(line);
    }

    /**
     * Add multiple lines of code.
     */
    public void addln(String[] lines) {
        for (int idx = 0; idx < lines.length; idx++) {
            fCode.println(lines[idx]);
        }
    }

    /**
     * Add variables that will be inserted at the top of the code
     * body.
     */
    public void addVars(String line) {
        if (fVars == null) {
            fVars = new StringIndentWriter(INIT_BUFFER_SIZE);
        }
        fVars.println(line);
    }

    /**
     * Add variables that will be inserted at the top of the code body.
     */
    public void addVars(String[] lines) {
        for (int idx = 0; idx < lines.length; idx++) {
            addVars(lines[idx]);
        }
    }

    /**
     * Determine if there is any code in the block.
     */
    public boolean isEmpty() {
        return ((fVars == null) && (fCode.getBuffer().length() == 0));
    }

    /**
     * Print Java code.
     */
    public void print(IndentWriter out) {
        if (fVars != null) {
            out.print(fVars.toString());
        }
        out.print(fCode.toString());
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy