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

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