Search in sources :

Example 21 with RubyClass

use of org.jruby.RubyClass in project jo by headius.

the class JoLibrary method load.

public void load(Ruby runtime, boolean wrap) throws IOException {
    RubyModule jo = runtime.defineModule("Jo");
    final ExecutorService executor = Executors.newCachedThreadPool(new ThreadFactory() {

        public Thread newThread(Runnable r) {
            Thread t = new Thread(r);
            t.setDaemon(true);
            return t;
        }
    });
    jo.setInternalVariable("executor", executor);
    RubyClass joFuture = jo.defineClassUnder("Future", runtime.getObject(), ObjectAllocator.NOT_ALLOCATABLE_ALLOCATOR);
    RubyClass joChannel = jo.defineClassUnder("Channel", runtime.getObject(), ObjectAllocator.NOT_ALLOCATABLE_ALLOCATOR);
    jo.defineAnnotatedMethods(JoMethods.class);
    joFuture.defineAnnotatedMethods(JoFuture.class);
    joChannel.defineAnnotatedMethods(JoChannel.class);
    runtime.addFinalizer(new Finalizable() {

        public void finalize() {
            executor.shutdown();
        }
    });
}
Also used : ThreadFactory(java.util.concurrent.ThreadFactory) RubyModule(org.jruby.RubyModule) Finalizable(org.jruby.Finalizable) ExecutorService(java.util.concurrent.ExecutorService) RubyClass(org.jruby.RubyClass)

Example 22 with RubyClass

use of org.jruby.RubyClass in project nokogiri by sparklemotion.

the class XsltStylesheet method parse_stylesheet_doc.

@JRubyMethod(meta = true, rest = true)
public static IRubyObject parse_stylesheet_doc(ThreadContext context, IRubyObject klazz, IRubyObject[] args) {
    Ruby runtime = context.getRuntime();
    ensureFirstArgIsDocument(runtime, args[0]);
    XmlDocument xmlDoc = (XmlDocument) args[0];
    ensureDocumentHasNoError(context, xmlDoc);
    Document doc = ((XmlDocument) xmlDoc.dup_implementation(context, true)).getDocument();
    XsltStylesheet xslt = (XsltStylesheet) NokogiriService.XSLT_STYLESHEET_ALLOCATOR.allocate(runtime, (RubyClass) klazz);
    try {
        xslt.init(args[1], doc);
    } catch (TransformerConfigurationException ex) {
        throw runtime.newRuntimeError("could not parse xslt stylesheet");
    }
    return xslt;
}
Also used : TransformerConfigurationException(javax.xml.transform.TransformerConfigurationException) JRubyClass(org.jruby.anno.JRubyClass) RubyClass(org.jruby.RubyClass) Document(org.w3c.dom.Document) Ruby(org.jruby.Ruby) JRubyMethod(org.jruby.anno.JRubyMethod)

Example 23 with RubyClass

use of org.jruby.RubyClass in project nokogiri by sparklemotion.

the class XsltStylesheet method createDocumentFromString.

private IRubyObject createDocumentFromString(ThreadContext context, Ruby runtime, String stringResult) {
    IRubyObject[] args = new IRubyObject[4];
    args[0] = stringOrBlank(runtime, stringResult);
    // url
    args[1] = runtime.getNil();
    // encoding
    args[2] = runtime.getNil();
    RubyClass parse_options = (RubyClass) runtime.getClassFromPath("Nokogiri::XML::ParseOptions");
    if (htmlish) {
        args[3] = parse_options.getConstant("DEFAULT_HTML");
        RubyClass htmlDocumentClass = getNokogiriClass(runtime, "Nokogiri::HTML::Document");
        return RuntimeHelpers.invoke(context, htmlDocumentClass, "parse", args);
    } else {
        args[3] = parse_options.getConstant("DEFAULT_XML");
        RubyClass xmlDocumentClass = getNokogiriClass(runtime, "Nokogiri::XML::Document");
        XmlDocument xmlDocument = (XmlDocument) RuntimeHelpers.invoke(context, xmlDocumentClass, "parse", args);
        if (((Document) xmlDocument.getNode()).getDocumentElement() == null) {
            RubyArray errors = (RubyArray) xmlDocument.getInstanceVariable("@errors");
            RuntimeHelpers.invoke(context, errors, "<<", args[0]);
        }
        return xmlDocument;
    }
}
Also used : RubyArray(org.jruby.RubyArray) JRubyClass(org.jruby.anno.JRubyClass) RubyClass(org.jruby.RubyClass) IRubyObject(org.jruby.runtime.builtin.IRubyObject)

Example 24 with RubyClass

use of org.jruby.RubyClass in project nokogiri by sparklemotion.

the class NokogiriService method createDocuments.

private void createDocuments(Ruby ruby, RubyModule xmlModule, RubyModule htmlModule, RubyClass node) {
    RubyClass xmlDocument = xmlModule.defineClassUnder("Document", node, XML_DOCUMENT_ALLOCATOR);
    xmlDocument.defineAnnotatedMethods(XmlDocument.class);
    //RubyModule htmlDoc = html.defineOrGetClassUnder("Document", document);
    RubyModule htmlDocument = htmlModule.defineClassUnder("Document", xmlDocument, HTML_DOCUMENT_ALLOCATOR);
    htmlDocument.defineAnnotatedMethods(HtmlDocument.class);
}
Also used : RubyModule(org.jruby.RubyModule) RubyClass(org.jruby.RubyClass)

Example 25 with RubyClass

use of org.jruby.RubyClass in project nokogiri by sparklemotion.

the class NokogiriService method createNokogiriModule.

private void createNokogiriModule(Ruby ruby, RubyModule nokogiri) {
    RubyClass encHandler = nokogiri.defineClassUnder("EncodingHandler", ruby.getObject(), ENCODING_HANDLER_ALLOCATOR);
    encHandler.defineAnnotatedMethods(EncodingHandler.class);
}
Also used : RubyClass(org.jruby.RubyClass)

Aggregations

RubyClass (org.jruby.RubyClass)29 JRubyClass (org.jruby.anno.JRubyClass)9 RubyModule (org.jruby.RubyModule)8 JRubyMethod (org.jruby.anno.JRubyMethod)7 Ruby (org.jruby.Ruby)4 Document (org.w3c.dom.Document)4 NokogiriHelpers.rubyStringToString (nokogiri.internals.NokogiriHelpers.rubyStringToString)3 RubyString (org.jruby.RubyString)3 IOException (java.io.IOException)2 TransformerConfigurationException (javax.xml.transform.TransformerConfigurationException)2 XmlDocument (nokogiri.XmlDocument)2 RubyArray (org.jruby.RubyArray)2 IRubyObject (org.jruby.runtime.builtin.IRubyObject)2 SAXException (org.xml.sax.SAXException)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ExecutorService (java.util.concurrent.ExecutorService)1 ThreadFactory (java.util.concurrent.ThreadFactory)1 Finalizable (org.jruby.Finalizable)1 RaiseException (org.jruby.exceptions.RaiseException)1