Skip to content

Commit

Permalink
Adding support for relative file references
Browse files Browse the repository at this point in the history
  • Loading branch information
mainstringargs committed Aug 20, 2019
1 parent 79ed0e1 commit 42edca1
Show file tree
Hide file tree
Showing 3 changed files with 122 additions and 31 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ apply plugin: 'com.bmuschko.nexus'

archivesBaseName = 'FunStart4j'
group = 'io.github.mainstringargs'
version = '1.0.0'
version = '1.1.0'

// In this section you declare where to find the dependencies of your project
repositories {
Expand Down
74 changes: 58 additions & 16 deletions src/main/java/io/github/mainstringargs/funstart4j/FunStart4j.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package io.github.mainstringargs.funstart4j;

import java.io.File;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.file.Path;
import java.nio.file.Paths;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -24,42 +27,81 @@ public class FunStart4j {
*/
public static void main(String[] args) {

boolean urlRefSuccess = false;
final FunStart4JConfiguration configuration = FunStart4JConfiguration.getConfigurationFromArguments(args);

URL website = null;
URL url = null;
try {
if (args.length != 0) {
website = new URL(args[args.length - 1]);
String urlRef = args[args.length - 1];
try {

File file = new File(urlRef);
if (file.isFile()) {
url = file.toURI().toURL();
urlRefSuccess = true;
}
else if(urlRef.toLowerCase().startsWith("file")) {
Path path = Paths.get(urlRef.replaceAll("file://", ""));
path = path.normalize();
url = path.toFile().toURI().toURL();
urlRefSuccess = true;
}
else {
try {
url = new URL(urlRef);
urlRefSuccess = true;
} catch (Exception e1) {
e1.printStackTrace();
}
}
} catch (Exception e) {
logger.info("File loading failed, attempting to read as web URL");
e.printStackTrace();
try {
url = new URL(urlRef);
urlRefSuccess = true;
} catch (Exception e1) {
e1.printStackTrace();
}

}
} else {
website = new URL("https://worldwind.arc.nasa.gov/java/latest/webstart/AirspaceBuilder.jnlp");
url = new URL("https://worldwind.arc.nasa.gov/java/latest/webstart/AirspaceBuilder.jnlp");
urlRefSuccess = true;
}

} catch (MalformedURLException e) {
} catch (Exception e) {
e.printStackTrace();
if (logger.isInfoEnabled())
logger.info("Exception while creating URL", e);
}

try {
if (logger.isInfoEnabled())
logger.info("Grabbing JNLP from: " + website.toURI());
if (urlRefSuccess) {
try {
if (logger.isInfoEnabled())
logger.info("Grabbing JNLP from: " + url.toURI());

// JNLPHandler jnlpHandler = new JNLPHandler(website.toURI());
//
// jnlpHandler.parseJNLP();
//
// jnlpHandler.runApplication(configuration);

final URI jnlpLocation = website.toURI();
final URI jnlpLocation = url.toURI();

javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
FunStart4jGUI.createAndShowGUI(jnlpLocation, configuration);
}
});
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
FunStart4jGUI.createAndShowGUI(jnlpLocation, configuration);
}
});

} catch (URISyntaxException e) {
if (logger.isInfoEnabled())
logger.info("Exception while creating URI", e);
} catch (URISyntaxException e) {
if (logger.isInfoEnabled())
logger.info("Exception while creating URI", e);
}
} else {
logger.error("Loading " + url + " failed!");
}

}
Expand Down
77 changes: 63 additions & 14 deletions src/main/java/io/github/mainstringargs/funstart4j/JNLPHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
Expand Down Expand Up @@ -137,7 +138,21 @@ public void parseJNLP() {

Jnlp data = null;
try {
data = (Jnlp) um.unmarshal(jnlpUri.toURL());
URL url = jnlpUri.toURL();

if (url.getProtocol().equalsIgnoreCase("file")) {
try {
File file = new File(url.getPath());
data = (Jnlp) um.unmarshal(file);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

} else {
data = (Jnlp) um.unmarshal(url);
}

} catch (JAXBException | MalformedURLException e) {
if (logger.isInfoEnabled())
logger.info("Exception Unmarshalling", e);
Expand Down Expand Up @@ -198,25 +213,40 @@ public void parseJNLP(URI jnlpUri) {
JAXBContext jc = null;
try {
jc = JAXBContext.newInstance(Jnlp.class);
} catch (JAXBException e) {
} catch (Exception e) {
if (logger.isInfoEnabled())
logger.info("JAXBException", e);
logger.info("Exception", e);
}

Unmarshaller um = null;
try {
um = jc.createUnmarshaller();
} catch (JAXBException e) {
} catch (Exception e) {
if (logger.isInfoEnabled())
logger.info("JAXBException", e);
logger.info("Exception", e);
}

Jnlp data = null;

try {
data = (Jnlp) um.unmarshal(jnlpUri.toURL());
} catch (JAXBException | MalformedURLException e) {

if (jnlpUri.toString().toLowerCase().startsWith("file")) {
try {
File file = new File(jnlpUri.getRawPath());
data = (Jnlp) um.unmarshal(file);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

} else {
URL url = jnlpUri.toURL();
data = (Jnlp) um.unmarshal(url);
}

} catch (Exception e) {
if (logger.isInfoEnabled())
logger.info("JAXBException | MalformedURLException", e);
logger.info("Exception", e);
}

if (logger.isInfoEnabled()) {
Expand All @@ -237,7 +267,7 @@ public void parseJNLP(URI jnlpUri) {
}

for (Resources resource : resources) {
logger.info(resource.getArch() + " " + resource.getOs());
logger.info("resources: " + resource.getArch() + " " + resource.getOs());
for (Object libRef : resource.getJavaOrJ2SeOrJarOrNativelibOrExtensionOrPropertyOrPackage()) {

if (libRef instanceof Jar) {
Expand Down Expand Up @@ -471,22 +501,25 @@ private String getJavaLocation(String javaHome) {
*/
public static URI getURIReference(String codeBase, String parentRef, String fileName) {
String fullUri = codeBase + "/" + fileName;

if (codeBase.toUpperCase().startsWith("HTTP")) {
// no-op
} else if (fileName.toUpperCase().startsWith("HTTP")) {
fullUri = fileName;
} else if (parentRef.toUpperCase().startsWith("HTTP")) {
fullUri = parentRef + "/" + fileName;
} else if (parentRef.toUpperCase().startsWith("FILE")) {
return new File(parentRef.replaceAll("file://", "") + File.separator + fileName).toURI();
} else if (new File(parentRef).isDirectory()) {
return new File(parentRef + File.separator + fileName).toURI();
}

URI theUri = null;

try {
theUri = new URI(fullUri);
} catch (URISyntaxException e) {
} catch (Exception e) {
if (logger.isInfoEnabled())
logger.info("URISyntaxException", e);
logger.info("Exception", e);
}

return theUri.normalize();
Expand All @@ -513,9 +546,25 @@ public static String getFileNameFromUri(URI uri) {
* @return the parent uri
*/
private String getParentUri(URI uri) {
String uriString = uri.toString();

return uriString.substring(0, uriString.lastIndexOf('/') + 1).split("\\?")[0].split("#")[0];
try {
File f = new File(uri);

if (f.isFile()) {
return f.getParent();

} else {
String uriString = uri.toString();

return uriString.substring(0, uriString.lastIndexOf('/') + 1).split("\\?")[0].split("#")[0];

}
} catch (Exception e) {
String uriString = uri.toString();

return uriString.substring(0, uriString.lastIndexOf('/') + 1).split("\\?")[0].split("#")[0];
}

}

}

0 comments on commit 42edca1

Please sign in to comment.