1 51 package org.apache.fop.fo.pagination; 52 53 import org.apache.avalon.framework.logger.Logger; 55 56 61 public class PageNumberGenerator { 62 63 private String format; 64 private char groupingSeparator; 65 private int groupingSize; 66 private int letterValue; 67 68 private final int DECIMAL = 1; private final int LOWERALPHA = 2; private final int UPPERALPHA = 3; private final int LOWERROMAN = 4; private final int UPPERROMAN = 5; 75 private int formatType = DECIMAL; 77 private int minPadding = 0; 79 private String zeros[] = { 81 "", "0", "00", "000", "0000", "00000" 82 }; 83 84 private Logger log; 85 86 public PageNumberGenerator(String format, char groupingSeparator, 87 int groupingSize, int letterValue) { 88 this.format = format; 89 this.groupingSeparator = groupingSeparator; 90 this.groupingSize = groupingSize; 91 this.letterValue = letterValue; 92 93 int fmtLen = format.length(); 96 if (fmtLen == 1) { 97 if (format.equals("1")) { 98 formatType = DECIMAL; 99 minPadding = 0; 100 } else if (format.equals("a")) { 101 formatType = LOWERALPHA; 102 } else if (format.equals("A")) { 103 formatType = UPPERALPHA; 104 } else if (format.equals("i")) { 105 formatType = LOWERROMAN; 106 } else if (format.equals("I")) { 107 formatType = UPPERROMAN; 108 } else { 109 formatType = DECIMAL; 112 minPadding = 0; 113 } 114 } else { 115 for (int i = 0; i < fmtLen - 1; i++) { 119 if (format.charAt(i) != '0') { 120 formatType = DECIMAL; 122 minPadding = 0; 123 } else { 124 minPadding = fmtLen - 1; 125 } 126 } 127 } 128 } 129 130 public void setLogger(Logger logger) { 131 log = logger; 132 } 133 134 public String makeFormattedPageNumber(int number) { 135 String pn = null; 136 if (formatType == DECIMAL) { 137 pn = Integer.toString(number); 138 if (minPadding >= pn.length()) { 139 int nz = minPadding - pn.length() + 1; 140 pn = zeros[nz] + pn; 141 } 142 } else if ((formatType == LOWERROMAN) || (formatType == UPPERROMAN)) { 143 pn = makeRoman(number); 144 if (formatType == UPPERROMAN) 145 pn = pn.toUpperCase(); 146 } else { 147 pn = makeAlpha(number); 149 if (formatType == UPPERALPHA) 150 pn = pn.toUpperCase(); 151 } 152 return pn; 153 } 154 155 private String makeRoman(int num) { 156 int arabic[] = { 157 1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1 158 }; 159 String roman[] = { 160 "m", "cm", "d", "cd", "c", "xc", "l", "xl", "x", "ix", "v", "iv", 161 "i" 162 }; 163 164 int i = 0; 165 StringBuffer romanNumber = new StringBuffer (); 166 167 while (num > 0) { 168 while (num >= arabic[i]) { 169 num = num - arabic[i]; 170 romanNumber.append(roman[i]); 171 } 172 i = i + 1; 173 } 174 return romanNumber.toString(); 175 } 176 177 private String makeAlpha(int num) { 178 String letters = "abcdefghijklmnopqrstuvwxyz"; 179 StringBuffer alphaNumber = new StringBuffer (); 180 181 int base = 26; 182 int rem = 0; 183 184 num--; 185 if (num < base) { 186 alphaNumber.append(letters.charAt(num)); 187 } else { 188 while (num >= base) { 189 rem = num % base; 190 alphaNumber.append(letters.charAt(rem)); 191 num = num / base; 192 } 193 alphaNumber.append(letters.charAt(num - 1)); 194 } 195 return alphaNumber.reverse().toString(); 196 } 197 198 } 199 200 | Popular Tags |