Friday 8 September 2017

javafx - RANDOM_CODE_1

i'm using this code to extract property - values from JavaFx's default css(es).

and that will be used as my javafx css refrence.



 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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package extras;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeSet;
import java.util.regex.Matcher;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import com.osbcp.cssparser.CSSParser;
import com.osbcp.cssparser.PropertyValue;

public class Toast {
    public static void main(String[] args) throws IOException, Exception  {

        Map<String, List<PropertyValue>> map = Stream.of(new File("folder containing css files").listFiles())
                .flatMap((File f) -> {
                    try {
                        return CSSParser.parse(new String(Files.readAllBytes(f.toPath()))).stream();
                    } catch (Exception e) {
                        System.out.println(f.getName()+"  "+e);
                    };
                    return Stream.empty();
                })
                .flatMap(r -> r.getPropertyValues().stream())
                .collect(Collectors.groupingBy(PropertyValue::getProperty));

        Map<String, TreeSet<String>> result = new HashMap<>();
        List<String> filterRegexes = new ArrayList<>();

        map.forEach((key, list) -> {
            TreeSet<String> list2 = 
                    list.stream()
                    .flatMap(p -> {
                        // split multi value values 
                        if(p.getValue().indexOf(')') < 0)
                            return Stream.of(p.getValue().split(","));

                        char[] chars = p.getValue().toCharArray();

                        ArrayList<String> str = new ArrayList<>();

                        int start = 0;
                        for (int end = 0; end < chars.length; end++) {
                            if(chars[end] == ',' && chars[end-1] != ')') {
                                boolean clean = true;
                                for (int k = start; k < end && clean; k++)
                                    clean = chars[k] != ')' && chars[k] != '(';

                                if(clean) {
                                    str.add(new String(chars, start, end - start));
                                    start = end + 1;
                                }
                            }
                        }
                        str.add(new String(chars, start, chars.length - start));

                        return str.stream();
                    })
                    .map(s -> s.replaceAll("\\s+", " ").trim())
                    .filter(s -> {
                        
                        //filter out similar looking values
                        
                        if(s.isEmpty())
                            return false;
                        
                        if(filterRegexes.stream().anyMatch(regex -> s.matches(regex)))
                            return false;
                        
                        String regex = s.replaceAll("\\d*\\.\\d+", Matcher.quoteReplacement("\\d*\\.\\d+"))
                                .replaceAll("\\d+", Matcher.quoteReplacement("\\d+"));
                        
                        filterRegexes.add(regex);
                        return true;
                        
                    })
                    .collect(Collectors.toCollection(TreeSet::new));

            result.put(key, list2);
        });
        
        System.out.println("DONE");
    }
}
styled using hilite.me

No comments:

Post a Comment