/** * Get the relative path between two virtual files * * @param parent the parent * @param child the child * @return the relative path */ static String getRelativePath(VirtualFile parent, VirtualFile child) { if (child == null) { throw new IllegalArgumentException("Null child"); } String childPath = child.getPathName(); if (parent != null) { String parentPath = parent.getPathName(); if (parentPath.length() == childPath.length()) { return ""; } // Not sure about this? It is obviously not a direct child if it is shorter? if (parentPath.length() < childPath.length()) { if (!parentPath.endsWith("/")) { parentPath = parentPath + "/"; } if (childPath.startsWith(parentPath)) { return childPath.substring(parentPath.length()); } } } if (childPath.endsWith("/")) { childPath = childPath.substring(0, childPath.length() - 1); } return childPath; }
/** * Get the vfs file name safely * * @param root the virutal file * @return the name */ static final String safeVirtualFileName(VirtualFile root) { if (root == null) throw new IllegalArgumentException("Null root"); try { return root.toURI().toString(); } catch (Exception e) { return root.getName(); } }
public void visit(VirtualFile vf) { try { if (isMapping(vf)) urls.add(vf.toURL()); } catch (Exception e) { throw new RuntimeException("Visit failed: " + e); } }
@Override protected void handleURL(URL url, final Set<String> classes, final Set<URL> urls) { try { final VirtualFile archive = VFS.getRoot(url.toURI()); archive.visit( new VirtualFileVisitor() { public VisitorAttributes getAttributes() { return VisitorAttributes.RECURSE_LEAVES_ONLY; } public void visit(VirtualFile vf) { try { String name = getRelativePath(archive, vf); URL url = vf.toURL(); handle(name, url, classes, urls); } catch (Exception e) { throw new RuntimeException(e); } } }); } catch (Exception e) { throw new RuntimeException("Error handling url " + url, e); } }
protected boolean isSame(VirtualFile original) throws Exception { return original.isLeaf(); }
/** * Is virtual file a mapping file. * * @param vf the virtual file * @return true if virtual file is mapping */ protected boolean isMapping(VirtualFile vf) { return vf.getName().indexOf(".hbm.xml") > 0; }