XmlDocumentProcessor { public XmlDocumentProcessor(Set<String> deletableSpanTags, Map<String, Set<String>> tagsWithAtts, Set<String> singletonTags, boolean throwExceptionOnUnrecognizedTag) { this.deletableSpanTags = deletableSpanTags; this.tagsWithAtts = tagsWithAtts; this.singletonTags = singletonTags; this.throwExceptionOnUnrecognizedTag = throwExceptionOnUnrecognizedTag; } XmlDocumentProcessor(Set<String> deletableSpanTags, Map<String, Set<String>> tagsWithAtts,
                                Set<String> singletonTags, boolean throwExceptionOnUnrecognizedTag); static Map<IntPair, Set<String>> compileAttributeValues(List<SpanInfo> xmlMarkup); static Map<IntPair, SpanInfo> compileOffsetSpanMapping(List<SpanInfo> retainedTagInfo); Pair<StringTransformation, List<SpanInfo>> processXml(String xmlText); static final String SPAN_INFO; }
@Test public void testXmlDocumentProcessor() { Map<String, Set<String>> tagsWithAtts = new HashMap<>(); Set<String> attributeNames = new HashSet<>(); attributeNames.add("author"); attributeNames.add("id"); attributeNames.add("datetime"); tagsWithAtts.put("post", attributeNames); attributeNames = new HashSet<>(); attributeNames.add("id"); tagsWithAtts.put("doc", attributeNames); Set<String> deletableSpanTags = new HashSet<>(); deletableSpanTags.add("quote"); deletableSpanTags.add("distraction"); Set<String> tagsToIgnore = new HashSet<>(); tagsToIgnore.add("img"); tagsToIgnore.add("snip"); boolean throwExceptionOnXmlTagMiss = true; XmlDocumentProcessor proc = new XmlDocumentProcessor(deletableSpanTags, tagsWithAtts, tagsToIgnore, throwExceptionOnXmlTagMiss); Pair<StringTransformation, List<XmlDocumentProcessor.SpanInfo>> nt = proc.processXml(ORIG_TEXT); StringTransformation st = nt.getFirst(); List<XmlDocumentProcessor.SpanInfo> retainedTagInfo = nt.getSecond(); String cleanText = st.getTransformedText(); assertEquals(ORIG_TEXT, st.getOrigText()); assertEquals(CLEAN_TEXT, cleanText); Map<IntPair, XmlDocumentProcessor.SpanInfo> offsetToSpans = XmlDocumentProcessor.compileOffsetSpanMapping(retainedTagInfo); assertTrue(offsetToSpans.containsKey(POST_OFFSETS)); XmlDocumentProcessor.SpanInfo spanInfo = offsetToSpans.get(POST_OFFSETS); assertTrue(spanInfo.attributes.containsKey(AUTHOR)); assertEquals(NAME, spanInfo.attributes.get(AUTHOR).getFirst()); assertEquals(AUTHOR_OFFSETS, spanInfo.attributes.get(AUTHOR).getSecond()); String origAuthStr = st.getOrigText().substring(AUTHOR_OFFSETS.getFirst(), AUTHOR_OFFSETS.getSecond()); assertEquals(NAME, origAuthStr); assertTrue(offsetToSpans.containsKey(DISTR_OFFSETS)); spanInfo = offsetToSpans.get(DISTR_OFFSETS); assertTrue(spanInfo.label.equals("distraction")); assertEquals(DISTR_SUBSTR, ORIG_TEXT.substring(DISTR_OFFSETS.getFirst(), DISTR_OFFSETS.getSecond())); assertTrue(offsetToSpans.containsKey(IQ_OFFSETS)); int iqStart = st.computeModifiedOffsetFromOriginal(IQ_OFFSETS.getFirst()); int iqEnd = st.computeModifiedOffsetFromOriginal(IQ_OFFSETS.getSecond()); assertEquals("", cleanText.substring(iqStart, iqEnd)); assertEquals(ORIG_TEXT.indexOf("Whassup"), IQ_OFFSETS.getFirst()); int doStart = cleanText.indexOf("do?"); int doEnd = doStart + 3; IntPair origYouOffsets = st.getOriginalOffsets(doStart, doEnd); assertEquals("do?", ORIG_TEXT.substring(origYouOffsets.getFirst(), origYouOffsets.getSecond())); }