View Javadoc

1   /**
2    * Copyright (c) 2008-2012, 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  package org.yaml.snakeyaml.resolver;
17  
18  import java.util.ArrayList;
19  import java.util.HashMap;
20  import java.util.List;
21  import java.util.Map;
22  import java.util.regex.Pattern;
23  
24  import org.yaml.snakeyaml.nodes.NodeId;
25  import org.yaml.snakeyaml.nodes.Tag;
26  
27  /**
28   * Resolver tries to detect a type by scalars's content (when the type is
29   * implicit)
30   */
31  public class Resolver {
32      public static final Pattern BOOL = Pattern
33              .compile("^(?:yes|Yes|YES|no|No|NO|true|True|TRUE|false|False|FALSE|on|On|ON|off|Off|OFF)$");
34  
35      /**
36       * The regular expression is taken from the 1.2 specification but '_'s are
37       * added to keep backwards compatibility
38       */
39      public static final Pattern FLOAT = Pattern
40              .compile("^([-+]?(\\.[0-9]+|[0-9_]+(\\.[0-9_]*)?)([eE][-+]?[0-9]+)?|[-+]?[0-9][0-9_]*(?::[0-5]?[0-9])+\\.[0-9_]*|[-+]?\\.(?:inf|Inf|INF)|\\.(?:nan|NaN|NAN))$");
41      public static final Pattern INT = Pattern
42              .compile("^(?:[-+]?0b[0-1_]+|[-+]?0[0-7_]+|[-+]?(?:0|[1-9][0-9_]*)|[-+]?0x[0-9a-fA-F_]+|[-+]?[1-9][0-9_]*(?::[0-5]?[0-9])+)$");
43      public static final Pattern MERGE = Pattern.compile("^(?:<<)$");
44      public static final Pattern NULL = Pattern.compile("^(?:~|null|Null|NULL| )$");
45      public static final Pattern EMPTY = Pattern.compile("^$");
46      public static final Pattern TIMESTAMP = Pattern
47              .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])?))?)$");
48      public static final Pattern VALUE = Pattern.compile("^(?:=)$");
49      public static final Pattern YAML = Pattern.compile("^(?:!|&|\\*)$");
50  
51      protected Map<Character, List<ResolverTuple>> yamlImplicitResolvers = new HashMap<Character, List<ResolverTuple>>();
52  
53      /**
54       * Create Resolver
55       * 
56       * @param respectDefaultImplicitScalars
57       *            false to parse/dump scalars as plain Strings
58       * @deprecated override addImplicitResolvers instead
59       */
60      public Resolver(boolean respectDefaultImplicitScalars) {
61          if (respectDefaultImplicitScalars) {
62              addImplicitResolvers();
63          }
64      }
65  
66      protected void addImplicitResolvers() {
67          addImplicitResolver(Tag.BOOL, BOOL, "yYnNtTfFoO");
68          /*
69           * INT must be before FLOAT because the regular expression for FLOAT
70           * matches INT (see issue 130)
71           * http://code.google.com/p/snakeyaml/issues/detail?id=130
72           */
73          addImplicitResolver(Tag.INT, INT, "-+0123456789");
74          addImplicitResolver(Tag.FLOAT, FLOAT, "-+0123456789.");
75          addImplicitResolver(Tag.MERGE, MERGE, "<");
76          addImplicitResolver(Tag.NULL, NULL, "~nN\0");
77          addImplicitResolver(Tag.NULL, EMPTY, null);
78          addImplicitResolver(Tag.TIMESTAMP, TIMESTAMP, "0123456789");
79          addImplicitResolver(Tag.VALUE, VALUE, "=");
80          // The following implicit resolver is only for documentation
81          // purposes.
82          // It cannot work
83          // because plain scalars cannot start with '!', '&', or '*'.
84          addImplicitResolver(Tag.YAML, YAML, "!&*");
85      }
86  
87      public Resolver() {
88          this(true);
89      }
90  
91      public void addImplicitResolver(Tag tag, Pattern regexp, String first) {
92          if (first == null) {
93              List<ResolverTuple> curr = yamlImplicitResolvers.get(null);
94              if (curr == null) {
95                  curr = new ArrayList<ResolverTuple>();
96                  yamlImplicitResolvers.put(null, curr);
97              }
98              curr.add(new ResolverTuple(tag, regexp));
99          } else {
100             char[] chrs = first.toCharArray();
101             for (int i = 0, j = chrs.length; i < j; i++) {
102                 Character theC = Character.valueOf(chrs[i]);
103                 if (theC == 0) {
104                     // special case: for null
105                     theC = null;
106                 }
107                 List<ResolverTuple> curr = yamlImplicitResolvers.get(theC);
108                 if (curr == null) {
109                     curr = new ArrayList<ResolverTuple>();
110                     yamlImplicitResolvers.put(theC, curr);
111                 }
112                 curr.add(new ResolverTuple(tag, regexp));
113             }
114         }
115     }
116 
117     public Tag resolve(NodeId kind, String value, boolean implicit) {
118         if (kind == NodeId.scalar && implicit) {
119             List<ResolverTuple> resolvers = null;
120             if (value.length() == 0) {
121                 resolvers = yamlImplicitResolvers.get('\0');
122             } else {
123                 resolvers = yamlImplicitResolvers.get(value.charAt(0));
124             }
125             if (resolvers != null) {
126                 for (ResolverTuple v : resolvers) {
127                     Tag tag = v.getTag();
128                     Pattern regexp = v.getRegexp();
129                     if (regexp.matcher(value).matches()) {
130                         return tag;
131                     }
132                 }
133             }
134             if (yamlImplicitResolvers.containsKey(null)) {
135                 for (ResolverTuple v : yamlImplicitResolvers.get(null)) {
136                     Tag tag = v.getTag();
137                     Pattern regexp = v.getRegexp();
138                     if (regexp.matcher(value).matches()) {
139                         return tag;
140                     }
141                 }
142             }
143         }
144         switch (kind) {
145         case scalar:
146             return Tag.STR;
147         case sequence:
148             return Tag.SEQ;
149         default:
150             return Tag.MAP;
151         }
152     }
153 }