001: /*
002: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
003: */
004: package com.tc.text;
005:
006: import com.tc.util.NonPortableDetail;
007: import com.tc.util.NonPortableReason;
008:
009: import java.io.PrintWriter;
010: import java.util.Collection;
011: import java.util.Iterator;
012: import java.util.LinkedList;
013:
014: /**
015: * Format a {@link com.tc.util.NonPortableReason} to a PrintWriter, such as System.out or System.err.
016: */
017: public class ConsoleNonPortableReasonFormatter implements
018: NonPortableReasonFormatter {
019:
020: private final PrintWriter out;
021: private final StringFormatter stringFormatter;
022: private final Collection details;
023: private String instructions;
024: private int maxLabelLength;
025: private final String separator;
026: private final ParagraphFormatter paragraphFormatter;
027:
028: /**
029: * @param out Writer to format to
030: * @param separator Separator character to use in details between label and value
031: * @param stringFormatter Can format strings with padding and such
032: * @param paragraphFormatter Can format paragraphs nicely (word wrap, etc)
033: */
034: public ConsoleNonPortableReasonFormatter(PrintWriter out,
035: String separator, StringFormatter stringFormatter,
036: ParagraphFormatter paragraphFormatter) {
037: this .out = out;
038: this .separator = separator;
039: this .stringFormatter = stringFormatter;
040: this .paragraphFormatter = paragraphFormatter;
041: details = new LinkedList();
042: }
043:
044: public void formatReasonTypeName(byte type) {
045: String name;
046: switch (type) {
047: case NonPortableReason.CLASS_NOT_ADAPTABLE:
048: name = "CLASS_NOT_ADAPTABLE";
049: break;
050: case NonPortableReason.SUPER_CLASS_NOT_ADAPTABLE:
051: name = "SUPER_CLASS_NOT_ADAPTABLE";
052: break;
053: case NonPortableReason.SUBCLASS_OF_LOGICALLY_MANAGED_CLASS:
054: name = "SUBCLASS_OF_LOGICALLY_MANAGED_CLASS";
055: break;
056: case NonPortableReason.CLASS_NOT_IN_BOOT_JAR:
057: name = "CLASS_NOT_IN_BOOT_JAR";
058: break;
059: case NonPortableReason.CLASS_NOT_INCLUDED_IN_CONFIG:
060: name = "CLASS_NOT_INCLUDED_IN_CONFIG";
061: break;
062: case NonPortableReason.SUPER_CLASS_NOT_INSTRUMENTED:
063: name = "SUPER_CLASS_NOT_INSTRUMENTED";
064: break;
065: default:
066: name = "UNDEFINED";
067: }
068: out.print(name);
069: }
070:
071: public void formatDetail(NonPortableDetail detail) {
072: details.add(detail);
073: if (detail.getLabel().length() > maxLabelLength) {
074: maxLabelLength = detail.getLabel().length();
075: }
076: }
077:
078: public void flush() {
079: for (Iterator i = details.iterator(); i.hasNext();) {
080: NonPortableDetail detail = (NonPortableDetail) i.next();
081: out.print(stringFormatter.rightPad(maxLabelLength, detail
082: .getLabel()));
083: out.print(separator);
084: out.print(detail.getValue());
085: if (i.hasNext()) {
086: out.println();
087: }
088: }
089: details.clear();
090: if (instructions != null) {
091: out.println();
092: out.print(instructions);
093: }
094: out.flush();
095: }
096:
097: public void formatReasonText(String reasonText) {
098: if (null == reasonText || 0 == reasonText.length())
099: return;
100:
101: out.println(paragraphFormatter.format(reasonText));
102: out.println();
103: out
104: .println("For more information on this issue, please visit our Troubleshooting Guide at:");
105: out.println("http://terracotta.org/kit/troubleshooting");
106: out.println();
107: }
108:
109: public void formatInstructionsText(String instructionsText) {
110: if (null == instructionsText || 0 == instructionsText.length()) {
111: instructions = null;
112: }
113:
114: instructions = instructionsText;
115: }
116:
117: }
|