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.

AreaTreeParser.java 54KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249
  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.area;
  19. import java.awt.Color;
  20. import java.awt.Rectangle;
  21. import java.awt.geom.Rectangle2D;
  22. import java.io.FileNotFoundException;
  23. import java.io.IOException;
  24. import java.nio.CharBuffer;
  25. import java.util.List;
  26. import java.util.Map;
  27. import java.util.Set;
  28. import java.util.Stack;
  29. import javax.xml.transform.Source;
  30. import javax.xml.transform.Transformer;
  31. import javax.xml.transform.TransformerConfigurationException;
  32. import javax.xml.transform.TransformerException;
  33. import javax.xml.transform.dom.DOMResult;
  34. import javax.xml.transform.sax.SAXResult;
  35. import javax.xml.transform.sax.SAXTransformerFactory;
  36. import javax.xml.transform.sax.TransformerHandler;
  37. import org.w3c.dom.DOMImplementation;
  38. import org.w3c.dom.Document;
  39. import org.xml.sax.Attributes;
  40. import org.xml.sax.ContentHandler;
  41. import org.xml.sax.Locator;
  42. import org.xml.sax.SAXException;
  43. import org.xml.sax.helpers.AttributesImpl;
  44. import org.xml.sax.helpers.DefaultHandler;
  45. import org.apache.commons.logging.Log;
  46. import org.apache.commons.logging.LogFactory;
  47. import org.apache.xmlgraphics.image.loader.ImageException;
  48. import org.apache.xmlgraphics.image.loader.ImageInfo;
  49. import org.apache.xmlgraphics.image.loader.ImageManager;
  50. import org.apache.xmlgraphics.image.loader.ImageSessionContext;
  51. import org.apache.xmlgraphics.util.QName;
  52. import org.apache.fop.ResourceEventProducer;
  53. import org.apache.fop.accessibility.AccessibilityEventProducer;
  54. import org.apache.fop.accessibility.StructureTreeBuilder;
  55. import org.apache.fop.apps.FOUserAgent;
  56. import org.apache.fop.area.Trait.Background;
  57. import org.apache.fop.area.Trait.InternalLink;
  58. import org.apache.fop.area.inline.AbstractTextArea;
  59. import org.apache.fop.area.inline.ForeignObject;
  60. import org.apache.fop.area.inline.Image;
  61. import org.apache.fop.area.inline.InlineArea;
  62. import org.apache.fop.area.inline.InlineBlockParent;
  63. import org.apache.fop.area.inline.InlineParent;
  64. import org.apache.fop.area.inline.Leader;
  65. import org.apache.fop.area.inline.Space;
  66. import org.apache.fop.area.inline.SpaceArea;
  67. import org.apache.fop.area.inline.TextArea;
  68. import org.apache.fop.area.inline.Viewport;
  69. import org.apache.fop.area.inline.WordArea;
  70. import org.apache.fop.fo.Constants;
  71. import org.apache.fop.fo.ElementMappingRegistry;
  72. import org.apache.fop.fo.expr.PropertyException;
  73. import org.apache.fop.fo.extensions.ExtensionAttachment;
  74. import org.apache.fop.fonts.Font;
  75. import org.apache.fop.fonts.FontInfo;
  76. import org.apache.fop.traits.BorderProps;
  77. import org.apache.fop.util.ColorUtil;
  78. import org.apache.fop.util.ContentHandlerFactory;
  79. import org.apache.fop.util.ContentHandlerFactoryRegistry;
  80. import org.apache.fop.util.ConversionUtils;
  81. import org.apache.fop.util.DefaultErrorListener;
  82. import org.apache.fop.util.DelegatingContentHandler;
  83. import org.apache.fop.util.XMLConstants;
  84. import org.apache.fop.util.XMLUtil;
  85. /**
  86. * This is a parser for the area tree XML (intermediate format) which is used to reread an area
  87. * tree (or part of it) into memory again for rendering to the final output format.
  88. */
  89. public class AreaTreeParser {
  90. /** Logger instance */
  91. protected static Log log = LogFactory.getLog(AreaTreeParser.class);
  92. private static SAXTransformerFactory tFactory
  93. = (SAXTransformerFactory)SAXTransformerFactory.newInstance();
  94. /**
  95. * Parses an intermediate file (area tree XML) into an AreaTreeModel instance by adding
  96. * pages to it.
  97. * @param src the Source instance pointing to the intermediate file
  98. * @param treeModel the AreaTreeModel that the parsed pages are added to
  99. * @param userAgent the user agent
  100. * @throws TransformerException if an error occurs while parsing the area tree XML
  101. */
  102. public void parse(Source src, AreaTreeModel treeModel, FOUserAgent userAgent)
  103. throws TransformerException {
  104. Transformer transformer = tFactory.newTransformer();
  105. transformer.setErrorListener(new DefaultErrorListener(log));
  106. SAXResult res = new SAXResult(getContentHandler(treeModel, userAgent));
  107. transformer.transform(src, res);
  108. }
  109. /**
  110. * Creates a new ContentHandler instance that you can send the area tree XML to. The parsed
  111. * pages are added to the AreaTreeModel instance you pass in as a parameter.
  112. * @param treeModel the AreaTreeModel that the parsed pages are added to
  113. * @param userAgent the user agent
  114. * @return the ContentHandler instance to receive the SAX stream from the area tree XML
  115. */
  116. public ContentHandler getContentHandler(AreaTreeModel treeModel, FOUserAgent userAgent) {
  117. ElementMappingRegistry elementMappingRegistry
  118. = userAgent.getFactory().getElementMappingRegistry();
  119. return new Handler(treeModel, userAgent, elementMappingRegistry);
  120. }
  121. private static class Handler extends DefaultHandler {
  122. private Map makers = new java.util.HashMap();
  123. private AreaTreeModel treeModel;
  124. private FOUserAgent userAgent;
  125. private ElementMappingRegistry elementMappingRegistry;
  126. private Attributes lastAttributes;
  127. private CharBuffer content = CharBuffer.allocate(64);
  128. private boolean ignoreCharacters = true;
  129. private PageViewport currentPageViewport;
  130. private Map pageViewportsByKey = new java.util.HashMap();
  131. // set of "ID firsts" that have already been assigned to a PV:
  132. private Set idFirstsAssigned = new java.util.HashSet();
  133. private Stack areaStack = new Stack();
  134. private boolean firstFlow;
  135. private Stack delegateStack = new Stack();
  136. private ContentHandler delegate;
  137. private DOMImplementation domImplementation;
  138. private Locator locator;
  139. private StructureTreeBuilder structureTreeBuilder;
  140. private ContentHandler structureTreeBuilderWrapper;
  141. private Attributes pageSequenceAttributes;
  142. private final class StructureTreeBuilderWrapper extends DelegatingContentHandler {
  143. private StructureTreeBuilderWrapper()
  144. throws SAXException {
  145. super(structureTreeBuilder.getHandlerForNextPageSequence());
  146. }
  147. public void endDocument() throws SAXException {
  148. super.endDocument();
  149. startAreaTreeElement("pageSequence", pageSequenceAttributes);
  150. pageSequenceAttributes = null;
  151. }
  152. }
  153. public Handler(AreaTreeModel treeModel, FOUserAgent userAgent,
  154. ElementMappingRegistry elementMappingRegistry) {
  155. this.treeModel = treeModel;
  156. this.userAgent = userAgent;
  157. this.elementMappingRegistry = elementMappingRegistry;
  158. makers.put("areaTree", new AreaTreeMaker());
  159. makers.put("page", new PageMaker());
  160. makers.put("pageSequence", new PageSequenceMaker());
  161. makers.put("title", new TitleMaker());
  162. makers.put("pageViewport", new PageViewportMaker());
  163. makers.put("regionViewport", new RegionViewportMaker());
  164. makers.put("regionBefore", new RegionBeforeMaker());
  165. makers.put("regionAfter", new RegionAfterMaker());
  166. makers.put("regionStart", new RegionStartMaker());
  167. makers.put("regionEnd", new RegionEndMaker());
  168. makers.put("regionBody", new RegionBodyMaker());
  169. makers.put("flow", new FlowMaker());
  170. makers.put("mainReference", new MainReferenceMaker());
  171. makers.put("span", new SpanMaker());
  172. makers.put("footnote", new FootnoteMaker());
  173. makers.put("beforeFloat", new BeforeFloatMaker());
  174. makers.put("block", new BlockMaker());
  175. makers.put("lineArea", new LineAreaMaker());
  176. makers.put("inline", new InlineMaker());
  177. makers.put("inlineparent", new InlineParentMaker());
  178. makers.put("inlineblockparent", new InlineBlockParentMaker());
  179. makers.put("text", new TextMaker());
  180. makers.put("word", new WordMaker());
  181. makers.put("space", new SpaceMaker());
  182. makers.put("leader", new LeaderMaker());
  183. makers.put("viewport", new ViewportMaker());
  184. makers.put("image", new ImageMaker());
  185. makers.put("foreignObject", new ForeignObjectMaker());
  186. makers.put("bookmarkTree", new BookmarkTreeMaker());
  187. makers.put("bookmark", new BookmarkMaker());
  188. makers.put("destination", new DestinationMaker());
  189. if (userAgent.isAccessibilityEnabled()) {
  190. structureTreeBuilder = new StructureTreeBuilder(tFactory);
  191. userAgent.setStructureTree(structureTreeBuilder.getStructureTree());
  192. }
  193. }
  194. private Area findAreaType(Class clazz) {
  195. if (areaStack.size() > 0) {
  196. int pos = areaStack.size() - 1;
  197. Object obj = null;
  198. while ( pos >= 0 ) {
  199. obj = areaStack.get(pos);
  200. if ( clazz.isInstance ( obj ) ) {
  201. break;
  202. } else {
  203. pos--;
  204. }
  205. }
  206. if (pos >= 0) {
  207. return (Area)obj;
  208. }
  209. }
  210. return null;
  211. }
  212. private RegionViewport getCurrentRegionViewport() {
  213. return (RegionViewport)findAreaType(RegionViewport.class);
  214. }
  215. private BodyRegion getCurrentBodyRegion() {
  216. return (BodyRegion)findAreaType(BodyRegion.class);
  217. }
  218. private BlockParent getCurrentBlockParent() {
  219. return (BlockParent)findAreaType(BlockParent.class);
  220. }
  221. private AbstractTextArea getCurrentText() {
  222. return (AbstractTextArea)findAreaType(AbstractTextArea.class);
  223. }
  224. private Viewport getCurrentViewport() {
  225. return (Viewport)findAreaType(Viewport.class);
  226. }
  227. /** {@inheritDoc} */
  228. public void setDocumentLocator(Locator locator) {
  229. this.locator = locator;
  230. }
  231. private Locator getLocator() {
  232. return this.locator;
  233. }
  234. /** {@inheritDoc} */
  235. public void startElement(String uri, String localName, String qName, Attributes attributes)
  236. throws SAXException {
  237. if (delegate != null) {
  238. delegateStack.push(qName);
  239. delegate.startElement(uri, localName, qName, attributes);
  240. } else if (domImplementation != null) {
  241. //domImplementation is set so we need to start a new DOM building sub-process
  242. TransformerHandler handler;
  243. try {
  244. handler = tFactory.newTransformerHandler();
  245. } catch (TransformerConfigurationException e) {
  246. throw new SAXException("Error creating a new TransformerHandler", e);
  247. }
  248. Document doc = domImplementation.createDocument(uri, qName, null);
  249. //It's easier to work with an empty document, so remove the root element
  250. doc.removeChild(doc.getDocumentElement());
  251. handler.setResult(new DOMResult(doc));
  252. Area parent = (Area)areaStack.peek();
  253. ((ForeignObject)parent).setDocument(doc);
  254. //activate delegate for nested foreign document
  255. domImplementation = null; //Not needed anymore now
  256. this.delegate = handler;
  257. delegateStack.push(qName);
  258. delegate.startDocument();
  259. delegate.startElement(uri, localName, qName, attributes);
  260. } else {
  261. boolean handled = true;
  262. if ("".equals(uri)) {
  263. if (localName.equals("pageSequence") && userAgent.isAccessibilityEnabled()) {
  264. structureTreeBuilderWrapper = new StructureTreeBuilderWrapper();
  265. pageSequenceAttributes = new AttributesImpl(attributes);
  266. } else if (localName.equals("structureTree")) {
  267. if (userAgent.isAccessibilityEnabled()) {
  268. delegate = structureTreeBuilderWrapper;
  269. } else {
  270. /* Delegate to a handler that does nothing */
  271. delegate = new DefaultHandler();
  272. }
  273. delegateStack.push(qName);
  274. delegate.startDocument();
  275. delegate.startElement(uri, localName, qName, attributes);
  276. } else {
  277. if (pageSequenceAttributes != null) {
  278. /*
  279. * This means that no structure-element tag was
  280. * found in the XML, otherwise a
  281. * StructureTreeBuilderWrapper object would have
  282. * been created, which would have reset the
  283. * pageSequenceAttributes field.
  284. */
  285. AccessibilityEventProducer.Provider
  286. .get(userAgent.getEventBroadcaster())
  287. .noStructureTreeInXML(this);
  288. }
  289. handled = startAreaTreeElement(localName, attributes);
  290. }
  291. } else {
  292. ContentHandlerFactoryRegistry registry
  293. = userAgent.getFactory().getContentHandlerFactoryRegistry();
  294. ContentHandlerFactory factory = registry.getFactory(uri);
  295. if (factory != null) {
  296. delegate = factory.createContentHandler();
  297. delegateStack.push(qName);
  298. delegate.startDocument();
  299. delegate.startElement(uri, localName, qName, attributes);
  300. } else {
  301. handled = false;
  302. }
  303. }
  304. if (!handled) {
  305. if (uri == null || uri.length() == 0) {
  306. throw new SAXException("Unhandled element " + localName
  307. + " in namespace: " + uri);
  308. } else {
  309. log.warn("Unhandled element " + localName
  310. + " in namespace: " + uri);
  311. }
  312. }
  313. }
  314. }
  315. private boolean startAreaTreeElement(String localName, Attributes attributes)
  316. throws SAXException {
  317. lastAttributes = new AttributesImpl(attributes);
  318. Maker maker = (Maker)makers.get(localName);
  319. content.clear();
  320. ignoreCharacters = true;
  321. if (maker != null) {
  322. ignoreCharacters = maker.ignoreCharacters();
  323. maker.startElement(attributes);
  324. } else if ("extension-attachments".equals(localName)) {
  325. //TODO implement me
  326. } else {
  327. return false;
  328. }
  329. return true;
  330. }
  331. /** {@inheritDoc} */
  332. public void endElement(String uri, String localName, String qName) throws SAXException {
  333. if (delegate != null) {
  334. delegate.endElement(uri, localName, qName);
  335. delegateStack.pop();
  336. if (delegateStack.size() == 0) {
  337. delegate.endDocument();
  338. if (delegate instanceof ContentHandlerFactory.ObjectSource) {
  339. Object obj = ((ContentHandlerFactory.ObjectSource)delegate).getObject();
  340. handleExternallyGeneratedObject(obj);
  341. }
  342. delegate = null; //Sub-document is processed, return to normal processing
  343. }
  344. } else {
  345. if ("".equals(uri)) {
  346. Maker maker = (Maker)makers.get(localName);
  347. if (maker != null) {
  348. maker.endElement();
  349. content.clear();
  350. }
  351. ignoreCharacters = true;
  352. } else {
  353. //log.debug("Ignoring " + localName + " in namespace: " + uri);
  354. }
  355. }
  356. }
  357. // ============== Maker classes for the area tree objects =============
  358. private static interface Maker {
  359. void startElement(Attributes attributes) throws SAXException;
  360. void endElement();
  361. boolean ignoreCharacters();
  362. }
  363. private abstract class AbstractMaker implements Maker {
  364. public void startElement(Attributes attributes) throws SAXException {
  365. //nop
  366. }
  367. public void endElement() {
  368. //nop
  369. }
  370. public boolean ignoreCharacters() {
  371. return true;
  372. }
  373. }
  374. private class AreaTreeMaker extends AbstractMaker {
  375. public void startElement(Attributes attributes) {
  376. // In case the Handler is reused:
  377. idFirstsAssigned.clear();
  378. }
  379. }
  380. private class PageSequenceMaker extends AbstractMaker {
  381. public void startElement(Attributes attributes) {
  382. PageSequence pageSequence = new PageSequence(null);
  383. String lang = attributes.getValue("language");
  384. pageSequence.setLanguage(lang);
  385. String country = attributes.getValue("country");
  386. pageSequence.setCountry(country);
  387. transferForeignObjects(attributes, pageSequence);
  388. areaStack.push(pageSequence);
  389. }
  390. }
  391. private class TitleMaker extends AbstractMaker {
  392. public void startElement(Attributes attributes) {
  393. LineArea line = new LineArea();
  394. transferForeignObjects(attributes, line);
  395. areaStack.push(line);
  396. }
  397. public void endElement() {
  398. LineArea line = (LineArea)areaStack.pop();
  399. PageSequence pageSequence = (PageSequence)areaStack.peek();
  400. pageSequence.setTitle(line);
  401. }
  402. }
  403. private class PageViewportMaker extends AbstractMaker {
  404. public void startElement(Attributes attributes) {
  405. if (!areaStack.isEmpty()) {
  406. PageSequence pageSequence = (PageSequence)areaStack.peek();
  407. treeModel.startPageSequence(pageSequence);
  408. areaStack.pop();
  409. }
  410. if (currentPageViewport != null) {
  411. throw new IllegalStateException("currentPageViewport must be null");
  412. }
  413. Rectangle viewArea = XMLUtil.getAttributeAsRectangle(attributes, "bounds");
  414. int pageNumber = XMLUtil.getAttributeAsInt(attributes, "nr", -1);
  415. String key = attributes.getValue("key");
  416. String pageNumberString = attributes.getValue("formatted-nr");
  417. String pageMaster = attributes.getValue("simple-page-master-name");
  418. boolean blank = XMLUtil.getAttributeAsBoolean(attributes, "blank", false);
  419. currentPageViewport = new PageViewport(viewArea,
  420. pageNumber, pageNumberString,
  421. pageMaster, blank);
  422. transferForeignObjects(attributes, currentPageViewport);
  423. currentPageViewport.setKey(key);
  424. pageViewportsByKey.put(key, currentPageViewport);
  425. }
  426. }
  427. private class PageMaker extends AbstractMaker {
  428. public void startElement(Attributes attributes) {
  429. Page p = new Page();
  430. currentPageViewport.setPage(p);
  431. }
  432. public void endElement() {
  433. treeModel.addPage(currentPageViewport);
  434. currentPageViewport = null;
  435. }
  436. }
  437. private class RegionViewportMaker extends AbstractMaker {
  438. public void startElement(Attributes attributes) {
  439. RegionViewport rv = getCurrentRegionViewport();
  440. if (rv != null) {
  441. throw new IllegalStateException("Current RegionViewport must be null");
  442. }
  443. Rectangle2D viewArea = XMLUtil.getAttributeAsRectangle2D(attributes, "rect");
  444. rv = new RegionViewport(viewArea);
  445. transferForeignObjects(attributes, rv);
  446. rv.setClip(XMLUtil.getAttributeAsBoolean(attributes, "clipped", false));
  447. setAreaAttributes(attributes, rv);
  448. setTraits(attributes, rv, SUBSET_COMMON);
  449. setTraits(attributes, rv, SUBSET_BOX);
  450. setTraits(attributes, rv, SUBSET_COLOR);
  451. areaStack.push(rv);
  452. }
  453. public void endElement() {
  454. assertObjectOfClass(areaStack.pop(), RegionViewport.class);
  455. }
  456. }
  457. private class RegionBeforeMaker extends AbstractMaker {
  458. public void startElement(Attributes attributes) {
  459. pushNewRegionReference(attributes, Constants.FO_REGION_BEFORE);
  460. }
  461. public void endElement() {
  462. assertObjectOfClass(areaStack.pop(), RegionReference.class);
  463. }
  464. }
  465. private class RegionAfterMaker extends AbstractMaker {
  466. public void startElement(Attributes attributes) {
  467. pushNewRegionReference(attributes, Constants.FO_REGION_AFTER);
  468. }
  469. public void endElement() {
  470. assertObjectOfClass(areaStack.pop(), RegionReference.class);
  471. }
  472. }
  473. private class RegionStartMaker extends AbstractMaker {
  474. public void startElement(Attributes attributes) {
  475. pushNewRegionReference(attributes, Constants.FO_REGION_START);
  476. }
  477. public void endElement() {
  478. assertObjectOfClass(areaStack.pop(), RegionReference.class);
  479. }
  480. }
  481. private class RegionEndMaker extends AbstractMaker {
  482. public void startElement(Attributes attributes) {
  483. pushNewRegionReference(attributes, Constants.FO_REGION_END);
  484. }
  485. public void endElement() {
  486. assertObjectOfClass(areaStack.pop(), RegionReference.class);
  487. }
  488. }
  489. private class RegionBodyMaker extends AbstractMaker {
  490. public void startElement(Attributes attributes) {
  491. BodyRegion body = getCurrentBodyRegion();
  492. if (body != null) {
  493. throw new IllegalStateException("Current BodyRegion must be null");
  494. }
  495. String regionName = attributes.getValue("name");
  496. int columnCount = XMLUtil.getAttributeAsInt(attributes, "columnCount", 1);
  497. int columnGap = XMLUtil.getAttributeAsInt(attributes, "columnGap", 0);
  498. RegionViewport rv = getCurrentRegionViewport();
  499. body = new BodyRegion(Constants.FO_REGION_BODY,
  500. regionName, rv, columnCount, columnGap);
  501. transferForeignObjects(attributes, body);
  502. body.setCTM(getAttributeAsCTM(attributes, "ctm"));
  503. setAreaAttributes(attributes, body);
  504. setTraits(attributes, body, SUBSET_BORDER_PADDING);
  505. rv.setRegionReference(body);
  506. currentPageViewport.getPage().setRegionViewport(
  507. Constants.FO_REGION_BODY, rv);
  508. areaStack.push(body);
  509. }
  510. public void endElement() {
  511. assertObjectOfClass(areaStack.pop(), BodyRegion.class);
  512. }
  513. }
  514. private class FlowMaker extends AbstractMaker {
  515. public void startElement(Attributes attributes) {
  516. BodyRegion body = getCurrentBodyRegion();
  517. if (!firstFlow) {
  518. body.getMainReference().getCurrentSpan().moveToNextFlow();
  519. } else {
  520. firstFlow = false;
  521. }
  522. NormalFlow flow = body.getMainReference().getCurrentSpan().getCurrentFlow();
  523. transferForeignObjects(attributes, flow);
  524. setAreaAttributes(attributes, flow);
  525. areaStack.push(flow);
  526. }
  527. public void endElement() {
  528. assertObjectOfClass(areaStack.pop(), NormalFlow.class);
  529. }
  530. }
  531. private class MainReferenceMaker extends AbstractMaker {
  532. public void startElement(Attributes attributes) {
  533. //mainReference is created by the BodyRegion
  534. MainReference mr = getCurrentBodyRegion().getMainReference();
  535. transferForeignObjects(attributes, mr);
  536. setAreaAttributes(attributes, mr);
  537. }
  538. }
  539. private class SpanMaker extends AbstractMaker {
  540. public void startElement(Attributes attributes) {
  541. int ipd = XMLUtil.getAttributeAsInt(attributes, "ipd", 0);
  542. int columnCount = XMLUtil.getAttributeAsInt(attributes, "columnCount", 1);
  543. BodyRegion body = getCurrentBodyRegion();
  544. Span span = new Span(columnCount,
  545. body.getColumnGap(), ipd);
  546. transferForeignObjects(attributes, span);
  547. setAreaAttributes(attributes, span);
  548. body.getMainReference().getSpans().add(span);
  549. firstFlow = true;
  550. }
  551. }
  552. private class FootnoteMaker extends AbstractMaker {
  553. public void startElement(Attributes attributes) {
  554. Footnote fn = getCurrentBodyRegion().getFootnote();
  555. transferForeignObjects(attributes, fn);
  556. fn.setTop(XMLUtil.getAttributeAsInt(attributes, "top-offset", 0));
  557. areaStack.push(fn);
  558. }
  559. public void endElement() {
  560. assertObjectOfClass(areaStack.pop(), Footnote.class);
  561. }
  562. }
  563. private class BeforeFloatMaker extends AbstractMaker {
  564. public void startElement(Attributes attributes) {
  565. BeforeFloat bf = getCurrentBodyRegion().getBeforeFloat();
  566. transferForeignObjects(attributes, bf);
  567. areaStack.push(bf);
  568. }
  569. public void endElement() {
  570. assertObjectOfClass(areaStack.pop(), BeforeFloat.class);
  571. }
  572. }
  573. private class BlockMaker extends AbstractMaker {
  574. public void startElement(Attributes attributes) {
  575. boolean isViewport = XMLUtil.getAttributeAsBoolean(attributes,
  576. "is-viewport-area", false);
  577. Block block;
  578. if (isViewport) {
  579. BlockViewport bv = new BlockViewport();
  580. bv.setClip(XMLUtil.getAttributeAsBoolean(attributes, "clipped", false));
  581. bv.setCTM(getAttributeAsCTM(attributes, "ctm"));
  582. if (bv.getPositioning() != BlockViewport.RELATIVE) {
  583. bv.setXOffset(
  584. XMLUtil.getAttributeAsInt(attributes, "left-position", 0));
  585. bv.setYOffset(
  586. XMLUtil.getAttributeAsInt(attributes, "top-position", 0));
  587. }
  588. block = bv;
  589. } else {
  590. block = new Block();
  591. }
  592. String positioning = attributes.getValue("positioning");
  593. if ("absolute".equalsIgnoreCase(positioning)) {
  594. block.setPositioning(Block.ABSOLUTE);
  595. } else if ("fixed".equalsIgnoreCase(positioning)) {
  596. block.setPositioning(Block.FIXED);
  597. } else if ("relative".equalsIgnoreCase(positioning)) {
  598. block.setPositioning(Block.RELATIVE);
  599. } else {
  600. block.setPositioning(Block.STACK);
  601. }
  602. if (attributes.getValue("left-offset") != null) {
  603. block.setXOffset(XMLUtil.getAttributeAsInt(attributes, "left-offset", 0));
  604. }
  605. if (attributes.getValue("top-offset") != null) {
  606. block.setYOffset(XMLUtil.getAttributeAsInt(attributes, "top-offset", 0));
  607. }
  608. transferForeignObjects(attributes, block);
  609. setAreaAttributes(attributes, block);
  610. setTraits(attributes, block, SUBSET_COMMON);
  611. setTraits(attributes, block, SUBSET_BOX);
  612. setTraits(attributes, block, SUBSET_COLOR);
  613. Area parent = (Area)areaStack.peek();
  614. //BlockParent parent = getCurrentBlockParent();
  615. parent.addChildArea(block);
  616. areaStack.push(block);
  617. }
  618. public void endElement() {
  619. assertObjectOfClass(areaStack.pop(), Block.class);
  620. }
  621. }
  622. private class LineAreaMaker extends AbstractMaker {
  623. public void startElement(Attributes attributes) {
  624. LineArea line = new LineArea();
  625. setAreaAttributes(attributes, line);
  626. setTraits(attributes, line, SUBSET_COMMON);
  627. setTraits(attributes, line, SUBSET_BOX);
  628. setTraits(attributes, line, SUBSET_COLOR);
  629. BlockParent parent = getCurrentBlockParent();
  630. parent.addChildArea(line);
  631. areaStack.push(line);
  632. }
  633. public void endElement() {
  634. assertObjectOfClass(areaStack.pop(), LineArea.class);
  635. }
  636. }
  637. // Maker for "generic" inline areas
  638. private class InlineMaker extends AbstractMaker {
  639. public void startElement(Attributes attributes) {
  640. InlineArea inl = new InlineArea();
  641. transferForeignObjects(attributes, inl);
  642. inl.setOffset(XMLUtil.getAttributeAsInt(attributes, "offset", 0));
  643. setAreaAttributes(attributes, inl);
  644. setTraits(attributes, inl, SUBSET_COMMON);
  645. setTraits(attributes, inl, SUBSET_BOX);
  646. setTraits(attributes, inl, SUBSET_COLOR);
  647. Area parent = (Area)areaStack.peek();
  648. parent.addChildArea(inl);
  649. areaStack.push(inl);
  650. }
  651. public void endElement() {
  652. assertObjectOfClass(areaStack.pop(), InlineArea.class);
  653. }
  654. }
  655. private class InlineParentMaker extends AbstractMaker {
  656. public void startElement(Attributes attributes) {
  657. InlineParent ip = new InlineParent();
  658. transferForeignObjects(attributes, ip);
  659. ip.setOffset(XMLUtil.getAttributeAsInt(attributes, "offset", 0));
  660. setAreaAttributes(attributes, ip);
  661. setTraits(attributes, ip, SUBSET_COMMON);
  662. setTraits(attributes, ip, SUBSET_BOX);
  663. setTraits(attributes, ip, SUBSET_COLOR);
  664. setTraits(attributes, ip, SUBSET_LINK);
  665. setPtr(ip, attributes);
  666. Area parent = (Area)areaStack.peek();
  667. parent.addChildArea(ip);
  668. areaStack.push(ip);
  669. }
  670. public void endElement() {
  671. assertObjectOfClass(areaStack.pop(), InlineParent.class);
  672. }
  673. }
  674. private class InlineBlockParentMaker extends AbstractMaker {
  675. public void startElement(Attributes attributes) {
  676. InlineBlockParent ibp = new InlineBlockParent();
  677. transferForeignObjects(attributes, ibp);
  678. ibp.setOffset(XMLUtil.getAttributeAsInt(attributes, "offset", 0));
  679. setAreaAttributes(attributes, ibp);
  680. setTraits(attributes, ibp, SUBSET_COMMON);
  681. setTraits(attributes, ibp, SUBSET_BOX);
  682. setTraits(attributes, ibp, SUBSET_COLOR);
  683. Area parent = (Area)areaStack.peek();
  684. parent.addChildArea(ibp);
  685. areaStack.push(ibp);
  686. }
  687. public void endElement() {
  688. assertObjectOfClass(areaStack.pop(), InlineBlockParent.class);
  689. }
  690. }
  691. private class TextMaker extends AbstractMaker {
  692. public void startElement(Attributes attributes) {
  693. if (getCurrentText() != null) {
  694. throw new IllegalStateException("Current Text must be null");
  695. }
  696. TextArea text = new TextArea();
  697. setAreaAttributes(attributes, text);
  698. setTraits(attributes, text, SUBSET_COMMON);
  699. setTraits(attributes, text, SUBSET_BOX);
  700. setTraits(attributes, text, SUBSET_COLOR);
  701. setTraits(attributes, text, SUBSET_FONT);
  702. text.setBaselineOffset(XMLUtil.getAttributeAsInt(attributes, "baseline", 0));
  703. text.setOffset(XMLUtil.getAttributeAsInt(attributes, "offset", 0));
  704. text.setTextLetterSpaceAdjust(XMLUtil.getAttributeAsInt(attributes,
  705. "tlsadjust", 0));
  706. text.setTextWordSpaceAdjust(XMLUtil.getAttributeAsInt(attributes,
  707. "twsadjust", 0));
  708. setPtr(text, attributes);
  709. Area parent = (Area)areaStack.peek();
  710. parent.addChildArea(text);
  711. areaStack.push(text);
  712. }
  713. public void endElement() {
  714. assertObjectOfClass(areaStack.pop(), TextArea.class);
  715. }
  716. }
  717. private class WordMaker extends AbstractMaker {
  718. public void endElement() {
  719. int offset = XMLUtil.getAttributeAsInt(lastAttributes, "offset", 0);
  720. int[] letterAdjust
  721. = ConversionUtils.toIntArray(
  722. lastAttributes.getValue("letter-adjust"), "\\s");
  723. content.flip();
  724. WordArea word = new WordArea(content.toString().trim(), offset, letterAdjust);
  725. AbstractTextArea text = getCurrentText();
  726. word.setParentArea(text);
  727. text.addChildArea(word);
  728. }
  729. public boolean ignoreCharacters() {
  730. return false;
  731. }
  732. }
  733. private class SpaceMaker extends AbstractMaker {
  734. public void endElement() {
  735. int offset = XMLUtil.getAttributeAsInt(lastAttributes, "offset", 0);
  736. //TODO the isAdjustable parameter is currently not used/implemented
  737. if (content.position() > 0) {
  738. content.flip();
  739. boolean adjustable = XMLUtil.getAttributeAsBoolean(lastAttributes, "adj", true);
  740. SpaceArea space = new SpaceArea(content.charAt(0), offset, adjustable);
  741. AbstractTextArea text = getCurrentText();
  742. space.setParentArea(text);
  743. text.addChildArea(space);
  744. } else {
  745. Space space = new Space();
  746. setAreaAttributes(lastAttributes, space);
  747. setTraits(lastAttributes, space, SUBSET_COMMON);
  748. setTraits(lastAttributes, space, SUBSET_BOX);
  749. setTraits(lastAttributes, space, SUBSET_COLOR);
  750. space.setOffset(offset);
  751. Area parent = (Area)areaStack.peek();
  752. parent.addChildArea(space);
  753. }
  754. }
  755. public boolean ignoreCharacters() {
  756. return false;
  757. }
  758. }
  759. private class LeaderMaker extends AbstractMaker {
  760. public void startElement(Attributes attributes) {
  761. Leader leader = new Leader();
  762. transferForeignObjects(attributes, leader);
  763. setAreaAttributes(attributes, leader);
  764. setTraits(attributes, leader, SUBSET_COMMON);
  765. setTraits(attributes, leader, SUBSET_BOX);
  766. setTraits(attributes, leader, SUBSET_COLOR);
  767. setTraits(attributes, leader, SUBSET_FONT);
  768. leader.setOffset(XMLUtil.getAttributeAsInt(attributes, "offset", 0));
  769. String ruleStyle = attributes.getValue("ruleStyle");
  770. if (ruleStyle != null) {
  771. leader.setRuleStyle(ruleStyle);
  772. }
  773. leader.setRuleThickness(
  774. XMLUtil.getAttributeAsInt(attributes, "ruleThickness", 0));
  775. Area parent = (Area)areaStack.peek();
  776. parent.addChildArea(leader);
  777. }
  778. }
  779. private class ViewportMaker extends AbstractMaker {
  780. public void startElement(Attributes attributes) {
  781. Viewport viewport = new Viewport(null);
  782. transferForeignObjects(attributes, viewport);
  783. setAreaAttributes(attributes, viewport);
  784. setTraits(attributes, viewport, SUBSET_COMMON);
  785. setTraits(attributes, viewport, SUBSET_BOX);
  786. setTraits(attributes, viewport, SUBSET_COLOR);
  787. viewport.setContentPosition(XMLUtil.getAttributeAsRectangle2D(attributes, "pos"));
  788. viewport.setClip(XMLUtil.getAttributeAsBoolean(attributes, "clip", false));
  789. viewport.setOffset(XMLUtil.getAttributeAsInt(attributes, "offset", 0));
  790. setPtr(viewport, attributes);
  791. Area parent = (Area)areaStack.peek();
  792. parent.addChildArea(viewport);
  793. areaStack.push(viewport);
  794. }
  795. public void endElement() {
  796. assertObjectOfClass(areaStack.pop(), Viewport.class);
  797. }
  798. }
  799. private class ImageMaker extends AbstractMaker {
  800. public void startElement(Attributes attributes) {
  801. String url = attributes.getValue("url");
  802. Image image = new Image(url);
  803. transferForeignObjects(attributes, image);
  804. setAreaAttributes(attributes, image);
  805. setTraits(attributes, image, SUBSET_COMMON);
  806. setPtr(image, attributes);
  807. getCurrentViewport().setContent(image);
  808. }
  809. }
  810. private class ForeignObjectMaker extends AbstractMaker {
  811. public void startElement(Attributes attributes) throws SAXException {
  812. String ns = attributes.getValue("ns");
  813. domImplementation
  814. = elementMappingRegistry.getDOMImplementationForNamespace(ns);
  815. if (domImplementation == null) {
  816. throw new SAXException("No DOMImplementation could be"
  817. + " identified to handle namespace: " + ns);
  818. }
  819. ForeignObject foreign = new ForeignObject(ns);
  820. transferForeignObjects(attributes, foreign);
  821. setAreaAttributes(attributes, foreign);
  822. setTraits(attributes, foreign, SUBSET_COMMON);
  823. getCurrentViewport().setContent(foreign);
  824. areaStack.push(foreign);
  825. }
  826. public void endElement() {
  827. assertObjectOfClass(areaStack.pop(), ForeignObject.class);
  828. }
  829. }
  830. private class BookmarkTreeMaker extends AbstractMaker {
  831. public void startElement(Attributes attributes) {
  832. BookmarkData bm = new BookmarkData();
  833. areaStack.push(bm);
  834. }
  835. public void endElement() {
  836. Object tos = areaStack.pop();
  837. assertObjectOfClass(tos, BookmarkData.class);
  838. treeModel.handleOffDocumentItem((BookmarkData) tos);
  839. // as long as the bookmark tree comes after the last PageViewport in the
  840. // area tree XML, we don't have to worry about resolved/unresolved. The
  841. // only resolution needed is the mapping of the pvKey to the PV instance.
  842. }
  843. }
  844. private class BookmarkMaker extends AbstractMaker {
  845. public void startElement(Attributes attributes) {
  846. String title = attributes.getValue("title");
  847. boolean showChildren = XMLUtil.getAttributeAsBoolean(
  848. attributes, "show-children", false);
  849. String[] linkdata
  850. = InternalLink.parseXMLAttribute(attributes.getValue("internal-link"));
  851. PageViewport pv = (PageViewport) pageViewportsByKey.get(linkdata[0]);
  852. BookmarkData bm = new BookmarkData(title, showChildren, pv, linkdata[1]);
  853. Object tos = areaStack.peek();
  854. if (tos instanceof BookmarkData) {
  855. BookmarkData parent = (BookmarkData) tos;
  856. parent.addSubData(bm);
  857. }
  858. areaStack.push(bm);
  859. }
  860. public void endElement() {
  861. assertObjectOfClass(areaStack.pop(), BookmarkData.class);
  862. }
  863. }
  864. private class DestinationMaker extends AbstractMaker {
  865. public void startElement(Attributes attributes) {
  866. String[] linkdata
  867. = InternalLink.parseXMLAttribute(lastAttributes.getValue("internal-link"));
  868. PageViewport pv = (PageViewport) pageViewportsByKey.get(linkdata[0]);
  869. DestinationData dest = new DestinationData(linkdata[1]);
  870. List pages = new java.util.ArrayList();
  871. pages.add(pv);
  872. dest.resolveIDRef(linkdata[1], pages);
  873. areaStack.push(dest);
  874. }
  875. public void endElement() {
  876. Object tos = areaStack.pop();
  877. assertObjectOfClass(tos, DestinationData.class);
  878. treeModel.handleOffDocumentItem((DestinationData) tos);
  879. }
  880. }
  881. // ====================================================================
  882. private void pushNewRegionReference(Attributes attributes, int side) {
  883. String regionName = attributes.getValue("name");
  884. RegionViewport rv = getCurrentRegionViewport();
  885. RegionReference reg = new RegionReference(side,
  886. regionName, rv);
  887. transferForeignObjects(attributes, reg);
  888. reg.setCTM(getAttributeAsCTM(attributes, "ctm"));
  889. setAreaAttributes(attributes, reg);
  890. setTraits(attributes, reg, SUBSET_BORDER_PADDING);
  891. rv.setRegionReference(reg);
  892. currentPageViewport.getPage().setRegionViewport(
  893. side, rv);
  894. areaStack.push(reg);
  895. }
  896. private void assertObjectOfClass(Object obj, Class clazz) {
  897. if (!clazz.isInstance(obj)) {
  898. throw new IllegalStateException("Object is not an instance of "
  899. + clazz.getName() + " but of " + obj.getClass().getName());
  900. }
  901. }
  902. /**
  903. * Handles objects created by "sub-parsers" that implement the ObjectSource interface.
  904. * An example of object handled here are ExtensionAttachments.
  905. * @param obj the Object to be handled.
  906. */
  907. protected void handleExternallyGeneratedObject(Object obj) {
  908. if (areaStack.size() == 0 && obj instanceof ExtensionAttachment) {
  909. ExtensionAttachment attachment = (ExtensionAttachment)obj;
  910. if (this.currentPageViewport == null) {
  911. this.treeModel.handleOffDocumentItem(
  912. new OffDocumentExtensionAttachment(attachment));
  913. } else {
  914. this.currentPageViewport.addExtensionAttachment(attachment);
  915. }
  916. } else {
  917. Object o = areaStack.peek();
  918. if (o instanceof AreaTreeObject && obj instanceof ExtensionAttachment) {
  919. AreaTreeObject ato = (AreaTreeObject)o;
  920. ExtensionAttachment attachment = (ExtensionAttachment)obj;
  921. ato.addExtensionAttachment(attachment);
  922. } else {
  923. log.warn("Don't know how to handle externally generated object: " + obj);
  924. }
  925. }
  926. }
  927. private void setAreaAttributes(Attributes attributes, Area area) {
  928. area.setIPD(Integer.parseInt(attributes.getValue("ipd")));
  929. area.setBPD(Integer.parseInt(attributes.getValue("bpd")));
  930. }
  931. private static final Object[] SUBSET_COMMON = new Object[] {
  932. Trait.PROD_ID};
  933. private static final Object[] SUBSET_LINK = new Object[] {
  934. Trait.INTERNAL_LINK, Trait.EXTERNAL_LINK};
  935. private static final Object[] SUBSET_COLOR = new Object[] {
  936. Trait.BACKGROUND, Trait.COLOR};
  937. private static final Object[] SUBSET_FONT = new Object[] {
  938. Trait.FONT, Trait.FONT_SIZE, Trait.BLINK,
  939. Trait.OVERLINE, Trait.OVERLINE_COLOR,
  940. Trait.LINETHROUGH, Trait.LINETHROUGH_COLOR,
  941. Trait.UNDERLINE, Trait.UNDERLINE_COLOR};
  942. private static final Object[] SUBSET_BOX = new Object[] {
  943. Trait.BORDER_BEFORE, Trait.BORDER_AFTER, Trait.BORDER_START, Trait.BORDER_END,
  944. Trait.SPACE_BEFORE, Trait.SPACE_AFTER, Trait.SPACE_START, Trait.SPACE_END,
  945. Trait.PADDING_BEFORE, Trait.PADDING_AFTER, Trait.PADDING_START, Trait.PADDING_END,
  946. Trait.START_INDENT, Trait.END_INDENT,
  947. Trait.IS_REFERENCE_AREA, Trait.IS_VIEWPORT_AREA};
  948. private static final Object[] SUBSET_BORDER_PADDING = new Object[] {
  949. Trait.BORDER_BEFORE, Trait.BORDER_AFTER, Trait.BORDER_START, Trait.BORDER_END,
  950. Trait.PADDING_BEFORE, Trait.PADDING_AFTER, Trait.PADDING_START, Trait.PADDING_END};
  951. private void setTraits(Attributes attributes, Area area, Object[] traitSubset) {
  952. for (int i = traitSubset.length; --i >= 0;) {
  953. Object trait = traitSubset[i];
  954. String traitName = Trait.getTraitName(trait);
  955. String value = attributes.getValue(traitName);
  956. if (value != null) {
  957. Class cl = Trait.getTraitClass(trait);
  958. if (cl == Integer.class) {
  959. area.addTrait(trait, new Integer(value));
  960. } else if (cl == Boolean.class) {
  961. area.addTrait(trait, Boolean.valueOf(value));
  962. } else if (cl == String.class) {
  963. area.addTrait(trait, value);
  964. if (trait == Trait.PROD_ID
  965. && !idFirstsAssigned.contains(value)
  966. && currentPageViewport != null) {
  967. currentPageViewport.setFirstWithID(value);
  968. idFirstsAssigned.add(value);
  969. }
  970. } else if (cl == Color.class) {
  971. try {
  972. area.addTrait(trait, ColorUtil.parseColorString(this.userAgent, value));
  973. } catch (PropertyException e) {
  974. throw new IllegalArgumentException(e.getMessage());
  975. }
  976. } else if (cl == InternalLink.class) {
  977. area.addTrait(trait, new InternalLink(value));
  978. } else if (cl == Trait.ExternalLink.class) {
  979. area.addTrait(trait, Trait.ExternalLink.makeFromTraitValue(value));
  980. } else if (cl == Background.class) {
  981. Background bkg = new Background();
  982. try {
  983. Color col = ColorUtil.parseColorString(
  984. this.userAgent, attributes.getValue("bkg-color"));
  985. bkg.setColor(col);
  986. } catch (PropertyException e) {
  987. throw new IllegalArgumentException(e.getMessage());
  988. }
  989. String uri = attributes.getValue("bkg-img");
  990. if (uri != null) {
  991. bkg.setURL(uri);
  992. try {
  993. ImageManager manager = userAgent.getFactory().getImageManager();
  994. ImageSessionContext sessionContext
  995. = userAgent.getImageSessionContext();
  996. ImageInfo info = manager.getImageInfo(uri, sessionContext);
  997. bkg.setImageInfo(info);
  998. } catch (ImageException e) {
  999. ResourceEventProducer eventProducer
  1000. = ResourceEventProducer.Provider.get(
  1001. this.userAgent.getEventBroadcaster());
  1002. eventProducer.imageError(this, uri, e, getLocator());
  1003. } catch (FileNotFoundException fnfe) {
  1004. ResourceEventProducer eventProducer
  1005. = ResourceEventProducer.Provider.get(
  1006. this.userAgent.getEventBroadcaster());
  1007. eventProducer.imageNotFound(this, uri, fnfe, getLocator());
  1008. } catch (IOException ioe) {
  1009. ResourceEventProducer eventProducer
  1010. = ResourceEventProducer.Provider.get(
  1011. this.userAgent.getEventBroadcaster());
  1012. eventProducer.imageIOError(this, uri, ioe, getLocator());
  1013. }
  1014. String repeat = attributes.getValue("bkg-repeat");
  1015. if (repeat != null) {
  1016. bkg.setRepeat(repeat);
  1017. }
  1018. bkg.setHoriz(XMLUtil.getAttributeAsInt(attributes,
  1019. "bkg-horz-offset", 0));
  1020. bkg.setVertical(XMLUtil.getAttributeAsInt(attributes,
  1021. "bkg-vert-offset", 0));
  1022. }
  1023. area.addTrait(trait, bkg);
  1024. } else if (cl == BorderProps.class) {
  1025. area.addTrait(trait, BorderProps.valueOf(this.userAgent, value));
  1026. }
  1027. } else {
  1028. if (trait == Trait.FONT) {
  1029. String fontName = attributes.getValue("font-name");
  1030. if (fontName != null) {
  1031. String fontStyle = attributes.getValue("font-style");
  1032. int fontWeight = XMLUtil.getAttributeAsInt(
  1033. attributes, "font-weight", Font.WEIGHT_NORMAL);
  1034. area.addTrait(trait,
  1035. FontInfo.createFontKey(fontName, fontStyle, fontWeight));
  1036. }
  1037. }
  1038. }
  1039. }
  1040. }
  1041. private static CTM getAttributeAsCTM(Attributes attributes, String name) {
  1042. String s = attributes.getValue(name).trim();
  1043. if (s.startsWith("[") && s.endsWith("]")) {
  1044. s = s.substring(1, s.length() - 1);
  1045. double[] values = ConversionUtils.toDoubleArray(s, "\\s");
  1046. if (values.length != 6) {
  1047. throw new IllegalArgumentException("CTM must consist of 6 double values!");
  1048. }
  1049. return new CTM(values[0], values[1], values[2], values[3], values[4], values[5]);
  1050. } else {
  1051. throw new IllegalArgumentException("CTM must be surrounded by square brackets!");
  1052. }
  1053. }
  1054. private static void transferForeignObjects(Attributes atts, AreaTreeObject ato) {
  1055. for (int i = 0, c = atts.getLength(); i < c; i++) {
  1056. String ns = atts.getURI(i);
  1057. if (ns.length() > 0) {
  1058. if (XMLConstants.XMLNS_NAMESPACE_URI.equals(ns)) {
  1059. continue;
  1060. }
  1061. QName qname = new QName(ns, atts.getQName(i));
  1062. ato.setForeignAttribute(qname, atts.getValue(i));
  1063. }
  1064. }
  1065. }
  1066. private void setPtr(Area area, Attributes attributes) {
  1067. String ptr = attributes.getValue("ptr");
  1068. if (ptr != null) {
  1069. area.addTrait(Trait.PTR, ptr);
  1070. }
  1071. }
  1072. /** {@inheritDoc} */
  1073. public void characters(char[] ch, int start, int length) throws SAXException {
  1074. if (delegate != null) {
  1075. delegate.characters(ch, start, length);
  1076. } else if (!ignoreCharacters) {
  1077. int maxLength = this.content.capacity() - this.content.position();
  1078. if (maxLength < length) {
  1079. // allocate a larger buffer and transfer content
  1080. CharBuffer newContent
  1081. = CharBuffer.allocate(this.content.position() + length);
  1082. this.content.flip();
  1083. newContent.put(this.content);
  1084. this.content = newContent;
  1085. }
  1086. // make sure the full capacity is used
  1087. this.content.limit(this.content.capacity());
  1088. // add characters to the buffer
  1089. this.content.put(ch, start, length);
  1090. // decrease the limit, if necessary
  1091. if (this.content.position() < this.content.limit()) {
  1092. this.content.limit(this.content.position());
  1093. }
  1094. }
  1095. }
  1096. }
  1097. }