1 22 package org.jboss.verifier; 23 24 45 46 import java.util.StringTokenizer ; 48 import java.util.Enumeration ; 49 import java.util.Collections ; 50 import java.util.Arrays ; 51 import java.util.Iterator ; 52 import java.text.ParseException ; 53 54 55 62 public class Section { 63 64 private String [] section; 65 private String info; 66 67 70 public Section( String id ) 71 { 72 try 73 { 74 section = parseSection( id ); 75 } catch ( ParseException e ) 76 { 77 throw new IllegalArgumentException ( CONSTRUCTION_ERROR ); 78 } 79 } 80 81 85 public Section( String id, String info ) 86 { 87 this( id ); 88 this.info = info; 89 } 90 91 98 99 102 public String getSectionToken( int index ) 103 { 104 if( section.length >= index ) 105 throw new IndexOutOfBoundsException (GET_SECTION_INDEX_ERROR); 106 107 return section[index]; 108 } 109 110 public Iterator getSectionTokens() 111 { 112 return Collections.unmodifiableList(Arrays.asList(section)).iterator(); 113 } 114 115 118 public String getSection() { 119 StringBuffer buffer = new StringBuffer (); 120 121 for ( int i = 0; i < section.length; ++i ) 122 { 123 buffer.append( section[i] ); 124 if ( i + 1 < section.length ) 125 buffer.append("."); 126 } 127 128 return buffer.toString(); 129 } 130 131 134 public String toString() 135 { 136 if( info != null ) 137 { 138 return getSection() + ": " + info; 139 } else 140 { 141 return getSection(); 142 } 143 } 144 145 public boolean hasInfo() 146 { 147 return ( info != null ) ? true : false; 148 } 149 150 public String getInfo() 151 { 152 return info; 153 } 154 155 162 163 166 private String [] parseSection( String id ) 167 throws ParseException 168 { 169 StringTokenizer tokenizer = new StringTokenizer ( id, DELIMETER ); 170 String [] token = new String [ tokenizer.countTokens() ]; 171 172 for (int i = 0; tokenizer.hasMoreTokens(); ++i) { 173 token[i] = tokenizer.nextToken(); 174 } 175 176 return token; 177 } 178 179 180 187 188 191 private final static String DELIMETER = "."; 192 193 196 private final static String PARSE_SECTION_ERROR = 197 "Section token cannot be longer than one character"; 198 private final static String GET_SECTION_INDEX_ERROR = 199 "Section index too large"; 200 private final static String CONSTRUCTION_ERROR = 201 "Cannot parse section string"; 202 } 203 204 207 | Popular Tags |