    001    package cpw.mods.fml.common.versioning;
    002
    003    import java.util.List;
    004    import java.util.logging.Level;
    005
    006    import com.google.common.base.Splitter;
    007    import com.google.common.base.Strings;
    008    import com.google.common.collect.Lists;
    009
    010    import cpw.mods.fml.common.FMLLog;
    011    import 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     */
    025    public 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    }