Created
May 11, 2014 19:08
-
-
Save weitzj/a35438be0e4162180fa0 to your computer and use it in GitHub Desktop.
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
import javafx.application.Application; | |
import javafx.application.Platform; | |
import javafx.concurrent.Worker; | |
import javafx.scene.Scene; | |
import javafx.scene.web.WebView; | |
import javafx.stage.Stage; | |
import org.w3c.dom.Node; | |
import javax.xml.transform.OutputKeys; | |
import javax.xml.transform.Transformer; | |
import javax.xml.transform.TransformerFactory; | |
import javax.xml.transform.dom.DOMSource; | |
import javax.xml.transform.stream.StreamResult; | |
import java.io.StringWriter; | |
/** | |
* User: Jan Weitz | |
* Date: 11/05/14 | |
* Time: 19:24 | |
*/ | |
public class LargeTagNames extends Application { | |
private static final String TEST_LOWERCASE_HTML = "" + | |
"<html>\n" + | |
" <head>\n" + | |
" <title>MyTitle</title>\n" + | |
" </head>\n" + | |
" <body>\n" + | |
" <div class=\"lowerCaseClass\">\n" + | |
" All Tags are UPPERCASE!\n" + | |
" </div>\n" + | |
" </body>\n" + | |
"</html>"; | |
public static void main(String[] args) { | |
Application.launch(LargeTagNames.class); | |
} | |
@Override | |
public void start(Stage primaryStage) throws Exception { | |
WebView webView = new WebView(); | |
//Show the pageSource from the webEngine in stdout once the page is loaded. | |
webView.getEngine().getLoadWorker().stateProperty().addListener( | |
(ov, oldState, newState) -> { | |
if (Worker.State.SUCCEEDED == newState) { | |
System.out.println("############################################################################"); | |
System.out.println("ORIGINAL CONTENT:\n" + TEST_LOWERCASE_HTML); | |
System.out.println("\n\n############################################################################"); | |
System.out.println("WEBVIEW DOCUMENT:\n" + nodeToString(webView.getEngine().getDocument())); | |
System.out.println("\n\n############################################################################"); | |
System.out.println("JAVASCRIPT OUTERHTML:\n" + webView.getEngine().executeScript("document.documentElement.outerHTML")); | |
Platform.runLater(() -> Platform.exit()); | |
} | |
} | |
); | |
webView.getEngine().loadContent(TEST_LOWERCASE_HTML); | |
primaryStage.setScene(new Scene(webView)); | |
} | |
private static String nodeToString(final Node node) { | |
if (null == node) return null; | |
StringWriter writer = new StringWriter(); | |
try { | |
Transformer transformer = TransformerFactory.newInstance().newTransformer(); | |
transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); | |
transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); | |
transformer.transform(new DOMSource(node), new StreamResult(writer)); | |
return writer.toString(); | |
} catch (Exception ex) { | |
ex.printStackTrace(); | |
return null; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Output: