PathSearch { public static List<Chunk> split(String s) { int pos = s.indexOf(SLASH); if (pos == -1) { throw new RuntimeException("path did not start with or contain '/'"); } List<Chunk> list = new ArrayList(); int startPos = 0; int searchPos = 0; boolean anyDepth = false; while (pos != -1) { if (pos == 0) { startPos = 1; searchPos = 1; } else if (s.charAt(pos - 1) == '\\') { s = s.substring(0, pos - 1) + s.substring(pos); searchPos = pos; } else { String temp = s.substring(startPos, pos); if (temp.isEmpty()) { anyDepth = true; } else { list.add(new Chunk(anyDepth, temp)); anyDepth = false; } startPos = pos + 1; searchPos = startPos; } pos = s.indexOf(SLASH, searchPos); } if (startPos != s.length()) { String temp = s.substring(startPos); if (!temp.isEmpty()) { list.add(new Chunk(anyDepth, temp)); } } return list; } PathSearch(String path, boolean findAll); static boolean isWildcard(String locator); static List<Chunk> split(String s); @Override String toString(); final boolean findAll; final List<Chunk> chunks; }
@Test public void testPathParsing() { List<PathSearch.Chunk> list = PathSearch.split(" logger.debug("list: {}", list); PathSearch.Chunk first = list.get(0); assertTrue(first.anyDepth); assertEquals("hello", first.controlType); PathSearch.Chunk second = list.get(1); assertFalse(second.anyDepth); assertEquals("world", second.controlType); PathSearch.Chunk third = list.get(2); assertTrue(third.anyDepth); assertEquals("foo", third.controlType); }
@Test public void testPathEdge() { List<PathSearch.Chunk> list = PathSearch.split("/hello logger.debug("list: {}", list); PathSearch.Chunk first = list.get(0); assertFalse(first.anyDepth); assertEquals("hello", first.controlType); PathSearch.Chunk second = list.get(1); assertTrue(second.anyDepth); assertEquals("world", second.controlType); }
@Test public void testIndex() { List<PathSearch.Chunk> list = PathSearch.split("/hello[3] logger.debug("list: {}", list); PathSearch.Chunk first = list.get(0); assertFalse(first.anyDepth); assertEquals("hello", first.controlType); assertEquals(2, first.index); PathSearch.Chunk second = list.get(1); assertTrue(second.anyDepth); assertEquals(-1, second.index); assertEquals("world", second.controlType); }
@Test public void testClassName() { List<PathSearch.Chunk> list = PathSearch.split("/hello[3] logger.debug("list: {}", list); PathSearch.Chunk first = list.get(0); assertFalse(first.anyDepth); assertEquals("hello", first.controlType); assertNull(first.className); assertEquals(2, first.index); PathSearch.Chunk second = list.get(1); assertTrue(second.anyDepth); assertEquals("world", second.controlType); assertEquals("Foo", second.className); PathSearch.Chunk third = list.get(2); assertFalse(third.anyDepth); assertEquals(null, third.controlType); assertEquals("Bar", third.className); }
@Test public void testOnlyName() { List<PathSearch.Chunk> list = PathSearch.split(" logger.debug("list: {}", list); PathSearch.Chunk first = list.get(0); assertTrue(first.anyDepth); assertEquals("foo", first.controlType); assertNull(first.className); assertNull(first.name); assertEquals(-1, first.index); PathSearch.Chunk second = list.get(1); assertTrue(second.anyDepth); assertEquals(null, second.controlType); assertEquals(null, second.className); assertEquals("Bar One", second.name); PathSearch.Chunk third = list.get(2); assertFalse(third.anyDepth); assertEquals(null, third.controlType); assertEquals("Baz", third.name); }
@Test public void testOnlyName2() { List<PathSearch.Chunk> list = PathSearch.split(" logger.debug("list: {}", list); PathSearch.Chunk first = list.get(0); assertTrue(first.anyDepth); assertEquals("listitem", first.controlType); assertNull(first.className); assertNull(first.name); assertEquals(-1, first.index); PathSearch.Chunk second = list.get(1); assertFalse(second.anyDepth); assertEquals(null, second.controlType); assertEquals(null, second.className); assertEquals("Taxpayer Information", second.name); assertNotNull(second.nameCondition); }