Skip to content

Commit

Permalink
The name of the platform is saved in the configuration file to avoid …
Browse files Browse the repository at this point in the history
…using the wrong kind of platform.
  • Loading branch information
kelemen committed Oct 10, 2012
1 parent 87c6835 commit c3bfcbf
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 5 deletions.
33 changes: 29 additions & 4 deletions src/org/netbeans/gradle/project/persistent/XmlPropertyFormat.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import javax.xml.transform.stream.StreamResult;
import org.netbeans.api.java.platform.JavaPlatform;
import org.netbeans.api.java.platform.JavaPlatformManager;
import org.netbeans.api.java.platform.Specification;
import org.netbeans.gradle.project.properties.PredefinedTask;
import org.netbeans.gradle.project.properties.PropertiesSnapshot;
import org.openide.modules.SpecificationVersion;
Expand All @@ -40,6 +41,7 @@ final class XmlPropertyFormat {

private static final String ROOT_NODE = "gradle-project-properties";
private static final String SOURCE_ENCODING_NODE = "source-encoding";
private static final String PLATFORM_NAME_NODE = "target-platform-name";
private static final String PLATFORM_NODE = "target-platform";
private static final String SOURCE_LEVEL_NODE = "source-level";
private static final String COMMON_TASKS_NODE = "common-tasks";
Expand All @@ -56,6 +58,8 @@ final class XmlPropertyFormat {
private static final String VALUE_YES = "yes";
private static final String VALUE_NO = "no";

private static final String DEFAULT_SPECIFICATION_NAME = "j2se";

private static Element addChild(Node parent, String tagName) {
Element element = parent.getOwnerDocument().createElement(tagName);
parent.appendChild(element);
Expand Down Expand Up @@ -136,6 +140,7 @@ public static void saveToXml(File propertyfile, PropertiesSnapshot snapshot) {
addSimpleChild(root, SOURCE_ENCODING_NODE, sourceEncoding);

JavaPlatform platform = snapshot.getPlatform();
addSimpleChild(root, PLATFORM_NAME_NODE, platform.getSpecification().getName());
addSimpleChild(root, PLATFORM_NODE, platform.getSpecification().getVersion().toString());

String sourceLevel = snapshot.getSourceLevel();
Expand Down Expand Up @@ -179,7 +184,7 @@ private static Charset parseCharset(String name) {
return null;
}

private static JavaPlatform parsePlatform(String versionStr) {
private static JavaPlatform parsePlatform(String specName, String versionStr) {
SpecificationVersion version;
try {
version = new SpecificationVersion(versionStr);
Expand All @@ -190,7 +195,9 @@ private static JavaPlatform parsePlatform(String versionStr) {

JavaPlatform[] platforms = JavaPlatformManager.getDefault().getInstalledPlatforms();
for (JavaPlatform platform: platforms) {
if (version.equals(platform.getSpecification().getVersion())) {
Specification specification = platform.getSpecification();
if (specName.equalsIgnoreCase(specification.getName())
&& version.equals(specification.getVersion())) {
return platform;
}
}
Expand All @@ -207,12 +214,25 @@ private static JavaPlatform parsePlatform(String versionStr) {

JavaPlatform bestMatch = null;
for (JavaPlatform platform: platforms) {
Specification platformSpecification = platform.getSpecification();
if (platformSpecification == null) {
continue;
}

if (!specName.equalsIgnoreCase(platformSpecification.getName())) {
continue;
}

SpecificationVersion thisVersion = platformSpecification.getVersion();
if (thisVersion == null) {
continue;
}

if (bestMatch == null) {
bestMatch = platform;
}
else {
SpecificationVersion bestVersion = bestMatch.getSpecification().getVersion();
SpecificationVersion thisVersion = platform.getSpecification().getVersion();

// required version is greater than the one we currently have
if (version.compareTo(bestVersion) > 0) {
Expand Down Expand Up @@ -364,9 +384,14 @@ public static PropertiesSnapshot readFromXml(File propertiesFile) {
result.setSourceEncoding(sourceEncoding);
}

String platformName = tryGetValueOfNode(root, PLATFORM_NAME_NODE);
if (platformName == null) {
platformName = DEFAULT_SPECIFICATION_NAME;
}

String platformStr = tryGetValueOfNode(root, PLATFORM_NODE);
JavaPlatform platform = platformStr != null
? parsePlatform(platformStr)
? parsePlatform(platformName, platformStr)
: null;
if (platform != null) {
result.setPlatform(platform);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,22 @@
import java.util.Collection;
import java.util.regex.Pattern;
import org.netbeans.api.java.platform.JavaPlatform;
import org.netbeans.api.java.platform.Specification;
import org.openide.modules.SpecificationVersion;

public abstract class AbstractProjectProperties implements ProjectProperties {
public static final String DEFAULT_SOURCE_LEVEL = "1.7";
public static final Charset DEFAULT_SOURCE_ENCODING = Charset.forName("UTF-8");

public static String getSourceLevelFromPlatform(JavaPlatform platform) {
SpecificationVersion version = platform.getSpecification().getVersion();
if (version == null) {
return DEFAULT_SOURCE_LEVEL;
}

String[] versionParts = version.toString().split(Pattern.quote("."));
if (versionParts.length < 2) {
return "1.7";
return DEFAULT_SOURCE_LEVEL;
}
else {
return versionParts[0] + "." + versionParts[1];
Expand Down

0 comments on commit c3bfcbf

Please sign in to comment.