KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jimm > datavision > layout > DocBookLE


1 package jimm.datavision.layout;
2 import jimm.datavision.*;
3 import jimm.datavision.field.*;
4 import jimm.util.StringUtils;
5 import java.io.*;
6 import java.util.*;
7
8 /**
9  * A DocBook col is used to represeent a column in a DocBook table.
10  *
11  * @author Jim Menard, <a HREF="mailto:jimm@io.com">jimm@io.com</a>
12  */

13 class DocBookCol {
14
15 Field field;
16 double x;
17 double width;
18
19 /**
20  * Constructor.
21  */

22 DocBookCol(Field field, double x, double width) {
23     this.field = field;
24     this.x = x;
25     this.width = width;
26 }
27
28 /**
29  * Writes this field's data into a DocBook table cell.
30  */

31 void output(PrintWriter out) {
32     if (field == null || !field.isVisible()) {
33     out.println("<entry></entry>");
34     return;
35     }
36
37     String JavaDoc str = field.toString();
38
39     Format format = field.getFormat();
40     out.print("<entry align=\"" + Format.alignToString(format.getAlign())
41           + "\">");
42
43 // if (format.size != 0) out.print("<font size=\" + format.size + \">");
44
if (format.isBold()) out.print("<emphasis role=\"bold\">");
45     if (format.isItalic()) out.print("<replaceable>");
46 // if (format.underline) out.print("\\underline{");
47

48     out.print(StringUtils.escapeXML(str));
49
50 // if (format.underline) out.print("}");
51
if (format.isItalic()) out.print("</replaceable>");
52     if (format.isBold()) out.print("</emphasis>");
53 // if (format.size != 0) out.print("}");
54

55     out.println("</entry>");
56 }
57
58 }
59
60 /**
61  * A DocBook layout engine creates DocBook documents. Field layout is
62  * achieved by creating tables.
63  */

64 public class DocBookLE extends SortedLayoutEngine {
65
66 protected HashMap sectionCols;
67
68 /**
69  * Constructor.
70  *
71  * @param out the output writer
72  */

73 public DocBookLE(PrintWriter out) {
74     super(out);
75 }
76
77 /**
78  * This override outputs information at the top of the DocBook document.
79  */

80 protected void doStart() {
81     sectionCols = new HashMap();
82     out.println("<!DOCTYPE informaltable PUBLIC \"-//OASIS//DTD DocBook V3.1//EN\">");
83     out.println("<!-- Generated by DataVision version " + info.Version
84         + " -->");
85     out.println("<!-- " + info.URL + " -->");
86     out.println("<informaltable colsep=\"1\" rowsep=\"1\">");
87 }
88
89 /**
90  * This override outputs the end of the document.
91  */

92 protected void doEnd() {
93     out.println("</informaltable>");
94 }
95
96 /**
97  * This override starts a new page.
98  */

99 protected void doStartPage() {
100     // Apparently beginpage isn't allowed just anywhere
101
if (pageNumber > 1)
102     out.println("<beginpage pagenum=\"" + pageNumber + "\">");
103     out.println("<!-- ======== Page " + pageNumber + " ======== -->");
104 }
105
106 /**
107  * This override outputs a report section.
108  *
109  * @param sect the report section
110  */

111 protected void doOutputSection(Section sect) {
112     Collection cols = calcSectionCols(sect);
113     if (cols.isEmpty())
114     return;
115
116     out.println("<tgroup cols=" + cols.size() + ">");
117
118     // Write col specs
119
int i = 1;
120     for (Iterator iter = cols.iterator(); iter.hasNext(); ++i) {
121     DocBookCol col = (DocBookCol)iter.next();
122     out.println("<colspec colname=c" + i + " colwidth=\""
123             + col.width + "\">");
124     }
125
126     // Output the fields in the section
127
out.println("<tbody>");
128     out.println("<row>");
129     for (Iterator iter = cols.iterator(); iter.hasNext(); ++i) {
130     DocBookCol col = (DocBookCol)iter.next();
131     col.output(out);
132     }
133     out.println("</row>");
134     out.println("</tbody>");
135
136     out.println("</tgroup>");
137 }
138
139 /**
140  * Does nothing, since we output fields in {@link #doOutputSection}.
141  */

142 protected void doOutputField(Field field) {}
143
144 /**
145  * Does nothing, since we output images in {@link #doOutputSection}.
146  */

147 protected void doOutputImage(ImageField image) {}
148
149 /**
150  * Does nothing, since we output lines in {@link #doOutputSection}.
151  */

152 protected void doOutputLine(Line line) {}
153
154 /**
155  * Returns a collection of <code>DocBookCol</code> objects. Each one
156  * represents a field that will be output.
157  *
158  * @param sect a section
159  */

160 protected Collection calcSectionCols(Section sect) {
161     Collection cols = null;
162     if ((cols = (Collection)sectionCols.get(sect)) != null)
163     return cols;
164
165     cols = new ArrayList();
166     double x = 0;
167 // FIX: sort these by their x position.
168
for (Iterator iter = sect.fields(); iter.hasNext(); ) {
169     Field f = (Field)iter.next();
170     Rectangle bounds = f.getBounds();
171     if (bounds.x > x) {
172         cols.add(new DocBookCol(null, x, bounds.x - x));
173         x = bounds.x;
174     }
175     cols.add(new DocBookCol(f, bounds.x, bounds.width));
176     x += bounds.width;
177     }
178
179     sectionCols.put(sect, cols);
180     return cols;
181 }
182
183 }
184
Popular Tags