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 8525 2012-05-07 08:32:23Z schulte2005 $
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 8525 2012-05-07 08:32:23Z schulte2005 $
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.invalidCharacters = invalidCharacters;
080        this.minimumLength = minimumLength;
081        this.maximumLength = maximumLength;
082    }
083
084    //----------------------------------------------------IllegalStringMessage--
085    //--Message-----------------------------------------------------------------
086
087    /**
088     * {@inheritDoc}
089     * <ul>
090     * <li>[0]: The invalid string or {@code null} if no such string is known.</li>
091     * <li>[1]: The invalid characters or {@code null} if no such characters are known.</li>
092     * <li>[2]: The minimum required length or {@code null} if no such requirement exists.</li>
093     * <li>[3]: The maximum allowed length or {@code null} if no such limit exists.</li>
094     * </ul>
095     */
096    public Object[] getFormatArguments( final Locale locale )
097    {
098        return new Object[]
099            {
100                this.invalidString, this.invalidCharacters, this.minimumLength, this.maximumLength
101            };
102
103    }
104
105    public String getText( final Locale locale )
106    {
107        final StringBuffer b = new StringBuffer( 128 );
108
109        if ( this.invalidString != null )
110        {
111            b.append( this.getIllegalStringMessage( locale, this.invalidString ) ).append( " " );
112        }
113
114        if ( this.invalidCharacters != null )
115        {
116            final StringBuffer c = new StringBuffer( this.invalidCharacters.length * 2 ).append( "[" );
117
118            for ( int i = 0, s0 = this.invalidCharacters.length; i < s0; i++ )
119            {
120                c.append( this.invalidCharacters[i] ).append( ", " );
121            }
122
123            c.setLength( c.length() - 2 );
124            c.append( "]" );
125
126            b.append( this.getIllegalCharactersMessage( locale, c.toString() ) ).append( " " );
127        }
128
129        if ( this.minimumLength != null )
130        {
131            b.append( this.getIllegalMinimumStringLengthMessage( locale, this.minimumLength ) ).append( " " );
132        }
133
134        if ( this.maximumLength != null )
135        {
136            b.append( this.getIllegalMaximumStringLengthMessage( locale, this.maximumLength ) ).append( " " );
137        }
138
139        if ( b.length() > 0 )
140        {
141            b.setLength( b.length() - 1 );
142        }
143
144        return b.toString();
145    }
146
147    //-----------------------------------------------------------------Message--
148    //--Messages----------------------------------------------------------------
149
150// <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:jdtausMessages
151    // This section is managed by jdtaus-container-mojo.
152
153    /**
154     * Gets the text of message <code>illegalString</code>.
155     * <blockquote><pre>Ungültige Zeichenkette ''{0}''.</pre></blockquote>
156     * <blockquote><pre>Illegal string ''{0}''.</pre></blockquote>
157     *
158     * @param locale The locale of the message instance to return.
159     * @param illegalString Illegal string.
160     *
161     * @return Information about an illegal string.
162     */
163    private String getIllegalStringMessage( final Locale locale,
164            final java.lang.String illegalString )
165    {
166        return ContainerFactory.getContainer().
167            getMessage( this, "illegalString", locale,
168                new Object[]
169                {
170                    illegalString
171                });
172
173    }
174
175    /**
176     * Gets the text of message <code>illegalMaximumStringLength</code>.
177     * <blockquote><pre>Mehr als {0,number} Zeichen.</pre></blockquote>
178     * <blockquote><pre>More than {0,number} characters.</pre></blockquote>
179     *
180     * @param locale The locale of the message instance to return.
181     * @param maximumLength Maximum allowed length.
182     *
183     * @return Information about an illegal maximum string length.
184     */
185    private String getIllegalMaximumStringLengthMessage( final Locale locale,
186            final java.lang.Number maximumLength )
187    {
188        return ContainerFactory.getContainer().
189            getMessage( this, "illegalMaximumStringLength", locale,
190                new Object[]
191                {
192                    maximumLength
193                });
194
195    }
196
197    /**
198     * Gets the text of message <code>illegalMinimumStringLength</code>.
199     * <blockquote><pre>Weniger als {0,number} Zeichen.</pre></blockquote>
200     * <blockquote><pre>Less than {0,number} characters.</pre></blockquote>
201     *
202     * @param locale The locale of the message instance to return.
203     * @param minimumLength Minimum required length.
204     *
205     * @return Information about an illegal minimum string length.
206     */
207    private String getIllegalMinimumStringLengthMessage( final Locale locale,
208            final java.lang.Number minimumLength )
209    {
210        return ContainerFactory.getContainer().
211            getMessage( this, "illegalMinimumStringLength", locale,
212                new Object[]
213                {
214                    minimumLength
215                });
216
217    }
218
219    /**
220     * Gets the text of message <code>illegalCharacters</code>.
221     * <blockquote><pre>Ungültige Zeichen {0}.</pre></blockquote>
222     * <blockquote><pre>Illegal characters {0}.</pre></blockquote>
223     *
224     * @param locale The locale of the message instance to return.
225     * @param illegalCharacters Illegal characters.
226     *
227     * @return Information about illegal characters.
228     */
229    private String getIllegalCharactersMessage( final Locale locale,
230            final java.lang.String illegalCharacters )
231    {
232        return ContainerFactory.getContainer().
233            getMessage( this, "illegalCharacters", locale,
234                new Object[]
235                {
236                    illegalCharacters
237                });
238
239    }
240
241// </editor-fold>//GEN-END:jdtausMessages
242
243    //----------------------------------------------------------------Messages--
244}