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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823
  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 StructureTreeHandler 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 Locale pageSequenceLanguage;
  140. private final StructureTreeEventHandler structureTreeEventHandler;
  141. private StructureTreeHandler(StructureTreeEventHandler structureTreeEventHandler,
  142. Locale pageSequenceLanguage) throws SAXException {
  143. this.pageSequenceLanguage = pageSequenceLanguage;
  144. this.structureTreeEventHandler = structureTreeEventHandler;
  145. }
  146. void startStructureTree(String type) {
  147. structureTreeEventHandler.startPageSequence(pageSequenceLanguage, type);
  148. }
  149. public void endDocument() throws SAXException {
  150. startIFElement(EL_PAGE_SEQUENCE, pageSequenceAttributes);
  151. pageSequenceAttributes = null;
  152. }
  153. @Override
  154. public void startElement(String uri, String localName, String qName,
  155. Attributes attributes) throws SAXException {
  156. if (!"structure-tree".equals(localName)) {
  157. if (localName.equals("marked-content")) {
  158. localName = "#PCDATA";
  159. }
  160. String structID = attributes.getValue(InternalElementMapping.URI,
  161. InternalElementMapping.STRUCT_ID);
  162. if (structID == null) {
  163. structureTreeEventHandler.startNode(localName, attributes);
  164. } else if (localName.equals("external-graphic")
  165. || localName.equals("instream-foreign-object")) {
  166. StructureTreeElement structureTreeElement
  167. = structureTreeEventHandler.startImageNode(localName, attributes);
  168. structureTreeElements.put(structID, structureTreeElement);
  169. } else {
  170. StructureTreeElement structureTreeElement = structureTreeEventHandler
  171. .startReferencedNode(localName, attributes);
  172. structureTreeElements.put(structID, structureTreeElement);
  173. }
  174. }
  175. }
  176. @Override
  177. public void endElement(String uri, String localName, String arqNameg2)
  178. throws SAXException {
  179. if (!"structure-tree".equals(localName)) {
  180. structureTreeEventHandler.endNode(localName);
  181. }
  182. }
  183. }
  184. public Handler(IFDocumentHandler documentHandler, FOUserAgent userAgent,
  185. ElementMappingRegistry elementMappingRegistry) {
  186. this.documentHandler = documentHandler;
  187. this.userAgent = userAgent;
  188. this.elementMappingRegistry = elementMappingRegistry;
  189. elementHandlers.put(EL_DOCUMENT, new DocumentHandler());
  190. elementHandlers.put(EL_HEADER, new DocumentHeaderHandler());
  191. elementHandlers.put(EL_LOCALE, new LocaleHandler());
  192. elementHandlers.put(EL_TRAILER, new DocumentTrailerHandler());
  193. elementHandlers.put(EL_PAGE_SEQUENCE, new PageSequenceHandler());
  194. elementHandlers.put(EL_PAGE, new PageHandler());
  195. elementHandlers.put(EL_PAGE_HEADER, new PageHeaderHandler());
  196. elementHandlers.put(EL_PAGE_CONTENT, new PageContentHandler());
  197. elementHandlers.put(EL_PAGE_TRAILER, new PageTrailerHandler());
  198. //Page content
  199. elementHandlers.put(EL_VIEWPORT, new ViewportHandler());
  200. elementHandlers.put(EL_GROUP, new GroupHandler());
  201. elementHandlers.put(EL_ID, new IDHandler());
  202. elementHandlers.put(EL_FONT, new FontHandler());
  203. elementHandlers.put(EL_TEXT, new TextHandler());
  204. elementHandlers.put(EL_CLIP_RECT, new ClipRectHandler());
  205. elementHandlers.put(EL_RECT, new RectHandler());
  206. elementHandlers.put(EL_LINE, new LineHandler());
  207. elementHandlers.put(EL_BORDER_RECT, new BorderRectHandler());
  208. elementHandlers.put(EL_IMAGE, new ImageHandler());
  209. }
  210. private void establishForeignAttributes(Map<QName, String> foreignAttributes) {
  211. documentHandler.getContext().setForeignAttributes(foreignAttributes);
  212. }
  213. private void resetForeignAttributes() {
  214. documentHandler.getContext().resetForeignAttributes();
  215. }
  216. /** {@inheritDoc} */
  217. public void startElement(String uri, String localName, String qName, Attributes attributes)
  218. throws SAXException {
  219. if (delegate != null) {
  220. delegateDepth++;
  221. delegate.startElement(uri, localName, qName, attributes);
  222. } else {
  223. boolean handled = true;
  224. if (NAMESPACE.equals(uri)) {
  225. if (localName.equals(EL_PAGE_SEQUENCE) && userAgent.isAccessibilityEnabled()) {
  226. pageSequenceAttributes = new AttributesImpl(attributes);
  227. Locale language = getLanguage(attributes);
  228. structureTreeHandler = new StructureTreeHandler(
  229. userAgent.getStructureTreeEventHandler(), language);
  230. } else if (localName.equals(EL_STRUCTURE_TREE)) {
  231. if (userAgent.isAccessibilityEnabled()) {
  232. String type = attributes.getValue("type");
  233. structureTreeHandler.startStructureTree(type);
  234. delegate = structureTreeHandler;
  235. } else {
  236. /* Delegate to a handler that does nothing */
  237. delegate = new DefaultHandler();
  238. }
  239. delegateDepth++;
  240. delegate.startDocument();
  241. delegate.startElement(uri, localName, qName, attributes);
  242. } else {
  243. if (pageSequenceAttributes != null) {
  244. /*
  245. * This means that no structure-element tag was
  246. * found in the XML, otherwise a
  247. * StructureTreeBuilderWrapper object would have
  248. * been created, which would have reset the
  249. * pageSequenceAttributes field.
  250. */
  251. AccessibilityEventProducer.Provider
  252. .get(userAgent.getEventBroadcaster())
  253. .noStructureTreeInXML(this);
  254. }
  255. handled = startIFElement(localName, attributes);
  256. }
  257. } else if (DocumentNavigationExtensionConstants.NAMESPACE.equals(uri)) {
  258. if (this.navParser == null) {
  259. this.navParser = new DocumentNavigationHandler(
  260. this.documentHandler.getDocumentNavigationHandler(),
  261. structureTreeElements);
  262. }
  263. delegate = this.navParser;
  264. delegateDepth++;
  265. delegate.startDocument();
  266. delegate.startElement(uri, localName, qName, attributes);
  267. } else {
  268. ContentHandlerFactoryRegistry registry
  269. = userAgent.getFactory().getContentHandlerFactoryRegistry();
  270. ContentHandlerFactory factory = registry.getFactory(uri);
  271. if (factory == null) {
  272. DOMImplementation domImplementation
  273. = elementMappingRegistry.getDOMImplementationForNamespace(uri);
  274. if (domImplementation == null) {
  275. domImplementation = ElementMapping.getDefaultDOMImplementation();
  276. /*
  277. throw new SAXException("No DOMImplementation could be"
  278. + " identified to handle namespace: " + uri);
  279. */
  280. }
  281. factory = new DOMBuilderContentHandlerFactory(uri, domImplementation);
  282. }
  283. delegate = factory.createContentHandler();
  284. delegateDepth++;
  285. delegate.startDocument();
  286. delegate.startElement(uri, localName, qName, attributes);
  287. }
  288. if (!handled) {
  289. if (uri == null || uri.length() == 0) {
  290. throw new SAXException("Unhandled element " + localName
  291. + " in namespace: " + uri);
  292. } else {
  293. log.warn("Unhandled element " + localName
  294. + " in namespace: " + uri);
  295. }
  296. }
  297. }
  298. }
  299. private static Locale getLanguage(Attributes attributes) {
  300. String xmllang = attributes.getValue(XML_NAMESPACE, "lang");
  301. return (xmllang == null) ? null : LanguageTags.toLocale(xmllang);
  302. }
  303. private boolean startIFElement(String localName, Attributes attributes)
  304. throws SAXException {
  305. lastAttributes = new AttributesImpl(attributes);
  306. ElementHandler elementHandler = elementHandlers.get(localName);
  307. content.setLength(0);
  308. ignoreCharacters = true;
  309. if (elementHandler != null) {
  310. ignoreCharacters = elementHandler.ignoreCharacters();
  311. try {
  312. elementHandler.startElement(attributes);
  313. } catch (IFException ife) {
  314. handleIFException(ife);
  315. }
  316. return true;
  317. } else {
  318. return false;
  319. }
  320. }
  321. private void handleIFException(IFException ife) throws SAXException {
  322. if (ife.getCause() instanceof SAXException) {
  323. //unwrap
  324. throw (SAXException)ife.getCause();
  325. } else {
  326. //wrap
  327. throw new SAXException(ife);
  328. }
  329. }
  330. /** {@inheritDoc} */
  331. public void endElement(String uri, String localName, String qName) throws SAXException {
  332. if (delegate != null) {
  333. delegate.endElement(uri, localName, qName);
  334. delegateDepth--;
  335. if (delegateDepth == 0) {
  336. delegate.endDocument();
  337. if (delegate instanceof ContentHandlerFactory.ObjectSource) {
  338. Object obj = ((ContentHandlerFactory.ObjectSource)delegate).getObject();
  339. if (inForeignObject) {
  340. this.foreignObject = (Document)obj;
  341. } else {
  342. handleExternallyGeneratedObject(obj);
  343. }
  344. }
  345. delegate = null; //Sub-document is processed, return to normal processing
  346. }
  347. } else {
  348. if (NAMESPACE.equals(uri)) {
  349. ElementHandler elementHandler = elementHandlers.get(localName);
  350. if (elementHandler != null) {
  351. try {
  352. elementHandler.endElement();
  353. } catch (IFException ife) {
  354. handleIFException(ife);
  355. }
  356. content.setLength(0);
  357. }
  358. ignoreCharacters = true;
  359. } else {
  360. if (log.isTraceEnabled()) {
  361. log.trace("Ignoring " + localName + " in namespace: " + uri);
  362. }
  363. }
  364. }
  365. }
  366. // ============== Element handlers for the intermediate format =============
  367. private interface ElementHandler {
  368. void startElement(Attributes attributes) throws IFException, SAXException;
  369. void endElement() throws IFException;
  370. boolean ignoreCharacters();
  371. }
  372. private abstract class AbstractElementHandler implements ElementHandler {
  373. public void startElement(Attributes attributes) throws IFException, SAXException {
  374. //nop
  375. }
  376. public void endElement() throws IFException {
  377. //nop
  378. }
  379. public boolean ignoreCharacters() {
  380. return true;
  381. }
  382. }
  383. private class DocumentHandler extends AbstractElementHandler {
  384. public void startElement(Attributes attributes) throws IFException {
  385. documentHandler.startDocument();
  386. }
  387. public void endElement() throws IFException {
  388. documentHandler.endDocument();
  389. }
  390. }
  391. private class DocumentHeaderHandler extends AbstractElementHandler {
  392. public void startElement(Attributes attributes) throws IFException {
  393. documentHandler.startDocumentHeader();
  394. }
  395. public void endElement() throws IFException {
  396. documentHandler.endDocumentHeader();
  397. }
  398. }
  399. private class LocaleHandler extends AbstractElementHandler {
  400. public void startElement(Attributes attributes) throws IFException {
  401. documentHandler.setDocumentLocale(getLanguage(attributes));
  402. }
  403. }
  404. private class DocumentTrailerHandler extends AbstractElementHandler {
  405. public void startElement(Attributes attributes) throws IFException {
  406. documentHandler.startDocumentTrailer();
  407. }
  408. public void endElement() throws IFException {
  409. documentHandler.endDocumentTrailer();
  410. }
  411. }
  412. private class PageSequenceHandler extends AbstractElementHandler {
  413. public void startElement(Attributes attributes) throws IFException {
  414. String id = attributes.getValue("id");
  415. Locale language = getLanguage(attributes);
  416. if (language != null) {
  417. documentHandler.getContext().setLanguage(language);
  418. }
  419. Map<QName, String> foreignAttributes = getForeignAttributes(lastAttributes);
  420. establishForeignAttributes(foreignAttributes);
  421. documentHandler.startPageSequence(id);
  422. resetForeignAttributes();
  423. }
  424. public void endElement() throws IFException {
  425. documentHandler.endPageSequence();
  426. documentHandler.getContext().setLanguage(null);
  427. }
  428. }
  429. private class PageHandler extends AbstractElementHandler {
  430. public void startElement(Attributes attributes) throws IFException {
  431. int index = Integer.parseInt(attributes.getValue("index"));
  432. String name = attributes.getValue("name");
  433. String pageMasterName = attributes.getValue("page-master-name");
  434. int width = Integer.parseInt(attributes.getValue("width"));
  435. int height = Integer.parseInt(attributes.getValue("height"));
  436. Map<QName, String> foreignAttributes = getForeignAttributes(lastAttributes);
  437. establishForeignAttributes(foreignAttributes);
  438. documentHandler.startPage(index, name, pageMasterName,
  439. new Dimension(width, height));
  440. resetForeignAttributes();
  441. }
  442. public void endElement() throws IFException {
  443. documentHandler.endPage();
  444. }
  445. }
  446. private class PageHeaderHandler extends AbstractElementHandler {
  447. public void startElement(Attributes attributes) throws IFException {
  448. documentHandler.startPageHeader();
  449. }
  450. public void endElement() throws IFException {
  451. documentHandler.endPageHeader();
  452. }
  453. }
  454. private class PageContentHandler extends AbstractElementHandler {
  455. public void startElement(Attributes attributes) throws IFException {
  456. painter = documentHandler.startPageContent();
  457. }
  458. public void endElement() throws IFException {
  459. painter = null;
  460. documentHandler.getContext().setID("");
  461. documentHandler.endPageContent();
  462. }
  463. }
  464. private class PageTrailerHandler extends AbstractElementHandler {
  465. public void startElement(Attributes attributes) throws IFException {
  466. documentHandler.startPageTrailer();
  467. }
  468. public void endElement() throws IFException {
  469. documentHandler.endPageTrailer();
  470. }
  471. }
  472. private class ViewportHandler extends AbstractElementHandler {
  473. public void startElement(Attributes attributes) throws IFException {
  474. String transform = attributes.getValue("transform");
  475. AffineTransform[] transforms
  476. = AffineTransformArrayParser.createAffineTransform(transform);
  477. int width = Integer.parseInt(attributes.getValue("width"));
  478. int height = Integer.parseInt(attributes.getValue("height"));
  479. Rectangle clipRect = XMLUtil.getAttributeAsRectangle(attributes, "clip-rect");
  480. painter.startViewport(transforms, new Dimension(width, height), clipRect);
  481. }
  482. public void endElement() throws IFException {
  483. painter.endViewport();
  484. }
  485. }
  486. private class GroupHandler extends AbstractElementHandler {
  487. public void startElement(Attributes attributes) throws IFException {
  488. String transform = attributes.getValue("transform");
  489. AffineTransform[] transforms
  490. = AffineTransformArrayParser.createAffineTransform(transform);
  491. painter.startGroup(transforms);
  492. }
  493. public void endElement() throws IFException {
  494. painter.endGroup();
  495. }
  496. }
  497. private class IDHandler extends AbstractElementHandler {
  498. @Override
  499. public void startElement(Attributes attributes) throws IFException, SAXException {
  500. String id = attributes.getValue("name");
  501. documentHandler.getContext().setID(id);
  502. }
  503. }
  504. private class FontHandler extends AbstractElementHandler {
  505. public void startElement(Attributes attributes) throws IFException {
  506. String family = attributes.getValue("family");
  507. String style = attributes.getValue("style");
  508. Integer weight = XMLUtil.getAttributeAsInteger(attributes, "weight");
  509. String variant = attributes.getValue("variant");
  510. Integer size = XMLUtil.getAttributeAsInteger(attributes, "size");
  511. Color color;
  512. try {
  513. color = getAttributeAsColor(attributes, "color");
  514. } catch (PropertyException pe) {
  515. throw new IFException("Error parsing the color attribute", pe);
  516. }
  517. painter.setFont(family, style, weight, variant, size, color);
  518. }
  519. }
  520. private class TextHandler extends AbstractElementHandler {
  521. public void endElement() throws IFException {
  522. int x = Integer.parseInt(lastAttributes.getValue("x"));
  523. int y = Integer.parseInt(lastAttributes.getValue("y"));
  524. String s = lastAttributes.getValue("letter-spacing");
  525. int letterSpacing = (s != null ? Integer.parseInt(s) : 0);
  526. s = lastAttributes.getValue("word-spacing");
  527. int wordSpacing = (s != null ? Integer.parseInt(s) : 0);
  528. int[] dx = XMLUtil.getAttributeAsIntArray(lastAttributes, "dx");
  529. int[][] dp = XMLUtil.getAttributeAsPositionAdjustments(lastAttributes, "dp");
  530. // if only DX present, then convert DX to DP; otherwise use only DP,
  531. // effectively ignoring DX
  532. if ( ( dp == null ) && ( dx != null ) ) {
  533. dp = IFUtil.convertDXToDP ( dx );
  534. }
  535. establishStructureTreeElement(lastAttributes);
  536. painter.drawText(x, y, letterSpacing, wordSpacing, dp, content.toString());
  537. resetStructureTreeElement();
  538. }
  539. public boolean ignoreCharacters() {
  540. return false;
  541. }
  542. }
  543. private class ClipRectHandler extends AbstractElementHandler {
  544. public void startElement(Attributes attributes) throws IFException {
  545. int x = Integer.parseInt(attributes.getValue("x"));
  546. int y = Integer.parseInt(attributes.getValue("y"));
  547. int width = Integer.parseInt(attributes.getValue("width"));
  548. int height = Integer.parseInt(attributes.getValue("height"));
  549. painter.clipRect(new Rectangle(x, y, width, height));
  550. }
  551. }
  552. private class RectHandler extends AbstractElementHandler {
  553. public void startElement(Attributes attributes) throws IFException {
  554. int x = Integer.parseInt(attributes.getValue("x"));
  555. int y = Integer.parseInt(attributes.getValue("y"));
  556. int width = Integer.parseInt(attributes.getValue("width"));
  557. int height = Integer.parseInt(attributes.getValue("height"));
  558. Color fillColor;
  559. try {
  560. fillColor = getAttributeAsColor(attributes, "fill");
  561. } catch (PropertyException pe) {
  562. throw new IFException("Error parsing the fill attribute", pe);
  563. }
  564. painter.fillRect(new Rectangle(x, y, width, height), fillColor);
  565. }
  566. }
  567. private class LineHandler extends AbstractElementHandler {
  568. public void startElement(Attributes attributes) throws IFException {
  569. int x1 = Integer.parseInt(attributes.getValue("x1"));
  570. int y1 = Integer.parseInt(attributes.getValue("y1"));
  571. int x2 = Integer.parseInt(attributes.getValue("x2"));
  572. int y2 = Integer.parseInt(attributes.getValue("y2"));
  573. int width = Integer.parseInt(attributes.getValue("stroke-width"));
  574. Color color;
  575. try {
  576. color = getAttributeAsColor(attributes, "color");
  577. } catch (PropertyException pe) {
  578. throw new IFException("Error parsing the fill attribute", pe);
  579. }
  580. RuleStyle style = RuleStyle.valueOf(attributes.getValue("style"));
  581. painter.drawLine(new Point(x1, y1), new Point(x2, y2), width, color, style);
  582. }
  583. }
  584. private static final String[] SIDES = new String[] {"top", "bottom", "left", "right"};
  585. private class BorderRectHandler extends AbstractElementHandler {
  586. public void startElement(Attributes attributes) throws IFException {
  587. int x = Integer.parseInt(attributes.getValue("x"));
  588. int y = Integer.parseInt(attributes.getValue("y"));
  589. int width = Integer.parseInt(attributes.getValue("width"));
  590. int height = Integer.parseInt(attributes.getValue("height"));
  591. BorderProps[] borders = new BorderProps[4];
  592. for (int i = 0; i < 4; i++) {
  593. String b = attributes.getValue(SIDES[i]);
  594. if (b != null) {
  595. borders[i] = BorderProps.valueOf(userAgent, b);
  596. }
  597. }
  598. painter.drawBorderRect(new Rectangle(x, y, width, height),
  599. borders[0], borders[1], borders[2], borders[3]);
  600. }
  601. }
  602. private class ImageHandler extends AbstractElementHandler {
  603. public void startElement(Attributes attributes) throws IFException {
  604. inForeignObject = true;
  605. }
  606. public void endElement() throws IFException {
  607. int x = Integer.parseInt(lastAttributes.getValue("x"));
  608. int y = Integer.parseInt(lastAttributes.getValue("y"));
  609. int width = Integer.parseInt(lastAttributes.getValue("width"));
  610. int height = Integer.parseInt(lastAttributes.getValue("height"));
  611. Map<QName, String> foreignAttributes = getForeignAttributes(lastAttributes);
  612. establishForeignAttributes(foreignAttributes);
  613. establishStructureTreeElement(lastAttributes);
  614. if (foreignObject != null) {
  615. painter.drawImage(foreignObject,
  616. new Rectangle(x, y, width, height));
  617. foreignObject = null;
  618. } else {
  619. String uri = lastAttributes.getValue(
  620. XLINK_HREF.getNamespaceURI(), XLINK_HREF.getLocalName());
  621. if (uri == null) {
  622. throw new IFException("xlink:href is missing on image", null);
  623. }
  624. painter.drawImage(uri, new Rectangle(x, y, width, height));
  625. }
  626. resetForeignAttributes();
  627. resetStructureTreeElement();
  628. inForeignObject = false;
  629. }
  630. public boolean ignoreCharacters() {
  631. return false;
  632. }
  633. }
  634. // ====================================================================
  635. /**
  636. * Handles objects created by "sub-parsers" that implement the ObjectSource interface.
  637. * An example of object handled here are ExtensionAttachments.
  638. * @param obj the Object to be handled.
  639. * @throws SAXException if an error occurs while handling the extension object
  640. */
  641. protected void handleExternallyGeneratedObject(Object obj) throws SAXException {
  642. try {
  643. documentHandler.handleExtensionObject(obj);
  644. } catch (IFException ife) {
  645. handleIFException(ife);
  646. }
  647. }
  648. private Color getAttributeAsColor(Attributes attributes, String name)
  649. throws PropertyException {
  650. String s = attributes.getValue(name);
  651. if (s == null) {
  652. return null;
  653. } else {
  654. return ColorUtil.parseColorString(userAgent, s);
  655. }
  656. }
  657. private static Map<QName, String> getForeignAttributes(Attributes atts) {
  658. Map<QName, String> foreignAttributes = null;
  659. for (int i = 0, c = atts.getLength(); i < c; i++) {
  660. String ns = atts.getURI(i);
  661. if (ns.length() > 0) {
  662. if (handledNamespaces.contains(ns)) {
  663. continue;
  664. }
  665. if (foreignAttributes == null) {
  666. foreignAttributes = new java.util.HashMap<QName, String>();
  667. }
  668. QName qname = new QName(ns, atts.getQName(i));
  669. foreignAttributes.put(qname, atts.getValue(i));
  670. }
  671. }
  672. return foreignAttributes;
  673. }
  674. private void establishStructureTreeElement(Attributes attributes) {
  675. String structRef = attributes.getValue(InternalElementMapping.URI,
  676. InternalElementMapping.STRUCT_REF);
  677. if (structRef != null && structRef.length() > 0) {
  678. assert structureTreeElements.containsKey(structRef);
  679. StructureTreeElement structureTreeElement = structureTreeElements.get(structRef);
  680. documentHandler.getContext().setStructureTreeElement(structureTreeElement);
  681. }
  682. }
  683. private void resetStructureTreeElement() {
  684. documentHandler.getContext().resetStructureTreeElement();
  685. }
  686. /** {@inheritDoc} */
  687. public void characters(char[] ch, int start, int length) throws SAXException {
  688. if (delegate != null) {
  689. delegate.characters(ch, start, length);
  690. } else if (!ignoreCharacters) {
  691. this.content.append(ch, start, length);
  692. }
  693. }
  694. }
  695. }