Skip to content

Commit 42edca1

Browse files
Adding support for relative file references
1 parent 79ed0e1 commit 42edca1

File tree

3 files changed

+122
-31
lines changed

3 files changed

+122
-31
lines changed

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ apply plugin: 'com.bmuschko.nexus'
3737

3838
archivesBaseName = 'FunStart4j'
3939
group = 'io.github.mainstringargs'
40-
version = '1.0.0'
40+
version = '1.1.0'
4141

4242
// In this section you declare where to find the dependencies of your project
4343
repositories {

src/main/java/io/github/mainstringargs/funstart4j/FunStart4j.java

Lines changed: 58 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
package io.github.mainstringargs.funstart4j;
22

3+
import java.io.File;
34
import java.net.MalformedURLException;
45
import java.net.URI;
56
import java.net.URISyntaxException;
67
import java.net.URL;
8+
import java.nio.file.Path;
9+
import java.nio.file.Paths;
710

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

30+
boolean urlRefSuccess = false;
2731
final FunStart4JConfiguration configuration = FunStart4JConfiguration.getConfigurationFromArguments(args);
2832

29-
URL website = null;
33+
URL url = null;
3034
try {
3135
if (args.length != 0) {
32-
website = new URL(args[args.length - 1]);
36+
String urlRef = args[args.length - 1];
37+
try {
38+
39+
File file = new File(urlRef);
40+
if (file.isFile()) {
41+
url = file.toURI().toURL();
42+
urlRefSuccess = true;
43+
}
44+
else if(urlRef.toLowerCase().startsWith("file")) {
45+
Path path = Paths.get(urlRef.replaceAll("file://", ""));
46+
path = path.normalize();
47+
url = path.toFile().toURI().toURL();
48+
urlRefSuccess = true;
49+
}
50+
else {
51+
try {
52+
url = new URL(urlRef);
53+
urlRefSuccess = true;
54+
} catch (Exception e1) {
55+
e1.printStackTrace();
56+
}
57+
}
58+
} catch (Exception e) {
59+
logger.info("File loading failed, attempting to read as web URL");
60+
e.printStackTrace();
61+
try {
62+
url = new URL(urlRef);
63+
urlRefSuccess = true;
64+
} catch (Exception e1) {
65+
e1.printStackTrace();
66+
}
67+
68+
}
3369
} else {
34-
website = new URL("https://worldwind.arc.nasa.gov/java/latest/webstart/AirspaceBuilder.jnlp");
70+
url = new URL("https://worldwind.arc.nasa.gov/java/latest/webstart/AirspaceBuilder.jnlp");
71+
urlRefSuccess = true;
3572
}
3673

37-
} catch (MalformedURLException e) {
74+
} catch (Exception e) {
75+
e.printStackTrace();
3876
if (logger.isInfoEnabled())
3977
logger.info("Exception while creating URL", e);
4078
}
4179

42-
try {
43-
if (logger.isInfoEnabled())
44-
logger.info("Grabbing JNLP from: " + website.toURI());
80+
if (urlRefSuccess) {
81+
try {
82+
if (logger.isInfoEnabled())
83+
logger.info("Grabbing JNLP from: " + url.toURI());
4584

4685
// JNLPHandler jnlpHandler = new JNLPHandler(website.toURI());
4786
//
4887
// jnlpHandler.parseJNLP();
4988
//
5089
// jnlpHandler.runApplication(configuration);
5190

52-
final URI jnlpLocation = website.toURI();
91+
final URI jnlpLocation = url.toURI();
5392

54-
javax.swing.SwingUtilities.invokeLater(new Runnable() {
55-
public void run() {
56-
FunStart4jGUI.createAndShowGUI(jnlpLocation, configuration);
57-
}
58-
});
93+
javax.swing.SwingUtilities.invokeLater(new Runnable() {
94+
public void run() {
95+
FunStart4jGUI.createAndShowGUI(jnlpLocation, configuration);
96+
}
97+
});
5998

60-
} catch (URISyntaxException e) {
61-
if (logger.isInfoEnabled())
62-
logger.info("Exception while creating URI", e);
99+
} catch (URISyntaxException e) {
100+
if (logger.isInfoEnabled())
101+
logger.info("Exception while creating URI", e);
102+
}
103+
} else {
104+
logger.error("Loading " + url + " failed!");
63105
}
64106

65107
}

src/main/java/io/github/mainstringargs/funstart4j/JNLPHandler.java

Lines changed: 63 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import java.net.MalformedURLException;
88
import java.net.URI;
99
import java.net.URISyntaxException;
10+
import java.net.URL;
1011
import java.util.ArrayList;
1112
import java.util.LinkedHashMap;
1213
import java.util.List;
@@ -137,7 +138,21 @@ public void parseJNLP() {
137138

138139
Jnlp data = null;
139140
try {
140-
data = (Jnlp) um.unmarshal(jnlpUri.toURL());
141+
URL url = jnlpUri.toURL();
142+
143+
if (url.getProtocol().equalsIgnoreCase("file")) {
144+
try {
145+
File file = new File(url.getPath());
146+
data = (Jnlp) um.unmarshal(file);
147+
} catch (Exception e) {
148+
// TODO Auto-generated catch block
149+
e.printStackTrace();
150+
}
151+
152+
} else {
153+
data = (Jnlp) um.unmarshal(url);
154+
}
155+
141156
} catch (JAXBException | MalformedURLException e) {
142157
if (logger.isInfoEnabled())
143158
logger.info("Exception Unmarshalling", e);
@@ -198,25 +213,40 @@ public void parseJNLP(URI jnlpUri) {
198213
JAXBContext jc = null;
199214
try {
200215
jc = JAXBContext.newInstance(Jnlp.class);
201-
} catch (JAXBException e) {
216+
} catch (Exception e) {
202217
if (logger.isInfoEnabled())
203-
logger.info("JAXBException", e);
218+
logger.info("Exception", e);
204219
}
205220

206221
Unmarshaller um = null;
207222
try {
208223
um = jc.createUnmarshaller();
209-
} catch (JAXBException e) {
224+
} catch (Exception e) {
210225
if (logger.isInfoEnabled())
211-
logger.info("JAXBException", e);
226+
logger.info("Exception", e);
212227
}
213228

214229
Jnlp data = null;
230+
215231
try {
216-
data = (Jnlp) um.unmarshal(jnlpUri.toURL());
217-
} catch (JAXBException | MalformedURLException e) {
232+
233+
if (jnlpUri.toString().toLowerCase().startsWith("file")) {
234+
try {
235+
File file = new File(jnlpUri.getRawPath());
236+
data = (Jnlp) um.unmarshal(file);
237+
} catch (Exception e) {
238+
// TODO Auto-generated catch block
239+
e.printStackTrace();
240+
}
241+
242+
} else {
243+
URL url = jnlpUri.toURL();
244+
data = (Jnlp) um.unmarshal(url);
245+
}
246+
247+
} catch (Exception e) {
218248
if (logger.isInfoEnabled())
219-
logger.info("JAXBException | MalformedURLException", e);
249+
logger.info("Exception", e);
220250
}
221251

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

239269
for (Resources resource : resources) {
240-
logger.info(resource.getArch() + " " + resource.getOs());
270+
logger.info("resources: " + resource.getArch() + " " + resource.getOs());
241271
for (Object libRef : resource.getJavaOrJ2SeOrJarOrNativelibOrExtensionOrPropertyOrPackage()) {
242272

243273
if (libRef instanceof Jar) {
@@ -471,22 +501,25 @@ private String getJavaLocation(String javaHome) {
471501
*/
472502
public static URI getURIReference(String codeBase, String parentRef, String fileName) {
473503
String fullUri = codeBase + "/" + fileName;
474-
475504
if (codeBase.toUpperCase().startsWith("HTTP")) {
476505
// no-op
477506
} else if (fileName.toUpperCase().startsWith("HTTP")) {
478507
fullUri = fileName;
479508
} else if (parentRef.toUpperCase().startsWith("HTTP")) {
480509
fullUri = parentRef + "/" + fileName;
510+
} else if (parentRef.toUpperCase().startsWith("FILE")) {
511+
return new File(parentRef.replaceAll("file://", "") + File.separator + fileName).toURI();
512+
} else if (new File(parentRef).isDirectory()) {
513+
return new File(parentRef + File.separator + fileName).toURI();
481514
}
482515

483516
URI theUri = null;
484517

485518
try {
486519
theUri = new URI(fullUri);
487-
} catch (URISyntaxException e) {
520+
} catch (Exception e) {
488521
if (logger.isInfoEnabled())
489-
logger.info("URISyntaxException", e);
522+
logger.info("Exception", e);
490523
}
491524

492525
return theUri.normalize();
@@ -513,9 +546,25 @@ public static String getFileNameFromUri(URI uri) {
513546
* @return the parent uri
514547
*/
515548
private String getParentUri(URI uri) {
516-
String uriString = uri.toString();
517549

518-
return uriString.substring(0, uriString.lastIndexOf('/') + 1).split("\\?")[0].split("#")[0];
550+
try {
551+
File f = new File(uri);
552+
553+
if (f.isFile()) {
554+
return f.getParent();
555+
556+
} else {
557+
String uriString = uri.toString();
558+
559+
return uriString.substring(0, uriString.lastIndexOf('/') + 1).split("\\?")[0].split("#")[0];
560+
561+
}
562+
} catch (Exception e) {
563+
String uriString = uri.toString();
564+
565+
return uriString.substring(0, uriString.lastIndexOf('/') + 1).split("\\?")[0].split("#")[0];
566+
}
567+
519568
}
520569

521570
}

0 commit comments

Comments
 (0)