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.

AbstractRenderer.java 24KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762
  1. /*
  2. * Copyright 1999-2004 The Apache Software Foundation.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. /* $Id$ */
  17. package org.apache.fop.render;
  18. // Java
  19. import java.awt.geom.Rectangle2D;
  20. import java.io.IOException;
  21. import java.io.OutputStream;
  22. import java.util.Date;
  23. import java.util.Map;
  24. import java.util.List;
  25. import java.util.Iterator;
  26. // XML
  27. import org.w3c.dom.Document;
  28. // FOP
  29. import org.apache.fop.apps.FOPException;
  30. import org.apache.fop.area.Area;
  31. import org.apache.fop.area.BeforeFloat;
  32. import org.apache.fop.area.Block;
  33. import org.apache.fop.area.BlockViewport;
  34. import org.apache.fop.area.BodyRegion;
  35. import org.apache.fop.area.CTM;
  36. import org.apache.fop.area.Flow;
  37. import org.apache.fop.area.Footnote;
  38. import org.apache.fop.area.LineArea;
  39. import org.apache.fop.area.MainReference;
  40. import org.apache.fop.area.Span;
  41. import org.apache.fop.area.Page;
  42. import org.apache.fop.area.PageViewport;
  43. import org.apache.fop.area.RegionViewport;
  44. import org.apache.fop.area.RegionReference;
  45. import org.apache.fop.area.Title;
  46. import org.apache.fop.area.TreeExt;
  47. import org.apache.fop.area.inline.Container;
  48. import org.apache.fop.area.inline.ForeignObject;
  49. import org.apache.fop.area.inline.Image;
  50. import org.apache.fop.area.inline.InlineArea;
  51. import org.apache.fop.area.inline.InlineParent;
  52. import org.apache.fop.area.inline.Leader;
  53. import org.apache.fop.area.inline.Space;
  54. import org.apache.fop.area.inline.Viewport;
  55. import org.apache.fop.area.inline.TextArea;
  56. import org.apache.fop.area.inline.Character;
  57. import org.apache.fop.apps.FOUserAgent;
  58. import org.apache.fop.fo.Constants;
  59. import org.apache.fop.fonts.FontInfo;
  60. import org.apache.fop.fo.pagination.Region;
  61. import org.apache.commons.logging.Log;
  62. import org.apache.commons.logging.LogFactory;
  63. // Avalon
  64. import org.apache.avalon.framework.configuration.Configurable;
  65. import org.apache.avalon.framework.configuration.Configuration;
  66. import org.apache.avalon.framework.configuration.ConfigurationException;
  67. /**
  68. * Abstract base class for all renderers. The Abstract renderer does all the
  69. * top level processing of the area tree and adds some abstract methods to
  70. * handle viewports. This keeps track of the current block and inline position.
  71. */
  72. public abstract class AbstractRenderer
  73. implements Renderer, Configurable, Constants {
  74. /**
  75. * user agent
  76. */
  77. protected FOUserAgent userAgent;
  78. /**
  79. * logging instance
  80. */
  81. protected static Log logger = LogFactory.getLog("FOP");
  82. /**
  83. * producer (usually "FOP")
  84. */
  85. protected String producer = "FOP";
  86. /**
  87. * creator of document
  88. */
  89. protected String creator = null;
  90. /**
  91. * creation time
  92. */
  93. protected Date creationDate = null;
  94. /**
  95. * block progression position
  96. */
  97. protected int currentBPPosition = 0;
  98. /**
  99. * inline progression position
  100. */
  101. protected int currentIPPosition = 0;
  102. /**
  103. * current inline progression position in block
  104. */
  105. protected int currentBlockIPPosition = 0;
  106. /**
  107. * the block progression position of the containing block used for
  108. * absolutely positioned blocks
  109. */
  110. protected int containingBPPosition = 0;
  111. /**
  112. * the inline progression position of the containing block used for
  113. * absolutely positioned blocks
  114. */
  115. protected int containingIPPosition = 0;
  116. /**
  117. * @see org.apache.avalon.framework.configuration.Configurable#configure(Configuration)
  118. */
  119. public void configure(Configuration conf) throws ConfigurationException {
  120. }
  121. /**
  122. * Returns the Commons-Logging instance for this class
  123. * @return The Commons-Logging instance
  124. */
  125. protected Log getLogger() {
  126. return logger;
  127. }
  128. /** @see org.apache.fop.render.Renderer */
  129. public void setProducer(String inProducer) {
  130. producer = inProducer;
  131. }
  132. /** @see org.apache.fop.render.Renderer */
  133. public void setCreator(String inCreator) {
  134. creator = inCreator;
  135. }
  136. /**
  137. * @see org.apache.fop.render.Renderer
  138. */
  139. public abstract void setupFontInfo(FontInfo fontInfo);
  140. /**
  141. * @see org.apache.fop.render.Renderer
  142. */
  143. public void setUserAgent(FOUserAgent agent) {
  144. userAgent = agent;
  145. }
  146. /**
  147. * @param date
  148. */
  149. public void setCreationDate(Date date) {
  150. creationDate = date;
  151. }
  152. /** @see org.apache.fop.render.Renderer */
  153. public void startRenderer(OutputStream outputStream)
  154. throws IOException { }
  155. /** @see org.apache.fop.render.Renderer */
  156. public void stopRenderer()
  157. throws IOException { }
  158. /**
  159. * Check if this renderer supports out of order rendering. If this renderer
  160. * supports out of order rendering then it means that the pages that are
  161. * not ready will be prepared and a future page will be rendered.
  162. *
  163. * @return True if the renderer supports out of order rendering
  164. * @see org.apache.fop.render.Renderer
  165. */
  166. public boolean supportsOutOfOrder() {
  167. return false;
  168. }
  169. /**
  170. * @param ext (todo) Description of the Parameter
  171. * @see org.apache.fop.render.Renderer
  172. */
  173. public void renderExtension(TreeExt ext) { }
  174. /**
  175. * Prepare a page for rendering. This is called if the renderer supports
  176. * out of order rendering. The renderer should prepare the page so that a
  177. * page further on in the set of pages can be rendered. The body of the
  178. * page should not be rendered. The page will be rendered at a later time
  179. * by the call to render page.
  180. *
  181. * @see org.apache.fop.render.Renderer
  182. */
  183. public void preparePage(PageViewport page) { }
  184. /**
  185. * Utility method to convert a page sequence title to a string. Some
  186. * renderers may only be able to use a string title. A title is a sequence
  187. * of inline areas that this method attempts to convert to an equivalent
  188. * string.
  189. *
  190. * @param title The Title to convert
  191. * @return An expanded string representing the title
  192. */
  193. protected String convertTitleToString(Title title) {
  194. List children = title.getInlineAreas();
  195. String str = convertToString(children);
  196. return str.trim();
  197. }
  198. private String convertToString(List children) {
  199. StringBuffer sb = new StringBuffer();
  200. for (int count = 0; count < children.size(); count++) {
  201. InlineArea inline = (InlineArea) children.get(count);
  202. if (inline instanceof Character) {
  203. sb.append(((Character) inline).getChar());
  204. } else if (inline instanceof TextArea) {
  205. sb.append(((TextArea) inline).getTextArea());
  206. } else if (inline instanceof InlineParent) {
  207. sb.append(convertToString(
  208. ((InlineParent) inline).getChildAreas()));
  209. } else {
  210. sb.append(" ");
  211. }
  212. }
  213. return sb.toString();
  214. }
  215. /** @see org.apache.fop.render.Renderer */
  216. public void startPageSequence(Title seqTitle) {
  217. //do nothing
  218. }
  219. // normally this would be overriden to create a page in the
  220. // output
  221. /** @see org.apache.fop.render.Renderer */
  222. public void renderPage(PageViewport page)
  223. throws IOException, FOPException {
  224. Page p = page.getPage();
  225. renderPageAreas(p);
  226. }
  227. /**
  228. * Renders page areas.
  229. *
  230. * @param page The page whos page areas are to be rendered
  231. */
  232. protected void renderPageAreas(Page page) {
  233. /* Spec does not appear to specify whether fo:region-body should
  234. appear above or below side regions in cases of overlap. FOP
  235. decision is to have fo:region-body on top, hence it is rendered
  236. last here. */
  237. RegionViewport viewport;
  238. viewport = page.getRegionViewport(Region.BEFORE_CODE);
  239. renderRegionViewport(viewport);
  240. viewport = page.getRegionViewport(Region.START_CODE);
  241. renderRegionViewport(viewport);
  242. viewport = page.getRegionViewport(Region.END_CODE);
  243. renderRegionViewport(viewport);
  244. viewport = page.getRegionViewport(Region.AFTER_CODE);
  245. renderRegionViewport(viewport);
  246. viewport = page.getRegionViewport(Region.BODY_CODE);
  247. renderRegionViewport(viewport);
  248. }
  249. /**
  250. * Renders a region viewport. <p>
  251. *
  252. * The region may clip the area and it establishes a position from where
  253. * the region is placed.</p>
  254. *
  255. * @param port The region viewport to be rendered
  256. */
  257. protected void renderRegionViewport(RegionViewport port) {
  258. if (port != null) {
  259. Rectangle2D view = port.getViewArea();
  260. // The CTM will transform coordinates relative to
  261. // this region-reference area into page coords, so
  262. // set origin for the region to 0,0.
  263. currentBPPosition = 0;
  264. currentIPPosition = 0;
  265. currentBlockIPPosition = currentIPPosition;
  266. RegionReference region = port.getRegion();
  267. handleRegionTraits(port);
  268. // shouldn't the viewport have the CTM
  269. startVParea(region.getCTM());
  270. // do after starting viewport area
  271. if (region.getRegionClass() == Region.BODY_CODE) {
  272. renderBodyRegion((BodyRegion) region);
  273. } else {
  274. renderRegion(region);
  275. }
  276. endVParea();
  277. }
  278. }
  279. /**
  280. * (todo) Description of the Method
  281. *
  282. * @param ctm The coordinate transformation matrix to use
  283. */
  284. protected void startVParea(CTM ctm) { }
  285. /**
  286. * Handle the traits for a region
  287. * This is used to draw the traits for the given page region.
  288. * (See Sect. 6.4.1.2 of XSL-FO spec.)
  289. * @param rv the RegionViewport whose region is to be drawn
  290. */
  291. protected void handleRegionTraits(RegionViewport rv) {
  292. // draw border and background
  293. }
  294. /**
  295. * (todo) Description of the Method
  296. */
  297. protected void endVParea() { }
  298. /**
  299. * Renders a region reference area.
  300. *
  301. * @param region The region reference area
  302. */
  303. protected void renderRegion(RegionReference region) {
  304. List blocks = region.getBlocks();
  305. renderBlocks(null, blocks);
  306. }
  307. /**
  308. * Renders a body region area.
  309. *
  310. * @param region The body region
  311. */
  312. protected void renderBodyRegion(BodyRegion region) {
  313. BeforeFloat bf = region.getBeforeFloat();
  314. if (bf != null) {
  315. renderBeforeFloat(bf);
  316. }
  317. MainReference mr = region.getMainReference();
  318. if (mr != null) {
  319. renderMainReference(mr);
  320. }
  321. Footnote foot = region.getFootnote();
  322. if (foot != null) {
  323. renderFootnote(foot);
  324. }
  325. }
  326. /**
  327. * Renders a before float area.
  328. *
  329. * @param bf The before float area
  330. */
  331. protected void renderBeforeFloat(BeforeFloat bf) {
  332. List blocks = bf.getChildAreas();
  333. if (blocks != null) {
  334. renderBlocks(null, blocks);
  335. Block sep = bf.getSeparator();
  336. if (sep != null) {
  337. renderBlock(sep);
  338. }
  339. }
  340. }
  341. /**
  342. * Renders a footnote
  343. *
  344. * @param footnote The footnote
  345. */
  346. protected void renderFootnote(Footnote footnote) {
  347. List blocks = footnote.getChildAreas();
  348. if (blocks != null) {
  349. Block sep = footnote.getSeparator();
  350. if (sep != null) {
  351. renderBlock(sep);
  352. }
  353. renderBlocks(null, blocks);
  354. }
  355. }
  356. /**
  357. * Renders the main reference area.
  358. * <p>
  359. * The main reference area contains a list of spans that are
  360. * stacked on the page.
  361. * The spans contain a list of normal flow reference areas
  362. * that are positioned into columns.
  363. * </p>
  364. *
  365. * @param mr The main reference area
  366. */
  367. protected void renderMainReference(MainReference mr) {
  368. int saveIPPos = currentIPPosition;
  369. Span span = null;
  370. List spans = mr.getSpans();
  371. for (int count = 0; count < spans.size(); count++) {
  372. span = (Span) spans.get(count);
  373. int offset = (mr.getWidth()
  374. - (span.getColumnCount() - 1) * mr.getColumnGap())
  375. / span.getColumnCount() + mr.getColumnGap();
  376. for (int c = 0; c < span.getColumnCount(); c++) {
  377. Flow flow = (Flow) span.getFlow(c);
  378. renderFlow(flow);
  379. currentIPPosition += offset;
  380. }
  381. currentIPPosition = saveIPPos;
  382. currentBPPosition += span.getHeight();
  383. }
  384. }
  385. /**
  386. * Renders a flow reference area.
  387. *
  388. * @param flow The flow reference area
  389. */
  390. protected void renderFlow(Flow flow) {
  391. // the normal flow reference area contains stacked blocks
  392. List blocks = flow.getChildAreas();
  393. if (blocks != null) {
  394. renderBlocks(null, blocks);
  395. }
  396. }
  397. /**
  398. * Handle block traits.
  399. * This method is called when the correct ip and bp posiiton is
  400. * set. This should be overridden to draw border and background
  401. * traits for the block area.
  402. *
  403. * @param block the block area
  404. */
  405. protected void handleBlockTraits(Block block) {
  406. // draw border and background
  407. }
  408. /**
  409. * Renders a block viewport.
  410. *
  411. * @param bv The block viewport
  412. * @param children The children to render within the block viewport
  413. */
  414. protected void renderBlockViewport(BlockViewport bv, List children) {
  415. // clip and position viewport if necessary
  416. if (bv.getPositioning() == Block.ABSOLUTE) {
  417. // save positions
  418. int saveIP = currentIPPosition;
  419. int saveBP = currentBPPosition;
  420. CTM ctm = bv.getCTM();
  421. currentIPPosition = 0;
  422. currentBPPosition = 0;
  423. startVParea(ctm);
  424. handleBlockTraits(bv);
  425. renderBlocks(bv, children);
  426. endVParea();
  427. // clip if necessary
  428. currentIPPosition = saveIP;
  429. currentBPPosition = saveBP;
  430. } else {
  431. // save position and offset
  432. int saveIP = currentIPPosition;
  433. int saveBP = currentBPPosition;
  434. handleBlockTraits(bv);
  435. renderBlocks(bv, children);
  436. currentIPPosition = saveIP;
  437. currentBPPosition = saveBP + bv.getHeight();
  438. }
  439. }
  440. /**
  441. * Renders a list of block areas.
  442. *
  443. * @param parent the parent block if the parent is a block, otherwise
  444. * a null value.
  445. * @param blocks The block areas
  446. */
  447. protected void renderBlocks(Block parent, List blocks) {
  448. // the position of the containing block is used for
  449. // absolutely positioned areas
  450. int contBP = currentBPPosition;
  451. int contIP = currentIPPosition;
  452. containingBPPosition = contBP;
  453. containingIPPosition = contIP;
  454. for (int count = 0; count < blocks.size(); count++) {
  455. Object obj = blocks.get(count);
  456. if (obj instanceof Block) {
  457. containingBPPosition = contBP;
  458. containingIPPosition = contIP;
  459. renderBlock((Block) obj);
  460. containingBPPosition = contBP;
  461. containingIPPosition = contIP;
  462. } else {
  463. // a line area is rendered from the top left position
  464. // of the line, each inline object is offset from there
  465. LineArea line = (LineArea) obj;
  466. currentBlockIPPosition =
  467. currentIPPosition + line.getStartIndent();
  468. renderLineArea(line);
  469. currentBPPosition += line.getHeight();
  470. }
  471. }
  472. }
  473. /**
  474. * Renders a block area.
  475. *
  476. * @param block The block area
  477. */
  478. protected void renderBlock(Block block) {
  479. List children = block.getChildAreas();
  480. if (children == null) {
  481. handleBlockTraits(block);
  482. // simply move position
  483. currentBPPosition += block.getHeight();
  484. } else if (block instanceof BlockViewport) {
  485. renderBlockViewport((BlockViewport) block, children);
  486. } else {
  487. // save position and offset
  488. int saveIP = currentIPPosition;
  489. int saveBP = currentBPPosition;
  490. if (block.getPositioning() == Block.ABSOLUTE) {
  491. currentIPPosition = containingIPPosition + block.getXOffset();
  492. currentBPPosition = containingBPPosition + block.getYOffset();
  493. handleBlockTraits(block);
  494. renderBlocks(block, children);
  495. // absolute blocks do not effect the layout
  496. currentBPPosition = saveBP;
  497. } else {
  498. // relative blocks are offset
  499. currentIPPosition += block.getXOffset();
  500. currentBPPosition += block.getYOffset();
  501. handleBlockTraits(block);
  502. renderBlocks(block, children);
  503. // stacked and relative blocks effect stacking
  504. currentBPPosition = saveBP + block.getHeight();
  505. }
  506. currentIPPosition = saveIP;
  507. }
  508. }
  509. /**
  510. * Renders a line area. <p>
  511. *
  512. * A line area may have grouped styling for its children such as underline,
  513. * background.</p>
  514. *
  515. * @param line The line area
  516. */
  517. protected void renderLineArea(LineArea line) {
  518. List children = line.getInlineAreas();
  519. for (int count = 0; count < children.size(); count++) {
  520. InlineArea inline = (InlineArea) children.get(count);
  521. renderInlineArea(inline);
  522. }
  523. }
  524. protected void renderInlineArea(InlineArea inlineArea) {
  525. if (inlineArea instanceof TextArea) {
  526. renderText((TextArea) inlineArea);
  527. } else if (inlineArea instanceof InlineParent) {
  528. renderInlineParent((InlineParent) inlineArea);
  529. } else if (inlineArea instanceof Space) {
  530. renderInlineSpace((Space) inlineArea);
  531. } else if (inlineArea instanceof Character) {
  532. renderCharacter((Character) inlineArea);
  533. } else if (inlineArea instanceof Viewport) {
  534. renderViewport((Viewport) inlineArea);
  535. } else if (inlineArea instanceof Leader) {
  536. renderLeader((Leader) inlineArea);
  537. }
  538. }
  539. /** @see org.apache.fop.render.Renderer */
  540. protected void renderCharacter(Character ch) {
  541. currentBlockIPPosition += ch.getWidth();
  542. }
  543. /** @see org.apache.fop.render.Renderer */
  544. protected void renderInlineSpace(Space space) {
  545. // an inline space moves the inline progression position
  546. // for the current block by the width or height of the space
  547. // it may also have styling (only on this object) that needs
  548. // handling
  549. currentBlockIPPosition += space.getWidth();
  550. }
  551. /** @see org.apache.fop.render.Renderer */
  552. protected void renderLeader(Leader area) {
  553. currentBlockIPPosition += area.getWidth();
  554. }
  555. /** @see org.apache.fop.render.Renderer */
  556. protected void renderText(TextArea text) {
  557. currentBlockIPPosition += text.getWidth();
  558. }
  559. /** @see org.apache.fop.render.Renderer */
  560. protected void renderInlineParent(InlineParent ip) {
  561. int saveIP = currentBlockIPPosition;
  562. Iterator iter = ip.getChildAreas().iterator();
  563. while (iter.hasNext()) {
  564. renderInlineArea((InlineArea) iter.next());
  565. }
  566. currentBlockIPPosition = saveIP + ip.getWidth();
  567. }
  568. /** @see org.apache.fop.render.Renderer */
  569. protected void renderViewport(Viewport viewport) {
  570. Area content = viewport.getContent();
  571. int saveBP = currentBPPosition;
  572. currentBPPosition += viewport.getOffset();
  573. Rectangle2D contpos = viewport.getContentPosition();
  574. if (content instanceof Image) {
  575. renderImage((Image) content, contpos);
  576. } else if (content instanceof Container) {
  577. renderContainer((Container) content);
  578. } else if (content instanceof ForeignObject) {
  579. renderForeignObject((ForeignObject) content, contpos);
  580. }
  581. currentBlockIPPosition += viewport.getWidth();
  582. currentBPPosition = saveBP;
  583. }
  584. /**
  585. * Renders an image area.
  586. *
  587. * @param image The image
  588. * @param pos The target position of the image
  589. * (todo) Make renderImage() protected
  590. */
  591. public void renderImage(Image image, Rectangle2D pos) {
  592. // Default: do nothing.
  593. // Some renderers (ex. Text) don't support images.
  594. }
  595. /** @see org.apache.fop.render.Renderer */
  596. public void renderContainer(Container cont) {
  597. int saveIP = currentIPPosition;
  598. currentIPPosition = currentBlockIPPosition;
  599. int saveBlockIP = currentBlockIPPosition;
  600. int saveBP = currentBPPosition;
  601. List blocks = cont.getBlocks();
  602. renderBlocks(null, blocks);
  603. currentIPPosition = saveIP;
  604. currentBlockIPPosition = saveBlockIP;
  605. currentBPPosition = saveBP;
  606. }
  607. /**
  608. * Renders a foreign object area.
  609. *
  610. * @param fo The foreign object area
  611. * @param pos The target position of the foreign object
  612. * (todo) Make renderForeignObject() protected
  613. */
  614. public void renderForeignObject(ForeignObject fo, Rectangle2D pos) {
  615. // Default: do nothing.
  616. // Some renderers (ex. Text) don't support foreign objects.
  617. }
  618. /**
  619. * Set the default xml handler for the given mime type.
  620. * @param mime MIME type
  621. * @param handler XMLHandler to use
  622. */
  623. public void setDefaultXMLHandler(FOUserAgent foua, String mime,
  624. XMLHandler handler) {
  625. foua.defaults.put(mime, handler);
  626. }
  627. /**
  628. * Add an xml handler for the given mime type and xml namespace.
  629. * @param mime MIME type
  630. * @param ns Namespace URI
  631. * @param handler XMLHandler to use
  632. */
  633. public void addXMLHandler(FOUserAgent foua, String mime, String ns,
  634. XMLHandler handler) {
  635. Map mh = (Map) foua.handlers.get(mime);
  636. if (mh == null) {
  637. mh = new java.util.HashMap();
  638. foua.handlers.put(mime, mh);
  639. }
  640. mh.put(ns, handler);
  641. }
  642. /**
  643. * Render the xml document with the given xml namespace.
  644. * The Render Context is by the handle to render into the current
  645. * rendering target.
  646. * @param ctx rendering context
  647. * @param doc DOM Document containing the source document
  648. * @param namespace Namespace URI of the document
  649. */
  650. public void renderXML(FOUserAgent foua, RendererContext ctx, Document doc,
  651. String namespace) {
  652. String mime = ctx.getMimeType();
  653. Map mh = (Map) foua.handlers.get(mime);
  654. XMLHandler handler = null;
  655. if (mh != null) {
  656. handler = (XMLHandler) mh.get(namespace);
  657. }
  658. if (handler == null) {
  659. handler = (XMLHandler) foua.defaults.get(mime);
  660. }
  661. if (handler != null) {
  662. try {
  663. handler.handleXML(ctx, doc, namespace);
  664. } catch (Throwable t) {
  665. // could not handle document
  666. getLogger().error("Some XML content will be ignored. "
  667. + "Could not render XML", t);
  668. }
  669. } else {
  670. // no handler found for document
  671. getLogger().warn("Some XML content will be ignored. "
  672. + "No handler defined for XML: " + namespace);
  673. }
  674. }
  675. }