summaryrefslogtreecommitdiffstats
path: root/theme-compiler/src/com/vaadin/sass/resolver/VaadinResolver.java
blob: 1caf5dcac855816b1dbd8de48e1cd9c1b322d021 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package com.vaadin.sass.resolver;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.w3c.css.sac.InputSource;

public class VaadinResolver implements ScssStylesheetResolver {

    @Override
    public InputSource resolve(String identifier) {
        if (identifier.endsWith(".css")) {
            // CSS support mainly for testing, don't load from classpath etc
            ScssStylesheetResolver resolver = new FilesystemResolver();
            return resolver.resolve(identifier);
        }

        InputSource source = null;

        Pattern pattern = Pattern
                .compile("\\.\\.\\/([^\\/]+)\\/([^\\/]+\\.scss)");
        Matcher matcher = pattern.matcher(identifier);

        if (matcher.find()) {
            // theme include
            ScssStylesheetResolver resolver = new FilesystemResolver();
            source = resolver.resolve(identifier);

            if (source == null) {
                String themeName = matcher.group(1);
                String fileName = matcher.group(2);
                resolver = new ClassloaderResolver();
                String id = "VAADIN/themes/" + themeName + "/" + fileName;
                source = resolver.resolve(id);
            }

        } else {
            ScssStylesheetResolver resolver = new FilesystemResolver();
            source = resolver.resolve(identifier);

            if (source == null) {
                resolver = new ClassloaderResolver();
                source = resolver.resolve(identifier);
            }
        }

        return source;
    }
}