public void testCrawlUp() throws Exception { String text = "Look Pa, some more!"; ModifiableTraversableSource src = (ModifiableTraversableSource) resolver.resolveURI("jcr://path/to/very/deep/content"); write(src, text); // Do a fresh lookup src = (ModifiableTraversableSource) resolver.resolveURI("jcr://path/to/very/deep/content"); ModifiableTraversableSource parent = (ModifiableTraversableSource) src.getParent(); assertTrue(parent.exists()); assertEquals("jcr://path/to/very/deep", parent.getURI()); parent = (ModifiableTraversableSource) parent.getParent(); assertTrue(parent.exists()); assertEquals("jcr://path/to/very", parent.getURI()); parent = (ModifiableTraversableSource) parent.getParent(); assertTrue(parent.exists()); assertEquals("jcr://path/to", parent.getURI()); parent = (ModifiableTraversableSource) parent.getParent(); assertTrue(parent.exists()); assertEquals("jcr://path", parent.getURI()); parent = (ModifiableTraversableSource) parent.getParent(); assertTrue(parent.exists()); assertEquals("jcr://", parent.getURI()); // Root node has no parent parent = (ModifiableTraversableSource) parent.getParent(); assertNull(parent); }
public static void copy(ModifiableTraversableSource from, ModifiableTraversableSource to) throws IOException { if (!from.exists()) { throw new IOException("Cannot find source file/folder"); } if (from.isCollection()) { to.makeCollection(); Collection contents; try { contents = from.getChildren(); } catch (SourceException se) { throw new RuntimeException("Unable to list contents for collection " + from); } for (Iterator iter = contents.iterator(); iter.hasNext(); ) { ModifiableTraversableSource src = (ModifiableTraversableSource) iter.next(); SourceUtil.copy(src, resolve(to.getURI() + "/" + src.getName())); } } else { to = (ModifiableTraversableSource) resolve(to.getURI()); InputStream in = null; OutputStream out = null; try { in = from.getInputStream(); out = to.getOutputStream(); copy(in, out); } finally { if (out != null) out.close(); if (in != null) in.close(); } } }