4444import javax .xml .parsers .ParserConfigurationException ;
4545import javax .xml .xpath .XPath ;
4646import javax .xml .xpath .XPathConstants ;
47- import javax .xml .xpath .XPathExpression ;
48- import javax .xml .xpath .XPathExpressionException ;
4947import javax .xml .xpath .XPathFactory ;
5048
5149/**
5755 * Maven for available versions. Additionally, M2E APIs are internal and unstable, and thus may
5856 * change between versions.
5957 *
60- * <p>The artifact retriever reads Maven Central metadata xml files to retrieve available and latest
58+ * <p>The artifact retriever reads Maven Central metadata XML files to retrieve available and latest
6159 * versions.
6260 */
6361public class DataflowArtifactRetriever {
@@ -68,26 +66,6 @@ public class DataflowArtifactRetriever {
6866 ImmutableSortedSet .<ArtifactVersion >of (
6967 new DefaultArtifactVersion ("1.9.0" ), new DefaultArtifactVersion ("2.0.0-beta1" ));
7068
71- private static final XPath X_PATH = XPathFactory .newInstance ().newXPath ();
72-
73- private static XPathExpression createLatestVersionExpression () {
74- try {
75- return X_PATH .compile ("/metadata/versioning/latest" );
76- } catch (XPathExpressionException e ) {
77- throw new IllegalStateException (
78- "Could not create constant expression to select latest version" , e );
79- }
80- }
81-
82- private static XPathExpression createAllVersionsExpression () {
83- try {
84- return X_PATH .compile ("/metadata/versioning/versions/version" );
85- } catch (XPathExpressionException e ) {
86- throw new IllegalStateException (
87- "Could not create constant expression to select all versions" , e );
88- }
89- }
90-
9169 private static URL getMetadataUrl (String artifactId ) {
9270 try {
9371 return new URL (
@@ -110,13 +88,12 @@ private static URL getMetadataUrl(String artifactId) {
11088 .refreshAfterWrite (4 , TimeUnit .HOURS )
11189 .build (
11290 new CacheLoader <String , ArtifactVersion >() {
113- private final XPathExpression latestVersionExpression =
114- createLatestVersionExpression ();
11591
11692 @ Override
11793 public ArtifactVersion load (String artifactId ) throws Exception {
94+ XPath xpath = XPathFactory .newInstance ().newXPath ();
11895 Document document = getMetadataDocument (artifactId );
119- String result = latestVersionExpression .evaluate (document );
96+ String result = xpath .evaluate ("/metadata/versioning/latest" , document );
12097 return new DefaultArtifactVersion (result );
12198 }
12299 });
@@ -126,14 +103,15 @@ public ArtifactVersion load(String artifactId) throws Exception {
126103 .refreshAfterWrite (4 , TimeUnit .HOURS )
127104 .build (
128105 new CacheLoader <String , NavigableSet <ArtifactVersion >>() {
129- private final XPathExpression allVersionsExpression =
130- createAllVersionsExpression ();
131106
132107 @ Override
133108 public NavigableSet <ArtifactVersion > load (String artifactId ) throws Exception {
109+ XPath xpath = XPathFactory .newInstance ().newXPath ();
134110 Document document = getMetadataDocument (artifactId );
135- NodeList versionNodes =
136- (NodeList ) allVersionsExpression .evaluate (document , XPathConstants .NODESET );
111+ NodeList versionNodes = (NodeList ) xpath .evaluate (
112+ "/metadata/versioning/versions/version" ,
113+ document ,
114+ XPathConstants .NODESET );
137115 Builder <ArtifactVersion > versions = ImmutableSortedSet .naturalOrder ();
138116 for (int i = 0 ; i < versionNodes .getLength (); i ++) {
139117 String versionString = versionNodes .item (i ).getTextContent ();
0 commit comments