FredQc
FredQc

Reputation: 66

Show text file without ".txt" extension in WebView

I use a Gradle plugin that generates automatically a report to show all the libraries used in my project. The report is an html file. The html file has links to the licenses text files.

My purpose is to show the report html file in the webview. That's work like a charm.

But some links to the licenses files works, some others doesn't work...

I realize that it is only the links of license file that end with ".txt" that is working.

I make an isolated test to be sure and the problem is replicable.

This piece of code assume that there is two text files in resources : "file" and "file.txt".

public void start(Stage primaryStage) throws Exception
{
    WebView webView = new WebView();

    URL url = this.getClass().getResource("file.txt"); // OK!
    //URL url = this.getClass().getResource("file"); // Doesn't work
    webView.getEngine().load(url.toString());

    StackPane pane = new StackPane(webView);

    Scene scene = new Scene(pane, 400, 400);

    primaryStage.setScene(scene);

    primaryStage.show();
}

How can I force the WebView to load my text files without the ".txt" extension?

I know that a quick workaround would be to add ".txt" to the text files name and to adjust the html code, but I use over 90 libraries...

Upvotes: 2

Views: 532

Answers (1)

FredQc
FredQc

Reputation: 66

I found a satisfying solution for my problem.

I got the idea from MMAdams comment.

Note that is not applicable to all situations.

In my case, I know that all the files to be viewed are local, more precisely bundle in the jar.

I notice that the webview was cancelling the rendering of the file when it was a text file without the extension ".txt".

So, I configure the webview to act a specific way when it was entering in a "cancelling" state:

public void start(Stage primaryStage) throws Exception
{
    WebView webView = new WebView();

    webView.getEngine().getLoadWorker().stateProperty().addListener(new ChangeListener<Worker.State>()
    {
        @Override
        public void changed(ObservableValue ov, Worker.State oldState,
                Worker.State newState)
        {                
            if (newState == Worker.State.CANCELLED)
            {
                try
                {
                    URL fileURL = new URL(webView.getEngine().getLocation());

                    // Convert content of the file to a String using an InputStream
                    // as the file is in the resources (bundle in jar) so it cannot
                    // be access with File.
                    try (InputStream fileStream = fileURL.openStream())
                    {
                        Scanner s = new Scanner(fileURL.openStream(), "UTF-8").useDelimiter("\\A");
                        String content = s.hasNext() ? s.next() : "";
                        webView.getEngine().loadContent(content, "text/plain");
                    }
                }
                catch (Exception ex)
                {
                   ex.printStackTrace();
                }
            }
        }
    });


    //URL url = this.getClass().getResource("file.txt"); // OK!
    URL url = this.getClass().getResource("file"); // Now working...
    webView.getEngine().load(url.toString());

    StackPane pane = new StackPane(webView);

    Scene scene = new Scene(pane, 400, 400);

    primaryStage.setScene(scene);

    primaryStage.show();
}

Upvotes: 1

Related Questions