public FileSource(FileSource parent, String filename, String charsetName) { this(parent, new File(parent.getInputFile().getParentFile(), filename), charsetName); }
private String getCanonicalPath() { try { return getInputFile().getCanonicalPath(); } catch (IOException e) { return getInputFile().getAbsolutePath(); } }
@Override public URI getURI() { try { String path = getInputFile().toString(); path = URIUtils.convertPlatformSeparatorToUri(path); return new URI(path); } catch (URISyntaxException e) { return null; } }
@Override public String getContent() throws FileNotFound, CannotReadFile { try { Reader input; if (charsetName != null) { input = new InputStreamReader(new FileInputStream(getInputFile()), charsetName); } else { input = new FileReader(getInputFile()); } try { String content = IOUtils.toString(input).replace("\r\n", "\n"); setLastModified(getInputFile().lastModified()); return content; } finally { input.close(); } } catch (FileNotFoundException ex) { throw new FileNotFound(); } catch (IOException ex) { throw new CannotReadFile(); } }
@Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; FileSource other = (FileSource) obj; String absoluteInputFile = getCanonicalPath(); if (absoluteInputFile == null) { if (other.getInputFile() != null) return false; } else if (!absoluteInputFile.equals(other.getCanonicalPath())) return false; if (charsetName == null) { if (other.charsetName != null) return false; } else if (!charsetName.equals(other.charsetName)) return false; return true; }
@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 String toString() { return getInputFile().toString(); }
@Override public String getName() { return getInputFile().getName(); }
public static FileSource changeSuffix(FileSource source, String dottedSuffix) { if (source == null) return null; return new LessSource.FileSource(changeSuffix(source.getInputFile(), dottedSuffix)); }
@Override public byte[] getBytes() throws FileNotFound, CannotReadFile { try { byte[] content = FileUtils.readFileToByteArray(getInputFile()); setLastModified(getInputFile().lastModified()); return content; } catch (FileNotFoundException ex) { throw new FileNotFound(); } catch (IOException ex) { throw new CannotReadFile(); } }