Subversion Repository Public Repository

litesoft

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
// This Source Code is in the Public Domain per: http://litesoft.org/License.txt
package org.litesoft.core.util;

import java.util.*;

import org.litesoft.charstreams.*;
import org.litesoft.core.simpletypes.nonpublic.*;
import org.litesoft.core.simpletypes.temporal.*;
import org.litesoft.core.simpletypes.temporal.nonpublic.*;
import org.litesoft.core.typeutils.*;

public class UtilsCommon extends EqualSupport
{
    protected UtilsCommon()
    {
    }

    public static String cvtTextForDisplay( String pText )
    {
        if ( pText == null )
        {
            return "[null]";
        }
        int length = pText.length();
        StringBuilder sb = new StringBuilder( length );
        for ( int i = 0; i < length; i++ )
        {
            char c = pText.charAt( i );
            switch ( c )
            {
                case '[':
                case ']':
                case '\\':
                case '"':
                    sb.append( '\\' );
                    sb.append( c );
                    break;
                case '\n':
                    sb.append( "\\n" );
                    break;
                case '\r':
                    sb.append( "\\r" );
                    break;
                case '\t':
                    sb.append( "\\t" );
                    break;
                case '\f':
                    sb.append( "\\f" );
                    break;
                default:
                    if ( (' ' <= c) && (c < 127) )
                    {
                        sb.append( c );
                        break;
                    }
                    sb.append( '[' );
                    sb.append( Characters.cvtCharForDisplay( c ) );
                    sb.append( ']' );
                    break;
            }
        }
        return sb.toString();
    }

    public static Object[] convert( int pEntries, Enumeration<Object> pEnumeration )
    {
        Object[] them = new Object[pEntries];
        for ( int i = 0; (i < pEntries) && pEnumeration.hasMoreElements(); i++ )
        {
            them[i] = pEnumeration.nextElement();
        }
        return them;
    }

    // Groups

    // Object Group:

    @SuppressWarnings({"unchecked", "ManualArrayToCollectionCopy", "MismatchedQueryAndUpdateOfCollection"})
    public static <T> Collection<T> convert( Collection<T> pCollectionToFill, Object[] pObjects )
    {
        Collection<Object> toFill = (Collection<Object>) pCollectionToFill;
        for ( Object object : pObjects )
        {
            toFill.add( object );
        }
        return pCollectionToFill;
    }

    public static void appendClassName( StringBuilder pBuffer, int pTabLevel, String pLabel, Object pValue )
    {
        append( pBuffer, pTabLevel, pLabel, (pValue == null) ? null : pValue.getClass().getName() );
    }

    public static void append( StringBuilder pBuffer, int pTabLevel, String pLabel, Object pValue )
    {
        append( pBuffer, pTabLevel, pLabel );
        pBuffer.append( ':' );
        pBuffer.append( (pValue == null) ? "null" : pValue.toString() );
    }

    public static void append( StringBuilder pBuffer, int pTabLevel, String pValue )
    {
        pBuffer.append( '\n' );
        while ( pTabLevel-- >= 1 )
        {
            pBuffer.append( '\t' );
        }
        pBuffer.append( (pValue == null) ? "null" : pValue );
    }

    public static String normalizeCtrlChars( String pText )
    {
        if ( pText != null )
        {
            for ( int i = pText.length(); i-- > 0; )
            {
                if ( pText.charAt( i ) < ' ' )
                {
                    StringBuilder sb = new StringBuilder( pText );
                    for (; i >= 0; i-- )
                    {
                        char c = sb.charAt( i );
                        if ( c < ' ' )
                        {
                            switch ( c )
                            {
                                case '\n':
                                case '\r':
                                case '\f':
                                    break;
                                case '\t':
                                    sb.setCharAt( i, ' ' );
                                    break;
                                default:
                                    sb.setCharAt( i, '.' );
                                    break;
                            }
                        }
                        pText = sb.toString();
                    }
                    break;
                }
            }
        }
        return pText;
    }

    /**
     * Given path(s) that may be system dependent, create a path
     * that is converted to a system independent form (forward Slashes).
     * Note: Ignore null entries (null, or all nulls returns null).
     */
    public static String forwardSlashPath( String... pPaths )
    {
        StringBuilder sb = null;
        if ( pPaths != null )
        {
            for ( String path : pPaths )
            {
                if ( path != null )
                {
                    if ( sb == null )
                    {
                        sb = new StringBuilder();
                    }
                    if ( path.length() != 0 )
                    {
                        String fsp = Paths.forwardSlash( path );
                        if ( sb.length() == 0 )
                        {
                            sb.append( fsp );
                        }
                        else
                        {
                            boolean pStartsWithSlash = (fsp.charAt( 0 ) == '/');
                            if ( sb.charAt( sb.length() - 1 ) == '/' )
                            {
                                sb.append( pStartsWithSlash ? fsp.substring( 1 ) : fsp );
                            }
                            else
                            {
                                if ( !pStartsWithSlash )
                                {
                                    sb.append( '/' );
                                }
                                sb.append( fsp );
                            }
                        }
                    }
                }
            }
        }
        return (sb == null) ? null : sb.toString();
    }

    /**
     * @param pText !null
     */
    public static void formatToJavaSourceString( StringBuilder pSB, String pText )
    {
        LowLevelEncode( pSB, new CharSourceFromSequence( pText ), true );
    }

    public static String encodeToConstrainedASCII( String pText )
    {
        if ( Strings.isNullOrEmpty( pText ) )
        {
            return pText;
        }
        return LowLevelEncode( new StringBuilder(), new CharSourceFromSequence( pText, 0 ), true ).toString();
    }

    public static String decodeFromConstrainedASCII( String pText )
    {
        if ( Strings.isNullOrEmpty( pText ) )
        {
            return pText;
        }
        return LowLevelDecode( new StringBuilder(), new CharSourceFromSequence( pText, 0 ), false ).toString();
    }

    private static StringBuilder LowLevelEncode( StringBuilder pSB, CharSource pSource, boolean pDoubleQuoteSpecial )
    {
        for ( int c; -1 != (c = pSource.get()); )
        {
            if ( pDoubleQuoteSpecial && (c == '"') )
            {
                pSB.append( '\\' );
                pSB.append( (char) c );
            }
            else
            {
                switch ( c )
                {
                    case '\\':
                        pSB.append( '\\' );
                        pSB.append( '\\' );
                        break;
                    case '\n':
                        pSB.append( '\\' );
                        pSB.append( 'n' );
                        break;
                    case '\r':
                        pSB.append( '\\' );
                        pSB.append( 'r' );
                        break;
                    case '\t':
                        pSB.append( '\\' );
                        pSB.append( 't' );
                        break;
                    default:
                        if ( (' ' <= c) && (c < 127) )
                        {
                            pSB.append( (char) c );
                            break;
                        }
                        pSB.append( '\\' );
                        pSB.append( 'x' );
                        pSB.append( Hex.toChar( c >> 24 ) );
                        pSB.append( Hex.toChar( c >> 16 ) );
                        pSB.append( Hex.toChar( c >> 8 ) );
                        pSB.append( Hex.toChar( c ) );
                        break;
                }
            }
        }
        return pSB;
    }

    private static StringBuilder LowLevelDecode( StringBuilder pSB, CharSource pSource, boolean pDoubleQuoteSpecial )
    {
        for ( int c; -1 != (c = pSource.get()); )
        {
            if ( (c < ' ') || (127 <= c) )
            {
                throw new IllegalArgumentException( "Decoding Error: Char[" + pSource.getLastOffset() + "] Not between ' ' & Ascii 126 (inclusive)" );
            }
            if ( c != '\\' )
            {
                pSB.append( (char) c );
                continue;
            }
            if ( -1 == (c = pSource.get()) )
            {
                throw new IllegalArgumentException( "Decoding Error: No Char after escape \\" );
            }
            if ( pDoubleQuoteSpecial && (c == '"') )
            {
                pSB.append( (char) c );
            }
            else
            {
                switch ( c )
                {
                    case '\\':
                        pSB.append( (char) c );
                        break;
                    case 'n':
                        pSB.append( '\n' );
                        break;
                    case 'r':
                        pSB.append( '\r' );
                        break;
                    case 't':
                        pSB.append( '\t' );
                        break;
                    case 'x':
                        int c1 = pSource.get();
                        int c2 = pSource.get();
                        int c3 = pSource.get();
                        int c4 = pSource.get();
                        if ( c4 == -1 )
                        {
                            throw new IllegalArgumentException( "Decoding Error: Not enough Chars after \\x" );
                        }
                        try
                        {
                            int zChar = (Hex.fromCharChecked( (char) c1 ) << 24) + //
                                        (Hex.fromCharChecked( (char) c2 ) << 16) + //
                                        (Hex.fromCharChecked( (char) c3 ) << 8) + //
                                        Hex.fromCharChecked( (char) c4 );
                            pSB.append( (char) zChar );
                        }
                        catch ( IllegalArgumentException e )
                        {
                            throw new IllegalArgumentException( "Decoding Error: After \\x expected 4 hex digits: " + e.getMessage() );
                        }
                        break;
                    default:
                        throw new IllegalArgumentException( "Decoding Error: Invalid Char '" + c + "' after escape \\" );
                }
            }
        }
        return pSB;
    }

    // Array / varArgs

    // Objects:

    // Object Group:

    public static SimpleDate forceEndOfMonth( SimpleDate pSimpleDate )
    {
        if ( pSimpleDate != null )
        {
            int zDaysInMonth = CalendarSupport.daysInMonth( pSimpleDate.getYear(), pSimpleDate.getMonth() );
            if ( pSimpleDate.getDay() != zDaysInMonth )
            {
                pSimpleDate.setDay( zDaysInMonth );
            }
        }
        return pSimpleDate;
    }
}

Commits for litesoft/trunk/Java/core/Anywhere/src/org/litesoft/core/util/UtilsCommon.java

Diff revisions: vs.
Revision Author Commited Message
822 Diff Diff GeorgeS picture GeorgeS Sun 19 Aug, 2012 01:03:51 +0000
821 Diff Diff GeorgeS picture GeorgeS Sun 19 Aug, 2012 00:08:41 +0000
819 Diff Diff GeorgeS picture GeorgeS Sat 18 Aug, 2012 18:09:40 +0000
811 Diff Diff GeorgeS picture GeorgeS Sat 18 Aug, 2012 13:45:18 +0000
810 Diff Diff GeorgeS picture GeorgeS Thu 16 Aug, 2012 04:16:07 +0000
809 Diff Diff GeorgeS picture GeorgeS Thu 16 Aug, 2012 04:10:46 +0000
806 Diff Diff GeorgeS picture GeorgeS Thu 16 Aug, 2012 03:48:22 +0000
805 Diff Diff GeorgeS picture GeorgeS Wed 15 Aug, 2012 12:53:09 +0000
804 Diff Diff GeorgeS picture GeorgeS Wed 15 Aug, 2012 12:48:51 +0000
803 GeorgeS picture GeorgeS Wed 15 Aug, 2012 04:08:34 +0000