| View previous topic :: View next topic |
| Author |
Message |
zaphod Power User


Joined: 04 Nov 2004 Posts: 51 Location: Magdeburg, Germany
|
Posted: Wed Feb 08, 2006 3:43 am Post subject: URL to path in Java |
|
|
When I try to get a directory or file with the UNO API, I get a String in the format of an UNO URL (e.g. with the code at the bottom). But what I want is the (absolute)path and not the Uno URL. In Basic there are the functions ConvertToURL(<path>), which converts the path like "C:\User abc\bla.sxw" to the appropriate "file:///..." and ConvertFromURL(<url>) to transforms it back again. In CPP there is the class osl::FileBase as the appropriate counterpart.
Is it possible to get the same functionality with the Java API?
| Code: | Object obj = getServiceManager().createInstanceWithContext("com.sun.star.util.PathSettings", context);
XPropertySet xPropertySet = (XPropertySet)UnoRuntime.queryInterface(XPropertySet.class, obj);
String sPath = ((String) xPropertySet.getPropertyValue("UserConfig"));
//
//sPath Linux.....: file:///home/arthur/.openoffice.org2/user/config
//sPath Windows: file:///C:/Dokumente%20und%20Einstellungen/pc/Anwendungsdaten/OpenOffice.org2/user/config
|
|
|
| Back to top |
|
 |
bolekovnik General User

Joined: 24 Jan 2006 Posts: 20
|
Posted: Fri Feb 10, 2006 9:08 am Post subject: |
|
|
Hi!
Maybe this piece of code can bring you on the right way:
| Code: |
private static String APPLICATION_DIRECTORY = "file:///"+System.getProperty( "user.dir" ).replace( '\\', '/' );
|
Cheers! |
|
| Back to top |
|
 |
Lew Wadoo General User

Joined: 05 Jul 2009 Posts: 7 Location: Russia, Moscow
|
Posted: Tue Jun 19, 2012 10:36 pm Post subject: Re: URL to path in Java |
|
|
| zaphod wrote: | When I try to get a directory or file with the UNO API, I get a String in the format of an UNO URL (e.g. with the code at the bottom). But what I want is the (absolute)path and not the Uno URL. In Basic there are the functions ConvertToURL(<path>), which converts the path like "C:\User abc\bla.sxw" to the appropriate "file:///..." and ConvertFromURL(<url>) to transforms it back again. In CPP there is the class osl::FileBase as the appropriate counterpart.
Is it possible to get the same functionality with the Java API?
|
The method getPath() returns the decoded path of the URI.
Try this code:
| Code: |
public String decodePath(URI uriFile) {
return uriFile.getPath();
}
public URI convertStringToUri(String sURI) {
URI uriFile = null;
try {
uriFile = new URI(sURI);
} catch (URISyntaxException e) {
e.printStackTrace();
}
return uriFile;
}
private void test() {
URI uriFile = convertStringToUri("file:///C:/Dokumente%20und%20Einstellungen/pc/Anwendungsdaten/OpenOffice.org2/user/config");
String sDecodedPath = decodePath(uriFile);
}
|
|
|
| Back to top |
|
 |
|