• 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

0.0
/src/main/java/org/carrot2/labs/smartsprites/ant/SmartSpritesTask.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.ant;
38

39
import java.io.*;
40
import java.util.ArrayList;
41
import java.util.Iterator;
42
import java.util.List;
43

44
import org.apache.tools.ant.BuildException;
45
import org.apache.tools.ant.Task;
46
import org.apache.tools.ant.types.FileSet;
47
import org.apache.tools.ant.types.Resource;
48
import org.carrot2.labs.smartsprites.SmartSpritesParameters;
49
import org.carrot2.labs.smartsprites.SpriteBuilder;
50
import org.carrot2.labs.smartsprites.SmartSpritesParameters.PngDepth;
51
import org.carrot2.labs.smartsprites.message.*;
52
import org.carrot2.labs.smartsprites.message.Message.MessageLevel;
53
import org.carrot2.util.EnumUtils;
54

55
/**
56
 * Ant task for calling SmartSprites processing.
57
 */
58
public class SmartSpritesTask extends Task
×
59
{
60
    private String rootDir;
61
    private String outputDir;
62
    private String documentRootDir;
63
    private MessageLevel logLevel;
64
    private MessageLevel failOnLevel = MessageLevel.ERROR;
×
65
    private String cssFileSuffix = SmartSpritesParameters.DEFAULT_CSS_FILE_SUFFIX;
×
66
    private String cssFileEncoding = SmartSpritesParameters.DEFAULT_CSS_FILE_ENCODING;
×
67
    private PngDepth spritePngDepth = SmartSpritesParameters.DEFAULT_SPRITE_PNG_DEPTH;
×
68
    private boolean spritePngIe6 = SmartSpritesParameters.DEFAULT_SPRITE_PNG_IE6;
×
69
    private boolean markSpriteImages = SmartSpritesParameters.DEFAULT_MARK_SPRITE_IMAGES;
×
70

71
    private List<String> cssFiles = new ArrayList<>();
×
72

73
    public void setRootDir(File dir)
74
    {
75
        this.rootDir = dir.getPath();
×
76
    }
×
77

78
    public void setOutputDir(File outputDir)
79
    {
80
        this.outputDir = outputDir.getPath();
×
81
    }
×
82

83
    public void setDocumentRootDir(File documentRootDir)
84
    {
85
        this.documentRootDir = documentRootDir.getPath();
×
86
    }
×
87

88
    public void setLogLevel(String logLevel)
89
    {
90
        this.logLevel = getLogLevelFromString(logLevel, MessageLevel.INFO);
×
91
    }
×
92

93
    public void setFailOnLevel(String failOnLevel)
94
    {
95
        this.failOnLevel = getLogLevelFromString(failOnLevel, MessageLevel.ERROR);
×
96
    }
×
97

98
    private MessageLevel getLogLevelFromString(String logLevel, MessageLevel defaultLevel)
99
    {
100
        try
101
        {
102
            return MessageLevel.valueOf(logLevel);
×
103
        }
104
        catch (Exception e)
×
105
        {
106
            return defaultLevel;
×
107
        }
108
    }
109

110
    public void setCssFileEncoding(String cssFileEncoding)
111
    {
112
        this.cssFileEncoding = cssFileEncoding;
×
113
    }
×
114

115
    public void setCssFileSuffix(String cssFileSuffix)
116
    {
117
        this.cssFileSuffix = cssFileSuffix;
×
118
    }
×
119

120
    public void setSpritePngDepth(String spritePngDepthString)
121
    {
122
        this.spritePngDepth = EnumUtils.valueOf(spritePngDepthString, PngDepth.class,
×
123
            SmartSpritesParameters.DEFAULT_SPRITE_PNG_DEPTH);
124
    }
×
125

126
    public void setSpritePngIe6(boolean spritePngIe6)
127
    {
128
        this.spritePngIe6 = spritePngIe6;
×
129
    }
×
130

131
    public void setMarkSpriteImages(boolean markSpriteImages)
132
    {
133
        this.markSpriteImages = markSpriteImages;
×
134
    }
×
135

136
    @Override
137
    public void execute()
138
    {
139
        final SmartSpritesParameters parameters = new SmartSpritesParameters(rootDir,
×
140
            cssFiles, outputDir, documentRootDir, logLevel, cssFileSuffix,
141
            spritePngDepth, spritePngIe6, cssFileEncoding, markSpriteImages);
142

143
        final FailureDetectorMessageSink failureDetectorMessageSink = new FailureDetectorMessageSink();
×
144
        MessageLog log = new MessageLog(new AntLogMessageSink(),
×
145
            failureDetectorMessageSink);
146

147
        if (parameters.validate(log))
×
148
        {
149
            try
150
            {
151
                new SpriteBuilder(parameters, log).buildSprites();
×
152
            }
153
            catch (IOException e)
×
154
            {
155
                throw new BuildException(e);
×
156
            }
×
157
        }
158

159
        if (failureDetectorMessageSink.shouldFail)
×
160
        {
161
            throw new BuildException(failureDetectorMessageSink.failureLevel.name() + " messages found");
×
162
        }
163
    }
×
164

165
    private class AntLogMessageSink implements MessageSink
×
166
    {
167
        public void add(Message message)
168
        {
169
            if (MessageLevel.COMPARATOR.compare(message.level, logLevel) >= 0)
×
170
            {
171
                log(message.toString());
×
172
            }
173
        }
×
174
    }
175

176
    private class FailureDetectorMessageSink implements MessageSink
×
177
    {
178
        boolean shouldFail = false;
×
179
        MessageLevel failureLevel = null;
×
180

181
        public void add(Message message)
182
        {
183
            if (failOnLevel != null
×
184
                && MessageLevel.COMPARATOR.compare(message.level, failOnLevel) >= 0
×
185
                && message.level != MessageLevel.STATUS)
186
            {
187
                failureLevel = message.level;
×
188
                shouldFail = true;
×
189
            }
190
        }
×
191
    }
192

193
    public void addConfiguredFileset(FileSet fileset)
194
    {
195
        for (Iterator<Resource> it = fileset.iterator(); it.hasNext();)
×
196
        {
197
            cssFiles.add(it.next().getName());
×
198
        }
199
    }
×
200
}
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