NamePathMapperImpl implements NamePathMapper { @Override public String getOakPath(String jcrPath) { if (!needsFullMapping(jcrPath)) { return jcrPath; } int length = jcrPath.length(); if (length > 0 && jcrPath.charAt(0) == '[') { if (jcrPath.charAt(length - 1) != ']') { log.debug("Could not parse path " + jcrPath + ": unterminated identifier"); return null; } if (this.idManager == null) { log.debug("Could not parse path " + jcrPath + ": could not resolve identifier"); return null; } return this.idManager.getPath(jcrPath.substring(1, length - 1)); } final StringBuilder parseErrors = new StringBuilder(); PathListener listener = new PathListener() { @Override public void error(String message) { parseErrors.append(message); } @Override public boolean name(String name, int index) { if (index < 0) { error("invalid index: " + index); return false; } String oakName = nameMapper.getOakNameOrNull(name); if (oakName == null) { error("Invalid name: " + name); return false; } if (index > 1) { oakName += "[" + index + ']'; } elements.add(oakName); return true; } }; JcrPathParser.parse(jcrPath, listener); if (parseErrors.length() != 0) { log.debug("Could not parse path " + jcrPath + ": " + parseErrors.toString()); return null; } if (listener.elements.isEmpty()) { return ""; } StringBuilder oakPath = new StringBuilder(); for (String element : listener.elements) { if (element.isEmpty()) { oakPath.append('/'); } else { oakPath.append(element); oakPath.append('/'); } } oakPath.deleteCharAt(oakPath.length() - 1); return oakPath.toString(); } NamePathMapperImpl(NameMapper nameMapper);  NamePathMapperImpl(NameMapper nameMapper, IdentifierManager idManager); @Override String getOakNameOrNull(@NotNull String jcrName); @NotNull @Override String getOakName(@NotNull String jcrName); @NotNull @Override String getJcrName(@NotNull String oakName); @Override @NotNull Map<String, String> getSessionLocalMappings(); @Override String getOakPath(String jcrPath); @Override @NotNull String getJcrPath(final String oakPath);  }
@Test public void testInvalidIdentifierPath() { String uuid = UUIDUtils.generateUUID(); List<String> invalid = new ArrayList<String>(); invalid.add('[' + uuid + "]abc"); invalid.add('[' + uuid + "]/a/b/c"); for (String jcrPath : invalid) { assertNull(npMapper.getOakPath(jcrPath)); } }
@Test public void testTrailingSlash() { assertEquals("/oak-foo:bar/oak-quu:qux",npMapper.getOakPath("/foo:bar/quu:qux/")); assertEquals("/a/b/c",npMapper.getOakPath("/a/b/c/")); }
@Test public void testJcrToOak() { assertEquals("/", npMapper.getOakPath("/")); assertEquals("foo", npMapper.getOakPath("{}foo")); assertEquals("/oak-foo:bar", npMapper.getOakPath("/foo:bar")); assertEquals("/oak-foo:bar/oak-quu:qux", npMapper.getOakPath("/foo:bar/quu:qux")); assertEquals("oak-foo:bar", npMapper.getOakPath("foo:bar")); assertEquals("oak-nt:unstructured", npMapper.getOakPath("{http: assertEquals("foobar/oak-jcr:content", npMapper.getOakPath("foobar/{http: assertEquals("foobar", npMapper.getOakPath("foobar/{http: assertEquals("", npMapper.getOakPath("foobar/{http: assertEquals("..", npMapper.getOakPath("foobar/{http: assertEquals("../..", npMapper.getOakPath("foobar/{http: assertEquals("oak-jcr:content", npMapper.getOakPath("foobar/../{http: assertEquals("../oak-jcr:content", npMapper.getOakPath("foobar/../../{http: assertEquals("..", npMapper.getOakPath("..")); assertEquals("", npMapper.getOakPath(".")); assertEquals("foobar/oak-jcr:content", npMapper.getOakPath("foobar/{http: assertEquals("foobar/oak-jcr:content", npMapper.getOakPath("foobar/{http: assertEquals("foobar/oak-jcr:content", npMapper.getOakPath("foobar/./{http: assertEquals("oak-jcr:content", npMapper.getOakPath("foobar/./../{http: assertEquals("/a/b/c", npMapper.getOakPath("/a/b[1]/c[01]")); assertEquals("/a/b[2]/c[3]", npMapper.getOakPath("/a[1]/b[2]/c[03]")); assertEquals("/a/b", npMapper.getOakPath("/a/b/a/..")); }
@Test public void testJcrToOakKeepIndexNoRemap() { NameMapper mapper = new GlobalNameMapper(GLOBAL); NamePathMapper npMapper = new NamePathMapperImpl(mapper); assertEquals("/", npMapper.getOakPath("/")); assertEquals("/foo:bar", npMapper.getOakPath("/foo:bar")); assertEquals("/foo:bar/quu:qux", npMapper.getOakPath("/foo:bar/quu:qux")); assertEquals("foo:bar", npMapper.getOakPath("foo:bar")); }
@Test public void testInvalidJcrPaths() { String[] paths = { " "/foo "/.. "/..", "/foo/../..", "foo::bar", "foo:bar:baz", "foo:bar]baz", "foo:bar[baz", "foo:bar|baz", "foo:bar*baz" }; NamePathMapper[] mappers = { npMapper, new NamePathMapperImpl(new LocalNameMapper( GLOBAL, Collections.<String, String>emptyMap())) }; for (NamePathMapper mapper : mappers) { for (String path : paths) { assertNull(path, mapper.getOakPath(path)); } } }
@Test public void testBracketsInPaths() throws Exception { String[] paths = { "/parent/childB1", "/parent/}childB2", "/parent/{childB3}", "/parent/sub/childB4", "/parent/sub/}childB5", "/parent/sub/{childB6}", }; for (String path : paths) { assertEquals(path, npMapper.getOakPath(path)); } }
@Test public void testIllegalBracketsInPaths() throws Exception { String[] paths = { "/parent/sub/{childB7", "/parent/sub/{childB7", "/parent/{", "/parent/{childA1", "/parent/{{childA2" }; for (String path : paths) { assertNull(npMapper.getOakPath(path)); } }
@Test public void testWhitespace() { String[] paths = new String[] { " leading", "trailing\n", " ", "\t", "oak: leading", "oak:trailing\n", "oak: ", "oak:\t" }; NamePathMapper noLocal = new NamePathMapperImpl(new LocalNameMapper( GLOBAL, Collections.<String, String>emptyMap())); for (String path : paths) { assertEquals("without local mappings", path, noLocal.getOakPath(path)); assertEquals("with local mappings", path, npMapper.getOakPath(path)); } }