Skip to content

Commit d42ced4

Browse files
author
jossonsmith
committed
Fixed String#equalsIgnoreCase bug
Add java.net.URLDecoder/URLEncoder, java.util.Arrays (Partial API)
1 parent b8d84cc commit d42ced4

File tree

5 files changed

+329
-3
lines changed

5 files changed

+329
-3
lines changed

sources/net.sf.j2s.java.core/src/java/lang/String.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -147,8 +147,8 @@ String.prototype.equals = function (anObject) {
147147
};
148148

149149
String.prototype.equalsIgnoreCase = function (anotherString) {
150-
return this == anotherString
151-
|| this.toLowerCase () == anotherString.toLowerCase ();
150+
return (anotherString == null) ? false : (this == anotherString
151+
|| this.toLowerCase () == anotherString.toLowerCase ());
152152
};
153153

154154
/* private */
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
/* Copyright 1998, 2005 The Apache Software Foundation or its licensors, as applicable
2+
*
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS,
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
15+
16+
package java.net;
17+
18+
19+
//import java.io.ByteArrayOutputStream;
20+
import java.io.UnsupportedEncodingException;
21+
22+
/*
23+
import org.apache.harmony.luni.util.Msg;
24+
import org.apache.harmony.luni.util.Util;
25+
*/
26+
27+
/**
28+
* This class is used to decode a string which is encoded in the
29+
* <code>application/x-www-form-urlencoded</code> MIME content type.
30+
*/
31+
public class URLDecoder {
32+
33+
/**
34+
* Decodes the string argument which is assumed to be encoded in the
35+
* <code>x-www-form-urlencoded</code> MIME content type.
36+
* <p>
37+
* '+' will be converted to space, '%' and two following hex digit
38+
* characters are converted to the equivalent byte value. All other
39+
* characters are passed through unmodified.
40+
* <p>
41+
* e.g. "A+B+C %24%25" -> "A B C $%"
42+
*
43+
* @param s
44+
* java.lang.String The encoded string.
45+
* @return java.lang.String The decoded version.
46+
*
47+
* @deprecated use URLDecoder#decode(String, String) instead
48+
*
49+
* @j2sNative
50+
* return decodeURIComponent(arguments[0]);
51+
*/
52+
public static String decode(String s) {
53+
// return Util.decode(s, true);
54+
return null;
55+
}
56+
57+
/**
58+
* Decodes the string argument which is assumed to be encoded in the
59+
* <code>x-www-form-urlencoded</code> MIME content type using the
60+
* specified encoding scheme.
61+
* <p>
62+
* '+' will be converted to space, '%' and two following hex digit
63+
* characters are converted to the equivalent byte value. All other
64+
* characters are passed through unmodified.
65+
*
66+
* <p>
67+
* e.g. "A+B+C %24%25" -> "A B C $%"
68+
*
69+
* @param s
70+
* java.lang.String The encoded string.
71+
* @param enc
72+
* java.lang.String The encoding scheme to use
73+
* @return java.lang.String The decoded version.
74+
*/
75+
public static String decode(String s, String enc)
76+
throws UnsupportedEncodingException {
77+
78+
if (enc == null) {
79+
throw new NullPointerException();
80+
}
81+
82+
/**
83+
* @j2sNative
84+
* return decodeURIComponent(arguments[0]);
85+
*/ {}
86+
/*
87+
// If the given encoding is an empty string throw an exception.
88+
if (enc.length() == 0) {
89+
throw new UnsupportedEncodingException(Msg
90+
.getString("K00a5", "enc")); //$NON-NLS-1$
91+
}
92+
93+
StringBuffer result = new StringBuffer(s.length());
94+
ByteArrayOutputStream out = new ByteArrayOutputStream();
95+
for (int i = 0; i < s.length();) {
96+
char c = s.charAt(i);
97+
if (c == '+')
98+
result.append(' ');
99+
else if (c == '%') {
100+
out.reset();
101+
do {
102+
if (i + 2 >= s.length())
103+
throw new IllegalArgumentException(Msg.getString(
104+
"K01fe", i));
105+
int d1 = Character.digit(s.charAt(i + 1), 16);
106+
int d2 = Character.digit(s.charAt(i + 2), 16);
107+
if (d1 == -1 || d2 == -1)
108+
throw new IllegalArgumentException(Msg.getString(
109+
"K01ff", s.substring(i, i + 3), String
110+
.valueOf(i)));
111+
out.write((byte) ((d1 << 4) + d2));
112+
i += 3;
113+
} while (i < s.length() && s.charAt(i) == '%');
114+
result.append(out.toString(enc));
115+
continue;
116+
} else
117+
result.append(c);
118+
i++;
119+
}
120+
return result.toString();
121+
*/
122+
return null;
123+
}
124+
}
Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
/* Copyright 1998, 2005 The Apache Software Foundation or its licensors, as applicable
2+
*
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS,
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
15+
16+
package java.net;
17+
18+
19+
import java.io.UnsupportedEncodingException;
20+
21+
/**
22+
* This class is used to encode a string using the format required by
23+
* <code>application/x-www-form-urlencoded</code> MIME content type.
24+
*/
25+
public class URLEncoder {
26+
static final String digits = "0123456789ABCDEF";
27+
28+
/**
29+
* Prevents this class from being instantiated.
30+
*
31+
*/
32+
private URLEncoder() {
33+
}
34+
35+
/**
36+
* This class contains a utility method for converting a string to the
37+
* format required by the <code>application/x-www-form-urlencoded</code>
38+
* MIME content type.
39+
* <p>
40+
* All characters except letters ('a'..'z', 'A'..'Z') and numbers ('0'..'9')
41+
* and characters '.', '-', '*', '_' are converted into their hexidecimal
42+
* value prepended by '%'.
43+
* <p>
44+
* For example: '#' -> %23
45+
* <p>
46+
* In addition, spaces are substituted by '+'
47+
*
48+
* @return java.lang.String the string to be converted
49+
* @param s
50+
* java.lang.String the converted string
51+
*
52+
* @deprecated use URLEncoder#encode(String, String) instead
53+
*
54+
* @j2sNative
55+
* return encodeURIComponent(arguments[0]);
56+
*/
57+
public static String encode(String s) {
58+
StringBuffer buf = new StringBuffer();
59+
for (int i = 0; i < s.length(); i++) {
60+
char ch = s.charAt(i);
61+
if ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z')
62+
|| (ch >= '0' && ch <= '9') || ".-*_".indexOf(ch) > -1)
63+
buf.append(ch);
64+
else if (ch == ' ')
65+
buf.append('+');
66+
else {
67+
byte[] bytes = new String(new char[] { ch }).getBytes();
68+
for (int j = 0; j < bytes.length; j++) {
69+
buf.append('%');
70+
buf.append(digits.charAt((bytes[j] & 0xf0) >> 4));
71+
buf.append(digits.charAt(bytes[j] & 0xf));
72+
}
73+
}
74+
}
75+
return buf.toString();
76+
}
77+
78+
/**
79+
* This class contains a utility method for converting a string to the
80+
* format required by the <code>application/x-www-form-urlencoded</code>
81+
* MIME content type.
82+
* <p>
83+
* All characters except letters ('a'..'z', 'A'..'Z') and numbers ('0'..'9')
84+
* and characters '.', '-', '*', '_' are converted into their hexadecimal
85+
* value prepended by '%'.
86+
* <p>
87+
* For example: '#' -> %23
88+
* <p>
89+
* In addition, spaces are substituted by '+'
90+
*
91+
* @return java.lang.String the string to be converted
92+
* @param s
93+
* java.lang.String the converted string
94+
*
95+
* @j2sNative
96+
* return encodeURIComponent(arguments[0]);
97+
*/
98+
public static String encode(String s, String enc)
99+
throws UnsupportedEncodingException {
100+
if (enc == null) {
101+
throw new NullPointerException();
102+
}
103+
// check for UnsupportedEncodingException
104+
"".getBytes(enc);
105+
106+
StringBuffer buf = new StringBuffer();
107+
int start = -1;
108+
for (int i = 0; i < s.length(); i++) {
109+
char ch = s.charAt(i);
110+
if ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z')
111+
|| (ch >= '0' && ch <= '9') || " .-*_".indexOf(ch) > -1) {
112+
if (start >= 0) {
113+
convert(s.substring(start, i), buf, enc);
114+
start = -1;
115+
}
116+
if (ch != ' ') {
117+
buf.append(ch);
118+
} else {
119+
buf.append('+');
120+
}
121+
} else {
122+
if (start < 0) {
123+
start = i;
124+
}
125+
}
126+
}
127+
if (start >= 0) {
128+
convert(s.substring(start, s.length()), buf, enc);
129+
}
130+
return buf.toString();
131+
}
132+
133+
/**
134+
* @j2sIgnore
135+
*/
136+
private static void convert(String s, StringBuffer buf, String enc)
137+
throws UnsupportedEncodingException {
138+
byte[] bytes = s.getBytes(enc);
139+
for (int j = 0; j < bytes.length; j++) {
140+
buf.append('%');
141+
buf.append(digits.charAt((bytes[j] & 0xf0) >> 4));
142+
buf.append(digits.charAt(bytes[j] & 0xf));
143+
}
144+
}
145+
}

sources/net.sf.j2s.java.core/src/java/package.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
(function () {
22
ClazzLoader.registerPackages ("java", [
3-
"io", "lang", "lang.reflect", "util"]);
3+
"io", "lang", "lang.reflect", "util", "net"]);
44

55
window["reflect"] = java.lang.reflect;
66

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*******************************************************************************
2+
* Java2Script Pacemaker (http://j2s.sourceforge.net)
3+
*
4+
* Copyright (c) 2006 ognize.com and others.
5+
* All rights reserved. This program and the accompanying materials
6+
* are made available under the terms of the Eclipse Public License v1.0
7+
* which accompanies this distribution, and is available at
8+
* http://www.eclipse.org/legal/epl-v10.html
9+
*
10+
* Contributors:
11+
* ognize.com - initial API and implementation
12+
*******************************************************************************/
13+
14+
package java.util;
15+
16+
/**
17+
* @author josson smith
18+
*
19+
* 2006-8-27
20+
*/
21+
public class Arrays {
22+
23+
/**
24+
* Returns <tt>true</tt> if the two specified arrays of Objects are
25+
* <i>equal</i> to one another. The two arrays are considered equal if
26+
* both arrays contain the same number of elements, and all corresponding
27+
* pairs of elements in the two arrays are equal. Two objects <tt>e1</tt>
28+
* and <tt>e2</tt> are considered <i>equal</i> if <tt>(e1==null ? e2==null
29+
* : e1.equals(e2))</tt>. In other words, the two arrays are equal if
30+
* they contain the same elements in the same order. Also, two array
31+
* references are considered equal if both are <tt>null</tt>.<p>
32+
*
33+
* @param a one array to be tested for equality.
34+
* @param a2 the other array to be tested for equality.
35+
* @return <tt>true</tt> if the two arrays are equal.
36+
*/
37+
public static boolean equals(Object[] a, Object[] a2) {
38+
if (a==a2)
39+
return true;
40+
if (a==null || a2==null)
41+
return false;
42+
43+
int length = a.length;
44+
if (a2.length != length)
45+
return false;
46+
47+
for (int i=0; i<length; i++) {
48+
Object o1 = a[i];
49+
Object o2 = a2[i];
50+
if (!(o1==null ? o2==null : o1.equals(o2)))
51+
return false;
52+
}
53+
54+
return true;
55+
}
56+
57+
}

0 commit comments

Comments
 (0)