• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright (c) 2008, 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.nodes;
17 
18 import java.math.BigDecimal;
19 import java.math.BigInteger;
20 import java.net.URI;
21 import java.sql.Timestamp;
22 import java.util.Date;
23 import java.util.HashMap;
24 import java.util.HashSet;
25 import java.util.Map;
26 import java.util.Set;
27 
28 import org.yaml.snakeyaml.error.YAMLException;
29 import org.yaml.snakeyaml.util.UriEncoder;
30 
31 public final class Tag implements Comparable<Tag> {
32     public static final String PREFIX = "tag:yaml.org,2002:";
33     public static final Tag YAML = new Tag(PREFIX + "yaml");
34     public static final Tag MERGE = new Tag(PREFIX + "merge");
35     public static final Tag SET = new Tag(PREFIX + "set");
36     public static final Tag PAIRS = new Tag(PREFIX + "pairs");
37     public static final Tag OMAP = new Tag(PREFIX + "omap");
38     public static final Tag BINARY = new Tag(PREFIX + "binary");
39     public static final Tag INT = new Tag(PREFIX + "int");
40     public static final Tag FLOAT = new Tag(PREFIX + "float");
41     public static final Tag TIMESTAMP = new Tag(PREFIX + "timestamp");
42     public static final Tag BOOL = new Tag(PREFIX + "bool");
43     public static final Tag NULL = new Tag(PREFIX + "null");
44     public static final Tag STR = new Tag(PREFIX + "str");
45     public static final Tag SEQ = new Tag(PREFIX + "seq");
46     public static final Tag MAP = new Tag(PREFIX + "map");
47     public static final Map<Tag, Set<Class<?>>> COMPATIBILITY_MAP;
48     static {
49         COMPATIBILITY_MAP = new HashMap<Tag, Set<Class<?>>>();
50         Set<Class<?>> floatSet = new HashSet<Class<?>>();
51         floatSet.add(Double.class);
52         floatSet.add(Float.class);
53         floatSet.add(BigDecimal.class);
COMPATIBILITY_MAP.put(FLOAT, floatSet)54         COMPATIBILITY_MAP.put(FLOAT, floatSet);
55         //
56         Set<Class<?>> intSet = new HashSet<Class<?>>();
57         intSet.add(Integer.class);
58         intSet.add(Long.class);
59         intSet.add(BigInteger.class);
COMPATIBILITY_MAP.put(INT, intSet)60         COMPATIBILITY_MAP.put(INT, intSet);
61         //
62         Set<Class<?>> timestampSet = new HashSet<Class<?>>();
63         timestampSet.add(Date.class);
64         timestampSet.add(java.sql.Date.class);
65         timestampSet.add(Timestamp.class);
COMPATIBILITY_MAP.put(TIMESTAMP, timestampSet)66         COMPATIBILITY_MAP.put(TIMESTAMP, timestampSet);
67     }
68 
69     private final String value;
70     private boolean secondary = false; // see http://www.yaml.org/refcard.html
71 
Tag(String tag)72     public Tag(String tag) {
73         if (tag == null) {
74             throw new NullPointerException("Tag must be provided.");
75         } else if (tag.length() == 0) {
76             throw new IllegalArgumentException("Tag must not be empty.");
77         } else if (tag.trim().length() != tag.length()) {
78             throw new IllegalArgumentException("Tag must not contain leading or trailing spaces.");
79         }
80         this.value = UriEncoder.encode(tag);
81         this.secondary = !tag.startsWith(PREFIX);
82     }
83 
Tag(Class<? extends Object> clazz)84     public Tag(Class<? extends Object> clazz) {
85         if (clazz == null) {
86             throw new NullPointerException("Class for tag must be provided.");
87         }
88         this.value = Tag.PREFIX + UriEncoder.encode(clazz.getName());
89     }
90 
91     //TODO to be removed ?
Tag(URI uri)92     public Tag(URI uri) {
93         if (uri == null) {
94             throw new NullPointerException("URI for tag must be provided.");
95         }
96         this.value = uri.toASCIIString();
97     }
98 
isSecondary()99     public boolean isSecondary() {
100         return secondary;
101     }
102 
getValue()103     public String getValue() {
104         return value;
105     }
106 
startsWith(String prefix)107     public boolean startsWith(String prefix) {
108         return value.startsWith(prefix);
109     }
110 
getClassName()111     public String getClassName() {
112         if (!value.startsWith(Tag.PREFIX)) {
113             throw new YAMLException("Invalid tag: " + value);
114         }
115         return UriEncoder.decode(value.substring(Tag.PREFIX.length()));
116     }
117 
getLength()118     public int getLength() {
119         return value.length();
120     }
121 
122     @Override
toString()123     public String toString() {
124         return value;
125     }
126 
127     @Override
equals(Object obj)128     public boolean equals(Object obj) {
129         if (obj instanceof Tag) {
130             return value.equals(((Tag) obj).getValue());
131         } else
132             return false;
133     }
134 
135     @Override
hashCode()136     public int hashCode() {
137         return value.hashCode();
138     }
139 
140     /**
141      * Java has more then 1 class compatible with a language-independent tag
142      * (!!int, !!float, !!timestamp etc)
143      *
144      * @param clazz
145      *            - Class to check compatibility
146      * @return true when the Class can be represented by this
147      *         language-independent tag
148      */
isCompatible(Class<?> clazz)149     public boolean isCompatible(Class<?> clazz) {
150         Set<Class<?>> set = COMPATIBILITY_MAP.get(this);
151         if (set != null) {
152             return set.contains(clazz);
153         } else {
154             return false;
155         }
156     }
157 
158     /**
159      * Check whether this tag matches the global tag for the Class
160      *
161      * @param clazz
162      *            - Class to check
163      * @return true when the this tag can be used as a global tag for the Class
164      */
matches(Class<? extends Object> clazz)165     public boolean matches(Class<? extends Object> clazz) {
166         return value.equals(Tag.PREFIX + clazz.getName());
167     }
168 
compareTo(Tag o)169     public int compareTo(Tag o) {
170         return value.compareTo(o.getValue());
171     }
172 }
173