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

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