1b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov/**
211a89b445f3bde56bf07e6a0d04f0b0256dcb215Andrey Somov * Copyright (c) 2008, http://www.snakeyaml.org
3b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov *
4b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov * Licensed under the Apache License, Version 2.0 (the "License");
5b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov * you may not use this file except in compliance with the License.
6b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov * You may obtain a copy of the License at
7b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov *
8b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov *     http://www.apache.org/licenses/LICENSE-2.0
9b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov *
10b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov * Unless required by applicable law or agreed to in writing, software
11b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov * distributed under the License is distributed on an "AS IS" BASIS,
12b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov * See the License for the specific language governing permissions and
14b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov * limitations under the License.
15b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov */
16b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somovpackage org.yaml.snakeyaml.scanner;
17b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov
18b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somovimport java.util.Arrays;
19b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov
20b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somovpublic final class Constant {
21b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov    private final static String ALPHA_S = "abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ-_";
22b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov
23b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov    private final static String LINEBR_S = "\n\u0085\u2028\u2029";
24b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov    private final static String FULL_LINEBR_S = "\r" + LINEBR_S;
25b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov    private final static String NULL_OR_LINEBR_S = "\0" + FULL_LINEBR_S;
26b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov    private final static String NULL_BL_LINEBR_S = " " + NULL_OR_LINEBR_S;
27b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov    private final static String NULL_BL_T_LINEBR_S = "\t" + NULL_BL_LINEBR_S;
28b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov    private final static String NULL_BL_T_S = "\0 \t";
29b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov    private final static String URI_CHARS_S = ALPHA_S + "-;/?:@&=+$,_.!~*\'()[]%";
30b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov
31b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov    public final static Constant LINEBR = new Constant(LINEBR_S);
32b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov    public final static Constant FULL_LINEBR = new Constant(FULL_LINEBR_S);
33b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov    public final static Constant NULL_OR_LINEBR = new Constant(NULL_OR_LINEBR_S);
34b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov    public final static Constant NULL_BL_LINEBR = new Constant(NULL_BL_LINEBR_S);
35b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov    public final static Constant NULL_BL_T_LINEBR = new Constant(NULL_BL_T_LINEBR_S);
36b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov    public final static Constant NULL_BL_T = new Constant(NULL_BL_T_S);
37b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov    public final static Constant URI_CHARS = new Constant(URI_CHARS_S);
38b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov
39b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov    public final static Constant ALPHA = new Constant(ALPHA_S);
40b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov
41b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov    private String content;
42b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov    boolean[] contains = new boolean[128];
43b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov    boolean noASCII = false;
44b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov
45b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov    private Constant(String content) {
46b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov        Arrays.fill(contains, false);
47b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov        StringBuilder sb = new StringBuilder();
48b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov        for (int i = 0; i < content.length(); i++) {
49b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov            char ch = content.charAt(i);
50b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov            if (ch < 128)
51b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov                contains[ch] = true;
52b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov            else
53b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov                sb.append(ch);
54b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov        }
55b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov        if (sb.length() > 0) {
56b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov            noASCII = true;
57b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov            this.content = sb.toString();
58b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov        }
59b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov    }
60b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov
61b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov    public boolean has(char ch) {
62b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov        return (ch < 128) ? contains[ch] : noASCII && content.indexOf(ch, 0) != -1;
63b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov    }
64b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov
65b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov    public boolean hasNo(char ch) {
66b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov        return !has(ch);
67b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov    }
68b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov
69b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov    public boolean has(char ch, String additional) {
70b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov        return has(ch) || additional.indexOf(ch, 0) != -1;
71b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov    }
72b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov
73b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov    public boolean hasNo(char ch, String additional) {
74b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov        return !has(ch, additional);
75b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov    }
76b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov}
77