Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

AreaTreeParser.java 43KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003
  1. /*
  2. * Copyright 2006 The Apache Software Foundation.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. /* $Id$ */
  17. package org.apache.fop.area;
  18. import java.awt.geom.Rectangle2D;
  19. import java.util.Map;
  20. import java.util.Stack;
  21. import java.util.StringTokenizer;
  22. import javax.xml.transform.Source;
  23. import javax.xml.transform.Transformer;
  24. import javax.xml.transform.TransformerConfigurationException;
  25. import javax.xml.transform.TransformerException;
  26. import javax.xml.transform.dom.DOMResult;
  27. import javax.xml.transform.sax.SAXResult;
  28. import javax.xml.transform.sax.SAXTransformerFactory;
  29. import javax.xml.transform.sax.TransformerHandler;
  30. import org.apache.commons.logging.Log;
  31. import org.apache.commons.logging.LogFactory;
  32. import org.apache.fop.apps.FOUserAgent;
  33. import org.apache.fop.area.Trait.Background;
  34. import org.apache.fop.area.Trait.Color;
  35. import org.apache.fop.area.inline.AbstractTextArea;
  36. import org.apache.fop.area.inline.Character;
  37. import org.apache.fop.area.inline.ForeignObject;
  38. import org.apache.fop.area.inline.Image;
  39. import org.apache.fop.area.inline.InlineBlockParent;
  40. import org.apache.fop.area.inline.InlineParent;
  41. import org.apache.fop.area.inline.Leader;
  42. import org.apache.fop.area.inline.Space;
  43. import org.apache.fop.area.inline.SpaceArea;
  44. import org.apache.fop.area.inline.TextArea;
  45. import org.apache.fop.area.inline.Viewport;
  46. import org.apache.fop.area.inline.WordArea;
  47. import org.apache.fop.fo.Constants;
  48. import org.apache.fop.fo.ElementMappingRegistry;
  49. import org.apache.fop.fo.extensions.ExtensionAttachment;
  50. import org.apache.fop.fonts.Font;
  51. import org.apache.fop.fonts.FontTriplet;
  52. import org.apache.fop.image.FopImage;
  53. import org.apache.fop.image.ImageFactory;
  54. import org.apache.fop.traits.BorderProps;
  55. import org.apache.fop.util.ContentHandlerFactory;
  56. import org.apache.fop.util.ContentHandlerFactoryRegistry;
  57. import org.apache.fop.util.DefaultErrorListener;
  58. import org.w3c.dom.DOMImplementation;
  59. import org.w3c.dom.Document;
  60. import org.xml.sax.Attributes;
  61. import org.xml.sax.ContentHandler;
  62. import org.xml.sax.SAXException;
  63. import org.xml.sax.helpers.DefaultHandler;
  64. /**
  65. * This is a parser for the area tree XML (intermediate format) which is used to reread an area
  66. * tree (or part of it) into memory again for rendering to the final output format.
  67. */
  68. public class AreaTreeParser {
  69. /** Logger instance */
  70. protected static Log log = LogFactory.getLog(AreaTreeParser.class);
  71. private static SAXTransformerFactory tFactory
  72. = (SAXTransformerFactory)SAXTransformerFactory.newInstance();
  73. /**
  74. * Parses an intermediate file (area tree XML) into an AreaTreeModel instance by adding
  75. * pages to it.
  76. * @param src the Source instance pointing to the intermediate file
  77. * @param treeModel the AreaTreeModel that the parsed pages are added to
  78. * @param userAgent the user agent
  79. * @throws TransformerException if an error occurs while parsing the area tree XML
  80. */
  81. public void parse(Source src, AreaTreeModel treeModel, FOUserAgent userAgent)
  82. throws TransformerException {
  83. Transformer transformer = tFactory.newTransformer();
  84. transformer.setErrorListener(new DefaultErrorListener(log));
  85. SAXResult res = new SAXResult(getContentHandler(treeModel, userAgent));
  86. transformer.transform(src, res);
  87. }
  88. /**
  89. * Creates a new ContentHandler instance that you can send the area tree XML to. The parsed
  90. * pages are added to the AreaTreeModel instance you pass in as a parameter.
  91. * @param treeModel the AreaTreeModel that the parsed pages are added to
  92. * @param userAgent the user agent
  93. * @return the ContentHandler instance to receive the SAX stream from the area tree XML
  94. */
  95. public ContentHandler getContentHandler(AreaTreeModel treeModel, FOUserAgent userAgent) {
  96. //TODO Retrieve this instance from the environment class once it has been created.
  97. ElementMappingRegistry elementMappingRegistry = new ElementMappingRegistry(userAgent);
  98. return new Handler(treeModel, userAgent, elementMappingRegistry);
  99. }
  100. private static class Handler extends DefaultHandler {
  101. private Map makers = new java.util.HashMap();
  102. private AreaTreeModel treeModel;
  103. private FOUserAgent userAgent;
  104. private ElementMappingRegistry elementMappingRegistry;
  105. private Attributes lastAttributes;
  106. private StringBuffer content = new StringBuffer();
  107. private PageViewport currentPageViewport;
  108. private Stack areaStack = new Stack();
  109. private boolean firstFlow;
  110. private Stack delegateStack = new Stack();
  111. private ContentHandler delegate;
  112. private DOMImplementation domImplementation;
  113. public Handler(AreaTreeModel treeModel, FOUserAgent userAgent,
  114. ElementMappingRegistry elementMappingRegistry) {
  115. this.treeModel = treeModel;
  116. this.userAgent = userAgent;
  117. this.elementMappingRegistry = elementMappingRegistry;
  118. makers.put("areaTree", new AreaTreeMaker());
  119. makers.put("page", new PageMaker());
  120. makers.put("pageSequence", new PageSequenceMaker());
  121. makers.put("pageViewport", new PageViewportMaker());
  122. makers.put("regionViewport", new RegionViewportMaker());
  123. makers.put("regionBefore", new RegionBeforeMaker());
  124. makers.put("regionAfter", new RegionAfterMaker());
  125. makers.put("regionStart", new RegionStartMaker());
  126. makers.put("regionEnd", new RegionEndMaker());
  127. makers.put("regionBody", new RegionBodyMaker());
  128. makers.put("flow", new FlowMaker());
  129. makers.put("mainReference", new MainReferenceMaker());
  130. makers.put("span", new SpanMaker());
  131. makers.put("footnote", new FootnoteMaker());
  132. makers.put("beforeFloat", new BeforeFloatMaker());
  133. makers.put("block", new BlockMaker());
  134. makers.put("lineArea", new LineAreaMaker());
  135. makers.put("inlineparent", new InlineParentMaker());
  136. makers.put("inlineblockparent", new InlineBlockParentMaker());
  137. makers.put("text", new TextMaker());
  138. makers.put("word", new WordMaker());
  139. makers.put("space", new SpaceMaker());
  140. makers.put("char", new CharMaker());
  141. makers.put("leader", new LeaderMaker());
  142. makers.put("viewport", new ViewportMaker());
  143. makers.put("image", new ImageMaker());
  144. makers.put("foreignObject", new ForeignObjectMaker());
  145. }
  146. private static Rectangle2D parseRect(String rect) {
  147. StringTokenizer tokenizer = new StringTokenizer(rect, " ");
  148. return new Rectangle2D.Double(
  149. Double.parseDouble(tokenizer.nextToken()),
  150. Double.parseDouble(tokenizer.nextToken()),
  151. Double.parseDouble(tokenizer.nextToken()),
  152. Double.parseDouble(tokenizer.nextToken()));
  153. }
  154. private Area findAreaType(Class clazz) {
  155. if (areaStack.size() > 0) {
  156. int pos = areaStack.size() - 1;
  157. Object obj = null;
  158. while (pos >= 0 && !(clazz.isInstance(obj = areaStack.get(pos)))) {
  159. pos--;
  160. }
  161. if (pos >= 0) {
  162. return (Area)obj;
  163. }
  164. }
  165. return null;
  166. }
  167. private RegionViewport getCurrentRegionViewport() {
  168. return (RegionViewport)findAreaType(RegionViewport.class);
  169. }
  170. private BodyRegion getCurrentBodyRegion() {
  171. return (BodyRegion)findAreaType(BodyRegion.class);
  172. }
  173. private BlockParent getCurrentBlockParent() {
  174. return (BlockParent)findAreaType(BlockParent.class);
  175. }
  176. private AbstractTextArea getCurrentText() {
  177. return (AbstractTextArea)findAreaType(AbstractTextArea.class);
  178. }
  179. private Viewport getCurrentViewport() {
  180. return (Viewport)findAreaType(Viewport.class);
  181. }
  182. /** @see org.xml.sax.helpers.DefaultHandler */
  183. public void startElement(String uri, String localName, String qName, Attributes attributes)
  184. throws SAXException {
  185. if (delegate != null) {
  186. delegateStack.push(qName);
  187. delegate.startElement(uri, localName, qName, attributes);
  188. } else if (domImplementation != null) {
  189. //domImplementation is set so we need to start a new DOM building sub-process
  190. TransformerHandler handler;
  191. try {
  192. handler = tFactory.newTransformerHandler();
  193. } catch (TransformerConfigurationException e) {
  194. throw new SAXException("Error creating a new TransformerHandler", e);
  195. }
  196. Document doc = domImplementation.createDocument(uri, qName, null);
  197. //It's easier to work with an empty document, so remove the root element
  198. doc.removeChild(doc.getDocumentElement());
  199. handler.setResult(new DOMResult(doc));
  200. Area parent = (Area)areaStack.peek();
  201. ((ForeignObject)parent).setDocument(doc);
  202. //activate delegate for nested foreign document
  203. domImplementation = null; //Not needed anymore now
  204. this.delegate = handler;
  205. delegateStack.push(qName);
  206. delegate.startDocument();
  207. delegate.startElement(uri, localName, qName, attributes);
  208. } else {
  209. lastAttributes = attributes;
  210. boolean handled = true;
  211. if ("".equals(uri)) {
  212. Maker maker = (Maker)makers.get(localName);
  213. if (maker != null) {
  214. maker.startElement(attributes);
  215. } else if ("extension-attachments".equals(localName)) {
  216. //TODO implement me
  217. } else {
  218. handled = false;
  219. }
  220. } else {
  221. ContentHandlerFactory factory
  222. = ContentHandlerFactoryRegistry.getInstance().getFactory(uri);
  223. if (factory != null) {
  224. delegate = factory.createContentHandler();
  225. delegateStack.push(qName);
  226. delegate.startDocument();
  227. delegate.startElement(uri, localName, qName, attributes);
  228. } else {
  229. handled = false;
  230. }
  231. }
  232. if (!handled) {
  233. if (uri == null || uri.length() == 0) {
  234. throw new SAXException("Unhandled element " + localName
  235. + " in namespace: " + uri);
  236. } else {
  237. log.warn("Unhandled element " + localName
  238. + " in namespace: " + uri);
  239. }
  240. }
  241. }
  242. }
  243. /** @see org.xml.sax.helpers.DefaultHandler */
  244. public void endElement(String uri, String localName, String qName) throws SAXException {
  245. if (delegate != null) {
  246. delegate.endElement(uri, localName, qName);
  247. delegateStack.pop();
  248. if (delegateStack.size() == 0) {
  249. delegate.endDocument();
  250. if (delegate instanceof ContentHandlerFactory.ObjectSource) {
  251. Object obj = ((ContentHandlerFactory.ObjectSource)delegate).getObject();
  252. handleExternallyGeneratedObject(obj);
  253. }
  254. delegate = null; //Sub-document is processed, return to normal processing
  255. }
  256. } else {
  257. if ("".equals(uri)) {
  258. Maker maker = (Maker)makers.get(localName);
  259. if (maker != null) {
  260. maker.endElement();
  261. }
  262. } else {
  263. //log.debug("Ignoring " + localName + " in namespace: " + uri);
  264. }
  265. content.setLength(0); //Reset text buffer (see characters())
  266. }
  267. }
  268. // ============== Maker classes for the area tree objects =============
  269. private static interface Maker {
  270. void startElement(Attributes attributes) throws SAXException;
  271. void endElement();
  272. }
  273. private abstract class AbstractMaker implements Maker {
  274. public void startElement(Attributes attributes) throws SAXException {
  275. //nop
  276. }
  277. public void endElement() {
  278. //nop
  279. }
  280. }
  281. private class AreaTreeMaker extends AbstractMaker {
  282. //no overrides
  283. }
  284. private class PageSequenceMaker extends AbstractMaker {
  285. public void startElement(Attributes attributes) {
  286. treeModel.startPageSequence(null);
  287. }
  288. }
  289. private class PageViewportMaker extends AbstractMaker {
  290. public void startElement(Attributes attributes) {
  291. if (currentPageViewport != null) {
  292. throw new IllegalStateException("currentPageViewport must be null");
  293. }
  294. Rectangle2D viewArea = parseRect(attributes.getValue("bounds"));
  295. int pageNumber = getAttributeAsInteger(attributes, "nr", -1);
  296. String pageNumberString = attributes.getValue("formatted-nr");
  297. String pageMaster = attributes.getValue("simple-page-master-name");
  298. boolean blank = getAttributeAsBoolean(attributes, "blank", false);
  299. currentPageViewport = new PageViewport(viewArea,
  300. pageNumber, pageNumberString,
  301. pageMaster, blank);
  302. }
  303. }
  304. private class PageMaker extends AbstractMaker {
  305. public void startElement(Attributes attributes) {
  306. Page p = new Page();
  307. currentPageViewport.setPage(p);
  308. }
  309. public void endElement() {
  310. treeModel.addPage(currentPageViewport);
  311. currentPageViewport = null;
  312. }
  313. }
  314. private class RegionViewportMaker extends AbstractMaker {
  315. public void startElement(Attributes attributes) {
  316. RegionViewport rv = getCurrentRegionViewport();
  317. if (rv != null) {
  318. throw new IllegalStateException("Current RegionViewport must be null");
  319. }
  320. Rectangle2D viewArea = parseRect(attributes.getValue("rect"));
  321. rv = new RegionViewport(viewArea);
  322. rv.setClip(getAttributeAsBoolean(attributes, "clipped", false));
  323. setAreaAttributes(attributes, rv);
  324. setTraits(attributes, rv, SUBSET_COMMON);
  325. setTraits(attributes, rv, SUBSET_BOX);
  326. setTraits(attributes, rv, SUBSET_COLOR);
  327. areaStack.push(rv);
  328. }
  329. public void endElement() {
  330. assertObjectOfClass(areaStack.pop(), RegionViewport.class);
  331. }
  332. }
  333. private class RegionBeforeMaker extends AbstractMaker {
  334. public void startElement(Attributes attributes) {
  335. pushNewRegionReference(attributes, Constants.FO_REGION_BEFORE);
  336. }
  337. public void endElement() {
  338. assertObjectOfClass(areaStack.pop(), RegionReference.class);
  339. }
  340. }
  341. private class RegionAfterMaker extends AbstractMaker {
  342. public void startElement(Attributes attributes) {
  343. pushNewRegionReference(attributes, Constants.FO_REGION_AFTER);
  344. }
  345. public void endElement() {
  346. assertObjectOfClass(areaStack.pop(), RegionReference.class);
  347. }
  348. }
  349. private class RegionStartMaker extends AbstractMaker {
  350. public void startElement(Attributes attributes) {
  351. pushNewRegionReference(attributes, Constants.FO_REGION_START);
  352. }
  353. public void endElement() {
  354. assertObjectOfClass(areaStack.pop(), RegionReference.class);
  355. }
  356. }
  357. private class RegionEndMaker extends AbstractMaker {
  358. public void startElement(Attributes attributes) {
  359. pushNewRegionReference(attributes, Constants.FO_REGION_END);
  360. }
  361. public void endElement() {
  362. assertObjectOfClass(areaStack.pop(), RegionReference.class);
  363. }
  364. }
  365. private class RegionBodyMaker extends AbstractMaker {
  366. public void startElement(Attributes attributes) {
  367. BodyRegion body = getCurrentBodyRegion();
  368. if (body != null) {
  369. throw new IllegalStateException("Current BodyRegion must be null");
  370. }
  371. String regionName = attributes.getValue("name");
  372. int columnCount = getAttributeAsInteger(attributes, "columnCount", 1);
  373. int columnGap = getAttributeAsInteger(attributes, "columnGap", 0);
  374. RegionViewport rv = getCurrentRegionViewport();
  375. body = new BodyRegion(Constants.FO_REGION_BODY,
  376. regionName, rv, columnCount, columnGap);
  377. body.setCTM(getAttributeAsCTM(attributes, "ctm"));
  378. setAreaAttributes(attributes, body);
  379. rv.setRegionReference(body);
  380. currentPageViewport.getPage().setRegionViewport(
  381. Constants.FO_REGION_BODY, rv);
  382. areaStack.push(body);
  383. }
  384. public void endElement() {
  385. assertObjectOfClass(areaStack.pop(), BodyRegion.class);
  386. }
  387. }
  388. private class FlowMaker extends AbstractMaker {
  389. public void startElement(Attributes attributes) {
  390. BodyRegion body = getCurrentBodyRegion();
  391. if (!firstFlow) {
  392. body.getMainReference().getCurrentSpan().moveToNextFlow();
  393. } else {
  394. firstFlow = false;
  395. }
  396. NormalFlow flow = body.getMainReference().getCurrentSpan().getCurrentFlow();
  397. setAreaAttributes(attributes, flow);
  398. areaStack.push(flow);
  399. }
  400. public void endElement() {
  401. assertObjectOfClass(areaStack.pop(), NormalFlow.class);
  402. }
  403. }
  404. private class MainReferenceMaker extends AbstractMaker {
  405. public void startElement(Attributes attributes) {
  406. //mainReference is created by the BodyRegion
  407. setAreaAttributes(attributes, getCurrentBodyRegion().getMainReference());
  408. }
  409. }
  410. private class SpanMaker extends AbstractMaker {
  411. public void startElement(Attributes attributes) {
  412. int ipd = getAttributeAsInteger(attributes, "ipd", 0);
  413. int columnCount = getAttributeAsInteger(attributes, "columnCount", 1);
  414. BodyRegion body = getCurrentBodyRegion();
  415. Span span = new Span(columnCount,
  416. body.getColumnGap(), ipd);
  417. setAreaAttributes(attributes, span);
  418. body.getMainReference().getSpans().add(span);
  419. firstFlow = true;
  420. }
  421. }
  422. private class FootnoteMaker extends AbstractMaker {
  423. public void startElement(Attributes attributes) {
  424. areaStack.push(getCurrentBodyRegion().getFootnote());
  425. }
  426. public void endElement() {
  427. assertObjectOfClass(areaStack.pop(), Footnote.class);
  428. }
  429. }
  430. private class BeforeFloatMaker extends AbstractMaker {
  431. public void startElement(Attributes attributes) {
  432. areaStack.push(getCurrentBodyRegion().getBeforeFloat());
  433. }
  434. public void endElement() {
  435. assertObjectOfClass(areaStack.pop(), BeforeFloat.class);
  436. }
  437. }
  438. private class BlockMaker extends AbstractMaker {
  439. public void startElement(Attributes attributes) {
  440. boolean isViewport = getAttributeAsBoolean(attributes,
  441. "is-viewport-area", false);
  442. Block block;
  443. if (isViewport) {
  444. BlockViewport bv = new BlockViewport();
  445. bv.setClip(getAttributeAsBoolean(attributes, "clipped", false));
  446. bv.setCTM(getAttributeAsCTM(attributes, "ctm"));
  447. if (bv.getPositioning() != BlockViewport.RELATIVE) {
  448. bv.setXOffset(
  449. getAttributeAsInteger(attributes, "left-position", 0));
  450. bv.setYOffset(
  451. getAttributeAsInteger(attributes, "top-position", 0));
  452. }
  453. block = bv;
  454. } else {
  455. block = new Block();
  456. }
  457. String positioning = attributes.getValue("positioning");
  458. if ("absolute".equalsIgnoreCase(positioning)) {
  459. block.setPositioning(Block.ABSOLUTE);
  460. } else if ("fixed".equalsIgnoreCase(positioning)) {
  461. block.setPositioning(Block.FIXED);
  462. } else if ("relative".equalsIgnoreCase(positioning)) {
  463. block.setPositioning(Block.RELATIVE);
  464. } else {
  465. block.setPositioning(Block.STACK);
  466. }
  467. if (attributes.getValue("left-offset") != null) {
  468. block.setXOffset(getAttributeAsInteger(attributes, "left-offset", 0));
  469. }
  470. if (attributes.getValue("top-offset") != null) {
  471. block.setYOffset(getAttributeAsInteger(attributes, "top-offset", 0));
  472. }
  473. setAreaAttributes(attributes, block);
  474. setTraits(attributes, block, SUBSET_COMMON);
  475. setTraits(attributes, block, SUBSET_BOX);
  476. setTraits(attributes, block, SUBSET_COLOR);
  477. Area parent = (Area)areaStack.peek();
  478. //BlockParent parent = getCurrentBlockParent();
  479. parent.addChildArea(block);
  480. areaStack.push(block);
  481. }
  482. public void endElement() {
  483. assertObjectOfClass(areaStack.pop(), Block.class);
  484. }
  485. }
  486. private class LineAreaMaker extends AbstractMaker {
  487. public void startElement(Attributes attributes) {
  488. LineArea line = new LineArea();
  489. setAreaAttributes(attributes, line);
  490. setTraits(attributes, line, SUBSET_COMMON);
  491. setTraits(attributes, line, SUBSET_BOX);
  492. setTraits(attributes, line, SUBSET_COLOR);
  493. BlockParent parent = getCurrentBlockParent();
  494. parent.addChildArea(line);
  495. areaStack.push(line);
  496. }
  497. public void endElement() {
  498. assertObjectOfClass(areaStack.pop(), LineArea.class);
  499. }
  500. }
  501. private class InlineParentMaker extends AbstractMaker {
  502. public void startElement(Attributes attributes) {
  503. InlineParent ip = new InlineParent();
  504. ip.setOffset(getAttributeAsInteger(attributes, "offset", 0));
  505. setAreaAttributes(attributes, ip);
  506. setTraits(attributes, ip, SUBSET_COMMON);
  507. setTraits(attributes, ip, SUBSET_BOX);
  508. setTraits(attributes, ip, SUBSET_COLOR);
  509. setTraits(attributes, ip, SUBSET_LINK);
  510. Area parent = (Area)areaStack.peek();
  511. parent.addChildArea(ip);
  512. areaStack.push(ip);
  513. }
  514. public void endElement() {
  515. assertObjectOfClass(areaStack.pop(), InlineParent.class);
  516. }
  517. }
  518. private class InlineBlockParentMaker extends AbstractMaker {
  519. public void startElement(Attributes attributes) {
  520. InlineBlockParent ibp = new InlineBlockParent();
  521. ibp.setOffset(getAttributeAsInteger(attributes, "offset", 0));
  522. setAreaAttributes(attributes, ibp);
  523. setTraits(attributes, ibp, SUBSET_COMMON);
  524. setTraits(attributes, ibp, SUBSET_BOX);
  525. setTraits(attributes, ibp, SUBSET_COLOR);
  526. Area parent = (Area)areaStack.peek();
  527. parent.addChildArea(ibp);
  528. areaStack.push(ibp);
  529. }
  530. public void endElement() {
  531. assertObjectOfClass(areaStack.pop(), InlineBlockParent.class);
  532. }
  533. }
  534. private class TextMaker extends AbstractMaker {
  535. public void startElement(Attributes attributes) {
  536. if (getCurrentText() != null) {
  537. throw new IllegalStateException("Current Text must be null");
  538. }
  539. TextArea text = new TextArea();
  540. setAreaAttributes(attributes, text);
  541. setTraits(attributes, text, SUBSET_COMMON);
  542. setTraits(attributes, text, SUBSET_BOX);
  543. setTraits(attributes, text, SUBSET_COLOR);
  544. setTraits(attributes, text, SUBSET_FONT);
  545. text.setBaselineOffset(getAttributeAsInteger(attributes, "baseline", 0));
  546. text.setOffset(getAttributeAsInteger(attributes, "offset", 0));
  547. text.setTextLetterSpaceAdjust(getAttributeAsInteger(attributes,
  548. "tlsadjust", 0));
  549. text.setTextWordSpaceAdjust(getAttributeAsInteger(attributes,
  550. "twsadjust", 0));
  551. Area parent = (Area)areaStack.peek();
  552. parent.addChildArea(text);
  553. areaStack.push(text);
  554. }
  555. public void endElement() {
  556. assertObjectOfClass(areaStack.pop(), TextArea.class);
  557. }
  558. }
  559. private class WordMaker extends AbstractMaker {
  560. public void endElement() {
  561. int offset = getAttributeAsInteger(lastAttributes, "offset", 0);
  562. String txt = content.toString();
  563. WordArea word = new WordArea(txt, offset);
  564. AbstractTextArea text = getCurrentText();
  565. word.setParentArea(text);
  566. text.addChildArea(word);
  567. }
  568. }
  569. private class SpaceMaker extends AbstractMaker {
  570. public void endElement() {
  571. int offset = getAttributeAsInteger(lastAttributes, "offset", 0);
  572. String txt = content.toString();
  573. //TODO the isAdjustable parameter is currently not used/implemented
  574. if (txt.length() > 0) {
  575. SpaceArea space = new SpaceArea(txt.charAt(0), offset, false);
  576. AbstractTextArea text = getCurrentText();
  577. space.setParentArea(text);
  578. text.addChildArea(space);
  579. } else {
  580. Space space = new Space();
  581. setAreaAttributes(lastAttributes, space);
  582. setTraits(lastAttributes, space, SUBSET_COMMON);
  583. setTraits(lastAttributes, space, SUBSET_BOX);
  584. setTraits(lastAttributes, space, SUBSET_COLOR);
  585. space.setOffset(offset);
  586. Area parent = (Area)areaStack.peek();
  587. parent.addChildArea(space);
  588. }
  589. }
  590. }
  591. private class CharMaker extends AbstractMaker {
  592. public void endElement() {
  593. String txt = content.toString();
  594. Character ch = new Character(txt.charAt(0));
  595. setAreaAttributes(lastAttributes, ch);
  596. setTraits(lastAttributes, ch, SUBSET_COMMON);
  597. setTraits(lastAttributes, ch, SUBSET_BOX);
  598. setTraits(lastAttributes, ch, SUBSET_COLOR);
  599. setTraits(lastAttributes, ch, SUBSET_FONT);
  600. ch.setOffset(getAttributeAsInteger(lastAttributes, "offset", 0));
  601. ch.setBaselineOffset(getAttributeAsInteger(lastAttributes, "baseline", 0));
  602. Area parent = (Area)areaStack.peek();
  603. parent.addChildArea(ch);
  604. }
  605. }
  606. private class LeaderMaker extends AbstractMaker {
  607. public void startElement(Attributes attributes) {
  608. Leader leader = new Leader();
  609. setAreaAttributes(attributes, leader);
  610. setTraits(attributes, leader, SUBSET_COMMON);
  611. setTraits(attributes, leader, SUBSET_BOX);
  612. setTraits(attributes, leader, SUBSET_COLOR);
  613. setTraits(attributes, leader, SUBSET_FONT);
  614. leader.setOffset(getAttributeAsInteger(attributes, "offset", 0));
  615. String ruleStyle = attributes.getValue("ruleStyle");
  616. if (ruleStyle != null) {
  617. leader.setRuleStyle(ruleStyle);
  618. }
  619. leader.setRuleThickness(
  620. getAttributeAsInteger(attributes, "ruleThickness", 0));
  621. Area parent = (Area)areaStack.peek();
  622. parent.addChildArea(leader);
  623. }
  624. public void endElement() {
  625. }
  626. }
  627. private class ViewportMaker extends AbstractMaker {
  628. public void startElement(Attributes attributes) {
  629. Viewport viewport = new Viewport(null);
  630. setAreaAttributes(attributes, viewport);
  631. setTraits(attributes, viewport, SUBSET_COMMON);
  632. setTraits(attributes, viewport, SUBSET_BOX);
  633. setTraits(attributes, viewport, SUBSET_COLOR);
  634. viewport.setContentPosition(getAttributeAsRectangle2D(attributes, "pos"));
  635. viewport.setClip(getAttributeAsBoolean(attributes, "clip", false));
  636. viewport.setOffset(getAttributeAsInteger(attributes, "offset", 0));
  637. Area parent = (Area)areaStack.peek();
  638. parent.addChildArea(viewport);
  639. areaStack.push(viewport);
  640. }
  641. public void endElement() {
  642. assertObjectOfClass(areaStack.pop(), Viewport.class);
  643. }
  644. }
  645. private class ImageMaker extends AbstractMaker {
  646. public void startElement(Attributes attributes) {
  647. String url = attributes.getValue("url");
  648. Image image = new Image(url);
  649. setAreaAttributes(attributes, image);
  650. setTraits(attributes, image, SUBSET_COMMON);
  651. getCurrentViewport().setContent(image);
  652. }
  653. }
  654. private class ForeignObjectMaker extends AbstractMaker {
  655. public void startElement(Attributes attributes) throws SAXException {
  656. String ns = attributes.getValue("ns");
  657. domImplementation
  658. = elementMappingRegistry.getDOMImplementationForNamespace(ns);
  659. if (domImplementation == null) {
  660. throw new SAXException("No DOMImplementation could be"
  661. + " identified to handle namespace: " + ns);
  662. }
  663. ForeignObject foreign = new ForeignObject(ns);
  664. setAreaAttributes(attributes, foreign);
  665. setTraits(attributes, foreign, SUBSET_COMMON);
  666. getCurrentViewport().setContent(foreign);
  667. areaStack.push(foreign);
  668. }
  669. public void endElement() {
  670. assertObjectOfClass(areaStack.pop(), ForeignObject.class);
  671. }
  672. }
  673. /*
  674. private class ?Maker extends AbstractMaker {
  675. public void startElement(Attributes attributes) {
  676. }
  677. public void endElement() {
  678. }
  679. }
  680. */
  681. // ====================================================================
  682. private void pushNewRegionReference(Attributes attributes, int side) {
  683. String regionName = attributes.getValue("name");
  684. RegionViewport rv = getCurrentRegionViewport();
  685. RegionReference reg = new RegionReference(side,
  686. regionName, rv);
  687. reg.setCTM(getAttributeAsCTM(attributes, "ctm"));
  688. setAreaAttributes(attributes, reg);
  689. rv.setRegionReference(reg);
  690. currentPageViewport.getPage().setRegionViewport(
  691. side, rv);
  692. areaStack.push(reg);
  693. }
  694. private void assertObjectOfClass(Object obj, Class clazz) {
  695. if (!clazz.isInstance(obj)) {
  696. throw new IllegalStateException("Object is not an instance of "
  697. + clazz.getName() + " but of " + obj.getClass().getName());
  698. }
  699. }
  700. /**
  701. * Handles objects created by "sub-parsers" that implement the ObjectSource interface.
  702. * An example of object handled here are ExtensionAttachments.
  703. * @param obj the Object to be handled.
  704. */
  705. protected void handleExternallyGeneratedObject(Object obj) {
  706. if (areaStack.size() == 0 && obj instanceof ExtensionAttachment) {
  707. ExtensionAttachment attachment = (ExtensionAttachment)obj;
  708. if (this.currentPageViewport == null) {
  709. this.treeModel.handleOffDocumentItem(
  710. new OffDocumentExtensionAttachment(attachment));
  711. } else {
  712. this.currentPageViewport.addExtensionAttachment(attachment);
  713. }
  714. } else {
  715. log.warn("Don't know how to handle externally generated object: " + obj);
  716. }
  717. }
  718. private void setAreaAttributes(Attributes attributes, Area area) {
  719. area.setIPD(Integer.parseInt(attributes.getValue("ipd")));
  720. area.setBPD(Integer.parseInt(attributes.getValue("bpd")));
  721. }
  722. private static final Object[] SUBSET_COMMON = new Object[] {
  723. Trait.PROD_ID};
  724. private static final Object[] SUBSET_LINK = new Object[] {
  725. Trait.INTERNAL_LINK, Trait.EXTERNAL_LINK};
  726. private static final Object[] SUBSET_COLOR = new Object[] {
  727. Trait.BACKGROUND, Trait.COLOR};
  728. private static final Object[] SUBSET_FONT = new Object[] {
  729. Trait.FONT, Trait.FONT_SIZE, Trait.BLINK,
  730. Trait.OVERLINE, Trait.OVERLINE_COLOR,
  731. Trait.LINETHROUGH, Trait.LINETHROUGH_COLOR,
  732. Trait.UNDERLINE, Trait.UNDERLINE_COLOR};
  733. private static final Object[] SUBSET_BOX = new Object[] {
  734. Trait.BORDER_BEFORE, Trait.BORDER_AFTER, Trait.BORDER_START, Trait.BORDER_END,
  735. Trait.SPACE_BEFORE, Trait.SPACE_AFTER, Trait.SPACE_START, Trait.SPACE_END,
  736. Trait.PADDING_BEFORE, Trait.PADDING_AFTER, Trait.PADDING_START, Trait.PADDING_END,
  737. Trait.START_INDENT, Trait.END_INDENT,
  738. Trait.IS_REFERENCE_AREA, Trait.IS_VIEWPORT_AREA};
  739. private void setTraits(Attributes attributes, Area area, Object[] traitSubset) {
  740. for (int i = 0, c = traitSubset.length; i < c; i++) {
  741. Object trait = traitSubset[i];
  742. String traitName = Trait.getTraitName(trait);
  743. String value = attributes.getValue(traitName);
  744. if (value != null) {
  745. Class cl = Trait.getTraitClass(trait);
  746. if (cl == Integer.class) {
  747. //if (value != null) {
  748. area.addTrait(trait, new Integer(value));
  749. //}
  750. } else if (cl == Boolean.class) {
  751. //String value = attributes.getValue(Trait.getTraitName(trait));
  752. //if (value != null) {
  753. area.addTrait(trait, Boolean.valueOf(value));
  754. //}
  755. } else if (cl == String.class) {
  756. //String value = attributes.getValue(Trait.getTraitName(trait));
  757. //if (value != null) {
  758. area.addTrait(trait, value);
  759. //}
  760. } else if (cl == Color.class) {
  761. //String value = attributes.getValue(Trait.getTraitName(trait));
  762. //if (value != null) {
  763. area.addTrait(trait, Color.valueOf(value));
  764. //}
  765. } else if (cl == Background.class) {
  766. //String value = attributes.getValue(Trait.getTraitName(trait));
  767. //if (value != null) {
  768. Background bkg = new Background();
  769. Color col = Color.valueOf(attributes.getValue("bkg-color"));
  770. if (col != null) {
  771. bkg.setColor(col);
  772. }
  773. String url = attributes.getValue("bkg-img");
  774. if (url != null) {
  775. bkg.setURL(url);
  776. ImageFactory fact = ImageFactory.getInstance();
  777. FopImage img = fact.getImage(url, userAgent);
  778. if (img == null) {
  779. log.error("Background image not available: " + url);
  780. } else {
  781. // load dimensions
  782. if (!img.load(FopImage.DIMENSIONS)) {
  783. log.error("Cannot read background image dimensions: "
  784. + url);
  785. }
  786. }
  787. bkg.setFopImage(img);
  788. String repeat = attributes.getValue("bkg-repeat");
  789. if (repeat != null) {
  790. bkg.setRepeat(repeat);
  791. }
  792. bkg.setHoriz(getAttributeAsInteger(attributes,
  793. "bkg-horz-offset", 0));
  794. bkg.setVertical(getAttributeAsInteger(attributes,
  795. "bkg-vert-offset", 0));
  796. }
  797. area.addTrait(trait, bkg);
  798. //}
  799. } else if (cl == BorderProps.class) {
  800. //String value = attributes.getValue(Trait.getTraitName(trait));
  801. //if (value != null) {
  802. area.addTrait(trait, BorderProps.valueOf(value));
  803. //}
  804. }
  805. } else {
  806. //Class cl = Trait.getTraitClass(trait);
  807. if (trait == Trait.FONT) {
  808. String fontName = attributes.getValue("font-name");
  809. if (fontName != null) {
  810. String fontStyle = attributes.getValue("font-style");
  811. int fontWeight = getAttributeAsInteger(
  812. attributes, "font-weight", Font.NORMAL);
  813. area.addTrait(trait,
  814. new FontTriplet(fontName, fontStyle, fontWeight));
  815. }
  816. }
  817. }
  818. }
  819. }
  820. private boolean getAttributeAsBoolean(Attributes attributes, String name,
  821. boolean defaultValue) {
  822. String s = attributes.getValue(name);
  823. if (s == null) {
  824. return defaultValue;
  825. } else {
  826. return Boolean.valueOf(s).booleanValue();
  827. }
  828. }
  829. private int getAttributeAsInteger(Attributes attributes, String name,
  830. int defaultValue) {
  831. String s = attributes.getValue(name);
  832. if (s == null) {
  833. return defaultValue;
  834. } else {
  835. return Integer.parseInt(s);
  836. }
  837. }
  838. private CTM getAttributeAsCTM(Attributes attributes, String name) {
  839. String s = attributes.getValue(name).trim();
  840. if (s.startsWith("[") && s.endsWith("]")) {
  841. s = s.substring(1, s.length() - 1);
  842. StringTokenizer tokenizer = new StringTokenizer(s, " ");
  843. double[] values = new double[] {
  844. Double.parseDouble(tokenizer.nextToken()),
  845. Double.parseDouble(tokenizer.nextToken()),
  846. Double.parseDouble(tokenizer.nextToken()),
  847. Double.parseDouble(tokenizer.nextToken()),
  848. Double.parseDouble(tokenizer.nextToken()),
  849. Double.parseDouble(tokenizer.nextToken())};
  850. return new CTM(values[0], values[1], values[2], values[3], values[4], values[5]);
  851. } else {
  852. throw new IllegalArgumentException("CTM must be surrounded by square brackets");
  853. }
  854. }
  855. private Rectangle2D getAttributeAsRectangle2D(Attributes attributes, String name) {
  856. String s = attributes.getValue(name).trim();
  857. StringTokenizer tokenizer = new StringTokenizer(s, " ");
  858. double[] values = new double[] {
  859. Double.parseDouble(tokenizer.nextToken()),
  860. Double.parseDouble(tokenizer.nextToken()),
  861. Double.parseDouble(tokenizer.nextToken()),
  862. Double.parseDouble(tokenizer.nextToken())};
  863. return new Rectangle2D.Double(values[0], values[1], values[2], values[3]);
  864. }
  865. /** @see org.xml.sax.ContentHandler#characters(char[], int, int) */
  866. public void characters(char[] ch, int start, int length) throws SAXException {
  867. if (delegate != null) {
  868. delegate.characters(ch, start, length);
  869. } else {
  870. content.append(ch, start, length);
  871. }
  872. }
  873. }
  874. }