View Javadoc

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.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                 // TODO to be removed later (version 2.0?)
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      * 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      */
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      */
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 }