|
What this is
Other links
The source code
/*
* Sun Public License Notice
*
* The contents of this file are subject to the Sun Public License
* Version 1.0 (the "License"). You may not use this file except in
* compliance with the License. A copy of the License is available at
* http://www.sun.com/
*
* The Original Code is NetBeans. The Initial Developer of the Original
* Code is Sun Microsystems, Inc. Portions Copyright 1997-2003 Sun
* Microsystems, Inc. All Rights Reserved.
*/
package org.netbeans.modules.schema2beansdev.gen;
import java.util.*;
import java.io.*;
public class JavaWriter extends IndentingWriter {
// Parts of a Java class. If you add another section be sure to
// increment defaultSectionCount and put a line into insertSectionAfter
public int HEADER_SECTION = 0;
public int DECL_SECTION = 1;
public int CONSTRUCTOR_SECTION = 2;
public int BODY_SECTION = 3;
public int TRAILER_SECTION = 4;
static final protected int defaultSectionCount = 5;
static public final int PUBLIC = 0x0;
static public final int PROTECTED = 0x1;
static public final int PACKAGE_LEVEL = 0x2;
static public final int PRIVATE = 0x3;
static public final int ACCESS_MASK = 0x3;
static public final int STATIC = 0x10;
static public final int FINAL = 0x20;
static public final int BEANINFO = 0x100;
static public final int IO = 0x200;
static public final int UNSUPPORTED = 0x400;
static public final int METHOD_SEMANTIC_MASK = 0xf00;
protected boolean newlineBeforeCurlyBrace = false;
public boolean storeMethods = true;
public JavaWriter() {
super(defaultSectionCount);
privateInit();
}
public JavaWriter(JavaWriter source) {
super(source);
HEADER_SECTION = source.HEADER_SECTION;
DECL_SECTION = source.DECL_SECTION;
CONSTRUCTOR_SECTION = source.CONSTRUCTOR_SECTION;
BODY_SECTION = source.BODY_SECTION;
TRAILER_SECTION = source.TRAILER_SECTION;
newlineBeforeCurlyBrace = source.newlineBeforeCurlyBrace;
storeMethods = source.storeMethods;
methods = new LinkedHashMap();
}
/**
* Insert a custom section after another section.
* eg:
* JavaWriter jw = new JavaWriter();
* int SPECIAL_SECTION = jw.insertSectionAfter(jw.CONSTRUCTOR_SECTION);
*/
public int insertSectionAfter(int sectionNum) {
insertAdditionalBuffers(sectionNum, 1);
if (sectionNum < HEADER_SECTION) ++HEADER_SECTION;
if (sectionNum < DECL_SECTION) ++DECL_SECTION;
if (sectionNum < CONSTRUCTOR_SECTION) ++CONSTRUCTOR_SECTION;
if (sectionNum < BODY_SECTION) ++BODY_SECTION;
if (sectionNum < TRAILER_SECTION) ++TRAILER_SECTION;
return sectionNum + 1;
}
public void reset() {
super.reset();
privateInit();
}
private void privateInit() {
for (int i = 0; i < bufferCount; i++) {
if (i == HEADER_SECTION)
indentLevel[i] = 0;
else
indentLevel[i] = 1;
}
methods = new LinkedHashMap();
}
/**
* Send buffers to @param out
* Everything is passed thru native2ascii.
*/
public void writeTo(Writer out) throws IOException {
Writer n2aout = new BufferedWriter(new JavaUtil.N2AFilter(out));
super.writeTo(n2aout);
n2aout.flush();
}
/**
* Send buffers to @param out
* Everything is passed thru native2ascii.
*/
public void writeTo(OutputStream out) throws IOException {
Writer w = new OutputStreamWriter(out);
writeTo(w);
w.flush();
}
public void writeTo(GenBuffer o) {
super.writeTo(o);
if (o instanceof JavaWriter) {
JavaWriter out = (JavaWriter) o;
if (storeMethods) {
out.methods.putAll(methods);
}
}
}
public boolean writeOptions(int options) throws IOException {
boolean needSpace = writeAccess(options);
if ((options & STATIC) == STATIC) {
if (needSpace)
write(" ");
write("static");
needSpace = true;
}
if ((options & FINAL) == FINAL) {
if (needSpace)
write(" ");
write("final");
needSpace = true;
}
return needSpace;
}
public boolean writeAccess(int accessLevel) throws IOException {
switch (accessLevel & ACCESS_MASK) {
case PUBLIC:
write("public");
return true;
case PROTECTED:
write("protected");
return true;
case PACKAGE_LEVEL:
// write nothing
return false;
case PRIVATE:
write("private");
return true;
}
return false;
}
/**
* Writes a class declaration into the DECL_SECTION.
*/
public void writeClassDecl(String name, String extendsStatement,
String implementsStatement, int options) throws IOException {
pushSelect(HEADER_SECTION);
try {
if (writeOptions(options))
write(" ");
write("class ", name, " ");
if (extendsStatement != null) {
write("extends ", extendsStatement, " ");
}
if (implementsStatement != null) {
write("implements ", implementsStatement, " ");
}
begin();
popSelect();
pushSelect(TRAILER_SECTION);
end();
} finally {
popSelect();
}
}
public void beginMethod(String name) throws IOException {
beginMethod(name, "", null);
}
public void beginMethod(String name, String parameters) throws IOException {
beginMethod(name, parameters, null);
}
public void beginMethod(String name, String parameters, String exceptions) throws IOException {
beginMethod(name, parameters, exceptions, "void", PUBLIC);
}
public void beginMethod(String name, String parameters, String exceptions,
String returnType) throws IOException {
beginMethod(name, parameters, exceptions, returnType, PUBLIC);
}
public void beginMethod(String name, String parameters, String exceptions,
String returnType, int options) throws IOException {
writeMethod(name, parameters, exceptions, returnType, options);
write(" ");
begin();
}
public void endMethod() throws IOException {
end();
cr();
}
public void writeMethod(String name, String parameters, String exceptions,
String returnType, int options) throws IOException {
String nameParameters = name+"("+parameters+")";
if (storeMethods) {
addToMethodStore(name, parameters, exceptions, returnType, options);
}
if (writeOptions(options))
write(" ");
write(returnType);
write(" ");
write(nameParameters);
if (exceptions != null)
write(" throws ", exceptions);
}
private Map methods; // Map
|
| ... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
Copyright 1998-2024 Alvin Alexander, alvinalexander.com
All Rights Reserved.
A percentage of advertising revenue from
pages under the /java/jwarehouse
URI on this website is
paid back to open source projects.