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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778
  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. XMLUtil.addAttribute(atts, XMLConstants.XML_SPACE, "preserve");
  196. addForeignAttributes(atts);
  197. handler.startElement(EL_PAGE_SEQUENCE, atts);
  198. if (this.getUserAgent().isAccessibilityEnabled()) {
  199. StructureTree structureTree = getUserAgent().getStructureTree();
  200. handler.startElement(EL_STRUCTURE_TREE); // add structure tree
  201. NodeList nodes = structureTree.getPageSequence(pageSequenceIndex++);
  202. for (int i = 0, n = nodes.getLength(); i < n; i++) {
  203. Node node = nodes.item(i);
  204. new DOM2SAX(handler).writeFragment(node);
  205. }
  206. handler.endElement(EL_STRUCTURE_TREE);
  207. }
  208. } catch (SAXException e) {
  209. throw new IFException("SAX error in startPageSequence()", e);
  210. }
  211. }
  212. /** {@inheritDoc} */
  213. public void endPageSequence() throws IFException {
  214. try {
  215. handler.endElement(EL_PAGE_SEQUENCE);
  216. } catch (SAXException e) {
  217. throw new IFException("SAX error in endPageSequence()", e);
  218. }
  219. }
  220. /** {@inheritDoc} */
  221. public void startPage(int index, String name, String pageMasterName, Dimension size)
  222. throws IFException {
  223. try {
  224. AttributesImpl atts = new AttributesImpl();
  225. addAttribute(atts, "index", Integer.toString(index));
  226. addAttribute(atts, "name", name);
  227. addAttribute(atts, "page-master-name", pageMasterName);
  228. addAttribute(atts, "width", Integer.toString(size.width));
  229. addAttribute(atts, "height", Integer.toString(size.height));
  230. addForeignAttributes(atts);
  231. handler.startElement(EL_PAGE, atts);
  232. } catch (SAXException e) {
  233. throw new IFException("SAX error in startPage()", e);
  234. }
  235. }
  236. /** {@inheritDoc} */
  237. public void startPageHeader() throws IFException {
  238. try {
  239. handler.startElement(EL_PAGE_HEADER);
  240. } catch (SAXException e) {
  241. throw new IFException("SAX error in startPageHeader()", e);
  242. }
  243. }
  244. /** {@inheritDoc} */
  245. public void endPageHeader() throws IFException {
  246. try {
  247. handler.endElement(EL_PAGE_HEADER);
  248. } catch (SAXException e) {
  249. throw new IFException("SAX error in endPageHeader()", e);
  250. }
  251. }
  252. /** {@inheritDoc} */
  253. public IFPainter startPageContent() throws IFException {
  254. try {
  255. handler.startElement(EL_PAGE_CONTENT);
  256. this.state = IFState.create();
  257. return this;
  258. } catch (SAXException e) {
  259. throw new IFException("SAX error in startPageContent()", e);
  260. }
  261. }
  262. /** {@inheritDoc} */
  263. public void endPageContent() throws IFException {
  264. try {
  265. this.state = null;
  266. handler.endElement(EL_PAGE_CONTENT);
  267. } catch (SAXException e) {
  268. throw new IFException("SAX error in endPageContent()", e);
  269. }
  270. }
  271. /** {@inheritDoc} */
  272. public void startPageTrailer() throws IFException {
  273. try {
  274. handler.startElement(EL_PAGE_TRAILER);
  275. } catch (SAXException e) {
  276. throw new IFException("SAX error in startPageTrailer()", e);
  277. }
  278. }
  279. /** {@inheritDoc} */
  280. public void endPageTrailer() throws IFException {
  281. try {
  282. commitNavigation();
  283. handler.endElement(EL_PAGE_TRAILER);
  284. } catch (SAXException e) {
  285. throw new IFException("SAX error in endPageTrailer()", e);
  286. }
  287. }
  288. /** {@inheritDoc} */
  289. public void endPage() throws IFException {
  290. try {
  291. handler.endElement(EL_PAGE);
  292. } catch (SAXException e) {
  293. throw new IFException("SAX error in endPage()", e);
  294. }
  295. }
  296. //---=== IFPainter ===---
  297. /** {@inheritDoc} */
  298. public void startViewport(AffineTransform transform, Dimension size, Rectangle clipRect)
  299. throws IFException {
  300. startViewport(IFUtil.toString(transform), size, clipRect);
  301. }
  302. /** {@inheritDoc} */
  303. public void startViewport(AffineTransform[] transforms, Dimension size, Rectangle clipRect)
  304. throws IFException {
  305. startViewport(IFUtil.toString(transforms), size, clipRect);
  306. }
  307. private void startViewport(String transform, Dimension size, Rectangle clipRect)
  308. throws IFException {
  309. try {
  310. AttributesImpl atts = new AttributesImpl();
  311. if (transform != null && transform.length() > 0) {
  312. addAttribute(atts, "transform", transform);
  313. }
  314. addAttribute(atts, "width", Integer.toString(size.width));
  315. addAttribute(atts, "height", Integer.toString(size.height));
  316. if (clipRect != null) {
  317. addAttribute(atts, "clip-rect", IFUtil.toString(clipRect));
  318. }
  319. handler.startElement(EL_VIEWPORT, atts);
  320. } catch (SAXException e) {
  321. throw new IFException("SAX error in startViewport()", e);
  322. }
  323. }
  324. /** {@inheritDoc} */
  325. public void endViewport() throws IFException {
  326. try {
  327. handler.endElement(EL_VIEWPORT);
  328. } catch (SAXException e) {
  329. throw new IFException("SAX error in endViewport()", e);
  330. }
  331. }
  332. /** {@inheritDoc} */
  333. public void startGroup(AffineTransform[] transforms) throws IFException {
  334. startGroup(IFUtil.toString(transforms));
  335. }
  336. /** {@inheritDoc} */
  337. public void startGroup(AffineTransform transform) throws IFException {
  338. startGroup(IFUtil.toString(transform));
  339. }
  340. private void startGroup(String transform) throws IFException {
  341. try {
  342. AttributesImpl atts = new AttributesImpl();
  343. if (transform != null && transform.length() > 0) {
  344. addAttribute(atts, "transform", transform);
  345. }
  346. handler.startElement(EL_GROUP, atts);
  347. } catch (SAXException e) {
  348. throw new IFException("SAX error in startGroup()", e);
  349. }
  350. }
  351. /** {@inheritDoc} */
  352. public void endGroup() throws IFException {
  353. try {
  354. handler.endElement(EL_GROUP);
  355. } catch (SAXException e) {
  356. throw new IFException("SAX error in endGroup()", e);
  357. }
  358. }
  359. /** {@inheritDoc} */
  360. public void drawImage(String uri, Rectangle rect) throws IFException {
  361. try {
  362. AttributesImpl atts = new AttributesImpl();
  363. addAttribute(atts, XLINK_HREF, uri);
  364. addAttribute(atts, "x", Integer.toString(rect.x));
  365. addAttribute(atts, "y", Integer.toString(rect.y));
  366. addAttribute(atts, "width", Integer.toString(rect.width));
  367. addAttribute(atts, "height", Integer.toString(rect.height));
  368. addForeignAttributes(atts);
  369. addStructurePointerAttribute(atts);
  370. handler.element(EL_IMAGE, atts);
  371. } catch (SAXException e) {
  372. throw new IFException("SAX error in startGroup()", e);
  373. }
  374. }
  375. private void addForeignAttributes(AttributesImpl atts) throws SAXException {
  376. Map foreignAttributes = getContext().getForeignAttributes();
  377. if (!foreignAttributes.isEmpty()) {
  378. Iterator iter = foreignAttributes.entrySet().iterator();
  379. while (iter.hasNext()) {
  380. Map.Entry entry = (Map.Entry)iter.next();
  381. addAttribute(atts, (QName)entry.getKey(), entry.getValue().toString());
  382. }
  383. }
  384. }
  385. /** {@inheritDoc} */
  386. public void drawImage(Document doc, Rectangle rect) throws IFException {
  387. try {
  388. AttributesImpl atts = new AttributesImpl();
  389. addAttribute(atts, "x", Integer.toString(rect.x));
  390. addAttribute(atts, "y", Integer.toString(rect.y));
  391. addAttribute(atts, "width", Integer.toString(rect.width));
  392. addAttribute(atts, "height", Integer.toString(rect.height));
  393. addForeignAttributes(atts);
  394. addStructurePointerAttribute(atts);
  395. handler.startElement(EL_IMAGE, atts);
  396. new DOM2SAX(handler).writeDocument(doc, true);
  397. handler.endElement(EL_IMAGE);
  398. } catch (SAXException e) {
  399. throw new IFException("SAX error in startGroup()", e);
  400. }
  401. }
  402. private static String toString(Paint paint) {
  403. if (paint instanceof Color) {
  404. return ColorUtil.colorToString((Color)paint);
  405. } else {
  406. throw new UnsupportedOperationException("Paint not supported: " + paint);
  407. }
  408. }
  409. /** {@inheritDoc} */
  410. public void clipRect(Rectangle rect) throws IFException {
  411. try {
  412. AttributesImpl atts = new AttributesImpl();
  413. addAttribute(atts, "x", Integer.toString(rect.x));
  414. addAttribute(atts, "y", Integer.toString(rect.y));
  415. addAttribute(atts, "width", Integer.toString(rect.width));
  416. addAttribute(atts, "height", Integer.toString(rect.height));
  417. handler.element(EL_CLIP_RECT, atts);
  418. } catch (SAXException e) {
  419. throw new IFException("SAX error in clipRect()", e);
  420. }
  421. }
  422. /** {@inheritDoc} */
  423. public void fillRect(Rectangle rect, Paint fill) throws IFException {
  424. if (fill == null) {
  425. return;
  426. }
  427. try {
  428. AttributesImpl atts = new AttributesImpl();
  429. addAttribute(atts, "x", Integer.toString(rect.x));
  430. addAttribute(atts, "y", Integer.toString(rect.y));
  431. addAttribute(atts, "width", Integer.toString(rect.width));
  432. addAttribute(atts, "height", Integer.toString(rect.height));
  433. addAttribute(atts, "fill", toString(fill));
  434. handler.element(EL_RECT, atts);
  435. } catch (SAXException e) {
  436. throw new IFException("SAX error in fillRect()", e);
  437. }
  438. }
  439. /** {@inheritDoc} */
  440. public void drawBorderRect(Rectangle rect, BorderProps before, BorderProps after,
  441. BorderProps start, BorderProps end) throws IFException {
  442. if (before == null && after == null && start == null && end == null) {
  443. return;
  444. }
  445. try {
  446. AttributesImpl atts = new AttributesImpl();
  447. addAttribute(atts, "x", Integer.toString(rect.x));
  448. addAttribute(atts, "y", Integer.toString(rect.y));
  449. addAttribute(atts, "width", Integer.toString(rect.width));
  450. addAttribute(atts, "height", Integer.toString(rect.height));
  451. if (before != null) {
  452. addAttribute(atts, "before", before.toString());
  453. }
  454. if (after != null) {
  455. addAttribute(atts, "after", after.toString());
  456. }
  457. if (start != null) {
  458. addAttribute(atts, "start", start.toString());
  459. }
  460. if (end != null) {
  461. addAttribute(atts, "end", end.toString());
  462. }
  463. handler.element(EL_BORDER_RECT, atts);
  464. } catch (SAXException e) {
  465. throw new IFException("SAX error in drawBorderRect()", e);
  466. }
  467. }
  468. /** {@inheritDoc} */
  469. public void drawLine(Point start, Point end, int width, Color color, RuleStyle style)
  470. throws IFException {
  471. try {
  472. AttributesImpl atts = new AttributesImpl();
  473. addAttribute(atts, "x1", Integer.toString(start.x));
  474. addAttribute(atts, "y1", Integer.toString(start.y));
  475. addAttribute(atts, "x2", Integer.toString(end.x));
  476. addAttribute(atts, "y2", Integer.toString(end.y));
  477. addAttribute(atts, "stroke-width", Integer.toString(width));
  478. addAttribute(atts, "color", ColorUtil.colorToString(color));
  479. addAttribute(atts, "style", style.getName());
  480. handler.element(EL_LINE, atts);
  481. } catch (SAXException e) {
  482. throw new IFException("SAX error in drawLine()", e);
  483. }
  484. }
  485. /** {@inheritDoc} */
  486. public void drawText(int x, int y, int letterSpacing, int wordSpacing,
  487. int[] dx, String text) throws IFException {
  488. try {
  489. AttributesImpl atts = new AttributesImpl();
  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. /**
  580. * @return a new rendering context
  581. * @throws IllegalStateException unless overridden
  582. */
  583. protected RenderingContext createRenderingContext() throws IllegalStateException {
  584. throw new IllegalStateException("Should never be called!");
  585. }
  586. private void addAttribute(AttributesImpl atts,
  587. org.apache.xmlgraphics.util.QName attribute, String value) throws SAXException {
  588. handler.startPrefixMapping(attribute.getPrefix(), attribute.getNamespaceURI());
  589. XMLUtil.addAttribute(atts, attribute, value);
  590. }
  591. private void addAttribute(AttributesImpl atts, String localName, String value) {
  592. XMLUtil.addAttribute(atts, localName, value);
  593. }
  594. private void addStructurePointerAttribute(AttributesImpl atts) {
  595. String ptr = getContext().getStructurePointer();
  596. if (ptr != null) {
  597. addAttribute(atts, "ptr", ptr);
  598. }
  599. }
  600. // ---=== IFDocumentNavigationHandler ===---
  601. private Map incompleteActions = new java.util.HashMap();
  602. private List completeActions = new java.util.LinkedList();
  603. private void noteAction(AbstractAction action) {
  604. if (action == null) {
  605. throw new NullPointerException("action must not be null");
  606. }
  607. if (!action.isComplete()) {
  608. assert action.hasId();
  609. incompleteActions.put(action.getId(), action);
  610. }
  611. }
  612. /** {@inheritDoc} */
  613. public void renderNamedDestination(NamedDestination destination) throws IFException {
  614. noteAction(destination.getAction());
  615. AttributesImpl atts = new AttributesImpl();
  616. atts.addAttribute(null, "name", "name", XMLConstants.CDATA, destination.getName());
  617. try {
  618. handler.startElement(DocumentNavigationExtensionConstants.NAMED_DESTINATION, atts);
  619. serializeXMLizable(destination.getAction());
  620. handler.endElement(DocumentNavigationExtensionConstants.NAMED_DESTINATION);
  621. } catch (SAXException e) {
  622. throw new IFException("SAX error serializing named destination", e);
  623. }
  624. }
  625. /** {@inheritDoc} */
  626. public void renderBookmarkTree(BookmarkTree tree) throws IFException {
  627. AttributesImpl atts = new AttributesImpl();
  628. try {
  629. handler.startElement(DocumentNavigationExtensionConstants.BOOKMARK_TREE, atts);
  630. Iterator iter = tree.getBookmarks().iterator();
  631. while (iter.hasNext()) {
  632. Bookmark b = (Bookmark)iter.next();
  633. serializeBookmark(b);
  634. }
  635. handler.endElement(DocumentNavigationExtensionConstants.BOOKMARK_TREE);
  636. } catch (SAXException e) {
  637. throw new IFException("SAX error serializing bookmark tree", e);
  638. }
  639. }
  640. private void serializeBookmark(Bookmark bookmark) throws SAXException, IFException {
  641. noteAction(bookmark.getAction());
  642. AttributesImpl atts = new AttributesImpl();
  643. atts.addAttribute(null, "title", "title", XMLUtil.CDATA, bookmark.getTitle());
  644. atts.addAttribute(null, "starting-state", "starting-state",
  645. XMLUtil.CDATA, bookmark.isShown() ? "show" : "hide");
  646. handler.startElement(DocumentNavigationExtensionConstants.BOOKMARK, atts);
  647. serializeXMLizable(bookmark.getAction());
  648. Iterator iter = bookmark.getChildBookmarks().iterator();
  649. while (iter.hasNext()) {
  650. Bookmark b = (Bookmark)iter.next();
  651. serializeBookmark(b);
  652. }
  653. handler.endElement(DocumentNavigationExtensionConstants.BOOKMARK);
  654. }
  655. /** {@inheritDoc} */
  656. public void renderLink(Link link) throws IFException {
  657. noteAction(link.getAction());
  658. AttributesImpl atts = new AttributesImpl();
  659. atts.addAttribute(null, "rect", "rect",
  660. XMLConstants.CDATA, IFUtil.toString(link.getTargetRect()));
  661. if (getUserAgent().isAccessibilityEnabled()) {
  662. addAttribute(atts, "ptr", link.getAction().getStructurePointer());
  663. }
  664. try {
  665. handler.startElement(DocumentNavigationExtensionConstants.LINK, atts);
  666. serializeXMLizable(link.getAction());
  667. handler.endElement(DocumentNavigationExtensionConstants.LINK);
  668. } catch (SAXException e) {
  669. throw new IFException("SAX error serializing link", e);
  670. }
  671. }
  672. /** {@inheritDoc} */
  673. public void addResolvedAction(AbstractAction action) throws IFException {
  674. assert action.isComplete();
  675. assert action.hasId();
  676. AbstractAction noted = (AbstractAction)incompleteActions.remove(action.getId());
  677. if (noted != null) {
  678. completeActions.add(action);
  679. } else {
  680. //ignore as it was already complete when it was first used.
  681. }
  682. }
  683. private void commitNavigation() throws IFException {
  684. Iterator iter = this.completeActions.iterator();
  685. while (iter.hasNext()) {
  686. AbstractAction action = (AbstractAction)iter.next();
  687. iter.remove();
  688. serializeXMLizable(action);
  689. }
  690. assert this.completeActions.size() == 0;
  691. }
  692. private void finishDocumentNavigation() {
  693. assert this.incompleteActions.size() == 0 : "Still holding incomplete actions!";
  694. }
  695. private void serializeXMLizable(XMLizable object) throws IFException {
  696. try {
  697. object.toSAX(handler);
  698. } catch (SAXException e) {
  699. throw new IFException("SAX error serializing object", e);
  700. }
  701. }
  702. }