Accessing Flexmark (Java) from Query
Saturday, 19 September 2020 · 9 messages · last reply 21 Sep 2020
AM
Andreas Mixich19 Sep 2020 22:54 #
Hi,
how can I translate this Java[1] to XQuery according to [2]?
package com.vladsch.flexmark.samples;
import com.vladsch.flexmark.util.ast.Node;
import com.vladsch.flexmark.html.HtmlRenderer;
import com.vladsch.flexmark.parser.Parser;
import com.vladsch.flexmark.util.data.MutableDataSet;
public class BasicSample {
public static void main(String[] args) {
MutableDataSet options = new MutableDataSet();
// uncomment to set optional extensions
//options.set(Parser.EXTENSIONS,
Arrays.asList(TablesExtension.create(), StrikethroughExtension.create()));
// uncomment to convert soft-breaks to hard breaks
//options.set(HtmlRenderer.SOFT_BREAK, "<br />\n");
Parser parser = Parser.builder(options).build();
HtmlRenderer renderer = HtmlRenderer.builder(options).build();
// You can re-use parser and renderer instances
Node document = parser.parse("This is *Sparta*");
String html = renderer.render(document); // "<p>This is
<em>Sparta</em></p>\n"
System.out.println(html);
}
}I only get so far as to:
declare namespace Node = "com.vladsch.flexmark.util.ast.Node";
declare namespace HtmlRenderer =" com.vladsch.flexmark.html.HtmlRenderer";
declare namespace Parser = "com.vladsch.flexmark.parser.Parser";
declare namespace MutableDataSet =
"com.vladsch.flexmark.util.data.MutableDataSet";
declare option db:checkstrings 'false';
let $mds := MutableDataSet:new()
let $builder := Parser:builder($mds)
return $builderwhich results in
DataSet{dataSet={}}
I found no documentation, on how to translate something as
Parser.builder(options).build()
to XQuery. How do I express the method
build()of the instance
Parser.builder(options)
I just created?
Also, when I try the other way, by importing, for the following code
import module namespace Node = "java:com.vladsch.flexmark.util.ast.Node";
import module namespace HtmlRenderer =
"java:com.vladsch.flexmark.html.HtmlRenderer";
import module namespace Parser =
"java:com.vladsch.flexmark.parser.Parser";
import module namespace MutableDataSet =
"java:com.vladsch.flexmark.util.data.MutableDataSet";
let $mds := MutableDataSet:new()I get:
[XQST0059] Could not instantiate com.vladsch.flexmark.util.ast.Node: java.lang.InstantiationException.
Could it be, that it is only possible to call static properties via [1]? Am I right, that I need to write
wrapper code in Java and then prepare results in a way, that I can retreive them from my own Java class?
Thank you.
[1]:
https://github.com/vsch/flexmark-java/blob/master/flexmark-java-samples/src/com/vladsch/flexmark/java/samples/BasicSample.java [2]: https://docs.basex.org/wiki/Java_Bindings
[3]: https://github.com/vsch/flexmark-java
--
Goody Bye, Minden jót, Mit freundlichen Grüßen,
Andreas Mixich
MH
Martin Honnen19 Sep 2020 23:30 #
Quoted text · Andreas Mixich · 65 lines
On 19.09.2020 22:54, Andreas Mixich wrote: > Hi, > > how can I translate this Java[1] to XQuery according to [2]? > > package com.vladsch.flexmark.samples; > > import com.vladsch.flexmark.util.ast.Node; > import com.vladsch.flexmark.html.HtmlRenderer; > import com.vladsch.flexmark.parser.Parser; > import com.vladsch.flexmark.util.data.MutableDataSet; > > public class BasicSample { > public static void main(String[] args) { > MutableDataSet options = new MutableDataSet(); > > // uncomment to set optional extensions > //options.set(Parser.EXTENSIONS, > Arrays.asList(TablesExtension.create(), StrikethroughExtension.create())); > > // uncomment to convert soft-breaks to hard breaks > //options.set(HtmlRenderer.SOFT_BREAK, "<br />\n"); > > Parser parser = Parser.builder(options).build(); > HtmlRenderer renderer = HtmlRenderer.builder(options).build(); > > // You can re-use parser and renderer instances > Node document = parser.parse("This is *Sparta*"); > String html = renderer.render(document); // "<p>This is > <em>Sparta</em></p>\n" > System.out.println(html); > } > } > > I only get so far as to: > > declare namespace Node = "com.vladsch.flexmark.util.ast.Node"; > declare namespace HtmlRenderer =" com.vladsch.flexmark.html.HtmlRenderer"; > declare namespace Parser = "com.vladsch.flexmark.parser.Parser"; > declare namespace MutableDataSet = > "com.vladsch.flexmark.util.data.MutableDataSet"; > > declare option db:checkstrings 'false'; > > let $mds := MutableDataSet:new() > let $builder := Parser:builder($mds) > return $builder > > which results in > > DataSet{dataSet={}} > > I found no documentation, on how to translate something as > > Parser.builder(options).build() > > to XQuery. How do I express the method > > build() > > of the instance > > Parser.builder(options) > > I just created?
Does
import com.vladsch.flexmark.parser.Parser.Builder;
with
$builder => Parser.Builder:build()work?
AM
Andreas Mixich20 Sep 2020 01:41 #
Quoted text · Martin Honnen · 6 lines
Am 19.09.2020 um 23:30 schrieb Martin Honnen: > import com.vladsch.flexmark.parser.Parser.Builder; > > with > > $builder => Parser.Builder:build()
Hello Martin,
thanks for taking the time!
I am not quite sure I understand. Do you want me to:
import module namespace Parser.Builder =
"java:com.vladsch.flexmark.parser.Parser.Builder";to get the
Parser.Builder prefix for:
Parser.Builder:build()
?
In any case, I get:
[XQST0059] Java class not found:
com.vladsch.flexmark.parser.Parser.Builder.--
Goody Bye, Minden jót, Mit freundlichen Grüßen,
Andreas Mixich
AM
Andreas Mixich20 Sep 2020 01:49 #
I forgot to mention, that
import module namespace Parser =
"java:com.vladsch.flexmark.parser.Parser";results in
[XQST0059] Could not instantiate com.vladsch.flexmark.parser.Parser: com.vladsch.flexmark.parser.Parser.<init>().
And that I also tried importing the class hierarchy (minus
java.lang.Object) for Parser.Builder as documented in:
https://javadoc.io/doc/com.vladsch.flexmark/flexmark/latest/com/vladsch/flexmark/parser/Parser.Builder.html import module namespace DataSet =
"java:com.vladsch.flexmark.util.data.DataSet";
import module namespace MutableDataSet =
"java:com.vladsch.flexmark.util.data.MutableDataSet";
import module namespace BuilderBase =
"java:com.vladsch.flexmark.util.builder.BuilderBase"; ()
which resulted in:
[XQST0059] Could not instantiate
com.vladsch.flexmark.util.builder.BuilderBase:
Class org.basex.query.util.pkg.ModuleLoader can not access a member of class com.vladsch.flexmark.util.builder.BuilderBase with modifiers "protected".
--
Goody Bye, Minden jót, Mit freundlichen Grüßen,
Andreas Mixich
MH
Quoted text · Andreas Mixich · 16 lines
On 20.09.2020 01:41, Andreas Mixich wrote: > Am 19.09.2020 um 23:30 schrieb Martin Honnen: >> import com.vladsch.flexmark.parser.Parser.Builder; >> >> with >> >> $builder => Parser.Builder:build() > Hello Martin, > > thanks for taking the time! > > I am not quite sure I understand. Do you want me to: > > import module namespace Parser.Builder = > "java:com.vladsch.flexmark.parser.Parser.Builder"; >
I am not sure I have completely understood the reflection and namespace conventions but after some reading and testing it seems the inner class Builder should be accessible by
declare namespace Builder =
'java:com.vladsch.flexmark.parser.Parser$Builder';and then
$builder => Builder:build()should work for the variable
let $builder := Parser:builder($mds)you have, you should be able to chain
MutableDataSet:new() => Parser:builder() => Builder:build()
AB
Andy Bunce20 Sep 2020 19:18 #
Hi Andreas,
I think Martin has it right, I have some old code that does it like that [1]. You can try it via[2]
/Andy
[1]
https://github.com/expkg-zone58/ex-markdown/blob/master/src/main/content/markdown.xqm#L12 [2] https://github.com/expkg-zone58/ex-markdown/releases
Quoted text · Martin Honnen · 42 lines
On Sun, 20 Sep 2020 at 15:30, Martin Honnen <martin.honnen at gmx.de> wrote: > > On 20.09.2020 01:41, Andreas Mixich wrote: > > Am 19.09.2020 um 23:30 schrieb Martin Honnen: > >> import com.vladsch.flexmark.parser.Parser.Builder; > >> > >> with > >> > >> $builder => Parser.Builder:build() > > Hello Martin, > > > > thanks for taking the time! > > > > I am not quite sure I understand. Do you want me to: > > > > import module namespace Parser.Builder = > > "java:com.vladsch.flexmark.parser.Parser.Builder"; > > > > I am not sure I have completely understood the reflection and namespace > conventions but after some reading and testing it seems the inner class > Builder should be accessible by > > > declare namespace Builder = > 'java:com.vladsch.flexmark.parser.Parser$Builder'; > > > and then > > $builder => Builder:build() > > > should work for the variable > > let $builder := Parser:builder($mds) > > you have, you should be able to chain > > MutableDataSet:new() => Parser:builder() => Builder:build() > >
AM
Quoted text · Martin Honnen · 5 lines
Am 20.09.2020 um 16:30 schrieb Martin Honnen: > you have, you should be able to chain > > MutableDataSet:new() => Parser:builder() => Builder:build() >
Very much appreciated! Thank you :-)
--
Goody Bye, Minden jót, Mit freundlichen Grüßen,
Andreas Mixich
AM
Quoted text · Andy Bunce · 12 lines
Am 20.09.2020 um 19:18 schrieb Andy Bunce: > Hi Andreas, > > I think Martin has it right, I have some old code that does it like > that [1]. You can try it via[2] > > /Andy > > [1] > https://github.com/expkg-zone58/ex-markdown/blob/master/src/main/content/markdown.xqm#L12 > [2] https://github.com/expkg-zone58/ex-markdown/releases >
Super! Thank you. It's not running out of the box (Flexmark had some major code restructuring some time ago, class paths have changed for example,
java:com.vladsch.flexmark.util.data.MutableDataSet
) but I will try to figure it out, also comparing it to Martin's code, so I can make it flow :-)
Currently I get the error:
[XPTY0004] com.vladsch.flexmark.parser.Parser$Builder:build: Multiple functions with 1.
Anyways, very much appreciated for the code example!
--
Goody Bye, Minden jót, Mit freundlichen Grüßen,
Andreas Mixich
ML
Marco Lettere21 Sep 2020 09:23 #
Quoted text · Andreas Mixich · 5 lines
On 20/09/20 22:42, Andreas Mixich wrote: > Currently I get the error: > > [XPTY0004] com.vladsch.flexmark.parser.Parser$Builder:build: Multiple > functions with 1.
Hi Andreas,
this is because of Java distinguishes method signatures not only by number of parameters but also by type. You have to use the middle-dot notation to clearly define which method you need as in [1].
Regards,
Marco.
[1] https://docs.basex.org/wiki/Java_Bindings#Functions_and_Variables
Written for BaseX 9.4. BaseX may have changed since: please check the current documentation.
⚡Generated with XQuery