private File toJavaFile(ResourcePath directoryPath) { return new File(directoryPath.getFileSystemPathString()); } }
public String relativePath(ResourcePath other, String separator) { List<String> pathComponents = new ArrayList<String>(); ResourcePath pathToTest = other; boolean success = false; while (pathToTest != null) { if (pathToTest.equals(this)) { success = true; break; } pathComponents.add(0, pathToTest.getName()); pathToTest = pathToTest.getParent(); } if (!success || pathComponents.isEmpty()) { return null; } else { return GosuStringUtil.join(pathComponents, separator); } }
/** * Returns the path string for this path using the / path separator. * * @return the path string for this path using the / path separator. * @see #getPathString(String) */ public String getPathString() { return getPathString("/"); }
private void constructPathString(StringBuilder sb, String separator) { if (_parent != null) { _parent.constructPathString(sb, separator); // We don't want to add the separator character after the windows network root; otherwise // we want to add the separator in before adding in our name if (!(_parent instanceof ResourcePathHead && _parent.getName().equals(WINDOWS_NETWORK_ROOT))) { sb.append(separator); } } sb.append(_name); }
/** * Two ResourcePath objects are considered to be equal if they represent the same leaf path and if their parents * are equal. Note that currently the name matching is case-sensitive, even when this is being called on * a case-insensitive file system. * * @param obj the other object * @return true if the objects are equal, false otherwise */ @Override public boolean equals(Object obj) { if (obj instanceof ResourcePath) { ResourcePath otherPath = (ResourcePath) obj; return otherPath.getName().equals(getName()) && GosuObjectUtil.equals(getParent(), otherPath.getParent()); } else { return false; } }
@Override public String getClassNameFromFile( IDirectory root, IFile file, String[] fileExts ) { String strClassPath = root.getPath().getFileSystemPathString() + File.separatorChar; String strQualifiedClassName = file.getPath().getFileSystemPathString().substring( strClassPath.length() ); if( !Util.isClassFileName( strQualifiedClassName, fileExts ) ) { String strExts = ""; for( String strExt : fileExts ) { strExts += " " + strExt; } throw new IllegalArgumentException( file.getPath().getName() + " is not a legal Gosu class name. It does not end with [" + strExts.trim() + "]" ); } strQualifiedClassName = strQualifiedClassName.substring( 0, strQualifiedClassName.lastIndexOf( '.' ) ); strQualifiedClassName = strQualifiedClassName.replace( '/', '.' ).replace( '\\', '.' ); if( strQualifiedClassName.startsWith( "." ) ) { strQualifiedClassName = strQualifiedClassName.substring( 1 ); } return strQualifiedClassName; }
@Override public ResourcePath getPath() { return ResourcePath.parse(_file.getAbsolutePath()); }
DynamicArray<String> components = tokenizePathFragment(otherPath, 0); ResourcePath result = this; for (int i = 0; i < components.size; i++) { result = result.getParent(); if (result == null) { throw new IllegalArgumentException("Joining the path [" + otherPath + "] to the base path [" + getPathString() + "] resulted in traversing backwards past the root path element"); result = new ResourcePath(result, s);
@Override public String getName() { return _path.getName(); }
public String relativePath(ResourcePath other) { return relativePath(other, File.separator); }
public static ResourcePath parse(String pathString) { String rootElement; int lastIndex; if (pathString.startsWith(WINDOWS_NETWORK_ROOT)) { rootElement = WINDOWS_NETWORK_ROOT; lastIndex = 2; } else if (pathString.startsWith("/")) { rootElement = ""; lastIndex = 1; } else { char first = pathString.charAt(0); if (pathString.length() > 1 && pathString.charAt(1) == ':' && Character.isLetter(first)) { rootElement = Character.toUpperCase(first) + ":"; lastIndex = 2; if (pathString.length() > 2 && (pathString.charAt(2) == '/' || pathString.charAt(2) == '\\')) { lastIndex = 3; } } else { throw new IllegalArgumentException("The path string [" + pathString + "] is not an absolute path starting at a drive root"); } } DynamicArray<String> results = tokenizePathFragment(pathString, lastIndex); results = normalizePath(results); return construct(rootElement, results, results.size() - 1); }
private static ResourcePath construct(String headElement, List<String> fullPath, int nameIndex) { if (nameIndex == -1) { if (headElement == null) { return null; } else { return new ResourcePathHead(headElement); } } else { return new ResourcePath(construct(headElement, fullPath, nameIndex - 1), fullPath.get(nameIndex)); } }
@Override public boolean equals(Object obj) { if (obj instanceof IResource) { return _path.equals(((IResource) obj).getPath()); } else { return false; } }
/** * Returns the path string for this path using the specified path separator. The path * constructed will begin with the root of the path, which will be one of: * <ul> * <li>A windows drive letter, normalized to upper case, followed by : and the separator</li> * <li>The windows network path start \\</li> * <li>Just the separator, in the case of the unix root</li> * </ul> * After the path root will follow all path components, separated with the given separator. The * separator will not be appended to the end of the path. Some example paths returned by this * method are: * <ul> * <li>C:\temp\downloads</li> * <li>/</li> * <li>\\files\documents</li> * <ul> * * @param separator the separator to use when constructing the path * @return the path string for this path using the specified path separator */ public String getPathString(String separator) { StringBuilder sb = new StringBuilder(); constructPathString(sb, separator); return sb.toString(); }
@Override public String getClassNameFromFile( IDirectory root, IFile file, String[] fileExts ) { String strClassPath = root.getPath().getFileSystemPathString() + File.separatorChar; String strQualifiedClassName = file.getPath().getFileSystemPathString().substring( strClassPath.length() ); if( !Util.isClassFileName( strQualifiedClassName, fileExts ) ) { String strExts = ""; for( String strExt : fileExts ) { strExts += " " + strExt; } throw new IllegalArgumentException( file.getPath().getName() + " is not a legal Gosu class name. It does not end with [" + strExts.trim() + "]" ); } strQualifiedClassName = strQualifiedClassName.substring( 0, strQualifiedClassName.lastIndexOf( '.' ) ); strQualifiedClassName = strQualifiedClassName.replace( '/', '.' ).replace( '\\', '.' ); if( strQualifiedClassName.startsWith( "." ) ) { strQualifiedClassName = strQualifiedClassName.substring( 1 ); } return strQualifiedClassName; }
@Override public ResourcePath getPath() { return ResourcePath.parse(_file.getAbsolutePath()); }
DynamicArray<String> components = tokenizePathFragment(otherPath, 0); ResourcePath result = this; for (int i = 0; i < components.size; i++) { result = result.getParent(); if (result == null) { throw new IllegalArgumentException("Joining the path [" + otherPath + "] to the base path [" + getPathString() + "] resulted in traversing backwards past the root path element"); result = new ResourcePath(result, s);
@Override public String getName() { return _path.getName(); }
private void constructPathString(StringBuilder sb, String separator) { if (_parent != null) { _parent.constructPathString(sb, separator); // We don't want to add the separator character after the windows network root; otherwise // we want to add the separator in before adding in our name if (!(_parent instanceof ResourcePathHead && _parent.getName().equals(WINDOWS_NETWORK_ROOT))) { sb.append(separator); } } sb.append(_name); }