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.util.Date;
22 import java.util.HashMap;
23 import java.util.HashSet;
24 import java.util.Map;
25 import java.util.Set;
26
27 import org.yaml.snakeyaml.error.YAMLException;
28 import org.yaml.snakeyaml.util.UriEncoder;
29
30 public final class Tag {
31     public static final String PREFIX = "tag:yaml.org,2002:";
32     public static final Tag YAML = new Tag(PREFIX + "yaml");
33     public static final Tag MERGE = new Tag(PREFIX + "merge");
34     public static final Tag SET = new Tag(PREFIX + "set");
35     public static final Tag PAIRS = new Tag(PREFIX + "pairs");
36     public static final Tag OMAP = new Tag(PREFIX + "omap");
37     public static final Tag BINARY = new Tag(PREFIX + "binary");
38     public static final Tag INT = new Tag(PREFIX + "int");
39     public static final Tag FLOAT = new Tag(PREFIX + "float");
40     public static final Tag TIMESTAMP = new Tag(PREFIX + "timestamp");
41     public static final Tag BOOL = new Tag(PREFIX + "bool");
42     public static final Tag NULL = new Tag(PREFIX + "null");
43     public static final Tag STR = new Tag(PREFIX + "str");
44     public static final Tag SEQ = new Tag(PREFIX + "seq");
45     public static final Tag MAP = new Tag(PREFIX + "map");
46     protected static final Map<Tag, Set<Class<?>>> COMPATIBILITY_MAP;
47     static {
48         COMPATIBILITY_MAP = new HashMap<Tag, Set<Class<?>>>();
49         Set<Class<?>> floatSet = new HashSet<Class<?>>();
50         floatSet.add(Double.class);
51         floatSet.add(Float.class);
52         floatSet.add(BigDecimal.class);
53         COMPATIBILITY_MAP.put(FLOAT, floatSet);
54         //
55         Set<Class<?>> intSet = new HashSet<Class<?>>();
56         intSet.add(Integer.class);
57         intSet.add(Long.class);
58         intSet.add(BigInteger.class);
59         COMPATIBILITY_MAP.put(INT, intSet);
60         //
61         Set<Class<?>> timestampSet = new HashSet<Class<?>>();
62         timestampSet.add(Date.class);
63
64         // java.sql is a separate module since jigsaw was introduced in java9
65         try {
66             timestampSet.add(Class.forName("java.sql.Date"));
67             timestampSet.add(Class.forName("java.sql.Timestamp"));
68         } catch (ClassNotFoundException ignored) {
69             // ignore - we are running in a module path without java.sql
70         }
71
72         COMPATIBILITY_MAP.put(TIMESTAMP, timestampSet);
73     }
74
75     private final String value;
76     private boolean secondary = false// see http://www.yaml.org/refcard.html
77
78     public Tag(String tag) {
79         if (tag == null) {
80             throw new NullPointerException("Tag must be provided.");
81         } else if (tag.length() == 0) {
82             throw new IllegalArgumentException("Tag must not be empty.");
83         } else if (tag.trim().length() != tag.length()) {
84             throw new IllegalArgumentException("Tag must not contain leading or trailing spaces.");
85         }
86         this.value = UriEncoder.encode(tag);
87         this.secondary = !tag.startsWith(PREFIX);
88     }
89
90     public Tag(Class<? extends Object> clazz) {
91         if (clazz == null) {
92             throw new NullPointerException("Class for tag must be provided.");
93         }
94         this.value = Tag.PREFIX + UriEncoder.encode(clazz.getName());
95     }
96
97     /**
98      * @deprecated - it will be removed
99      * @param uri - URI to be encoded as tag value
100      */

101     public Tag(URI uri) {
102         if (uri == null) {
103             throw new NullPointerException("URI for tag must be provided.");
104         }
105         this.value = uri.toASCIIString();
106     }
107
108     public boolean isSecondary() {
109         return secondary;
110     }
111
112     public String getValue() {
113         return value;
114     }
115
116     public boolean startsWith(String prefix) {
117         return value.startsWith(prefix);
118     }
119
120     public String getClassName() {
121         if (!value.startsWith(Tag.PREFIX)) {
122             throw new YAMLException("Invalid tag: " + value);
123         }
124         return UriEncoder.decode(value.substring(Tag.PREFIX.length()));
125     }
126
127     @Override
128     public String toString() {
129         return value;
130     }
131
132     @Override
133     public boolean equals(Object obj) {
134         if (obj instanceof Tag) {
135             return value.equals(((Tag) obj).getValue());
136         } else
137             return false;
138     }
139
140     @Override
141     public int hashCode() {
142         return value.hashCode();
143     }
144
145     /**
146      * Java has more then 1 class compatible with a language-independent tag
147      * (!!int, !!float, !!timestamp etc)
148      *
149      * @param clazz
150      *            - Class to check compatibility
151      * @return true when the Class can be represented by this
152      *         language-independent tag
153      */

154     public boolean isCompatible(Class<?> clazz) {
155         Set<Class<?>> set = COMPATIBILITY_MAP.get(this);
156         if (set != null) {
157             return set.contains(clazz);
158         } else {
159             return false;
160         }
161     }
162
163     /**
164      * Check whether this tag matches the global tag for the Class
165      * 
166      * @param clazz
167      *            - Class to check
168      * @return true when the this tag can be used as a global tag for the Class
169      */

170     public boolean matches(Class<? extends Object> clazz) {
171         return value.equals(Tag.PREFIX + clazz.getName());
172     }
173
174 }
175