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 | 0 | public final class Tag implements Comparable<Tag> { |
32 | |
public static final String PREFIX = "tag:yaml.org,2002:"; |
33 | 1 | public static final Tag YAML = new Tag(PREFIX + "yaml"); |
34 | 1 | public static final Tag VALUE = new Tag(PREFIX + "value"); |
35 | 1 | public static final Tag MERGE = new Tag(PREFIX + "merge"); |
36 | 1 | public static final Tag SET = new Tag(PREFIX + "set"); |
37 | 1 | public static final Tag PAIRS = new Tag(PREFIX + "pairs"); |
38 | 1 | public static final Tag OMAP = new Tag(PREFIX + "omap"); |
39 | 1 | public static final Tag BINARY = new Tag(PREFIX + "binary"); |
40 | 1 | public static final Tag INT = new Tag(PREFIX + "int"); |
41 | 1 | public static final Tag FLOAT = new Tag(PREFIX + "float"); |
42 | 1 | public static final Tag TIMESTAMP = new Tag(PREFIX + "timestamp"); |
43 | 1 | public static final Tag BOOL = new Tag(PREFIX + "bool"); |
44 | 1 | public static final Tag NULL = new Tag(PREFIX + "null"); |
45 | 1 | public static final Tag STR = new Tag(PREFIX + "str"); |
46 | 1 | public static final Tag SEQ = new Tag(PREFIX + "seq"); |
47 | 1 | public static final Tag MAP = new Tag(PREFIX + "map"); |
48 | |
public static final Map<Tag, Set<Class<?>>> COMPATIBILITY_MAP; |
49 | |
static { |
50 | 1 | COMPATIBILITY_MAP = new HashMap<Tag, Set<Class<?>>>(); |
51 | 1 | Set<Class<?>> floatSet = new HashSet<Class<?>>(); |
52 | 1 | floatSet.add(Double.class); |
53 | 1 | floatSet.add(Float.class); |
54 | 1 | floatSet.add(BigDecimal.class); |
55 | 1 | COMPATIBILITY_MAP.put(FLOAT, floatSet); |
56 | |
|
57 | 1 | Set<Class<?>> intSet = new HashSet<Class<?>>(); |
58 | 1 | intSet.add(Integer.class); |
59 | 1 | intSet.add(Long.class); |
60 | 1 | intSet.add(BigInteger.class); |
61 | 1 | COMPATIBILITY_MAP.put(INT, intSet); |
62 | |
|
63 | 1 | Set<Class<?>> timestampSet = new HashSet<Class<?>>(); |
64 | 1 | timestampSet.add(Date.class); |
65 | 1 | timestampSet.add(java.sql.Date.class); |
66 | 1 | timestampSet.add(Timestamp.class); |
67 | 1 | COMPATIBILITY_MAP.put(TIMESTAMP, timestampSet); |
68 | 1 | } |
69 | |
|
70 | |
private final String value; |
71 | |
|
72 | 6294 | public Tag(String tag) { |
73 | 6294 | if (tag == null) { |
74 | 1 | throw new NullPointerException("Tag must be provided."); |
75 | 6293 | } else if (tag.length() == 0) { |
76 | 1 | throw new IllegalArgumentException("Tag must not be empty."); |
77 | 6292 | } else if (tag.trim().length() != tag.length()) { |
78 | 1 | throw new IllegalArgumentException("Tag must not contain leading or trailing spaces."); |
79 | |
} |
80 | 6291 | this.value = UriEncoder.encode(tag); |
81 | 6291 | } |
82 | |
|
83 | 45179 | public Tag(Class<? extends Object> clazz) { |
84 | 45179 | if (clazz == null) { |
85 | 3 | throw new NullPointerException("Class for tag must be provided."); |
86 | |
} |
87 | 45176 | this.value = Tag.PREFIX + UriEncoder.encode(clazz.getName()); |
88 | 45176 | } |
89 | |
|
90 | 2 | public Tag(URI uri) { |
91 | 2 | if (uri == null) { |
92 | 1 | throw new NullPointerException("URI for tag must be provided."); |
93 | |
} |
94 | 1 | this.value = uri.toASCIIString(); |
95 | 1 | } |
96 | |
|
97 | |
public String getValue() { |
98 | 2418932 | return value; |
99 | |
} |
100 | |
|
101 | |
public boolean startsWith(String prefix) { |
102 | 434 | return value.startsWith(prefix); |
103 | |
} |
104 | |
|
105 | |
public String getClassName() { |
106 | 322 | if (!value.startsWith(Tag.PREFIX)) { |
107 | 4 | throw new YAMLException("Invalid tag: " + value); |
108 | |
} |
109 | 318 | return UriEncoder.decode(value.substring(Tag.PREFIX.length())); |
110 | |
} |
111 | |
|
112 | |
public int getLength() { |
113 | 1 | return value.length(); |
114 | |
} |
115 | |
|
116 | |
@Override |
117 | |
public String toString() { |
118 | 102 | return value; |
119 | |
} |
120 | |
|
121 | |
@Override |
122 | |
public boolean equals(Object obj) { |
123 | 1688240 | if (obj instanceof Tag) { |
124 | 1688232 | return value.equals(((Tag) obj).getValue()); |
125 | 8 | } else if (obj instanceof String) { |
126 | 6 | if (value.equals(obj.toString())) { |
127 | |
|
128 | 2 | System.err.println("Comparing Tag and String is deprecated."); |
129 | 2 | return true; |
130 | |
} |
131 | |
} |
132 | 6 | return false; |
133 | |
} |
134 | |
|
135 | |
@Override |
136 | |
public int hashCode() { |
137 | 2620037 | return value.hashCode(); |
138 | |
} |
139 | |
|
140 | |
|
141 | |
|
142 | |
|
143 | |
|
144 | |
|
145 | |
|
146 | |
|
147 | |
|
148 | |
|
149 | |
public boolean isCompatible(Class<?> clazz) { |
150 | 508997 | Set<Class<?>> set = COMPATIBILITY_MAP.get(this); |
151 | 508997 | if (set != null) { |
152 | 101140 | return set.contains(clazz); |
153 | |
} else { |
154 | 407857 | return false; |
155 | |
} |
156 | |
} |
157 | |
|
158 | |
|
159 | |
|
160 | |
|
161 | |
|
162 | |
|
163 | |
|
164 | |
|
165 | |
public boolean matches(Class<? extends Object> clazz) { |
166 | 200124 | return value.equals(Tag.PREFIX + clazz.getName()); |
167 | |
} |
168 | |
|
169 | |
public int compareTo(Tag o) { |
170 | 1 | return value.compareTo(o.getValue()); |
171 | |
} |
172 | |
} |