/// <summary>List directory contents for a resource folder.</summary> /// <remarks>List directory contents for a resource folder. Not recursive.</remarks> /// <author>Andrew Reslan</author> /// <param name="clazz">Any java class that lives in the same place as the resources folder /// </param> /// <param name="path">Should end with "/", but not start with one.</param> /// <returns>An array of the name of each member item, or null if path does not denote a directory /// </returns> /// <exception cref="Sharpen.URISyntaxException">Sharpen.URISyntaxException</exception> /// <exception cref="System.IO.IOException">System.IO.IOException</exception> public static string[] GetResourceListing(Type clazz, string path) { Uri dirURL = clazz.GetClassLoader().GetResource(path); if (dirURL != null && dirURL.Scheme.Equals("file")) { return new FilePath(dirURL.ToURI()).List(); } if (dirURL != null && dirURL.Scheme.Equals("jar")) { string jarPath = Sharpen.Runtime.Substring(dirURL.AbsolutePath, 5, dirURL.AbsolutePath .IndexOf("!")); JarFile jar = new JarFile(URLDecoder.Decode(jarPath, "UTF-8")); Enumeration<JarEntry> entries = ((Enumeration<JarEntry>)jar.Entries()); ICollection<string> result = new HashSet<string>(); while (entries.MoveNext()) { string name = entries.Current.GetName(); if (name.StartsWith(path)) { string entry = Sharpen.Runtime.Substring(name, path.Length); int checkSubdir = entry.IndexOf("/"); if (checkSubdir >= 0) { // if it is a subdirectory, we just return the directory name entry = Sharpen.Runtime.Substring(entry, 0, checkSubdir); } result.AddItem(entry); } } return Sharpen.Collections.ToArray(result, new string[result.Count]); } throw new NotSupportedException("Cannot list files for URL " + dirURL); }
private static Type[] GetVisibleClasses() { //--Variables IList <Type> classes = new List <Type>(); // (get classpath) string pathSep = Runtime.GetProperty("path.separator"); string[] cp = Runtime.GetProperties().GetProperty("java.class.path", null).Split(pathSep); // --Fill Options // (get classes) foreach (string entry in cp) { Redwood.Util.Log("Checking cp " + entry); //(should skip?) if (entry.Equals(".") || entry.Trim().IsEmpty()) { continue; } //(no, don't skip) File f = new File(entry); if (f.IsDirectory()) { // --Case: Files try { using (IDirectoryStream <IPath> stream = Files.NewDirectoryStream(f.ToPath(), "*.class")) { foreach (IPath p in stream) { //(get the associated class) Type clazz = FilePathToClass(entry, p.ToString()); if (clazz != null) { //(add the class if it's valid) classes.Add(clazz); } } } } catch (IOException ioe) { Redwood.Util.Error(ioe); } } else { //noinspection StatementWithEmptyBody if (!IsIgnored(entry)) { // --Case: Jar try { using (JarFile jar = new JarFile(f)) { IEnumeration <JarEntry> e = ((IEnumeration <JarEntry>)jar.Entries()); while (e.MoveNext()) { //(for each jar file element) JarEntry jarEntry = e.Current; string clazz = jarEntry.GetName(); if (clazz.Matches(".*class$")) { //(if it's a class) clazz = Sharpen.Runtime.Substring(clazz, 0, clazz.Length - 6).ReplaceAll("/", "."); //(add it) try { classes.Add(Sharpen.Runtime.GetType(clazz, false, ClassLoader.GetSystemClassLoader())); } catch (TypeLoadException) { Redwood.Util.Warn("Could not load class in jar: " + f + " at path: " + clazz); } catch (NoClassDefFoundError) { Redwood.Util.Debug("Could not scan class: " + clazz + " (in jar: " + f + ')'); } } } } } catch (IOException) { Redwood.Util.Warn("Could not open jar file: " + f + "(are you sure the file exists?)"); } } } } //case: ignored jar return(Sharpen.Collections.ToArray(classes, new Type[classes.Count])); }