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

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