import com.sun.javafx.css.parser.CSSParser; import javafx.fxml.FXMLLoader; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.StringReader; import java.net.URI; import java.net.URL; import java.net.URLConnection; import java.net.URLStreamHandler; import java.net.URLStreamHandlerFactory; import java.util.Base64; /** * Created by dgrieve on 8/7/14. */ public class Main { public static void main(String[] args) { // parser needs fixing! CSSParser.getInstance().parseExpr("foo", "url(data:text/xml,);"); try { URL.setURLStreamHandlerFactory(new URLStreamHandlerFactory() { URLStreamHandler streamHandler = new URLStreamHandler() { @Override protected URLConnection openConnection(URL url) throws IOException { return new URLConnection(url) { @Override public void connect() throws IOException { } @Override public InputStream getInputStream() throws IOException { String file = url.getFile(); String data = file.substring(file.indexOf(',') + 1); return new ByteArrayInputStream(Base64.getDecoder().decode(data)); } @Override public Object getContent() throws IOException { FXMLLoader loader = new FXMLLoader(url); return loader.load(); } }; } }; @Override public URLStreamHandler createURLStreamHandler(String protocol) { if ("data".equals(protocol)) { return streamHandler; } return null; } }); // 1 2 3 4 5 6 7 // 01234567890123456789012345678901234567890123456789012345678901234567890 StringReader stringReader = new StringReader(""); // encoding to base64 would be done in URLConverter if not already base64 ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); int ch = -1; while((ch = stringReader.read()) != -1) { byteArrayOutputStream.write(ch); } String s = Base64.getEncoder().encodeToString(byteArrayOutputStream.toByteArray()); String uriString = "data:text/xml," + s; URL url = URI.create(uriString).toURL(); // for now, getContent would be called from Labeled's handling of applyStyle System.err.println(url.getContent()); } catch (IOException exception ) { System.err.println(exception); System.exit(-1); } } }