1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package org.jdtaus.banking.dtaus.spi;
22
23 import java.util.ArrayList;
24 import java.util.HashMap;
25 import java.util.Iterator;
26 import java.util.LinkedList;
27 import java.util.List;
28 import java.util.Locale;
29 import java.util.Map;
30 import org.jdtaus.banking.dtaus.Header;
31 import org.jdtaus.core.text.Message;
32 import org.jdtaus.core.text.Messages;
33
34
35
36
37
38
39
40
41
42
43
44
45 public class IllegalHeaderException
46 extends org.jdtaus.banking.dtaus.IllegalHeaderException
47 {
48
49
50 private static final long serialVersionUID = -6726814718299291560L;
51
52
53 public IllegalHeaderException()
54 {
55 super();
56 }
57
58
59 private static final String PROP_UNSPECIFIED = "org.jdtaus.banking.dtaus.Header";
60
61
62
63
64
65 private Map messages = new HashMap( 100 );
66
67
68
69
70
71
72
73
74 public void addMessage( final Message message )
75 {
76 this.addMessage( PROP_UNSPECIFIED, message );
77 }
78
79
80
81
82
83
84
85
86 public final void addMessages( final Messages messages )
87 {
88 if ( messages == null )
89 {
90 throw new NullPointerException( "messages" );
91 }
92
93 for ( int i = messages.size() - 1; i >= 0; i-- )
94 {
95 this.addMessage( messages.getMessage( i ) );
96 }
97 }
98
99
100
101
102
103
104
105
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
130
131
132
133
134
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
155
156
157
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
173
174
175
176
177
178
179
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
194
195
196
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
215
216
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
276
277
278
279 public String toString()
280 {
281 return super.toString() + '\n' + this.internalString();
282 }
283
284 }