You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

XMLRenderer.java 9.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602
  1. /*-- $Id$ --
  2. ============================================================================
  3. The Apache Software License, Version 1.1
  4. ============================================================================
  5. Copyright (C) 1999 The Apache Software Foundation. All rights reserved.
  6. Redistribution and use in source and binary forms, with or without modifica-
  7. tion, are permitted provided that the following conditions are met:
  8. 1. Redistributions of source code must retain the above copyright notice,
  9. this list of conditions and the following disclaimer.
  10. 2. Redistributions in binary form must reproduce the above copyright notice,
  11. this list of conditions and the following disclaimer in the documentation
  12. and/or other materials provided with the distribution.
  13. 3. The end-user documentation included with the redistribution, if any, must
  14. include the following acknowledgment: "This product includes software
  15. developed by the Apache Software Foundation (http://www.apache.org/)."
  16. Alternately, this acknowledgment may appear in the software itself, if
  17. and wherever such third-party acknowledgments normally appear.
  18. 4. The names "Fop" and "Apache Software Foundation" must not be used to
  19. endorse or promote products derived from this software without prior
  20. written permission. For written permission, please contact
  21. apache@apache.org.
  22. 5. Products derived from this software may not be called "Apache", nor may
  23. "Apache" appear in their name, without prior written permission of the
  24. Apache Software Foundation.
  25. THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
  26. INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
  27. FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  28. APACHE SOFTWARE FOUNDATION OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
  29. INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLU-
  30. DING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  31. OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  32. ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  33. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  34. THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  35. This software consists of voluntary contributions made by many individuals
  36. on behalf of the Apache Software Foundation and was originally created by
  37. James Tauber <jtauber@jtauber.com>. For more information on the Apache
  38. Software Foundation, please see <http://www.apache.org/>.
  39. */
  40. package org.apache.fop.render.xml;
  41. // FOP
  42. import org.apache.fop.svg.*;
  43. import org.apache.fop.render.Renderer;
  44. import org.apache.fop.image.ImageArea;
  45. import org.apache.fop.layout.*;
  46. import org.apache.fop.pdf.*;
  47. // Java
  48. import java.io.IOException;
  49. import java.io.PrintWriter;
  50. import java.util.Enumeration;
  51. /**
  52. * Renderer that renders areas to XML for debugging purposes.
  53. */
  54. public class XMLRenderer implements Renderer {
  55. /** indentation to use for pretty-printing the XML */
  56. protected int indent = 0;
  57. /** the application producing the XML */
  58. protected String producer;
  59. /** the writer used to output the XML */
  60. protected PrintWriter writer;
  61. /**
  62. * set the document's producer
  63. *
  64. * @param producer string indicating application producing the XML
  65. */
  66. public void setProducer(String producer) {
  67. this.producer = producer;
  68. }
  69. /**
  70. * render the areas into XML
  71. *
  72. * @param areaTree the laid-out area tree
  73. * @param writer the PrintWriter to give the XML to
  74. */
  75. public void render(AreaTree areaTree, PrintWriter writer)
  76. throws IOException {
  77. System.err.println("rendering areas to XML");
  78. this.writer = writer;
  79. this.writer.write("<?xml version=\"1.0\"?>\n<!-- produced by "
  80. + this.producer + " -->\n");
  81. writeStartTag("<AreaTree>");
  82. Enumeration e = areaTree.getPages().elements();
  83. while (e.hasMoreElements()) {
  84. this.renderPage((Page) e.nextElement());
  85. }
  86. writeEndTag("</AreaTree>");
  87. this.writer.flush();
  88. System.err.println("written out XML");
  89. }
  90. /**
  91. * write out spaces to make indent
  92. */
  93. protected void writeIndent() {
  94. StringBuffer s = new StringBuffer();
  95. for (int i= 0; i<this.indent; i++) {
  96. s = s.append(" ");
  97. }
  98. this.writer.write(s.toString());
  99. }
  100. /**
  101. * write out an element
  102. *
  103. * @param element the full text of the element including tags
  104. */
  105. protected void writeElement(String element) {
  106. writeIndent();
  107. this.writer.write(element+"\n");
  108. }
  109. /**
  110. * write out an empty-element-tag
  111. *
  112. * @param tag the text of the tag
  113. */
  114. protected void writeEmptyElementTag(String tag) {
  115. writeIndent();
  116. this.writer.write(tag + "\n");
  117. }
  118. /**
  119. * write out an end tag
  120. *
  121. * @param tag the text of the tag
  122. */
  123. protected void writeEndTag(String tag) {
  124. this.indent--;
  125. writeIndent();
  126. this.writer.write(tag + "\n");
  127. }
  128. /**
  129. * write out a start tag
  130. *
  131. * @param tag the text of the tag
  132. */
  133. protected void writeStartTag(String tag) {
  134. writeIndent();
  135. this.writer.write(tag + "\n");
  136. this.indent++;
  137. }
  138. /**
  139. * set up the font info
  140. *
  141. * @param fontInfo the font info object to set up
  142. */
  143. public void setupFontInfo(FontInfo fontInfo) {
  144. /* use PDF's font setup to get PDF metrics */
  145. org.apache.fop.render.pdf.FontSetup.setup(fontInfo);
  146. }
  147. /**
  148. * render an area container to XML
  149. *
  150. * @param area the area container to render
  151. */
  152. public void renderAreaContainer(AreaContainer area) {
  153. writeStartTag("<AreaContainer>");
  154. Enumeration e = area.getChildren().elements();
  155. while (e.hasMoreElements()) {
  156. Box b = (Box) e.nextElement();
  157. b.render(this);
  158. }
  159. writeEndTag("</AreaContainer>");
  160. }
  161. /**
  162. * render a block area to XML
  163. *
  164. * @param area the block area to render
  165. */
  166. public void renderBlockArea(BlockArea area) {
  167. writeStartTag("<BlockArea start-indent=\""
  168. + area.getStartIndent()
  169. + "\" end-indent=\""
  170. + area.getEndIndent() + "\">");
  171. Enumeration e = area.getChildren().elements();
  172. while (e.hasMoreElements()) {
  173. Box b = (Box) e.nextElement();
  174. b.render(this);
  175. }
  176. writeEndTag("</BlockArea>");
  177. }
  178. /**
  179. * render a display space to XML
  180. *
  181. * @param space the space to render
  182. */
  183. public void renderDisplaySpace(DisplaySpace space) {
  184. writeEmptyElementTag("<DisplaySpace size=\""
  185. + space.getSize() +"\"/>");
  186. }
  187. /**
  188. * render an SVG area to XML
  189. *
  190. * @param area the area to render
  191. */
  192. public void renderSVGArea(SVGArea area) {
  193. writeEmptyElementTag("<SVG/>");
  194. }
  195. /**
  196. * render an image area to XML
  197. *
  198. * @param area the area to render
  199. */
  200. public void renderImageArea(ImageArea area) {
  201. writeEmptyElementTag("<ImageArea/>");
  202. }
  203. /**
  204. * render an inline area to XML
  205. *
  206. * @param area the area to render
  207. */
  208. public void renderInlineArea(InlineArea area) {
  209. String fontWeight = area.getFontState().getFontWeight();
  210. StringBuffer sb = new StringBuffer();
  211. String s = area.getText();
  212. int l = s.length();
  213. for (int i=0; i < l; i++) {
  214. char ch = s.charAt(i);
  215. if (ch>127)
  216. sb = sb.append("&#"+(int)ch+";");
  217. else
  218. sb = sb.append(ch);
  219. }
  220. writeElement("<InlineArea font-weight=\""
  221. + fontWeight + "\" red=\""
  222. + area.getRed() + "\" green=\""
  223. + area.getGreen() + "\" blue = \""
  224. + area.getBlue() + " width = \""
  225. + area.getContentWidth() + "\">" + sb.toString()
  226. + "</InlineArea>");
  227. }
  228. /**
  229. * render an inline space to XML
  230. *
  231. * @param space the space to render
  232. */
  233. public void renderInlineSpace(InlineSpace space) {
  234. writeEmptyElementTag("<InlineSpace size=\""
  235. + space.getSize() +"\"/>");
  236. }
  237. /**
  238. * render a line area to XML
  239. *
  240. * @param area the area to render
  241. */
  242. public void renderLineArea(LineArea area) {
  243. String fontWeight = area.getFontState().getFontWeight();
  244. writeStartTag("<LineArea font-weight=\""
  245. + fontWeight + "\">");
  246. Enumeration e = area.getChildren().elements();
  247. while (e.hasMoreElements()) {
  248. Box b = (Box)e.nextElement();
  249. b.render(this);
  250. }
  251. writeEndTag("</LineArea>");
  252. }
  253. /**
  254. * render a page to XML
  255. *
  256. * @param page the page to render
  257. */
  258. public void renderPage(Page page) {
  259. AreaContainer body, before, after;
  260. writeStartTag("<Page>");
  261. body = page.getBody();
  262. before = page.getBefore();
  263. after = page.getAfter();
  264. if (before != null) {
  265. renderAreaContainer(before);
  266. }
  267. renderAreaContainer(body);
  268. if (after != null) {
  269. renderAreaContainer(after);
  270. }
  271. writeEndTag("</Page>");
  272. }
  273. /**
  274. * render a rule area to XML
  275. *
  276. * @param area the area to render
  277. */
  278. public void renderRuleArea(RuleArea area) {
  279. writeEmptyElementTag("<Rule start-indent=\""
  280. + area.getStartIndent()
  281. + "\" end-indent=\""
  282. + area.getEndIndent()
  283. + "\" rule-thickness=\""
  284. + area.getRuleThickness()
  285. + "\" red=\"" + area.getRed()
  286. + "\" green=\"" + area.getGreen()
  287. + "\" blue = \"" + area.getBlue()
  288. + "\"/>");
  289. }
  290. }