1 package org
.argeo
.api
.acr
;
4 import java
.net
.URISyntaxException
;
5 import java
.time
.Instant
;
6 import java
.time
.format
.DateTimeParseException
;
9 import javax
.xml
.XMLConstants
;
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
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()), //
29 private final Class
<?
> clss
;
30 private final AttributeFormatter
<?
> formatter
;
32 private <T
> CrAttributeType(Class
<T
> clss
, AttributeFormatter
<T
> formatter
) {
34 this.formatter
= formatter
;
37 public Class
<?
> getClss() {
41 public AttributeFormatter
<?
> getFormatter() {
46 public String
getDefaultPrefix() {
48 return CrName
.CR_DEFAULT_PREFIX
;
54 public String
getNamespaceURI() {
56 return CrName
.CR_NAMESPACE_URI
;
58 return XMLConstants
.W3C_XML_SCHEMA_NS_URI
;
61 public static Object
parse(String str
) {
63 throw new IllegalArgumentException("String cannot be null");
66 if (str
.length() == 4 || str
.length() == 5)
67 return BOOLEAN
.getFormatter().parse(str
);
68 } catch (IllegalArgumentException e
) {
72 return INTEGER
.getFormatter().parse(str
);
73 } catch (IllegalArgumentException e
) {
77 return LONG
.getFormatter().parse(str
);
78 } catch (IllegalArgumentException e
) {
82 return DOUBLE
.getFormatter().parse(str
);
83 } catch (IllegalArgumentException e
) {
87 return DATE_TIME
.getFormatter().parse(str
);
88 } catch (IllegalArgumentException e
) {
92 if (str
.length() == 36)
93 return UUID
.getFormatter().parse(str
);
94 } catch (IllegalArgumentException e
) {
98 java
.net
.URI uri
= (java
.net
.URI
) ANY_URI
.getFormatter().parse(str
);
99 if (uri
.getScheme() != null)
101 String path
= uri
.getPath();
102 if (path
.indexOf('/') >= 0)
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
) {
113 return STRING
.getFormatter().parse(str
);
116 static class BooleanFormatter
implements AttributeFormatter
<Boolean
> {
119 * @param str must be exactly equals to either 'true' or 'false' (different
120 * contract than {@link Boolean#parseBoolean(String)}.
123 public Boolean
parse(String str
) throws IllegalArgumentException
{
124 if ("true".equals(str
))
126 if ("false".equals(str
))
127 return Boolean
.FALSE
;
128 throw new IllegalArgumentException("Argument is neither 'true' or 'false' : " + str
);
132 static class IntegerFormatter
implements AttributeFormatter
<Integer
> {
134 public Integer
parse(String str
) throws NumberFormatException
{
135 return Integer
.parseInt(str
);
139 static class LongFormatter
implements AttributeFormatter
<Long
> {
141 public Long
parse(String str
) throws NumberFormatException
{
142 return Long
.parseLong(str
);
146 static class DoubleFormatter
implements AttributeFormatter
<Double
> {
149 public Double
parse(String str
) throws NumberFormatException
{
150 return Double
.parseDouble(str
);
154 static class InstantFormatter
implements AttributeFormatter
<Instant
> {
157 public Instant
parse(String str
) throws IllegalArgumentException
{
159 return Instant
.parse(str
);
160 } catch (DateTimeParseException e
) {
161 throw new IllegalArgumentException("Cannot parse '" + str
+ "' as an instant", e
);
166 static class UuidFormatter
implements AttributeFormatter
<UUID
> {
169 public UUID
parse(String str
) throws IllegalArgumentException
{
170 return java
.util
.UUID
.fromString(str
);
174 static class UriFormatter
implements AttributeFormatter
<URI
> {
177 public URI
parse(String str
) throws IllegalArgumentException
{
180 } catch (URISyntaxException e
) {
181 throw new IllegalArgumentException("Cannot parse " + str
+ " as an URI.", e
);
187 static class StringFormatter
implements AttributeFormatter
<String
> {
190 public String
parse(String str
) {
195 public String
format(String obj
) {