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

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