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

hazendaz / smartsprites / #43

11 Nov 2023 07:47PM UTC coverage: 88.431%. Remained the same
#43

push

github

hazendaz
[tests] Fix tests given they expected order and now license on everything for +36

1437 of 1625 relevant lines covered (88.43%)

0.88 hits per line

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

65.22
/src/main/java/org/carrot2/labs/smartsprites/resource/FileSystemResourceHandler.java
1
/*
2
 * SmartSprites Project
3
 *
4
 * Copyright (C) 2007-2009, Stanisław Osiński.
5
 * All rights reserved.
6
 *
7
 * Redistribution and use in source and binary forms, with or without modification,
8
 * are permitted provided that the following conditions are met:
9
 *
10
 * - Redistributions of  source code must  retain the above  copyright notice, this
11
 *   list of conditions and the following disclaimer.
12
 *
13
 * - Redistributions in binary form must reproduce the above copyright notice, this
14
 *   list of conditions and the following  disclaimer in  the documentation  and/or
15
 *   other materials provided with the distribution.
16
 *
17
 * - Neither the name of the SmartSprites Project nor the names of its contributors
18
 *   may  be used  to endorse  or  promote  products derived   from  this  software
19
 *   without specific prior written permission.
20
 *
21
 * - We kindly request that you include in the end-user documentation provided with
22
 *   the redistribution and/or in the software itself an acknowledgement equivalent
23
 *   to  the  following: "This product includes software developed by the SmartSprites
24
 *   Project."
25
 *
26
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"  AND
27
 * ANY EXPRESS OR  IMPLIED WARRANTIES, INCLUDING,  BUT NOT LIMITED  TO, THE IMPLIED
28
 * WARRANTIES  OF  MERCHANTABILITY  AND  FITNESS  FOR  A  PARTICULAR  PURPOSE   ARE
29
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE  FOR
30
 * ANY DIRECT, INDIRECT, INCIDENTAL,  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL  DAMAGES
31
 * (INCLUDING, BUT  NOT LIMITED  TO, PROCUREMENT  OF SUBSTITUTE  GOODS OR SERVICES;
32
 * LOSS OF USE, DATA, OR PROFITS;  OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND  ON
33
 * ANY  THEORY  OF  LIABILITY,  WHETHER  IN  CONTRACT,  STRICT  LIABILITY,  OR TORT
34
 * (INCLUDING NEGLIGENCE OR OTHERWISE)  ARISING IN ANY WAY  OUT OF THE USE  OF THIS
35
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36
 */
37
package org.carrot2.labs.smartsprites.resource;
38

39
import java.io.File;
40
import java.io.FileInputStream;
41
import java.io.FileOutputStream;
42
import java.io.IOException;
43
import java.io.InputStream;
44
import java.io.InputStreamReader;
45
import java.io.OutputStream;
46
import java.io.OutputStreamWriter;
47
import java.io.Reader;
48
import java.io.UnsupportedEncodingException;
49
import java.io.Writer;
50
import java.nio.charset.Charset;
51

52
import org.apache.commons.io.FilenameUtils;
53
import org.carrot2.labs.smartsprites.SmartSpritesParameters;
54
import org.carrot2.labs.smartsprites.message.Message;
55
import org.carrot2.labs.smartsprites.message.MessageLog;
56
import org.carrot2.labs.smartsprites.message.Message.MessageType;
57
import org.carrot2.util.FileUtils;
58
import org.carrot2.util.StringUtils;
59

60
/**
61
 * This class defines the resource handler which manage resources from the file system.
62
 * 
63
 * @author Ibrahim Chaehoi
64
 * @author Stanislaw Osinski
65
 */
66
public class FileSystemResourceHandler implements ResourceHandler
67
{
68
    /** The message log */
69
    private final MessageLog messageLog;
70

71
    /** The root directory */
72
    private final String documentRootDir;
73

74
    /** The charset to assume in the {@link #getResourceAsReader(String)} method. */
75
    private final Charset charset;
76

77
    /**
78
     * Creates a new {@link FileSystemResourceHandler}.
79
     * 
80
     * @param documentRootDirPath the document root directory path, can be <code>null</code>
81
     * @param charset the charset to assume in the {@link #getResourceAsReader(String)}
82
     *            method
83
     * @param messageLog the message log
84
     */
85
    public FileSystemResourceHandler(String documentRootDirPath, String charset,
86
        MessageLog messageLog)
87
    {
1✔
88
        this.documentRootDir = documentRootDirPath;
1✔
89
        this.messageLog = messageLog;
1✔
90
        this.charset = Charset.forName(charset);
1✔
91
    }
1✔
92

93
    public InputStream getResourceAsInputStream(String path) throws IOException
94
    {
95
        return new FileInputStream(FileUtils.getCanonicalOrAbsoluteFile(path));
1✔
96
    }
97

98
    public Reader getResourceAsReader(String path) throws IOException
99
    {
100
        try
101
        {
102
            return new InputStreamReader(getResourceAsInputStream(path), charset);
1✔
103
        }
104
        catch (UnsupportedEncodingException e)
×
105
        {
106
            // Should not happen as we're checking the charset in constructor
107
            throw new RuntimeException(e);
×
108
        }
109
    }
110

111
    public OutputStream getResourceAsOutputStream(String path) throws IOException
112
    {
113
        // Create directories if needed
114
        final File parentFile = new File(path).getParentFile();
1✔
115
        if (!parentFile.exists() && !parentFile.mkdirs())
1✔
116
        {
117
            messageLog.warning(Message.MessageType.CANNOT_CREATE_DIRECTORIES,
×
118
                parentFile.getPath());
×
119
        }
120
        return new FileOutputStream(FileUtils.getCanonicalOrAbsoluteFile(path));
1✔
121
    }
122

123
    public Writer getResourceAsWriter(String path) throws IOException
124
    {
125
        try
126
        {
127
            return new OutputStreamWriter(getResourceAsOutputStream(path), charset);
1✔
128
        }
129
        catch (UnsupportedEncodingException e)
×
130
        {
131
            // Should not happen as we're checking the charset in constructor
132
            throw new RuntimeException(e);
×
133
        }
134
    }
135

136
    /**
137
     * This implementation detects if the resource path starts with a "/" and resolves
138
     * such resources against the provided
139
     * {@link SmartSpritesParameters#getDocumentRootDir()} directory.
140
     */
141
    public String getResourcePath(String baseFile, String filePath)
142
    {
143
        if (filePath.startsWith("/"))
1✔
144
        {
145
            if (StringUtils.isNotBlank(documentRootDir))
1✔
146
            {
147
                return FilenameUtils.concat(documentRootDir, filePath.substring(1));
1✔
148
            }
149
            else
150
            {
151
                messageLog.warning(MessageType.ABSOLUTE_PATH_AND_NO_DOCUMENT_ROOT,
×
152
                    filePath);
153
                return "";
×
154
            }
155
        }
156
        else
157
        {
158
            return FilenameUtils.concat(FilenameUtils.getFullPath(baseFile), filePath);
1✔
159
        }
160
    }
161
}
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