    001package cpw.mods.fml.common.versioning;
    002
    003import java.util.List;
    004import java.util.logging.Level;
    005
    006import com.google.common.base.Splitter;
    007import com.google.common.base.Strings;
    008import com.google.common.collect.Lists;
    009
    010import cpw.mods.fml.common.FMLLog;
    011import cpw.mods.fml.common.LoaderException;
    012
    013/**
    014 * Parses version strings according to the specification here:
    015 * http://docs.codehaus.org/display/MAVEN/Versioning
    016 * and allows for comparison of versions based on that document.
    017 * Bounded version specifications are defined as
    018 * http://maven.apache.org/plugins/maven-enforcer-plugin/rules/versionRanges.html
    019 *
    020 * Borrows heavily from maven version range management code
    021 *
    022 * @author cpw
    023 *
    024 */
    025public class VersionParser
    026{
    027    private static final Splitter SEPARATOR = Splitter.on('@').omitEmptyStrings().trimResults();
    028    public static ArtifactVersion parseVersionReference(String labelledRef)
    029    {
    030        if (Strings.isNullOrEmpty(labelledRef))
    031        {
    032            throw new RuntimeException(String.format("Empty reference %s", labelledRef));
    033        }
    034        List<String> parts = Lists.newArrayList(SEPARATOR.split(labelledRef));
    035        if (parts.size()>2)
    036        {
    037            throw new RuntimeException(String.format("Invalid versioned reference %s", labelledRef));
    038        }
    039        if (parts.size()==1)
    040        {
    041            return new DefaultArtifactVersion(parts.get(0), true);
    042        }
    043        return new DefaultArtifactVersion(parts.get(0),parseRange(parts.get(1)));
    044    }
    045
    046    public static boolean satisfies(ArtifactVersion target, ArtifactVersion source)
    047    {
    048        return target.containsVersion(source);
    049    }
    050
    051    public static VersionRange parseRange(String range)
    052    {
    053        try
    054        {
    055            return VersionRange.createFromVersionSpec(range);
    056        }
    057        catch (InvalidVersionSpecificationException e)
    058        {
    059            FMLLog.log(Level.SEVERE, e, "Unable to parse a version range specification successfully %s", range);
    060            throw new LoaderException(e);
    061        }
    062    }
    063}