super("content");
}
@Override
public Collection<Resource> apply(ContentItemBackend backend, Collection<Resource>... args) throws IllegalArgumentException {
ContentItem ci = ((ContentItemBackend)backend).getContentItem();
// Collection<Resource> contexts = args[0];
Set<String> mimeTypes;
if(args == null || args.length < 1){
mimeTypes = null;
} else {
//TODO: Wait for ld-path to parse the context
// http://code.google.com/p/ldpath/issues/detail?id=7
// //1. check if the first parameter is the context
// if(!args[0].isEmpty() && backend.isURI(args[0].iterator().next())){
// contexts = args[0];
// if(args.length > 1){ // cut the context from the args
// Collection<Resource>[] tmp = new Collection[args.length-1];
// System.arraycopy(args, 0, tmp, 0, tmp.length);
// args = tmp;
// } else {
// args = new Collection[]{};
// }
// } else { //use the ContentItem as context
// contexts = java.util.Collections.singleton((Resource)ci.getUri());
// }
mimeTypes = new HashSet<String>();
for(Iterator<Resource> params = concat(args).iterator();params.hasNext();){
Resource param = params.next();
String mediaTypeString = backend.stringValue(param);
try {
mimeTypes.add(parseMimeType(mediaTypeString).get(null));
} catch (IllegalArgumentException e) {
log.warn(String.format("Invalid mediaType '%s' (based on RFC 2046) parsed!",
mediaTypeString),e);
}
}
}
Collection<Resource> result;
Blob blob;
if(mimeTypes == null || mimeTypes.isEmpty()){
blob = ci.getBlob();
} else {
Entry<UriRef,Blob> entry = ContentItemHelper.getBlob(ci, mimeTypes);
blob = entry != null ? entry.getValue() : null;
}
if(blob == null){
result = java.util.Collections.emptySet();
} else {
String charset = blob.getParameter().get("charset");
try {
if(charset != null){
result = java.util.Collections.singleton(
backend.createLiteral(IOUtils.toString(blob.getStream(), charset)));
} else { //binary content
byte[] data = IOUtils.toByteArray(blob.getStream());
result = java.util.Collections.singleton(
(Resource)lf.createTypedLiteral(data));
}
} catch (IOException e) {
throw new IllegalStateException("Unable to read contents from Blob '"
+ blob.getMimeType()+"' of ContentItem "+ci.getUri(),e);
}
}
return result;
}