-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
51 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package develop; | ||
|
||
import java.io.File; | ||
import java.net.MalformedURLException; | ||
import java.net.URI; | ||
import java.net.URISyntaxException; | ||
|
||
public class UriParser | ||
{ | ||
public static void main( String[] args ) throws URISyntaxException, MalformedURLException | ||
{ | ||
parseUri( new URI( "file:/Volumes/test" ) ); | ||
parseUri( new URI( "/Volumes/test" ) ); | ||
parseUri( new URI( "https://fiji.sc" ) ); | ||
parseUri( new URI("C:/test") ); | ||
} | ||
|
||
public static void parseUri( URI uri ) throws MalformedURLException, URISyntaxException | ||
{ | ||
if ( uri.getScheme() == null ) | ||
{ | ||
System.out.println("--"); | ||
System.out.println("Missing schema, treating as file." ); | ||
treatAsFile( new URI( "file:" + uri ) ); | ||
} | ||
else if ( uri.getScheme().contains( "file" ) ) | ||
{ | ||
System.out.println("--"); | ||
treatAsFile( uri ); | ||
} | ||
else if ( uri.getScheme().contains( "http" ) ) | ||
{ | ||
System.out.println("--"); | ||
String address = uri.toURL().toString(); | ||
System.out.println("http address: " + address); | ||
} | ||
else | ||
{ | ||
System.out.println("--"); | ||
System.out.println("Unknown schema: " + uri.getScheme() + ", treating as File." ); | ||
treatAsFile( new URI( "file:" + uri ) ); | ||
} | ||
} | ||
|
||
private static void treatAsFile( URI uri ) throws MalformedURLException | ||
{ | ||
String path = uri.toURL().getPath(); | ||
File file = new File( path ); | ||
System.out.println("File path: " + file.getAbsolutePath() ); | ||
} | ||
} |