coder 0.0.1 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,10 +1,11 @@
1
1
  require 'coder/cleaner/builtin'
2
2
  require 'coder/cleaner/iconv'
3
+ require 'coder/cleaner/java'
3
4
  require 'coder/cleaner/simple'
4
5
 
5
6
  module Coder
6
7
  module Cleaner
7
- Default = [Builtin, Iconv, Simple].detect { |c| c.available? }
8
+ Default = [Builtin, Java, Iconv, Simple].detect { |c| c.available? }
8
9
 
9
10
  def self.new(encoding)
10
11
  Default.new(encoding)
@@ -6,6 +6,14 @@ module Coder
6
6
  OPTIONS = { :undef => :replace, :invalid => :replace, :replace => "" }
7
7
 
8
8
  def self.available?
9
+ has_encoding? and mri?
10
+ end
11
+
12
+ def self.mri?
13
+ !defined?(RUBY_ENGINE) or RUBY_ENGINE == 'ruby'
14
+ end
15
+
16
+ def self.has_encoding?
9
17
  defined? Encoding.find and
10
18
  defined? EncodingError and
11
19
  String.method_defined? :encode and
@@ -20,7 +20,7 @@ module Coder
20
20
  def self.available?
21
21
  load_iconv
22
22
  !!::Iconv.conv("iso-8859-1//ignore", "utf-8", "\305\253" + "a"*8160)
23
- rescue Exception => e
23
+ rescue Exception
24
24
  false
25
25
  end
26
26
 
@@ -0,0 +1,33 @@
1
+ require 'coder/error'
2
+
3
+ module Coder
4
+ module Cleaner
5
+ class Java
6
+ def self.available?
7
+ require 'java'
8
+ !!::Java::JavaNioCharset::Charset
9
+ rescue
10
+ false
11
+ end
12
+
13
+ def initialize(encoding)
14
+ encoding = encoding.to_s.upcase
15
+ @charset = ::Java::JavaNioCharset::Charset.for_name(encoding)
16
+ @decoder = @charset.new_decoder
17
+ @decoder.on_malformed_input(::Java::JavaNioCharset::CodingErrorAction::IGNORE)
18
+ @decoder.on_unmappable_character(::Java::JavaNioCharset::CodingErrorAction::IGNORE)
19
+ rescue ::Java::JavaNioCharset::UnsupportedCharsetException
20
+ raise Coder::InvalidEncoding, "unknown encoding name - #{encoding}"
21
+ rescue Java::JavaLang::RuntimeException => e
22
+ raise Coder::Error, e.message, e.backtrace
23
+ end
24
+
25
+ def clean(str)
26
+ buffer = ::Java::JavaNio::ByteBuffer.wrap(str.to_java_bytes)
27
+ @decoder.decode(buffer).to_s
28
+ rescue Java::JavaLang::RuntimeException => e
29
+ raise Coder::Error, e.message, e.backtrace
30
+ end
31
+ end
32
+ end
33
+ end
@@ -1,3 +1,3 @@
1
1
  module Coder
2
- VERSION = "0.0.1"
2
+ VERSION = "0.1.0"
3
3
  end
@@ -1,3 +1,4 @@
1
+ # encoding: UTF-8
1
2
  require 'coder/cleaner'
2
3
 
3
4
  shared_examples Coder::Cleaner do
@@ -24,6 +25,10 @@ describe Coder::Cleaner::Builtin do
24
25
  it_behaves_like Coder::Cleaner if described_class.available?
25
26
  end
26
27
 
28
+ describe Coder::Cleaner::Java do
29
+ it_behaves_like Coder::Cleaner if described_class.available?
30
+ end
31
+
27
32
  describe Coder::Cleaner::Iconv do
28
33
  it_behaves_like Coder::Cleaner if described_class.available?
29
34
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: coder
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.1.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-09-21 00:00:00.000000000 Z
12
+ date: 2012-09-23 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rspec
@@ -61,6 +61,7 @@ files:
61
61
  - lib/coder/cleaner.rb
62
62
  - lib/coder/cleaner/builtin.rb
63
63
  - lib/coder/cleaner/iconv.rb
64
+ - lib/coder/cleaner/java.rb
64
65
  - lib/coder/cleaner/simple.rb
65
66
  - lib/coder/cleaner/simple/byte_buffer.rb
66
67
  - lib/coder/cleaner/simple/encodings.rb