List of usage examples for com.google.gwt.dev.cfg ModuleDef findPublicFile
public synchronized Resource findPublicFile(String partialPath)
From source file:cc.alcina.framework.entity.gen.SimpleCssResourceGenerator.java
License:Apache License
private String replaceWithDataUrls(ResourceContext context, String toWrite) throws Exception { Pattern urlPat = Pattern.compile("url\\s*\\((?!'?data:)(?!http:)(.+?)\\)"); Matcher m = urlPat.matcher(toWrite); while (m.find()) { String url = m.group(1);//ww w . j a va2s.c o m int qIdx = url.indexOf('?'); if (qIdx != -1) { url = url.substring(0, qIdx); } // url = url.replaceFirst("(.+?)\\?.*", "$1"); url = url.replace("'", "").replace("\"", ""); StandardGeneratorContext generatorContext = (StandardGeneratorContext) context.getGeneratorContext(); Field compilerContextField = StandardGeneratorContext.class.getDeclaredField("compilerContext"); compilerContextField.setAccessible(true); CompilerContext compilerContext = (CompilerContext) compilerContextField.get(generatorContext); Field moduleField = CompilerContext.class.getDeclaredField("module"); moduleField.setAccessible(true); ModuleDef module = (ModuleDef) moduleField.get(compilerContext); Resource resource = module.findPublicFile(url); if (resource == null) { resource = module.findPublicFile("gwt/standard/" + url); } if (resource == null) { if (url.contains("://")) { continue; } else { if (logMissingUrlResources) { String[] pub = getAllPublicFiles(module); // System.out.println("missing url resource - " + url); for (String path : pub) { if (path.contains(url)) { System.out.format("Maybe - %s : %s\n", url, path); } } } continue; } } InputStream contents = resource.openContents(); byte[] bytes = ResourceUtilities.readStreamToByteArray(contents); String out = Base64.encodeBytes(bytes); String fileName = url.replaceFirst(".+/", ""); String extension = fileName.replaceFirst(".+\\.", ""); String mimeType = null; if (extension.toLowerCase().equals("gif")) { mimeType = "image/gif"; } else if (extension.toLowerCase().equals("jpeg")) { mimeType = "image/jpeg"; } else if (extension.toLowerCase().equals("jpg")) { mimeType = "image/jpeg"; } else if (extension.toLowerCase().equals("png")) { mimeType = "image/png"; } if (mimeType != null) { String encoded = String.format("url(data:%s;base64,%s)", mimeType, out.replace("\n", "")); if (encoded.length() > 5000) { // System.out.println("warn - large css sprite - " + url); } if (encoded.length() < MAX_DATA_URL_LENGTH) { toWrite = m.replaceFirst(encoded); m = urlPat.matcher(toWrite); } } else { System.out.println("unable to resolve mime type - " + url); // continue on } } return toWrite; }