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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  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;
  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. * Reset the handler, release all references
  214. */
  215. protected final void reset() {
  216. if (pendingInlines != null) {
  217. pendingInlines.clear();
  218. }
  219. nestedBlockStack.clear();
  220. charIter = null;
  221. firstWhiteSpaceInSeq = null;
  222. }
  223. /**
  224. * Handle white-space for the fo that is passed in, starting at
  225. * firstTextNode (when a nested FO is encountered)
  226. * @param fo the FO for which to handle white-space
  227. * @param firstTextNode the node at which to start
  228. */
  229. public void handleWhiteSpace(FObjMixed fo, FONode firstTextNode) {
  230. handleWhiteSpace(fo, firstTextNode, null);
  231. }
  232. private void handleWhiteSpace() {
  233. EOLchecker lfCheck = new EOLchecker(charIter);
  234. nonWhiteSpaceCount = 0;
  235. while (charIter.hasNext()) {
  236. if (!inWhiteSpace) {
  237. firstWhiteSpaceInSeq = charIter.mark();
  238. }
  239. char currentChar = charIter.nextChar();
  240. int currentCharClass = CharUtilities.classOf(currentChar);
  241. if (currentCharClass == CharUtilities.LINEFEED
  242. && linefeedTreatment == Constants.EN_TREAT_AS_SPACE) {
  243. // if we have a linefeed and it is supposed to be treated
  244. // like a space, that's what we do and continue
  245. currentChar = '\u0020';
  246. charIter.replaceChar('\u0020');
  247. currentCharClass = CharUtilities.classOf(currentChar);
  248. }
  249. switch (CharUtilities.classOf(currentChar)) {
  250. case CharUtilities.XMLWHITESPACE:
  251. // Some kind of whitespace character, except linefeed.
  252. if (inWhiteSpace
  253. && whiteSpaceCollapse == Constants.EN_TRUE) {
  254. // We are in a run of whitespace and should collapse
  255. // Just delete the char
  256. charIter.remove();
  257. } else {
  258. // Do the white space treatment here
  259. boolean bIgnore = false;
  260. switch (whiteSpaceTreatment) {
  261. case Constants.EN_IGNORE:
  262. bIgnore = true;
  263. break;
  264. case Constants.EN_IGNORE_IF_BEFORE_LINEFEED:
  265. bIgnore = lfCheck.beforeLinefeed();
  266. break;
  267. case Constants.EN_IGNORE_IF_SURROUNDING_LINEFEED:
  268. bIgnore = afterLinefeed
  269. || lfCheck.beforeLinefeed();
  270. break;
  271. case Constants.EN_IGNORE_IF_AFTER_LINEFEED:
  272. bIgnore = afterLinefeed;
  273. break;
  274. case Constants.EN_PRESERVE:
  275. //nothing to do now, replacement takes place later
  276. break;
  277. default:
  278. //nop
  279. }
  280. // Handle ignore and replacement
  281. if (bIgnore) {
  282. charIter.remove();
  283. } else {
  284. // this is to retain a single space between words
  285. inWhiteSpace = true;
  286. if (currentChar != '\u0020') {
  287. charIter.replaceChar('\u0020');
  288. }
  289. }
  290. }
  291. break;
  292. case CharUtilities.LINEFEED:
  293. // A linefeed
  294. switch (linefeedTreatment) {
  295. case Constants.EN_IGNORE:
  296. charIter.remove();
  297. break;
  298. case Constants.EN_TREAT_AS_ZERO_WIDTH_SPACE:
  299. charIter.replaceChar(CharUtilities.ZERO_WIDTH_SPACE);
  300. inWhiteSpace = false;
  301. break;
  302. case Constants.EN_PRESERVE:
  303. lfCheck.reset();
  304. inWhiteSpace = false;
  305. afterLinefeed = true; // for following whitespace
  306. break;
  307. default:
  308. //nop
  309. }
  310. break;
  311. case CharUtilities.EOT:
  312. // A "boundary" objects such as non-character inline
  313. // or nested block object was encountered. (? can't happen)
  314. // If any whitespace run in progress, finish it.
  315. // FALL THROUGH
  316. default:
  317. // Any other character
  318. inWhiteSpace = false;
  319. afterLinefeed = false;
  320. nonWhiteSpaceCount++;
  321. lfCheck.reset();
  322. break;
  323. }
  324. }
  325. }
  326. private void addPendingInline(FObjMixed fo) {
  327. if (pendingInlines == null) {
  328. pendingInlines = new java.util.ArrayList(5);
  329. }
  330. pendingInlines.add(new PendingInline(fo, firstWhiteSpaceInSeq));
  331. }
  332. private void handlePendingInlines() {
  333. if (!(pendingInlines == null || pendingInlines.isEmpty())) {
  334. if (nonWhiteSpaceCount == 0) {
  335. /* handle white-space for all pending inlines*/
  336. PendingInline p;
  337. for (int i = pendingInlines.size(); --i >= 0;) {
  338. p = (PendingInline)pendingInlines.get(i);
  339. charIter = (RecursiveCharIterator)p.firstTrailingWhiteSpace;
  340. handleWhiteSpace();
  341. pendingInlines.remove(p);
  342. }
  343. } else {
  344. /* there is non-white-space text between the pending
  345. * inline(s) and the end of the block;
  346. * clear list of pending inlines */
  347. pendingInlines.clear();
  348. }
  349. }
  350. }
  351. /**
  352. * Helper class, used during white-space handling to look ahead, and
  353. * see if the next character is a linefeed (or if there will be
  354. * an equivalent effect during layout, i.e. end-of-block or
  355. * the following child is a block-level FO)
  356. */
  357. private class EOLchecker {
  358. private boolean nextIsEOL;
  359. private RecursiveCharIterator charIter;
  360. EOLchecker(CharIterator charIter) {
  361. this.charIter = (RecursiveCharIterator) charIter;
  362. }
  363. boolean beforeLinefeed() {
  364. if (!nextIsEOL) {
  365. CharIterator lfIter = charIter.mark();
  366. while (lfIter.hasNext()) {
  367. int charClass = CharUtilities.classOf(lfIter.nextChar());
  368. if (charClass == CharUtilities.LINEFEED) {
  369. if (linefeedTreatment == Constants.EN_PRESERVE) {
  370. nextIsEOL = true;
  371. return nextIsEOL;
  372. }
  373. } else if (charClass != CharUtilities.XMLWHITESPACE) {
  374. return nextIsEOL;
  375. }
  376. }
  377. // No more characters == end of text run
  378. // means EOL if there either is a nested block to be added,
  379. // or if this is the last text node in the current block
  380. nextIsEOL = nextChildIsBlockLevel || endOfBlock;
  381. }
  382. return nextIsEOL;
  383. }
  384. void reset() {
  385. nextIsEOL = false;
  386. }
  387. }
  388. /**
  389. * Helper class to store unfinished inline nodes together
  390. * with an iterator that starts at the first white-space
  391. * character in the sequence of trailing white-space
  392. */
  393. private class PendingInline {
  394. protected FObjMixed fo;
  395. protected CharIterator firstTrailingWhiteSpace;
  396. PendingInline(FObjMixed fo, CharIterator firstTrailingWhiteSpace) {
  397. this.fo = fo;
  398. this.firstTrailingWhiteSpace = firstTrailingWhiteSpace;
  399. }
  400. }
  401. }