private void cleanBundle(String bundle) { String scriptText = getText(); Lexer lexer = getLexer(); Iterable<Token> tokens = lexer.getTokens(scriptText); List<String> usedImages = new ArrayList<String>(); boolean inString = false; String current; for (Token t : tokens) { current = t.getValue(); if (t.getType() == TokenType.Comment) { cleanBundleCheckComments(lexer, t.getValue().substring(1), usedImages); continue; } if (t.getType() == TokenType.String_Doc) { cleanBundleCheckComments( lexer, t.getValue().substring(3, t.getValue().length() - 3), usedImages); continue; } if (!inString) { if (!current.isEmpty() && "'\"".contains(current)) { inString = true; } continue; } if (!current.isEmpty() && "'\"".contains(current)) { inString = false; continue; } cleanBundleIsImageUsed(current, usedImages); } if (usedImages.isEmpty()) { return; } FileManager.deleteNotUsedImages(bundle, usedImages); }
@SuppressWarnings("unchecked") public static Lexer getByFullName(String pack, String sub, String name) throws ResolutionException { String fullname = name; if (!pack.isEmpty()) { if (!sub.isEmpty()) { fullname = pack + "." + sub + "." + fullname; } else { fullname = pack + "." + fullname; } } // Try cache Lexer lexer = lexers.get(fullname); if (lexer != null) return lexer; try { return (Lexer) Jygments.class.getClassLoader().loadClass(fullname).newInstance(); } catch (InstantiationException x) { } catch (IllegalAccessException x) { } catch (ClassNotFoundException x) { } InputStream stream = Util.getJsonFile(pack, sub, name, fullname); if (stream != null) { try { String converted = Util.rejsonToJson(stream); ObjectMapper objectMapper = new ObjectMapper(); objectMapper.getFactory().configure(JsonParser.Feature.ALLOW_COMMENTS, true); Map<String, Object> json = objectMapper.readValue(converted, HashMap.class); Object className = json.get("class"); if (className == null) className = ""; lexer = getByName(className.toString()); lexer.addJson(json); lexer.resolve(); if (lexer != null) { // Cache it Lexer existing = lexers.putIfAbsent(fullname, lexer); if (existing != null) lexer = existing; } return lexer; } catch (JsonParseException x) { throw new ResolutionException(x); } catch (JsonMappingException x) { throw new ResolutionException(x); } catch (IOException x) { throw new ResolutionException(x); } } return null; }
public static Lexer getForFileName(String fileName) throws ResolutionException { if (lexerMap.isEmpty()) { try { File jarFile = new File(Jygments.class.getProtectionDomain().getCodeSource().getLocation().toURI()); JarInputStream jarInputStream = new JarInputStream(new FileInputStream(jarFile)); try { for (JarEntry jarEntry = jarInputStream.getNextJarEntry(); jarEntry != null; jarEntry = jarInputStream.getNextJarEntry()) { if (jarEntry.getName().endsWith(Util.extJSON)) { String lexerName = jarEntry.getName(); // strip off the JSON file ending lexerName = lexerName.substring(0, lexerName.length() - Util.extJSON.length()); Lexer lexer = Lexer.getByFullName(lexerName); for (String filename : lexer.filenames) if (filename.startsWith("*.")) lexerMap.put(filename.substring(filename.lastIndexOf('.')), lexer); } } } finally { jarInputStream.close(); } } catch (URISyntaxException x) { throw new ResolutionException(x); } catch (FileNotFoundException x) { throw new ResolutionException(x); } catch (IOException x) { throw new ResolutionException(x); } } return lexerMap.get(fileName.substring(fileName.lastIndexOf('.'))); }
private Lexer getLexer() { // TODO this only works for cleanbundle to find the image strings String scriptType = "python"; if (null != lexers.get(scriptType)) { return lexers.get(scriptType); } try { Lexer lexer = Lexer.getByName(scriptType); lexers.put(scriptType, lexer); return lexer; } catch (ResolutionException ex) { return null; } }
private void cleanBundleCheckComments(Lexer lexer, String comment, List<String> usedImages) { Iterable<Token> tokens = lexer.getTokens(comment); boolean inString = false; String current; for (Token t : tokens) { current = t.getValue(); if (!inString) { if ("'\"".contains(current)) { inString = true; } continue; } if ("'\"".contains(current)) { inString = false; continue; } cleanBundleIsImageUsed(current, usedImages); } }