@Override public String process(final String filename, final String source, final Config conf, final ClassLoader loader) throws Exception { String path = filename; try { LessCompiler compiler = new ThreadUnsafeLessCompiler(); LessSource src = new LessStrSource(source, path); CompilationResult result = compiler.compile(src, lessConf(conf)); result.getWarnings().forEach(warn -> { log.warn("{}:{}:{}:{}: {}", path, warn.getType(), warn.getLine(), warn.getCharacter(), warn.getMessage()); }); if (path.endsWith(".map")) { return result.getSourceMap(); } else { return result.getCss(); } } catch (Less4jException ex) { List<AssetProblem> problems = ex.getErrors().stream() .map(it -> new AssetProblem(path, it.getLine(), it.getCharacter(), it.getMessage(), null)) .collect(Collectors.toList()); throw new AssetException(name(), problems); } }
@Override public InputStream open(String s, URLConnection resource) throws IOException { LessCompiler compiler = new ThreadUnsafeLessCompiler(); try { LessCompiler.CompilationResult result = compiler.compile(resource.getURL()); return new ByteArrayInputStream(result.getCss().getBytes()); } catch (Less4jException e) { List<LessCompiler.Problem> errors = e.getErrors(); ArrayList<Message> messages = new ArrayList<Message>(errors.size()); for (LessCompiler.Problem error : errors) { String text = error.getMessage() != null ? error.getMessage() : "There is an error in your .less file"; String errorName = error.getType().name(); LessSource source = error.getSource(); Message msg; if (source != null) { msg = new Message(MetaModelPluginImpl.COMPILATION_ERROR, errorName, source.getName(), error.getLine(), text); } else { msg = new Message(MetaModelPluginImpl.GENERAL_PROBLEM, errorName, text); } MetaModelPluginImpl.log.info(msg.toDisplayString()); messages.add(msg); } throw new ProcessingException(messages); } } }
@Override protected void sendResource(URL resourceUrl, RouteContext routeContext) throws IOException { try { // compile less to css log.trace("Send css for '{}'", resourceUrl); LessSource.URLSource source = new LessSource.URLSource(resourceUrl); String content = source.getContent(); String result = sourceMap.get(content); if (result == null) { ThreadUnsafeLessCompiler compiler = new ThreadUnsafeLessCompiler(); LessCompiler.Configuration configuration = new LessCompiler.Configuration(); configuration.setCompressing(minify); LessCompiler.CompilationResult compilationResult = compiler.compile(resourceUrl, configuration); for (LessCompiler.Problem warning : compilationResult.getWarnings()) { log.warn("Line: {}, Character: {}, Message: {} ", warning.getLine(), warning.getCharacter(), warning.getMessage()); } result = compilationResult.getCss(); if (routeContext.getApplication().getPippoSettings().isProd()) { sourceMap.put(content, result); } } // send css routeContext.getResponse().contentType("text/css"); routeContext.getResponse().ok().send(result); } catch (Exception e) { throw new PippoRuntimeException(e); } }
@Override public InputStream open(String s, URLConnection resource) throws IOException { LessCompiler compiler = new ThreadUnsafeLessCompiler(); try { LessCompiler.CompilationResult result = compiler.compile(resource.getURL()); return new ByteArrayInputStream(result.getCss().getBytes()); } catch (Less4jException e) { List<LessCompiler.Problem> errors = e.getErrors(); ArrayList<Message> messages = new ArrayList<Message>(errors.size()); for (LessCompiler.Problem error : errors) { String text = error.getMessage() != null ? error.getMessage() : "There is an error in your .less file"; String errorName = error.getType().name(); LessSource source = error.getSource(); Message msg; if (source != null) { msg = new Message(MetaModelPluginImpl.COMPILATION_ERROR, errorName, source.getName(), error.getLine(), text); } else { msg = new Message(MetaModelPluginImpl.GENERAL_PROBLEM, errorName, text); } MetaModelPluginImpl.log.info(msg.toDisplayString()); messages.add(msg); } throw new ProcessingException(messages); } } }
@Override public void render(LessCompiler.Problem problem, MarkupWriter writer) { List<String> strings = CollectionFactory.newList(); if (InternalUtils.isNonBlank(problem.getMessage())) { strings.add(problem.getMessage()); } // Inside WRO4J we see that the LessSource is a StringSource with no useful toString(), so // it is omitted. We may need to create our own processors, stripping away a couple of layers of // WRO4J to get proper exception reporting! if (problem.getLine() > 0) { strings.add("line " + problem.getLine()); } if (problem.getCharacter() > 0) { strings.add("position " + problem.getCharacter()); } writer.write(InternalUtils.join(strings, " - ")); } });
@Override public void render(LessCompiler.Problem problem, MarkupWriter writer) { List<String> strings = CollectionFactory.newList(); if (InternalUtils.isNonBlank(problem.getMessage())) { strings.add(problem.getMessage()); } // Inside WRO4J we see that the LessSource is a StringSource with no useful toString(), so // it is omitted. We may need to create our own processors, stripping away a couple of layers of // WRO4J to get proper exception reporting! if (problem.getLine() > 0) { strings.add("line " + problem.getLine()); } if (problem.getCharacter() > 0) { strings.add("position " + problem.getCharacter()); } writer.write(InternalUtils.join(strings, " - ")); } });
private String codeSnippet(Problem problem) { StringBuilder result = new StringBuilder(); String[] lines = getContentLines(problem); if (lines == null || lines.length == 0) return result.toString(); int errorLine = problem.getLine(); int start = errorLine - 1; int end = errorLine + 1; start = start < 1 ? 1 : start; end = end > lines.length ? lines.length : end; int numberLength = String.valueOf(end).length()+1; for (int i = start; i<=end; i++) { result.append("\n").append(toPaddedNumber(i, numberLength)).append(": "); result.append(lines[i-1]); } result.append("\n"); return result.toString(); }
private String[] getContentLines(Problem problem) { try { LessSource source = problem.getSource(); if (source==null) return new String[0]; String content = source.getContent(); if (content == null) return new String[0]; return content.split("\n"); } catch (FileNotFound e) { } catch (CannotReadFile e) { } return new String[0]; }
private String problemAsString(final Problem problem) { return String.format("%s:%s %s.", problem.getLine(), problem.getCharacter(), problem.getMessage()); } }
private static String format(Problem warning) { return "WARNING " + warning.getLine() +":" + warning.getCharacter()+ " " + warning.getMessage(); } }
private String problemAsString(Problem problem) { return String.format("%s:%s %s.", problem.getLine(), problem.getCharacter(), problem.getMessage()); } }
private String problemAsString(final Problem problem) { return String.format("%s:%s %s.", problem.getLine(), problem.getCharacter(), problem.getMessage()); } }
private String toLineCharReport(Problem problem) { if (problem.getLine() == -1 || problem.getCharacter() == -1) return ""; return problem.getLine() + ":" + problem.getCharacter(); }
private String toString(Problem problem, SourceNamePrinter sourceNamePrinter) { String filename = sourceNamePrinter.printSourceName(problem.getSource()); if (!filename.isEmpty()) filename = filename + " "; String lineChar = toLineCharReport(problem); if (!lineChar.isEmpty()) lineChar = lineChar + " "; String snippet = codeSnippet(problem); return filename + lineChar + problem.getMessage() + snippet; }
@Override public String toString() { final StringBuilder builder = new StringBuilder("less4j errors while generating '" + name + "':\n"); for (LessCompiler.Problem error : e.getErrors()) { builder.append(" > [").append(error.getType()).append("] ") .append(error.getMessage()) .append(" in line ").append(error.getLine()) .append(":").append(error.getCharacter()).append("\n"); } return builder.toString(); } }
@Override protected void sendResource(URL resourceUrl, RouteContext routeContext) throws IOException { try { // compile less to css log.trace("Send css for '{}'", resourceUrl); LessSource.URLSource source = new LessSource.URLSource(resourceUrl); String content = source.getContent(); String result = sourceMap.get(content); if (result == null) { ThreadUnsafeLessCompiler compiler = new ThreadUnsafeLessCompiler(); LessCompiler.Configuration configuration = new LessCompiler.Configuration(); configuration.setCompressing(minify); LessCompiler.CompilationResult compilationResult = compiler.compile(resourceUrl, configuration); for (LessCompiler.Problem warning : compilationResult.getWarnings()) { log.warn("Line: {}, Character: {}, Message: {} ", warning.getLine(), warning.getCharacter(), warning.getMessage()); } result = compilationResult.getCss(); if (routeContext.getApplication().getPippoSettings().isProd()) { sourceMap.put(content, result); } } // send css routeContext.getResponse().contentType("text/css"); routeContext.getResponse().ok().send(result); } catch (Exception e) { throw new PippoRuntimeException(e); } }