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.

XMLWhiteSpaceHandler.java 17KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  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.fo;
  19. import java.util.List;
  20. import java.util.Stack;
  21. import org.apache.fop.fo.flow.Block;
  22. import org.apache.fop.fo.flow.Float;
  23. import org.apache.fop.util.CharUtilities;
  24. /**
  25. * Class encapsulating the functionality for white-space-handling
  26. * during refinement stage.
  27. * The <code>handleWhiteSpace()</code> methods are called during
  28. * FOTree-building and marker-cloning:
  29. * <br>
  30. * <ul>
  31. * <li> from <code>FObjMixed.addChildNode()</code></li>
  32. * <li> from <code>FObjMixed.endOfNode()</code></li>
  33. * <li> from <code>FObjMixed.handleWhiteSpaceFor()</code></li>
  34. * </ul>
  35. * <br>
  36. * Each time one of the variants is called, white-space is handled
  37. * for all <code>FOText</code> or <code>Character</code> nodes that
  38. * were added:
  39. * <br>
  40. * <ul>
  41. * <li> either prior to <code>newChild</code> (and after the previous
  42. * non-text child node)</li>
  43. * <li> or, if <code>newChild</code> is <code>null</code>,
  44. * after the previous non-text child</li>
  45. * </ul>
  46. * <br>
  47. * The iteration always starts at <code>firstTextNode</code>,
  48. * goes on until the last text-node is reached, and deals only
  49. * with <code>FOText</code> or <code>Character</code> nodes.
  50. * <br>
  51. * <em>Note</em>: if the method is called from an inline's endOfNode(),
  52. * there is too little context to decide whether trailing
  53. * white-space may be removed, so the pending inline is stored
  54. * in a List, together with an iterator for which the next()
  55. * method returns the first in the trailing sequence of white-
  56. * space characters. This List is processed again at the end
  57. * of the ancestor block.
  58. */
  59. public class XMLWhiteSpaceHandler {
  60. /** True if we are in a run of white space */
  61. private boolean inWhiteSpace;
  62. /** True if the last char was a linefeed */
  63. private boolean afterLinefeed = true;
  64. /** Counter, increased every time a non-white-space is encountered */
  65. private int nonWhiteSpaceCount;
  66. private int linefeedTreatment;
  67. private int whiteSpaceTreatment;
  68. private int whiteSpaceCollapse;
  69. private boolean endOfBlock;
  70. private boolean nextChildIsBlockLevel;
  71. private RecursiveCharIterator charIter;
  72. private List pendingInlines;
  73. private Stack nestedBlockStack = new java.util.Stack<Block>();
  74. private CharIterator firstWhiteSpaceInSeq;
  75. /**
  76. * Handle white-space for the fo that is passed in, starting at
  77. * firstTextNode
  78. * @param fo the FO for which to handle white-space
  79. * @param firstTextNode the node at which to start
  80. * @param nextChild the node that will be added to the list
  81. * after firstTextNode
  82. */
  83. public void handleWhiteSpace(FObjMixed fo,
  84. FONode firstTextNode,
  85. FONode nextChild) {
  86. Block currentBlock = null;
  87. int foId = fo.getNameId();
  88. /* set the current block */
  89. switch (foId) {
  90. case Constants.FO_BLOCK:
  91. currentBlock = (Block) fo;
  92. if (nestedBlockStack.empty() || fo != nestedBlockStack.peek()) {
  93. if (nextChild != null) {
  94. /* if already in a block, push the current block
  95. * onto the stack of nested blocks
  96. */
  97. nestedBlockStack.push(currentBlock);
  98. }
  99. } else {
  100. if (nextChild == null) {
  101. nestedBlockStack.pop();
  102. }
  103. }
  104. break;
  105. case Constants.FO_RETRIEVE_MARKER:
  106. /* look for the nearest block ancestor, if any */
  107. FONode ancestor = fo;
  108. do {
  109. ancestor = ancestor.getParent();
  110. } while (ancestor.getNameId() != Constants.FO_BLOCK
  111. && ancestor.getNameId() != Constants.FO_STATIC_CONTENT);
  112. if (ancestor.getNameId() == Constants.FO_BLOCK) {
  113. currentBlock = (Block) ancestor;
  114. nestedBlockStack.push(currentBlock);
  115. }
  116. break;
  117. default:
  118. if (!nestedBlockStack.empty()) {
  119. currentBlock = (Block) nestedBlockStack.peek();
  120. }
  121. }
  122. if (currentBlock != null) {
  123. linefeedTreatment = currentBlock.getLinefeedTreatment();
  124. whiteSpaceCollapse = currentBlock.getWhitespaceCollapse();
  125. whiteSpaceTreatment = currentBlock.getWhitespaceTreatment();
  126. } else {
  127. linefeedTreatment = Constants.EN_TREAT_AS_SPACE;
  128. whiteSpaceCollapse = Constants.EN_TRUE;
  129. whiteSpaceTreatment = Constants.EN_IGNORE_IF_SURROUNDING_LINEFEED;
  130. }
  131. endOfBlock = (nextChild == null && fo == currentBlock);
  132. if (firstTextNode == null) {
  133. //no text means no white-space to handle; return early
  134. afterLinefeed = (fo == currentBlock && fo.firstChild == null);
  135. nonWhiteSpaceCount = 0;
  136. if (endOfBlock) {
  137. handlePendingInlines();
  138. }
  139. return;
  140. }
  141. charIter = new RecursiveCharIterator(fo, firstTextNode);
  142. inWhiteSpace = false;
  143. if (firstTextNode.siblings != null && firstTextNode.siblings[0] != null
  144. && firstTextNode.siblings[0].getNameId() == Constants.FO_FLOAT) {
  145. inWhiteSpace = ((Float) firstTextNode.siblings[0]).getInWhiteSpace();
  146. }
  147. if (fo == currentBlock
  148. || currentBlock == null
  149. || (foId == Constants.FO_RETRIEVE_MARKER
  150. && fo.getParent() == currentBlock)) {
  151. if (firstTextNode == fo.firstChild) {
  152. afterLinefeed = true;
  153. } else {
  154. int previousChildId = firstTextNode.siblings[0].getNameId();
  155. afterLinefeed = (previousChildId == Constants.FO_BLOCK
  156. || previousChildId == Constants.FO_TABLE_AND_CAPTION
  157. || previousChildId == Constants.FO_TABLE
  158. || previousChildId == Constants.FO_LIST_BLOCK
  159. || previousChildId == Constants.FO_BLOCK_CONTAINER);
  160. }
  161. }
  162. if (foId == Constants.FO_WRAPPER) {
  163. FONode parent = fo.parent;
  164. int parentId = parent.getNameId();
  165. while (parentId == Constants.FO_WRAPPER) {
  166. parent = parent.parent;
  167. parentId = parent.getNameId();
  168. }
  169. if (parentId == Constants.FO_FLOW
  170. || parentId == Constants.FO_STATIC_CONTENT
  171. || parentId == Constants.FO_BLOCK_CONTAINER
  172. || parentId == Constants.FO_TABLE_CELL) {
  173. endOfBlock = (nextChild == null);
  174. }
  175. }
  176. if (nextChild != null) {
  177. int nextChildId = nextChild.getNameId();
  178. nextChildIsBlockLevel = (
  179. nextChildId == Constants.FO_BLOCK
  180. || nextChildId == Constants.FO_TABLE_AND_CAPTION
  181. || nextChildId == Constants.FO_TABLE
  182. || nextChildId == Constants.FO_LIST_BLOCK
  183. || nextChildId == Constants.FO_BLOCK_CONTAINER);
  184. } else {
  185. nextChildIsBlockLevel = false;
  186. }
  187. handleWhiteSpace();
  188. if (fo == currentBlock
  189. && (endOfBlock || nextChildIsBlockLevel)) {
  190. handlePendingInlines();
  191. }
  192. if (nextChild == null) {
  193. if (fo != currentBlock) {
  194. /* current FO is not a block, and is about to end */
  195. if (nonWhiteSpaceCount > 0 && pendingInlines != null) {
  196. /* there is non-white-space text between the pending
  197. * inline(s) and the end of the non-block node;
  198. * clear list of pending inlines */
  199. pendingInlines.clear();
  200. }
  201. if (inWhiteSpace) {
  202. /* means there is at least one trailing space in the
  203. inline FO that is about to end */
  204. addPendingInline();
  205. }
  206. } else {
  207. /* end of block: clear the references and pop the
  208. * nested block stack */
  209. if (!nestedBlockStack.empty()) {
  210. nestedBlockStack.pop();
  211. }
  212. charIter = null;
  213. firstWhiteSpaceInSeq = null;
  214. }
  215. }
  216. if (nextChild instanceof Float) {
  217. ((Float) nextChild).setInWhiteSpace(inWhiteSpace);
  218. }
  219. }
  220. /**
  221. * Reset the handler, release all references
  222. */
  223. protected final void reset() {
  224. if (pendingInlines != null) {
  225. pendingInlines.clear();
  226. }
  227. nestedBlockStack.clear();
  228. charIter = null;
  229. firstWhiteSpaceInSeq = null;
  230. }
  231. /**
  232. * Handle white-space for the fo that is passed in, starting at
  233. * firstTextNode (when a nested FO is encountered)
  234. * @param fo the FO for which to handle white-space
  235. * @param firstTextNode the node at which to start
  236. */
  237. public void handleWhiteSpace(FObjMixed fo, FONode firstTextNode) {
  238. handleWhiteSpace(fo, firstTextNode, null);
  239. }
  240. private void handleWhiteSpace() {
  241. EOLchecker lfCheck = new EOLchecker(charIter);
  242. nonWhiteSpaceCount = 0;
  243. while (charIter.hasNext()) {
  244. if (!inWhiteSpace) {
  245. firstWhiteSpaceInSeq = charIter.mark();
  246. }
  247. char currentChar = charIter.nextChar();
  248. int currentCharClass = CharUtilities.classOf(currentChar);
  249. if (currentCharClass == CharUtilities.LINEFEED
  250. && linefeedTreatment == Constants.EN_TREAT_AS_SPACE) {
  251. // if we have a linefeed and it is supposed to be treated
  252. // like a space, that's what we do and continue
  253. currentChar = '\u0020';
  254. charIter.replaceChar('\u0020');
  255. currentCharClass = CharUtilities.classOf(currentChar);
  256. }
  257. switch (CharUtilities.classOf(currentChar)) {
  258. case CharUtilities.XMLWHITESPACE:
  259. // Some kind of whitespace character, except linefeed.
  260. if (inWhiteSpace
  261. && whiteSpaceCollapse == Constants.EN_TRUE) {
  262. // We are in a run of whitespace and should collapse
  263. // Just delete the char
  264. charIter.remove();
  265. } else {
  266. // Do the white space treatment here
  267. boolean bIgnore = false;
  268. switch (whiteSpaceTreatment) {
  269. case Constants.EN_IGNORE:
  270. bIgnore = true;
  271. break;
  272. case Constants.EN_IGNORE_IF_BEFORE_LINEFEED:
  273. bIgnore = lfCheck.beforeLinefeed();
  274. break;
  275. case Constants.EN_IGNORE_IF_SURROUNDING_LINEFEED:
  276. bIgnore = afterLinefeed
  277. || lfCheck.beforeLinefeed();
  278. break;
  279. case Constants.EN_IGNORE_IF_AFTER_LINEFEED:
  280. bIgnore = afterLinefeed;
  281. break;
  282. case Constants.EN_PRESERVE:
  283. //nothing to do now, replacement takes place later
  284. break;
  285. default:
  286. //nop
  287. }
  288. // Handle ignore and replacement
  289. if (bIgnore) {
  290. charIter.remove();
  291. } else {
  292. // this is to retain a single space between words
  293. inWhiteSpace = true;
  294. if (currentChar != '\u0020') {
  295. charIter.replaceChar('\u0020');
  296. }
  297. }
  298. }
  299. break;
  300. case CharUtilities.LINEFEED:
  301. // A linefeed
  302. switch (linefeedTreatment) {
  303. case Constants.EN_IGNORE:
  304. charIter.remove();
  305. break;
  306. case Constants.EN_TREAT_AS_ZERO_WIDTH_SPACE:
  307. charIter.replaceChar(CharUtilities.ZERO_WIDTH_SPACE);
  308. inWhiteSpace = false;
  309. break;
  310. case Constants.EN_PRESERVE:
  311. lfCheck.reset();
  312. inWhiteSpace = false;
  313. afterLinefeed = true; // for following whitespace
  314. break;
  315. default:
  316. //nop
  317. }
  318. break;
  319. case CharUtilities.EOT:
  320. // A "boundary" objects such as non-character inline
  321. // or nested block object was encountered. (? can't happen)
  322. // If any whitespace run in progress, finish it.
  323. // FALL THROUGH
  324. default:
  325. // Any other character
  326. inWhiteSpace = false;
  327. afterLinefeed = false;
  328. nonWhiteSpaceCount++;
  329. lfCheck.reset();
  330. break;
  331. }
  332. }
  333. }
  334. private void addPendingInline() {
  335. if (pendingInlines == null) {
  336. pendingInlines = new java.util.ArrayList(5);
  337. }
  338. pendingInlines.add(new PendingInline(firstWhiteSpaceInSeq));
  339. }
  340. private void handlePendingInlines() {
  341. if (!(pendingInlines == null || pendingInlines.isEmpty())) {
  342. if (nonWhiteSpaceCount == 0) {
  343. /* handle white-space for all pending inlines*/
  344. PendingInline p;
  345. for (int i = pendingInlines.size(); --i >= 0;) {
  346. p = (PendingInline)pendingInlines.get(i);
  347. charIter = (RecursiveCharIterator)p.firstTrailingWhiteSpace;
  348. handleWhiteSpace();
  349. pendingInlines.remove(p);
  350. }
  351. } else {
  352. /* there is non-white-space text between the pending
  353. * inline(s) and the end of the block;
  354. * clear list of pending inlines */
  355. pendingInlines.clear();
  356. }
  357. }
  358. }
  359. /**
  360. * Helper class, used during white-space handling to look ahead, and
  361. * see if the next character is a linefeed (or if there will be
  362. * an equivalent effect during layout, i.e. end-of-block or
  363. * the following child is a block-level FO)
  364. */
  365. private class EOLchecker {
  366. private boolean nextIsEOL;
  367. private RecursiveCharIterator charIter;
  368. EOLchecker(CharIterator charIter) {
  369. this.charIter = (RecursiveCharIterator) charIter;
  370. }
  371. boolean beforeLinefeed() {
  372. if (!nextIsEOL) {
  373. CharIterator lfIter = charIter.mark();
  374. while (lfIter.hasNext()) {
  375. int charClass = CharUtilities.classOf(lfIter.nextChar());
  376. if (charClass == CharUtilities.LINEFEED) {
  377. if (linefeedTreatment == Constants.EN_PRESERVE) {
  378. nextIsEOL = true;
  379. return nextIsEOL;
  380. }
  381. } else if (charClass != CharUtilities.XMLWHITESPACE) {
  382. return nextIsEOL;
  383. }
  384. }
  385. // No more characters == end of text run
  386. // means EOL if there either is a nested block to be added,
  387. // or if this is the last text node in the current block
  388. nextIsEOL = nextChildIsBlockLevel || endOfBlock;
  389. }
  390. return nextIsEOL;
  391. }
  392. void reset() {
  393. nextIsEOL = false;
  394. }
  395. }
  396. /**
  397. * Helper class to store unfinished inline nodes together
  398. * with an iterator that starts at the first white-space
  399. * character in the sequence of trailing white-space
  400. */
  401. private class PendingInline {
  402. protected CharIterator firstTrailingWhiteSpace;
  403. PendingInline(CharIterator firstTrailingWhiteSpace) {
  404. this.firstTrailingWhiteSpace = firstTrailingWhiteSpace;
  405. }
  406. }
  407. }