RefParser { public List<RefFactory> parse(final String reference) { final List<RefFactory> sections = new ArrayList<>(); if (reference != null && !reference.isEmpty()) { final char[] chars = reference.toCharArray(); boolean inQuote = false; boolean inArray = false; int pos = 0; if (chars[0] == REF_CHAR || chars[0] == QUOTE) { for (int i = 0; i < chars.length; i++) { final char c = chars[i]; if (c == QUOTE) { inQuote = !inQuote; } else if (!inQuote) { if (c == ARRAY_START) { inArray = true; } else if (inArray) { if (c == ARRAY_END) { inArray = false; } } else if (c == COMPOSITE_DELIMITER) { final String string = reference.substring(pos, i); final RefFactory section = parseSection(reference, string); sections.add(section); pos = i + 1; } } } if (pos < chars.length) { final String string = reference.substring(pos, chars.length); final RefFactory section = parseSection(reference, string); sections.add(section); } } else { sections.add(new TextRefFactory(reference, reference)); } } return sections; }  List<RefFactory> parse(final String reference); static final char REF_CHAR; static final char QUOTE; }
@Test void testSingle() { final RefParser refParser = new RefParser(); List<RefFactory> sections = null; StoreRefFactory refDesc = null; MatchIndex matchIndex = null; sections = refParser.parse(null); assertThat(sections.size()).isEqualTo(0); sections = refParser.parse(""); assertThat(sections.size()).isEqualTo(0); sections = refParser.parse("$heading$"); assertThat(sections.size()).isEqualTo(1); refDesc = (StoreRefFactory) sections.get(0); assertThat(refDesc.getRefId()).isEqualTo("heading"); assertThat(refDesc.getRefGroup()).isEqualTo(0); matchIndex = refDesc.getMatchIndex(); assertThat(matchIndex).isNull(); sections = refParser.parse("$"); assertThat(sections.size()).isEqualTo(1); refDesc = (StoreRefFactory) sections.get(0); assertThat(refDesc.getRefId()).isNull(); assertThat(refDesc.getRefGroup()).isEqualTo(0); matchIndex = refDesc.getMatchIndex(); assertThat(matchIndex).isNull(); sections = refParser.parse("$6"); assertThat(sections.size()).isEqualTo(1); refDesc = (StoreRefFactory) sections.get(0); assertThat(refDesc.getRefId()).isNull(); assertThat(refDesc.getRefGroup()).isEqualTo(6); matchIndex = refDesc.getMatchIndex(); assertThat(matchIndex).isNull(); sections = refParser.parse("$6[2]"); assertThat(sections.size()).isEqualTo(1); refDesc = (StoreRefFactory) sections.get(0); assertThat(refDesc.getRefId()).isNull(); assertThat(refDesc.getRefGroup()).isEqualTo(6); matchIndex = refDesc.getMatchIndex(); assertThat(matchIndex.isOffset()).isEqualTo(false); assertThat(matchIndex.getIndex()).isEqualTo(2); sections = refParser.parse("$6[+2]"); assertThat(sections.size()).isEqualTo(1); refDesc = (StoreRefFactory) sections.get(0); assertThat(refDesc.getRefId()).isNull(); assertThat(refDesc.getRefGroup()).isEqualTo(6); matchIndex = refDesc.getMatchIndex(); assertThat(matchIndex.isOffset()).isEqualTo(true); assertThat(matchIndex.getIndex()).isEqualTo(2); sections = refParser.parse("$6[-2]"); assertThat(sections.size()).isEqualTo(1); refDesc = (StoreRefFactory) sections.get(0); assertThat(refDesc.getRefId()).isNull(); assertThat(refDesc.getRefGroup()).isEqualTo(6); matchIndex = refDesc.getMatchIndex(); assertThat(matchIndex.isOffset()).isEqualTo(true); assertThat(matchIndex.getIndex()).isEqualTo(-2); sections = refParser.parse("$heading$6"); assertThat(sections.size()).isEqualTo(1); refDesc = (StoreRefFactory) sections.get(0); assertThat(refDesc.getRefId()).isEqualTo("heading"); assertThat(refDesc.getRefGroup()).isEqualTo(6); matchIndex = refDesc.getMatchIndex(); assertThat(matchIndex).isNull(); sections = refParser.parse("$heading$6[2]"); assertThat(sections.size()).isEqualTo(1); refDesc = (StoreRefFactory) sections.get(0); assertThat(refDesc.getRefId()).isEqualTo("heading"); assertThat(refDesc.getRefGroup()).isEqualTo(6); matchIndex = refDesc.getMatchIndex(); assertThat(matchIndex.isOffset()).isEqualTo(false); assertThat(matchIndex.getIndex()).isEqualTo(2); sections = refParser.parse("$heading$6[+2]"); assertThat(sections.size()).isEqualTo(1); refDesc = (StoreRefFactory) sections.get(0); assertThat(refDesc.getRefId()).isEqualTo("heading"); assertThat(refDesc.getRefGroup()).isEqualTo(6); matchIndex = refDesc.getMatchIndex(); assertThat(matchIndex.isOffset()).isEqualTo(true); assertThat(matchIndex.getIndex()).isEqualTo(2); sections = refParser.parse("$heading$6[-2]"); assertThat(sections.size()).isEqualTo(1); refDesc = (StoreRefFactory) sections.get(0); assertThat(refDesc.getRefId()).isEqualTo("heading"); assertThat(refDesc.getRefGroup()).isEqualTo(6); matchIndex = refDesc.getMatchIndex(); assertThat(matchIndex.isOffset()).isEqualTo(true); assertThat(matchIndex.getIndex()).isEqualTo(-2); }
@Test void testComposite() { StoreRefFactory refDesc = null; MatchIndex matchIndex = null; TextRefFactory textDesc = null; final RefParser refParser = new RefParser(); final List<RefFactory> sections = refParser.parse("$heading$1[+3]+'test'+'2'+$heading2$+$3+$heading3$[5]"); assertThat(sections.size()).isEqualTo(6); refDesc = (StoreRefFactory) sections.get(0); assertThat(refDesc.getRefId()).isEqualTo("heading"); assertThat(refDesc.getRefGroup()).isEqualTo(1); matchIndex = refDesc.getMatchIndex(); assertThat(matchIndex.isOffset()).isEqualTo(true); assertThat(matchIndex.getIndex()).isEqualTo(3); textDesc = (TextRefFactory) sections.get(1); assertThat(textDesc.getText()).isEqualTo("test"); textDesc = (TextRefFactory) sections.get(2); assertThat(textDesc.getText()).isEqualTo("2"); refDesc = (StoreRefFactory) sections.get(3); assertThat(refDesc.getRefId()).isEqualTo("heading2"); assertThat(refDesc.getRefGroup()).isEqualTo(0); matchIndex = refDesc.getMatchIndex(); assertThat(matchIndex).isNull(); refDesc = (StoreRefFactory) sections.get(4); assertThat(refDesc.getRefId()).isNull(); assertThat(refDesc.getRefGroup()).isEqualTo(3); matchIndex = refDesc.getMatchIndex(); assertThat(matchIndex).isNull(); refDesc = (StoreRefFactory) sections.get(5); assertThat(refDesc.getRefId()).isEqualTo("heading3"); assertThat(refDesc.getRefGroup()).isEqualTo(0); matchIndex = refDesc.getMatchIndex(); assertThat(matchIndex.isOffset()).isEqualTo(false); assertThat(matchIndex.getIndex()).isEqualTo(5); }
@Test void testFixed() { TextRefFactory textDesc = null; final RefParser refParser = new RefParser(); final List<RefFactory> sections = refParser.parse("some heading"); assertThat(sections.size()).isEqualTo(1); textDesc = (TextRefFactory) sections.get(0); assertThat(textDesc.getText()).isEqualTo("some heading"); }
@Test void testFixedWithQuotes() { TextRefFactory textDesc = null; final RefParser refParser = new RefParser(); final List<RefFactory> sections = refParser.parse("'some ''other'' heading'"); assertThat(sections.size()).isEqualTo(1); textDesc = (TextRefFactory) sections.get(0); assertThat(textDesc.getText()).isEqualTo("some 'other' heading"); }