Skip to content

Commit

Permalink
correctly handle Charset.forName fails (null argument never happens)
Browse files Browse the repository at this point in the history
also use enum values instead of wrapping it to a set (to find encoding)
  • Loading branch information
kares committed Feb 1, 2017
1 parent da45d3e commit 830e5ea
Showing 1 changed file with 15 additions and 17 deletions.
32 changes: 15 additions & 17 deletions ext/java/nokogiri/HtmlSaxParserContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import java.io.InputStream;
import java.nio.charset.Charset;
import java.nio.charset.IllegalCharsetNameException;
import java.nio.charset.UnsupportedCharsetException;
import java.util.EnumSet;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
Expand All @@ -46,10 +47,7 @@

import org.apache.xerces.parsers.AbstractSAXParser;
import org.cyberneko.html.parsers.SAXParser;
import org.jruby.Ruby;
import org.jruby.RubyClass;
import org.jruby.RubyFixnum;
import org.jruby.RubyString;
import org.jruby.*;
import org.jruby.anno.JRubyClass;
import org.jruby.anno.JRubyMethod;
import org.jruby.runtime.ThreadContext;
Expand Down Expand Up @@ -104,7 +102,7 @@ public static IRubyObject parse_memory(ThreadContext context,
return ctx;
}

public static enum EncodingType {
public enum EncodingType {
NONE(0, "NONE"),
UTF_8(1, "UTF-8"),
UTF16LE(2, "UTF16LE"),
Expand All @@ -131,6 +129,7 @@ public static enum EncodingType {

private final int value;
private final String name;

EncodingType(int value, String name) {
this.value = value;
this.name = name;
Expand All @@ -145,9 +144,8 @@ public String toString() {
}
}

private static String findName(int value) {
EnumSet<EncodingType> set = EnumSet.allOf(EncodingType.class);
for (EncodingType type : set) {
private static String findName(final int value) {
for (EncodingType type : EncodingType.values()) {
if (type.getValue() == value) return type.toString();
}
return null;
Expand All @@ -157,20 +155,20 @@ private static String findEncoding(ThreadContext context, IRubyObject encoding)
String rubyEncoding = null;
if (encoding instanceof RubyString) {
rubyEncoding = rubyStringToString(encoding);
} else if (encoding instanceof RubyFixnum) {
}
else if (encoding instanceof RubyFixnum) {
int value = RubyFixnum.fix2int((RubyFixnum) encoding);
rubyEncoding = findName(value);
}
if (rubyEncoding == null) return null;
try {
Charset charset = Charset.forName(rubyEncoding);
return charset.displayName();
} catch (IllegalCharsetNameException e) {
throw context.getRuntime().newEncodingCompatibilityError(
rubyEncoding + "is not supported in Java.");
} catch (IllegalArgumentException e) {
throw context.getRuntime().newInvalidEncoding(
"encoding should not be nil");
return Charset.forName(rubyEncoding).displayName();
}
catch (UnsupportedCharsetException e) {
throw context.getRuntime().newEncodingCompatibilityError(rubyEncoding + "is not supported");
}
catch (IllegalCharsetNameException e) {
throw context.getRuntime().newInvalidEncoding(e.getMessage());
}
}

Expand Down

0 comments on commit 830e5ea

Please sign in to comment.