1 package org.apache.maven.doxia.module.fo;
2
3 /*
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
19 * under the License.
20 */
21
22 import org.apache.maven.doxia.sink.Sink;
23
24 /**
25 * Used to count the position in a numbered list.
26 *
27 * @author ltheussl
28 * @since 1.1
29 */
30 public class NumberedListItem
31 {
32
33 /** Arabic decimals from 1 - 26. */
34 private static final String[] DECIMALS =
35 {
36 "1", "2", "3", "4", "5", "6", "7", "8", "9", "10",
37 "11", "12", "13", "14", "15", "16", "17", "18", "19", "20",
38 "21", "22", "23", "24", "25", "26"
39 };
40
41 /** Lower-case alphanumerics from a - z. */
42 private static final String[] LOWER_ALPHAS =
43 {
44 "a", "b", "c", "d", "e", "f", "g", "h", "i", "j",
45 "k", "l", "m", "n", "o", "p", "q", "r", "s", "t",
46 "u", "v", "w", "x", "y", "z"
47 };
48
49 /** Upper-case alphanumerics from A - Z. */
50 private static final String[] UPPER_ALPHAS =
51 {
52 "A", "B", "C", "D", "E", "F", "G", "H", "I", "J",
53 "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T",
54 "U", "V", "W", "X", "Y", "Z"
55 };
56
57 /** Lower-case roman numbers from i - xxvi. */
58 private static final String[] LOWER_ROMANS =
59 {
60 "i", "ii", "iii", "iv", "v", "vi", "vii", "viii", "ix", "x",
61 "xi", "xii", "xiii", "xiv", "xv", "xvi", "xvii", "xviii", "xix", "xx",
62 "xxi", "xxii", "xxiii", "xxiv", "xxv", "xxvi"
63 };
64
65 /** Upper-case roman numbers from I - XXVI. */
66 private static final String[] UPPER_ROMANS =
67 {
68 "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX", "X",
69 "XI", "XII", "XIII", "XIV", "XV", "XVI", "XVII", "XVIII", "XIX", "XX",
70 "XXI", "XXII", "XXIII", "XXIV", "XXV", "XXVI"
71 };
72
73 /** The position in the list. */
74 private int count;
75
76 /** The numbering format. */
77 private final int format;
78
79 /**
80 * Constructor. Initializes count and format.
81 *
82 * @param itemFormat The numbering format of this List.
83 * Should be one of the formats defined in {@link org.apache.maven.doxia.sink.Sink}.
84 */
85 public NumberedListItem( int itemFormat )
86 {
87 if ( !isValidItemFormat( itemFormat ) )
88 {
89 throw new IllegalArgumentException( "Unknown item format!" );
90 }
91
92 this.format = itemFormat;
93 this.count = 0;
94 }
95
96 /**
97 * Returns the current count, ie the position in the list.
98 *
99 * @return The current count.
100 */
101 public int count()
102 {
103 return count;
104 }
105
106 /**
107 * Returns the numbering format.
108 *
109 * @return The numbering format.
110 */
111 public int format()
112 {
113 return format;
114 }
115
116 /**
117 * Increase the current count by 1.
118 */
119 public void next()
120 {
121 count++;
122 }
123
124 /**
125 * Returns the symbol for the current list item.
126 *
127 * @return The symbol for the current list item.
128 */
129 public String getListItemSymbol()
130 {
131 int j = count() - 1;
132
133 if ( j < 0 )
134 {
135 j = 0;
136 }
137 else if ( j > DECIMALS.length - 1 )
138 {
139 j = DECIMALS.length - 1;
140 }
141
142 String symbol;
143
144 switch ( format() )
145 {
146 case Sink.NUMBERING_UPPER_ALPHA:
147 symbol = UPPER_ALPHAS[j];
148 break;
149 case Sink.NUMBERING_LOWER_ALPHA:
150 symbol = LOWER_ALPHAS[j];
151 break;
152 case Sink.NUMBERING_UPPER_ROMAN:
153 symbol = UPPER_ROMANS[j];
154 break;
155 case Sink.NUMBERING_LOWER_ROMAN:
156 symbol = LOWER_ROMANS[j];
157 break;
158 case Sink.NUMBERING_DECIMAL:
159 default:
160 symbol = DECIMALS[j];
161 }
162
163 return symbol + ".";
164 }
165
166 /**
167 * Determines if the given format is one of the formats defined in
168 * {@link org.apache.maven.doxia.sink.Sink}.
169 *
170 * @param itemFormat the format to check.
171 * @return True if the format is a valid item format according to the Sink API.
172 */
173 private boolean isValidItemFormat( int itemFormat )
174 {
175 return ( ( itemFormat == Sink.NUMBERING_UPPER_ALPHA )
176 || ( itemFormat == Sink.NUMBERING_LOWER_ALPHA )
177 || ( itemFormat == Sink.NUMBERING_UPPER_ROMAN )
178 || ( itemFormat == Sink.NUMBERING_LOWER_ROMAN )
179 || ( itemFormat == Sink.NUMBERING_DECIMAL ) );
180 }
181
182 }