001 /*
002 * Copyright 2007-2008 Sun Microsystems, Inc. All Rights Reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation. Sun designates this
008 * particular file as subject to the "Classpath" exception as provided
009 * by Sun in the LICENSE file that accompanied this code.
010 *
011 * This code is distributed in the hope that it will be useful, but WITHOUT
012 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014 * version 2 for more details (a copy is included in the LICENSE file that
015 * accompanied this code).
016 *
017 * You should have received a copy of the GNU General Public License version
018 * 2 along with this work; if not, write to the Free Software Foundation,
019 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020 *
021 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022 * CA 95054 USA or visit www.sun.com if you need additional information or
023 * have any questions.
024 */
025
026 package com.sun.tools.javap;
027
028 import java.io.PrintWriter;
029
030 import com.sun.tools.classfile.AttributeException;
031 import com.sun.tools.classfile.ConstantPoolException;
032 import com.sun.tools.classfile.DescriptorException;
033
034 /*
035 * A writer similar to a PrintWriter but which does not hide exceptions.
036 * The standard print calls are line-buffered; report calls write messages directly.
037 *
038 * <p><b>This is NOT part of any API supported by Sun Microsystems. If
039 * you write code that depends on this, you do so at your own risk.
040 * This code and its internal interfaces are subject to change or
041 * deletion without notice.</b>
042 */
043 public class BasicWriter {
044 protected BasicWriter(Context context) {
045 lineWriter = LineWriter.instance(context);
046 out = context.get(PrintWriter.class);
047 }
048
049 protected void print(String s) {
050 lineWriter.print(s);
051 }
052
053 protected void print(Object o) {
054 lineWriter.print(o == null ? null : o.toString());
055 }
056
057 protected void println() {
058 lineWriter.println();
059 }
060
061 protected void println(String s) {
062 lineWriter.print(s);
063 lineWriter.println();
064 }
065
066 protected void println(Object o) {
067 lineWriter.print(o == null ? null : o.toString());
068 lineWriter.println();
069 }
070
071 protected String report(AttributeException e) {
072 out.println("Error: " + e.getMessage()); // i18n?
073 return "???";
074 }
075
076 protected String report(ConstantPoolException e) {
077 out.println("Error: " + e.getMessage()); // i18n?
078 return "???";
079 }
080
081 protected String report(DescriptorException e) {
082 out.println("Error: " + e.getMessage()); // i18n?
083 return "???";
084 }
085
086 protected String report(String msg) {
087 out.println("Error: " + msg); // i18n?
088 return "???";
089 }
090
091 private LineWriter lineWriter;
092 private PrintWriter out;
093
094 private static class LineWriter {
095 static LineWriter instance(Context context) {
096 LineWriter instance = context.get(LineWriter.class);
097 if (instance == null)
098 instance = new LineWriter(context);
099 return instance;
100 }
101
102 protected LineWriter(Context context) {
103 context.put(LineWriter.class, this);
104 out = context.get(PrintWriter.class);
105 buffer = new StringBuilder();
106 }
107
108 protected void print(String s) {
109 if (s == null)
110 s = "null";
111 for (int i = 0; i < s.length(); i++) {
112 char c = s.charAt(i);
113 if (c == '\n') {
114 println();
115 } else {
116 buffer.append(c);
117 }
118 }
119
120 }
121
122 protected void println() {
123 out.println(buffer);
124 buffer.setLength(0);
125 }
126
127 private PrintWriter out;
128 private StringBuilder buffer;
129 }
130 }
131