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.

PageNumberGenerator.java 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /*
  2. * $Id$
  3. * Copyright (C) 2001 The Apache Software Foundation. All rights reserved.
  4. * For details on use and redistribution please refer to the
  5. * LICENSE file included with these sources.
  6. */
  7. package org.apache.fop.fo.pagination;
  8. // FOP
  9. import org.apache.fop.fo.properties.*;
  10. // Avalon
  11. import org.apache.avalon.framework.logger.AbstractLogEnabled;
  12. // Java
  13. import java.util.*;
  14. /**
  15. * This class uses the 'format', 'groupingSeparator', 'groupingSize',
  16. * and 'letterValue' properties on fo:page-sequence to return a String
  17. * corresponding to the supplied integer page number.
  18. */
  19. public class PageNumberGenerator extends AbstractLogEnabled {
  20. private String format;
  21. private char groupingSeparator;
  22. private int groupingSize;
  23. private int letterValue;
  24. // constants
  25. private int DECIMAL = 1; // '0*1'
  26. private int LOWERALPHA = 2; // 'a'
  27. private int UPPERALPHA = 3; // 'A'
  28. private int LOWERROMAN = 4; // 'i'
  29. private int UPPERROMAN = 5; // 'I'
  30. // flags
  31. private int formatType = DECIMAL;
  32. private int minPadding = 0; // for decimal formats
  33. // preloaded strings of zeros
  34. private String zeros[] = {
  35. "", "0", "00", "000", "0000", "00000"
  36. };
  37. public PageNumberGenerator(String format, char groupingSeparator,
  38. int groupingSize, int letterValue) {
  39. this.format = format;
  40. this.groupingSeparator = groupingSeparator;
  41. this.groupingSize = groupingSize;
  42. this.letterValue = letterValue;
  43. // the only accepted format strings are currently '0*1' 'a', 'A', 'i'
  44. // and 'I'
  45. int fmtLen = format.length();
  46. if (fmtLen == 1) {
  47. if (format.equals("1")) {
  48. formatType = DECIMAL;
  49. minPadding = 0;
  50. } else if (format.equals("a")) {
  51. formatType = LOWERALPHA;
  52. } else if (format.equals("A")) {
  53. formatType = UPPERALPHA;
  54. } else if (format.equals("i")) {
  55. formatType = LOWERROMAN;
  56. } else if (format.equals("I")) {
  57. formatType = UPPERROMAN;
  58. } else {
  59. // token not handled
  60. //getLogger().debug("'format' token not recognized; using '1'");
  61. formatType = DECIMAL;
  62. minPadding = 0;
  63. }
  64. } else {
  65. // only accepted token is '0+1'at this stage. Because of the
  66. // wonderful regular expression support in Java, we will resort to a
  67. // loop
  68. for (int i = 0; i < fmtLen - 1; i++) {
  69. if (format.charAt(i) != '0') {
  70. //getLogger().debug("'format' token not recognized; using '1'");
  71. formatType = DECIMAL;
  72. minPadding = 0;
  73. } else {
  74. minPadding = fmtLen - 1;
  75. }
  76. }
  77. }
  78. }
  79. public String makeFormattedPageNumber(int number) {
  80. String pn = null;
  81. if (formatType == DECIMAL) {
  82. pn = Integer.toString(number);
  83. if (minPadding >= pn.length()) {
  84. int nz = minPadding - pn.length() + 1;
  85. pn = zeros[nz] + pn;
  86. }
  87. } else if ((formatType == LOWERROMAN) || (formatType == UPPERROMAN)) {
  88. pn = makeRoman(number);
  89. if (formatType == UPPERROMAN)
  90. pn = pn.toUpperCase();
  91. } else {
  92. // alphabetic
  93. pn = makeAlpha(number);
  94. if (formatType == UPPERALPHA)
  95. pn = pn.toUpperCase();
  96. }
  97. return pn;
  98. }
  99. private String makeRoman(int num) {
  100. int arabic[] = {
  101. 1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1
  102. };
  103. String roman[] = {
  104. "m", "cm", "d", "cd", "c", "xc", "l", "xl", "x", "ix", "v", "iv",
  105. "i"
  106. };
  107. int i = 0;
  108. StringBuffer romanNumber = new StringBuffer();
  109. while (num > 0) {
  110. while (num >= arabic[i]) {
  111. num = num - arabic[i];
  112. romanNumber.append(roman[i]);
  113. }
  114. i = i + 1;
  115. }
  116. return romanNumber.toString();
  117. }
  118. private String makeAlpha(int num) {
  119. String letters = "abcdefghijklmnopqrstuvwxyz";
  120. StringBuffer alphaNumber = new StringBuffer();
  121. int base = 26;
  122. int rem = 0;
  123. num--;
  124. if (num < base) {
  125. alphaNumber.append(letters.charAt(num));
  126. } else {
  127. while (num >= base) {
  128. rem = num % base;
  129. alphaNumber.append(letters.charAt(rem));
  130. num = num / base;
  131. }
  132. alphaNumber.append(letters.charAt(num - 1));
  133. }
  134. return alphaNumber.reverse().toString();
  135. }
  136. }