Exposing a Java library as a JavaScript library

134 views
Skip to first unread message

hsivonen

unread,
May 29, 2008, 8:19:05 AM5/29/08
to Google Web Toolkit
I'd like to compile a rather complex Java library that I've written
into a JavaScript library that would expose a single JavaScript
function entry point in the global scope. Something like
public static void parseHtml5(String source, JavaScriptObject
nativeDomDocument)
exposed as window.parseHtml5(source, nativeDomDocument)

How do I set up Java-to-JavaScript compilation under Eclipse in such a
way that the output is just a single .js file which upon inclusion
adds an entry point function to the window object? I don't want a full
GWT app.

I need to include classes whose package name doesn't end with
".client". Also, I have some SAX dependencies, so I need to provide a
non-JDK version of a subset of SAX to the JS compilation, but I don't
want that to interfere with normal Java compilation. It would be
preferable to be able to add a JavaScript compile target to an
existing Eclipse project layout.

More concretely, I now have
nu.validator.htmlparser.common (needs to be included in JS)
nu.validator.htmlparser.impl (needs to be included in JS)
nu.validator.htmlparser.(various) (other packages that should be
omitted from JS compilation)
under src/ in my Eclipse project.

I'd like to be able to just add nu.validator.htmlparser.gwt with the
GWT specific classes (including JSNI stuff) under e.g. gwt-src/ (so
that it is easy to omit in Mavenized .jar export) and org.xml.sax with
copies of the part of SAX interfaces and exceptions that the JS
compiler needs to see someplace where it doesn't trip up normal Java
operation.

(I'm using GWT 1.5 RC1 and Eclipse 3.3.2 on Leopard.)

hsivonen

unread,
May 29, 2008, 8:36:03 AM5/29/08
to Google Web Toolkit
I should mention that I saw the pattern for exposing the entry point
in the FAQ. My problem is figuring out the project layout and script
bootstrapping.

Thomas Broyer

unread,
May 29, 2008, 11:56:28 AM5/29/08
to Google Web Toolkit

Hi Henri, nice to see you there ;-)


On 29 mai, 14:19, hsivonen <hsivo...@gmail.com> wrote:
> I'd like to compile a rather complex Java library that I've written
> into a JavaScript library that would expose a single JavaScript
> function entry point in the global scope. Something like
> public static void parseHtml5(String source, JavaScriptObject
> nativeDomDocument)
> exposed as window.parseHtml5(source, nativeDomDocument)
>
> How do I set up Java-to-JavaScript compilation under Eclipse in such a
> way that the output is just a single .js file which upon inclusion
> adds an entry point function to the window object? I don't want a full
> GWT app.

To compile to JS and have some code running "at loading time" (to
export your parseHtml5 function), you need to make a GWT
"application" (your need an EntryPoint). Note however that a GWT app
doesn't have to have any UI at all (you're not forced to include the
com.google.gwt.user.User module if you don't want to), it's just JS
after all...

> I need to include classes whose package name doesn't end with
> ".client".

Put a <source path="..." /> in your "HTMLParser.gwt.xml" module
description file.

> Also, I have some SAX dependencies, so I need to provide a
> non-JDK version of a subset of SAX to the JS compilation, but I don't
> want that to interfere with normal Java compilation.

The "common practice" is to place your code in a "super" directory,
sibling of your "src" dir, and put a <super-source path="..." /> in
your module.
The super/* won't be a "source folder" in your Eclipse project so it
won't interfere with "normal Java compilation", but you'll add to the
classpath when running the GWTCompiler.
See http://code.google.com/p/bunsenandbeaker/wiki/DevGuideModuleXml
for info about <super-source/>, and have a look at GWT's user/ source.

> More concretely, I now have
> nu.validator.htmlparser.common (needs to be included in JS)
> nu.validator.htmlparser.impl (needs to be included in JS)
> nu.validator.htmlparser.(various) (other packages that should be
> omitted from JS compilation)
> under src/ in my Eclipse project.
>
> I'd like to be able to just add nu.validator.htmlparser.gwt with the
> GWT specific classes (including JSNI stuff) under e.g. gwt-src/ (so
> that it is easy to omit in Mavenized .jar export) and org.xml.sax with
> copies of the part of SAX interfaces and exceptions that the JS
> compiler needs to see someplace where it doesn't trip up normal Java
> operation.

I'd place the HTMLParser.gwt.xml in nu.validator.htmlparser and use:
<source path="common" />
<source path="impl" />
<source path="gwt" />

Another solution is to have a Common.gwt.xml in
nu.validator.htmlparser.common and an Impl.gwt.xml in
nu.validator.htmlparser.impl, and <inherits/> them from your
HTMLParser.gwt.xml residing in nu.validator.htmlparser.gwt.

Those are just two example layouts among others...

hsivonen

unread,
May 30, 2008, 4:53:10 AM5/30/08
to Google Web Toolkit
On May 29, 6:56 pm, Thomas Broyer <t.bro...@gmail.com> wrote:
> Hi Henri, nice to see you there ;-)

Hi. :-)

> On 29 mai, 14:19, hsivonen <hsivo...@gmail.com> wrote:

> To compile to JS and have some code running "at loading time" (to
> export your parseHtml5 function), you need to make a GWT
> "application" (your need an EntryPoint). Note however that a GWT app
> doesn't have to have any UI at all (you're not forced to include the
> com.google.gwt.user.User module if you don't want to), it's just JS
> after all...

OK.

> > Also, I have some SAX dependencies, so I need to provide a
> > non-JDK version of a subset of SAX to the JS compilation, but I don't
> > want that to interfere with normal Java compilation.
>
> The "common practice" is to place your code in a "super" directory,
> sibling of your "src" dir, and put a <super-source path="..." /> in
> your module.
> The super/* won't be a "source folder" in your Eclipse project so it
> won't interfere with "normal Java compilation", but you'll add to the
> classpath when running the GWTCompiler.
> Seehttp://code.google.com/p/bunsenandbeaker/wiki/DevGuideModuleXml
> for info about <super-source/>,

The referenced document doesn't explain how to refer to super/ that is
a sibling of src/.

> and have a look at GWT's user/ source.

It says <super-source path="translatable"/>, but I can't figure out
how it finds translatable.

I have $APPDIR/gwt-src/nu/validator/htmlparser/HtmlParser.gwt.xml and
$APPDIR/super/org/xml/sax/*

What do I put it the path attribute of <super-source/> in
HtmlParser.gwt.xml so that the org.xml.sax package is found and
overrides the JDK when compiling to JS?

> I'd place the HTMLParser.gwt.xml in nu.validator.htmlparser and use:
> <source path="common" />
> <source path="impl" />
> <source path="gwt" />

Yeah, that seems to work, since the different source folders in have
their contents merged when treated as classpath. (However, using this
same trick for overriding JDK classes doesn't work.)

Thank you!

hsivonen

unread,
May 30, 2008, 5:23:02 AM5/30/08
to Google Web Toolkit
OK. Figured it out.
I made
super/nu/validator/htmlparser/translatable/org/xml/sax

and put <super-source path="translatable"/> into the module file.

I think this stuff needs better docs.
Reply all
Reply to author
Forward
0 new messages