Skip to content

Commit 18f57f3

Browse files
committed
Add JRuby extension to the gem
This pulls in the nkf extension implementation from JRuby. The build and load logic has been updated along the same lines as ruby/digest and the gem appears to build correctly for the -java platform. Fixes #13
1 parent e7df23a commit 18f57f3

File tree

10 files changed

+970
-5
lines changed

10 files changed

+970
-5
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,6 @@
99
*.bundle
1010
*.dll
1111
*.so
12+
lib/nkf.jar
13+
.idea
14+
nkf.iml

Rakefile

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,18 @@ Rake::TestTask.new(:test) do |t|
77
t.test_files = FileList["test/**/test_*.rb"]
88
end
99

10-
require 'rake/extensiontask'
11-
Rake::ExtensionTask.new("nkf")
10+
if RUBY_ENGINE == "jruby"
11+
require "rake/javaextensiontask"
12+
Rake::JavaExtensionTask.new("nkf") do |ext|
13+
ext.source_version = "1.8"
14+
ext.target_version = "1.8"
15+
ext.ext_dir = "ext/java"
16+
end
17+
18+
task :build => :compile
19+
else
20+
require 'rake/extensiontask'
21+
Rake::ExtensionTask.new("nkf")
22+
end
23+
1224
task :default => :test
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/***** BEGIN LICENSE BLOCK *****
2+
* Version: EPL 2.0/GPL 2.0/LGPL 2.1
3+
*
4+
* The contents of this file are subject to the Eclipse Public
5+
* License Version 2.0 (the "License"); you may not use this file
6+
* except in compliance with the License. You may obtain a copy of
7+
* the License at http://www.eclipse.org/legal/epl-v20.html
8+
*
9+
* Software distributed under the License is distributed on an "AS
10+
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
11+
* implied. See the License for the specific language governing
12+
* rights and limitations under the License.
13+
*
14+
* Copyright (C) 2011 Koichiro Ohba <koichiro@meadowy.org>
15+
*
16+
* Alternatively, the contents of this file may be used under the terms of
17+
* either of the GNU General Public License Version 2 or later (the "GPL"),
18+
* or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
19+
* in which case the provisions of the GPL or the LGPL are applicable instead
20+
* of those above. If you wish to allow use of your version of this file only
21+
* under the terms of either the GPL or the LGPL, and not to allow others to
22+
* use your version of this file under the terms of the EPL, indicate your
23+
* decision by deleting the provisions above and replace them with the notice
24+
* and other provisions required by the GPL or the LGPL. If you do not delete
25+
* the provisions above, a recipient may use your version of this file under
26+
* the terms of any one of the EPL, the GPL or the LGPL.
27+
***** END LICENSE BLOCK *****/
28+
29+
package org.jruby.ext.nkf;
30+
31+
import java.util.List;
32+
import java.util.ArrayList;
33+
34+
public class Command {
35+
private final List<Option> options = new ArrayList<Option>();
36+
public boolean hasOption(String opt) {
37+
for (Option option : options) {
38+
if (opt.equals(option.getOpt())) return true;
39+
if (opt.equals(option.getLongOpt())) return true;
40+
}
41+
return false;
42+
}
43+
public void addOption(Option opt) {
44+
options.add(opt);
45+
}
46+
public Option getOption(String opt) {
47+
for (Option option : options) {
48+
if (opt.equals(option.getOpt())) return option;
49+
if (opt.equals(option.getLongOpt())) return option;
50+
}
51+
return null;
52+
}
53+
public String getOptionValue(String opt) {
54+
return getOption(opt).getValue();
55+
}
56+
public String toString() {
57+
return options.toString();
58+
}
59+
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/***** BEGIN LICENSE BLOCK *****
2+
* Version: EPL 2.0/GPL 2.0/LGPL 2.1
3+
*
4+
* The contents of this file are subject to the Eclipse Public
5+
* License Version 2.0 (the "License"); you may not use this file
6+
* except in compliance with the License. You may obtain a copy of
7+
* the License at http://www.eclipse.org/legal/epl-v20.html
8+
*
9+
* Software distributed under the License is distributed on an "AS
10+
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
11+
* implied. See the License for the specific language governing
12+
* rights and limitations under the License.
13+
*
14+
* Copyright (C) 2011 Koichiro Ohba <koichiro@meadowy.org>
15+
*
16+
* Alternatively, the contents of this file may be used under the terms of
17+
* either of the GNU General Public License Version 2 or later (the "GPL"),
18+
* or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
19+
* in which case the provisions of the GPL or the LGPL are applicable instead
20+
* of those above. If you wish to allow use of your version of this file only
21+
* under the terms of either the GPL or the LGPL, and not to allow others to
22+
* use your version of this file under the terms of the EPL, indicate your
23+
* decision by deleting the provisions above and replace them with the notice
24+
* and other provisions required by the GPL or the LGPL. If you do not delete
25+
* the provisions above, a recipient may use your version of this file under
26+
* the terms of any one of the EPL, the GPL or the LGPL.
27+
***** END LICENSE BLOCK *****/
28+
29+
package org.jruby.ext.nkf;
30+
31+
public class CommandParser {
32+
public Command parse(Options opt, String args) {
33+
Command cc = new Command();
34+
String[] tokens = args.split("\\s");
35+
for (int i = 0; i < tokens.length; i++) {
36+
// long option
37+
if (tokens[i].startsWith("--")) {
38+
String s = stripDash(tokens[i]);
39+
if (opt.hasLongOption(s)) {
40+
cc.addOption(opt.matchLongOption(s));
41+
}
42+
} else {
43+
// short option
44+
String s = stripDash(tokens[i]);
45+
int max = s.length();
46+
for (int j = 0; j < max; j++) {
47+
if (opt.hasShortOption(s)) {
48+
Option cmd = opt.matchShortOption(s);
49+
if (cmd.getValue() != null) {
50+
int op_len = cmd.getValue().length();
51+
s = s.substring(op_len);
52+
j = j + op_len;
53+
}
54+
cc.addOption(cmd);
55+
}
56+
s = s.substring(1);
57+
}
58+
}
59+
}
60+
return cc;
61+
}
62+
private String stripDash(String s) {
63+
if (s.startsWith("--")) {
64+
return s.substring(2, s.length());
65+
} else if (s.startsWith("-")) {
66+
return s.substring(1, s.length());
67+
} else {
68+
return s;
69+
}
70+
}
71+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package org.jruby.ext.nkf;
2+
3+
import org.jruby.Ruby;
4+
import org.jruby.runtime.load.Library;
5+
6+
import java.io.IOException;
7+
8+
public class NKFLibrary implements Library {
9+
@Override
10+
public void load(Ruby ruby, boolean b) throws IOException {
11+
RubyNKF.load(ruby);
12+
}
13+
}
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
/***** BEGIN LICENSE BLOCK *****
2+
* Version: EPL 2.0/GPL 2.0/LGPL 2.1
3+
*
4+
* The contents of this file are subject to the Eclipse Public
5+
* License Version 2.0 (the "License"); you may not use this file
6+
* except in compliance with the License. You may obtain a copy of
7+
* the License at http://www.eclipse.org/legal/epl-v20.html
8+
*
9+
* Software distributed under the License is distributed on an "AS
10+
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
11+
* implied. See the License for the specific language governing
12+
* rights and limitations under the License.
13+
*
14+
* Copyright (C) 2011 Koichiro Ohba <koichiro@meadowy.org>
15+
*
16+
* Alternatively, the contents of this file may be used under the terms of
17+
* either of the GNU General Public License Version 2 or later (the "GPL"),
18+
* or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
19+
* in which case the provisions of the GPL or the LGPL are applicable instead
20+
* of those above. If you wish to allow use of your version of this file only
21+
* under the terms of either the GPL or the LGPL, and not to allow others to
22+
* use your version of this file under the terms of the EPL, indicate your
23+
* decision by deleting the provisions above and replace them with the notice
24+
* and other provisions required by the GPL or the LGPL. If you do not delete
25+
* the provisions above, a recipient may use your version of this file under
26+
* the terms of any one of the EPL, the GPL or the LGPL.
27+
***** END LICENSE BLOCK *****/
28+
29+
package org.jruby.ext.nkf;
30+
31+
import java.util.regex.Pattern;
32+
33+
public class Option {
34+
private final String opt;
35+
private final String longOpt;
36+
private boolean hasArg = false;
37+
private String value = null;
38+
private Pattern pattern;
39+
40+
public Option(String opt, String longOpt, String pattern) {
41+
this.opt = opt;
42+
this.longOpt = longOpt;
43+
if (pattern != null) {
44+
this.hasArg = true;
45+
this.pattern = Pattern.compile(pattern);
46+
}
47+
}
48+
String getOpt() { return opt; }
49+
String getLongOpt() { return longOpt; }
50+
boolean hasShortOpt() {
51+
return opt != null;
52+
}
53+
boolean hasLongOpt() {
54+
return longOpt != null;
55+
}
56+
boolean hasArg() {
57+
return hasArg;
58+
}
59+
public String getValue() {
60+
return value;
61+
}
62+
void setValue(String v) {
63+
value = v;
64+
}
65+
String getKey() {
66+
if (opt == null)
67+
return longOpt;
68+
else
69+
return opt;
70+
}
71+
Pattern pattern() {
72+
return pattern;
73+
}
74+
public String toString() {
75+
return "[opt: " + opt
76+
+ " longOpt: " + longOpt
77+
+ " hasArg: " + hasArg
78+
+ " pattern: " + pattern
79+
+ " value: " + value + "]";
80+
}
81+
}
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
/***** BEGIN LICENSE BLOCK *****
2+
* Version: EPL 2.0/GPL 2.0/LGPL 2.1
3+
*
4+
* The contents of this file are subject to the Eclipse Public
5+
* License Version 2.0 (the "License"); you may not use this file
6+
* except in compliance with the License. You may obtain a copy of
7+
* the License at http://www.eclipse.org/legal/epl-v20.html
8+
*
9+
* Software distributed under the License is distributed on an "AS
10+
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
11+
* implied. See the License for the specific language governing
12+
* rights and limitations under the License.
13+
*
14+
* Copyright (C) 2011 Koichiro Ohba <koichiro@meadowy.org>
15+
*
16+
* Alternatively, the contents of this file may be used under the terms of
17+
* either of the GNU General Public License Version 2 or later (the "GPL"),
18+
* or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
19+
* in which case the provisions of the GPL or the LGPL are applicable instead
20+
* of those above. If you wish to allow use of your version of this file only
21+
* under the terms of either the GPL or the LGPL, and not to allow others to
22+
* use your version of this file under the terms of the EPL, indicate your
23+
* decision by deleting the provisions above and replace them with the notice
24+
* and other provisions required by the GPL or the LGPL. If you do not delete
25+
* the provisions above, a recipient may use your version of this file under
26+
* the terms of any one of the EPL, the GPL or the LGPL.
27+
***** END LICENSE BLOCK *****/
28+
29+
package org.jruby.ext.nkf;
30+
31+
import java.util.Map;
32+
import java.util.LinkedHashMap;
33+
import java.util.regex.Matcher;
34+
35+
public class Options {
36+
private final Map<String, Option> shortOpts = new LinkedHashMap<String, Option>();
37+
private final Map<String, Option> longOpts = new LinkedHashMap<String, Option>();
38+
39+
public Options addOption(String opt) {
40+
return addOption(opt, null);
41+
}
42+
public Options addOption(String opt, String longOpt) {
43+
return addOption(opt, longOpt, null);
44+
}
45+
public Options addOption(String opt, String longOpt, String pattern) {
46+
return addOption(new Option(opt, longOpt, pattern));
47+
}
48+
public Options addOption(Option opt) {
49+
if (opt.hasLongOpt()) {
50+
longOpts.put(opt.getLongOpt(), opt);
51+
}
52+
if (opt.hasShortOpt()) {
53+
shortOpts.put(opt.getOpt(), opt);
54+
}
55+
return this;
56+
}
57+
boolean hasShortOption(String opt) {
58+
for (Map.Entry<String , Option> e : shortOpts.entrySet()) {
59+
if (opt.startsWith(e.getKey())) {
60+
return true;
61+
}
62+
}
63+
return false;
64+
}
65+
public Option matchShortOption(String opt) {
66+
// independent of opt length
67+
for (Map.Entry<String , Option> e : shortOpts.entrySet()) {
68+
//System.out.println(opt + " = " + e.getKey());
69+
if (opt.startsWith(e.getKey())) {
70+
//System.out.println("match[" + e.getKey() + "]");
71+
Option cmd = e.getValue();
72+
if (cmd.hasArg()) {
73+
Matcher m = cmd.pattern().matcher(opt);
74+
if (m.find()) {
75+
//System.out.println("regix[" + m.group() + "]");
76+
cmd.setValue(m.group());
77+
}
78+
}
79+
return cmd;
80+
}
81+
}
82+
return null;
83+
}
84+
boolean hasLongOption(String opt) {
85+
for (Map.Entry<String , Option> e : longOpts.entrySet()) {
86+
if (opt.startsWith(e.getKey())) {
87+
return true;
88+
}
89+
}
90+
return false;
91+
}
92+
Option matchLongOption(String opt) {
93+
for (Map.Entry<String , Option> e : longOpts.entrySet()) {
94+
//System.out.println(opt + " = " + e.getKey());
95+
if (opt.startsWith(e.getKey())) {
96+
//System.out.println("match[" + e.getKey() + "]");
97+
Option cmd = e.getValue();
98+
if (cmd.hasArg()) {
99+
Matcher m = cmd.pattern().matcher(opt);
100+
if (m.find()) {
101+
//System.out.println("regix[" + m.group() + "]");
102+
cmd.setValue(m.group(1));
103+
}
104+
}
105+
return cmd;
106+
}
107+
}
108+
return null;
109+
}
110+
}

0 commit comments

Comments
 (0)