KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > fop > fo > pagination > PageNumberGenerator


1 /*
2  * $Id: PageNumberGenerator.java,v 1.3.2.2 2003/02/25 12:57:04 jeremias Exp $
3  * ============================================================================
4  * The Apache Software License, Version 1.1
5  * ============================================================================
6  *
7  * Copyright (C) 1999-2003 The Apache Software Foundation. All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without modifica-
10  * tion, are permitted provided that the following conditions are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright notice,
13  * this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright notice,
16  * this list of conditions and the following disclaimer in the documentation
17  * and/or other materials provided with the distribution.
18  *
19  * 3. The end-user documentation included with the redistribution, if any, must
20  * include the following acknowledgment: "This product includes software
21  * developed by the Apache Software Foundation (http://www.apache.org/)."
22  * Alternately, this acknowledgment may appear in the software itself, if
23  * and wherever such third-party acknowledgments normally appear.
24  *
25  * 4. The names "FOP" and "Apache Software Foundation" must not be used to
26  * endorse or promote products derived from this software without prior
27  * written permission. For written permission, please contact
28  * apache@apache.org.
29  *
30  * 5. Products derived from this software may not be called "Apache", nor may
31  * "Apache" appear in their name, without prior written permission of the
32  * Apache Software Foundation.
33  *
34  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
35  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
36  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
37  * APACHE SOFTWARE FOUNDATION OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
38  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLU-
39  * DING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
40  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
41  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
42  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
43  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
44  * ============================================================================
45  *
46  * This software consists of voluntary contributions made by many individuals
47  * on behalf of the Apache Software Foundation and was originally created by
48  * James Tauber <jtauber@jtauber.com>. For more information on the Apache
49  * Software Foundation, please see <http://www.apache.org/>.
50  */

51 package org.apache.fop.fo.pagination;
52
53 // Avalon
54
import org.apache.avalon.framework.logger.Logger;
55
56 /**
57  * This class uses the 'format', 'groupingSeparator', 'groupingSize',
58  * and 'letterValue' properties on fo:page-sequence to return a String
59  * corresponding to the supplied integer page number.
60  */

61 public class PageNumberGenerator {
62
63     private String JavaDoc format;
64     private char groupingSeparator;
65     private int groupingSize;
66     private int letterValue;
67
68     // constants
69
private final int DECIMAL = 1; // '0*1'
70
private final int LOWERALPHA = 2; // 'a'
71
private final int UPPERALPHA = 3; // 'A'
72
private final int LOWERROMAN = 4; // 'i'
73
private final int UPPERROMAN = 5; // 'I'
74

75     // flags
76
private int formatType = DECIMAL;
77     private int minPadding = 0; // for decimal formats
78

79     // preloaded strings of zeros
80
private String JavaDoc zeros[] = {
81         "", "0", "00", "000", "0000", "00000"
82     };
83
84     private Logger log;
85
86     public PageNumberGenerator(String JavaDoc 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         // the only accepted format strings are currently '0*1' 'a', 'A', 'i'
94
// and 'I'
95
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                 // token not handled
110
//log.debug("'format' token not recognized; using '1'");
111
formatType = DECIMAL;
112                 minPadding = 0;
113             }
114         } else {
115             // only accepted token is '0+1'at this stage. Because of the
116
// wonderful regular expression support in Java, we will resort to a
117
// loop
118
for (int i = 0; i < fmtLen - 1; i++) {
119                 if (format.charAt(i) != '0') {
120                     //log.debug("'format' token not recognized; using '1'");
121
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 JavaDoc makeFormattedPageNumber(int number) {
135         String JavaDoc 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             // alphabetic
148
pn = makeAlpha(number);
149             if (formatType == UPPERALPHA)
150                 pn = pn.toUpperCase();
151         }
152         return pn;
153     }
154
155     private String JavaDoc makeRoman(int num) {
156         int arabic[] = {
157             1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1
158         };
159         String JavaDoc roman[] = {
160             "m", "cm", "d", "cd", "c", "xc", "l", "xl", "x", "ix", "v", "iv",
161             "i"
162         };
163
164         int i = 0;
165         StringBuffer JavaDoc romanNumber = new StringBuffer JavaDoc();
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 JavaDoc makeAlpha(int num) {
178         String JavaDoc letters = "abcdefghijklmnopqrstuvwxyz";
179         StringBuffer JavaDoc alphaNumber = new StringBuffer JavaDoc();
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