-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[inputoutput] Add PathBuilder and SimplePathBuilder.
Signed-off-by: Stéphane Galland <galland@arakhne.org>
- Loading branch information
1 parent
37673a5
commit 94f4ad0
Showing
2 changed files
with
339 additions
and
0 deletions.
There are no files selected for viewing
204 changes: 204 additions & 0 deletions
204
core/inputoutput/src/main/java/org/arakhne/afc/inputoutput/path/PathBuilder.java
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,204 @@ | ||
/* | ||
* $Id$ | ||
* This file is a part of the Arakhne Foundation Classes, http://www.arakhne.org/afc | ||
* | ||
* Copyright (c) 2000-2012 Stephane GALLAND. | ||
* Copyright (c) 2005-10, Multiagent Team, Laboratoire Systemes et Transports, | ||
* Universite de Technologie de Belfort-Montbeliard. | ||
* Copyright (c) 2013-2018 The original authors, and other authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.arakhne.afc.inputoutput.path; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
import java.net.URL; | ||
|
||
import org.eclipse.xtext.xbase.lib.Inline; | ||
import org.eclipse.xtext.xbase.lib.Pure; | ||
|
||
import org.arakhne.afc.vmutil.FileSystem; | ||
|
||
/** Interface that permits to build absolute paths from relative paths | ||
* and relative paths from absolute paths. | ||
* | ||
* <p>This interface supports both {@link File} and {@link URL}. | ||
* | ||
* @author $Author: sgalland$ | ||
* @version $FullVersion$ | ||
* @mavengroupid $GroupId$ | ||
* @mavenartifactid $ArtifactId$ | ||
* @since 15.0 | ||
*/ | ||
public interface PathBuilder { | ||
|
||
/** Set the default/current directory used to make absolute the reltive paths. | ||
* | ||
* <p>This function tries to build an {@link URL}, and if it fails it assumed that the given | ||
* string is a filename to pass to {@link File}. | ||
* | ||
* @param currentDirectory is the default/current directory used to make absolute the reltive paths. | ||
*/ | ||
void setCurrentDirectory(String currentDirectory); | ||
|
||
/** Set the default/current directory used to make absolute the reltive paths. | ||
* | ||
* @param currentDirectory is the default/current directory used to make absolute the reltive paths. | ||
*/ | ||
void setCurrentDirectory(File currentDirectory); | ||
|
||
/** Set the default/current directory used to make absolute the reltive paths. | ||
* | ||
* @param currentDirectory is the default/current directory used to make absolute the reltive paths. | ||
*/ | ||
void setCurrentDirectory(URL currentDirectory); | ||
|
||
/** Replies the default/current directory used to make absolute the reltive paths. | ||
* | ||
* <p>This function replies the external form of the value replied by | ||
* {@link #getCurrentDirectoryURL()} | ||
* | ||
* @return the default/current directory used to make absolute the reltive paths. | ||
* @see #getCurrentDirectoryFile() | ||
* @see #getCurrentDirectoryURL() | ||
*/ | ||
@Pure | ||
@Inline(value = "getCurrentDirectoryURL().toExternalForm()") | ||
default String getCurrentDirectoryString() { | ||
return getCurrentDirectoryURL().toExternalForm(); | ||
} | ||
|
||
/** Replies the default/current directory used to make absolute the reltive paths. | ||
* | ||
* <p>If the current directory is a {@link File}, it is replied. | ||
* If the current directory is an {@link URL} with "file" protocol, its path is replied. | ||
* In all other cases the user home directory is replied, or the default directory if | ||
* the user home is unavailable. | ||
* | ||
* @return the default/current directory used to make absolute the reltive paths. | ||
* @see #getCurrentDirectoryString() | ||
* @see #getCurrentDirectoryURL() | ||
*/ | ||
@Pure | ||
File getCurrentDirectoryFile(); | ||
|
||
/** Replies the default/current directory used to make absolute the reltive paths. | ||
* | ||
* <p>This function replies the URL of the current directory even if the current | ||
* directory was set with a {@link File}. | ||
* | ||
* @return the default/current directory used to make absolute the reltive paths. | ||
* @see #getCurrentDirectoryFile() | ||
* @see #getCurrentDirectoryString() | ||
*/ | ||
@Pure | ||
URL getCurrentDirectoryURL(); | ||
|
||
/** Make the given filename absolute. | ||
* | ||
* @param filename the file. | ||
* @return absolute equivalent name. | ||
*/ | ||
@Pure | ||
default URL makeAbsolute(File filename) { | ||
return FileSystem.makeAbsolute(filename, getCurrentDirectoryURL()); | ||
} | ||
|
||
/** Make the given filename absolute. | ||
* | ||
* @param filename the file. | ||
* @return absolute equivalent name. | ||
*/ | ||
@Pure | ||
default URL makeAbsolute(URL filename) { | ||
if (filename == null) { | ||
return null; | ||
} | ||
return FileSystem.makeAbsolute(filename, getCurrentDirectoryURL()); | ||
} | ||
|
||
/** Make the given filename absolute. | ||
* | ||
* <p>If the given filename is an URL, the external form of the URL is replied. | ||
* | ||
* @param filename the file. | ||
* @return absolute equivalent name or <code>null</code> if it is impossible to obtain an URL. | ||
*/ | ||
@Pure | ||
default URL makeAbsolute(String filename) { | ||
if (filename == null || filename.length() == 0) { | ||
return null; | ||
} | ||
final URL url = FileSystem.convertStringToURL(filename, true, false); | ||
if (url != null) { | ||
return makeAbsolute(url); | ||
} | ||
return makeAbsolute(new File(filename)); | ||
} | ||
|
||
/** Make the given filename relative. | ||
* | ||
* @param filename the file. | ||
* @return relative equivalent name. | ||
* @throws IOException in case of error. | ||
*/ | ||
@Pure | ||
default File makeRelative(File filename) throws IOException { | ||
return FileSystem.makeRelative(filename, getCurrentDirectoryURL()); | ||
} | ||
|
||
/** Make the given filename relative. | ||
* | ||
* @param filename the file. | ||
* @return relative equivalent name. | ||
* @throws IOException in case of error. | ||
*/ | ||
@Pure | ||
default File makeRelative(URL filename) throws IOException { | ||
if (filename == null) { | ||
return null; | ||
} | ||
return FileSystem.makeRelative(filename, getCurrentDirectoryURL()); | ||
} | ||
|
||
/** Make the given filename relative. | ||
* | ||
* <p>If the given filename is an URL, the external form of the URL is replied. | ||
* | ||
* @param filename filename. | ||
* @return relative equivalent name or <code>null</code> if it is impossible to obtain an File. | ||
* @throws IOException in case of error. | ||
*/ | ||
@Pure | ||
default File makeRelative(String filename) throws IOException { | ||
if (filename == null || filename.length() == 0) { | ||
return null; | ||
} | ||
final URL url = FileSystem.convertStringToURL(filename, true, false); | ||
if (url != null) { | ||
return makeRelative(url); | ||
} | ||
return makeRelative(new File(filename)); | ||
} | ||
|
||
/** Try to make the given URL shorter in its form. | ||
* | ||
* @param url the URL to convert. | ||
* @return the shorter URL or the url itself. | ||
*/ | ||
@Pure | ||
default URL toShorterURL(URL url) { | ||
return FileSystem.toShortestURL(url); | ||
} | ||
|
||
} |
135 changes: 135 additions & 0 deletions
135
core/inputoutput/src/main/java/org/arakhne/afc/inputoutput/path/SimplePathBuilder.java
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,135 @@ | ||
/* | ||
* $Id$ | ||
* This file is a part of the Arakhne Foundation Classes, http://www.arakhne.org/afc | ||
* | ||
* Copyright (c) 2000-2012 Stephane GALLAND. | ||
* Copyright (c) 2005-10, Multiagent Team, Laboratoire Systemes et Transports, | ||
* Universite de Technologie de Belfort-Montbeliard. | ||
* Copyright (c) 2013-2018 The original authors, and other authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.arakhne.afc.inputoutput.path; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
import java.net.MalformedURLException; | ||
import java.net.URL; | ||
|
||
import org.arakhne.afc.vmutil.FileSystem; | ||
import org.arakhne.afc.vmutil.URISchemeType; | ||
|
||
/** Simple implementation of a tool that permits to convert filenames from | ||
* absolute to relative and the opposite.. | ||
* | ||
* @author $Author: sgalland$ | ||
* @version $FullVersion$ | ||
* @mavengroupid $GroupId$ | ||
* @mavenartifactid $ArtifactId$ | ||
* @since 15.0 | ||
*/ | ||
public class SimplePathBuilder implements PathBuilder { | ||
|
||
private File currentFile; | ||
|
||
private URL currentURL; | ||
|
||
/** Constructor. | ||
*/ | ||
public SimplePathBuilder() { | ||
// | ||
} | ||
|
||
/** Constructor with current directory. | ||
* @param currentDirectory is the default/current directory used to make absolute the reltive paths. | ||
*/ | ||
public SimplePathBuilder(String currentDirectory) { | ||
setCurrentDirectory(currentDirectory); | ||
} | ||
|
||
/** Constructor with current directory. | ||
* @param currentDirectory is the default/current directory used to make absolute the reltive paths. | ||
*/ | ||
public SimplePathBuilder(File currentDirectory) { | ||
setCurrentDirectory(currentDirectory); | ||
} | ||
|
||
/** Constructor with current directory. | ||
* @param currentDirectory is the default/current directory used to make absolute the reltive paths. | ||
*/ | ||
public SimplePathBuilder(URL currentDirectory) { | ||
setCurrentDirectory(currentDirectory); | ||
} | ||
|
||
@Override | ||
public void setCurrentDirectory(String currentDirectory) { | ||
if (currentDirectory == null) { | ||
this.currentFile = null; | ||
this.currentURL = null; | ||
} else { | ||
try { | ||
this.currentURL = new URL(currentDirectory); | ||
this.currentFile = null; | ||
} catch (MalformedURLException exception) { | ||
this.currentFile = new File(currentDirectory); | ||
this.currentURL = null; | ||
} | ||
} | ||
} | ||
|
||
@Override | ||
public void setCurrentDirectory(File currentDirectory) { | ||
this.currentFile = currentDirectory; | ||
this.currentURL = null; | ||
} | ||
|
||
@Override | ||
public void setCurrentDirectory(URL currentDirectory) { | ||
this.currentURL = currentDirectory; | ||
this.currentFile = null; | ||
} | ||
|
||
@Override | ||
public File getCurrentDirectoryFile() { | ||
if (this.currentFile != null) { | ||
return this.currentFile; | ||
} | ||
if (URISchemeType.FILE.isURL(this.currentURL)) { | ||
return new File(this.currentURL.getPath()); | ||
} | ||
try { | ||
return FileSystem.getUserHomeDirectory(); | ||
} catch (IOException exception) { | ||
return new File((String) null); | ||
} | ||
} | ||
|
||
@Override | ||
public URL getCurrentDirectoryURL() { | ||
try { | ||
if (this.currentFile != null) { | ||
return this.currentFile.toURI().toURL(); | ||
} | ||
if (this.currentURL != null) { | ||
return this.currentURL; | ||
} | ||
try { | ||
return FileSystem.getUserHomeDirectory().toURI().toURL(); | ||
} catch (IOException exception) { | ||
return new File((String) null).toURI().toURL(); | ||
} | ||
} catch (MalformedURLException exception) { | ||
return null; | ||
} | ||
} | ||
|
||
} |