KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > ws > jaxme > js > JavaField


1 /*
2  * Copyright 2003, 2004 The Apache Software Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15
16  */

17 package org.apache.ws.jaxme.js;
18
19 import java.io.IOException JavaDoc;
20
21
22 /** <p>Implements a field that a java class or interface may have.</p>
23  *
24  * @author <a HREF="mailto:joe@ispsoft.de">Jochen Wiedmann</a>
25  */

26 public class JavaField extends JavaSourceObject implements DirectAccessible {
27   private class MyTarget implements IndentationTarget {
28     private StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
29     private IndentationTarget actualTarget;
30     private MyTarget(IndentationTarget pActualTarget) {
31       actualTarget = pActualTarget;
32     }
33     public boolean isInterface() {
34       return actualTarget.isInterface();
35     }
36     public String JavaDoc asString(JavaQName pQName) {
37       return actualTarget.asString(pQName);
38     }
39     public void write(String JavaDoc pValue) {
40       sb.append(pValue);
41     }
42     public void write() {
43       sb.append("\n");
44     }
45     public void indent(int i) {
46     }
47     public String JavaDoc getResult() { return sb.toString(); }
48   }
49
50   private boolean isTransient;
51   private boolean isNullable = true;
52
53   /** <p>Creates a new JavaField with the given name, type and
54    * protection.</p>
55    */

56   JavaField(String JavaDoc pName, JavaQName pType, JavaSource.Protection pProtection) {
57     super(pName, pType, pProtection);
58   }
59
60   /** <p>Sets whether the field is transient. By default it isn't.</p>
61    */

62   public void setTransient(boolean pTransient) {
63      isTransient = pTransient;
64   }
65
66   /** <p>Returns whether the field is transient. By default it isn't.</p>
67     */

68   public boolean isTransient() {
69      return isTransient;
70   }
71
72
73   /** <p>Returns a string representation of this field.</p>
74    */

75   public void write(IndentationTarget pTarget) throws IOException JavaDoc {
76     pTarget.indent(0);
77     writeNoEol(pTarget);
78     pTarget.write();
79   }
80
81   protected void writeNoEol(IndentationTarget pTarget) throws IOException JavaDoc {
82     if (pTarget.isInterface()) {
83       return;
84     }
85     JavaComment jcon = getComment();
86     if (jcon != null) {
87       jcon.write(pTarget);
88       pTarget.indent(0);
89     }
90     JavaSource.Protection protection = getProtection();
91     if (protection != null && !protection.equals(JavaSource.DEFAULT_PROTECTION)) {
92       pTarget.write(protection.toString());
93       pTarget.write(" ");
94     }
95     if (isFinal()) {
96       pTarget.write("final ");
97     }
98     if (isStatic()) {
99       pTarget.write("static ");
100     }
101     if (isTransient()) {
102         pTarget.write("transient ");
103     }
104     pTarget.write(pTarget.asString(getType()));
105     pTarget.write(" ");
106     pTarget.write(getName());
107     if (isEmpty()) {
108       pTarget.write(";");
109     } else {
110       pTarget.write(" = ");
111       MyTarget mt = new MyTarget(pTarget);
112       super.write(mt);
113       String JavaDoc result = mt.getResult();
114       if (result.endsWith("\r\n")) {
115         result = result.substring(0, result.length()-2);
116       } else if (result.endsWith("\n")) {
117         result = result.substring(0, result.length()-1);
118       }
119       pTarget.write(result);
120       if (!result.endsWith(";")) {
121         pTarget.write(";");
122       }
123     }
124   }
125
126   public void setValue(Object JavaDoc pValue) {
127     clear();
128     addLine(pValue);
129   }
130
131   public boolean isNullable() { return isNullable; }
132   public void setNullable(boolean pNullable) { isNullable = pNullable; }
133 }
134
Popular Tags