Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added cross-platform paths for Darwin and Windows #5

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
/target
/.idea
*.iml
187 changes: 152 additions & 35 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -1,38 +1,155 @@
<?xml version="1.0" encoding="UTF-8"?>
<project
xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xd/maven-4.0.0.xsd">

<modelVersion>4.0.0</modelVersion>

<groupId>org.freedesktop</groupId>
<artifactId>xdg-java</artifactId>
<version>0.0.1-SNAPSHOT</version>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.10</version>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
</build>
xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xd/maven-4.0.0.xsd">

<modelVersion>4.0.0</modelVersion>

<groupId>net.kothar</groupId>
<artifactId>xdg-java</artifactId>
<version>0.1.1</version>

<name>XDG Java Utilities</name>
<url>https://github.com/kothar/xdg-java</url>
<description>Java bindings for various FreeDesktop.org standards</description>

<developers>
<developer>
<name>Omair Majid</name>
<url>https://github.com/omajid</url>
</developer>
<developer>
<name>Michael Houston</name>
<email>[email protected]</email>
<organization>Kothar Labs</organization>
<organizationUrl>https://kothar.net</organizationUrl>
</developer>
</developers>

<licenses>
<license>
<name>GNU LESSER GENERAL PUBLIC LICENSE Version 2.1</name>
<url>https://opensource.org/licenses/LGPL-2.1</url>
</license>
</licenses>

<scm>
<url>https://github.com/kothar/xdg-java</url>
<developerConnection>scm:git:[email protected]:kothar/xdg-java.git</developerConnection>
<tag>HEAD</tag>
</scm>

<issueManagement>
<url>https://github.com/kothar/xdg-java/issues</url>
</issueManagement>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>

<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.10</version>
<scope>test</scope>
</dependency>
</dependencies>

<distributionManagement>
<snapshotRepository>
<id>ossrh</id>
<url>https://oss.sonatype.org/content/repositories/snapshots</url>
</snapshotRepository>
</distributionManagement>

<build>
<plugins>
<plugin>
<groupId>com.amashchenko.maven.plugin</groupId>
<artifactId>gitflow-maven-plugin</artifactId>
<version>1.12.0</version>
</plugin>

<!-- Release management -->
<plugin>
<groupId>org.sonatype.plugins</groupId>
<artifactId>nexus-staging-maven-plugin</artifactId>
<version>1.6.7</version>
<extensions>true</extensions>
<configuration>
<serverId>ossrh</serverId>
<nexusUrl>https://oss.sonatype.org/</nexusUrl>
<autoReleaseAfterClose>true</autoReleaseAfterClose>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>2.2.1</version>
<executions>
<execution>
<id>attach-sources</id>
<goals>
<goal>jar-no-fork</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>2.9.1</version>
<executions>
<execution>
<id>attach-javadocs</id>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-release-plugin</artifactId>
<version>2.5.3</version>
<configuration>
<releaseProfiles>release-sign-artifacts</releaseProfiles>
</configuration>
<dependencies>
<dependency>
<groupId>org.apache.maven.scm</groupId>
<artifactId>maven-scm-provider-gitexe</artifactId>
<version>1.9</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>

<profiles>
<profile>
<id>release-sign-artifacts</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-gpg-plugin</artifactId>
<version>1.5</version>
<executions>
<execution>
<id>sign-artifacts</id>
<phase>verify</phase>
<goals>
<goal>sign</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
</profiles>
</project>
24 changes: 10 additions & 14 deletions src/main/java/org/freedesktop/BaseDirectory.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public class BaseDirectory {
*/
public static final String XDG_RUNTIME_DIR = "XDG_RUNTIME_DIR";

private static Map<String,String> environment = System.getenv();
static Map<String,String> environment = Platform.environment;

/**
* Get the base directory or set of base directories defined by the
Expand Down Expand Up @@ -78,15 +78,10 @@ public static String get(String name) {
}
}

/** This is meant only for testing */
static void setEnvironment(Map<String,String> env) {
environment = env;
}

private static String getCacheHome() {
String value = environment.get(XDG_CACHE_HOME);
if (value == null || value.trim().length() == 0) {
String XDG_CACHE_HOME_DEFAULT = environment.get("HOME") + File.separator + ".cache";
String XDG_CACHE_HOME_DEFAULT = Platform.getCurrent().getCacheHome();
value = XDG_CACHE_HOME_DEFAULT;
}
return value;
Expand All @@ -95,7 +90,7 @@ private static String getCacheHome() {
private static String getConfigHome() {
String value = environment.get(XDG_CONFIG_HOME);
if (value == null || value.trim().length() == 0) {
String XDG_CONFIG_HOME_DEFAULT = environment.get("HOME") + File.separator + ".config";
String XDG_CONFIG_HOME_DEFAULT = Platform.getCurrent().getConfigHome();
value = XDG_CONFIG_HOME_DEFAULT;
}
return value;
Expand All @@ -104,7 +99,7 @@ private static String getConfigHome() {
private static String getConfigDirs() {
String value = environment.get(XDG_CONFIG_DIRS);
if (value == null || value.trim().length() == 0) {
String XDG_CONFIG_DIRS_DEFAULT = File.separator + "etc" + File.separator + "xdg";
String XDG_CONFIG_DIRS_DEFAULT = Platform.getCurrent().getConfigDirs();
value = XDG_CONFIG_DIRS_DEFAULT;
}
return value;
Expand All @@ -113,8 +108,7 @@ private static String getConfigDirs() {
private static String getDataHome() {
String value = environment.get(XDG_DATA_HOME);
if (value == null || value.trim().length() == 0) {
String XDG_DATA_HOME_DEFAULT = environment.get("HOME") +
File.separator + ".local" + File.separator + "share";
String XDG_DATA_HOME_DEFAULT = Platform.getCurrent().getDataHome();
value = XDG_DATA_HOME_DEFAULT;
}
return value;
Expand All @@ -123,16 +117,18 @@ private static String getDataHome() {
private static String getDataDirs() {
String value = environment.get(XDG_DATA_DIRS);
if (value == null || value.trim().length() == 0) {
String XDG_DATA_DIRS_DEFAULT = File.separator + "usr" + File.separator + "local" + File.separator + "share" + File.separator;
XDG_DATA_DIRS_DEFAULT = XDG_DATA_DIRS_DEFAULT + File.pathSeparator;
XDG_DATA_DIRS_DEFAULT = XDG_DATA_DIRS_DEFAULT + File.separator + "usr" + File.separator + "share" + File.separator;
String XDG_DATA_DIRS_DEFAULT = Platform.getCurrent().getDataDirs();
value = XDG_DATA_DIRS_DEFAULT;
}
return value;
}

private static String getRuntimeDir() {
String value = environment.get(XDG_RUNTIME_DIR);
if (value == null || value.trim().length() == 0) {
String XDG_RUNTIME_DIR_DEFAULT = Platform.getCurrent().getRuntimeDir();
value = XDG_RUNTIME_DIR_DEFAULT;
}
return value;
}

Expand Down
31 changes: 31 additions & 0 deletions src/main/java/org/freedesktop/Platform.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package org.freedesktop;

import java.util.Map;

import org.freedesktop.platforms.Darwin;
import org.freedesktop.platforms.Default;
import org.freedesktop.platforms.Windows;

public abstract class Platform {

protected static Map<String,String> environment = System.getenv();

public abstract String getCacheHome();
public abstract String getConfigDirs();
public abstract String getConfigHome();
public abstract String getDataDirs();
public abstract String getDataHome();
public abstract String getRuntimeDir();

public static Platform getCurrent() {

String osName = System.getProperty("os.name");
if (osName.startsWith("Mac OS X")) {
return new Darwin();
} else if (osName.startsWith("Windows")) {
return new Windows();
}

return new Default();
}
}
37 changes: 37 additions & 0 deletions src/main/java/org/freedesktop/platforms/Darwin.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package org.freedesktop.platforms;

import org.freedesktop.Platform;

public class Darwin extends Platform {

@Override
public String getCacheHome() {
return environment.get("HOME") + "/Library/Caches";
}

@Override
public String getConfigDirs() {
return "/Library/Preferences;/Library/Application Support";
}

@Override
public String getConfigHome() {
return environment.get("HOME") + "/Library/Preferences";
}

@Override
public String getDataDirs() {
return "/Library";
}

@Override
public String getDataHome() {
return environment.get("HOME") + "/Library";
}

@Override
public String getRuntimeDir() {
return environment.get("TMPDIR");
}

}
42 changes: 42 additions & 0 deletions src/main/java/org/freedesktop/platforms/Default.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package org.freedesktop.platforms;

import java.io.File;

import org.freedesktop.Platform;

public class Default extends Platform {

@Override
public String getCacheHome() {
return environment.get("HOME") + File.separator + ".cache";
}

@Override
public String getConfigDirs() {
return File.separator + "etc" + File.separator + "xdg";
}

@Override
public String getConfigHome() {
return environment.get("HOME") + File.separator + ".config";
}

@Override
public String getDataDirs() {
String XDG_DATA_DIRS_DEFAULT = File.separator + "usr" + File.separator + "local" + File.separator + "share" + File.separator;
XDG_DATA_DIRS_DEFAULT = XDG_DATA_DIRS_DEFAULT + File.pathSeparator;
XDG_DATA_DIRS_DEFAULT = XDG_DATA_DIRS_DEFAULT + File.separator + "usr" + File.separator + "share" + File.separator;
return XDG_DATA_DIRS_DEFAULT;
}

@Override
public String getDataHome() {
return environment.get("HOME") + File.separator + ".local" + File.separator + "share";
}

@Override
public String getRuntimeDir() {
return null;
}

}
Loading