001package org.apache.maven.doxia.module.fo; 002 003/* 004 * Licensed to the Apache Software Foundation (ASF) under one 005 * or more contributor license agreements. See the NOTICE file 006 * distributed with this work for additional information 007 * regarding copyright ownership. The ASF licenses this file 008 * to you under the Apache License, Version 2.0 (the 009 * "License"); you may not use this file except in compliance 010 * with the License. You may obtain a copy of the License at 011 * 012 * http://www.apache.org/licenses/LICENSE-2.0 013 * 014 * Unless required by applicable law or agreed to in writing, 015 * software distributed under the License is distributed on an 016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 017 * KIND, either express or implied. See the License for the 018 * specific language governing permissions and limitations 019 * under the License. 020 */ 021 022import org.apache.maven.doxia.sink.Sink; 023 024/** 025 * Used to count the position in a numbered list. 026 * 027 * @author ltheussl 028 * @version $Id$ 029 * @since 1.1 030 */ 031public class NumberedListItem 032{ 033 034 /** Arabic decimals from 1 - 26. */ 035 private static final String[] DECIMALS = 036 { 037 "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", 038 "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", 039 "21", "22", "23", "24", "25", "26" 040 }; 041 042 /** Lower-case alphanumerics from a - z. */ 043 private static final String[] LOWER_ALPHAS = 044 { 045 "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", 046 "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", 047 "u", "v", "w", "x", "y", "z" 048 }; 049 050 /** Upper-case alphanumerics from A - Z. */ 051 private static final String[] UPPER_ALPHAS = 052 { 053 "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", 054 "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", 055 "U", "V", "W", "X", "Y", "Z" 056 }; 057 058 /** Lower-case roman numbers from i - xxvi. */ 059 private static final String[] LOWER_ROMANS = 060 { 061 "i", "ii", "iii", "iv", "v", "vi", "vii", "viii", "ix", "x", 062 "xi", "xii", "xiii", "xiv", "xv", "xvi", "xvii", "xviii", "xix", "xx", 063 "xxi", "xxii", "xxiii", "xxiv", "xxv", "xxvi" 064 }; 065 066 /** Upper-case roman numbers from I - XXVI. */ 067 private static final String[] UPPER_ROMANS = 068 { 069 "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX", "X", 070 "XI", "XII", "XIII", "XIV", "XV", "XVI", "XVII", "XVIII", "XIX", "XX", 071 "XXI", "XXII", "XXIII", "XXIV", "XXV", "XXVI" 072 }; 073 074 /** The position in the list. */ 075 private int count; 076 077 /** The numbering format. */ 078 private final int format; 079 080 /** 081 * Constructor. Initializes count and format. 082 * 083 * @param itemFormat The numbering format of this List. 084 * Should be one of the formats defined in {@link org.apache.maven.doxia.sink.Sink}. 085 */ 086 public NumberedListItem( int itemFormat ) 087 { 088 if ( !isValidItemFormat( itemFormat ) ) 089 { 090 throw new IllegalArgumentException( "Unknown item format!" ); 091 } 092 093 this.format = itemFormat; 094 this.count = 0; 095 } 096 097 /** 098 * Returns the current count, ie the position in the list. 099 * 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}