001/*
002 *  jDTAUS Core Messages
003 *  Copyright (C) 2005 Christian Schulte
004 *
005 *  This library is free software; you can redistribute it and/or
006 *  modify it under the terms of the GNU Lesser General Public
007 *  License as published by the Free Software Foundation; either
008 *  version 2.1 of the License, or any later version.
009 *
010 *  This library is distributed in the hope that it will be useful,
011 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
012 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
013 *  Lesser General Public License for more details.
014 *
015 *  You should have received a copy of the GNU Lesser General Public
016 *  License along with this library; if not, write to the Free Software
017 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
018 *
019 *  $JDTAUS: IllegalStringMessage.java 8738 2012-10-05 20:54:36Z schulte $
020 */
021package org.jdtaus.core.messages;
022
023import java.util.Locale;
024import org.jdtaus.core.container.ContainerFactory;
025import org.jdtaus.core.text.Message;
026
027/**
028 * Message stating that an illegal string was specified.
029 *
030 * @author Christian Schulte
031 * @version $JDTAUS: IllegalStringMessage.java 8738 2012-10-05 20:54:36Z schulte $
032 * @since 1.10
033 */
034public final class IllegalStringMessage extends Message
035{
036    //--IllegalStringMessage----------------------------------------------------
037
038    /** Serial version UID for backwards compatibility with 1.10.x classes. */
039    private static final long serialVersionUID = -8783693360487171440L;
040
041    /**
042     * The invalid string.
043     * @serial
044     */
045    private String invalidString;
046
047    /**
048     * The invalid characters of the string.
049     * @serial
050     */
051    private char[] invalidCharacters;
052
053    /**
054     * The minimum required length.
055     * @serial
056     */
057    private Number minimumLength;
058
059    /**
060     * The maximum allowed length.
061     * @serial
062     */
063    private Number maximumLength;
064
065    /**
066     * Creates a new {@code IllegalStringMessage} instance taking an invalid string, an array of invalid characters,
067     * a minimum required length and a maximum allowed length.
068     *
069     * @param invalidString The invalid string or {@code null} if no such string is known.
070     * @param invalidCharacters The invalid characters or {@code null} if no such characters are known.
071     * @param minimumLength The minimum required length or {@code null} if no such requirement exists.
072     * @param maximumLength The maximum allowed length or {@code null} if no such limit exists.
073     */
074    public IllegalStringMessage( final String invalidString, final char[] invalidCharacters,
075                                 final Number minimumLength, final Number maximumLength )
076    {
077        super();
078        this.invalidString = invalidString;
079        this.minimumLength = minimumLength;
080        this.maximumLength = maximumLength;
081
082        if ( invalidCharacters != null )
083        {
084            this.invalidCharacters = new char[ invalidCharacters.length ];
085
086            for ( int i = 0, s0 = invalidCharacters.length; i < s0; i++ )
087            {
088                this.invalidCharacters[i] = invalidCharacters[i];
089            }
090        }
091        else
092        {
093            this.invalidCharacters = null;
094        }
095    }
096
097    //----------------------------------------------------IllegalStringMessage--
098    //--Message-----------------------------------------------------------------
099
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}