001/* 002 * jDTAUS Banking SPI 003 * Copyright (C) 2005 Christian Schulte 004 * <cs@schulte.it> 005 * 006 * This library is free software; you can redistribute it and/or 007 * modify it under the terms of the GNU Lesser General Public 008 * License as published by the Free Software Foundation; either 009 * version 2.1 of the License, or any later version. 010 * 011 * This library is distributed in the hope that it will be useful, 012 * but WITHOUT ANY WARRANTY; without even the implied warranty of 013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 014 * Lesser General Public License for more details. 015 * 016 * You should have received a copy of the GNU Lesser General Public 017 * License along with this library; if not, write to the Free Software 018 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 019 * 020 */ 021package org.jdtaus.banking.spi; 022 023import java.util.ArrayList; 024import java.util.HashMap; 025import java.util.Iterator; 026import java.util.LinkedList; 027import java.util.List; 028import java.util.Locale; 029import java.util.Map; 030import org.jdtaus.banking.Textschluessel; 031import org.jdtaus.core.text.Message; 032import org.jdtaus.core.text.Messages; 033 034/** 035 * Gets thrown whenever an illegal Textschlüssel is passed to a method expecting a legal Textschlüssel. 036 * <p>Example: Throwing an {@code IllegalTextschluesselException}<br/> 037 * <blockquote><pre> 038 * IllegalTextschluesselException e = new IllegalTextschluesselException(); 039 * e.addMessage(message); 040 * e.addMessage(Textschluessel.PROP_<i>XYZ</i>, message); 041 * throw e;</pre></blockquote></p> 042 * 043 * @author <a href="mailto:cs@schulte.it">Christian Schulte</a> 044 * @version $JDTAUS: IllegalTextschluesselException.java 8661 2012-09-27 11:29:58Z schulte $ 045 */ 046public class IllegalTextschluesselException extends org.jdtaus.banking.IllegalTextschluesselException 047{ 048 049 /** Serial version UID for backwards compatibility with 1.1.x classes. */ 050 private static final long serialVersionUID = 3384133251965896939L; 051 052 /** Creates a new {@code IllegalTextschluesselException} instance. */ 053 public IllegalTextschluesselException() 054 { 055 super(); 056 } 057 058 /** Key to the list of messages not bound to any particular property. */ 059 private static final String PROP_UNSPECIFIED = "org.jdtaus.banking.Textschluessel"; 060 061 /** 062 * The messages describing the exception. 063 * @serial 064 */ 065 private Map messages = new HashMap( 100 ); 066 067 /** 068 * Adds a message to the instance. 069 * 070 * @param message the message to add to the instance. 071 * 072 * @throws NullPointerException if {@code message} is {@code null}. 073 */ 074 public void addMessage( final Message message ) 075 { 076 this.addMessage( PROP_UNSPECIFIED, message ); 077 } 078 079 /** 080 * Adds messages to the instance. 081 * 082 * @param messages The messages to add to the instance. 083 * 084 * @throws NullPointerException if {@code messages} is {@code null}. 085 */ 086 public final void addMessages( final Messages messages ) 087 { 088 if ( messages == null ) 089 { 090 throw new NullPointerException( "messages" ); 091 } 092 093 for ( int i = messages.size() - 1; i >= 0; i-- ) 094 { 095 this.addMessage( messages.getMessage( i ) ); 096 } 097 } 098 099 /** 100 * Adds a message bound to a property to the instance. 101 * 102 * @param propertyName The name of a property {@code message} is bound to. 103 * @param message The message to add to the instance. 104 * 105 * @throws NullPointerException if either {@code message} or {@code propertyName} is {@code null}. 106 */ 107 public void addMessage( final String propertyName, final Message message ) 108 { 109 if ( propertyName == null ) 110 { 111 throw new NullPointerException( "propertyName" ); 112 } 113 if ( message == null ) 114 { 115 throw new NullPointerException( "message" ); 116 } 117 118 List msgs = (List) this.messages.get( propertyName ); 119 if ( msgs == null ) 120 { 121 msgs = new LinkedList(); 122 this.messages.put( propertyName, msgs ); 123 } 124 125 msgs.add( message ); 126 } 127 128 /** 129 * Adds messages bound to a property to the instance. 130 * 131 * @param propertyName The name of a property {@code messages} are bound to. 132 * @param messages The messages to add to the instance. 133 * 134 * @throws NullPointerException if either {@code messages} or {@code propertyName} is {@code null}. 135 */ 136 public final void addMessages( final String propertyName, final Messages messages ) 137 { 138 if ( propertyName == null ) 139 { 140 throw new NullPointerException( "propertyName" ); 141 } 142 if ( messages == null ) 143 { 144 throw new NullPointerException( "messages" ); 145 } 146 147 for ( int i = messages.size() - 1; i >= 0; i-- ) 148 { 149 this.addMessage( propertyName, messages.getMessage( i ) ); 150 } 151 } 152 153 /** 154 * Gets all messages describing the exception. 155 * 156 * @return An array of messages describing the exception or an empty array if the instance does not hold any 157 * messages. 158 */ 159 public Message[] getMessages() 160 { 161 final List col = new LinkedList(); 162 for ( Iterator it = this.messages.keySet().iterator(); it.hasNext(); ) 163 { 164 final String propertyName = (String) it.next(); 165 col.addAll( (List) this.messages.get( propertyName ) ); 166 } 167 168 return (Message[]) col.toArray( new Message[ col.size() ] ); 169 } 170 171 /** 172 * Gets messages bound to a property removing these messages from the instance. 173 * 174 * @param propertyName The name of a property to return any messages for. 175 * 176 * @return All messages bound to a property with name {@code propertyName} or an empty array if the instance does 177 * not hold messages for a property with name {@code propertyName}. 178 * 179 * @throws NullPointerException if {@code propertyName} is {@code null}. 180 */ 181 public Message[] getMessages( final String propertyName ) 182 { 183 if ( propertyName == null ) 184 { 185 throw new NullPointerException( "propertyName" ); 186 } 187 188 final List msgs = (List) this.messages.remove( propertyName ); 189 return msgs == null ? new Message[ 0 ] : (Message[]) msgs.toArray( new Message[ msgs.size() ] ); 190 } 191 192 /** 193 * Gets the names of all properties for which the exception holds messages. 194 * 195 * @return An array of the names of all properties for which the exception holds messages or an empty array if the 196 * exception does not hold any message bound to a property. 197 */ 198 public String[] getPropertyNames() 199 { 200 final List names = new ArrayList( this.messages.size() ); 201 for ( Iterator it = this.messages.keySet().iterator(); it.hasNext(); ) 202 { 203 final String name = (String) it.next(); 204 if ( !PROP_UNSPECIFIED.equals( name ) ) 205 { 206 names.add( name ); 207 } 208 } 209 210 return (String[]) names.toArray( new String[ names.size() ] ); 211 } 212 213 /** 214 * Creates a string representing the messages of the instance. 215 * 216 * @return A string representing the messages of the instance. 217 */ 218 private String internalString() 219 { 220 final StringBuffer buf = new StringBuffer( 200 ).append( '{' ); 221 final String[] propertyNames = this.getPropertyNames(); 222 final List unspecifiedMsgs = (List) this.messages.get( PROP_UNSPECIFIED ); 223 224 for ( int i = 0; i < propertyNames.length; i++ ) 225 { 226 buf.append( propertyNames[i] ).append( "={" ); 227 228 int j = 0; 229 final List msgs = (List) this.messages.get( propertyNames[i] ); 230 for ( Iterator it = msgs.iterator(); it.hasNext(); j++ ) 231 { 232 final Message msg = (Message) it.next(); 233 buf.append( '[' ).append( j ).append( "]=" ).append( msg.getText( Locale.getDefault() ) ); 234 if ( it.hasNext() ) 235 { 236 buf.append( ", " ); 237 } 238 } 239 240 buf.append( '}' ); 241 if ( i + 1 < propertyNames.length ) 242 { 243 buf.append( ", " ); 244 } 245 } 246 247 if ( unspecifiedMsgs != null && !unspecifiedMsgs.isEmpty() ) 248 { 249 if ( propertyNames.length > 0 ) 250 { 251 buf.append( ", " ); 252 } 253 254 buf.append( PROP_UNSPECIFIED ).append( "={" ); 255 256 int i = 0; 257 for ( Iterator it = unspecifiedMsgs.iterator(); it.hasNext(); i++ ) 258 { 259 final Message msg = (Message) it.next(); 260 buf.append( "[" ).append( i ).append( "]=" ).append( msg.getText( Locale.getDefault() ) ); 261 if ( it.hasNext() ) 262 { 263 buf.append( ", " ); 264 } 265 } 266 267 buf.append( '}' ); 268 } 269 270 buf.append( '}' ); 271 return buf.toString(); 272 } 273 274 /** 275 * Returns a string representation of the object. 276 * 277 * @return A string representation of the object. 278 */ 279 public String toString() 280 { 281 return super.toString() + '\n' + this.internalString(); 282 } 283 284}