/*
 * Copyright 2012-2017 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.springframework.boot.context.embedded;

import java.io.FileReader;

import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathExpression;
import javax.xml.xpath.XPathFactory;

import org.xml.sax.InputSource;

/**
 * Provides access to dependency versions by querying the project's pom.
 *
 * @author Andy Wilkinson
 */
final class Versions {

	private Versions() {
	}

	public static String getBootVersion() {
		return evaluateExpression(
				"/*[local-name()='project']/*[local-name()='parent']/*[local-name()='version']"
						+ "/text()");
	}

	private static String evaluateExpression(String expression) {
		try {
			XPathFactory xPathFactory = XPathFactory.newInstance();
			XPath xpath = xPathFactory.newXPath();
			XPathExpression expr = xpath.compile(expression);
			String version = expr.evaluate(new InputSource(new FileReader("pom.xml")));
			return version;
		}
		catch (Exception ex) {
			throw new IllegalStateException("Failed to evaluate expression", ex);
		}
	}

}

@@ -667,16 +667,37 @@
 edded;%0A%0A
+import java.io.File;%0A
 import j
@@ -750,48 +750,8 @@
 th;%0A
-import javax.xml.xpath.XPathExpression;%0A
 impo
@@ -817,16 +817,62 @@
 ource;%0A%0A
+import org.springframework.util.StringUtils;%0A%0A
 /**%0A * P
@@ -1010,151 +1010,488 @@
 ate 
-Versions() %7B%0A%09%7D%0A%0A%09public static String getBootVersion() %7B%0A%09%09return evaluateExpression(%0A%09%09%09%09%22/*%5Blocal-name()='project'%5D/*%5Blocal-name()='parent'%5D
+static final String PROPERTIES = %22/*%5Blocal-name()='project'%5D/*%5Blocal-name()='properties'%5D%22;%0A%0A%09private Versions() %7B%0A%09%7D%0A%0A%09public static String getBootVersion() %7B%0A%09%09String baseDir = StringUtils.cleanPath(new File(%22.%22).getAbsolutePath());%0A%09%09String mainBaseDir = evaluateExpression(%22pom.xml%22,%0A%09%09%09%09PROPERTIES + %22/*%5Blocal-name()='main.basedir'%5D/text()%22);%0A%09%09mainBaseDir = mainBaseDir.replace(%22$%7Bbasedir%7D%22, baseDir);%0A%09%09return evaluateExpression(mainBaseDir + %22/pom.xml%22,%0A%09%09%09%09PROPERTIES + %22
 /*%5Bl
@@ -1507,28 +1507,18 @@
 ()='
-ver
+revi
 sion'%5D
-%22%0A%09%09%09%09%09%09+ %22
 /tex
@@ -1577,16 +1577,29 @@
 (String 
+file, String 
 expressi
@@ -1619,61 +1619,65 @@
 %0A%09%09%09
-XPathFactory xPathFactory = XPathFactory.newInstance(
+InputSource source = new InputSource(new FileReader(file)
 );%0A%09
@@ -1692,17 +1692,17 @@
 xpath = 
-x
+X
 PathFact
@@ -1712,166 +1712,83 @@
 .new
-XPath();%0A%09%09%09XPathExpression expr = xpath.compile(expression);%0A%09%09%09String version = expr.evaluate(new InputSource(new FileReader(%22pom.xml%22)));%0A%09%09%09return version
+Instance().newXPath();%0A%09%09%09return xpath.compile(expression).evaluate(source)
 ;%0A%09%09

Fix the way integration tests find version numbers