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.

IFSerializer.java 28KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775
  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.intermediate;
  19. import java.awt.Color;
  20. import java.awt.Dimension;
  21. import java.awt.Paint;
  22. import java.awt.Point;
  23. import java.awt.Rectangle;
  24. import java.awt.geom.AffineTransform;
  25. import java.util.Iterator;
  26. import java.util.List;
  27. import java.util.Locale;
  28. import java.util.Map;
  29. import org.w3c.dom.Document;
  30. import org.w3c.dom.Node;
  31. import org.w3c.dom.NodeList;
  32. import org.xml.sax.SAXException;
  33. import org.xml.sax.helpers.AttributesImpl;
  34. import org.apache.xmlgraphics.util.QName;
  35. import org.apache.xmlgraphics.util.XMLizable;
  36. import org.apache.fop.accessibility.StructureTree;
  37. import org.apache.fop.fonts.FontInfo;
  38. import org.apache.fop.render.PrintRendererConfigurator;
  39. import org.apache.fop.render.RenderingContext;
  40. import org.apache.fop.render.intermediate.extensions.AbstractAction;
  41. import org.apache.fop.render.intermediate.extensions.Bookmark;
  42. import org.apache.fop.render.intermediate.extensions.BookmarkTree;
  43. import org.apache.fop.render.intermediate.extensions.DocumentNavigationExtensionConstants;
  44. import org.apache.fop.render.intermediate.extensions.Link;
  45. import org.apache.fop.render.intermediate.extensions.NamedDestination;
  46. import org.apache.fop.traits.BorderProps;
  47. import org.apache.fop.traits.RuleStyle;
  48. import org.apache.fop.util.ColorUtil;
  49. import org.apache.fop.util.DOM2SAX;
  50. import org.apache.fop.util.XMLConstants;
  51. import org.apache.fop.util.XMLUtil;
  52. /**
  53. * IFPainter implementation that serializes the intermediate format to XML.
  54. */
  55. public class IFSerializer extends AbstractXMLWritingIFDocumentHandler
  56. implements IFConstants, IFPainter, IFDocumentNavigationHandler {
  57. private IFDocumentHandler mimicHandler;
  58. private int pageSequenceIndex; // used for accessibility
  59. /** Holds the intermediate format state */
  60. private IFState state;
  61. /**
  62. * Default constructor.
  63. */
  64. public IFSerializer() {
  65. }
  66. /** {@inheritDoc} */
  67. protected String getMainNamespace() {
  68. return NAMESPACE;
  69. }
  70. /** {@inheritDoc} */
  71. public boolean supportsPagesOutOfOrder() {
  72. return false;
  73. //Theoretically supported but disabled to improve performance when
  74. //rendering the IF to the final format later on
  75. }
  76. /** {@inheritDoc} */
  77. public String getMimeType() {
  78. return MIME_TYPE;
  79. }
  80. /** {@inheritDoc} */
  81. public IFDocumentHandlerConfigurator getConfigurator() {
  82. if (this.mimicHandler != null) {
  83. return getMimickedDocumentHandler().getConfigurator();
  84. } else {
  85. return new PrintRendererConfigurator(getUserAgent());
  86. }
  87. }
  88. /** {@inheritDoc} */
  89. public IFDocumentNavigationHandler getDocumentNavigationHandler() {
  90. return this;
  91. }
  92. /**
  93. * Tells this serializer to mimic the given document handler (mostly applies to the font set
  94. * that is used during layout).
  95. * @param targetHandler the document handler to mimic
  96. */
  97. public void mimicDocumentHandler(IFDocumentHandler targetHandler) {
  98. this.mimicHandler = targetHandler;
  99. }
  100. /**
  101. * Returns the document handler that is being mimicked by this serializer.
  102. * @return the mimicked document handler or null if no such document handler has been set
  103. */
  104. public IFDocumentHandler getMimickedDocumentHandler() {
  105. return this.mimicHandler;
  106. }
  107. /** {@inheritDoc} */
  108. public FontInfo getFontInfo() {
  109. if (this.mimicHandler != null) {
  110. return this.mimicHandler.getFontInfo();
  111. } else {
  112. return null;
  113. }
  114. }
  115. /** {@inheritDoc} */
  116. public void setFontInfo(FontInfo fontInfo) {
  117. if (this.mimicHandler != null) {
  118. this.mimicHandler.setFontInfo(fontInfo);
  119. }
  120. }
  121. /** {@inheritDoc} */
  122. public void setDefaultFontInfo(FontInfo fontInfo) {
  123. if (this.mimicHandler != null) {
  124. this.mimicHandler.setDefaultFontInfo(fontInfo);
  125. }
  126. }
  127. /** {@inheritDoc} */
  128. public void startDocument() throws IFException {
  129. super.startDocument();
  130. try {
  131. handler.startDocument();
  132. handler.startPrefixMapping("", NAMESPACE);
  133. handler.startPrefixMapping(XLINK_PREFIX, XLINK_NAMESPACE);
  134. handler.startPrefixMapping(DocumentNavigationExtensionConstants.PREFIX,
  135. DocumentNavigationExtensionConstants.NAMESPACE);
  136. handler.startElement(EL_DOCUMENT);
  137. } catch (SAXException e) {
  138. throw new IFException("SAX error in startDocument()", e);
  139. }
  140. }
  141. /** {@inheritDoc} */
  142. public void startDocumentHeader() throws IFException {
  143. try {
  144. handler.startElement(EL_HEADER);
  145. } catch (SAXException e) {
  146. throw new IFException("SAX error in startDocumentHeader()", e);
  147. }
  148. }
  149. /** {@inheritDoc} */
  150. public void endDocumentHeader() throws IFException {
  151. try {
  152. handler.endElement(EL_HEADER);
  153. } catch (SAXException e) {
  154. throw new IFException("SAX error in startDocumentHeader()", e);
  155. }
  156. }
  157. /** {@inheritDoc} */
  158. public void startDocumentTrailer() throws IFException {
  159. try {
  160. handler.startElement(EL_TRAILER);
  161. } catch (SAXException e) {
  162. throw new IFException("SAX error in startDocumentTrailer()", e);
  163. }
  164. }
  165. /** {@inheritDoc} */
  166. public void endDocumentTrailer() throws IFException {
  167. try {
  168. handler.endElement(EL_TRAILER);
  169. } catch (SAXException e) {
  170. throw new IFException("SAX error in endDocumentTrailer()", e);
  171. }
  172. }
  173. /** {@inheritDoc} */
  174. public void endDocument() throws IFException {
  175. try {
  176. handler.endElement(EL_DOCUMENT);
  177. handler.endDocument();
  178. finishDocumentNavigation();
  179. } catch (SAXException e) {
  180. throw new IFException("SAX error in endDocument()", e);
  181. }
  182. }
  183. /** {@inheritDoc} */
  184. public void startPageSequence(String id) throws IFException {
  185. try {
  186. AttributesImpl atts = new AttributesImpl();
  187. if (id != null) {
  188. atts.addAttribute(XML_NAMESPACE, "id", "xml:id", XMLUtil.CDATA, id);
  189. }
  190. Locale lang = getContext().getLanguage();
  191. if (lang != null) {
  192. atts.addAttribute(XML_NAMESPACE, "lang", "xml:lang", XMLUtil.CDATA,
  193. XMLUtil.toRFC3066(lang));
  194. }
  195. addForeignAttributes(atts);
  196. handler.startElement(EL_PAGE_SEQUENCE, atts);
  197. if (this.getUserAgent().isAccessibilityEnabled()) {
  198. StructureTree structureTree = getUserAgent().getStructureTree();
  199. handler.startElement(EL_STRUCTURE_TREE); // add structure tree
  200. NodeList nodes = structureTree.getPageSequence(pageSequenceIndex++);
  201. for (int i = 0, n = nodes.getLength(); i < n; i++) {
  202. Node node = nodes.item(i);
  203. new DOM2SAX(handler).writeFragment(node);
  204. }
  205. handler.endElement(EL_STRUCTURE_TREE);
  206. }
  207. } catch (SAXException e) {
  208. throw new IFException("SAX error in startPageSequence()", e);
  209. }
  210. }
  211. /** {@inheritDoc} */
  212. public void endPageSequence() throws IFException {
  213. try {
  214. handler.endElement(EL_PAGE_SEQUENCE);
  215. } catch (SAXException e) {
  216. throw new IFException("SAX error in endPageSequence()", e);
  217. }
  218. }
  219. /** {@inheritDoc} */
  220. public void startPage(int index, String name, String pageMasterName, Dimension size)
  221. throws IFException {
  222. try {
  223. AttributesImpl atts = new AttributesImpl();
  224. addAttribute(atts, "index", Integer.toString(index));
  225. addAttribute(atts, "name", name);
  226. addAttribute(atts, "page-master-name", pageMasterName);
  227. addAttribute(atts, "width", Integer.toString(size.width));
  228. addAttribute(atts, "height", Integer.toString(size.height));
  229. addForeignAttributes(atts);
  230. handler.startElement(EL_PAGE, atts);
  231. } catch (SAXException e) {
  232. throw new IFException("SAX error in startPage()", e);
  233. }
  234. }
  235. /** {@inheritDoc} */
  236. public void startPageHeader() throws IFException {
  237. try {
  238. handler.startElement(EL_PAGE_HEADER);
  239. } catch (SAXException e) {
  240. throw new IFException("SAX error in startPageHeader()", e);
  241. }
  242. }
  243. /** {@inheritDoc} */
  244. public void endPageHeader() throws IFException {
  245. try {
  246. handler.endElement(EL_PAGE_HEADER);
  247. } catch (SAXException e) {
  248. throw new IFException("SAX error in endPageHeader()", e);
  249. }
  250. }
  251. /** {@inheritDoc} */
  252. public IFPainter startPageContent() throws IFException {
  253. try {
  254. handler.startElement(EL_PAGE_CONTENT);
  255. this.state = IFState.create();
  256. return this;
  257. } catch (SAXException e) {
  258. throw new IFException("SAX error in startPageContent()", e);
  259. }
  260. }
  261. /** {@inheritDoc} */
  262. public void endPageContent() throws IFException {
  263. try {
  264. this.state = null;
  265. handler.endElement(EL_PAGE_CONTENT);
  266. } catch (SAXException e) {
  267. throw new IFException("SAX error in endPageContent()", e);
  268. }
  269. }
  270. /** {@inheritDoc} */
  271. public void startPageTrailer() throws IFException {
  272. try {
  273. handler.startElement(EL_PAGE_TRAILER);
  274. } catch (SAXException e) {
  275. throw new IFException("SAX error in startPageTrailer()", e);
  276. }
  277. }
  278. /** {@inheritDoc} */
  279. public void endPageTrailer() throws IFException {
  280. try {
  281. commitNavigation();
  282. handler.endElement(EL_PAGE_TRAILER);
  283. } catch (SAXException e) {
  284. throw new IFException("SAX error in endPageTrailer()", e);
  285. }
  286. }
  287. /** {@inheritDoc} */
  288. public void endPage() throws IFException {
  289. try {
  290. handler.endElement(EL_PAGE);
  291. } catch (SAXException e) {
  292. throw new IFException("SAX error in endPage()", e);
  293. }
  294. }
  295. //---=== IFPainter ===---
  296. /** {@inheritDoc} */
  297. public void startViewport(AffineTransform transform, Dimension size, Rectangle clipRect)
  298. throws IFException {
  299. startViewport(IFUtil.toString(transform), size, clipRect);
  300. }
  301. /** {@inheritDoc} */
  302. public void startViewport(AffineTransform[] transforms, Dimension size, Rectangle clipRect)
  303. throws IFException {
  304. startViewport(IFUtil.toString(transforms), size, clipRect);
  305. }
  306. private void startViewport(String transform, Dimension size, Rectangle clipRect)
  307. throws IFException {
  308. try {
  309. AttributesImpl atts = new AttributesImpl();
  310. if (transform != null && transform.length() > 0) {
  311. addAttribute(atts, "transform", transform);
  312. }
  313. addAttribute(atts, "width", Integer.toString(size.width));
  314. addAttribute(atts, "height", Integer.toString(size.height));
  315. if (clipRect != null) {
  316. addAttribute(atts, "clip-rect", IFUtil.toString(clipRect));
  317. }
  318. handler.startElement(EL_VIEWPORT, atts);
  319. } catch (SAXException e) {
  320. throw new IFException("SAX error in startViewport()", e);
  321. }
  322. }
  323. /** {@inheritDoc} */
  324. public void endViewport() throws IFException {
  325. try {
  326. handler.endElement(EL_VIEWPORT);
  327. } catch (SAXException e) {
  328. throw new IFException("SAX error in endViewport()", e);
  329. }
  330. }
  331. /** {@inheritDoc} */
  332. public void startGroup(AffineTransform[] transforms) throws IFException {
  333. startGroup(IFUtil.toString(transforms));
  334. }
  335. /** {@inheritDoc} */
  336. public void startGroup(AffineTransform transform) throws IFException {
  337. startGroup(IFUtil.toString(transform));
  338. }
  339. private void startGroup(String transform) throws IFException {
  340. try {
  341. AttributesImpl atts = new AttributesImpl();
  342. if (transform != null && transform.length() > 0) {
  343. addAttribute(atts, "transform", transform);
  344. }
  345. handler.startElement(EL_GROUP, atts);
  346. } catch (SAXException e) {
  347. throw new IFException("SAX error in startGroup()", e);
  348. }
  349. }
  350. /** {@inheritDoc} */
  351. public void endGroup() throws IFException {
  352. try {
  353. handler.endElement(EL_GROUP);
  354. } catch (SAXException e) {
  355. throw new IFException("SAX error in endGroup()", e);
  356. }
  357. }
  358. /** {@inheritDoc} */
  359. public void drawImage(String uri, Rectangle rect) throws IFException {
  360. try {
  361. AttributesImpl atts = new AttributesImpl();
  362. addAttribute(atts, XLINK_HREF, uri);
  363. addAttribute(atts, "x", Integer.toString(rect.x));
  364. addAttribute(atts, "y", Integer.toString(rect.y));
  365. addAttribute(atts, "width", Integer.toString(rect.width));
  366. addAttribute(atts, "height", Integer.toString(rect.height));
  367. addForeignAttributes(atts);
  368. addStructurePointerAttribute(atts);
  369. handler.element(EL_IMAGE, atts);
  370. } catch (SAXException e) {
  371. throw new IFException("SAX error in startGroup()", e);
  372. }
  373. }
  374. private void addForeignAttributes(AttributesImpl atts) throws SAXException {
  375. Map foreignAttributes = getContext().getForeignAttributes();
  376. if (!foreignAttributes.isEmpty()) {
  377. Iterator iter = foreignAttributes.entrySet().iterator();
  378. while (iter.hasNext()) {
  379. Map.Entry entry = (Map.Entry)iter.next();
  380. addAttribute(atts, (QName)entry.getKey(), entry.getValue().toString());
  381. }
  382. }
  383. }
  384. /** {@inheritDoc} */
  385. public void drawImage(Document doc, Rectangle rect) throws IFException {
  386. try {
  387. AttributesImpl atts = new AttributesImpl();
  388. addAttribute(atts, "x", Integer.toString(rect.x));
  389. addAttribute(atts, "y", Integer.toString(rect.y));
  390. addAttribute(atts, "width", Integer.toString(rect.width));
  391. addAttribute(atts, "height", Integer.toString(rect.height));
  392. addForeignAttributes(atts);
  393. addStructurePointerAttribute(atts);
  394. handler.startElement(EL_IMAGE, atts);
  395. new DOM2SAX(handler).writeDocument(doc, true);
  396. handler.endElement(EL_IMAGE);
  397. } catch (SAXException e) {
  398. throw new IFException("SAX error in startGroup()", e);
  399. }
  400. }
  401. private static String toString(Paint paint) {
  402. if (paint instanceof Color) {
  403. return ColorUtil.colorToString((Color)paint);
  404. } else {
  405. throw new UnsupportedOperationException("Paint not supported: " + paint);
  406. }
  407. }
  408. /** {@inheritDoc} */
  409. public void clipRect(Rectangle rect) throws IFException {
  410. try {
  411. AttributesImpl atts = new AttributesImpl();
  412. addAttribute(atts, "x", Integer.toString(rect.x));
  413. addAttribute(atts, "y", Integer.toString(rect.y));
  414. addAttribute(atts, "width", Integer.toString(rect.width));
  415. addAttribute(atts, "height", Integer.toString(rect.height));
  416. handler.element(EL_CLIP_RECT, atts);
  417. } catch (SAXException e) {
  418. throw new IFException("SAX error in clipRect()", e);
  419. }
  420. }
  421. /** {@inheritDoc} */
  422. public void fillRect(Rectangle rect, Paint fill) throws IFException {
  423. if (fill == null) {
  424. return;
  425. }
  426. try {
  427. AttributesImpl atts = new AttributesImpl();
  428. addAttribute(atts, "x", Integer.toString(rect.x));
  429. addAttribute(atts, "y", Integer.toString(rect.y));
  430. addAttribute(atts, "width", Integer.toString(rect.width));
  431. addAttribute(atts, "height", Integer.toString(rect.height));
  432. addAttribute(atts, "fill", toString(fill));
  433. handler.element(EL_RECT, atts);
  434. } catch (SAXException e) {
  435. throw new IFException("SAX error in fillRect()", e);
  436. }
  437. }
  438. /** {@inheritDoc} */
  439. public void drawBorderRect(Rectangle rect, BorderProps before, BorderProps after,
  440. BorderProps start, BorderProps end) throws IFException {
  441. if (before == null && after == null && start == null && end == null) {
  442. return;
  443. }
  444. try {
  445. AttributesImpl atts = new AttributesImpl();
  446. addAttribute(atts, "x", Integer.toString(rect.x));
  447. addAttribute(atts, "y", Integer.toString(rect.y));
  448. addAttribute(atts, "width", Integer.toString(rect.width));
  449. addAttribute(atts, "height", Integer.toString(rect.height));
  450. if (before != null) {
  451. addAttribute(atts, "before", before.toString());
  452. }
  453. if (after != null) {
  454. addAttribute(atts, "after", after.toString());
  455. }
  456. if (start != null) {
  457. addAttribute(atts, "start", start.toString());
  458. }
  459. if (end != null) {
  460. addAttribute(atts, "end", end.toString());
  461. }
  462. handler.element(EL_BORDER_RECT, atts);
  463. } catch (SAXException e) {
  464. throw new IFException("SAX error in drawBorderRect()", e);
  465. }
  466. }
  467. /** {@inheritDoc} */
  468. public void drawLine(Point start, Point end, int width, Color color, RuleStyle style)
  469. throws IFException {
  470. try {
  471. AttributesImpl atts = new AttributesImpl();
  472. addAttribute(atts, "x1", Integer.toString(start.x));
  473. addAttribute(atts, "y1", Integer.toString(start.y));
  474. addAttribute(atts, "x2", Integer.toString(end.x));
  475. addAttribute(atts, "y2", Integer.toString(end.y));
  476. addAttribute(atts, "stroke-width", Integer.toString(width));
  477. addAttribute(atts, "color", ColorUtil.colorToString(color));
  478. addAttribute(atts, "style", style.getName());
  479. handler.element(EL_LINE, atts);
  480. } catch (SAXException e) {
  481. throw new IFException("SAX error in drawLine()", e);
  482. }
  483. }
  484. /** {@inheritDoc} */
  485. public void drawText(int x, int y, int letterSpacing, int wordSpacing,
  486. int[] dx, String text) throws IFException {
  487. try {
  488. AttributesImpl atts = new AttributesImpl();
  489. XMLUtil.addAttribute(atts, XMLConstants.XML_SPACE, "preserve");
  490. addAttribute(atts, "x", Integer.toString(x));
  491. addAttribute(atts, "y", Integer.toString(y));
  492. if (letterSpacing != 0) {
  493. addAttribute(atts, "letter-spacing", Integer.toString(letterSpacing));
  494. }
  495. if (wordSpacing != 0) {
  496. addAttribute(atts, "word-spacing", Integer.toString(wordSpacing));
  497. }
  498. if (dx != null) {
  499. addAttribute(atts, "dx", IFUtil.toString(dx));
  500. }
  501. addStructurePointerAttribute(atts);
  502. handler.startElement(EL_TEXT, atts);
  503. char[] chars = text.toCharArray();
  504. handler.characters(chars, 0, chars.length);
  505. handler.endElement(EL_TEXT);
  506. } catch (SAXException e) {
  507. throw new IFException("SAX error in setFont()", e);
  508. }
  509. }
  510. /** {@inheritDoc} */
  511. public void setFont(String family, String style, Integer weight, String variant, Integer size,
  512. Color color) throws IFException {
  513. try {
  514. AttributesImpl atts = new AttributesImpl();
  515. boolean changed;
  516. if (family != null) {
  517. changed = !family.equals(state.getFontFamily());
  518. if (changed) {
  519. state.setFontFamily(family);
  520. addAttribute(atts, "family", family);
  521. }
  522. }
  523. if (style != null) {
  524. changed = !style.equals(state.getFontStyle());
  525. if (changed) {
  526. state.setFontStyle(style);
  527. addAttribute(atts, "style", style);
  528. }
  529. }
  530. if (weight != null) {
  531. changed = (weight.intValue() != state.getFontWeight());
  532. if (changed) {
  533. state.setFontWeight(weight.intValue());
  534. addAttribute(atts, "weight", weight.toString());
  535. }
  536. }
  537. if (variant != null) {
  538. changed = !variant.equals(state.getFontVariant());
  539. if (changed) {
  540. state.setFontVariant(variant);
  541. addAttribute(atts, "variant", variant);
  542. }
  543. }
  544. if (size != null) {
  545. changed = (size.intValue() != state.getFontSize());
  546. if (changed) {
  547. state.setFontSize(size.intValue());
  548. addAttribute(atts, "size", size.toString());
  549. }
  550. }
  551. if (color != null) {
  552. changed = !color.equals(state.getTextColor());
  553. if (changed) {
  554. state.setTextColor(color);
  555. addAttribute(atts, "color", toString(color));
  556. }
  557. }
  558. if (atts.getLength() > 0) {
  559. handler.element(EL_FONT, atts);
  560. }
  561. } catch (SAXException e) {
  562. throw new IFException("SAX error in setFont()", e);
  563. }
  564. }
  565. /** {@inheritDoc} */
  566. public void handleExtensionObject(Object extension) throws IFException {
  567. if (extension instanceof XMLizable) {
  568. try {
  569. ((XMLizable)extension).toSAX(this.handler);
  570. } catch (SAXException e) {
  571. throw new IFException("SAX error while handling extension object", e);
  572. }
  573. } else {
  574. throw new UnsupportedOperationException(
  575. "Extension must implement XMLizable: "
  576. + extension + " (" + extension.getClass().getName() + ")");
  577. }
  578. }
  579. /** {@inheritDoc} */
  580. protected RenderingContext createRenderingContext() {
  581. throw new IllegalStateException("Should never be called!");
  582. }
  583. private void addAttribute(AttributesImpl atts,
  584. org.apache.xmlgraphics.util.QName attribute, String value) throws SAXException {
  585. handler.startPrefixMapping(attribute.getPrefix(), attribute.getNamespaceURI());
  586. XMLUtil.addAttribute(atts, attribute, value);
  587. }
  588. private void addAttribute(AttributesImpl atts, String localName, String value) {
  589. XMLUtil.addAttribute(atts, localName, value);
  590. }
  591. private void addStructurePointerAttribute(AttributesImpl atts) {
  592. String ptr = getContext().getStructurePointer();
  593. if (ptr != null) {
  594. addAttribute(atts, "ptr", ptr);
  595. }
  596. }
  597. // ---=== IFDocumentNavigationHandler ===---
  598. private Map incompleteActions = new java.util.HashMap();
  599. private List completeActions = new java.util.LinkedList();
  600. private void noteAction(AbstractAction action) {
  601. if (action == null) {
  602. throw new NullPointerException("action must not be null");
  603. }
  604. if (!action.isComplete()) {
  605. assert action.hasID();
  606. incompleteActions.put(action.getID(), action);
  607. }
  608. }
  609. /** {@inheritDoc} */
  610. public void renderNamedDestination(NamedDestination destination) throws IFException {
  611. noteAction(destination.getAction());
  612. AttributesImpl atts = new AttributesImpl();
  613. atts.addAttribute(null, "name", "name", XMLConstants.CDATA, destination.getName());
  614. try {
  615. handler.startElement(DocumentNavigationExtensionConstants.NAMED_DESTINATION, atts);
  616. serializeXMLizable(destination.getAction());
  617. handler.endElement(DocumentNavigationExtensionConstants.NAMED_DESTINATION);
  618. } catch (SAXException e) {
  619. throw new IFException("SAX error serializing named destination", e);
  620. }
  621. }
  622. /** {@inheritDoc} */
  623. public void renderBookmarkTree(BookmarkTree tree) throws IFException {
  624. AttributesImpl atts = new AttributesImpl();
  625. try {
  626. handler.startElement(DocumentNavigationExtensionConstants.BOOKMARK_TREE, atts);
  627. Iterator iter = tree.getBookmarks().iterator();
  628. while (iter.hasNext()) {
  629. Bookmark b = (Bookmark)iter.next();
  630. serializeBookmark(b);
  631. }
  632. handler.endElement(DocumentNavigationExtensionConstants.BOOKMARK_TREE);
  633. } catch (SAXException e) {
  634. throw new IFException("SAX error serializing bookmark tree", e);
  635. }
  636. }
  637. private void serializeBookmark(Bookmark bookmark) throws SAXException, IFException {
  638. noteAction(bookmark.getAction());
  639. AttributesImpl atts = new AttributesImpl();
  640. atts.addAttribute(null, "title", "title", XMLUtil.CDATA, bookmark.getTitle());
  641. atts.addAttribute(null, "starting-state", "starting-state",
  642. XMLUtil.CDATA, bookmark.isShown() ? "show" : "hide");
  643. handler.startElement(DocumentNavigationExtensionConstants.BOOKMARK, atts);
  644. serializeXMLizable(bookmark.getAction());
  645. Iterator iter = bookmark.getChildBookmarks().iterator();
  646. while (iter.hasNext()) {
  647. Bookmark b = (Bookmark)iter.next();
  648. serializeBookmark(b);
  649. }
  650. handler.endElement(DocumentNavigationExtensionConstants.BOOKMARK);
  651. }
  652. /** {@inheritDoc} */
  653. public void renderLink(Link link) throws IFException {
  654. noteAction(link.getAction());
  655. AttributesImpl atts = new AttributesImpl();
  656. atts.addAttribute(null, "rect", "rect",
  657. XMLConstants.CDATA, IFUtil.toString(link.getTargetRect()));
  658. if (getUserAgent().isAccessibilityEnabled()) {
  659. addAttribute(atts, "ptr", link.getAction().getStructurePointer());
  660. }
  661. try {
  662. handler.startElement(DocumentNavigationExtensionConstants.LINK, atts);
  663. serializeXMLizable(link.getAction());
  664. handler.endElement(DocumentNavigationExtensionConstants.LINK);
  665. } catch (SAXException e) {
  666. throw new IFException("SAX error serializing link", e);
  667. }
  668. }
  669. /** {@inheritDoc} */
  670. public void addResolvedAction(AbstractAction action) throws IFException {
  671. assert action.isComplete();
  672. assert action.hasID();
  673. AbstractAction noted = (AbstractAction)incompleteActions.remove(action.getID());
  674. if (noted != null) {
  675. completeActions.add(action);
  676. } else {
  677. //ignore as it was already complete when it was first used.
  678. }
  679. }
  680. private void commitNavigation() throws IFException {
  681. Iterator iter = this.completeActions.iterator();
  682. while (iter.hasNext()) {
  683. AbstractAction action = (AbstractAction)iter.next();
  684. iter.remove();
  685. serializeXMLizable(action);
  686. }
  687. assert this.completeActions.size() == 0;
  688. }
  689. private void finishDocumentNavigation() {
  690. assert this.incompleteActions.size() == 0 : "Still holding incomplete actions!";
  691. }
  692. private void serializeXMLizable(XMLizable object) throws IFException {
  693. try {
  694. object.toSAX(handler);
  695. } catch (SAXException e) {
  696. throw new IFException("SAX error serializing object", e);
  697. }
  698. }
  699. }