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.

IFParser.java 34KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808
  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.Point;
  22. import java.awt.Rectangle;
  23. import java.awt.geom.AffineTransform;
  24. import java.util.HashMap;
  25. import java.util.Locale;
  26. import java.util.Map;
  27. import java.util.Set;
  28. import javax.xml.transform.Source;
  29. import javax.xml.transform.Transformer;
  30. import javax.xml.transform.TransformerException;
  31. import javax.xml.transform.sax.SAXResult;
  32. import javax.xml.transform.sax.SAXTransformerFactory;
  33. import org.w3c.dom.DOMImplementation;
  34. import org.w3c.dom.Document;
  35. import org.xml.sax.Attributes;
  36. import org.xml.sax.ContentHandler;
  37. import org.xml.sax.SAXException;
  38. import org.xml.sax.helpers.AttributesImpl;
  39. import org.xml.sax.helpers.DefaultHandler;
  40. import org.apache.commons.logging.Log;
  41. import org.apache.commons.logging.LogFactory;
  42. import org.apache.xmlgraphics.util.QName;
  43. import org.apache.fop.accessibility.AccessibilityEventProducer;
  44. import org.apache.fop.accessibility.StructureTreeElement;
  45. import org.apache.fop.accessibility.StructureTreeEventHandler;
  46. import org.apache.fop.apps.FOUserAgent;
  47. import org.apache.fop.fo.ElementMapping;
  48. import org.apache.fop.fo.ElementMappingRegistry;
  49. import org.apache.fop.fo.expr.PropertyException;
  50. import org.apache.fop.fo.extensions.InternalElementMapping;
  51. import org.apache.fop.render.intermediate.extensions.DocumentNavigationExtensionConstants;
  52. import org.apache.fop.render.intermediate.extensions.DocumentNavigationHandler;
  53. import org.apache.fop.traits.BorderProps;
  54. import org.apache.fop.traits.RuleStyle;
  55. import org.apache.fop.util.ColorUtil;
  56. import org.apache.fop.util.ContentHandlerFactory;
  57. import org.apache.fop.util.ContentHandlerFactoryRegistry;
  58. import org.apache.fop.util.DOMBuilderContentHandlerFactory;
  59. import org.apache.fop.util.DefaultErrorListener;
  60. import org.apache.fop.util.LanguageTags;
  61. import org.apache.fop.util.XMLUtil;
  62. /**
  63. * This is a parser for the intermediate format XML which converts the intermediate file into
  64. * {@link IFPainter} events.
  65. */
  66. public class IFParser implements IFConstants {
  67. /** Logger instance */
  68. protected static final Log log = LogFactory.getLog(IFParser.class);
  69. private static SAXTransformerFactory tFactory
  70. = (SAXTransformerFactory)SAXTransformerFactory.newInstance();
  71. private static Set<String> handledNamespaces = new java.util.HashSet<String>();
  72. static {
  73. handledNamespaces.add(XMLNS_NAMESPACE_URI);
  74. handledNamespaces.add(XML_NAMESPACE);
  75. handledNamespaces.add(NAMESPACE);
  76. handledNamespaces.add(XLINK_NAMESPACE);
  77. }
  78. /**
  79. * Parses an intermediate file and paints it.
  80. * @param src the Source instance pointing to the intermediate file
  81. * @param documentHandler the intermediate format document handler used to process the IF events
  82. * @param userAgent the user agent
  83. * @throws TransformerException if an error occurs while parsing the area tree XML
  84. * @throws IFException if an IF-related error occurs inside the target document handler
  85. */
  86. public void parse(Source src, IFDocumentHandler documentHandler, FOUserAgent userAgent)
  87. throws TransformerException, IFException {
  88. try {
  89. Transformer transformer = tFactory.newTransformer();
  90. transformer.setErrorListener(new DefaultErrorListener(log));
  91. SAXResult res = new SAXResult(getContentHandler(documentHandler, userAgent));
  92. transformer.transform(src, res);
  93. } catch (TransformerException te) {
  94. //Unpack original IFException if applicable
  95. if (te.getCause() instanceof SAXException) {
  96. SAXException se = (SAXException)te.getCause();
  97. if (se.getCause() instanceof IFException) {
  98. throw (IFException)se.getCause();
  99. }
  100. } else if (te.getCause() instanceof IFException) {
  101. throw (IFException)te.getCause();
  102. }
  103. throw te;
  104. }
  105. }
  106. /**
  107. * Creates a new ContentHandler instance that you can send the area tree XML to. The parsed
  108. * pages are added to the AreaTreeModel instance you pass in as a parameter.
  109. * @param documentHandler the intermediate format document handler used to process the IF events
  110. * @param userAgent the user agent
  111. * @return the ContentHandler instance to receive the SAX stream from the area tree XML
  112. */
  113. public ContentHandler getContentHandler(IFDocumentHandler documentHandler,
  114. FOUserAgent userAgent) {
  115. ElementMappingRegistry elementMappingRegistry
  116. = userAgent.getFactory().getElementMappingRegistry();
  117. return new Handler(documentHandler, userAgent, elementMappingRegistry);
  118. }
  119. private static class Handler extends DefaultHandler {
  120. private Map<String, ElementHandler> elementHandlers = new HashMap<String, ElementHandler>();
  121. private IFDocumentHandler documentHandler;
  122. private IFPainter painter;
  123. private FOUserAgent userAgent;
  124. private ElementMappingRegistry elementMappingRegistry;
  125. private Attributes lastAttributes;
  126. private StringBuffer content = new StringBuffer();
  127. private boolean ignoreCharacters = true;
  128. //private Stack delegateStack = new Stack();
  129. private int delegateDepth;
  130. private ContentHandler delegate;
  131. private boolean inForeignObject;
  132. private Document foreignObject;
  133. private ContentHandler navParser;
  134. private ContentHandler structureTreeHandler;
  135. private Attributes pageSequenceAttributes;
  136. private Map<String, StructureTreeElement> structureTreeElements =
  137. new HashMap<String, StructureTreeElement>();
  138. private final class StructureTreeHandler extends DefaultHandler {
  139. private final StructureTreeEventHandler structureTreeEventHandler;
  140. private StructureTreeHandler(StructureTreeEventHandler structureTreeEventHandler,
  141. Locale pageSequenceLanguage) throws SAXException {
  142. this.structureTreeEventHandler = structureTreeEventHandler;
  143. structureTreeEventHandler.startPageSequence(pageSequenceLanguage);
  144. }
  145. public void endDocument() throws SAXException {
  146. startIFElement(EL_PAGE_SEQUENCE, pageSequenceAttributes);
  147. pageSequenceAttributes = null;
  148. }
  149. @Override
  150. public void startElement(String uri, String localName, String qName,
  151. Attributes attributes) throws SAXException {
  152. if (!"structure-tree".equals(localName)) {
  153. if (localName.equals("marked-content")) {
  154. localName = "#PCDATA";
  155. }
  156. String structID = attributes.getValue(InternalElementMapping.URI,
  157. InternalElementMapping.STRUCT_ID);
  158. if (structID == null) {
  159. structureTreeEventHandler.startNode(localName, attributes);
  160. } else if (localName.equals("external-graphic")
  161. || localName.equals("instream-foreign-object")) {
  162. StructureTreeElement structureTreeElement =
  163. structureTreeEventHandler.startImageNode(localName, attributes);
  164. structureTreeElements.put(structID, structureTreeElement);
  165. } else {
  166. StructureTreeElement structureTreeElement =
  167. structureTreeEventHandler.startReferencedNode(localName, attributes);
  168. structureTreeElements.put(structID, structureTreeElement);
  169. }
  170. }
  171. }
  172. @Override
  173. public void endElement(String uri, String localName, String arqNameg2)
  174. throws SAXException {
  175. if (!"structure-tree".equals(localName)) {
  176. structureTreeEventHandler.endNode(localName);
  177. }
  178. }
  179. }
  180. public Handler(IFDocumentHandler documentHandler, FOUserAgent userAgent,
  181. ElementMappingRegistry elementMappingRegistry) {
  182. this.documentHandler = documentHandler;
  183. this.userAgent = userAgent;
  184. this.elementMappingRegistry = elementMappingRegistry;
  185. elementHandlers.put(EL_DOCUMENT, new DocumentHandler());
  186. elementHandlers.put(EL_HEADER, new DocumentHeaderHandler());
  187. elementHandlers.put(EL_LOCALE, new LocaleHandler());
  188. elementHandlers.put(EL_TRAILER, new DocumentTrailerHandler());
  189. elementHandlers.put(EL_PAGE_SEQUENCE, new PageSequenceHandler());
  190. elementHandlers.put(EL_PAGE, new PageHandler());
  191. elementHandlers.put(EL_PAGE_HEADER, new PageHeaderHandler());
  192. elementHandlers.put(EL_PAGE_CONTENT, new PageContentHandler());
  193. elementHandlers.put(EL_PAGE_TRAILER, new PageTrailerHandler());
  194. //Page content
  195. elementHandlers.put(EL_VIEWPORT, new ViewportHandler());
  196. elementHandlers.put(EL_GROUP, new GroupHandler());
  197. elementHandlers.put(EL_ID, new IDHandler());
  198. elementHandlers.put(EL_FONT, new FontHandler());
  199. elementHandlers.put(EL_TEXT, new TextHandler());
  200. elementHandlers.put(EL_CLIP_RECT, new ClipRectHandler());
  201. elementHandlers.put(EL_RECT, new RectHandler());
  202. elementHandlers.put(EL_LINE, new LineHandler());
  203. elementHandlers.put(EL_BORDER_RECT, new BorderRectHandler());
  204. elementHandlers.put(EL_IMAGE, new ImageHandler());
  205. }
  206. private void establishForeignAttributes(Map<QName, String> foreignAttributes) {
  207. documentHandler.getContext().setForeignAttributes(foreignAttributes);
  208. }
  209. private void resetForeignAttributes() {
  210. documentHandler.getContext().resetForeignAttributes();
  211. }
  212. /** {@inheritDoc} */
  213. public void startElement(String uri, String localName, String qName, Attributes attributes)
  214. throws SAXException {
  215. if (delegate != null) {
  216. delegateDepth++;
  217. delegate.startElement(uri, localName, qName, attributes);
  218. } else {
  219. boolean handled = true;
  220. if (NAMESPACE.equals(uri)) {
  221. if (localName.equals(EL_PAGE_SEQUENCE) && userAgent.isAccessibilityEnabled()) {
  222. pageSequenceAttributes = new AttributesImpl(attributes);
  223. Locale language = getLanguage(attributes);
  224. structureTreeHandler = new StructureTreeHandler(
  225. userAgent.getStructureTreeEventHandler(), language);
  226. } else if (localName.equals(EL_STRUCTURE_TREE)) {
  227. if (userAgent.isAccessibilityEnabled()) {
  228. delegate = structureTreeHandler;
  229. } else {
  230. /* Delegate to a handler that does nothing */
  231. delegate = new DefaultHandler();
  232. }
  233. delegateDepth++;
  234. delegate.startDocument();
  235. delegate.startElement(uri, localName, qName, attributes);
  236. } else {
  237. if (pageSequenceAttributes != null) {
  238. /*
  239. * This means that no structure-element tag was
  240. * found in the XML, otherwise a
  241. * StructureTreeBuilderWrapper object would have
  242. * been created, which would have reset the
  243. * pageSequenceAttributes field.
  244. */
  245. AccessibilityEventProducer.Provider
  246. .get(userAgent.getEventBroadcaster())
  247. .noStructureTreeInXML(this);
  248. }
  249. handled = startIFElement(localName, attributes);
  250. }
  251. } else if (DocumentNavigationExtensionConstants.NAMESPACE.equals(uri)) {
  252. if (this.navParser == null) {
  253. this.navParser = new DocumentNavigationHandler(
  254. this.documentHandler.getDocumentNavigationHandler(), structureTreeElements);
  255. }
  256. delegate = this.navParser;
  257. delegateDepth++;
  258. delegate.startDocument();
  259. delegate.startElement(uri, localName, qName, attributes);
  260. } else {
  261. ContentHandlerFactoryRegistry registry
  262. = userAgent.getFactory().getContentHandlerFactoryRegistry();
  263. ContentHandlerFactory factory = registry.getFactory(uri);
  264. if (factory == null) {
  265. DOMImplementation domImplementation
  266. = elementMappingRegistry.getDOMImplementationForNamespace(uri);
  267. if (domImplementation == null) {
  268. domImplementation = ElementMapping.getDefaultDOMImplementation();
  269. /*
  270. throw new SAXException("No DOMImplementation could be"
  271. + " identified to handle namespace: " + uri);
  272. */
  273. }
  274. factory = new DOMBuilderContentHandlerFactory(uri, domImplementation);
  275. }
  276. delegate = factory.createContentHandler();
  277. delegateDepth++;
  278. delegate.startDocument();
  279. delegate.startElement(uri, localName, qName, attributes);
  280. }
  281. if (!handled) {
  282. if (uri == null || uri.length() == 0) {
  283. throw new SAXException("Unhandled element " + localName
  284. + " in namespace: " + uri);
  285. } else {
  286. log.warn("Unhandled element " + localName
  287. + " in namespace: " + uri);
  288. }
  289. }
  290. }
  291. }
  292. private static Locale getLanguage(Attributes attributes) {
  293. String xmllang = attributes.getValue(XML_NAMESPACE, "lang");
  294. return (xmllang == null) ? null : LanguageTags.toLocale(xmllang);
  295. }
  296. private boolean startIFElement(String localName, Attributes attributes)
  297. throws SAXException {
  298. lastAttributes = new AttributesImpl(attributes);
  299. ElementHandler elementHandler = elementHandlers.get(localName);
  300. content.setLength(0);
  301. ignoreCharacters = true;
  302. if (elementHandler != null) {
  303. ignoreCharacters = elementHandler.ignoreCharacters();
  304. try {
  305. elementHandler.startElement(attributes);
  306. } catch (IFException ife) {
  307. handleIFException(ife);
  308. }
  309. return true;
  310. } else {
  311. return false;
  312. }
  313. }
  314. private void handleIFException(IFException ife) throws SAXException {
  315. if (ife.getCause() instanceof SAXException) {
  316. //unwrap
  317. throw (SAXException)ife.getCause();
  318. } else {
  319. //wrap
  320. throw new SAXException(ife);
  321. }
  322. }
  323. /** {@inheritDoc} */
  324. public void endElement(String uri, String localName, String qName) throws SAXException {
  325. if (delegate != null) {
  326. delegate.endElement(uri, localName, qName);
  327. delegateDepth--;
  328. if (delegateDepth == 0) {
  329. delegate.endDocument();
  330. if (delegate instanceof ContentHandlerFactory.ObjectSource) {
  331. Object obj = ((ContentHandlerFactory.ObjectSource)delegate).getObject();
  332. if (inForeignObject) {
  333. this.foreignObject = (Document)obj;
  334. } else {
  335. handleExternallyGeneratedObject(obj);
  336. }
  337. }
  338. delegate = null; //Sub-document is processed, return to normal processing
  339. }
  340. } else {
  341. if (NAMESPACE.equals(uri)) {
  342. ElementHandler elementHandler = elementHandlers.get(localName);
  343. if (elementHandler != null) {
  344. try {
  345. elementHandler.endElement();
  346. } catch (IFException ife) {
  347. handleIFException(ife);
  348. }
  349. content.setLength(0);
  350. }
  351. ignoreCharacters = true;
  352. } else {
  353. if (log.isTraceEnabled()) {
  354. log.trace("Ignoring " + localName + " in namespace: " + uri);
  355. }
  356. }
  357. }
  358. }
  359. // ============== Element handlers for the intermediate format =============
  360. private static interface ElementHandler {
  361. void startElement(Attributes attributes) throws IFException, SAXException;
  362. void endElement() throws IFException;
  363. boolean ignoreCharacters();
  364. }
  365. private abstract class AbstractElementHandler implements ElementHandler {
  366. public void startElement(Attributes attributes) throws IFException, SAXException {
  367. //nop
  368. }
  369. public void endElement() throws IFException {
  370. //nop
  371. }
  372. public boolean ignoreCharacters() {
  373. return true;
  374. }
  375. }
  376. private class DocumentHandler extends AbstractElementHandler {
  377. public void startElement(Attributes attributes) throws IFException {
  378. documentHandler.startDocument();
  379. }
  380. public void endElement() throws IFException {
  381. documentHandler.endDocument();
  382. }
  383. }
  384. private class DocumentHeaderHandler extends AbstractElementHandler {
  385. public void startElement(Attributes attributes) throws IFException {
  386. documentHandler.startDocumentHeader();
  387. }
  388. public void endElement() throws IFException {
  389. documentHandler.endDocumentHeader();
  390. }
  391. }
  392. private class LocaleHandler extends AbstractElementHandler {
  393. public void startElement(Attributes attributes) throws IFException {
  394. documentHandler.setDocumentLocale(getLanguage(attributes));
  395. }
  396. }
  397. private class DocumentTrailerHandler extends AbstractElementHandler {
  398. public void startElement(Attributes attributes) throws IFException {
  399. documentHandler.startDocumentTrailer();
  400. }
  401. public void endElement() throws IFException {
  402. documentHandler.endDocumentTrailer();
  403. }
  404. }
  405. private class PageSequenceHandler extends AbstractElementHandler {
  406. public void startElement(Attributes attributes) throws IFException {
  407. String id = attributes.getValue("id");
  408. Locale language = getLanguage(attributes);
  409. if (language != null) {
  410. documentHandler.getContext().setLanguage(language);
  411. }
  412. Map<QName, String> foreignAttributes = getForeignAttributes(lastAttributes);
  413. establishForeignAttributes(foreignAttributes);
  414. documentHandler.startPageSequence(id);
  415. resetForeignAttributes();
  416. }
  417. public void endElement() throws IFException {
  418. documentHandler.endPageSequence();
  419. documentHandler.getContext().setLanguage(null);
  420. }
  421. }
  422. private class PageHandler extends AbstractElementHandler {
  423. public void startElement(Attributes attributes) throws IFException {
  424. int index = Integer.parseInt(attributes.getValue("index"));
  425. String name = attributes.getValue("name");
  426. String pageMasterName = attributes.getValue("page-master-name");
  427. int width = Integer.parseInt(attributes.getValue("width"));
  428. int height = Integer.parseInt(attributes.getValue("height"));
  429. Map<QName, String> foreignAttributes = getForeignAttributes(lastAttributes);
  430. establishForeignAttributes(foreignAttributes);
  431. documentHandler.startPage(index, name, pageMasterName,
  432. new Dimension(width, height));
  433. resetForeignAttributes();
  434. }
  435. public void endElement() throws IFException {
  436. documentHandler.endPage();
  437. }
  438. }
  439. private class PageHeaderHandler extends AbstractElementHandler {
  440. public void startElement(Attributes attributes) throws IFException {
  441. documentHandler.startPageHeader();
  442. }
  443. public void endElement() throws IFException {
  444. documentHandler.endPageHeader();
  445. }
  446. }
  447. private class PageContentHandler extends AbstractElementHandler {
  448. public void startElement(Attributes attributes) throws IFException {
  449. painter = documentHandler.startPageContent();
  450. }
  451. public void endElement() throws IFException {
  452. painter = null;
  453. documentHandler.getContext().setID("");
  454. documentHandler.endPageContent();
  455. }
  456. }
  457. private class PageTrailerHandler extends AbstractElementHandler {
  458. public void startElement(Attributes attributes) throws IFException {
  459. documentHandler.startPageTrailer();
  460. }
  461. public void endElement() throws IFException {
  462. documentHandler.endPageTrailer();
  463. }
  464. }
  465. private class ViewportHandler extends AbstractElementHandler {
  466. public void startElement(Attributes attributes) throws IFException {
  467. String transform = attributes.getValue("transform");
  468. AffineTransform[] transforms
  469. = AffineTransformArrayParser.createAffineTransform(transform);
  470. int width = Integer.parseInt(attributes.getValue("width"));
  471. int height = Integer.parseInt(attributes.getValue("height"));
  472. Rectangle clipRect = XMLUtil.getAttributeAsRectangle(attributes, "clip-rect");
  473. painter.startViewport(transforms, new Dimension(width, height), clipRect);
  474. }
  475. public void endElement() throws IFException {
  476. painter.endViewport();
  477. }
  478. }
  479. private class GroupHandler extends AbstractElementHandler {
  480. public void startElement(Attributes attributes) throws IFException {
  481. String transform = attributes.getValue("transform");
  482. AffineTransform[] transforms
  483. = AffineTransformArrayParser.createAffineTransform(transform);
  484. painter.startGroup(transforms);
  485. }
  486. public void endElement() throws IFException {
  487. painter.endGroup();
  488. }
  489. }
  490. private class IDHandler extends AbstractElementHandler {
  491. @Override
  492. public void startElement(Attributes attributes) throws IFException, SAXException {
  493. String id = attributes.getValue("name");
  494. documentHandler.getContext().setID(id);
  495. }
  496. }
  497. private class FontHandler extends AbstractElementHandler {
  498. public void startElement(Attributes attributes) throws IFException {
  499. String family = attributes.getValue("family");
  500. String style = attributes.getValue("style");
  501. Integer weight = XMLUtil.getAttributeAsInteger(attributes, "weight");
  502. String variant = attributes.getValue("variant");
  503. Integer size = XMLUtil.getAttributeAsInteger(attributes, "size");
  504. Color color;
  505. try {
  506. color = getAttributeAsColor(attributes, "color");
  507. } catch (PropertyException pe) {
  508. throw new IFException("Error parsing the color attribute", pe);
  509. }
  510. painter.setFont(family, style, weight, variant, size, color);
  511. }
  512. }
  513. private class TextHandler extends AbstractElementHandler {
  514. public void endElement() throws IFException {
  515. int x = Integer.parseInt(lastAttributes.getValue("x"));
  516. int y = Integer.parseInt(lastAttributes.getValue("y"));
  517. String s = lastAttributes.getValue("letter-spacing");
  518. int letterSpacing = (s != null ? Integer.parseInt(s) : 0);
  519. s = lastAttributes.getValue("word-spacing");
  520. int wordSpacing = (s != null ? Integer.parseInt(s) : 0);
  521. int[] dx = XMLUtil.getAttributeAsIntArray(lastAttributes, "dx");
  522. establishStructureTreeElement(lastAttributes);
  523. painter.drawText(x, y, letterSpacing, wordSpacing, dx, content.toString());
  524. resetStructureTreeElement();
  525. }
  526. public boolean ignoreCharacters() {
  527. return false;
  528. }
  529. }
  530. private class ClipRectHandler extends AbstractElementHandler {
  531. public void startElement(Attributes attributes) throws IFException {
  532. int x = Integer.parseInt(attributes.getValue("x"));
  533. int y = Integer.parseInt(attributes.getValue("y"));
  534. int width = Integer.parseInt(attributes.getValue("width"));
  535. int height = Integer.parseInt(attributes.getValue("height"));
  536. painter.clipRect(new Rectangle(x, y, width, height));
  537. }
  538. }
  539. private class RectHandler extends AbstractElementHandler {
  540. public void startElement(Attributes attributes) throws IFException {
  541. int x = Integer.parseInt(attributes.getValue("x"));
  542. int y = Integer.parseInt(attributes.getValue("y"));
  543. int width = Integer.parseInt(attributes.getValue("width"));
  544. int height = Integer.parseInt(attributes.getValue("height"));
  545. Color fillColor;
  546. try {
  547. fillColor = getAttributeAsColor(attributes, "fill");
  548. } catch (PropertyException pe) {
  549. throw new IFException("Error parsing the fill attribute", pe);
  550. }
  551. painter.fillRect(new Rectangle(x, y, width, height), fillColor);
  552. }
  553. }
  554. private class LineHandler extends AbstractElementHandler {
  555. public void startElement(Attributes attributes) throws IFException {
  556. int x1 = Integer.parseInt(attributes.getValue("x1"));
  557. int y1 = Integer.parseInt(attributes.getValue("y1"));
  558. int x2 = Integer.parseInt(attributes.getValue("x2"));
  559. int y2 = Integer.parseInt(attributes.getValue("y2"));
  560. int width = Integer.parseInt(attributes.getValue("stroke-width"));
  561. Color color;
  562. try {
  563. color = getAttributeAsColor(attributes, "color");
  564. } catch (PropertyException pe) {
  565. throw new IFException("Error parsing the fill attribute", pe);
  566. }
  567. RuleStyle style = RuleStyle.valueOf(attributes.getValue("style"));
  568. painter.drawLine(new Point(x1, y1), new Point(x2, y2), width, color, style);
  569. }
  570. }
  571. private static final String[] SIDES = new String[] {"before", "after", "start", "end"};
  572. private class BorderRectHandler extends AbstractElementHandler {
  573. public void startElement(Attributes attributes) throws IFException {
  574. int x = Integer.parseInt(attributes.getValue("x"));
  575. int y = Integer.parseInt(attributes.getValue("y"));
  576. int width = Integer.parseInt(attributes.getValue("width"));
  577. int height = Integer.parseInt(attributes.getValue("height"));
  578. BorderProps[] borders = new BorderProps[4];
  579. for (int i = 0; i < 4; i++) {
  580. String b = attributes.getValue(SIDES[i]);
  581. if (b != null) {
  582. borders[i] = BorderProps.valueOf(userAgent, b);
  583. }
  584. }
  585. painter.drawBorderRect(new Rectangle(x, y, width, height),
  586. borders[0], borders[1], borders[2], borders[3]);
  587. }
  588. }
  589. private class ImageHandler extends AbstractElementHandler {
  590. public void startElement(Attributes attributes) throws IFException {
  591. inForeignObject = true;
  592. }
  593. public void endElement() throws IFException {
  594. int x = Integer.parseInt(lastAttributes.getValue("x"));
  595. int y = Integer.parseInt(lastAttributes.getValue("y"));
  596. int width = Integer.parseInt(lastAttributes.getValue("width"));
  597. int height = Integer.parseInt(lastAttributes.getValue("height"));
  598. Map<QName, String> foreignAttributes = getForeignAttributes(lastAttributes);
  599. establishForeignAttributes(foreignAttributes);
  600. establishStructureTreeElement(lastAttributes);
  601. if (foreignObject != null) {
  602. painter.drawImage(foreignObject,
  603. new Rectangle(x, y, width, height));
  604. foreignObject = null;
  605. } else {
  606. String uri = lastAttributes.getValue(
  607. XLINK_HREF.getNamespaceURI(), XLINK_HREF.getLocalName());
  608. if (uri == null) {
  609. throw new IFException("xlink:href is missing on image", null);
  610. }
  611. painter.drawImage(uri, new Rectangle(x, y, width, height));
  612. }
  613. resetForeignAttributes();
  614. resetStructureTreeElement();
  615. inForeignObject = false;
  616. }
  617. public boolean ignoreCharacters() {
  618. return false;
  619. }
  620. }
  621. // ====================================================================
  622. /**
  623. * Handles objects created by "sub-parsers" that implement the ObjectSource interface.
  624. * An example of object handled here are ExtensionAttachments.
  625. * @param obj the Object to be handled.
  626. * @throws SAXException if an error occurs while handling the extension object
  627. */
  628. protected void handleExternallyGeneratedObject(Object obj) throws SAXException {
  629. try {
  630. documentHandler.handleExtensionObject(obj);
  631. } catch (IFException ife) {
  632. handleIFException(ife);
  633. }
  634. }
  635. private Color getAttributeAsColor(Attributes attributes, String name)
  636. throws PropertyException {
  637. String s = attributes.getValue(name);
  638. if (s == null) {
  639. return null;
  640. } else {
  641. return ColorUtil.parseColorString(userAgent, s);
  642. }
  643. }
  644. private static Map<QName, String> getForeignAttributes(Attributes atts) {
  645. Map<QName, String> foreignAttributes = null;
  646. for (int i = 0, c = atts.getLength(); i < c; i++) {
  647. String ns = atts.getURI(i);
  648. if (ns.length() > 0) {
  649. if (handledNamespaces.contains(ns)) {
  650. continue;
  651. }
  652. if (foreignAttributes == null) {
  653. foreignAttributes = new java.util.HashMap<QName, String>();
  654. }
  655. QName qname = new QName(ns, atts.getQName(i));
  656. foreignAttributes.put(qname, atts.getValue(i));
  657. }
  658. }
  659. return foreignAttributes;
  660. }
  661. private void establishStructureTreeElement(Attributes attributes) {
  662. String structRef = attributes.getValue(InternalElementMapping.URI,
  663. InternalElementMapping.STRUCT_REF);
  664. if (structRef != null && structRef.length() > 0) {
  665. assert structureTreeElements.containsKey(structRef);
  666. StructureTreeElement structureTreeElement = structureTreeElements.get(structRef);
  667. documentHandler.getContext().setStructureTreeElement(structureTreeElement);
  668. }
  669. }
  670. private void resetStructureTreeElement() {
  671. documentHandler.getContext().resetStructureTreeElement();
  672. }
  673. /** {@inheritDoc} */
  674. public void characters(char[] ch, int start, int length) throws SAXException {
  675. if (delegate != null) {
  676. delegate.characters(ch, start, length);
  677. } else if (!ignoreCharacters) {
  678. this.content.append(ch, start, length);
  679. }
  680. }
  681. }
  682. }