您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

XMLRenderer.java 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  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.layout.inline.*;
  48. import org.apache.fop.pdf.*;
  49. import org.apache.fop.fo.properties.LeaderPattern;
  50. // Java
  51. import java.io.IOException;
  52. import java.io.PrintWriter;
  53. import java.io.OutputStream;
  54. import java.util.Enumeration;
  55. import java.util.Hashtable;
  56. /**
  57. * Renderer that renders areas to XML for debugging purposes.
  58. */
  59. public class XMLRenderer implements Renderer {
  60. /** indentation to use for pretty-printing the XML */
  61. protected int indent = 0;
  62. /** the application producing the XML */
  63. protected String producer;
  64. /** the writer used to output the XML */
  65. protected PrintWriter writer;
  66. /** options */
  67. protected Hashtable options;
  68. public XMLRenderer() {
  69. }
  70. /** set up renderer options */
  71. public void setOptions(Hashtable options) {
  72. this.options = options;
  73. }
  74. /**
  75. * set the document's producer
  76. *
  77. * @param producer string indicating application producing the XML
  78. */
  79. public void setProducer(String producer) {
  80. this.producer = producer;
  81. }
  82. /**
  83. * render the areas into XML
  84. *
  85. * @param areaTree the laid-out area tree
  86. * @param stream the OutputStream to give the XML to
  87. */
  88. public void render(AreaTree areaTree, OutputStream stream)
  89. throws IOException {
  90. MessageHandler.logln("rendering areas to XML");
  91. this.writer = new PrintWriter(stream);
  92. this.writer.write("<?xml version=\"1.0\"?>\n<!-- produced by "
  93. + this.producer + " -->\n");
  94. writeStartTag("<AreaTree>");
  95. Enumeration e = areaTree.getPages().elements();
  96. while (e.hasMoreElements()) {
  97. this.renderPage((Page) e.nextElement());
  98. }
  99. writeEndTag("</AreaTree>");
  100. this.writer.flush();
  101. MessageHandler.errorln("written out XML");
  102. }
  103. /**
  104. * write out spaces to make indent
  105. */
  106. protected void writeIndent() {
  107. StringBuffer s = new StringBuffer();
  108. for (int i= 0; i<this.indent; i++) {
  109. s = s.append(" ");
  110. }
  111. this.writer.write(s.toString());
  112. }
  113. /**
  114. * write out an element
  115. *
  116. * @param element the full text of the element including tags
  117. */
  118. protected void writeElement(String element) {
  119. writeIndent();
  120. this.writer.write(element+"\n");
  121. }
  122. /**
  123. * write out an empty-element-tag
  124. *
  125. * @param tag the text of the tag
  126. */
  127. protected void writeEmptyElementTag(String tag) {
  128. writeIndent();
  129. this.writer.write(tag + "\n");
  130. }
  131. /**
  132. * write out an end tag
  133. *
  134. * @param tag the text of the tag
  135. */
  136. protected void writeEndTag(String tag) {
  137. this.indent--;
  138. writeIndent();
  139. this.writer.write(tag + "\n");
  140. }
  141. /**
  142. * write out a start tag
  143. *
  144. * @param tag the text of the tag
  145. */
  146. protected void writeStartTag(String tag) {
  147. writeIndent();
  148. this.writer.write(tag + "\n");
  149. this.indent++;
  150. }
  151. /**
  152. * set up the font info
  153. *
  154. * @param fontInfo the font info object to set up
  155. */
  156. public void setupFontInfo(FontInfo fontInfo) {
  157. /* use PDF's font setup to get PDF metrics */
  158. org.apache.fop.render.pdf.FontSetup.setup(fontInfo);
  159. }
  160. /**
  161. * render an area container to XML
  162. *
  163. * @param area the area container to render
  164. */
  165. public void renderAreaContainer(AreaContainer area) {
  166. writeStartTag("<AreaContainer>");
  167. Enumeration e = area.getChildren().elements();
  168. while (e.hasMoreElements()) {
  169. Box b = (Box) e.nextElement();
  170. b.render(this);
  171. }
  172. writeEndTag("</AreaContainer>");
  173. }
  174. /**
  175. * render a body area container to XML
  176. *
  177. * @param area the body area container to render
  178. */
  179. public void renderBodyAreaContainer(BodyAreaContainer area) {
  180. writeStartTag("<BodyAreaContainer>");
  181. Enumeration e = area.getChildren().elements();
  182. while (e.hasMoreElements()) {
  183. Box b = (Box) e.nextElement();
  184. b.render(this);
  185. }
  186. writeEndTag("</BodyAreaContainer>");
  187. }
  188. /**
  189. * render a span area to XML
  190. *
  191. * @param area the span area to render
  192. */
  193. public void renderSpanArea(SpanArea area) {
  194. writeStartTag("<SpanArea>");
  195. Enumeration e = area.getChildren().elements();
  196. while (e.hasMoreElements()) {
  197. Box b = (Box) e.nextElement();
  198. b.render(this);
  199. }
  200. writeEndTag("</SpanArea>");
  201. }
  202. /**
  203. * render a block area to XML
  204. *
  205. * @param area the block area to render
  206. */
  207. public void renderBlockArea(BlockArea area) {
  208. writeStartTag("<BlockArea start-indent=\""
  209. + area.getStartIndent()
  210. + "\" end-indent=\""
  211. + area.getEndIndent() + "\">");
  212. Enumeration e = area.getChildren().elements();
  213. while (e.hasMoreElements()) {
  214. Box b = (Box) e.nextElement();
  215. b.render(this);
  216. }
  217. writeEndTag("</BlockArea>");
  218. }
  219. /**
  220. * render a display space to XML
  221. *
  222. * @param space the space to render
  223. */
  224. public void renderDisplaySpace(DisplaySpace space) {
  225. if (!isCoarseXml())
  226. writeEmptyElementTag("<DisplaySpace size=\""
  227. + space.getSize() +"\"/>");
  228. }
  229. /** render a foreign object area */
  230. public void renderForeignObjectArea(ForeignObjectArea area)
  231. {
  232. // if necessary need to scale and align the content
  233. area.getObject().render(this);
  234. }
  235. /**
  236. * render an SVG area to XML
  237. *
  238. * @param area the area to render
  239. */
  240. public void renderSVGArea(SVGArea area) {
  241. writeEmptyElementTag("<SVG/>");
  242. }
  243. /**
  244. * render an image area to XML
  245. *
  246. * @param area the area to render
  247. */
  248. public void renderImageArea(ImageArea area) {
  249. writeEmptyElementTag("<ImageArea/>");
  250. }
  251. /**
  252. * render an inline area to XML
  253. *
  254. * @param area the area to render
  255. */
  256. public void renderWordArea(WordArea area) {
  257. String fontWeight = area.getFontState().getFontWeight();
  258. StringBuffer sb = new StringBuffer();
  259. String s = area.getText();
  260. int l = s.length();
  261. for (int i=0; i < l; i++) {
  262. char ch = s.charAt(i);
  263. if (ch>127)
  264. sb = sb.append("&#"+(int)ch+";");
  265. else
  266. sb = sb.append(ch);
  267. }
  268. if (!isCoarseXml()) {
  269. writeElement("<WordArea font-weight=\""
  270. + fontWeight + "\" red=\""
  271. + area.getRed() + "\" green=\""
  272. + area.getGreen() + "\" blue = \""
  273. + area.getBlue() + "\" width = \""
  274. + area.getContentWidth() + "\">" + sb.toString()
  275. + "</WordArea>");
  276. } else {
  277. this.writer.write(sb.toString());
  278. }
  279. }
  280. /**
  281. * render an inline space to XML
  282. *
  283. * @param space the space to render
  284. */
  285. public void renderInlineSpace(InlineSpace space) {
  286. if (!isCoarseXml())
  287. writeEmptyElementTag("<InlineSpace size=\""
  288. + space.getSize() +"\"/>");
  289. else
  290. this.writer.write(" ");
  291. }
  292. /**
  293. * render a line area to XML
  294. *
  295. * @param area the area to render
  296. */
  297. public void renderLineArea(LineArea area) {
  298. if (!isCoarseXml()) {
  299. String fontWeight = area.getFontState().getFontWeight();
  300. writeStartTag("<LineArea font-weight=\""
  301. + fontWeight + "\">");
  302. }
  303. Enumeration e = area.getChildren().elements();
  304. while (e.hasMoreElements()) {
  305. Box b = (Box)e.nextElement();
  306. b.render(this);
  307. }
  308. if (!isCoarseXml())
  309. writeEndTag("</LineArea>");
  310. else
  311. this.writer.write("\n");
  312. }
  313. /**
  314. * render a page to XML
  315. *
  316. * @param page the page to render
  317. */
  318. public void renderPage(Page page) {
  319. BodyAreaContainer body;
  320. AreaContainer before, after;
  321. writeStartTag("<Page number=\"" + page.getNumber() + "\">");
  322. body = page.getBody();
  323. before = page.getBefore();
  324. after = page.getAfter();
  325. if (before != null) {
  326. renderAreaContainer(before);
  327. }
  328. renderBodyAreaContainer(body);
  329. if (after != null) {
  330. renderAreaContainer(after);
  331. }
  332. writeEndTag("</Page>");
  333. }
  334. /**
  335. * render a leader area to XML
  336. *
  337. * @param area the area to render
  338. */
  339. public void renderLeaderArea(LeaderArea area) {
  340. if (isCoarseXml()) return;
  341. String leaderPattern = "";
  342. switch (area.getLeaderPattern()) {
  343. case LeaderPattern.SPACE:
  344. leaderPattern = "space";
  345. break;
  346. case LeaderPattern.RULE:
  347. leaderPattern = "rule";
  348. break;
  349. case LeaderPattern.DOTS:
  350. leaderPattern = "dots";
  351. break;
  352. case LeaderPattern.USECONTENT:
  353. leaderPattern = "use-content";
  354. break;
  355. }
  356. writeEmptyElementTag("<Leader leader-pattern=\"" + leaderPattern +
  357. " leader-length=\"" + area.getLeaderLength()+
  358. "\" rule-thickness=\"" + area.getRuleThickness() +
  359. "\" rule-style=\"" + area.getRuleStyle() +
  360. "\" red=\"" + area.getRed() + "\" green=\"" + area.getGreen() +
  361. "\" blue = \"" + area.getBlue() + "\"/>");
  362. }
  363. private boolean isCoarseXml() {
  364. return ((Boolean)options.get("fineDetail")).booleanValue();
  365. }
  366. }