• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

hazendaz / smartsprites / 555

21 May 2026 03:26PM UTC coverage: 87.799%. Remained the same
555

push

github

hazendaz
[tests] Fix tests that were looking at size of original license before spdx change

557 of 670 branches covered (83.13%)

Branch coverage included in aggregate %.

1350 of 1502 relevant lines covered (89.88%)

0.9 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

80.0
/src/main/java/org/carrot2/labs/smartsprites/resource/FileSystemResourceHandler.java
1
/*
2
 * SPDX-License-Identifier: BSD-3-Clause
3
 * See LICENSE file for details.
4
 *
5
 * Copyright 2021-2026 Hazendaz
6
 * Copyright (C) 2007-2009, Stanisław Osiński.
7
 */
8
package org.carrot2.labs.smartsprites.resource;
9

10
import java.io.File;
11
import java.io.IOException;
12
import java.io.InputStream;
13
import java.io.InputStreamReader;
14
import java.io.OutputStream;
15
import java.io.OutputStreamWriter;
16
import java.io.Reader;
17
import java.io.UnsupportedEncodingException;
18
import java.io.Writer;
19
import java.nio.charset.Charset;
20
import java.nio.file.Files;
21
import java.nio.file.Path;
22

23
import org.apache.commons.io.FilenameUtils;
24
import org.carrot2.labs.smartsprites.SmartSpritesParameters;
25
import org.carrot2.labs.smartsprites.message.Message;
26
import org.carrot2.labs.smartsprites.message.Message.MessageType;
27
import org.carrot2.labs.smartsprites.message.MessageLog;
28
import org.carrot2.util.FileUtils;
29
import org.carrot2.util.StringUtils;
30

31
/**
32
 * This class defines the resource handler which manage resources from the file system.
33
 *
34
 * @author Ibrahim Chaehoi
35
 * @author Stanislaw Osinski
36
 */
37
public class FileSystemResourceHandler implements ResourceHandler {
38

39
    /** The message log. */
40
    private final MessageLog messageLog;
41

42
    /** The root directory. */
43
    private final String documentRootDir;
44

45
    /** The charset to assume in the {@link #getResourceAsReader(String)} method. */
46
    private final Charset charset;
47

48
    /**
49
     * Creates a new {@link FileSystemResourceHandler}.
50
     *
51
     * @param documentRootDirPath
52
     *            the document root directory path, can be <code>null</code>
53
     * @param charset
54
     *            the charset to assume in the {@link #getResourceAsReader(String)} method
55
     * @param messageLog
56
     *            the message log
57
     */
58
    public FileSystemResourceHandler(String documentRootDirPath, String charset, MessageLog messageLog) {
1✔
59
        this.documentRootDir = documentRootDirPath;
1✔
60
        this.messageLog = messageLog;
1✔
61
        this.charset = Charset.forName(charset);
1✔
62
    }
1✔
63

64
    @Override
65
    public InputStream getResourceAsInputStream(String path) throws IOException {
66
        return Files.newInputStream(FileUtils.getCanonicalOrAbsoluteFile(path).toPath());
1✔
67
    }
68

69
    @Override
70
    public Reader getResourceAsReader(String path) throws IOException {
71
        try {
72
            return new InputStreamReader(getResourceAsInputStream(path), charset);
1✔
73
        } catch (UnsupportedEncodingException e) {
×
74
            // Should not happen as we're checking the charset in constructor
75
            throw new RuntimeException(e);
×
76
        }
77
    }
78

79
    @Override
80
    public OutputStream getResourceAsOutputStream(String path) throws IOException {
81
        // Create directories if needed
82
        final File parentFile = Path.of(path).toFile().getParentFile();
1✔
83
        if (!parentFile.exists() && !parentFile.mkdirs()) {
1!
84
            messageLog.warning(Message.MessageType.CANNOT_CREATE_DIRECTORIES, parentFile.getPath());
×
85
        }
86
        return Files.newOutputStream(FileUtils.getCanonicalOrAbsoluteFile(path).toPath());
1✔
87
    }
88

89
    @Override
90
    public Writer getResourceAsWriter(String path) throws IOException {
91
        try {
92
            return new OutputStreamWriter(getResourceAsOutputStream(path), charset);
1✔
93
        } catch (UnsupportedEncodingException e) {
×
94
            // Should not happen as we're checking the charset in constructor
95
            throw new RuntimeException(e);
×
96
        }
97
    }
98

99
    /**
100
     * This implementation detects if the resource path starts with a "/" and resolves such resources against the
101
     * provided {@link SmartSpritesParameters#getDocumentRootDir()} directory.
102
     */
103
    @Override
104
    public String getResourcePath(String baseFile, String filePath) {
105
        if (!filePath.startsWith("/")) {
1✔
106
            return FilenameUtils.concat(FilenameUtils.getFullPath(baseFile), filePath);
1✔
107
        }
108
        if (StringUtils.isNotBlank(documentRootDir)) {
1✔
109
            return FilenameUtils.concat(documentRootDir, filePath.substring(1));
1✔
110
        }
111
        messageLog.warning(MessageType.ABSOLUTE_PATH_AND_NO_DOCUMENT_ROOT, filePath);
1✔
112
        return "";
1✔
113
    }
114
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc