]> git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.api.acr/src/org/argeo/api/acr/CrAttributeType.java
Experiment with package level A2 metadata
[lgpl/argeo-commons.git] / org.argeo.api.acr / src / org / argeo / api / acr / CrAttributeType.java
1 package org.argeo.api.acr;
2
3 import java.net.URI;
4 import java.net.URISyntaxException;
5 import java.time.Instant;
6 import java.time.format.DateTimeParseException;
7 import java.util.UUID;
8
9 import javax.xml.XMLConstants;
10
11 /**
12 * Minimal standard attribute types that MUST be supported. All related classes
13 * belong to java.base and can be implicitly derived form a given
14 * <code>String<code>.
15 */
16 public enum CrAttributeType implements ContentNameSupplier {
17 BOOLEAN(Boolean.class, new BooleanFormatter()), //
18 INTEGER(Integer.class, new IntegerFormatter()), //
19 LONG(Long.class, new LongFormatter()), //
20 DOUBLE(Double.class, new DoubleFormatter()), //
21 // we do not support short and float, like recent additions to Java
22 // (e.g. optional primitives)
23 DATE_TIME(Instant.class, new InstantFormatter()), //
24 UUID(UUID.class, new UuidFormatter()), //
25 ANY_URI(URI.class, new UriFormatter()), //
26 STRING(String.class, new StringFormatter()), //
27 ;
28
29 private final Class<?> clss;
30 private final AttributeFormatter<?> formatter;
31
32 private <T> CrAttributeType(Class<T> clss, AttributeFormatter<T> formatter) {
33 this.clss = clss;
34 this.formatter = formatter;
35 }
36
37 public Class<?> getClss() {
38 return clss;
39 }
40
41 public AttributeFormatter<?> getFormatter() {
42 return formatter;
43 }
44
45 @Override
46 public String getDefaultPrefix() {
47 if (equals(UUID))
48 return CrName.CR_DEFAULT_PREFIX;
49 else
50 return "xs";
51 }
52
53 @Override
54 public String getNamespaceURI() {
55 if (equals(UUID))
56 return CrName.CR_NAMESPACE_URI;
57 else
58 return XMLConstants.W3C_XML_SCHEMA_NS_URI;
59 }
60
61 public static Object parse(String str) {
62 if (str == null)
63 throw new IllegalArgumentException("String cannot be null");
64 // order IS important
65 try {
66 if (str.length() == 4 || str.length() == 5)
67 return BOOLEAN.getFormatter().parse(str);
68 } catch (IllegalArgumentException e) {
69 // silent
70 }
71 try {
72 return INTEGER.getFormatter().parse(str);
73 } catch (IllegalArgumentException e) {
74 // silent
75 }
76 try {
77 return LONG.getFormatter().parse(str);
78 } catch (IllegalArgumentException e) {
79 // silent
80 }
81 try {
82 return DOUBLE.getFormatter().parse(str);
83 } catch (IllegalArgumentException e) {
84 // silent
85 }
86 try {
87 return DATE_TIME.getFormatter().parse(str);
88 } catch (IllegalArgumentException e) {
89 // silent
90 }
91 try {
92 if (str.length() == 36)
93 return UUID.getFormatter().parse(str);
94 } catch (IllegalArgumentException e) {
95 // silent
96 }
97 try {
98 java.net.URI uri = (java.net.URI) ANY_URI.getFormatter().parse(str);
99 if (uri.getScheme() != null)
100 return uri;
101 String path = uri.getPath();
102 if (path.indexOf('/') >= 0)
103 return uri;
104 // if it is not clearly a path, we will consider it as a string
105 // because their is no way to distinguish between 'any_string'
106 // and 'any_file_name'.
107 // Note that providing ./any_file_name would result in an equivalent URI
108 } catch (IllegalArgumentException e) {
109 // silent
110 }
111
112 // default
113 return STRING.getFormatter().parse(str);
114 }
115
116 static class BooleanFormatter implements AttributeFormatter<Boolean> {
117
118 /**
119 * @param str must be exactly equals to either 'true' or 'false' (different
120 * contract than {@link Boolean#parseBoolean(String)}.
121 */
122 @Override
123 public Boolean parse(String str) throws IllegalArgumentException {
124 if ("true".equals(str))
125 return Boolean.TRUE;
126 if ("false".equals(str))
127 return Boolean.FALSE;
128 throw new IllegalArgumentException("Argument is neither 'true' or 'false' : " + str);
129 }
130 }
131
132 static class IntegerFormatter implements AttributeFormatter<Integer> {
133 @Override
134 public Integer parse(String str) throws NumberFormatException {
135 return Integer.parseInt(str);
136 }
137 }
138
139 static class LongFormatter implements AttributeFormatter<Long> {
140 @Override
141 public Long parse(String str) throws NumberFormatException {
142 return Long.parseLong(str);
143 }
144 }
145
146 static class DoubleFormatter implements AttributeFormatter<Double> {
147
148 @Override
149 public Double parse(String str) throws NumberFormatException {
150 return Double.parseDouble(str);
151 }
152 }
153
154 static class InstantFormatter implements AttributeFormatter<Instant> {
155
156 @Override
157 public Instant parse(String str) throws IllegalArgumentException {
158 try {
159 return Instant.parse(str);
160 } catch (DateTimeParseException e) {
161 throw new IllegalArgumentException("Cannot parse '" + str + "' as an instant", e);
162 }
163 }
164 }
165
166 static class UuidFormatter implements AttributeFormatter<UUID> {
167
168 @Override
169 public UUID parse(String str) throws IllegalArgumentException {
170 return java.util.UUID.fromString(str);
171 }
172 }
173
174 static class UriFormatter implements AttributeFormatter<URI> {
175
176 @Override
177 public URI parse(String str) throws IllegalArgumentException {
178 try {
179 return new URI(str);
180 } catch (URISyntaxException e) {
181 throw new IllegalArgumentException("Cannot parse " + str + " as an URI.", e);
182 }
183 }
184
185 }
186
187 static class StringFormatter implements AttributeFormatter<String> {
188
189 @Override
190 public String parse(String str) {
191 return str;
192 }
193
194 @Override
195 public String format(String obj) {
196 return obj;
197 }
198
199 }
200
201 }