1 /***
2 * TextFormatter.java
3 *
4 * Project: Dependency Tool
5 *
6 * WHEN WHO WHAT
7 * 06.06.2003 pko initial public release
8 * 29.10.2002 pko creation
9 *
10 * Copyright 2003 ELCA Informatique SA
11 * Av. de la Harpe 22-24, 1000 Lausanne 13, Switzerland
12 * www.elca.ch
13 *
14 * This library is free software; you can redistribute it and/or
15 * modify it under the terms of the GNU Lesser General Public License
16 * as published by the Free Software Foundation; either version 2.1 of
17 * the License, or (at your option) any later version.
18 *
19 * This library is distributed in the hope that it will be useful, but
20 * WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22 * Lesser General Public License for more details.
23 *
24 * You should have received a copy of the GNU Lesser General Public
25 * License along with this library; if not, write to the Free Software
26 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
27 * USA
28 */
29
30 package ch.elca.dependency.report;
31
32 import java.util.Enumeration;
33 import javax.swing.tree.DefaultMutableTreeNode;
34
35 import org.apache.log4j.Logger;
36
37 /***
38 * <code>TextFormatter</code> formats a Report as plain text.
39 *
40 * @author Pawel Kowalski
41 * @version 1.0-beta
42 */
43 public class TextFormatter extends ReportFormatter {
44
45 /***
46 * Log4j Logger.
47 */
48 private final static Logger LOG = Logger.getLogger(TextFormatter.class);
49
50 /***
51 * Logger message: <code>INVALID_REPORT_OBJECT</code>.
52 */
53 private final static String INVALID_REPORT_OBJECT = "invalid report object";
54
55 /***
56 * A <code>NEWLINE</code> String.
57 */
58 private static final String NEWLINE = System.getProperty("line.separator");
59
60 /***
61 * A <code>SEPARATOR</code> String.
62 */
63 private static final String SEPARATOR = ": ";
64
65 /***
66 * An <code>INDENT</code> String.
67 */
68 private static final String INDENT = " ";
69
70 /***
71 * A <code>HEADER_DISTINGUISHER</code> String.
72 */
73 private static final String HEADER_DISTINGUISHER = "=";
74
75 /***
76 * The <code>HEADERLINE_LENGTH</code>.
77 */
78 private static final int HEADERLINE_LENGTH = 70;
79
80 //*********************************************************************************/
81 // format method
82 //*********************************************************************************/
83
84 /***
85 * Method to format an information tree as a String.
86 *
87 * @param rootNode a <code>DefaultMutableTreeNode</code> value
88 * @return a <code>String</code> value
89 */
90 String format(DefaultMutableTreeNode rootNode) {
91
92 StringBuffer buf = new StringBuffer();
93 DefaultMutableTreeNode treeNode = null;
94 ReportObject reportObject = null;
95
96 for (Enumeration enum = rootNode.preorderEnumeration();
97 enum.hasMoreElements();) {
98 treeNode = (DefaultMutableTreeNode)enum.nextElement();
99 if (treeNode.getUserObject() instanceof ReportObject) {
100 reportObject = (ReportObject)treeNode.getUserObject();
101 if (reportObject.get(Report.IS_HEADER) == Report.IS_HEADER) {
102 buf.append(NEWLINE).append(getIndent(reportObject))
103 .append(getHeaderLine(reportObject)).append(NEWLINE);
104 }
105 if (reportObject.isNodeNamePrintable()) {
106 buf.append(getIndent(reportObject))
107 .append(reportObject.getNodeName());
108 }
109
110 if (!reportObject.isNodeNamePrintable()) {
111 buf.append(getIndent(reportObject));
112 } else {
113 buf.append(SEPARATOR);
114 }
115 buf.append(reportObject.getNodeValue()).append(NEWLINE);
116
117
118 if (reportObject.get(Report.IS_HEADER) == Report.IS_HEADER) {
119 buf.append(getIndent(reportObject))
120 .append(getHeaderLine(reportObject)).append(NEWLINE);
121 }
122 } else {
123 LOG.warn(INVALID_REPORT_OBJECT);
124 }
125 }
126 return buf.toString();
127 }
128
129 //****************************************************************************************/
130 // private helper methods
131 //****************************************************************************************/
132
133 /***
134 * Get the indent for a <code>ReportObject</code>.
135 *
136 * @param reportObject a <code>ReportObject</code> value
137 * @return a <code>String</code> value
138 */
139 private String getIndent(ReportObject reportObject) {
140 Object object = reportObject.get(Report.INDENT);
141 if (!(object instanceof Integer)) {
142 return "";
143 }
144 StringBuffer buf = new StringBuffer();
145 for (int i = 0; i < ((Integer)object).intValue(); i++) {
146 buf.append(INDENT);
147 }
148 return buf.toString();
149 }
150
151 /***
152 * Get the header line for a <code>ReportObject</code>.
153 *
154 * @param reportObject a <code>ReportObject</code> value
155 * @return a <code>String</code> value
156 */
157 private String getHeaderLine(ReportObject reportObject) {
158 Object object = reportObject.get(Report.INDENT);
159 StringBuffer buf = new StringBuffer();
160 if (!(object instanceof Integer)) {
161 for (int i = 0; i < HEADERLINE_LENGTH; i++) {
162 buf.append(HEADER_DISTINGUISHER);
163 }
164 return buf.toString();
165 }
166 int indent = ((Integer)object).intValue();
167 for (int i = 0; i < HEADERLINE_LENGTH - indent * INDENT.length(); i++) {
168 buf.append(HEADER_DISTINGUISHER);
169 }
170 return buf.toString();
171 }
172 }
This page was automatically generated by Maven