1 33 34 package net.percederberg.grammatica.code.csharp; 35 36 import java.io.PrintWriter ; 37 import java.util.LinkedList ; 38 39 import net.percederberg.grammatica.code.CodeElement; 40 import net.percederberg.grammatica.code.CodeStyle; 41 42 48 public class CSharpConstructor extends CodeElement { 49 50 53 public static final int PUBLIC = CSharpModifier.PUBLIC; 54 55 58 public static final int PROTECTED_INTERNAL = 59 CSharpModifier.PROTECTED_INTERNAL; 60 61 64 public static final int PROTECTED = CSharpModifier.PROTECTED; 65 66 69 public static final int INTERNAL = CSharpModifier.INTERNAL; 70 71 74 public static final int PRIVATE = CSharpModifier.PRIVATE; 75 76 79 public static final int EXTERN = CSharpModifier.EXTERN; 80 81 84 private int modifiers; 85 86 89 private CSharpClass cls; 90 91 94 private String args; 95 96 99 private String initializer; 100 101 104 private LinkedList code; 105 106 109 private CSharpComment comment; 110 111 114 public CSharpConstructor() { 115 this(""); 116 } 117 118 123 public CSharpConstructor(String args) { 124 this(PUBLIC, args); 125 } 126 127 133 public CSharpConstructor(int modifiers, String args) { 134 this.modifiers = modifiers; 135 this.cls = null; 136 this.args = args; 137 this.initializer = null; 138 this.code = new LinkedList (); 139 this.comment = null; 140 } 141 142 148 public CSharpClass getCSharpClass() { 149 return this.cls; 150 } 151 152 157 void setCSharpClass(CSharpClass cls) { 158 this.cls = cls; 159 } 160 161 166 public void addInitializer(String initializer) { 167 this.initializer = initializer; 168 } 169 170 175 public void addCode(String codeLines) { 176 int pos; 177 178 pos = codeLines.indexOf('\n'); 179 while (pos >= 0) { 180 this.code.add(codeLines.substring(0, pos)); 181 codeLines = codeLines.substring(pos + 1); 182 pos = codeLines.indexOf('\n'); 183 } 184 this.code.add(codeLines); 185 } 186 187 192 public void addComment(CSharpComment comment) { 193 this.comment = comment; 194 } 195 196 204 public int category() { 205 return 7; 206 } 207 208 215 public void print(PrintWriter out, CodeStyle style, int indent) { 216 String indentStr = style.getIndent(indent); 217 String codeIndentStr = style.getIndent(indent + 1); 218 StringBuffer res = new StringBuffer (); 219 220 if (comment != null) { 222 comment.print(out, style, indent); 223 } 224 225 res.append(indentStr); 227 res.append(CSharpModifier.createModifierDecl(modifiers)); 228 res.append(cls.toString()); 229 res.append("("); 230 res.append(args); 231 res.append(")"); 232 233 if (initializer != null) { 235 res.append("\n"); 236 res.append(codeIndentStr); 237 res.append(": "); 238 res.append(initializer); 239 } 240 res.append(" {\n"); 241 242 if (initializer != null && code.size() > 0) { 244 res.append("\n"); 245 } 246 for (int i = 0; i < code.size(); i++) { 247 if (code.get(i).toString().length() > 0) { 248 res.append(codeIndentStr); 249 res.append(code.get(i).toString()); 250 res.append("\n"); 251 } else { 252 res.append("\n"); 253 } 254 } 255 res.append(indentStr); 256 res.append("}"); 257 258 out.println(res.toString()); 260 } 261 } 262 | Popular Tags |