| 1 | |
package org.webslinger.rules; |
| 2 | |
|
| 3 | |
import java.io.IOException; |
| 4 | |
import java.io.StringReader; |
| 5 | |
|
| 6 | |
import org.apache.commons.vfs.FileObject; |
| 7 | |
|
| 8 | |
import org.webslinger.commons.vfs.VFSUtil; |
| 9 | |
import org.webslinger.container.WebslingerContainer; |
| 10 | |
import org.webslinger.util.GeneratedResult; |
| 11 | |
import org.webslinger.util.TTLCachedObject; |
| 12 | |
import org.webslinger.util.TTLObject; |
| 13 | |
|
| 14 | 116 | public class CSSRulesTTLCachedObject extends TTLCachedObject<CompiledRules> { |
| 15 | |
static { |
| 16 | 1 | TTLObject.setDefaultTTLForClass(CSSRulesTTLCachedObject.class, 1000); |
| 17 | 1 | } |
| 18 | |
|
| 19 | |
private final WebslingerContainer container; |
| 20 | |
private final FileObject file; |
| 21 | |
|
| 22 | 26 | public CSSRulesTTLCachedObject(WebslingerContainer container, FileObject file) { |
| 23 | 26 | this.container = container; |
| 24 | 26 | this.file = file; |
| 25 | 26 | } |
| 26 | |
|
| 27 | |
protected long getTimestamp(CompiledRules old) throws IOException { |
| 28 | 94 | if (!file.exists()) return NOT_EXISTANT_TIMESTAMP; |
| 29 | 90 | return file.getContent().getLastModifiedTime(); |
| 30 | |
} |
| 31 | |
|
| 32 | |
protected GeneratedResult<CompiledRules> generate(CompiledRules old) throws IOException { |
| 33 | |
CompiledRules compiledRules; |
| 34 | 22 | Rules rulesParser = new Rules(new StringReader(VFSUtil.getString(file))); |
| 35 | |
try { |
| 36 | 22 | CSSRules cssRules = rulesParser.Rules(); |
| 37 | 22 | compiledRules = cssRules.compile(container); |
| 38 | 0 | } catch (ParseException e) { |
| 39 | 0 | throw (IOException) new IOException(e.getMessage()).initCause(e); |
| 40 | 22 | } |
| 41 | 22 | return new GeneratedResult<CompiledRules>(file.getContent().getLastModifiedTime(), compiledRules); |
| 42 | |
} |
| 43 | |
} |