Permalink
Browse files

fix issues with java import, should switch to some generic solution soon

  • Loading branch information...
1 parent 5d381da commit d0f29a26038a6b4f5d31315b3ea42de207442e70 Sven Fuchs committed Apr 18, 2010
@@ -1,5 +1,5 @@
class String
def camelize
- split(/[^a-z0-9]/i).map { |w| w.capitalize }.join
+ to_s.gsub(/\/(.?)/) { "::#{$1.upcase}" }.gsub(/(?:^|_)(.)/) { $1.upcase }
end
end unless String.method_defined?(:camelize)
@@ -1,5 +1,12 @@
class String
def underscore
- self[0, 1].downcase + self[1..-1].gsub(/[A-Z]/) { |c| "_#{c.downcase}" }
+ # self[0, 1].downcase + self[1..-1].gsub(/[A-Z]/) { |c| "_#{c.downcase}" }
+ word = to_s
+ word.gsub!(/::/, '/')
+ word.gsub!(/([A-Z]+)([A-Z][a-z])/,'\1_\2')
+ word.gsub!(/([a-z\d])([A-Z])/,'\1_\2')
+ word.tr!("-", "_")
+ word.downcase!
+ word
end
end unless String.method_defined?(:underscore)
@@ -59,7 +59,7 @@ def perform(request_settings)
end
def request_settings(request)
- url = Steam::Java::Net::Url.new(request.url)
+ url = Java::Net::Url.new(request.url)
settings = WebRequestSettings.new(url)
request.headers.each { |name, value| settings.setAdditionalHeader(name.to_s, value.to_s) } if request.headers
settings
@@ -29,7 +29,7 @@ class Connection
classifier = Version.getProductVersion == '2.6' ?
'org.apache.commons.httpclient.NameValuePair' : # HtmlUnit 2.6
'com.gargoylesoftware.htmlunit.util.NameValuePair' # HtmlUnit 2.7
- NameValuePair = Java.import(classifier, :NameValuePair)
+ NameValuePair = Java.import(classifier, 'NameValuePair')
attr_reader :connection, :java
View
@@ -33,19 +33,19 @@ def const_missing(name)
end
def import(signature, name = nil)
- init unless @initialized
- name = path_to_const_name(signature)
+ name ||= path_to_const_name(signature)
+ name.gsub!('Java::', '')
const_set_nested(name, Rjb::import(signature))
end
def path_to_const_name(path)
- path.split('.').map { |token| token.underscore.camelize }.join('::').gsub('Java::', '')
+ path.split('.').map { |token| token.underscore.camelize }.join('::')
end
def init
@initialized = true
- import('java.net.URL')
+ import('java.net.URL', 'Java::Net::Url')
import('java.lang.System')
import('java.util.Arrays')
import('java.util.ArrayList')
@@ -56,18 +56,19 @@ def init
def load_from(path)
paths = Dir["#{Steam.config[:html_unit][:java_path]}/*.jar"]
load(paths.join(':')) unless paths.empty?
+ init
end
def load(paths)
Rjb::load(paths, Steam.config[:java_load_params].to_a)
end
def logger(classifier)
- Java::Util::Logging::Logger.getLogger(classifier)
+ Util::Logging::Logger.getLogger(classifier)
end
def log_level(name)
- Java::Util::Logging::Level.send(name.to_s.upcase)
+ Util::Logging::Level.send(name.to_s.upcase)
end
end
end
View
@@ -19,7 +19,7 @@ class JavaTest < Test::Unit::TestCase
end
test "java_path_to_const_name" do
- assert_equal 'Util::ArrayList', Java.path_to_const_name("java.util.ArrayList")
+ assert_equal 'Java::Util::ArrayList', Java.path_to_const_name("java.util.ArrayList")
end
test "import" do

0 comments on commit d0f29a2

Please sign in to comment.