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.

WhitespaceCollapser.java 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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.render.rtf.rtflib.rtfdoc;
  19. /*
  20. * This file is part of the RTF library of the FOP project, which was originally
  21. * created by Bertrand Delacretaz bdelacretaz@codeconsult.ch and by other
  22. * contributors to the jfor project (www.jfor.org), who agreed to donate jfor to
  23. * the FOP project.
  24. */
  25. import java.util.StringTokenizer;
  26. /**
  27. * <p>Collapses whitespace of an RtfContainer that contains RtfText elements.</p>
  28. *
  29. * <p>This work was authored by Bertrand Delacretaz (bdelacretaz@codeconsult.ch).</p>
  30. */
  31. final class WhitespaceCollapser {
  32. private static final String SPACE = " ";
  33. private boolean lastEndSpace = true;
  34. /**
  35. * Remove extra whitespace in RtfText elements that are inside container.
  36. * @param c the container
  37. */
  38. WhitespaceCollapser(RtfContainer c) {
  39. // process all texts
  40. for (final Object kid : c.getChildren()) {
  41. if (kid instanceof RtfText) {
  42. RtfText current = (RtfText) kid;
  43. processText(current);
  44. } else if (kid instanceof RtfString) {
  45. RtfString current = (RtfString) kid;
  46. processString(current);
  47. } else {
  48. // if there is something between two texts, it counts for a space
  49. lastEndSpace = true;
  50. }
  51. }
  52. }
  53. /** @return last end space */
  54. public boolean getLastEndSpace() {
  55. return lastEndSpace;
  56. }
  57. /** process one RtfText from our container */
  58. private void processText(RtfText txt) {
  59. final String newString = processString(txt.getText());
  60. if (newString != null) {
  61. txt.setText(newString);
  62. }
  63. }
  64. /** process one RtfString from our container */
  65. private void processString(RtfString txt) {
  66. final String newString = processString(txt.getText());
  67. if (newString != null) {
  68. txt.setText(newString);
  69. }
  70. }
  71. /** process one String */
  72. private String processString(String txt) {
  73. final String orig = txt;
  74. // tokenize the text based on whitespace and regenerate it so as
  75. // to collapse multiple spaces into one
  76. if (orig == null) {
  77. return null;
  78. } else if (orig.length() > 0) {
  79. final boolean allSpaces = orig.trim().length() == 0;
  80. final boolean endSpace = allSpaces
  81. || Character.isWhitespace(orig.charAt(orig.length() - 1));
  82. final boolean beginSpace = Character.isWhitespace(orig.charAt(0));
  83. final StringBuffer sb = new StringBuffer(orig.length());
  84. // if text contains spaces only, keep at most one
  85. if (allSpaces) {
  86. if (!lastEndSpace) {
  87. sb.append(SPACE);
  88. }
  89. } else {
  90. // TODO to be compatible with different Locales, should use Character.isWhitespace
  91. // instead of this limited list
  92. boolean first = true;
  93. final StringTokenizer stk = new StringTokenizer(txt, " \t\n\r");
  94. while (stk.hasMoreTokens()) {
  95. if (first && beginSpace && !lastEndSpace) {
  96. sb.append(SPACE);
  97. }
  98. first = false;
  99. sb.append(stk.nextToken());
  100. if (stk.hasMoreTokens() || endSpace) {
  101. sb.append(SPACE);
  102. }
  103. }
  104. }
  105. lastEndSpace = endSpace;
  106. return sb.toString();
  107. } else {
  108. return "";
  109. }
  110. }
  111. }