package net.sourceforge.javautil.classloader.resolver.impl;
import net.sourceforge.javautil.classloader.resolver.IClassArtifactReference;
import net.sourceforge.javautil.classloader.resolver.IClassPackageReference;
import net.sourceforge.javautil.classloader.util.ClassPackageUtil;
import net.sourceforge.javautil.common.coersion.CoersionException;
import net.sourceforge.javautil.common.coersion.impl.CoersionAbstract;
/**
* This allows coersion of {@link IClassPackageReference}'s.
*
* @author elponderador
* @author $Author$
* @version $Id$
*/
public class ClassArtifactReferenceCoersion extends CoersionAbstract<String, IClassArtifactReference> {
public ClassArtifactReferenceCoersion() {
super(String.class, IClassArtifactReference.class);
this.toSuper = true;
}
public Object coerce(Object original, Class target) {
if (IClassArtifactReference.class.isAssignableFrom(target)) {
if (original instanceof String) {
String[] parts = ((String)original).split(":");
if (parts.length == 2)
return new ClassArtifactReferenceImpl(parts[0], parts[1]);
else if (parts.length == 3 || parts.length == 4)
return new ClassPackageReferenceImpl(parts[0], parts[1], parts[2], parts.length == 4 ? parts[3] : null);
else
throw new CoersionException("Invalid package reference: " + original);
}
} else if (target == String.class) {
if (original instanceof IClassArtifactReference) {
IClassArtifactReference ref = (IClassArtifactReference) original;
StringBuffer sb = new StringBuffer();
sb.append(ref.getGroupId()).append(":").append(ref.getArtifactId()).append(":");
if (ref instanceof IClassPackageReference) {
IClassPackageReference pref = (IClassPackageReference) ref;
sb.append(":").append(pref.getVersion().toVersionString());
if (pref.getClassifier() != null) sb.append(":").append(pref.getClassifier());
}
return sb.toString();
}
}
throw new CoersionException("Cannot coerce: " + original + " to " + target);
}
}