import java.util.Arrays;
import java.util.Base64;
import java.util.List;
+import java.util.Objects;
+import java.util.Optional;
import java.util.UUID;
import javax.xml.namespace.QName;
/**
* Minimal standard attribute types that MUST be supported. All related classes
* belong to java.base and can be implicitly derived form a given
- * <code>String<code>.
+ * <code>String</code>.
*/
public enum CrAttributeType {
BOOLEAN(Boolean.class, W3C_XML_SCHEMA_NS_URI, "boolean", new BooleanFormatter()), //
// we do not support short and float, like recent additions to Java
// (e.g. optional primitives)
DATE_TIME(Instant.class, W3C_XML_SCHEMA_NS_URI, "dateTime", new InstantFormatter()), //
- UUID(UUID.class, CrName.CR_NAMESPACE_URI, "uuid", new UuidFormatter()), //
+ UUID(UUID.class, ArgeoNamespace.CR_NAMESPACE_URI, "uuid", new UuidFormatter()), //
ANY_URI(URI.class, W3C_XML_SCHEMA_NS_URI, "anyUri", new UriFormatter()), //
STRING(String.class, W3C_XML_SCHEMA_NS_URI, "string", new StringFormatter()), //
;
private final Class<?> clss;
private final AttributeFormatter<?> formatter;
- private ContentName qName;
+ private final ContentName qName;
private <T> CrAttributeType(Class<T> clss, String namespaceUri, String localName, AttributeFormatter<T> formatter) {
this.clss = clss;
return qName;
}
- public void setqName(ContentName qName) {
- this.qName = qName;
- }
-
public Class<?> getClss() {
return clss;
}
return STRING.getFormatter().parse(str);
}
+ /**
+ * Cast well know java types based on {@link Object#toString()} of the provided
+ * object.
+ *
+ */
+ @SuppressWarnings("unchecked")
+ public static <T> Optional<T> cast(Class<T> clss, Object value) {
+ // TODO Or should we?
+ Objects.requireNonNull(value, "Cannot cast a null value");
+ if (String.class.isAssignableFrom(clss)) {
+ return Optional.of((T) value.toString());
+ }
+ // Numbers
+ else if (Long.class.isAssignableFrom(clss)) {
+ if (value instanceof Long)
+ return Optional.of((T) value);
+ return Optional.of((T) Long.valueOf(value.toString()));
+ } else if (Integer.class.isAssignableFrom(clss)) {
+ if (value instanceof Integer)
+ return Optional.of((T) value);
+ return Optional.of((T) Integer.valueOf(value.toString()));
+ } else if (Double.class.isAssignableFrom(clss)) {
+ if (value instanceof Double)
+ return Optional.of((T) value);
+ return Optional.of((T) Double.valueOf(value.toString()));
+ }
+ // Numbers
+// else if (Number.class.isAssignableFrom(clss)) {
+// if (value instanceof Number)
+// return Optional.of((T) value);
+// return Optional.of((T) Number.valueOf(value.toString()));
+// }
+ return Optional.empty();
+ }
+
/** Utility to convert a data: URI to bytes. */
public static byte[] bytesFromDataURI(URI uri) {
if (!"data".equals(uri.getScheme()))