aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/org/yaml/snakeyaml/resolver/Resolver.java
blob: 38960561ce8e74435921e9df8b06c0cfc881b4b8 (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
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/**
 * Copyright (c) 2008, SnakeYAML
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
 * in compliance with the License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License
 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 * or implied. See the License for the specific language governing permissions and limitations under
 * the License.
 */
package org.yaml.snakeyaml.resolver;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;
import org.yaml.snakeyaml.nodes.NodeId;
import org.yaml.snakeyaml.nodes.Tag;

/**
 * Resolver tries to detect a type by content (when the tag is implicit)
 */
public class Resolver {

  public static final Pattern BOOL = Pattern
      .compile("^(?:yes|Yes|YES|no|No|NO|true|True|TRUE|false|False|FALSE|on|On|ON|off|Off|OFF)$");

  /**
   * The regular expression is taken from the 1.2 specification but '_'s are added to keep backwards
   * compatibility
   */
  public static final Pattern FLOAT =
      Pattern.compile("^(" + "[-+]?(?:[0-9][0-9_]*)\\.[0-9_]*(?:[eE][-+]?[0-9]+)?" + // (base 10)
          "|[-+]?(?:[0-9][0-9_]*)(?:[eE][-+]?[0-9]+)" + // (base 10, scientific notation without .)
          "|[-+]?\\.[0-9_]+(?:[eE][-+]?[0-9]+)?" + // (base 10, starting with .)
          "|[-+]?[0-9][0-9_]*(?::[0-5]?[0-9])+\\.[0-9_]*" + // (base 60)
          "|[-+]?\\.(?:inf|Inf|INF)" + "|\\.(?:nan|NaN|NAN)" + ")$");
  public static final Pattern INT = Pattern.compile("^(?:" + "[-+]?0b_*[0-1][0-1_]*" + // (base 2)
      "|[-+]?0_*[0-7][0-7_]*" + // (base 8)
      "|[-+]?(?:0|[1-9][0-9_]*)" + // (base 10)
      "|[-+]?0x_*[0-9a-fA-F][0-9a-fA-F_]*" + // (base 16)
      "|[-+]?[1-9][0-9_]*(?::[0-5]?[0-9])+" + // (base 60)
      ")$");
  public static final Pattern MERGE = Pattern.compile("^(?:<<)$");
  public static final Pattern NULL = Pattern.compile("^(?:~|null|Null|NULL| )$");
  public static final Pattern EMPTY = Pattern.compile("^$");
  public static final Pattern TIMESTAMP = Pattern.compile(
      "^(?:[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]|[0-9][0-9][0-9][0-9]-[0-9][0-9]?-[0-9][0-9]?(?:[Tt]|[ \t]+)[0-9][0-9]?:[0-9][0-9]:[0-9][0-9](?:\\.[0-9]*)?(?:[ \t]*(?:Z|[-+][0-9][0-9]?(?::[0-9][0-9])?))?)$");
  public static final Pattern VALUE = Pattern.compile("^(?:=)$");
  public static final Pattern YAML = Pattern.compile("^(?:!|&|\\*)$");

  protected Map<Character, List<ResolverTuple>> yamlImplicitResolvers =
      new HashMap<Character, List<ResolverTuple>>();

  protected void addImplicitResolvers() {
    addImplicitResolver(Tag.BOOL, BOOL, "yYnNtTfFoO", 10);
    /*
     * INT must be before FLOAT because the regular expression for FLOAT matches INT (see issue 130)
     * http://code.google.com/p/snakeyaml/issues/detail?id=130
     */
    addImplicitResolver(Tag.INT, INT, "-+0123456789");
    addImplicitResolver(Tag.FLOAT, FLOAT, "-+0123456789.");
    addImplicitResolver(Tag.MERGE, MERGE, "<", 10);
    addImplicitResolver(Tag.NULL, NULL, "~nN\0", 10);
    addImplicitResolver(Tag.NULL, EMPTY, null, 10);
    addImplicitResolver(Tag.TIMESTAMP, TIMESTAMP, "0123456789", 50);
    // The following implicit resolver is only for documentation purposes.
    // It cannot work because plain scalars cannot start with '!', '&', or '*'.
    addImplicitResolver(Tag.YAML, YAML, "!&*", 10);
  }

  public Resolver() {
    addImplicitResolvers();
  }

  public void addImplicitResolver(Tag tag, Pattern regexp, String first) {
    addImplicitResolver(tag, regexp, first, 1024);
  }

  public void addImplicitResolver(Tag tag, Pattern regexp, String first, int limit) {
    if (first == null) {
      List<ResolverTuple> curr = yamlImplicitResolvers.get(null);
      if (curr == null) {
        curr = new ArrayList<ResolverTuple>();
        yamlImplicitResolvers.put(null, curr);
      }
      curr.add(new ResolverTuple(tag, regexp, limit));
    } else {
      char[] chrs = first.toCharArray();
      for (int i = 0, j = chrs.length; i < j; i++) {
        Character theC = Character.valueOf(chrs[i]);
        if (theC == 0) {
          // special case: for null
          theC = null;
        }
        List<ResolverTuple> curr = yamlImplicitResolvers.get(theC);
        if (curr == null) {
          curr = new ArrayList<ResolverTuple>();
          yamlImplicitResolvers.put(theC, curr);
        }
        curr.add(new ResolverTuple(tag, regexp, limit));
      }
    }
  }

  public Tag resolve(NodeId kind, String value, boolean implicit) {
    if (kind == NodeId.scalar && implicit) {
      final List<ResolverTuple> resolvers;
      if (value.length() == 0) {
        resolvers = yamlImplicitResolvers.get('\0');
      } else {
        resolvers = yamlImplicitResolvers.get(value.charAt(0));
      }
      if (resolvers != null) {
        for (ResolverTuple v : resolvers) {
          Tag tag = v.getTag();
          Pattern regexp = v.getRegexp();
          if (value.length() <= v.getLimit() && regexp.matcher(value).matches()) {
            return tag;
          }
        }
      }
      if (yamlImplicitResolvers.containsKey(null)) {
        // check null resolver
        for (ResolverTuple v : yamlImplicitResolvers.get(null)) {
          Tag tag = v.getTag();
          Pattern regexp = v.getRegexp();
          if (value.length() <= v.getLimit() && regexp.matcher(value).matches()) {
            return tag;
          }
        }
      }
    }
    switch (kind) {
      case scalar:
        return Tag.STR;
      case sequence:
        return Tag.SEQ;
      default:
        return Tag.MAP;
    }
  }
}