[Nice-commit] Nice/src/nice/tools/locator path.nice,NONE,1.1 main.nice,NONE,1.1 locator.nice,NONE,1.
Brought to you by:
bonniot
From: Daniel B. <bo...@us...> - 2004-07-02 19:31:13
|
Update of /cvsroot/nice/Nice/src/nice/tools/locator In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv14796/src/nice/tools/locator Added Files: path.nice main.nice locator.nice Log Message: Abstracted the resource location in pathes in the nice.tools.locator package. --- NEW FILE: main.nice --- /**************************************************************************/ /* N I C E */ /* A high-level object-oriented research language */ /* (c) Daniel Bonniot 2004 */ /* */ /* This program is free software; you can redistribute it and/or modify */ /* it under the terms of the GNU General Public License as published by */ /* the Free Software Foundation; either version 2 of the License, or */ /* (at your option) any later version. */ /* */ /**************************************************************************/ package nice.tools.locator; import java.net.*; /** A locator finds resources based on given path. @author Daniel Bonniot (bo...@us...) */ public class Locator { final URL[] path; ?URLConnection get(String resource); toString() = this.getClass.getName + ": " Arrays.asList(path).toString(); } new Locator(String path, String->void warning) = this(path: parsePath(path, warning)); --- NEW FILE: locator.nice --- /**************************************************************************/ /* N I C E */ /* A high-level object-oriented research language */ /* (c) Daniel Bonniot 2004 */ /* */ /* This program is free software; you can redistribute it and/or modify */ /* it under the terms of the GNU General Public License as published by */ /* the Free Software Foundation; either version 2 of the License, or */ /* (at your option) any later version. */ /* */ /**************************************************************************/ package nice.tools.locator; /** Implementation of the locator. @author Daniel Bonniot (bo...@us...) */ get(Locator this, String resource) { for (URL base : this.path) { try { let url = new URL(base, resource); let connection = url.openConnection(); connection.connect(); return connection; } catch(java.io.IOException e) {} } return null; } --- NEW FILE: path.nice --- /**************************************************************************/ /* N I C E */ /* A high-level object-oriented research language */ /* (c) Daniel Bonniot 2004 */ /* */ /* This program is free software; you can redistribute it and/or modify */ /* it under the terms of the GNU General Public License as published by */ /* the Free Software Foundation; either version 2 of the License, or */ /* (at your option) any later version. */ /* */ /**************************************************************************/ package nice.tools.locator; /** Handle string representations of a path. @author Daniel Bonniot (bo...@us...) */ public URL[] parsePath(String path, String->void warning) { List<URL> components = new ArrayList(); int start = 0; // skip starting separators while (start < path.length() && path.charAt(start) == java.io.File.pathSeparatorChar) start++; while(start < path.length()) { int end = path.indexOf(java.io.File.pathSeparatorChar, start); if (end == -1) end = path.length(); String pathComponent = path.substring(start, end); if (pathComponent.length() > 0) try{ java.io.File f = nice.tools.util.System.getFile(pathComponent); if (f.canRead()) components.add(url(f.getCanonicalFile())); else { if (!f.exists()) warning("Path component "pathComponent" does not exist"); else warning("Path component "pathComponent" is not readable"); } } catch(java.net.MalformedURLException e){ warning("Path component "pathComponent" is invalid"); } catch(java.io.IOException e){ warning("Path component "pathComponent" is invalid"); } start = end + 1; } return components.toArray(); } URL url(java.io.File f) { String path = f.getPath(); if (path.endsWith(".jar")) return new URL("jar", "", "file:" path "!/"); else return f.toURL(); } |