KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > fop > render > xml > XMLRenderer


1 /*
2  * $Id: XMLRenderer.java,v 1.27.2.12 2003/03/02 16:55:18 pietsch Exp $
3  * ============================================================================
4  * The Apache Software License, Version 1.1
5  * ============================================================================
6  *
7  * Copyright (C) 1999-2003 The Apache Software Foundation. All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without modifica-
10  * tion, are permitted provided that the following conditions are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright notice,
13  * this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright notice,
16  * this list of conditions and the following disclaimer in the documentation
17  * and/or other materials provided with the distribution.
18  *
19  * 3. The end-user documentation included with the redistribution, if any, must
20  * include the following acknowledgment: "This product includes software
21  * developed by the Apache Software Foundation (http://www.apache.org/)."
22  * Alternately, this acknowledgment may appear in the software itself, if
23  * and wherever such third-party acknowledgments normally appear.
24  *
25  * 4. The names "FOP" and "Apache Software Foundation" must not be used to
26  * endorse or promote products derived from this software without prior
27  * written permission. For written permission, please contact
28  * apache@apache.org.
29  *
30  * 5. Products derived from this software may not be called "Apache", nor may
31  * "Apache" appear in their name, without prior written permission of the
32  * Apache Software Foundation.
33  *
34  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
35  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
36  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
37  * APACHE SOFTWARE FOUNDATION OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
38  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLU-
39  * DING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
40  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
41  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
42  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
43  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
44  * ============================================================================
45  *
46  * This software consists of voluntary contributions made by many individuals
47  * on behalf of the Apache Software Foundation and was originally created by
48  * James Tauber <jtauber@jtauber.com>. For more information on the Apache
49  * Software Foundation, please see <http://www.apache.org/>.
50  */

51 package org.apache.fop.render.xml;
52
53 // FOP
54
import org.apache.fop.svg.*;
55 import org.apache.fop.render.Renderer;
56 import org.apache.fop.image.FopImage;
57 import org.apache.fop.image.ImageArea;
58 import org.apache.fop.layout.*;
59 import org.apache.fop.layout.inline.*;
60 import org.apache.fop.fo.properties.LeaderPattern;
61 import org.apache.fop.apps.FOPException;
62 import org.apache.fop.datatypes.IDReferences;
63
64 // Avalon
65
import org.apache.avalon.framework.logger.Logger;
66
67 // Java
68
import java.io.IOException JavaDoc;
69 import java.io.PrintWriter JavaDoc;
70 import java.io.OutputStream JavaDoc;
71 import java.util.List JavaDoc;
72
73 /**
74  * Renderer that renders areas to XML for debugging purposes.
75  *
76  * Modified by Mark Lillywhite mark-fop@inomial.com to use the
77  * new renderer interface. Not 100% certain that this is correct.
78  */

79 public class XMLRenderer implements Renderer {
80
81     protected Logger log;
82
83     public void setLogger(Logger logger) {
84         log = logger;
85     }
86
87     /**
88      * indentation to use for pretty-printing the XML
89      */

90     protected int indent = 0;
91
92     /**
93      * the application producing the XML
94      */

95     protected String JavaDoc producer;
96
97     /**
98      * the writer used to output the XML
99      */

100     protected PrintWriter JavaDoc writer;
101
102     /**
103      * options
104      */

105     protected java.util.Map JavaDoc options;
106     private boolean consistentOutput = false;
107
108     protected IDReferences idReferences;
109
110     public XMLRenderer() {}
111
112     /**
113      * set up renderer options
114      */

115     public void setOptions(java.util.Map JavaDoc options) {
116         this.options = options;
117         Boolean JavaDoc con = (Boolean JavaDoc)options.get("consistentOutput");
118         if(con != null) {
119             consistentOutput = con.booleanValue();
120         }
121     }
122
123     /**
124      * set the document's producer
125      *
126      * @param producer string indicating application producing the XML
127      */

128     public void setProducer(String JavaDoc producer) {
129         this.producer = producer;
130     }
131
132
133     public void render(Page page, OutputStream JavaDoc outputStream)
134     throws IOException JavaDoc {
135         idReferences = page.getIDReferences();
136         this.renderPage(page);
137     }
138
139     /**
140      * write out spaces to make indent
141      */

142     protected void writeIndent() {
143         StringBuffer JavaDoc s = new StringBuffer JavaDoc();
144         for (int i = 0; i < this.indent; i++) {
145             s = s.append(" ");
146         }
147         this.writer.write(s.toString());
148     }
149
150     /**
151      * write out an element
152      *
153      * @param element the full text of the element including tags
154      */

155     protected void writeElement(String JavaDoc element) {
156         writeIndent();
157         this.writer.write(element + "\n");
158     }
159
160     /**
161      * write out an empty-element-tag
162      *
163      * @param tag the text of the tag
164      */

165     protected void writeEmptyElementTag(String JavaDoc tag) {
166         writeIndent();
167         this.writer.write(tag + "\n");
168     }
169
170     /**
171      * write out an end tag
172      *
173      * @param tag the text of the tag
174      */

175     protected void writeEndTag(String JavaDoc tag) {
176         this.indent--;
177         writeIndent();
178         this.writer.write(tag + "\n");
179     }
180
181     /**
182      * write out a start tag
183      *
184      * @param tag the text of the tag
185      */

186     protected void writeStartTag(String JavaDoc tag) {
187         writeIndent();
188         this.writer.write(tag + "\n");
189         this.indent++;
190     }
191
192     /**
193      * set up the font info
194      *
195      * @param fontInfo the font info object to set up
196      */

197     public void setupFontInfo(FontInfo fontInfo) throws FOPException {
198
199         /* use PDF's font setup to get PDF metrics */
200         org.apache.fop.render.pdf.FontSetup.setup(fontInfo);
201     }
202
203     /**
204      * Renders an image, scaling it to the given width and height.
205      * If the scaled width and height is the same intrinsic size
206      * of the image, the image is not scaled.
207      *
208      * @param x the x position of left edge in millipoints
209      * @param y the y position of top edge in millipoints
210      * @param w the width in millipoints
211      * @param h the height in millipoints
212      * @param image the image to be rendered
213      * @param fs the font state to use when rendering text
214      * in non-bitmapped images.
215      */

216     protected void drawImageScaled(int x, int y, int w, int h,
217                    FopImage image,
218                    FontState fs) {
219     // XXX: implement this
220
}
221
222     /**
223      * Renders an image, clipping it as specified.
224      *
225      * @param x the x position of left edge in millipoints.
226      * @param y the y position of top edge in millipoints.
227      * @param clipX the left edge of the clip in millipoints
228      * @param clipY the top edge of the clip in millipoints
229      * @param clipW the clip width in millipoints
230      * @param clipH the clip height in millipoints
231      * @param fill the image to be rendered
232      * @param fs the font state to use when rendering text
233      * in non-bitmapped images.
234      */

235     protected void drawImageClipped(int x, int y,
236                     int clipX, int clipY,
237                     int clipW, int clipH,
238                     FopImage image,
239                     FontState fs) {
240     // XXX: implement this
241
}
242
243     /**
244      * render an area container to XML
245      *
246      * @param area the area container to render
247      */

248     public void renderAreaContainer(AreaContainer area) {
249         writeStartTag("<AreaContainer name=\"" + area.getAreaName() + "\">");
250         List JavaDoc children = area.getChildren();
251         for (int i = 0; i < children.size(); i++) {
252             Box b = (Box)children.get(i);
253             b.render(this);
254         }
255         writeEndTag("</AreaContainer>");
256     }
257
258     /**
259      * render a body area container to XML
260      *
261      * @param area the body area container to render
262      */

263     public void renderBodyAreaContainer(BodyAreaContainer area) {
264         writeStartTag("<BodyAreaContainer>");
265         List JavaDoc children = area.getChildren();
266         for (int i = 0; i < children.size(); i++) {
267             Box b = (Box)children.get(i);
268             b.render(this);
269         }
270         writeEndTag("</BodyAreaContainer>");
271     }
272
273     /**
274      * render a region area container to XML
275      *
276      * @param area the region area container to render
277      */

278     public void renderRegionAreaContainer(AreaContainer area) {
279         renderAreaContainer(area);
280     }
281
282     /**
283      * render a span area to XML
284      *
285      * @param area the span area to render
286      */

287     public void renderSpanArea(SpanArea area) {
288         writeStartTag("<SpanArea>");
289         List JavaDoc children = area.getChildren();
290         for (int i = 0; i < children.size(); i++) {
291             Box b = (Box)children.get(i);
292             b.render(this);
293         }
294         writeEndTag("</SpanArea>");
295     }
296
297     /**
298      * render a block area to XML
299      *
300      * @param area the block area to render
301      */

302     public void renderBlockArea(BlockArea area) {
303         StringBuffer JavaDoc baText = new StringBuffer JavaDoc();
304         baText.append("<BlockArea start-indent=\"" + area.getStartIndent()
305                       + "\"");
306         baText.append(" end-indent=\"" + area.getEndIndent() + "\"");
307         baText.append("\nis-first=\"" + area.isFirst() + "\"");
308         baText.append(" is-last=\"" + area.isLast() + "\"");
309         if (null != area.getGeneratedBy())
310             baText.append(" generated-by=\""
311                           + area.getGeneratedBy().getName() + "//");
312             if(consistentOutput) {
313                 baText.append(area.getGeneratedBy().getClass() + "\"");
314             } else {
315                 baText.append(area.getGeneratedBy() + "\"");
316             }
317         baText.append(">");
318         writeStartTag(baText.toString());
319
320         // write out marker info
321
// List markers = area.getMarkers();
322
// if (!markers.isEmpty()) {
323
// writeStartTag("<Markers>");
324
// for (int i = 0; i < markers.size(); i++) {
325
// org.apache.fop.fo.flow.Marker marker =
326
// (org.apache.fop.fo.flow.Marker)markers.get(i);
327
// StringBuffer maText = new StringBuffer();
328
// maText.append("<Marker marker-class-name=\""
329
// + marker.getMarkerClassName() + "\"");
330
// maText.append(" RegisteredArea=\"" + marker.getRegistryArea()
331
// + "\"");
332
// maText.append("/>");
333
// writeEmptyElementTag(maText.toString());
334
// }
335
// writeEndTag("</Markers>");
336
// }
337

338         List JavaDoc children = area.getChildren();
339         for (int i = 0; i < children.size(); i++) {
340             Box b = (Box)children.get(i);
341             b.render(this);
342         }
343         writeEndTag("</BlockArea>");
344     }
345
346     public void renderInlineArea(InlineArea area) {
347         StringBuffer JavaDoc iaText = new StringBuffer JavaDoc();
348         iaText.append("<InlineArea");
349         iaText.append("\nis-first=\"" + area.isFirst() + "\"");
350         iaText.append(" is-last=\"" + area.isLast() + "\"");
351         if (null != area.getGeneratedBy())
352             iaText.append(" generated-by=\""
353                           + area.getGeneratedBy().getName() + "//"
354                           + area.getGeneratedBy() + "\"");
355         iaText.append(">");
356         writeStartTag(iaText.toString());
357
358         // write out marker info
359
// List markers = area.getMarkers();
360
// if (!markers.isEmpty()) {
361
// writeStartTag("<Markers>");
362
// for (int i = 0; i < markers.size(); i++) {
363
// org.apache.fop.fo.flow.Marker marker =
364
// (org.apache.fop.fo.flow.Marker)markers.get(i);
365
// StringBuffer maText = new StringBuffer();
366
// maText.append("<Marker marker-class-name=\""
367
// + marker.getMarkerClassName() + "\"");
368
// maText.append(" RegisteredArea=\"" + marker.getRegistryArea()
369
// + "\"");
370
// maText.append("/>");
371
// writeEmptyElementTag(maText.toString());
372
// }
373
// writeEndTag("</Markers>");
374
// }
375

376         List JavaDoc children = area.getChildren();
377         for (int i = 0; i < children.size(); i++) {
378             Box b = (Box)children.get(i);
379             b.render(this);
380         }
381         writeEndTag("</InlineArea>");
382     }
383
384     /**
385      * render a display space to XML
386      *
387      * @param space the space to render
388      */

389     public void renderDisplaySpace(DisplaySpace space) {
390         if (!isCoarseXml())
391             writeEmptyElementTag("<DisplaySpace size=\"" + space.getSize()
392                                  + "\"/>");
393     }
394
395     /**
396      * render a foreign object area
397      */

398     public void renderForeignObjectArea(ForeignObjectArea area) {
399         // if necessary need to scale and align the content
400
area.getObject().render(this);
401     }
402
403     /**
404      * render an SVG area to XML
405      *
406      * @param area the area to render
407      */

408     public void renderSVGArea(SVGArea area) {
409         writeEmptyElementTag("<SVG/>");
410     }
411
412     /**
413      * render an image area to XML
414      *
415      * @param area the area to render
416      */

417     public void renderImageArea(ImageArea area) {
418         writeEmptyElementTag("<ImageArea/>");
419     }
420
421     /**
422      * render an inline area to XML
423      *
424      * @param area the area to render
425      */

426     public void renderWordArea(WordArea area) {
427         String JavaDoc fontWeight = area.getFontState().getFontWeight();
428         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
429         String JavaDoc s = area.getText();
430         int l = s.length();
431         for (int i = 0; i < l; i++) {
432             char ch = s.charAt(i);
433             if (ch > 127)
434                 sb = sb.append("&#" + (int)ch + ";");
435             else
436                 sb = sb.append(ch);
437         }
438         if (!isCoarseXml()) {
439             writeElement("<WordArea font-weight=\"" + fontWeight
440                          + "\" red=\"" + area.getRed() + "\" green=\""
441                          + area.getGreen() + "\" blue=\"" + area.getBlue()
442                          + "\" width=\"" + area.getContentWidth() + "\">"
443                          + sb.toString() + "</WordArea>");
444         } else {
445             this.writer.write(sb.toString());
446         }
447     }
448
449     /**
450      * render an inline space to XML
451      *
452      * @param space the space to render
453      */

454     public void renderInlineSpace(InlineSpace space) {
455         if (!isCoarseXml())
456             writeEmptyElementTag("<InlineSpace size=\"" + space.getSize()
457                                  + "\"/>");
458         else
459             this.writer.write(" ");
460     }
461
462     /**
463      * render a line area to XML
464      *
465      * @param area the area to render
466      */

467     public void renderLineArea(LineArea area) {
468         if (!isCoarseXml()) {
469             String JavaDoc fontWeight = area.getFontState().getFontWeight();
470             writeStartTag("<LineArea font-weight=\"" + fontWeight + "\">");
471         }
472         List JavaDoc children = area.getChildren();
473         for (int i = 0; i < children.size(); i++) {
474             Box b = (Box)children.get(i);
475             b.render(this);
476         }
477         if (!isCoarseXml())
478             writeEndTag("</LineArea>");
479         else
480             this.writer.write("\n");
481     }
482
483     /**
484      * render a page to XML
485      *
486      * @param page the page to render
487      */

488     public void renderPage(Page page) {
489         BodyAreaContainer body;
490         AreaContainer before, after, start, end;
491         writeStartTag("<Page number=\"" + page.getFormattedNumber() + "\">");
492         body = page.getBody();
493         before = page.getBefore();
494         after = page.getAfter();
495         start = page.getStart();
496         end = page.getEnd();
497         if (before != null) {
498             before.render(this);
499         }
500         if (after != null) {
501             after.render(this);
502         }
503         if (start != null) {
504             start.render(this);
505         }
506         if (end != null) {
507             end.render(this);
508         }
509         if (body != null) {
510             body.render(this);
511         }
512         writeEndTag("</Page>");
513     }
514
515     /**
516      * render a leader area to XML
517      *
518      * @param area the area to render
519      */

520     public void renderLeaderArea(LeaderArea area) {
521         if (isCoarseXml())
522             return;
523         String JavaDoc leaderPattern = "";
524         switch (area.getLeaderPattern()) {
525         case LeaderPattern.SPACE:
526             leaderPattern = "space";
527             break;
528         case LeaderPattern.RULE:
529             leaderPattern = "rule";
530             break;
531         case LeaderPattern.DOTS:
532             leaderPattern = "dots";
533             break;
534         case LeaderPattern.USECONTENT:
535             leaderPattern = "use-content";
536             break;
537         }
538
539         writeEmptyElementTag("<Leader leader-pattern=\"" + leaderPattern
540                              + " leader-length=\"" + area.getLeaderLength()
541                              + "\" rule-thickness=\""
542                              + area.getRuleThickness() + "\" rule-style=\""
543                              + area.getRuleStyle() + "\" red=\""
544                              + area.getRed() + "\" green=\""
545                              + area.getGreen() + "\" blue=\""
546                              + area.getBlue() + "\"/>");
547     }
548
549     private boolean isCoarseXml() {
550         if (options == null) {
551             return false;
552         }
553         Boolean JavaDoc b = (Boolean JavaDoc)options.get("fineDetail");
554         if (b != null) {
555             return b.booleanValue();
556         } else {
557             return false;
558         }
559     }
560
561     /**
562       Default start renderer method. This would
563       normally be overridden. (mark-fop@inomial.com).
564     */

565     public void startRenderer(OutputStream JavaDoc outputStream)
566     throws IOException JavaDoc {
567         log.debug("rendering areas to XML");
568         this.writer = new PrintWriter JavaDoc(outputStream);
569         this.writer.write( "<?xml version=\"1.0\"?>\n<!-- produced by " +
570                            this.producer + " -->\n");
571         writeStartTag("<AreaTree>");
572     }
573
574     /**
575       Default stop renderer method. This would
576       normally be overridden. (mark-fop@inomial.com).
577     */

578     public void stopRenderer(OutputStream JavaDoc outputStream)
579     throws IOException JavaDoc {
580         writeEndTag("</AreaTree>");
581         this.writer.flush();
582         log.debug("written out XML");
583     }
584
585     public IDReferences getIDReferences() {
586         return idReferences;
587     }
588 }
589
Popular Tags