| 1 |
|
package org.json; |
| 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 |
|
@author |
| 32 |
|
@version |
| 33 |
|
|
| |
|
| 0% |
Uncovered Elements: 90 (90) |
Complexity: 23 |
Complexity Density: 0.38 |
|
| 34 |
|
public class Cookie |
| 35 |
|
{ |
| 36 |
|
|
| 37 |
|
|
| 38 |
|
|
| 39 |
|
|
| 40 |
|
|
| 41 |
|
|
| 42 |
|
|
| 43 |
|
|
| 44 |
|
|
| 45 |
|
|
| 46 |
|
|
| 47 |
|
@param |
| 48 |
|
|
| 49 |
|
@return |
| 50 |
|
|
| |
|
| 0% |
Uncovered Elements: 16 (16) |
Complexity: 7 |
Complexity Density: 0.58 |
|
| 51 |
0 |
public static String escape(String string)... |
| 52 |
|
{ |
| 53 |
0 |
char c; |
| 54 |
0 |
String s = string.trim(); |
| 55 |
0 |
int length = s.length(); |
| 56 |
0 |
StringBuilder sb = new StringBuilder(length); |
| 57 |
0 |
for (int i = 0; i < length; i += 1) |
| 58 |
|
{ |
| 59 |
0 |
c = s.charAt(i); |
| 60 |
0 |
if (c < ' ' || c == '+' || c == '%' || c == '=' || c == ';') |
| 61 |
|
{ |
| 62 |
0 |
sb.append('%'); |
| 63 |
0 |
sb.append(Character.forDigit((char) ((c >>> 4) & 0x0f), 16)); |
| 64 |
0 |
sb.append(Character.forDigit((char) (c & 0x0f), 16)); |
| 65 |
|
} |
| 66 |
|
else |
| 67 |
|
{ |
| 68 |
0 |
sb.append(c); |
| 69 |
|
} |
| 70 |
|
} |
| 71 |
0 |
return sb.toString(); |
| 72 |
|
} |
| 73 |
|
|
| 74 |
|
|
| 75 |
|
|
| 76 |
|
|
| 77 |
|
|
| 78 |
|
|
| 79 |
|
|
| 80 |
|
|
| 81 |
|
|
| 82 |
|
|
| 83 |
|
|
| 84 |
|
@param |
| 85 |
|
|
| 86 |
|
@return |
| 87 |
|
|
| 88 |
|
@throws |
| 89 |
|
|
| |
|
| 0% |
Uncovered Elements: 24 (24) |
Complexity: 4 |
Complexity Density: 0.22 |
|
| 90 |
0 |
public static JSONObject toJSONObject(String string) throws JSONException... |
| 91 |
|
{ |
| 92 |
0 |
String name; |
| 93 |
0 |
JSONObject jo = new JSONObject(); |
| 94 |
0 |
Object value; |
| 95 |
0 |
JSONTokener x = new JSONTokener(string); |
| 96 |
0 |
jo.put("name", x.nextTo('=')); |
| 97 |
0 |
x.next('='); |
| 98 |
0 |
jo.put("value", x.nextTo(';')); |
| 99 |
0 |
x.next(); |
| 100 |
0 |
while (x.more()) |
| 101 |
|
{ |
| 102 |
0 |
name = unescape(x.nextTo("=;")); |
| 103 |
0 |
if (x.next() != '=') |
| 104 |
|
{ |
| 105 |
0 |
if (name.equals("secure")) |
| 106 |
|
{ |
| 107 |
0 |
value = Boolean.TRUE; |
| 108 |
|
} |
| 109 |
|
else |
| 110 |
|
{ |
| 111 |
0 |
throw x.syntaxError("Missing '=' in cookie parameter."); |
| 112 |
|
} |
| 113 |
|
} |
| 114 |
|
else |
| 115 |
|
{ |
| 116 |
0 |
value = unescape(x.nextTo(';')); |
| 117 |
0 |
x.next(); |
| 118 |
|
} |
| 119 |
0 |
jo.put(name, value); |
| 120 |
|
} |
| 121 |
0 |
return jo; |
| 122 |
|
} |
| 123 |
|
|
| 124 |
|
|
| 125 |
|
|
| 126 |
|
|
| 127 |
|
|
| 128 |
|
|
| 129 |
|
|
| 130 |
|
@param |
| 131 |
|
|
| 132 |
|
@return |
| 133 |
|
@throws |
| 134 |
|
|
| |
|
| 0% |
Uncovered Elements: 24 (24) |
Complexity: 5 |
Complexity Density: 0.31 |
|
| 135 |
0 |
public static String toString(JSONObject jo) throws JSONException... |
| 136 |
|
{ |
| 137 |
0 |
StringBuilder sb = new StringBuilder(); |
| 138 |
|
|
| 139 |
0 |
sb.append(escape(jo.getString("name"))); |
| 140 |
0 |
sb.append("="); |
| 141 |
0 |
sb.append(escape(jo.getString("value"))); |
| 142 |
0 |
if (jo.has("expires")) |
| 143 |
|
{ |
| 144 |
0 |
sb.append(";expires="); |
| 145 |
0 |
sb.append(jo.getString("expires")); |
| 146 |
|
} |
| 147 |
0 |
if (jo.has("domain")) |
| 148 |
|
{ |
| 149 |
0 |
sb.append(";domain="); |
| 150 |
0 |
sb.append(escape(jo.getString("domain"))); |
| 151 |
|
} |
| 152 |
0 |
if (jo.has("path")) |
| 153 |
|
{ |
| 154 |
0 |
sb.append(";path="); |
| 155 |
0 |
sb.append(escape(jo.getString("path"))); |
| 156 |
|
} |
| 157 |
0 |
if (jo.optBoolean("secure")) |
| 158 |
|
{ |
| 159 |
0 |
sb.append(";secure"); |
| 160 |
|
} |
| 161 |
0 |
return sb.toString(); |
| 162 |
|
} |
| 163 |
|
|
| 164 |
|
|
| 165 |
|
|
| 166 |
|
|
| 167 |
|
|
| 168 |
|
@param |
| 169 |
|
|
| 170 |
|
|
| 171 |
|
|
| 172 |
|
@return |
| 173 |
|
|
| |
|
| 0% |
Uncovered Elements: 22 (22) |
Complexity: 7 |
Complexity Density: 0.5 |
|
| 174 |
0 |
public static String unescape(String string)... |
| 175 |
|
{ |
| 176 |
0 |
int length = string.length(); |
| 177 |
0 |
StringBuilder sb = new StringBuilder(length); |
| 178 |
0 |
for (int i = 0; i < length; ++i) |
| 179 |
|
{ |
| 180 |
0 |
char c = string.charAt(i); |
| 181 |
0 |
if (c == '+') |
| 182 |
|
{ |
| 183 |
0 |
c = ' '; |
| 184 |
|
} |
| 185 |
0 |
else if (c == '%' && i + 2 < length) |
| 186 |
|
{ |
| 187 |
0 |
int d = JSONTokener.dehexchar(string.charAt(i + 1)); |
| 188 |
0 |
int e = JSONTokener.dehexchar(string.charAt(i + 2)); |
| 189 |
0 |
if (d >= 0 && e >= 0) |
| 190 |
|
{ |
| 191 |
0 |
c = (char) (d * 16 + e); |
| 192 |
0 |
i += 2; |
| 193 |
|
} |
| 194 |
|
} |
| 195 |
0 |
sb.append(c); |
| 196 |
|
} |
| 197 |
0 |
return sb.toString(); |
| 198 |
|
} |
| 199 |
|
} |