@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); } }
private Map<String, HiddenTokenAwareTree> toAntlrTree(Map<String, String> variables) throws Less4jException { Map<String, HiddenTokenAwareTree> result = new HashMap<String, HiddenTokenAwareTree>(); List<Problem> problems = new ArrayList<Problem>(); for (Entry<String, String> entry : variables.entrySet()) { String nameStr = entry.getKey(); String valueStr = entry.getValue(); ParseResult valueParseResult = toAntlrExpressionTree(nameStr, valueStr); problems.addAll(valueParseResult.getErrors()); result.put(nameStr, valueParseResult.getTree()); } if (!problems.isEmpty()) { CompilationResult compilationResult = new CompilationResult("Errors parsing custom variables, partial result is not available."); throw new Less4jException(problems, compilationResult); } return result; }
public void reportErrorsAndWarnings(Less4jException ex, String inputfileName, File rootInputFile) { printWarnings(inputfileName, rootInputFile, ex.getPartialResult()); reportErrors(ex, inputfileName, rootInputFile); }
public Less4jException(List<Problem> errors, CompilationResult partialResult) { super(); this.errors = errors; this.partialResult = partialResult; this.message = createMessage(); }
@Override public String compile(SourceFile sourceFile) { try { Configuration configuration = new Configuration(); configureSourceMap(configuration); return new ThreadUnsafeLessCompiler().compile(new PathSource(resources, sourceFile), configuration).getCss(); } catch (Less4jException e) { String message = cleanMessage(sourceFile, e.getMessage()); throw new CompilerException(message); } }
private ParseResult toAntlrTree(LessSource source) throws Less4jException { ParseResult result; try { result = parser.parseStyleSheet(source.getContent(), source); } catch (FileNotFound ex) { throw new Less4jException(new GeneralProblem("The file " + source + " does not exists."), new CompilationResult(null)); } catch (CannotReadFile ex) { throw new Less4jException(new GeneralProblem("Cannot read the file " + source + "."), new CompilationResult(null)); } if (result.hasErrors()) { CompilationResult compilationResult = new CompilationResult("Errors during parsing phase, partial result is not available."); throw new Less4jException(result.getErrors(), compilationResult); } return result; }
private void runAsSinglemode(List<String> files, boolean generateSourceMap, boolean isCompressing, boolean ieCompatibility, boolean printPartial) { if (files.isEmpty()) { print.reportError("No file available."); return; } String lessFileName = files.get(0); File lessFile = toFile(lessFileName); String cssFileName = singleModeCssFilename(files); File cssFile = toFile(cssFileName); String mapFileName = singleModeMapFilename(cssFileName, generateSourceMap); try { CompilationResult content = compile(lessFile, cssFile, generateSourceMap, isCompressing, ieCompatibility); singleModePrint(files, lessFileName, lessFile, cssFileName, mapFileName, content); } catch (Less4jException ex) { CompilationResult partialResult = ex.getPartialResult(); if (printPartial) { singleModePrint(files, lessFileName, lessFile, cssFileName, null, partialResult); print.reportErrors(ex, lessFileName, lessFile); } else { print.reportErrorsAndWarnings(ex, lessFileName, lessFile); } print.reportCouldNotCompileTheFile(lessFileName); } }
public void reportErrors(Less4jException ex, String inputfileName, File rootInputFile) { SourceNamePrinter sourceNamePrinter = new RelativeFileSourceNamePrinter(rootInputFile); ProblemsPrinter problemsPrinter = new ProblemsPrinter(sourceNamePrinter); if (!ex.getErrors().isEmpty()) standardErr.println("Errors produced by compilation of " + inputfileName); String errors = problemsPrinter.printErrors(ex.getErrors()); standardErr.print(errors); }
@Override public CompilationResult compile(LessSource source, Configuration options) throws Less4jException { if (options == null) options = new Configuration(); problemsHandler = new ProblemsHandler(); astBuilder = new ASTBuilder(problemsHandler); compiler = new LessToCssCompiler(problemsHandler, options); CompilationResult compilationResult = doCompile(source, options); if (problemsHandler.hasErrors()) { throw new Less4jException(problemsHandler.getErrors(), compilationResult); } return compilationResult; }
private void runAsMultimode(List<String> files, String outputDirectory, boolean generateSourceMap, boolean isCompressing, boolean ieCompatibility, boolean printPartial) { if (!print.ensureDirectory(outputDirectory)) return; for (String filename : files) { File inputFile = new File(filename); String cssFilename = toOutputFilename(outputDirectory, filename, Constants.CSS_SUFFIX); String mapFilename = generateSourceMap? toOutputFilename(outputDirectory, filename, Constants.FULL_SOURCE_MAP_SUFFIX): null; try { CompilationResult content = compile(inputFile, toFile(cssFilename), generateSourceMap, isCompressing, ieCompatibility); print.printToFiles(content, filename, inputFile, cssFilename, mapFilename); } catch (Less4jException ex) { CompilationResult partialResult = ex.getPartialResult(); if (printPartial) { print.printToFiles(partialResult, filename, inputFile, cssFilename, mapFilename); print.reportErrors(ex, filename, inputFile); } else { print.reportErrorsAndWarnings(ex, filename, inputFile); } print.reportCouldNotCompileTheFile(filename); } } }
@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 public void process(final Resource resource, final Reader reader, final Writer writer) throws IOException { try { final LessSource lessSource = new RelativeAwareLessSource(resource, IOUtils.toString(reader), locatorFactory); final CompilationResult result = compiler.compile(lessSource); logWarnings(result); writer.write(result.getCss()); } catch (final Less4jException e) { LOG.error("Failed to compile less resource: {}.", resource); for (final Problem problem : e.getErrors()) { LOG.error(problemAsString(problem)); } throw WroRuntimeException.wrap(e); } }
@Override public void process(final Resource resource, final Reader reader, final Writer writer) throws IOException { try { final LessSource lessSource = new RelativeAwareLessSource(resource, IOUtils.toString(reader), locatorFactory); final CompilationResult result = compiler.compile(lessSource); logWarnings(result); writer.write(result.getCss()); } catch (final Less4jException e) { LOG.error("Failed to compile less resource: {}.", resource); for (final Problem problem : e.getErrors()) { LOG.error(problemAsString(problem)); } throw WroRuntimeException.wrap(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 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); } } }