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

import java.io.*;
import java.net.*;

import org.litesoft.commonfoundation.typeutils.*;

import java.util.*;

import org.litesoft.commonfoundation.typeutils.Objects;

public class UrlFormatHelper
{
    private String mUrlBase;
    private Map<String, String[]> mParameters;

    public String getUrlBase()
    {
        return mUrlBase;
    }

    public Map<String, String[]> getParameters()
    {
        return mParameters;
    }

    private UrlFormatHelper( String pUrlBase, Map<String, String[]> pParameters )
    {
        mUrlBase = pUrlBase;
        mParameters = pParameters;
    }

    /**
     * Probably has a problem with keys with multiple values!
     */
    public static UrlFormatHelper decode( String pEncodedUrl )
    {
        String zUrlBase = null;
        Map<String, String[]> zParameters = new HashMap<String, String[]>();
        if ( null != (pEncodedUrl = Strings.noEmpty( pEncodedUrl )) )
        {
            int at = pEncodedUrl.indexOf( '?' );
            if ( at == -1 )
            {
                zUrlBase = pEncodedUrl;
            }
            else
            {
                zUrlBase = pEncodedUrl.substring( 0, at );
                String[] zParams = Strings.parseChar( pEncodedUrl.substring( at + 1 ).trim(), '&' );
                for ( String zParam : zParams )
                {
                    if ( (zParam = zParam.trim()).length() != 0 )
                    {
                        String zKey = null;
                        String zValue = zParam;
                        if ( -1 != (at = zParam.indexOf( '=' )) )
                        {
                            zKey = zParam.substring( 0, at ).trim();
                            zValue = zParam.substring( at + 1 ).trim();
                        }
                        decodeAndAddTo( zValue, zKey, zParameters );
                    }
                }
            }
        }
        return new UrlFormatHelper( zUrlBase, zParameters );
    }

    public static String encode( String pUrlBase, String... pKeyValueParameterPairs )
    {
        if ( !Objects.isNullOrEmpty( pKeyValueParameterPairs ) )
        {
            if ( (pKeyValueParameterPairs.length & 1) != 0 )
            {
                throw new IllegalArgumentException(
                        "Attempt to urlEncode Key Value Parameter Pairs that were NOT Key/Value pairs" );
            }

            StringBuilder sb = new StringBuilder();

            for ( int i = 0; i < pKeyValueParameterPairs.length; i += 2 )
            {
                String zKey = Strings.noEmpty( pKeyValueParameterPairs[i] );
                String zValue = encode( pKeyValueParameterPairs[i + 1] );

                if ( zKey == null )
                {
                    throw new IllegalArgumentException(
                            "urlEncode null key[" + i + "], with UrlBase: " + pUrlBase );
                }
                if ( zValue != null )
                {
                    if ( sb.length() != 0 )
                    {
                        sb.append( '&' );
                    }
                    sb.append( zKey ).append( '=' ).append( zValue );
                }
            }
            if ( sb.length() != 0 )
            {
                return pUrlBase + "?" + sb;
            }
        }
        return pUrlBase;
    }

    private static void decodeAndAddTo( String pValue, String pKey, Map<String, String[]> pParameters )
    {
        String zDecoded = pValue;
        if ( (pValue.length() != 0) )
        {
            try
            {
                zDecoded = URLDecoder.decode( pValue, "UTF-8" );
            }
            catch ( UnsupportedEncodingException e )
            {
                throw new RuntimeException( e );
            }
        }
        String[] zCurValues = pParameters.get( pKey );
        if ( zCurValues == null )
        {
            pParameters.put( pKey, new String[]{zDecoded} );
        }
        else
        {
            pParameters.put( pKey, Strings.appendString( zCurValues, zDecoded ) );
        }
    }

    private static String encode( String pField )
    {
        try
        {
            return (null != (pField = Strings.noEmpty( pField ))) ? //
                   URLEncoder.encode( pField, "UTF8" ) : null;
        }
        catch ( UnsupportedEncodingException e )
        {
            throw new RuntimeException( e );
        }
    }
}

Commits for litesoft/trunk/Java/core/jvm1.5/src/org/litesoft/util/UrlFormatHelper.java

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

Extracting commonfoundation

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
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