KickJava   Java API By Example, From Geeks To Geeks.

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


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

23
24 package org.enhydra.xml.xmlc.codegen;
25
26
27 /**
28  * Class used to build the code body of a method.
29  */

30 public final class JavaCode {
31     /**
32      * Initial size for StringBuffers.
33      */

34     private static final int INIT_BUFFER_SIZE = 1024;
35
36     /**
37      * Optional variable definitions to insert at the beginning
38      * of the body.
39      */

40     private StringIndentWriter fVars;
41
42     /**
43      * Accumulated code block.
44      */

45     private StringIndentWriter fCode = new StringIndentWriter(INIT_BUFFER_SIZE);
46
47     /**
48      * Constructor.
49      */

50     public JavaCode() {
51     }
52     
53     /**
54      * Increase indentation level.
55      */

56     public final void enter() {
57         fCode.enter();
58     }
59
60     /**
61      * Decrease indentation level.
62      */

63     public final void leave() {
64         fCode.leave();
65     }
66
67     /**
68      * Add an EOLN
69      */

70     public void addln() {
71         fCode.println();
72     }
73
74     /**
75      * Add of code to the current line
76      */

77     public void add(String JavaDoc code) {
78         fCode.print(code);
79     }
80
81     /**
82      * Add of a line of code.
83      */

84     public void addln(String JavaDoc line) {
85         fCode.println(line);
86     }
87
88     /**
89      * Add multiple lines of code.
90      */

91     public void addln(String JavaDoc[] lines) {
92         for (int idx = 0; idx < lines.length; idx++) {
93             fCode.println(lines[idx]);
94         }
95     }
96
97     /**
98      * Add variables that will be inserted at the top of the code
99      * body.
100      */

101     public void addVars(String JavaDoc line) {
102         if (fVars == null) {
103             fVars = new StringIndentWriter(INIT_BUFFER_SIZE);
104         }
105         fVars.println(line);
106     }
107
108     /**
109      * Add variables that will be inserted at the top of the code body.
110      */

111     public void addVars(String JavaDoc[] lines) {
112         for (int idx = 0; idx < lines.length; idx++) {
113             addVars(lines[idx]);
114         }
115     }
116
117     /**
118      * Determine if there is any code in the block.
119      */

120     public boolean isEmpty() {
121         return ((fVars == null) && (fCode.getBuffer().length() == 0));
122     }
123
124     /**
125      * Print Java code.
126      */

127     public void print(IndentWriter out) {
128         if (fVars != null) {
129             out.print(fVars.toString());
130         }
131         out.print(fCode.toString());
132     }
133 }
134
Popular Tags