Question

I've been struggling to get the GWT wrapper of Syntaxhighlighter to work. I got the libraries off http://code.google.com/p/gwt-syntaxhighlighter/, but none of them seem to be working. I'm starting to wonder if it's my understanding of using external libraries that is so poor, or if it's indeed badly built JARs. Has anyone managed to use this? The gwt-syntaxhighlighter-1.1.1-gwt-standard.jar that's supposed to be all I need to run this, lacks both the sources and the Highlighter.gwt.xml file. By combining the contents of the other JARs ( http://cl.ly/K1N4 ), I managed to get to the following exception, where I'm stuck now:

http://www.pastit.dotcloud.com/131

My setting is the following: I have the custom built jar on my build path. My MainPage.gwt.xml contains the following line:

<inherits name='com.alexgorbatchev.syntaxhighlighter.Highlighter'/>

And somewhere in my code I do the following:

final Highlighter highlighter = new Highlighter(Brushes.XML());
highlighter.setText(xml);
final VerticalPanel xmlPanel = new VerticalPanel();
xmlPanel.add(highlighter);

As soon as the above line is reached, the exception I provided is thrown. Any kind of help is much appreciated!

Was it helpful?

Solution

I also tried to use gwt-syntaxhighlighter, but due to the lack of documentation, I preferred to write a wrapper around the JavaScript solution.

I downloaded the latest version of the .js files at http://alexgorbatchev.com/SyntaxHighlighter/download/ .

Then created a folder named 'public' at the same level as the 'client' (not in) folder and added the 'script' and 'style' from the zip file.

Then I included all my needs in my project *.gwt.xml

<!-- Base CSS SyntaxHighlighter -->
<stylesheet src="styles/shCore.css"/>

<!-- Eclipse Syntax Theme -->
<stylesheet src="styles/shThemeEclipse.css"/>

<!-- Base JS SyntaxHighlighter -->
<script src="scripts/shCore.js"/>

<!-- Extension for each wanted language to support -->
<script src="scripts/shBrushJava.js"/>
<script src="scripts/shBrushXml.js"/>

Then I created a factory of brushes

public class BrushFactory {
    public native JavaScriptObject newJavaBrush() /*-{
        return new $wnd.SyntaxHighlighter.brushes.Java();
    }-*/;

    public native JavaScriptObject newXmlBrush() /*-{
        return new $wnd.SyntaxHighlighter.brushes.Xml();
    }-*/;
}

This is a JSNI class that implement the transformation from String to HTML syntax formatted String using SyntaxHighlighter.

public class SyntaxHighlighter {
    public static native String highlight(String code, JavaScriptObject brush, boolean toolbar) /*-{
        var params = {};
        params['toolbar'] = toolbar;
        brush.init(params);
        return brush.getHtml(code);
    }-*/;
}

Finally I output the String using an HTML widget in GWT.

String htmlCode = SyntaxHighlighter.highlight("YOUR_CODE_HERE", BrushFactory.newJavaBrush(), false);
HTML html = new HTML();
html.setHTML(htmlCode);

I use it and it works like a charm. This solution is the easiest and fastest way to use the library.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top