1 package org
.argeo
.api
.acr
;
3 import static javax
.xml
.XMLConstants
.W3C_XML_SCHEMA_NS_URI
;
6 import java
.net
.URISyntaxException
;
7 import java
.time
.Instant
;
8 import java
.time
.format
.DateTimeParseException
;
9 import java
.util
.Arrays
;
10 import java
.util
.Base64
;
11 import java
.util
.List
;
12 import java
.util
.UUID
;
14 import javax
.xml
.namespace
.QName
;
17 * Minimal standard attribute types that MUST be supported. All related classes
18 * belong to java.base and can be implicitly derived form a given
21 public enum CrAttributeType
{
22 BOOLEAN(Boolean
.class, W3C_XML_SCHEMA_NS_URI
, "boolean", new BooleanFormatter()), //
23 INTEGER(Integer
.class, W3C_XML_SCHEMA_NS_URI
, "integer", new IntegerFormatter()), //
24 LONG(Long
.class, W3C_XML_SCHEMA_NS_URI
, "long", new LongFormatter()), //
25 DOUBLE(Double
.class, W3C_XML_SCHEMA_NS_URI
, "double", new DoubleFormatter()), //
26 // we do not support short and float, like recent additions to Java
27 // (e.g. optional primitives)
28 DATE_TIME(Instant
.class, W3C_XML_SCHEMA_NS_URI
, "dateTime", new InstantFormatter()), //
29 UUID(UUID
.class, CrName
.CR_NAMESPACE_URI
, "uuid", new UuidFormatter()), //
30 ANY_URI(URI
.class, W3C_XML_SCHEMA_NS_URI
, "anyUri", new UriFormatter()), //
31 STRING(String
.class, W3C_XML_SCHEMA_NS_URI
, "string", new StringFormatter()), //
34 private final Class
<?
> clss
;
35 private final AttributeFormatter
<?
> formatter
;
37 private final ContentName qName
;
39 private <T
> CrAttributeType(Class
<T
> clss
, String namespaceUri
, String localName
, AttributeFormatter
<T
> formatter
) {
41 this.formatter
= formatter
;
43 qName
= new ContentName(namespaceUri
, localName
, RuntimeNamespaceContext
.getNamespaceContext());
46 public QName
getqName() {
50 public Class
<?
> getClss() {
54 public AttributeFormatter
<?
> getFormatter() {
59 // public String getDefaultPrefix() {
61 // return CrName.CR_DEFAULT_PREFIX;
67 // public String getNamespaceURI() {
69 // return CrName.CR_NAMESPACE_URI;
71 // return XMLConstants.W3C_XML_SCHEMA_NS_URI;
74 /** Default parsing procedure from a String to an object. */
75 public static Object
parse(String str
) {
77 throw new IllegalArgumentException("String cannot be null");
80 if (str
.length() == 4 || str
.length() == 5)
81 return BOOLEAN
.getFormatter().parse(str
);
82 } catch (IllegalArgumentException e
) {
86 return INTEGER
.getFormatter().parse(str
);
87 } catch (IllegalArgumentException e
) {
91 return LONG
.getFormatter().parse(str
);
92 } catch (IllegalArgumentException e
) {
96 return DOUBLE
.getFormatter().parse(str
);
97 } catch (IllegalArgumentException e
) {
101 return DATE_TIME
.getFormatter().parse(str
);
102 } catch (IllegalArgumentException e
) {
106 if (str
.length() == 36)
107 return UUID
.getFormatter().parse(str
);
108 } catch (IllegalArgumentException e
) {
112 java
.net
.URI uri
= (java
.net
.URI
) ANY_URI
.getFormatter().parse(str
);
113 if (uri
.getScheme() != null)
115 String path
= uri
.getPath();
116 if (path
.indexOf('/') >= 0)
118 // if it is not clearly a path, we will consider it as a string
119 // because their is no way to distinguish between 'any_string'
120 // and 'any_file_name'.
121 // Note that providing ./any_file_name would result in an equivalent URI
122 } catch (IllegalArgumentException e
) {
126 // TODO support QName as a type? It would require a NamespaceContext
127 // see https://www.oreilly.com/library/view/xml-schema/0596002521/re91.html
130 return STRING
.getFormatter().parse(str
);
133 /** Utility to convert a data: URI to bytes. */
134 public static byte[] bytesFromDataURI(URI uri
) {
135 if (!"data".equals(uri
.getScheme()))
136 throw new IllegalArgumentException("URI must have 'data' as a scheme");
137 String schemeSpecificPart
= uri
.getSchemeSpecificPart();
138 int commaIndex
= schemeSpecificPart
.indexOf(',');
139 String prefix
= schemeSpecificPart
.substring(0, commaIndex
);
140 List
<String
> info
= Arrays
.asList(prefix
.split(";"));
141 if (!info
.contains("base64"))
142 throw new IllegalArgumentException("URI must specify base64");
144 String base64Str
= schemeSpecificPart
.substring(commaIndex
+ 1);
145 return Base64
.getDecoder().decode(base64Str
);
149 /** Utility to convert bytes to a data: URI. */
150 public static URI
bytesToDataURI(byte[] arr
) {
151 String base64Str
= Base64
.getEncoder().encodeToString(arr
);
153 final String PREFIX
= "data:application/octet-stream;base64,";
154 return new URI(PREFIX
+ base64Str
);
155 } catch (URISyntaxException e
) {
156 throw new IllegalStateException("Cannot serialize bytes a Base64 data URI", e
);
161 static class BooleanFormatter
implements AttributeFormatter
<Boolean
> {
164 * @param str must be exactly equals to either 'true' or 'false' (different
165 * contract than {@link Boolean#parseBoolean(String)}.
168 public Boolean
parse(String str
) throws IllegalArgumentException
{
169 if ("true".equals(str
))
171 if ("false".equals(str
))
172 return Boolean
.FALSE
;
173 throw new IllegalArgumentException("Argument is neither 'true' or 'false' : " + str
);
177 static class IntegerFormatter
implements AttributeFormatter
<Integer
> {
179 public Integer
parse(String str
) throws NumberFormatException
{
180 return Integer
.parseInt(str
);
184 static class LongFormatter
implements AttributeFormatter
<Long
> {
186 public Long
parse(String str
) throws NumberFormatException
{
187 return Long
.parseLong(str
);
191 static class DoubleFormatter
implements AttributeFormatter
<Double
> {
194 public Double
parse(String str
) throws NumberFormatException
{
195 return Double
.parseDouble(str
);
199 static class InstantFormatter
implements AttributeFormatter
<Instant
> {
202 public Instant
parse(String str
) throws IllegalArgumentException
{
204 return Instant
.parse(str
);
205 } catch (DateTimeParseException e
) {
206 throw new IllegalArgumentException("Cannot parse '" + str
+ "' as an instant", e
);
211 static class UuidFormatter
implements AttributeFormatter
<UUID
> {
214 public UUID
parse(String str
) throws IllegalArgumentException
{
215 return java
.util
.UUID
.fromString(str
);
219 static class UriFormatter
implements AttributeFormatter
<URI
> {
222 public URI
parse(String str
) throws IllegalArgumentException
{
225 } catch (URISyntaxException e
) {
226 throw new IllegalArgumentException("Cannot parse " + str
+ " as an URI.", e
);
232 static class StringFormatter
implements AttributeFormatter
<String
> {
235 public String
parse(String str
) {
240 public String
format(String obj
) {