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

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