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 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; }
LessSource dataSource = source.relativeSource(filename); byte[] data = dataSource.getBytes();
LessSource importedSource; try { importedSource = source.relativeSource(filename); } catch (FileNotFound ex) { return importFileNotFound(importNode, filename); try { if (importNode.isInline()) { ASTCssNode importedNode = replaceByInlineValue(importNode, importedSource.getContent()); alreadyImportedSources.add(importedSource); configureVisibilityBlocks(importNode, Arrays.asList(importedNode));
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); } }
LessSource dataSource = source.relativeSource(filename); byte[] data = dataSource.getBytes();
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 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); }
private String toSourceContent(HiddenTokenAwareTree underlyingStructure, String sourceName, LessSource source) { if (configuration.isIncludeSourcesContent() || sourceName==null || includeContentOf.contains(source)) { if (cachedContent.containsKey(source)) { return cachedContent.get(source); } String content; try { content = source.getContent(); } catch (FileNotFound e) { throw new BugHappened("How did we compiled something we did not read?", underlyingStructure); } catch (CannotReadFile e) { throw new BugHappened("How did we compiled something we did not read?", underlyingStructure); } cachedContent.put(source, content); return content; } else { return null; } }
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; }
@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 StyleSheet getImportedAst(Import node, LessSource source) throws FileNotFound, CannotReadFile { StyleSheet importedAst = (StyleSheet) astCache.getAst(source); if (importedAst == null) { importedAst = parseContent(node, source.getContent(), source); astCache.setAst(source, importedAst); } return importedAst.clone(); }
@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 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; }