View Javadoc

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