-
Notifications
You must be signed in to change notification settings - Fork 172
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make Asciidoctor expose interfaces exclusively
- Loading branch information
1 parent
59fed13
commit def1e4c
Showing
8 changed files
with
628 additions
and
448 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
80 changes: 6 additions & 74 deletions
80
asciidoctorj-core/src/main/java/org/asciidoctor/converter/JavaConverterRegistry.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,82 +1,14 @@ | ||
package org.asciidoctor.converter; | ||
|
||
import org.jruby.Ruby; | ||
import org.jruby.RubyArray; | ||
import org.jruby.RubyClass; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
public class JavaConverterRegistry { | ||
|
||
private Ruby rubyRuntime; | ||
|
||
public JavaConverterRegistry(Ruby rubyRuntime) { | ||
this.rubyRuntime = rubyRuntime; | ||
} | ||
|
||
public <U, T extends Converter<U> & OutputFormatWriter<U>> void register(final Class<T> converterClass, String... backends) { | ||
|
||
RubyClass clazz = ConverterProxy.register(rubyRuntime, converterClass); | ||
|
||
ConverterFor converterForAnnotation = converterClass.getAnnotation(ConverterFor.class); | ||
if (converterForAnnotation != null) { | ||
// Backend annotation present => Register with name given in annotation | ||
String backend = !ConverterFor.UNDEFINED.equals(converterForAnnotation.format()) ? converterForAnnotation.format() : converterForAnnotation.value(); | ||
getConverterFactory() | ||
.callMethod("register", clazz, rubyRuntime.newArray(rubyRuntime.newString(backend))); | ||
|
||
} else if (backends.length == 0) { | ||
// No backend annotation and no backend defined => register as default backend | ||
getConverterFactory() | ||
.callMethod("register", clazz); | ||
} | ||
if (backends.length > 0) { | ||
// Always additionally register with names passed to this method | ||
final RubyArray rubyBackendNames = new RubyArray(rubyRuntime, backends.length); | ||
for (String backend: backends) { | ||
rubyBackendNames.add(rubyRuntime.newString(backend)); | ||
} | ||
getConverterFactory() | ||
.callMethod("register", clazz, rubyBackendNames); | ||
} | ||
} | ||
|
||
public Class<?> resolve(String backend) { | ||
RubyClass rubyClass = (RubyClass) getConverterFactory() | ||
.callMethod("resolve", rubyRuntime.newString(backend)); | ||
|
||
Class<?> clazz = rubyClass.getReifiedClass(); | ||
if (clazz != null) { | ||
return clazz; | ||
} else if (rubyClass.getAllocator() instanceof ConverterProxy.Allocator) { | ||
ConverterProxy.Allocator allocator = (ConverterProxy.Allocator) rubyClass.getAllocator(); | ||
return allocator.getConverterClass(); | ||
} | ||
return null; | ||
} | ||
public interface JavaConverterRegistry { | ||
|
||
public void unregisterAll() { | ||
getConverterFactory() | ||
.callMethod("unregister_all"); | ||
} | ||
<U, T extends Converter<U> & OutputFormatWriter<U>> void register(Class<T> converterClass, String... backends); | ||
|
||
private RubyClass getConverterFactory() { | ||
return rubyRuntime.getModule("Asciidoctor") | ||
.defineOrGetModuleUnder("Converter") | ||
.getClass("Factory"); | ||
} | ||
Class<?> resolve(String backend); | ||
|
||
public Map<String, Class<?>> converters() { | ||
final RubyArray rubyKeys = (RubyArray) getConverterFactory() | ||
.callMethod("converters") | ||
.callMethod(rubyRuntime.getCurrentContext(), "keys"); | ||
void unregisterAll(); | ||
|
||
Map<String, Class<?>> converters = new HashMap<String, Class<?>>(); | ||
for (Object rubyBackend : rubyKeys) { | ||
String backend = rubyBackend.toString(); | ||
converters.put(backend, resolve(backend)); | ||
} | ||
return converters; | ||
} | ||
} | ||
Map<String, Class<?>> converters(); | ||
} |
Oops, something went wrong.