1b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov/** 211a89b445f3bde56bf07e6a0d04f0b0256dcb215Andrey Somov * Copyright (c) 2008, http://www.snakeyaml.org 3b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov * 4b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov * Licensed under the Apache License, Version 2.0 (the "License"); 5b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov * you may not use this file except in compliance with the License. 6b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov * You may obtain a copy of the License at 7b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov * 8b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov * http://www.apache.org/licenses/LICENSE-2.0 9b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov * 10b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov * Unless required by applicable law or agreed to in writing, software 11b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov * distributed under the License is distributed on an "AS IS" BASIS, 12b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov * See the License for the specific language governing permissions and 14b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov * limitations under the License. 15b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov */ 16b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somovpackage org.yaml.snakeyaml.nodes; 17b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov 18b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somovimport java.math.BigDecimal; 19b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somovimport java.math.BigInteger; 20b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somovimport java.net.URI; 21b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somovimport java.sql.Timestamp; 22b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somovimport java.util.Date; 23b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somovimport java.util.HashMap; 24b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somovimport java.util.HashSet; 25b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somovimport java.util.Map; 26b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somovimport java.util.Set; 27b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov 28b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somovimport org.yaml.snakeyaml.error.YAMLException; 29b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somovimport org.yaml.snakeyaml.util.UriEncoder; 30b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov 31b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somovpublic final class Tag implements Comparable<Tag> { 32b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov public static final String PREFIX = "tag:yaml.org,2002:"; 33b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov public static final Tag YAML = new Tag(PREFIX + "yaml"); 34b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov public static final Tag MERGE = new Tag(PREFIX + "merge"); 35b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov public static final Tag SET = new Tag(PREFIX + "set"); 36b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov public static final Tag PAIRS = new Tag(PREFIX + "pairs"); 37b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov public static final Tag OMAP = new Tag(PREFIX + "omap"); 38b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov public static final Tag BINARY = new Tag(PREFIX + "binary"); 39b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov public static final Tag INT = new Tag(PREFIX + "int"); 40b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov public static final Tag FLOAT = new Tag(PREFIX + "float"); 41b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov public static final Tag TIMESTAMP = new Tag(PREFIX + "timestamp"); 42b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov public static final Tag BOOL = new Tag(PREFIX + "bool"); 43b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov public static final Tag NULL = new Tag(PREFIX + "null"); 44b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov public static final Tag STR = new Tag(PREFIX + "str"); 45b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov public static final Tag SEQ = new Tag(PREFIX + "seq"); 46b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov public static final Tag MAP = new Tag(PREFIX + "map"); 47b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov public static final Map<Tag, Set<Class<?>>> COMPATIBILITY_MAP; 48b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov static { 49b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov COMPATIBILITY_MAP = new HashMap<Tag, Set<Class<?>>>(); 50b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov Set<Class<?>> floatSet = new HashSet<Class<?>>(); 51b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov floatSet.add(Double.class); 52b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov floatSet.add(Float.class); 53b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov floatSet.add(BigDecimal.class); 54b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov COMPATIBILITY_MAP.put(FLOAT, floatSet); 55b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov // 56b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov Set<Class<?>> intSet = new HashSet<Class<?>>(); 57b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov intSet.add(Integer.class); 58b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov intSet.add(Long.class); 59b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov intSet.add(BigInteger.class); 60b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov COMPATIBILITY_MAP.put(INT, intSet); 61b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov // 62b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov Set<Class<?>> timestampSet = new HashSet<Class<?>>(); 63b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov timestampSet.add(Date.class); 64b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov timestampSet.add(java.sql.Date.class); 65b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov timestampSet.add(Timestamp.class); 66b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov COMPATIBILITY_MAP.put(TIMESTAMP, timestampSet); 67b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov } 68b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov 69b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov private final String value; 709708cf2a564249d1972875b6586ec5a0921fc622maslovalex private boolean secondary = false; // see http://www.yaml.org/refcard.html 71b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov 72b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov public Tag(String tag) { 73b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov if (tag == null) { 74b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov throw new NullPointerException("Tag must be provided."); 75b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov } else if (tag.length() == 0) { 76b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov throw new IllegalArgumentException("Tag must not be empty."); 77b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov } else if (tag.trim().length() != tag.length()) { 78b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov throw new IllegalArgumentException("Tag must not contain leading or trailing spaces."); 79b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov } 80b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov this.value = UriEncoder.encode(tag); 819708cf2a564249d1972875b6586ec5a0921fc622maslovalex this.secondary = !tag.startsWith(PREFIX); 82b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov } 83b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov 84b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov public Tag(Class<? extends Object> clazz) { 85b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov if (clazz == null) { 86b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov throw new NullPointerException("Class for tag must be provided."); 87b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov } 88b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov this.value = Tag.PREFIX + UriEncoder.encode(clazz.getName()); 89b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov } 90b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov 91f5d32ad9b0c188f534f341555e24a6ec905ddd2cAndrey Somov //TODO to be removed ? 92b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov public Tag(URI uri) { 93b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov if (uri == null) { 94b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov throw new NullPointerException("URI for tag must be provided."); 95b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov } 96b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov this.value = uri.toASCIIString(); 97b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov } 98b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov 999708cf2a564249d1972875b6586ec5a0921fc622maslovalex public boolean isSecondary() { 1009708cf2a564249d1972875b6586ec5a0921fc622maslovalex return secondary; 1019708cf2a564249d1972875b6586ec5a0921fc622maslovalex } 1029708cf2a564249d1972875b6586ec5a0921fc622maslovalex 103b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov public String getValue() { 104b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov return value; 105b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov } 106b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov 107b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov public boolean startsWith(String prefix) { 108b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov return value.startsWith(prefix); 109b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov } 110b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov 111b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov public String getClassName() { 112b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov if (!value.startsWith(Tag.PREFIX)) { 113b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov throw new YAMLException("Invalid tag: " + value); 114b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov } 115b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov return UriEncoder.decode(value.substring(Tag.PREFIX.length())); 116b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov } 117b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov 118b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov public int getLength() { 119b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov return value.length(); 120b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov } 121b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov 122b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov @Override 123b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov public String toString() { 124b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov return value; 125b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov } 126b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov 127b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov @Override 128b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov public boolean equals(Object obj) { 129b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov if (obj instanceof Tag) { 130b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov return value.equals(((Tag) obj).getValue()); 131615d3719f75e5aad70d1598ef160123705008216Andrey Somov } else 132615d3719f75e5aad70d1598ef160123705008216Andrey Somov return false; 133b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov } 134b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov 135b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov @Override 136b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov public int hashCode() { 137b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov return value.hashCode(); 138b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov } 139b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov 140b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov /** 141b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov * Java has more then 1 class compatible with a language-independent tag 142b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov * (!!int, !!float, !!timestamp etc) 1439b3d9d765773e00ad481ac2d5e89eab32ae68ea1Andrey Somov * 144b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov * @param clazz 145b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov * - Class to check compatibility 146b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov * @return true when the Class can be represented by this 147b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov * language-independent tag 148b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov */ 149b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov public boolean isCompatible(Class<?> clazz) { 150b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov Set<Class<?>> set = COMPATIBILITY_MAP.get(this); 151b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov if (set != null) { 152b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov return set.contains(clazz); 153b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov } else { 154b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov return false; 155b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov } 156b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov } 157b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov 158b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov /** 159b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov * Check whether this tag matches the global tag for the Class 1609b3d9d765773e00ad481ac2d5e89eab32ae68ea1Andrey Somov * 161b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov * @param clazz 162b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov * - Class to check 163b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov * @return true when the this tag can be used as a global tag for the Class 164b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov */ 165b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov public boolean matches(Class<? extends Object> clazz) { 166b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov return value.equals(Tag.PREFIX + clazz.getName()); 167b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov } 168b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov 169b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov public int compareTo(Tag o) { 170b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov return value.compareTo(o.getValue()); 171b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov } 172b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov} 173