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

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