1+ package boot .bin ;
2+
3+ import java .util .ArrayDeque ;
4+ import java .util .Deque ;
5+ import java .util .Arrays ;
6+ import javax .xml .parsers .SAXParser ;
7+ import javax .xml .parsers .SAXParserFactory ;
8+ import org .xml .sax .Attributes ;
9+ import org .xml .sax .SAXException ;
10+ import org .xml .sax .helpers .DefaultHandler ;
11+
12+ public class BootSnapshotVersionFetcher {
13+
14+ private static class MavenMetadataHandler extends DefaultHandler {
15+ String snapshotVersion ;
16+ Deque <String > stack = new ArrayDeque <>();
17+
18+ String marker [] = {"value" , "snapshotVersion" , "snapshotVersions" , "versioning" , "metadata" };
19+
20+ public String getSnapshotVersion () {
21+ return snapshotVersion ; }
22+
23+ public void startElement (String uri , String localName , String qName , Attributes attributes ) throws SAXException {
24+ stack .push (qName ); }
25+
26+ public void endElement (String uri , String localName , String qName ) throws SAXException {
27+ stack .pop (); }
28+
29+ public void characters (char ch [], int start , int length ) throws SAXException {
30+ if (Arrays .equals (stack .toArray (), marker ) && snapshotVersion == null ) {
31+ // String versionString = new String(ch, start, length);
32+ // System.out.println("version " + versionString);
33+ snapshotVersion = new String (ch , start , length ); }}};
34+
35+ public static String lastSnapshot (String version ) throws Exception {
36+ MavenMetadataHandler handler = new MavenMetadataHandler ();
37+ try { SAXParserFactory factory = SAXParserFactory .newInstance ();
38+ SAXParser saxParser = factory .newSAXParser ();
39+ String metadataFile = String .format ("https://clojars.org/repo/boot/base/%s/maven-metadata.xml" , version );
40+ saxParser .parse (metadataFile , handler ); }
41+ catch (Exception e ) { e .printStackTrace (); }
42+
43+ return handler .getSnapshotVersion (); }}
0 commit comments