Resource aSubj = id(aObj);
addNamespaces(aObj.getClass());
GraphBuilder aBuilder = new GraphBuilder();
Collection<AccessibleObject> aAccessors = new HashSet<AccessibleObject>();
aAccessors.addAll(getAnnotatedFields(aObj.getClass()));
aAccessors.addAll(getAnnotatedGetters(aObj.getClass(), true));
try {
ResourceBuilder aRes = aBuilder.instance(aBuilder.getValueFactory().createURI(PrefixMapping.GLOBAL.uri(aClass.value())),
aSubj);
for (AccessibleObject aAccess : aAccessors) {
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("Getting rdf for : {}", aAccess);
}
AsValueFunction aFunc = new AsValueFunction(aAccess);
if (aAccess.isAnnotationPresent(Transient.class)
|| (aAccess instanceof Field
&& Modifier.isTransient( ((Field)aAccess).getModifiers() ))) {
// transient fields or accessors with the Transient annotation do not get converted.
continue;
}
RdfProperty aPropertyAnnotation = BeanReflectUtil.getAnnotation(aAccess, RdfProperty.class);
String aBase = "urn:empire:clark-parsia:";
if (aRes instanceof URI) {
aBase = ((URI)aRes).getNamespace();
}
URI aProperty = aPropertyAnnotation != null
? aBuilder.getValueFactory().createURI(PrefixMapping.GLOBAL.uri(aPropertyAnnotation.value()))
: (aAccess instanceof Field ? aBuilder.getValueFactory().createURI(aBase + ((Field)aAccess).getName()) : null);
boolean aOldAccess = aAccess.isAccessible();
setAccessible(aAccess, true);
Object aValue = get(aAccess, aObj);
setAccessible(aAccess, aOldAccess);
if (aValue == null || aValue.toString().equals("")) {
continue;
}
else if (Collection.class.isAssignableFrom(aValue.getClass())) {
@SuppressWarnings("unchecked")
List<Value> aValueList = asList(aAccess, (Collection<?>) Collection.class.cast(aValue));
if (aValueList.isEmpty()) {
continue;
}
if (aPropertyAnnotation.isList()) {
aRes.addProperty(aProperty, aValueList);
}
else {
for (Value aVal : aValueList) {
aRes.addProperty(aProperty, aVal);
}
}
}
else {
aRes.addProperty(aProperty, aFunc.apply(aValue));
}
}
}
catch (IllegalAccessException e) {
throw new InvalidRdfException(e);
}
catch (RuntimeException e) {
throw new InvalidRdfException(e);
}
catch (InvocationTargetException e) {
throw new InvalidRdfException("Cannot invoke method", e);
}
return aBuilder.graph();
}