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

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