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
// This Source Code is in the Public Domain per: http://litesoft.org/License.txt
package org.litesoft.uispecification;

import org.litesoft.commonfoundation.typeutils.*;

/**
 * Parameter object for initializing screens. We will have many screens, so it
 * is easier to add new parameters here than to modify every factory.
 */
public class ParameterizedUriFragmentId
{
    public static final String PREFIX_DEFAULT = "-";
    public static final String PREFIX_ALTERNATE = "+";

    /**
     * We alternate between '-' and '+' for successive screens so that the "same" UriFragmentId is not the "same".
     * This forces GWT History to actually refresh (as an "Identical" History Tokens is just ignored).
     */
    public String toNewUriFragmentId( String pLastString )
    {
        String zPrefix = Strings.deNull( pLastString ).trim().startsWith( PREFIX_DEFAULT ) ? PREFIX_ALTERNATE : PREFIX_DEFAULT;
        return zPrefix + toString();
    }

    public static String removePrefix( String pPossiblyPrefixedUriFragmentId )
    {
        String rv = Strings.deNull( pPossiblyPrefixedUriFragmentId ).trim();
        if ( rv.startsWith( PREFIX_DEFAULT ) || rv.startsWith( PREFIX_ALTERNATE ) )
        {
            return rv.substring( 1 ).trim();
        }
        return rv;
    }

    private static final char PARTS_SEP = '-';
    public static final String ACCEPT_PARAM_PUNCTUATION = "!_=:;.,?|"; // Removed the PREFIXs

    public static final ParameterizedUriFragmentId DEFAULT = new ParameterizedUriFragmentId();

    private String mFragmentReference = null;
    private String mParameters = null;

    private ParameterizedUriFragmentId()
    {
    }

    public ParameterizedUriFragmentId( String pFragmentReference, String pParameters )
            throws IllegalArgumentException
    {
        mFragmentReference = Strings.noEmpty( pFragmentReference );
        if ( null != (mParameters = Strings.noEmpty( pParameters )) )
        {
            validateFragmentReference( mFragmentReference );
            validateParams( mParameters );
        }
    }

    public ParameterizedUriFragmentId( String pUriFragmentId )
            throws IllegalArgumentException
    {
        if ( null != (pUriFragmentId = Strings.noEmpty( pUriFragmentId )) )
        {
            int at = pUriFragmentId.indexOf( PARTS_SEP );
            if ( at == -1 )
            {
                validateFragmentReference( mFragmentReference = pUriFragmentId );
            }
            else
            {
                validateFragmentReference( mFragmentReference = Strings.noEmpty( pUriFragmentId.substring( 0, at ) ) );
                if ( null != (mParameters = Strings.noEmpty( pUriFragmentId.substring( at + 1 ) )) )
                {
                    validateParams( mParameters );
                }
            }
        }
    }

    public String getFragmentReference()
    {
        return mFragmentReference;
    }

    public String getParameters()
    {
        return mParameters;
    }

    @Override
    public String toString()
    {
        return (mParameters != null) //
               ? mFragmentReference + PARTS_SEP + mParameters : //
               Strings.deNull( mFragmentReference );
    }

    /**
     * Valid Parameters (by our definition) consists of nothing but characters from
     * the following ranges:
     * A-Z,a-z,0-9
     * AND the following punctuation:
     * see the constant: ACCEPT_PARAM_PUNCTUATION
     */
    private void validateParams( String pParameters )
    {
        validate( pParameters, "Parameters", ACCEPT_PARAM_PUNCTUATION );
    }

    /**
     * Valid FragmentReference (by our definition) consist of nothing but underscores and characters from
     * the following ranges:
     * A-Z,a-z,0-9
     */
    private void validateFragmentReference( String pFragmentReference )
    {
        validate( pFragmentReference, "FragmentReference", "_" );
    }

    private void validate( String pString, String pWhat, String pValidPunctuation )
    {
        for ( int i = 0; i < pString.length(); i++ )
        {
            char c = pString.charAt( i );
            if ( !Characters.isAlphaNumeric( c ) && (-1 == pValidPunctuation.indexOf( c )) )
            {
                throw new IllegalArgumentException( "Invalid character '" + c + "', at position " + i + ", in " + pWhat + ": " + pString );
            }
        }
    }

    /**
     * After leading & Trailing spaces are removes, all other spaces are removed and if a letter
     * immediately follows it, it is uppercased.
     * All characters outside of the following character ranges are converted to an underscores:
     * A-Z,a-z,0-9
     */
    public static String transformToValidFragmentReference( String pString )
    {
        if ( null != (pString = Strings.noEmpty( pString )) )
        {
            if ( Strings.isAllUppercaseOrSpaces( pString ) )
            {
                pString = pString.substring( 0, 1 ) + pString.substring( 1 ).toLowerCase();
            }
            for ( int i = 0; i < pString.length(); i++ )
            {
                if ( !Characters.isAlphaNumeric( pString.charAt( i ) ) )
                {
                    return transform( new StringBuilder( pString ), i ).toString();
                }
            }
        }
        return pString;
    }

    private static StringBuilder transform( StringBuilder pSB, int i )
    {
        while ( i < pSB.length() )
        {
            char c = pSB.charAt( i );
            if ( Characters.isAlphaNumeric( c ) )
            {
                i++;
            }
            else if ( c != ' ' )
            {
                pSB.setCharAt( i++, '_' );
            }
            else
            {
                pSB.deleteCharAt( i );
                c = pSB.charAt( i );
                if ( ('a' <= c) && (c <= 'z') )
                {
                    pSB.setCharAt( i++, Character.toUpperCase( c ) );
                }
            }
        }
        return pSB;
    }
}

Commits for litesoft/trunk/Java/core/Anywhere/src/org/litesoft/uispecification/ParameterizedUriFragmentId.java

Diff revisions: vs.
Revision Author Commited Message
939 Diff Diff GeorgeS picture GeorgeS Mon 02 Jun, 2014 21:30:31 +0000

Extracting commonfoundation

811 Diff Diff GeorgeS picture GeorgeS Sat 18 Aug, 2012 13:45:18 +0000
802 Diff Diff GeorgeS picture GeorgeS Wed 15 Aug, 2012 04:04:47 +0000
801 Diff Diff GeorgeS picture GeorgeS Wed 15 Aug, 2012 03:59:02 +0000
49 Diff Diff GeorgeS picture GeorgeS Mon 12 Apr, 2010 02:59:10 +0000

License Text

2 GeorgeS picture GeorgeS Sun 07 Feb, 2010 12:50:58 +0000