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

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