001: /**
002: * ===========================================
003: * JFreeReport : a free Java reporting library
004: * ===========================================
005: *
006: * Project Info: http://reporting.pentaho.org/
007: *
008: * (C) Copyright 2001-2007, by Object Refinery Ltd, Pentaho Corporation and Contributors.
009: *
010: * This library is free software; you can redistribute it and/or modify it under the terms
011: * of the GNU Lesser General Public License as published by the Free Software Foundation;
012: * either version 2.1 of the License, or (at your option) any later version.
013: *
014: * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
015: * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
016: * See the GNU Lesser General Public License for more details.
017: *
018: * You should have received a copy of the GNU Lesser General Public License along with this
019: * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
020: * Boston, MA 02111-1307, USA.
021: *
022: * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
023: * in the United States and other countries.]
024: *
025: * ------------
026: * FileModelPrinter.java
027: * ------------
028: * (C) Copyright 2001-2007, by Object Refinery Ltd, Pentaho Corporation and Contributors.
029: */package org.jfree.report.layout;
030:
031: import java.io.File;
032: import java.io.FileWriter;
033: import java.io.BufferedWriter;
034: import java.io.Writer;
035: import java.io.IOException;
036:
037: import org.jfree.report.layout.model.RenderNode;
038: import org.jfree.report.layout.model.RenderBox;
039: import org.jfree.report.layout.model.LogicalPageBox;
040: import org.jfree.report.layout.model.ParagraphRenderBox;
041: import org.jfree.report.layout.model.RenderableText;
042: import org.jfree.report.layout.model.FinishedRenderNode;
043:
044: /**
045: * Creation-Date: Jan 9, 2007, 2:22:59 PM
046: *
047: * @author Thomas Morgner
048: */
049: public class FileModelPrinter {
050: private static final boolean PRINT_LINEBOX_CONTENTS = false;
051:
052: private FileModelPrinter() {
053: }
054:
055: public static void print(final RenderBox box) {
056: try {
057: final File file = File
058: .createTempFile("model-dump-", ".txt");
059: final BufferedWriter writer = new BufferedWriter(
060: new FileWriter(file));
061: printBox(writer, box, 0);
062: writer.close();
063: } catch (Exception e) {
064: throw new Error();
065: }
066: }
067:
068: public static void printBox(final Writer writer,
069: final RenderBox box, final int level) throws Exception {
070: StringBuffer b = new StringBuffer();
071: for (int i = 0; i < level; i++) {
072: b.append(" ");
073: }
074: b.append(box.getClass().getName());
075: b.append('[');
076: //b.append(Integer.toHexString(System.identityHashCode(box)));
077: b.append(';');
078: b.append(box.getName());
079: b.append(']');
080: b.append("={stateKey=");
081: b.append(box.getStateKey());
082: b.append(", x=");
083: b.append(box.getX());
084: b.append(", y=");
085: b.append(box.getY());
086: b.append(", width=");
087: b.append(box.getWidth());
088: b.append(", height=");
089: b.append(box.getHeight());
090: b.append(", computed-x=");
091: b.append(box.getComputedX());
092: b.append(", computed-width=");
093: b.append(box.getComputedWidth());
094: b.append(", cached-x=");
095: b.append(box.getCachedX());
096: b.append(", cached-y=");
097: b.append(box.getCachedY());
098: b.append(", cached-width=");
099: b.append(box.getCachedWidth());
100: b.append(", cached-height=");
101: b.append(box.getCachedHeight());
102: b.append('}');
103: writer.write(b.toString());
104: writer.write("\n");
105:
106: b = new StringBuffer();
107: for (int i = 0; i < level; i++) {
108: b.append(" ");
109: }
110: b.append("- boxDefinition=");
111: b.append(box.getBoxDefinition());
112: writer.write(b.toString());
113: writer.write("\n");
114:
115: b = new StringBuffer();
116: for (int i = 0; i < level; i++) {
117: b.append(" ");
118: }
119: b.append("- nodeLayoutProperties=");
120: b.append(box.getNodeLayoutProperties());
121: writer.write(b.toString());
122: writer.write("\n");
123:
124: b = new StringBuffer();
125: for (int i = 0; i < level; i++) {
126: b.append(" ");
127: }
128: b.append("- staticBoxLayoutProperties=");
129: b.append(box.getStaticBoxLayoutProperties());
130: writer.write(b.toString());
131: writer.write("\n");
132:
133: if (box instanceof LogicalPageBox) {
134: final LogicalPageBox pageBox = (LogicalPageBox) box;
135: b = new StringBuffer();
136: for (int i = 0; i < level; i++) {
137: b.append(" ");
138: }
139: b.append("- PageBox={PageOffset=");
140: b.append(pageBox.getPageOffset());
141: b.append(", PageHeight=");
142: b.append(pageBox.getPageHeight());
143: b.append(", PageEnd=");
144: b.append(pageBox.getPageEnd());
145: b.append('}');
146: writer.write(b.toString());
147: writer.write("\n");
148: }
149:
150: if (box.isOpen()) {
151: b = new StringBuffer();
152: for (int i = 0; i < level; i++) {
153: b.append(" ");
154: }
155: b.append("- WARNING: THIS BOX IS STILL OPEN");
156: writer.write(b.toString());
157: writer.write("\n");
158: }
159:
160: if (box.isFinished()) {
161: b = new StringBuffer();
162: for (int i = 0; i < level; i++) {
163: b.append(" ");
164: }
165: b.append("- INFO: THIS BOX IS FINISHED");
166: writer.write(b.toString());
167: writer.write("\n");
168: }
169: if (box.isCommited()) {
170: b = new StringBuffer();
171: for (int i = 0; i < level; i++) {
172: b.append(" ");
173: }
174: b.append("- INFO: THIS BOX IS COMMITED");
175: writer.write(b.toString());
176: writer.write("\n");
177: }
178:
179: b = new StringBuffer();
180: for (int i = 0; i < level; i++) {
181: b.append(" ");
182: }
183: writer.write(b.toString());
184: writer.write("\n");
185:
186: if (box instanceof ParagraphRenderBox) {
187: if (PRINT_LINEBOX_CONTENTS) {
188: final ParagraphRenderBox paraBox = (ParagraphRenderBox) box;
189: if (paraBox.isComplexParagraph()) {
190: writer
191: .write("---------------- START PARAGRAPH LINEBOX CONTAINER -------------------------------------");
192: writer.write("\n");
193: printBox(writer, paraBox.getLineboxContainer(),
194: level + 1);
195: writer
196: .write("---------------- FINISH PARAGRAPH LINEBOX CONTAINER -------------------------------------");
197: writer.write("\n");
198: }
199: }
200: }
201:
202: if (box instanceof LogicalPageBox) {
203: final LogicalPageBox lbox = (LogicalPageBox) box;
204: printBox(writer, lbox.getHeaderArea(), level + 1);
205: printBox(writer, lbox.getWatermarkArea(), level + 1);
206: }
207: printChilds(writer, box, level);
208: if (box instanceof LogicalPageBox) {
209: final LogicalPageBox lbox = (LogicalPageBox) box;
210: printBox(writer, lbox.getFooterArea(), level + 1);
211: }
212: }
213:
214: private static void printChilds(final Writer writer,
215: final RenderBox box, final int level) throws Exception {
216: RenderNode childs = box.getFirstChild();
217: while (childs != null) {
218: if (childs instanceof RenderBox) {
219: printBox(writer, (RenderBox) childs, level + 1);
220: } else if (childs instanceof RenderableText) {
221: printText(writer, (RenderableText) childs, level + 1);
222: } else {
223: printNode(writer, childs, level + 1);
224: }
225: childs = childs.getNext();
226: }
227: }
228:
229: private static void printNode(final Writer writer,
230: final RenderNode node, final int level) throws IOException {
231: StringBuffer b = new StringBuffer();
232: for (int i = 0; i < level; i++) {
233: b.append(" ");
234: }
235: b.append(node.getClass().getName());
236: b.append('[');
237: //b.append(Integer.toHexString(System.identityHashCode(node)));
238: b.append(']');
239: b.append("={x=");
240: b.append(node.getX());
241: b.append(", y=");
242: b.append(node.getY());
243: b.append(", width=");
244: b.append(node.getWidth());
245: b.append(", height=");
246: b.append(node.getHeight());
247: b.append(", computed-x=");
248: b.append(node.getComputedX());
249: b.append(", computed-width=");
250: b.append(node.getComputedWidth());
251:
252: if (node instanceof FinishedRenderNode) {
253: final FinishedRenderNode fn = (FinishedRenderNode) node;
254: b.append(", layouted-width=");
255: b.append(fn.getLayoutedWidth());
256: b.append(", layouted-height=");
257: b.append(fn.getLayoutedHeight());
258: }
259: b.append('}');
260: writer.write(b.toString());
261: writer.write("\n");
262:
263: b = new StringBuffer();
264: for (int i = 0; i < level; i++) {
265: b.append(" ");
266: }
267: b.append("- nodeLayoutProperties=");
268: b.append(node.getNodeLayoutProperties());
269: writer.write(b.toString());
270: writer.write("\n");
271: }
272:
273: private static void printText(final Writer writer,
274: final RenderableText text, final int level)
275: throws IOException {
276: StringBuffer b = new StringBuffer();
277: for (int i = 0; i < level; i++) {
278: b.append(" ");
279: }
280: b.append("Text");
281: b.append('[');
282: //b.append(Integer.toHexString(System.identityHashCode(text)));
283: b.append(']');
284: b.append("={x=");
285: b.append(text.getX());
286: b.append(", y=");
287: b.append(text.getY());
288: b.append(", width=");
289: b.append(text.getWidth());
290: b.append(", height=");
291: b.append(text.getHeight());
292: b.append(", computed-x=");
293: b.append(text.getComputedX());
294: b.append(", computed-width=");
295: b.append(text.getComputedWidth());
296: b.append(", text='");
297: b.append(text.getRawText());
298: b.append("'}");
299: writer.write(b.toString());
300: writer.write("\n");
301:
302: b = new StringBuffer();
303: for (int i = 0; i < level; i++) {
304: b.append(" ");
305: }
306: b.append("- nodeLayoutProperties=");
307: b.append(text.getNodeLayoutProperties());
308: writer.write(b.toString());
309: writer.write("\n");
310: }
311:
312: }
|