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 34KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with
  4. * this work for additional information regarding copyright ownership.
  5. * The ASF licenses this file to You under the Apache License, Version 2.0
  6. * (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. /* $Id$ */
  18. package org.apache.fop.render.xml;
  19. // Java
  20. import java.awt.Color;
  21. import java.awt.geom.Rectangle2D;
  22. import java.io.IOException;
  23. import java.io.OutputStream;
  24. import java.util.Iterator;
  25. import java.util.List;
  26. import java.util.Map;
  27. import javax.xml.transform.TransformerConfigurationException;
  28. import javax.xml.transform.sax.SAXTransformerFactory;
  29. import javax.xml.transform.sax.TransformerHandler;
  30. import javax.xml.transform.stream.StreamResult;
  31. import org.w3c.dom.Document;
  32. import org.xml.sax.Attributes;
  33. import org.xml.sax.ContentHandler;
  34. import org.xml.sax.SAXException;
  35. import org.xml.sax.ext.LexicalHandler;
  36. import org.xml.sax.helpers.AttributesImpl;
  37. import org.apache.xmlgraphics.util.XMLizable;
  38. import org.apache.fop.apps.FOPException;
  39. import org.apache.fop.apps.FOUserAgent;
  40. import org.apache.fop.apps.MimeConstants;
  41. import org.apache.fop.area.Area;
  42. import org.apache.fop.area.AreaTreeObject;
  43. import org.apache.fop.area.BeforeFloat;
  44. import org.apache.fop.area.Block;
  45. import org.apache.fop.area.BlockViewport;
  46. import org.apache.fop.area.BodyRegion;
  47. import org.apache.fop.area.BookmarkData;
  48. import org.apache.fop.area.CTM;
  49. import org.apache.fop.area.DestinationData;
  50. import org.apache.fop.area.Footnote;
  51. import org.apache.fop.area.LineArea;
  52. import org.apache.fop.area.MainReference;
  53. import org.apache.fop.area.NormalFlow;
  54. import org.apache.fop.area.OffDocumentExtensionAttachment;
  55. import org.apache.fop.area.OffDocumentItem;
  56. import org.apache.fop.area.PageSequence;
  57. import org.apache.fop.area.PageViewport;
  58. import org.apache.fop.area.RegionReference;
  59. import org.apache.fop.area.RegionViewport;
  60. import org.apache.fop.area.Span;
  61. import org.apache.fop.area.Trait;
  62. import org.apache.fop.area.Trait.Background;
  63. import org.apache.fop.area.Trait.InternalLink;
  64. import org.apache.fop.area.inline.Container;
  65. import org.apache.fop.area.inline.ForeignObject;
  66. import org.apache.fop.area.inline.Image;
  67. import org.apache.fop.area.inline.InlineArea;
  68. import org.apache.fop.area.inline.InlineBlockParent;
  69. import org.apache.fop.area.inline.InlineParent;
  70. import org.apache.fop.area.inline.Leader;
  71. import org.apache.fop.area.inline.Space;
  72. import org.apache.fop.area.inline.SpaceArea;
  73. import org.apache.fop.area.inline.TextArea;
  74. import org.apache.fop.area.inline.Viewport;
  75. import org.apache.fop.area.inline.WordArea;
  76. import org.apache.fop.fo.Constants;
  77. import org.apache.fop.fo.extensions.ExtensionAttachment;
  78. import org.apache.fop.fonts.FontInfo;
  79. import org.apache.fop.fonts.FontTriplet;
  80. import org.apache.fop.render.PrintRenderer;
  81. import org.apache.fop.render.Renderer;
  82. import org.apache.fop.render.RendererContext;
  83. import org.apache.fop.render.XMLHandler;
  84. import org.apache.fop.util.ColorUtil;
  85. import org.apache.fop.util.QName;
  86. /**
  87. * Renderer that renders areas to XML for debugging purposes.
  88. * This creates an xml that contains the information of the area
  89. * tree. It does not output any state or derived information.
  90. * The output can be used to build a new area tree which can be
  91. * rendered to any renderer.
  92. */
  93. public class XMLRenderer extends PrintRenderer {
  94. /** XML MIME type */
  95. public static final String XML_MIME_TYPE = MimeConstants.MIME_FOP_AREA_TREE;
  96. /** Main namespace in use. */
  97. public static final String NS = "";
  98. /** CDATA type */
  99. public static final String CDATA = "CDATA";
  100. /** An empty Attributes object used when no attributes are needed. */
  101. public static final Attributes EMPTY_ATTS = new AttributesImpl();
  102. private boolean startedSequence = false;
  103. private RendererContext context;
  104. private boolean compactFormat = false;
  105. /** If not null, the XMLRenderer will mimic another renderer by using its font setup. */
  106. protected Renderer mimic;
  107. /** ContentHandler that the generated XML is written to */
  108. protected ContentHandler handler;
  109. /** AttributesImpl instance that can be used during XML generation. */
  110. protected AttributesImpl atts = new AttributesImpl();
  111. /** The OutputStream to write the generated XML to. */
  112. protected OutputStream out;
  113. /** A list of ExtensionAttachements received through processOffDocumentItem() */
  114. protected List extensionAttachments;
  115. /**
  116. * Creates a new XML renderer.
  117. */
  118. public XMLRenderer() {
  119. context = new RendererContext(this, XML_MIME_TYPE);
  120. }
  121. /**
  122. * {@inheritDoc}
  123. */
  124. public void setUserAgent(FOUserAgent agent) {
  125. super.setUserAgent(agent);
  126. XMLHandler xmlHandler = new XMLXMLHandler();
  127. userAgent.getXMLHandlerRegistry().addXMLHandler(xmlHandler);
  128. Boolean b = (Boolean)userAgent.getRendererOptions().get("compact-format");
  129. if (b != null) {
  130. setCompactFormat(b.booleanValue());
  131. }
  132. }
  133. /**
  134. * Call this method to make the XMLRenderer mimic a different renderer by using its font
  135. * setup. This is useful when working with the intermediate format parser.
  136. * @param renderer the renderer to mimic
  137. */
  138. public void mimicRenderer(Renderer renderer) {
  139. this.mimic = renderer;
  140. }
  141. /** {@inheritDoc} */
  142. public void setupFontInfo(FontInfo inFontInfo) {
  143. if (mimic != null) {
  144. mimic.setupFontInfo(inFontInfo);
  145. } else {
  146. super.setupFontInfo(inFontInfo);
  147. }
  148. }
  149. /**
  150. * Sets an outside TransformerHandler to use instead of the default one
  151. * create in this class in startRenderer().
  152. * @param handler Overriding TransformerHandler
  153. */
  154. public void setContentHandler(ContentHandler handler) {
  155. this.handler = handler;
  156. }
  157. public void setCompactFormat(boolean compact) {
  158. this.compactFormat = compact;
  159. }
  160. private boolean isDetailedFormat() {
  161. return !this.compactFormat;
  162. }
  163. /**
  164. * Handles SAXExceptions.
  165. * @param saxe the SAXException to handle
  166. */
  167. protected void handleSAXException(SAXException saxe) {
  168. throw new RuntimeException(saxe.getMessage());
  169. }
  170. /**
  171. * Writes a comment to the generated XML.
  172. * @param comment the comment
  173. */
  174. protected void comment(String comment) {
  175. if (handler instanceof LexicalHandler) {
  176. try {
  177. ((LexicalHandler) handler).comment(comment.toCharArray(), 0, comment.length());
  178. } catch (SAXException saxe) {
  179. handleSAXException(saxe);
  180. }
  181. }
  182. }
  183. /**
  184. * Starts a new element (without attributes).
  185. * @param tagName tag name of the element
  186. */
  187. protected void startElement(String tagName) {
  188. startElement(tagName, EMPTY_ATTS);
  189. }
  190. /**
  191. * Starts a new element.
  192. * @param tagName tag name of the element
  193. * @param atts attributes to add
  194. */
  195. protected void startElement(String tagName, Attributes atts) {
  196. try {
  197. handler.startElement(NS, tagName, tagName, atts);
  198. } catch (SAXException saxe) {
  199. handleSAXException(saxe);
  200. }
  201. }
  202. /**
  203. * Ends an element.
  204. * @param tagName tag name of the element
  205. */
  206. protected void endElement(String tagName) {
  207. try {
  208. handler.endElement(NS, tagName, tagName);
  209. } catch (SAXException saxe) {
  210. handleSAXException(saxe);
  211. }
  212. }
  213. /**
  214. * Sends plain text to the XML
  215. * @param text the text
  216. */
  217. protected void characters(String text) {
  218. try {
  219. char[] ca = text.toCharArray();
  220. handler.characters(ca, 0, ca.length);
  221. } catch (SAXException saxe) {
  222. handleSAXException(saxe);
  223. }
  224. }
  225. /**
  226. * Adds a new attribute to the protected member variable "atts".
  227. * @param name name of the attribute
  228. * @param value value of the attribute
  229. */
  230. protected void addAttribute(String name, String value) {
  231. atts.addAttribute(NS, name, name, CDATA, value);
  232. }
  233. /**
  234. * Adds a new attribute to the protected member variable "atts".
  235. * @param name name of the attribute
  236. * @param value value of the attribute
  237. */
  238. protected void addAttribute(QName name, String value) {
  239. atts.addAttribute(name.getNamespaceURI(), name.getLocalName(), name.getQName(),
  240. CDATA, value);
  241. }
  242. /**
  243. * Adds a new attribute to the protected member variable "atts".
  244. * @param name name of the attribute
  245. * @param value value of the attribute
  246. */
  247. protected void addAttribute(String name, int value) {
  248. addAttribute(name, Integer.toString(value));
  249. }
  250. /**
  251. * Adds a new attribute to the protected member variable "atts".
  252. * @param name name of the attribute
  253. * @param rect a Rectangle2D to format and use as attribute value
  254. */
  255. protected void addAttribute(String name, Rectangle2D rect) {
  256. addAttribute(name, createString(rect));
  257. }
  258. /**
  259. * Adds the general Area attributes.
  260. * @param area Area to extract attributes from
  261. */
  262. protected void addAreaAttributes(Area area) {
  263. addAttribute("ipd", area.getIPD());
  264. addAttribute("bpd", area.getBPD());
  265. if (isDetailedFormat()) {
  266. if (area.getIPD() != 0) {
  267. addAttribute("ipda", area.getAllocIPD());
  268. }
  269. if (area.getBPD() != 0) {
  270. addAttribute("bpda", area.getAllocBPD());
  271. }
  272. addAttribute("bap", area.getBorderAndPaddingWidthStart() + " "
  273. + area.getBorderAndPaddingWidthEnd() + " "
  274. + area.getBorderAndPaddingWidthBefore() + " "
  275. + area.getBorderAndPaddingWidthAfter());
  276. }
  277. }
  278. /**
  279. * Adds attributes from traits of an Area.
  280. * @param area Area to extract traits from
  281. */
  282. protected void addTraitAttributes(Area area) {
  283. Map traitMap = area.getTraits();
  284. if (traitMap != null) {
  285. Iterator iter = traitMap.entrySet().iterator();
  286. while (iter.hasNext()) {
  287. Map.Entry traitEntry = (Map.Entry) iter.next();
  288. Object key = traitEntry.getKey();
  289. String name = Trait.getTraitName(key);
  290. Class clazz = Trait.getTraitClass(key);
  291. if ("break-before".equals(name) || "break-after".equals(name)) {
  292. continue;
  293. }
  294. Object value = traitEntry.getValue();
  295. if (key == Trait.FONT) {
  296. FontTriplet triplet = (FontTriplet)value;
  297. addAttribute("font-name", triplet.getName());
  298. addAttribute("font-style", triplet.getStyle());
  299. addAttribute("font-weight", triplet.getWeight());
  300. } else if (clazz.equals(InternalLink.class)) {
  301. InternalLink iLink = (InternalLink)value;
  302. addAttribute(name, iLink.xmlAttribute());
  303. } else if (clazz.equals(Background.class)) {
  304. Background bkg = (Background)value;
  305. //TODO Remove the following line (makes changes in the test checks necessary)
  306. addAttribute(name, bkg.toString());
  307. if (bkg.getColor() != null) {
  308. addAttribute("bkg-color", bkg.getColor().toString());
  309. }
  310. if (bkg.getURL() != null) {
  311. addAttribute("bkg-img", bkg.getURL());
  312. String repString;
  313. int repeat = bkg.getRepeat();
  314. switch (repeat) {
  315. case Constants.EN_REPEAT:
  316. repString = "repeat";
  317. break;
  318. case Constants.EN_REPEATX:
  319. repString = "repeat-x";
  320. break;
  321. case Constants.EN_REPEATY:
  322. repString = "repeat-y";
  323. break;
  324. case Constants.EN_NOREPEAT:
  325. repString = "no-repeat";
  326. break;
  327. default:
  328. throw new IllegalStateException(
  329. "Illegal value for repeat encountered: " + repeat);
  330. }
  331. addAttribute("bkg-repeat", repString);
  332. addAttribute("bkg-horz-offset", bkg.getHoriz());
  333. addAttribute("bkg-vert-offset", bkg.getVertical());
  334. }
  335. } else if (clazz.equals(Color.class)) {
  336. Color c = (Color)value;
  337. addAttribute(name, ColorUtil.colorToString(c));
  338. } else if (key == Trait.START_INDENT || key == Trait.END_INDENT) {
  339. if (((Integer)value).intValue() != 0) {
  340. addAttribute(name, value.toString());
  341. }
  342. } else {
  343. addAttribute(name, value.toString());
  344. }
  345. }
  346. }
  347. transferForeignObjects(area);
  348. }
  349. private void transferForeignObjects(AreaTreeObject ato) {
  350. Map prefixes = new java.util.HashMap();
  351. Iterator iter = ato.getForeignAttributes().entrySet().iterator();
  352. while (iter.hasNext()) {
  353. Map.Entry entry = (Map.Entry)iter.next();
  354. QName qname = (QName)entry.getKey();
  355. prefixes.put(qname.getPrefix(), qname.getNamespaceURI());
  356. addAttribute(qname, (String)entry.getValue());
  357. }
  358. //Namespace declarations
  359. iter = prefixes.entrySet().iterator();
  360. while (iter.hasNext()) {
  361. Map.Entry entry = (Map.Entry)iter.next();
  362. String qn = "xmlns:" + (String)entry.getKey();
  363. atts.addAttribute("", (String)entry.getKey(), qn,
  364. CDATA, (String)entry.getValue());
  365. }
  366. }
  367. private String createString(Rectangle2D rect) {
  368. return "" + (int) rect.getX() + " " + (int) rect.getY() + " "
  369. + (int) rect.getWidth() + " " + (int) rect.getHeight();
  370. }
  371. private void handleDocumentExtensionAttachments() {
  372. if (extensionAttachments != null && extensionAttachments.size() > 0) {
  373. handleExtensionAttachments(extensionAttachments);
  374. extensionAttachments.clear();
  375. }
  376. }
  377. /** {@inheritDoc} */
  378. public void processOffDocumentItem(OffDocumentItem oDI) {
  379. if (oDI instanceof BookmarkData) {
  380. renderBookmarkTree((BookmarkData) oDI);
  381. } else if (oDI instanceof DestinationData) {
  382. renderDestination((DestinationData) oDI);
  383. } else if (oDI instanceof OffDocumentExtensionAttachment) {
  384. ExtensionAttachment attachment = ((OffDocumentExtensionAttachment)oDI).getAttachment();
  385. if (extensionAttachments == null) {
  386. extensionAttachments = new java.util.ArrayList();
  387. }
  388. extensionAttachments.add(attachment);
  389. } else {
  390. String warn = "Ignoring OffDocumentItem: " + oDI;
  391. log.warn(warn);
  392. }
  393. }
  394. /**
  395. * Renders a BookmarkTree object
  396. * @param bookmarkRoot the BookmarkData object representing the top of the tree
  397. */
  398. protected void renderBookmarkTree(BookmarkData bookmarkRoot) {
  399. if (bookmarkRoot.getWhenToProcess() == OffDocumentItem.END_OF_DOC) {
  400. endPageSequence();
  401. }
  402. /* If this kind of handling is also necessary for other renderers, then
  403. better add endPageSequence to the Renderer interface and call it
  404. explicitly from model.endDocument() */
  405. startElement("bookmarkTree");
  406. for (int i = 0; i < bookmarkRoot.getCount(); i++) {
  407. renderBookmarkItem(bookmarkRoot.getSubData(i));
  408. }
  409. endElement("bookmarkTree");
  410. }
  411. private void renderBookmarkItem(BookmarkData bm) {
  412. atts.clear();
  413. addAttribute("title", bm.getBookmarkTitle());
  414. addAttribute("show-children", String.valueOf(bm.showChildItems()));
  415. PageViewport pv = bm.getPageViewport();
  416. String pvKey = pv == null ? null : pv.getKey();
  417. addAttribute("internal-link",
  418. InternalLink.makeXMLAttribute(pvKey, bm.getIDRef()));
  419. startElement("bookmark", atts);
  420. for (int i = 0; i < bm.getCount(); i++) {
  421. renderBookmarkItem(bm.getSubData(i));
  422. }
  423. endElement("bookmark");
  424. }
  425. /**
  426. * Renders a DestinationData object (named destination)
  427. * @param destination the destination object
  428. */
  429. protected void renderDestination(DestinationData destination) {
  430. if (destination.getWhenToProcess() == OffDocumentItem.END_OF_DOC) {
  431. endPageSequence();
  432. }
  433. atts.clear();
  434. PageViewport pv = destination.getPageViewport();
  435. String pvKey = pv == null ? null : pv.getKey();
  436. addAttribute("internal-link",
  437. InternalLink.makeXMLAttribute(pvKey, destination.getIDRef()));
  438. startElement("destination", atts);
  439. endElement("destination");
  440. }
  441. /** {@inheritDoc} */
  442. public void startRenderer(OutputStream outputStream)
  443. throws IOException {
  444. log.debug("Rendering areas to Area Tree XML");
  445. if (this.handler == null) {
  446. SAXTransformerFactory factory
  447. = (SAXTransformerFactory)SAXTransformerFactory.newInstance();
  448. try {
  449. TransformerHandler transformerHandler = factory.newTransformerHandler();
  450. this.handler = transformerHandler;
  451. StreamResult res = new StreamResult(outputStream);
  452. transformerHandler.setResult(res);
  453. } catch (TransformerConfigurationException tce) {
  454. throw new RuntimeException(tce.getMessage());
  455. }
  456. this.out = outputStream;
  457. }
  458. try {
  459. handler.startDocument();
  460. } catch (SAXException saxe) {
  461. handleSAXException(saxe);
  462. }
  463. if (userAgent.getProducer() != null) {
  464. comment("Produced by " + userAgent.getProducer());
  465. }
  466. startElement("areaTree");
  467. }
  468. /**
  469. * {@inheritDoc}
  470. */
  471. public void stopRenderer() throws IOException {
  472. endPageSequence();
  473. endElement("areaTree");
  474. try {
  475. handler.endDocument();
  476. } catch (SAXException saxe) {
  477. handleSAXException(saxe);
  478. }
  479. if (this.out != null) {
  480. this.out.flush();
  481. }
  482. log.debug("Written out Area Tree XML");
  483. }
  484. /**
  485. * {@inheritDoc}
  486. */
  487. public void renderPage(PageViewport page) throws IOException, FOPException {
  488. atts.clear();
  489. addAttribute("bounds", page.getViewArea());
  490. addAttribute("key", page.getKey());
  491. addAttribute("nr", page.getPageNumber());
  492. addAttribute("formatted-nr", page.getPageNumberString());
  493. if (page.getSimplePageMasterName() != null) {
  494. addAttribute("simple-page-master-name", page.getSimplePageMasterName());
  495. }
  496. if (page.isBlank()) {
  497. addAttribute("blank", "true");
  498. }
  499. transferForeignObjects(page);
  500. startElement("pageViewport", atts);
  501. startElement("page");
  502. handlePageExtensionAttachments(page);
  503. super.renderPage(page);
  504. endElement("page");
  505. endElement("pageViewport");
  506. }
  507. private void handleExtensionAttachments(List attachments) {
  508. if (attachments != null && attachments.size() > 0) {
  509. startElement("extension-attachments");
  510. Iterator i = attachments.iterator();
  511. while (i.hasNext()) {
  512. ExtensionAttachment attachment = (ExtensionAttachment)i.next();
  513. if (attachment instanceof XMLizable) {
  514. try {
  515. ((XMLizable)attachment).toSAX(this.handler);
  516. } catch (SAXException e) {
  517. log.error("Error while serializing Extension Attachment", e);
  518. }
  519. } else {
  520. String warn = "Ignoring non-XMLizable ExtensionAttachment: " + attachment;
  521. log.warn(warn);
  522. }
  523. }
  524. endElement("extension-attachments");
  525. }
  526. }
  527. private void handlePageExtensionAttachments(PageViewport page) {
  528. handleExtensionAttachments(page.getExtensionAttachments());
  529. }
  530. /** {@inheritDoc} */
  531. public void startPageSequence(PageSequence pageSequence) {
  532. handleDocumentExtensionAttachments();
  533. endPageSequence(); // move this before handleDocumentExtensionAttachments() ?
  534. startedSequence = true;
  535. atts.clear();
  536. if (pageSequence.getLanguage() != null) {
  537. addAttribute("language", pageSequence.getLanguage());
  538. }
  539. if (pageSequence.getCountry() != null) {
  540. addAttribute("country", pageSequence.getCountry());
  541. }
  542. startElement("pageSequence", atts);
  543. LineArea seqTitle = pageSequence.getTitle();
  544. if (seqTitle != null) {
  545. startElement("title");
  546. List children = seqTitle.getInlineAreas();
  547. for (int count = 0; count < children.size(); count++) {
  548. InlineArea inline = (InlineArea) children.get(count);
  549. renderInlineArea(inline);
  550. }
  551. endElement("title");
  552. }
  553. }
  554. /**
  555. * Tells the renderer to finish the current PageSequence
  556. */
  557. public void endPageSequence() {
  558. if (startedSequence) {
  559. endElement("pageSequence");
  560. }
  561. startedSequence = false;
  562. }
  563. /**
  564. * {@inheritDoc}
  565. */
  566. protected void renderRegionViewport(RegionViewport port) {
  567. if (port != null) {
  568. atts.clear();
  569. addAreaAttributes(port);
  570. addTraitAttributes(port);
  571. addAttribute("rect", port.getViewArea());
  572. if (port.isClip()) {
  573. addAttribute("clipped", "true");
  574. }
  575. startElement("regionViewport", atts);
  576. RegionReference region = port.getRegionReference();
  577. atts.clear();
  578. addAreaAttributes(region);
  579. addTraitAttributes(region);
  580. addAttribute("name", region.getRegionName());
  581. addAttribute("ctm", region.getCTM().toString());
  582. if (region.getRegionClass() == FO_REGION_BEFORE) {
  583. startElement("regionBefore", atts);
  584. renderRegion(region);
  585. endElement("regionBefore");
  586. } else if (region.getRegionClass() == FO_REGION_START) {
  587. startElement("regionStart", atts);
  588. renderRegion(region);
  589. endElement("regionStart");
  590. } else if (region.getRegionClass() == FO_REGION_BODY) {
  591. BodyRegion body = (BodyRegion)region;
  592. if (body.getColumnCount() != 1) {
  593. addAttribute("columnGap", body.getColumnGap());
  594. addAttribute("columnCount", body.getColumnCount());
  595. }
  596. startElement("regionBody", atts);
  597. renderBodyRegion(body);
  598. endElement("regionBody");
  599. } else if (region.getRegionClass() == FO_REGION_END) {
  600. startElement("regionEnd", atts);
  601. renderRegion(region);
  602. endElement("regionEnd");
  603. } else if (region.getRegionClass() == FO_REGION_AFTER) {
  604. startElement("regionAfter", atts);
  605. renderRegion(region);
  606. endElement("regionAfter");
  607. }
  608. endElement("regionViewport");
  609. }
  610. }
  611. /** {@inheritDoc} */
  612. protected void startVParea(CTM ctm, Rectangle2D clippingRect) {
  613. //only necessary for graphical output
  614. }
  615. /** {@inheritDoc} */
  616. protected void endVParea() {
  617. //only necessary for graphical output
  618. }
  619. /**
  620. * {@inheritDoc}
  621. * org.apache.fop.area.inline.InlineArea)
  622. */
  623. protected void renderInlineAreaBackAndBorders(InlineArea area) {
  624. //only necessary for graphical output
  625. }
  626. /**
  627. * {@inheritDoc}
  628. */
  629. protected void renderBeforeFloat(BeforeFloat bf) {
  630. startElement("beforeFloat");
  631. super.renderBeforeFloat(bf);
  632. endElement("beforeFloat");
  633. }
  634. /**
  635. * {@inheritDoc}
  636. */
  637. protected void renderFootnote(Footnote footnote) {
  638. startElement("footnote");
  639. super.renderFootnote(footnote);
  640. endElement("footnote");
  641. }
  642. /**
  643. * {@inheritDoc}
  644. */
  645. protected void renderMainReference(MainReference mr) {
  646. atts.clear();
  647. addAreaAttributes(mr);
  648. addTraitAttributes(mr);
  649. if (mr.getColumnCount() != 1) {
  650. addAttribute("columnGap", mr.getColumnGap());
  651. }
  652. startElement("mainReference", atts);
  653. Span span = null;
  654. List spans = mr.getSpans();
  655. for (int count = 0; count < spans.size(); count++) {
  656. span = (Span) spans.get(count);
  657. atts.clear();
  658. if (span.getColumnCount() != 1) {
  659. addAttribute("columnCount", span.getColumnCount());
  660. }
  661. addAreaAttributes(span);
  662. addTraitAttributes(span);
  663. startElement("span", atts);
  664. for (int c = 0; c < span.getColumnCount(); c++) {
  665. NormalFlow flow = (NormalFlow) span.getNormalFlow(c);
  666. renderFlow(flow);
  667. }
  668. endElement("span");
  669. }
  670. endElement("mainReference");
  671. }
  672. /**
  673. * {@inheritDoc}
  674. */
  675. protected void renderFlow(NormalFlow flow) {
  676. // the normal flow reference area contains stacked blocks
  677. atts.clear();
  678. addAreaAttributes(flow);
  679. addTraitAttributes(flow);
  680. startElement("flow", atts);
  681. super.renderFlow(flow);
  682. endElement("flow");
  683. }
  684. /** {@inheritDoc} */
  685. protected void renderReferenceArea(Block block) {
  686. handleBlockTraits(block);
  687. List children = block.getChildAreas();
  688. if (children != null) {
  689. renderBlocks(block, children);
  690. }
  691. }
  692. /** {@inheritDoc} */
  693. protected void renderBlock(Block block) {
  694. atts.clear();
  695. addAreaAttributes(block);
  696. addTraitAttributes(block);
  697. int positioning = block.getPositioning();
  698. if (block instanceof BlockViewport) {
  699. BlockViewport bvp = (BlockViewport)block;
  700. boolean abspos = false;
  701. if (bvp.getPositioning() == Block.ABSOLUTE
  702. || bvp.getPositioning() == Block.FIXED) {
  703. abspos = true;
  704. }
  705. if (abspos) {
  706. addAttribute("left-position", bvp.getXOffset());
  707. addAttribute("top-position", bvp.getYOffset());
  708. }
  709. addAttribute("ctm", bvp.getCTM().toString());
  710. if (bvp.getClip()) {
  711. addAttribute("clipped", "true");
  712. }
  713. } else {
  714. if (block.getXOffset() != 0) {
  715. addAttribute("left-offset", block.getXOffset());
  716. }
  717. if (block.getYOffset() != 0) {
  718. addAttribute("top-offset", block.getYOffset());
  719. }
  720. }
  721. switch (positioning) {
  722. case Block.RELATIVE:
  723. addAttribute("positioning", "relative");
  724. break;
  725. case Block.ABSOLUTE:
  726. addAttribute("positioning", "absolute");
  727. break;
  728. case Block.FIXED:
  729. addAttribute("positioning", "fixed");
  730. break;
  731. default: //nop
  732. }
  733. startElement("block", atts);
  734. super.renderBlock(block);
  735. endElement("block");
  736. }
  737. /**
  738. * {@inheritDoc}
  739. */
  740. protected void renderLineArea(LineArea line) {
  741. atts.clear();
  742. addAreaAttributes(line);
  743. addTraitAttributes(line);
  744. startElement("lineArea", atts);
  745. super.renderLineArea(line);
  746. endElement("lineArea");
  747. }
  748. /**
  749. * {@inheritDoc}
  750. */
  751. protected void renderInlineArea(InlineArea inlineArea) {
  752. atts.clear();
  753. if (inlineArea.getClass() == InlineArea.class) {
  754. // Generic inline area. This is implemented to allow the 0x0 "dummy"
  755. // area generated by fo:wrapper to pass its id.
  756. addAreaAttributes(inlineArea);
  757. addTraitAttributes(inlineArea);
  758. startElement("inline", atts);
  759. endElement("inline");
  760. } else {
  761. super.renderInlineArea(inlineArea);
  762. // calls specific renderers for Text, Space, Viewport, etc. etc.
  763. }
  764. }
  765. /**
  766. * {@inheritDoc}
  767. */
  768. protected void renderViewport(Viewport viewport) {
  769. atts.clear();
  770. addAreaAttributes(viewport);
  771. addTraitAttributes(viewport);
  772. addAttribute("offset", viewport.getOffset());
  773. addAttribute("pos", viewport.getContentPosition());
  774. if (viewport.getClip()) {
  775. addAttribute("clip", "true");
  776. }
  777. startElement("viewport", atts);
  778. super.renderViewport(viewport);
  779. endElement("viewport");
  780. }
  781. /**
  782. * {@inheritDoc}
  783. */
  784. public void renderImage(Image image, Rectangle2D pos) {
  785. atts.clear();
  786. addAreaAttributes(image);
  787. addTraitAttributes(image);
  788. addAttribute("url", image.getURL());
  789. //addAttribute("pos", pos);
  790. startElement("image", atts);
  791. endElement("image");
  792. }
  793. /**
  794. * {@inheritDoc}
  795. */
  796. public void renderContainer(Container cont) {
  797. startElement("container");
  798. super.renderContainer(cont);
  799. endElement("container");
  800. }
  801. /**
  802. * Renders an fo:foreing-object.
  803. * @param fo the foreign object
  804. * @param pos the position of the foreign object
  805. * @see org.apache.fop.render.AbstractRenderer#renderForeignObject(ForeignObject, Rectangle2D)
  806. */
  807. public void renderForeignObject(ForeignObject fo, Rectangle2D pos) {
  808. atts.clear();
  809. addAreaAttributes(fo);
  810. addTraitAttributes(fo);
  811. String ns = fo.getNameSpace();
  812. addAttribute("ns", ns);
  813. startElement("foreignObject", atts);
  814. Document doc = fo.getDocument();
  815. context.setProperty(XMLXMLHandler.HANDLER, handler);
  816. renderXML(context, doc, ns);
  817. endElement("foreignObject");
  818. }
  819. /**
  820. * {@inheritDoc}
  821. */
  822. protected void renderInlineSpace(Space space) {
  823. atts.clear();
  824. addAreaAttributes(space);
  825. addTraitAttributes(space);
  826. addAttribute("offset", space.getOffset());
  827. startElement("space", atts);
  828. endElement("space");
  829. }
  830. /**
  831. * {@inheritDoc}
  832. */
  833. protected void renderText(TextArea text) {
  834. atts.clear();
  835. if (text.getTextWordSpaceAdjust() != 0) {
  836. addAttribute("twsadjust", text.getTextWordSpaceAdjust());
  837. }
  838. if (text.getTextLetterSpaceAdjust() != 0) {
  839. addAttribute("tlsadjust", text.getTextLetterSpaceAdjust());
  840. }
  841. addAttribute("offset", text.getOffset());
  842. addAttribute("baseline", text.getBaselineOffset());
  843. addAreaAttributes(text);
  844. addTraitAttributes(text);
  845. startElement("text", atts);
  846. super.renderText(text);
  847. endElement("text");
  848. }
  849. /**
  850. * {@inheritDoc}
  851. */
  852. protected void renderWord(WordArea word) {
  853. atts.clear();
  854. addAttribute("offset", word.getOffset());
  855. int[] letterAdjust = word.getLetterAdjustArray();
  856. if (letterAdjust != null) {
  857. StringBuffer sb = new StringBuffer(64);
  858. boolean nonZeroFound = false;
  859. for (int i = 0, c = letterAdjust.length; i < c; i++) {
  860. if (i > 0) {
  861. sb.append(' ');
  862. }
  863. sb.append(letterAdjust[i]);
  864. nonZeroFound |= (letterAdjust[i] != 0);
  865. }
  866. if (nonZeroFound) {
  867. addAttribute("letter-adjust", sb.toString());
  868. }
  869. }
  870. startElement("word", atts);
  871. characters(word.getWord());
  872. endElement("word");
  873. super.renderWord(word);
  874. }
  875. /**
  876. * {@inheritDoc}
  877. */
  878. protected void renderSpace(SpaceArea space) {
  879. atts.clear();
  880. addAttribute("offset", space.getOffset());
  881. if (!space.isAdjustable()) {
  882. addAttribute("adj", "false"); //default is true
  883. }
  884. startElement("space", atts);
  885. characters(space.getSpace());
  886. endElement("space");
  887. super.renderSpace(space);
  888. }
  889. /**
  890. * {@inheritDoc}
  891. */
  892. protected void renderInlineParent(InlineParent ip) {
  893. atts.clear();
  894. addAreaAttributes(ip);
  895. addTraitAttributes(ip);
  896. addAttribute("offset", ip.getOffset());
  897. startElement("inlineparent", atts);
  898. super.renderInlineParent(ip);
  899. endElement("inlineparent");
  900. }
  901. /**
  902. * {@inheritDoc}
  903. */
  904. protected void renderInlineBlockParent(InlineBlockParent ibp) {
  905. atts.clear();
  906. addAreaAttributes(ibp);
  907. addTraitAttributes(ibp);
  908. addAttribute("offset", ibp.getOffset());
  909. startElement("inlineblockparent", atts);
  910. super.renderInlineBlockParent(ibp);
  911. endElement("inlineblockparent");
  912. }
  913. /**
  914. * {@inheritDoc}
  915. */
  916. protected void renderLeader(Leader area) {
  917. atts.clear();
  918. addAreaAttributes(area);
  919. addTraitAttributes(area);
  920. addAttribute("offset", area.getOffset());
  921. addAttribute("ruleStyle", area.getRuleStyleAsString());
  922. addAttribute("ruleThickness", area.getRuleThickness());
  923. startElement("leader", atts);
  924. endElement("leader");
  925. super.renderLeader(area);
  926. }
  927. /** {@inheritDoc} */
  928. public String getMimeType() {
  929. return XML_MIME_TYPE;
  930. }
  931. }