/** * {@inheritDoc} * * @see Rule#getPathInRepository(org.modeshape.graph.property.Path, org.modeshape.graph.property.PathFactory) */ @Override public Path getPathInRepository( Path pathInSource, PathFactory factory ) { assert pathInSource.equals(pathInSource.getCanonicalPath()); // Check whether the source path is included by this rule ... if (!includes(pathInSource)) return null; // Project the repository path into the equivalent source path ... return projectPathInSourceToPathInRepository(pathInSource, factory); }
/** * {@inheritDoc} * <p> * This method considers a path that is at or below the rule's {@link #getPathInSource() source path} to be included, * except if there are {@link #getExceptionsToRule() exceptions} that explicitly disallow the path. * </p> * * @see Rule#getPathInSource(Path, PathFactory) */ @Override public Path getPathInSource( Path pathInRepository, PathFactory factory ) { assert pathInRepository.equals(pathInRepository.getCanonicalPath()); // Project the repository path into the equivalent source path ... Path pathInSource = projectPathInRepositoryToPathInSource(pathInRepository, factory); // Check whether the source path is included by this rule ... return includes(pathInSource) ? pathInSource : null; }
/** * Get the paths in the source that correspond to the supplied path within the repository. This method computes the paths * given all of the rules. In general, most sources will probably project a node onto a single repository node. However, some * sources may be configured such that the same node in the repository is a projection of multiple nodes within the source. * * @param canonicalPathInRepository the canonical path of the node within the repository; may not be null * @param factory the path factory; may not be null * @return the set of unique paths in the source projected from the repository path; never null * @throws IllegalArgumentException if the factory reference is null */ public Set<Path> getPathsInSource( Path canonicalPathInRepository, PathFactory factory ) { CheckArg.isNotNull(factory, "factory"); assert canonicalPathInRepository == null ? true : canonicalPathInRepository.equals(canonicalPathInRepository.getCanonicalPath()); Set<Path> paths = new HashSet<Path>(); for (Rule rule : getRules()) { Path pathInSource = rule.getPathInSource(canonicalPathInRepository, factory); if (pathInSource != null) paths.add(pathInSource); } return paths; }
/** * Get the paths in the repository that correspond to the supplied path within the source. This method computes the paths * given all of the rules. In general, most sources will probably project a node onto a single repository node. However, some * sources may be configured such that the same node in the source is projected into multiple nodes within the repository. * * @param canonicalPathInSource the canonical path of the node within the source; may not be null * @param factory the path factory; may not be null * @return the set of unique paths in the repository projected from the source path; never null * @throws IllegalArgumentException if the factory reference is null */ public Set<Path> getPathsInRepository( Path canonicalPathInSource, PathFactory factory ) { CheckArg.isNotNull(factory, "factory"); assert canonicalPathInSource == null ? true : canonicalPathInSource.equals(canonicalPathInSource.getCanonicalPath()); Set<Path> paths = new HashSet<Path>(); for (Rule rule : getRules()) { Path pathInRepository = rule.getPathInRepository(canonicalPathInSource, factory); if (pathInRepository != null) paths.add(pathInRepository); } return paths; }
@Test( expected = InvalidPathException.class ) public void shouldNotComputeCanonicalPathOfNodeThatIsNotAbsolute() { pathFactory.create("a/b/c/../d/./e/../..").getCanonicalPath(); }
@Test( expected = InvalidPathException.class ) public void shouldNotComputeCanonicalPathOfNodeThatIsNotAbsolute() { pathFactory.create("a/b/c/../d/./e/../..").getCanonicalPath(); }
@Test public void shouldReturnSelfForGetCanonicalPath() { assertThat(root.getCanonicalPath(), is(sameInstance(root))); }
@Test public void shouldReturnNormalizedPathForTheCanonicalPathOfAbsolutePath() { path = pathFactory.create("/a/b/c/../d/./e/../.."); assertThat(path.isNormalized(), is(false)); assertThat(path.isAbsolute(), is(true)); assertThat(path.getCanonicalPath(), hasSegments(pathFactory, "a", "b")); assertThat(path.getCanonicalPath().isAbsolute(), is(true)); assertThat(path.getCanonicalPath().isNormalized(), is(true)); }
@Test public void shouldReturnNormalizedPathForTheCanonicalPathOfAbsolutePath() { path = pathFactory.create("/a/b/c/../d/./e/../.."); assertThat(path.isNormalized(), is(false)); assertThat(path.isAbsolute(), is(true)); assertThat(path.getCanonicalPath(), hasSegments(pathFactory, "a", "b")); assertThat(path.getCanonicalPath().isAbsolute(), is(true)); assertThat(path.getCanonicalPath().isNormalized(), is(true)); }