* @return The converted representation.
*/
public Representation toRepresentation(Object source, Variant target,
UniformResource resource) {
Representation result = null;
ConverterHelper ch = ConverterUtils.getBestHelper(source, target,
resource);
if (ch != null) {
try {
boolean loggable = (resource == null) ? true : resource
.isLoggable();
if (loggable
&& Context.getCurrentLogger().isLoggable(Level.FINE)) {
Context.getCurrentLogger().fine(
"Converter selected for "
+ source.getClass().getSimpleName() + ": "
+ ch.getClass().getSimpleName());
}
if (target == null) {
List<VariantInfo> variants = ch.getVariants(source
.getClass());
if ((variants != null) && !variants.isEmpty()) {
if (resource != null) {
target = resource.getClientInfo()
.getPreferredVariant(variants,
resource.getMetadataService());
} else {
target = variants.get(0);
}
} else {
target = new Variant();
}
}
result = ch.toRepresentation(source, target, resource);
if (result != null) {
// Copy the variant metadata if necessary
if (result.getCharacterSet() == null) {
result.setCharacterSet(target.getCharacterSet());