private String getCssResultLocationName(Configuration options, LessSource source) { LessSource location = options.getCssResultLocation(); String name = location == null ? null : location.getName(); if (name == null) name = URIUtils.changeSuffix(source.getName(), Constants.CSS_SUFFIX); return name; }
@Override public String printSourceName(LessSource source) { if (source == null) return ""; URI uri = source.getURI(); if (uri!=null) return uri.toString(); String name = source.getName(); return name == null ? "" : name; }
public String toSourceMap() { for (LessSource source : additionalSourceFiles) { String sourceName = toSourceName(source); String sourceContent = toSourceContent(null, sourceName, source); generator.addSourceFile(sourceName, sourceContent); } // map file is assumed to have the same location as generated css String name = ""; if (cssDestination != null && cssDestination.getName() != null) { name = cssDestination.getName(); } try { StringBuilder sb = new StringBuilder(); generator.appendTo(sb, name); return sb.toString(); } catch (IOException e) { throw new IllegalStateException("Impossible to happen exception.", e); } }
@Override public String printSourceName(LessSource source) { if (source == null) return ""; File file = source instanceof LessSource.FileSource ? ((LessSource.FileSource) source).getInputFile() : null; if (file == null) { String name = source.getName(); return name == null ? "" : name; } if (rootInputFile == null) { return file.getPath(); } File absoluteRoot = rootInputFile.getAbsoluteFile(); File absoluteFile = file.getAbsoluteFile(); if (sameFile(absoluteRoot, absoluteFile)) return ""; return URIUtils.relativize(absoluteRoot.getParentFile(), absoluteFile); }
public static String relativizeSourceURIs(LessSource from, LessSource to) { if (to == null) return null; URI toURI = to.getURI(); if (toURI == null) return to.getName(); String toURIAsString = toURI.toString(); if (from == null) return toURIAsString; URI fromURI = from.getURI(); if (fromURI == null) return toURIAsString; String fromURIAsString = fromURI.toString(); return getRelativePath(fromURIAsString, toURIAsString, URIUtils.URI_FILE_SEPARATOR); }
@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); } } }
private CompilationResult createCompilationResult(ASTCssNode cssStyleSheet, LessSource lessSource, List<VariableDeclaration> externalVariables, Collection<LessSource> additionalSourceFiles, Configuration options) { LessSource cssDestination = options == null ? null : options.getCssResultLocation(); if (cssDestination == null) { String guessedCssName = URIUtils.changeSuffix(lessSource.getName(), Constants.CSS_SUFFIX); URI guessedURI = URIUtils.changeSuffix(lessSource.getURI(), Constants.CSS_SUFFIX); cssDestination = new LessSource.StringSource("", guessedCssName, guessedURI); } CssPrinter builder = new CssPrinter(lessSource, cssDestination, extractSources(externalVariables), additionalSourceFiles, options); builder.append(cssStyleSheet); StringBuilder css = builder.toCss(); String sourceMap = builder.toSourceMap(); handleSourceMapLink(cssStyleSheet, css, options, lessSource, sourceMap); CompilationResult compilationResult = new CompilationResult(css.toString(), sourceMap, problemsHandler.getWarnings()); return compilationResult; }