From 94f4ad0abf5d728194c86364fd12d08305437a62 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Galland?= Date: Wed, 22 Nov 2017 01:17:54 +0100 Subject: [PATCH] [inputoutput] Add PathBuilder and SimplePathBuilder. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Stéphane Galland --- .../afc/inputoutput/path/PathBuilder.java | 204 ++++++++++++++++++ .../inputoutput/path/SimplePathBuilder.java | 135 ++++++++++++ 2 files changed, 339 insertions(+) create mode 100644 core/inputoutput/src/main/java/org/arakhne/afc/inputoutput/path/PathBuilder.java create mode 100644 core/inputoutput/src/main/java/org/arakhne/afc/inputoutput/path/SimplePathBuilder.java diff --git a/core/inputoutput/src/main/java/org/arakhne/afc/inputoutput/path/PathBuilder.java b/core/inputoutput/src/main/java/org/arakhne/afc/inputoutput/path/PathBuilder.java new file mode 100644 index 000000000..9911efb91 --- /dev/null +++ b/core/inputoutput/src/main/java/org/arakhne/afc/inputoutput/path/PathBuilder.java @@ -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. + * + *

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. + * + *

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. + * + *

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. + * + *

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. + * + *

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. + * + *

If the given filename is an URL, the external form of the URL is replied. + * + * @param filename the file. + * @return absolute equivalent name or null 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. + * + *

If the given filename is an URL, the external form of the URL is replied. + * + * @param filename filename. + * @return relative equivalent name or null 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); + } + +} diff --git a/core/inputoutput/src/main/java/org/arakhne/afc/inputoutput/path/SimplePathBuilder.java b/core/inputoutput/src/main/java/org/arakhne/afc/inputoutput/path/SimplePathBuilder.java new file mode 100644 index 000000000..863b11b3b --- /dev/null +++ b/core/inputoutput/src/main/java/org/arakhne/afc/inputoutput/path/SimplePathBuilder.java @@ -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; + } + } + +}