SpecialTableMarker { public String insertInTable() throws SpecialTableNotFoundException { int stack = 0; int characterIndex = document.indexOf(controlWord); if (characterIndex == -1) { throw new SpecialTableNotFoundException(); } int tableIndex = 0; char character = document.charAt(characterIndex); while (character != '}' || stack > 0) { if (character == '{') { stack++; } if (character == '}') { stack--; } if (character == ';') { tableIndex++; } characterIndex++; character = document.charAt(characterIndex); } return document.substring(0, characterIndex) + insertionString + " " + tableIndex + document .substring(characterIndex); } SpecialTableMarker(String document, String insertionString, String controlWord); String insertInTable();  }
@Test public void testInsertInTable() throws Exception { String document = "{\\atbl;1;2;3;}"; SpecialTableMarker specialTableMarker = new SpecialTableMarker(document, "\\u2222221B", "\\atbl"); assertEquals(specialTableMarker.insertInTable(), "{\\atbl;1;2;3;\\u2222221B 4}"); }