View Javadoc

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