1 | /* |
2 | * jDTAUS Core Messages |
3 | * Copyright (C) 2005 Christian Schulte |
4 | * |
5 | * This library is free software; you can redistribute it and/or |
6 | * modify it under the terms of the GNU Lesser General Public |
7 | * License as published by the Free Software Foundation; either |
8 | * version 2.1 of the License, or any later version. |
9 | * |
10 | * This library is distributed in the hope that it will be useful, |
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
13 | * Lesser General Public License for more details. |
14 | * |
15 | * You should have received a copy of the GNU Lesser General Public |
16 | * License along with this library; if not, write to the Free Software |
17 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
18 | * |
19 | * $JDTAUS: IllegalStringMessage.java 8738 2012-10-05 20:54:36Z schulte $ |
20 | */ |
21 | package org.jdtaus.core.messages; |
22 | |
23 | import java.util.Locale; |
24 | import org.jdtaus.core.container.ContainerFactory; |
25 | import org.jdtaus.core.text.Message; |
26 | |
27 | /** |
28 | * Message stating that an illegal string was specified. |
29 | * |
30 | * @author Christian Schulte |
31 | * @version $JDTAUS: IllegalStringMessage.java 8738 2012-10-05 20:54:36Z schulte $ |
32 | * @since 1.10 |
33 | */ |
34 | public final class IllegalStringMessage extends Message |
35 | { |
36 | //--IllegalStringMessage---------------------------------------------------- |
37 | |
38 | /** Serial version UID for backwards compatibility with 1.10.x classes. */ |
39 | private static final long serialVersionUID = -8783693360487171440L; |
40 | |
41 | /** |
42 | * The invalid string. |
43 | * @serial |
44 | */ |
45 | private String invalidString; |
46 | |
47 | /** |
48 | * The invalid characters of the string. |
49 | * @serial |
50 | */ |
51 | private char[] invalidCharacters; |
52 | |
53 | /** |
54 | * The minimum required length. |
55 | * @serial |
56 | */ |
57 | private Number minimumLength; |
58 | |
59 | /** |
60 | * The maximum allowed length. |
61 | * @serial |
62 | */ |
63 | private Number maximumLength; |
64 | |
65 | /** |
66 | * Creates a new {@code IllegalStringMessage} instance taking an invalid string, an array of invalid characters, |
67 | * a minimum required length and a maximum allowed length. |
68 | * |
69 | * @param invalidString The invalid string or {@code null} if no such string is known. |
70 | * @param invalidCharacters The invalid characters or {@code null} if no such characters are known. |
71 | * @param minimumLength The minimum required length or {@code null} if no such requirement exists. |
72 | * @param maximumLength The maximum allowed length or {@code null} if no such limit exists. |
73 | */ |
74 | public IllegalStringMessage( final String invalidString, final char[] invalidCharacters, |
75 | final Number minimumLength, final Number maximumLength ) |
76 | { |
77 | super(); |
78 | this.invalidString = invalidString; |
79 | this.minimumLength = minimumLength; |
80 | this.maximumLength = maximumLength; |
81 | |
82 | if ( invalidCharacters != null ) |
83 | { |
84 | this.invalidCharacters = new char[ invalidCharacters.length ]; |
85 | |
86 | for ( int i = 0, s0 = invalidCharacters.length; i < s0; i++ ) |
87 | { |
88 | this.invalidCharacters[i] = invalidCharacters[i]; |
89 | } |
90 | } |
91 | else |
92 | { |
93 | this.invalidCharacters = null; |
94 | } |
95 | } |
96 | |
97 | //----------------------------------------------------IllegalStringMessage-- |
98 | //--Message----------------------------------------------------------------- |
99 | |
100 | /** |
101 | * {@inheritDoc} |
102 | * <ul> |
103 | * <li>[0]: The invalid string or {@code null} if no such string is known.</li> |
104 | * <li>[1]: The invalid characters or {@code null} if no such characters are known.</li> |
105 | * <li>[2]: The minimum required length or {@code null} if no such requirement exists.</li> |
106 | * <li>[3]: The maximum allowed length or {@code null} if no such limit exists.</li> |
107 | * </ul> |
108 | */ |
109 | public Object[] getFormatArguments( final Locale locale ) |
110 | { |
111 | return new Object[] |
112 | { |
113 | this.invalidString, this.invalidCharacters, this.minimumLength, this.maximumLength |
114 | }; |
115 | |
116 | } |
117 | |
118 | public String getText( final Locale locale ) |
119 | { |
120 | final StringBuffer b = new StringBuffer( 128 ); |
121 | |
122 | if ( this.invalidString != null ) |
123 | { |
124 | b.append( this.getIllegalStringMessage( locale, this.invalidString ) ).append( " " ); |
125 | } |
126 | |
127 | if ( this.invalidCharacters != null ) |
128 | { |
129 | final StringBuffer c = new StringBuffer( this.invalidCharacters.length * 2 ).append( "[" ); |
130 | |
131 | for ( int i = 0, s0 = this.invalidCharacters.length; i < s0; i++ ) |
132 | { |
133 | c.append( this.invalidCharacters[i] ).append( ", " ); |
134 | } |
135 | |
136 | c.setLength( c.length() - 2 ); |
137 | c.append( "]" ); |
138 | |
139 | b.append( this.getIllegalCharactersMessage( locale, c.toString() ) ).append( " " ); |
140 | } |
141 | |
142 | if ( this.minimumLength != null ) |
143 | { |
144 | b.append( this.getIllegalMinimumStringLengthMessage( locale, this.minimumLength ) ).append( " " ); |
145 | } |
146 | |
147 | if ( this.maximumLength != null ) |
148 | { |
149 | b.append( this.getIllegalMaximumStringLengthMessage( locale, this.maximumLength ) ).append( " " ); |
150 | } |
151 | |
152 | if ( b.length() > 0 ) |
153 | { |
154 | b.setLength( b.length() - 1 ); |
155 | } |
156 | |
157 | return b.toString(); |
158 | } |
159 | |
160 | //-----------------------------------------------------------------Message-- |
161 | //--Messages---------------------------------------------------------------- |
162 | |
163 | // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:jdtausMessages |
164 | // This section is managed by jdtaus-container-mojo. |
165 | |
166 | /** |
167 | * Gets the text of message <code>illegalString</code>. |
168 | * <blockquote><pre>Ungültige Zeichenkette ''{0}''.</pre></blockquote> |
169 | * <blockquote><pre>Illegal string ''{0}''.</pre></blockquote> |
170 | * |
171 | * @param locale The locale of the message instance to return. |
172 | * @param illegalString Illegal string. |
173 | * |
174 | * @return Information about an illegal string. |
175 | */ |
176 | private String getIllegalStringMessage( final Locale locale, |
177 | final java.lang.String illegalString ) |
178 | { |
179 | return ContainerFactory.getContainer(). |
180 | getMessage( this, "illegalString", locale, |
181 | new Object[] |
182 | { |
183 | illegalString |
184 | }); |
185 | |
186 | } |
187 | |
188 | /** |
189 | * Gets the text of message <code>illegalMaximumStringLength</code>. |
190 | * <blockquote><pre>Mehr als {0,number} Zeichen.</pre></blockquote> |
191 | * <blockquote><pre>More than {0,number} characters.</pre></blockquote> |
192 | * |
193 | * @param locale The locale of the message instance to return. |
194 | * @param maximumLength Maximum allowed length. |
195 | * |
196 | * @return Information about an illegal maximum string length. |
197 | */ |
198 | private String getIllegalMaximumStringLengthMessage( final Locale locale, |
199 | final java.lang.Number maximumLength ) |
200 | { |
201 | return ContainerFactory.getContainer(). |
202 | getMessage( this, "illegalMaximumStringLength", locale, |
203 | new Object[] |
204 | { |
205 | maximumLength |
206 | }); |
207 | |
208 | } |
209 | |
210 | /** |
211 | * Gets the text of message <code>illegalMinimumStringLength</code>. |
212 | * <blockquote><pre>Weniger als {0,number} Zeichen.</pre></blockquote> |
213 | * <blockquote><pre>Less than {0,number} characters.</pre></blockquote> |
214 | * |
215 | * @param locale The locale of the message instance to return. |
216 | * @param minimumLength Minimum required length. |
217 | * |
218 | * @return Information about an illegal minimum string length. |
219 | */ |
220 | private String getIllegalMinimumStringLengthMessage( final Locale locale, |
221 | final java.lang.Number minimumLength ) |
222 | { |
223 | return ContainerFactory.getContainer(). |
224 | getMessage( this, "illegalMinimumStringLength", locale, |
225 | new Object[] |
226 | { |
227 | minimumLength |
228 | }); |
229 | |
230 | } |
231 | |
232 | /** |
233 | * Gets the text of message <code>illegalCharacters</code>. |
234 | * <blockquote><pre>Ungültige Zeichen {0}.</pre></blockquote> |
235 | * <blockquote><pre>Illegal characters {0}.</pre></blockquote> |
236 | * |
237 | * @param locale The locale of the message instance to return. |
238 | * @param illegalCharacters Illegal characters. |
239 | * |
240 | * @return Information about illegal characters. |
241 | */ |
242 | private String getIllegalCharactersMessage( final Locale locale, |
243 | final java.lang.String illegalCharacters ) |
244 | { |
245 | return ContainerFactory.getContainer(). |
246 | getMessage( this, "illegalCharacters", locale, |
247 | new Object[] |
248 | { |
249 | illegalCharacters |
250 | }); |
251 | |
252 | } |
253 | |
254 | // </editor-fold>//GEN-END:jdtausMessages |
255 | |
256 | //----------------------------------------------------------------Messages-- |
257 | } |