diff --git a/ext/liboga/LibogaService.java b/ext/liboga/LibogaService.java index dee659f..c19e12a 100644 --- a/ext/liboga/LibogaService.java +++ b/ext/liboga/LibogaService.java @@ -10,16 +10,7 @@ public class LibogaService implements BasicLibraryService { public boolean basicLoad(final Ruby runtime) throws IOException { - RubyModule xml = (RubyModule) runtime.getModule("Oga") - .getConstant("XML"); - - RubyClass lexer = xml.defineClassUnder( - "Lexer", - runtime.getObject(), - runtime.getObject().getAllocator() - ); - - lexer.defineAnnotatedMethods(org.liboga.xml.Lexer.class); + org.liboga.xml.Lexer.load(runtime); return true; } diff --git a/ext/liboga/org/liboga/xml/Lexer.rl b/ext/liboga/org/liboga/xml/Lexer.rl index 4b80ae0..ae3ee72 100644 --- a/ext/liboga/org/liboga/xml/Lexer.rl +++ b/ext/liboga/org/liboga/xml/Lexer.rl @@ -4,13 +4,17 @@ package org.liboga.xml; import java.io.IOException; +import org.jcodings.Encoding; + import org.jruby.Ruby; import org.jruby.RubyModule; import org.jruby.RubyClass; import org.jruby.RubyObject; +import org.jruby.RubyString; import org.jruby.anno.JRubyClass; import org.jruby.anno.JRubyMethod; import org.jruby.runtime.ThreadContext; +import org.jruby.runtime.ObjectAllocator; import org.jruby.runtime.builtin.IRubyObject; @JRubyClass(name="Oga::XML::Lexer", parent="Object") @@ -18,6 +22,28 @@ public class Lexer extends RubyObject { %% write data; + public static void load(Ruby runtime) + { + RubyModule xml = (RubyModule) runtime.getModule("Oga") + .getConstant("XML"); + + RubyClass lexer = xml.defineClassUnder( + "Lexer", + runtime.getObject(), + ALLOCATOR + ); + + lexer.defineAnnotatedMethods(Lexer.class); + } + + private static final ObjectAllocator ALLOCATOR = new ObjectAllocator() + { + public IRubyObject allocate(Ruby runtime, RubyClass klass) + { + return new org.liboga.xml.Lexer(runtime, klass); + } + }; + public Lexer(Ruby runtime, RubyClass klass) { super(runtime, klass); @@ -26,22 +52,27 @@ public class Lexer extends RubyObject @JRubyMethod public IRubyObject advance_native(ThreadContext context) { + // Pull the data in from Ruby land. + RubyString rb_str = (RubyString) this.getInstanceVariable("@data"); + Encoding encoding = rb_str.getEncoding(); + + byte[] data = rb_str.getBytes(); + int act = 0; int cs = 0; int ts = 0; int te = 0; int p = 0; - int pe = 0; + int pe = data.length; int eof = 0; int top = 0; - int[] data = {}; int[] stack = {}; %% write init; %% write exec; - return context.getRuntime().getNil(); + return context.nil; } }