1
2
3
4
5
6
7
8
9
10
11
12
13
14
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 VALUE = new Tag(PREFIX + "value");
35 public static final Tag MERGE = new Tag(PREFIX + "merge");
36 public static final Tag SET = new Tag(PREFIX + "set");
37 public static final Tag PAIRS = new Tag(PREFIX + "pairs");
38 public static final Tag OMAP = new Tag(PREFIX + "omap");
39 public static final Tag BINARY = new Tag(PREFIX + "binary");
40 public static final Tag INT = new Tag(PREFIX + "int");
41 public static final Tag FLOAT = new Tag(PREFIX + "float");
42 public static final Tag TIMESTAMP = new Tag(PREFIX + "timestamp");
43 public static final Tag BOOL = new Tag(PREFIX + "bool");
44 public static final Tag NULL = new Tag(PREFIX + "null");
45 public static final Tag STR = new Tag(PREFIX + "str");
46 public static final Tag SEQ = new Tag(PREFIX + "seq");
47 public static final Tag MAP = new Tag(PREFIX + "map");
48 public static final Map<Tag, Set<Class<?>>> COMPATIBILITY_MAP;
49 static {
50 COMPATIBILITY_MAP = new HashMap<Tag, Set<Class<?>>>();
51 Set<Class<?>> floatSet = new HashSet<Class<?>>();
52 floatSet.add(Double.class);
53 floatSet.add(Float.class);
54 floatSet.add(BigDecimal.class);
55 COMPATIBILITY_MAP.put(FLOAT, floatSet);
56
57 Set<Class<?>> intSet = new HashSet<Class<?>>();
58 intSet.add(Integer.class);
59 intSet.add(Long.class);
60 intSet.add(BigInteger.class);
61 COMPATIBILITY_MAP.put(INT, intSet);
62
63 Set<Class<?>> timestampSet = new HashSet<Class<?>>();
64 timestampSet.add(Date.class);
65 timestampSet.add(java.sql.Date.class);
66 timestampSet.add(Timestamp.class);
67 COMPATIBILITY_MAP.put(TIMESTAMP, timestampSet);
68 }
69
70 private final String value;
71
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 }
82
83 public Tag(Class<? extends Object> clazz) {
84 if (clazz == null) {
85 throw new NullPointerException("Class for tag must be provided.");
86 }
87 this.value = Tag.PREFIX + UriEncoder.encode(clazz.getName());
88 }
89
90 public Tag(URI uri) {
91 if (uri == null) {
92 throw new NullPointerException("URI for tag must be provided.");
93 }
94 this.value = uri.toASCIIString();
95 }
96
97 public String getValue() {
98 return value;
99 }
100
101 public boolean startsWith(String prefix) {
102 return value.startsWith(prefix);
103 }
104
105 public String getClassName() {
106 if (!value.startsWith(Tag.PREFIX)) {
107 throw new YAMLException("Invalid tag: " + value);
108 }
109 return UriEncoder.decode(value.substring(Tag.PREFIX.length()));
110 }
111
112 public int getLength() {
113 return value.length();
114 }
115
116 @Override
117 public String toString() {
118 return value;
119 }
120
121 @Override
122 public boolean equals(Object obj) {
123 if (obj instanceof Tag) {
124 return value.equals(((Tag) obj).getValue());
125 } else if (obj instanceof String) {
126 if (value.equals(obj.toString())) {
127
128 System.err.println("Comparing Tag and String is deprecated.");
129 return true;
130 }
131 }
132 return false;
133 }
134
135 @Override
136 public int hashCode() {
137 return value.hashCode();
138 }
139
140
141
142
143
144
145
146
147
148
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
160
161
162
163
164
165 public boolean matches(Class<? extends Object> clazz) {
166 return value.equals(Tag.PREFIX + clazz.getName());
167 }
168
169 public int compareTo(Tag o) {
170 return value.compareTo(o.getValue());
171 }
172 }