Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

XMLRenderer.java 9.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  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.messaging.MessageHandler;
  44. import org.apache.fop.render.Renderer;
  45. import org.apache.fop.image.ImageArea;
  46. import org.apache.fop.layout.*;
  47. import org.apache.fop.pdf.*;
  48. // Java
  49. import java.io.IOException;
  50. import java.io.PrintWriter;
  51. import java.util.Enumeration;
  52. /**
  53. * Renderer that renders areas to XML for debugging purposes.
  54. */
  55. public class XMLRenderer implements Renderer {
  56. /** indentation to use for pretty-printing the XML */
  57. protected int indent = 0;
  58. /** the application producing the XML */
  59. protected String producer;
  60. /** the writer used to output the XML */
  61. protected PrintWriter writer;
  62. /**
  63. * set the document's producer
  64. *
  65. * @param producer string indicating application producing the XML
  66. */
  67. public void setProducer(String producer) {
  68. this.producer = producer;
  69. }
  70. /**
  71. * render the areas into XML
  72. *
  73. * @param areaTree the laid-out area tree
  74. * @param writer the PrintWriter to give the XML to
  75. */
  76. public void render(AreaTree areaTree, PrintWriter writer)
  77. throws IOException {
  78. MessageHandler.logln("rendering areas to XML");
  79. this.writer = writer;
  80. this.writer.write("<?xml version=\"1.0\"?>\n<!-- produced by "
  81. + this.producer + " -->\n");
  82. writeStartTag("<AreaTree>");
  83. Enumeration e = areaTree.getPages().elements();
  84. while (e.hasMoreElements()) {
  85. this.renderPage((Page) e.nextElement());
  86. }
  87. writeEndTag("</AreaTree>");
  88. this.writer.flush();
  89. MessageHandler.errorln("written out XML");
  90. }
  91. /**
  92. * write out spaces to make indent
  93. */
  94. protected void writeIndent() {
  95. StringBuffer s = new StringBuffer();
  96. for (int i= 0; i<this.indent; i++) {
  97. s = s.append(" ");
  98. }
  99. this.writer.write(s.toString());
  100. }
  101. /**
  102. * write out an element
  103. *
  104. * @param element the full text of the element including tags
  105. */
  106. protected void writeElement(String element) {
  107. writeIndent();
  108. this.writer.write(element+"\n");
  109. }
  110. /**
  111. * write out an empty-element-tag
  112. *
  113. * @param tag the text of the tag
  114. */
  115. protected void writeEmptyElementTag(String tag) {
  116. writeIndent();
  117. this.writer.write(tag + "\n");
  118. }
  119. /**
  120. * write out an end tag
  121. *
  122. * @param tag the text of the tag
  123. */
  124. protected void writeEndTag(String tag) {
  125. this.indent--;
  126. writeIndent();
  127. this.writer.write(tag + "\n");
  128. }
  129. /**
  130. * write out a start tag
  131. *
  132. * @param tag the text of the tag
  133. */
  134. protected void writeStartTag(String tag) {
  135. writeIndent();
  136. this.writer.write(tag + "\n");
  137. this.indent++;
  138. }
  139. /**
  140. * set up the font info
  141. *
  142. * @param fontInfo the font info object to set up
  143. */
  144. public void setupFontInfo(FontInfo fontInfo) {
  145. /* use PDF's font setup to get PDF metrics */
  146. org.apache.fop.render.pdf.FontSetup.setup(fontInfo);
  147. }
  148. /**
  149. * render an area container to XML
  150. *
  151. * @param area the area container to render
  152. */
  153. public void renderAreaContainer(AreaContainer area) {
  154. writeStartTag("<AreaContainer>");
  155. Enumeration e = area.getChildren().elements();
  156. while (e.hasMoreElements()) {
  157. Box b = (Box) e.nextElement();
  158. b.render(this);
  159. }
  160. writeEndTag("</AreaContainer>");
  161. }
  162. /**
  163. * render a block area to XML
  164. *
  165. * @param area the block area to render
  166. */
  167. public void renderBlockArea(BlockArea area) {
  168. writeStartTag("<BlockArea start-indent=\""
  169. + area.getStartIndent()
  170. + "\" end-indent=\""
  171. + area.getEndIndent() + "\">");
  172. Enumeration e = area.getChildren().elements();
  173. while (e.hasMoreElements()) {
  174. Box b = (Box) e.nextElement();
  175. b.render(this);
  176. }
  177. writeEndTag("</BlockArea>");
  178. }
  179. /**
  180. * render a display space to XML
  181. *
  182. * @param space the space to render
  183. */
  184. public void renderDisplaySpace(DisplaySpace space) {
  185. writeEmptyElementTag("<DisplaySpace size=\""
  186. + space.getSize() +"\"/>");
  187. }
  188. /**
  189. * render an SVG area to XML
  190. *
  191. * @param area the area to render
  192. */
  193. public void renderSVGArea(SVGArea area) {
  194. writeEmptyElementTag("<SVG/>");
  195. }
  196. /**
  197. * render an image area to XML
  198. *
  199. * @param area the area to render
  200. */
  201. public void renderImageArea(ImageArea area) {
  202. writeEmptyElementTag("<ImageArea/>");
  203. }
  204. /**
  205. * render an inline area to XML
  206. *
  207. * @param area the area to render
  208. */
  209. public void renderInlineArea(InlineArea area) {
  210. String fontWeight = area.getFontState().getFontWeight();
  211. StringBuffer sb = new StringBuffer();
  212. String s = area.getText();
  213. int l = s.length();
  214. for (int i=0; i < l; i++) {
  215. char ch = s.charAt(i);
  216. if (ch>127)
  217. sb = sb.append("&#"+(int)ch+";");
  218. else
  219. sb = sb.append(ch);
  220. }
  221. writeElement("<InlineArea font-weight=\""
  222. + fontWeight + "\" red=\""
  223. + area.getRed() + "\" green=\""
  224. + area.getGreen() + "\" blue = \""
  225. + area.getBlue() + " width = \""
  226. + area.getContentWidth() + "\">" + sb.toString()
  227. + "</InlineArea>");
  228. }
  229. /**
  230. * render an inline space to XML
  231. *
  232. * @param space the space to render
  233. */
  234. public void renderInlineSpace(InlineSpace space) {
  235. writeEmptyElementTag("<InlineSpace size=\""
  236. + space.getSize() +"\"/>");
  237. }
  238. /**
  239. * render a line area to XML
  240. *
  241. * @param area the area to render
  242. */
  243. public void renderLineArea(LineArea area) {
  244. String fontWeight = area.getFontState().getFontWeight();
  245. writeStartTag("<LineArea font-weight=\""
  246. + fontWeight + "\">");
  247. Enumeration e = area.getChildren().elements();
  248. while (e.hasMoreElements()) {
  249. Box b = (Box)e.nextElement();
  250. b.render(this);
  251. }
  252. writeEndTag("</LineArea>");
  253. }
  254. /**
  255. * render a page to XML
  256. *
  257. * @param page the page to render
  258. */
  259. public void renderPage(Page page) {
  260. AreaContainer body, before, after;
  261. writeStartTag("<Page>");
  262. body = page.getBody();
  263. before = page.getBefore();
  264. after = page.getAfter();
  265. if (before != null) {
  266. renderAreaContainer(before);
  267. }
  268. renderAreaContainer(body);
  269. if (after != null) {
  270. renderAreaContainer(after);
  271. }
  272. writeEndTag("</Page>");
  273. }
  274. /**
  275. * render a rule area to XML
  276. *
  277. * @param area the area to render
  278. */
  279. public void renderRuleArea(RuleArea area) {
  280. writeEmptyElementTag("<Rule start-indent=\""
  281. + area.getStartIndent()
  282. + "\" end-indent=\""
  283. + area.getEndIndent()
  284. + "\" rule-thickness=\""
  285. + area.getRuleThickness()
  286. + "\" red=\"" + area.getRed()
  287. + "\" green=\"" + area.getGreen()
  288. + "\" blue = \"" + area.getBlue()
  289. + "\"/>");
  290. }
  291. }