2
* Copyright 2004 by Paulo Soares.
4
* The contents of this file are subject to the Mozilla Public License Version 1.1
5
* (the "License"); you may not use this file except in compliance with the License.
6
* You may obtain a copy of the License at http://www.mozilla.org/MPL/
8
* Software distributed under the License is distributed on an "AS IS" basis,
9
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
10
* for the specific language governing rights and limitations under the License.
12
* The Original Code is 'iText, a free JAVA-PDF library'.
14
* The Initial Developer of the Original Code is Bruno Lowagie. Portions created by
15
* the Initial Developer are Copyright (C) 1999, 2000, 2001, 2002 by Bruno Lowagie.
16
* All Rights Reserved.
17
* Co-Developer of the code is Paulo Soares. Portions created by the Co-Developer
18
* are Copyright (C) 2000, 2001, 2002 by Paulo Soares. All Rights Reserved.
20
* Contributor(s): all the names of the contributors are added in the source code
23
* Alternatively, the contents of this file may be used under the terms of the
24
* LGPL license (the "GNU LIBRARY GENERAL PUBLIC LICENSE"), in which case the
25
* provisions of LGPL are applicable instead of those above. If you wish to
26
* allow use of your version of this file only under the terms of the LGPL
27
* License and not to allow others to use your version of this file under
28
* the MPL, indicate your decision by deleting the provisions above and
29
* replace them with the notice and other provisions required by the LGPL.
30
* If you do not delete the provisions above, a recipient may use your version
31
* of this file under either the MPL or the GNU LIBRARY GENERAL PUBLIC LICENSE.
33
* This library is free software; you can redistribute it and/or modify it
34
* under the terms of the MPL as stated above or under the terms of the GNU
35
* Library General Public License as published by the Free Software Foundation;
36
* either version 2 of the License, or any later version.
38
* This library is distributed in the hope that it will be useful, but WITHOUT
39
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
40
* FOR A PARTICULAR PURPOSE. See the GNU Library general Public License for more
43
* If you didn't download this code from the following link, you should check if
44
* you aren't using an obsolete version:
45
* http://www.lowagie.com/iText/
47
package com.lowagie.text.pdf;
49
import java.util.List;
50
import java.util.LinkedList;
51
import java.util.ListIterator;
54
* This class expands a string into a list of numbers. The main use is to select a
57
* The general systax is:<br>
58
* [!][o][odd][e][even]start-end
60
* You can have multiple ranges separated by commas ','. The '!' modifier removes the
61
* range from what is already selected. The range changes are incremental, that is,
62
* numbers are added or deleted as the range appears. The start or the end, but not both, can be ommited.
64
public class SequenceList {
65
protected static final int COMMA = 1;
66
protected static final int MINUS = 2;
67
protected static final int NOT = 3;
68
protected static final int TEXT = 4;
69
protected static final int NUMBER = 5;
70
protected static final int END = 6;
71
protected static final char EOT = '\uffff';
73
private static final int FIRST = 0;
74
private static final int DIGIT = 1;
75
private static final int OTHER = 2;
76
private static final int DIGIT2 = 3;
77
private static final String NOT_OTHER = "-,!0123456789";
79
protected char text[];
82
protected String other;
86
protected boolean odd;
87
protected boolean even;
88
protected boolean inverse;
90
protected SequenceList(String range) {
92
text = range.toCharArray();
95
protected char nextChar() {
97
if (ptr >= text.length)
105
protected void putBack() {
111
protected int getType() {
112
StringBuffer buf = new StringBuffer();
117
if (state == DIGIT) {
118
number = Integer.parseInt(other = buf.toString());
121
else if (state == OTHER) {
122
other = buf.toString().toLowerCase();
138
if (c >= '0' && c <= '9')
144
if (c >= '0' && c <= '9')
148
number = Integer.parseInt(other = buf.toString());
153
if (NOT_OTHER.indexOf(c) < 0)
157
other = buf.toString().toLowerCase();
165
private void otherProc() {
166
if (other.equals("odd") || other.equals("o")) {
170
else if (other.equals("even") || other.equals("e")) {
176
protected boolean getAttributes() {
179
odd = even = inverse = false;
182
int type = getType();
183
if (type == END || type == COMMA) {
186
return (type == END);
198
if (type == NUMBER) {
247
* Generates a list of numbers from a string.
248
* @param ranges the comma separated ranges
249
* @param maxNumber the maximum number in the range
250
* @return a list with the numbers as <CODE>Integer</CODE>
252
public static List expand(String ranges, int maxNumber) {
253
SequenceList parse = new SequenceList(ranges);
254
LinkedList list = new LinkedList();
255
boolean sair = false;
257
sair = parse.getAttributes();
258
if (parse.low == -1 && parse.high == -1 && !parse.even && !parse.odd)
262
if (parse.high < 1 || parse.high > maxNumber)
263
parse.high = maxNumber;
264
if (parse.low > maxNumber)
265
parse.low = maxNumber;
267
//System.out.println("low="+parse.low+",high="+parse.high+",odd="+parse.odd+",even="+parse.even+",inverse="+parse.inverse);
270
if (parse.low > parse.high) {
272
parse.low = parse.high;
275
for (ListIterator it = list.listIterator(); it.hasNext();) {
276
int n = ((Integer)it.next()).intValue();
277
if (parse.even && (n & 1) == 1)
279
if (parse.odd && (n & 1) == 0)
281
if (n >= parse.low && n <= parse.high)
286
if (parse.low > parse.high) {
288
if (parse.odd || parse.even) {
293
parse.low -= ((parse.low & 1) == 1 ? 0 : 1);
295
for (int k = parse.low; k >= parse.high; k += inc)
296
list.add(new Integer(k));
299
if (parse.odd || parse.even) {
304
parse.low += ((parse.low & 1) == 1 ? 1 : 0);
306
for (int k = parse.low; k <= parse.high; k += inc) {
307
list.add(new Integer(k));
311
// for (int k = 0; k < list.size(); ++k)
312
// System.out.print(((Integer)list.get(k)).intValue() + ",");
313
// System.out.println();
b'\\ No newline at end of file'