diff --git a/src/main/java/org/lesscss/FileResource.java b/src/main/java/org/lesscss/FileResource.java index 5de1ce1..2c21a71 100644 --- a/src/main/java/org/lesscss/FileResource.java +++ b/src/main/java/org/lesscss/FileResource.java @@ -7,43 +7,60 @@ /** * File based implementation of {@link Resource}. - * + * * @author Anton Pechinsky */ public class FileResource implements Resource { - private File file; + private final File file; - public FileResource(File file) { - if (file == null) { - throw new IllegalArgumentException("File must not be null!"); - } - this.file = file; - } + private File sourceDirectory; - public boolean exists() { - return file.exists(); + public FileResource(File file) { + if (file == null) { + throw new IllegalArgumentException("File must not be null!"); } + this.file = file; + } - public InputStream getInputStream() throws IOException { - return new FileInputStream(file); + public FileResource(File sourceDirectory, File file) { + this(file); + if (sourceDirectory == null) { + throw new IllegalArgumentException("sourceDirectory must not be null!"); } + this.sourceDirectory = sourceDirectory; + } - public long lastModified() { - return file.lastModified(); - } + public boolean exists() { + return file.exists(); + } - public Resource createRelative(String relativePath) { - File relativeFile = new File(file.getParentFile(), relativePath); - return new FileResource(relativeFile); - } + public InputStream getInputStream() throws IOException { + return new FileInputStream(file); + } - @Override - public String toString() { - return file.getAbsolutePath(); - } + public long lastModified() { + return file.lastModified(); + } - public String getName() { - return file.getAbsolutePath(); + public Resource createRelative(String relativePath) { + + File relativeFile; + if (sourceDirectory != null && relativePath.startsWith("/")) { + relativeFile = new File(sourceDirectory, relativePath); + return new FileResource(sourceDirectory, relativeFile); + } else { + relativeFile = new File(file.getParentFile(), relativePath); + return new FileResource(relativeFile); } + } + + @Override + public String toString() { + return file.getAbsolutePath(); + } + + public String getName() { + return file.getAbsolutePath(); + } } diff --git a/src/main/java/org/lesscss/LessSource.java b/src/main/java/org/lesscss/LessSource.java index 30b6f98..a7e4ef5 100644 --- a/src/main/java/org/lesscss/LessSource.java +++ b/src/main/java/org/lesscss/LessSource.java @@ -100,7 +100,11 @@ public LessSource(Resource resource, Charset charset) throws IOException { public LessSource(File input) throws IOException { this( new FileResource(input) ); } - + + public LessSource(File sourceDirectory, File input) throws IOException { + this( new FileResource(sourceDirectory, input) ); + } + private String loadResource(Resource resource, Charset charset) throws IOException { BOMInputStream inputStream = new BOMInputStream( resource.getInputStream() ); try {