aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/org/yaml/snakeyaml/nodes/Tag.java
blob: dbb6f5a8e0b9ae58c85668eb916b3cc069c7877b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
/**
 * Copyright (c) 2008, SnakeYAML
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
 * in compliance with the License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License
 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 * or implied. See the License for the specific language governing permissions and limitations under
 * the License.
 */
package org.yaml.snakeyaml.nodes;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.net.URI;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import org.yaml.snakeyaml.error.YAMLException;
import org.yaml.snakeyaml.util.UriEncoder;

public final class Tag {

  public static final String PREFIX = "tag:yaml.org,2002:";
  public static final Tag YAML = new Tag(PREFIX + "yaml");
  public static final Tag MERGE = new Tag(PREFIX + "merge");
  public static final Tag SET = new Tag(PREFIX + "set");
  public static final Tag PAIRS = new Tag(PREFIX + "pairs");
  public static final Tag OMAP = new Tag(PREFIX + "omap");
  public static final Tag BINARY = new Tag(PREFIX + "binary");
  public static final Tag INT = new Tag(PREFIX + "int");
  public static final Tag FLOAT = new Tag(PREFIX + "float");
  public static final Tag TIMESTAMP = new Tag(PREFIX + "timestamp");
  public static final Tag BOOL = new Tag(PREFIX + "bool");
  public static final Tag NULL = new Tag(PREFIX + "null");
  public static final Tag STR = new Tag(PREFIX + "str");
  public static final Tag SEQ = new Tag(PREFIX + "seq");
  public static final Tag MAP = new Tag(PREFIX + "map");
  // For use to indicate a DUMMY node that contains comments, when there is no other (empty
  // document)
  public static final Tag COMMENT = new Tag(PREFIX + "comment");
  private static final Map<Tag, Set<Class<?>>> COMPATIBILITY_MAP;

  static {
    COMPATIBILITY_MAP = new HashMap<Tag, Set<Class<?>>>();
    Set<Class<?>> floatSet = new HashSet<Class<?>>();
    floatSet.add(Double.class);
    floatSet.add(Float.class);
    floatSet.add(BigDecimal.class);
    COMPATIBILITY_MAP.put(FLOAT, floatSet);
    //
    Set<Class<?>> intSet = new HashSet<Class<?>>();
    intSet.add(Integer.class);
    intSet.add(Long.class);
    intSet.add(BigInteger.class);
    COMPATIBILITY_MAP.put(INT, intSet);
    //
    Set<Class<?>> timestampSet = new HashSet<Class<?>>();
    timestampSet.add(Date.class);

    // java.sql is a separate module since jigsaw was introduced in java9
    try {
      timestampSet.add(Class.forName("java.sql.Date"));
      timestampSet.add(Class.forName("java.sql.Timestamp"));
    } catch (ClassNotFoundException ignored) {
      // ignore - we are running in a module path without java.sql
    }

    COMPATIBILITY_MAP.put(TIMESTAMP, timestampSet);
  }

  private final String value;
  private boolean secondary = false; // see http://www.yaml.org/refcard.html

  public Tag(String tag) {
    if (tag == null) {
      throw new NullPointerException("Tag must be provided.");
    } else if (tag.length() == 0) {
      throw new IllegalArgumentException("Tag must not be empty.");
    } else if (tag.trim().length() != tag.length()) {
      throw new IllegalArgumentException("Tag must not contain leading or trailing spaces.");
    }
    this.value = UriEncoder.encode(tag);
    this.secondary = !tag.startsWith(PREFIX);
  }

  public Tag(Class<? extends Object> clazz) {
    if (clazz == null) {
      throw new NullPointerException("Class for tag must be provided.");
    }
    this.value = Tag.PREFIX + UriEncoder.encode(clazz.getName());
  }

  /**
   * @deprecated - it will be removed
   * @param uri - URI to be encoded as tag value
   */
  @Deprecated
  public Tag(URI uri) {
    if (uri == null) {
      throw new NullPointerException("URI for tag must be provided.");
    }
    this.value = uri.toASCIIString();
  }

  public boolean isSecondary() {
    return secondary;
  }

  public String getValue() {
    return value;
  }

  public boolean startsWith(String prefix) {
    return value.startsWith(prefix);
  }

  public String getClassName() {
    if (!value.startsWith(Tag.PREFIX)) {
      throw new YAMLException("Invalid tag: " + value);
    }
    return UriEncoder.decode(value.substring(Tag.PREFIX.length()));
  }

  @Override
  public String toString() {
    return value;
  }

  @Override
  public boolean equals(Object obj) {
    if (obj instanceof Tag) {
      return value.equals(((Tag) obj).getValue());
    } else {
      return false;
    }
  }

  @Override
  public int hashCode() {
    return value.hashCode();
  }

  /**
   * Java has more then 1 class compatible with a language-independent tag (!!int, !!float,
   * !!timestamp etc)
   *
   * @param clazz - Class to check compatibility
   * @return true when the Class can be represented by this language-independent tag
   */
  public boolean isCompatible(Class<?> clazz) {
    Set<Class<?>> set = COMPATIBILITY_MAP.get(this);
    if (set != null) {
      return set.contains(clazz);
    } else {
      return false;
    }
  }

  /**
   * Check whether this tag matches the global tag for the Class
   *
   * @param clazz - Class to check
   * @return true when the this tag can be used as a global tag for the Class
   */
  public boolean matches(Class<? extends Object> clazz) {
    return value.equals(Tag.PREFIX + clazz.getName());
  }

}