예제 #1
0
 private Block createBlock(AbstractBlock parent, String context, Map<String, Object> options) {
   IRubyObject rubyClass = rubyRuntime.evalScriptlet("Asciidoctor::Block");
   RubyHash convertMapToRubyHashWithSymbols =
       RubyHashUtil.convertMapToRubyHashWithSymbols(rubyRuntime, options);
   Object[] parameters = {
     parent.delegate(), RubyUtils.toSymbol(rubyRuntime, context), convertMapToRubyHashWithSymbols
   };
   return (Block)
       JavaEmbedUtils.invokeMethod(rubyRuntime, rubyClass, "new", parameters, Block.class);
 }
예제 #2
0
  @Test
  public void shouldWrapJavaIOExceptions() throws Exception {
    Ruby ruby = Ruby.newInstance();
    RackInput rackInput = mock(RackInput.class);
    when(rackInput.read(null)).thenThrow(new IOException("fake"));

    JRubyRackInput subject = new JRubyRackInput(ruby, rackInput);
    GlobalVariables globalVariables = ruby.getGlobalVariables();
    globalVariables.set("$rack_input", subject);

    IRubyObject result =
        ruby.evalScriptlet(
            "begin; $rack_input.read; rescue IOError => e; \"rescued #{e.message}\"; end");
    assertThat(result.asJavaString()).isEqualTo("rescued fake");
  }
예제 #3
0
  public static Ruby createRuby() throws Exception {
    RubyRuntimeFactoryImpl factory = new RubyRuntimeFactoryImpl();

    if (System.getProperty("gem.path") != null) {
      factory.setGemPath(System.getProperty("gem.path"));
    } else {
      factory.setGemPath("target/rubygems");
    }
    System.err.println("gem.path is " + factory.getGemPath());
    factory.setUseJRubyHomeEnvVar(false);

    Ruby ruby = factory.create();

    ruby.evalScriptlet("require %q(rubygems)");
    return ruby;
  }
예제 #4
0
  public ErbScriptEngine(ErbScriptEngineFactory factory) {
    super(factory);

    final ClassLoader oldClassLoader = Thread.currentThread().getContextClassLoader();
    try {
      Thread.currentThread().setContextClassLoader(getClass().getClassLoader());
      runtime = Ruby.newInstance();
      runtime.evalScriptlet(
          "require 'java';require 'erb';self.send :include, ERB::Util;class ERB;def get_binding;binding;end;attr_reader :props;def set_props(p);@props = p;"
              + "for name,v in @props;instance_eval \"def #{name}; @props['#{name}'];end\";end;end;end;");

      erbModule = runtime.getClassFromPath("ERB");
      bindingSym = RubySymbol.newSymbol(runtime, "get_binding");
    } finally {
      Thread.currentThread().setContextClassLoader(oldClassLoader);
    }
  }
예제 #5
0
  public Inline createInline(
      AbstractBlock parent,
      String context,
      String text,
      Map<String, Object> attributes,
      Map<String, Object> options) {

    options.put(Options.ATTRIBUTES, attributes);

    IRubyObject rubyClass = rubyRuntime.evalScriptlet("Asciidoctor::Inline");
    RubyHash convertMapToRubyHashWithSymbols =
        RubyHashUtil.convertMapToRubyHashWithSymbols(rubyRuntime, options);
    Object[] parameters = {
      parent.delegate(),
      RubyUtils.toSymbol(rubyRuntime, context),
      text,
      convertMapToRubyHashWithSymbols
    };
    return (Inline)
        JavaEmbedUtils.invokeMethod(rubyRuntime, rubyClass, "new", parameters, Inline.class);
  }
 private void javaImport(Ruby ruby, String className) {
   ruby.evalScriptlet(String.format("java_import '%s'", className));
 }
 private void javaImport(Ruby ruby, Class<?> clazz) {
   ruby.evalScriptlet(String.format("java_import '%s'", getImportLine(clazz)));
 }