BasicWriter.java revision 580
1N/A/*
1N/A * Copyright (c) 2007, 2008, Oracle and/or its affiliates. All rights reserved.
1N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
1N/A *
1N/A * This code is free software; you can redistribute it and/or modify it
1N/A * under the terms of the GNU General Public License version 2 only, as
1N/A * published by the Free Software Foundation. Oracle designates this
1N/A * particular file as subject to the "Classpath" exception as provided
1N/A * by Oracle in the LICENSE file that accompanied this code.
1N/A *
1N/A * This code is distributed in the hope that it will be useful, but WITHOUT
1N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
1N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
1N/A * version 2 for more details (a copy is included in the LICENSE file that
1N/A * accompanied this code).
1N/A *
1N/A * You should have received a copy of the GNU General Public License version
1N/A * 2 along with this work; if not, write to the Free Software Foundation,
1N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
1N/A *
1N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
1N/A * or visit www.oracle.com if you need additional information or have any
1N/A * questions.
1N/A */
1N/A
1N/Apackage com.sun.tools.javap;
1N/A
1N/Aimport java.io.PrintWriter;
1N/A
1N/Aimport com.sun.tools.classfile.AttributeException;
1N/Aimport com.sun.tools.classfile.ConstantPoolException;
1N/Aimport com.sun.tools.classfile.DescriptorException;
1N/A
1N/A/*
1N/A * A writer similar to a PrintWriter but which does not hide exceptions.
1N/A * The standard print calls are line-buffered; report calls write messages directly.
1N/A *
1N/A * <p><b>This is NOT part of any supported API.
1N/A * If you write code that depends on this, you do so at your own risk.
1N/A * This code and its internal interfaces are subject to change or
1N/A * deletion without notice.</b>
1N/A */
1N/Apublic class BasicWriter {
1N/A protected BasicWriter(Context context) {
1N/A lineWriter = LineWriter.instance(context);
1N/A out = context.get(PrintWriter.class);
1N/A messages = context.get(Messages.class);
1N/A if (messages == null)
1N/A throw new AssertionError();
1N/A }
1N/A
1N/A protected void print(String s) {
1N/A lineWriter.print(s);
1N/A }
protected void print(Object o) {
lineWriter.print(o == null ? null : o.toString());
}
protected void println() {
lineWriter.println();
}
protected void println(String s) {
lineWriter.print(s);
lineWriter.println();
}
protected void println(Object o) {
lineWriter.print(o == null ? null : o.toString());
lineWriter.println();
}
protected void indent(int delta) {
lineWriter.indent(delta);
}
protected void tab() {
lineWriter.tab();
}
protected void setPendingNewline(boolean b) {
lineWriter.pendingNewline = b;
}
protected String report(AttributeException e) {
out.println("Error: " + e.getMessage()); // i18n?
return "???";
}
protected String report(ConstantPoolException e) {
out.println("Error: " + e.getMessage()); // i18n?
return "???";
}
protected String report(DescriptorException e) {
out.println("Error: " + e.getMessage()); // i18n?
return "???";
}
protected String report(String msg) {
out.println("Error: " + msg); // i18n?
return "???";
}
protected String space(int w) {
if (w < spaces.length && spaces[w] != null)
return spaces[w];
StringBuilder sb = new StringBuilder();
for (int i = 0; i < w; i++)
sb.append(" ");
String s = sb.toString();
if (w < spaces.length)
spaces[w] = s;
return s;
}
private String[] spaces = new String[80];
private LineWriter lineWriter;
private PrintWriter out;
protected Messages messages;
private static class LineWriter {
static LineWriter instance(Context context) {
LineWriter instance = context.get(LineWriter.class);
if (instance == null)
instance = new LineWriter(context);
return instance;
}
protected LineWriter(Context context) {
context.put(LineWriter.class, this);
Options options = Options.instance(context);
indentWidth = options.indentWidth;
tabColumn = options.tabColumn;
out = context.get(PrintWriter.class);
buffer = new StringBuilder();
}
protected void print(String s) {
if (pendingNewline) {
println();
pendingNewline = false;
}
if (s == null)
s = "null";
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
switch (c) {
case '\n':
println();
break;
default:
if (buffer.length() == 0)
indent();
buffer.append(c);
}
}
}
protected void println() {
out.println(buffer);
buffer.setLength(0);
}
protected void indent(int delta) {
indentCount += delta;
}
protected void tab() {
if (buffer.length() == 0)
indent();
space(indentCount * indentWidth + tabColumn - buffer.length());
}
private void indent() {
space(indentCount * indentWidth);
}
private void space(int n) {
for (int i = 0; i < n; i++)
buffer.append(' ');
}
private PrintWriter out;
private StringBuilder buffer;
private int indentCount;
private int indentWidth;
private int tabColumn;
private boolean pendingNewline;
}
}