* @return the object's rdf:Id
* @throws InvalidRdfException thrown if the object does not support the minimum to create or retrieve an rdf:ID
* @see SupportsRdfId
*/
public static Resource id(Object theObj) throws InvalidRdfException {
SupportsRdfId aSupport = asSupportsRdfId(theObj);
if (aSupport.getRdfId() != null) {
return EmpireUtil.asResource(aSupport);
}
Field aIdField = BeanReflectUtil.getIdField(theObj.getClass());
String aValue = hash(Strings2.getRandomString(10));
String aNS = RdfId.DEFAULT;
URI aURI = FACTORY.createURI(aNS + aValue);
if (aIdField != null && !aIdField.getAnnotation(RdfId.class).namespace().equals("")) {
aNS = aIdField.getAnnotation(RdfId.class).namespace();
}
if (aIdField != null) {
boolean aOldAccess = aIdField.isAccessible();
aIdField.setAccessible(true);
try {
if (aIdField.get(theObj) == null) {
throw new InvalidRdfException("id field must have a value");
}
Object aValObj = aIdField.get(theObj);
aValue = Strings2.urlEncode(aValObj.toString());
if (aValObj instanceof java.net.URI || NetUtils.isURI(aValObj.toString())) {
try {
aURI = FACTORY.createURI(aValObj.toString());
}
catch (IllegalArgumentException e) {
// sometimes sesame disagrees w/ Java about what a valid URI is. so we'll have to try
// and construct a URI from the possible fragment
aURI = FACTORY.createURI(aNS + aValue);
}
}
else {
//aValue = hash(aValObj);
aURI = FACTORY.createURI(aNS + aValue);
}
}
catch (IllegalAccessException ex) {
throw new InvalidRdfException(ex);
}
aIdField.setAccessible(aOldAccess);
}
aSupport.setRdfId(new SupportsRdfId.URIKey(java.net.URI.create(aURI.toString())));
return aURI;
}