Coverage Report - org.yaml.snakeyaml.resolver.Resolver
 
Classes in this File Line Coverage Branch Coverage Complexity
Resolver
97%
65/67
96%
32/33
5.2
Resolver$1
100%
1/1
N/A
5.2
 
 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  1
     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  1
     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  1
     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  1
     public static final Pattern MERGE = Pattern.compile("^(?:<<)$");
 44  1
     public static final Pattern NULL = Pattern.compile("^(?:~|null|Null|NULL| )$");
 45  1
     public static final Pattern EMPTY = Pattern.compile("^$");
 46  1
     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  1
     public static final Pattern VALUE = Pattern.compile("^(?:=)$");
 49  1
     public static final Pattern YAML = Pattern.compile("^(?:!|&|\\*)$");
 50  
 
 51  133131
     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  133131
     public Resolver(boolean respectDefaultImplicitScalars) {
 61  133131
         if (respectDefaultImplicitScalars) {
 62  133130
             addImplicitResolvers();
 63  
         }
 64  133131
     }
 65  
 
 66  
     protected void addImplicitResolvers() {
 67  133128
         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  133128
         addImplicitResolver(Tag.INT, INT, "-+0123456789");
 74  133128
         addImplicitResolver(Tag.FLOAT, FLOAT, "-+0123456789.");
 75  133128
         addImplicitResolver(Tag.MERGE, MERGE, "<");
 76  133128
         addImplicitResolver(Tag.NULL, NULL, "~nN\0");
 77  133128
         addImplicitResolver(Tag.NULL, EMPTY, null);
 78  133128
         addImplicitResolver(Tag.TIMESTAMP, TIMESTAMP, "0123456789");
 79  133128
         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  133128
         addImplicitResolver(Tag.YAML, YAML, "!&*");
 85  133128
     }
 86  
 
 87  
     public Resolver() {
 88  133130
         this(true);
 89  133130
     }
 90  
 
 91  
     public void addImplicitResolver(Tag tag, Pattern regexp, String first) {
 92  1198174
         if (first == null) {
 93  133132
             List<ResolverTuple> curr = yamlImplicitResolvers.get(null);
 94  133132
             if (curr == null) {
 95  0
                 curr = new ArrayList<ResolverTuple>();
 96  0
                 yamlImplicitResolvers.put(null, curr);
 97  
             }
 98  133132
             curr.add(new ResolverTuple(tag, regexp));
 99  133132
         } else {
 100  1065042
             char[] chrs = first.toCharArray();
 101  8254055
             for (int i = 0, j = chrs.length; i < j; i++) {
 102  7189013
                 Character theC = Character.valueOf(chrs[i]);
 103  7189013
                 if (theC == 0) {
 104  
                     // special case: for null
 105  133131
                     theC = null;
 106  
                 }
 107  7189013
                 List<ResolverTuple> curr = yamlImplicitResolvers.get(theC);
 108  7189013
                 if (curr == null) {
 109  3993895
                     curr = new ArrayList<ResolverTuple>();
 110  3993895
                     yamlImplicitResolvers.put(theC, curr);
 111  
                 }
 112  7189013
                 curr.add(new ResolverTuple(tag, regexp));
 113  
             }
 114  
         }
 115  1198174
     }
 116  
 
 117  
     public Tag resolve(NodeId kind, String value, boolean implicit) {
 118  1905523
         if (kind == NodeId.scalar && implicit) {
 119  813013
             List<ResolverTuple> resolvers = null;
 120  813013
             if (value.length() == 0) {
 121  136
                 resolvers = yamlImplicitResolvers.get('\0');
 122  
             } else {
 123  812877
                 resolvers = yamlImplicitResolvers.get(value.charAt(0));
 124  
             }
 125  813013
             if (resolvers != null) {
 126  484961
                 for (ResolverTuple v : resolvers) {
 127  534765
                     Tag tag = v.getTag();
 128  534765
                     Pattern regexp = v.getRegexp();
 129  534765
                     if (regexp.matcher(value).matches()) {
 130  458882
                         return tag;
 131  
                     }
 132  75883
                 }
 133  
             }
 134  354131
             if (yamlImplicitResolvers.containsKey(null)) {
 135  354130
                 for (ResolverTuple v : yamlImplicitResolvers.get(null)) {
 136  708270
                     Tag tag = v.getTag();
 137  708270
                     Pattern regexp = v.getRegexp();
 138  708270
                     if (regexp.matcher(value).matches()) {
 139  141
                         return tag;
 140  
                     }
 141  708129
                 }
 142  
             }
 143  
         }
 144  1
         switch (kind) {
 145  
         case scalar:
 146  1346829
             return Tag.STR;
 147  
         case sequence:
 148  27003
             return Tag.SEQ;
 149  
         default:
 150  72668
             return Tag.MAP;
 151  
         }
 152  
     }
 153  
 }