File: Utilities.java

package info (click to toggle)
liblessen-java 1.0-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 276 kB
  • sloc: java: 2,981; xml: 31; makefile: 5
file content (94 lines) | stat: -rw-r--r-- 3,263 bytes parent folder | download
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
91
92
93
94
package com.metaweb.lessen;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.io.Reader;
import java.io.Writer;
import java.net.URL;
import java.net.URLConnection;
import java.util.Map;

import com.metaweb.lessen.tokenizers.ReaderTokenizer;
import com.metaweb.lessen.tokenizers.RegroupingTokenizer;
import com.metaweb.lessen.tokenizers.StringTokenizer;
import com.metaweb.lessen.tokenizers.Tokenizer;
import com.metaweb.lessen.tokenizers.VariableResolvingTokenizer;
import com.metaweb.lessen.tokens.Token;


public class Utilities {

    static public Tokenizer open(String s) {
        return wrap(new StringTokenizer(s));
    }

    static public Tokenizer open(Reader reader) {
        return wrap(new ReaderTokenizer(reader));
    }
    
    static public Tokenizer open(File file) throws FileNotFoundException {
        return open(new FileReader(file));
    }
    
    static public Tokenizer open(URL url) throws IOException {
        URLConnection conn = url.openConnection();
        String encoding = conn.getContentEncoding();
        
        Reader reader = encoding != null ?
            new InputStreamReader(conn.getInputStream(), encoding) :
            new InputStreamReader(conn.getInputStream());
        
        return open(reader);
    }
    
    static public Tokenizer openLess(File file, Map<String, String> variables) throws FileNotFoundException {
        return openLess(file, variables, new Scope(null));
    }
    
    static public Tokenizer openLess(File file, Map<String, String> variables, Scope scope) throws FileNotFoundException {
        return wrapLess(open(file), new FileResourceFinder(file), variables, scope);
    }
    
    static public Tokenizer openLess(URL url, Map<String, String> variables) throws IOException {
        return openLess(url, variables, new Scope(null));
    }
    
    static public Tokenizer openLess(URL url, Map<String, String> variables, Scope scope) throws IOException {
        return wrapLess(open(url), new URLResourceFinder(url), variables, scope);
    }
    
    static public void write(Tokenizer tokenizer, Writer writer) throws IOException {
        Token t;
        while ((t = tokenizer.getToken()) != null) {
            writer.write(t.getCleanText());
            tokenizer.next();
        }
    }
    
    static public void print(Tokenizer tokenizer, PrintStream ps) throws IOException {
        Token t;
        while ((t = tokenizer.getToken()) != null) {
            ps.print(t.getCleanText());
            tokenizer.next();
        }
    }

    static protected Tokenizer wrap(Tokenizer t) {
        return new RegroupingTokenizer(t);
    }
    
    static protected Tokenizer wrapLess(Tokenizer tokenizer, ResourceFinder resourceFinder, Map<String, String> variables, Scope scope) {
        tokenizer = new LessParser(
            variables == null ? tokenizer : 
                new VariableResolvingTokenizer(tokenizer, variables, true),
            variables == null ? resourceFinder : 
                new VariableResolvingResourceFinder(resourceFinder, variables),
            scope);
        
        return tokenizer;
    }
}