@Override public int size() { return getDelegate().size(); }
/** * @param doc * @return true if the document's title is explicitly set */ public static boolean hasExplicitTitle(MutableDocument<?, ?, ?> doc) { int start = doc.firstAnnotationChange(0, doc.size(), TITLE_KEY, null); if (start != -1 && !doc.getAnnotation(start, TITLE_KEY).isEmpty()) { return true; } else { return false; } }
/** * Set an explicit title on the document, independent of the document's text * content * * @param doc * @param title */ public static void setExplicitTitle(MutableDocument<?, ?, ?> doc, String title) { // TODO(danilatos): Automatically insert content when it's an empty document? // Perhaps a special title tag? Preconditions.checkArgument(doc.size() > 0, "Cannot set title on empty document"); doc.resetAnnotation(0, 1, TITLE_KEY, title); }
private Point<Node> l(int location) { if (location < 0) { location = doc.size() + location; } return doc.locate(location); }
private Point<Node> l(int location) { if (location < 0) { location = doc.size() + location; } return doc.locate(location); }
boolean afterPunctuation = false; int start = -1, end = doc.size() -1; E firstLineReached = null;
/** Check that a collapsed range over the document behaves correctly. */ public void testEntireCoverRanges() { String initialContent = "abc"; TestDocumentContext<Node, Element, Text> test = ContextProviders.createTestPojoContext( DocProviders.POJO.parse(initialContent).asOperation(), null, null, null, DocumentSchema.NO_SCHEMA_CONSTRAINTS); MutableDocument<Node, Element, Text> doc = test.document(); RangeTracker tracker = new RangeTracker(test.localAnnotations()); // entire range through mutation at middle, end, start tracker.trackRange(new Range(0, doc.size())); assertEquals(new Range(0, doc.size()), tracker.getRange()); doc.insertText(doc.locate(1), "a"); assertEquals(new Range(0, doc.size()), tracker.getRange()); doc.insertText(doc.locate(doc.size()), "b"); assertEquals(new Range(0, doc.size()), tracker.getRange()); doc.insertText(doc.locate(0), "c"); assertEquals(new Range(1, doc.size()), tracker.getRange()); }
/** Check that a collapsed range over the document behaves correctly. */ public void testEntireCoverRanges() { String initialContent = "abc"; TestDocumentContext<Node, Element, Text> test = ContextProviders.createTestPojoContext( DocProviders.POJO.parse(initialContent).asOperation(), null, null, null, DocumentSchema.NO_SCHEMA_CONSTRAINTS); MutableDocument<Node, Element, Text> doc = test.document(); RangeTracker tracker = new RangeTracker(test.localAnnotations()); // entire range through mutation at middle, end, start tracker.trackRange(new Range(0, doc.size())); assertEquals(new Range(0, doc.size()), tracker.getRange()); doc.insertText(doc.locate(1), "a"); assertEquals(new Range(0, doc.size()), tracker.getRange()); doc.insertText(doc.locate(doc.size()), "b"); assertEquals(new Range(0, doc.size()), tracker.getRange()); doc.insertText(doc.locate(0), "c"); assertEquals(new Range(1, doc.size()), tracker.getRange()); }
/** Check that a collapsed range at doc.size() behaves correctly. */ public void testCollapsedEndRange() { String initialContent = "abc"; TestDocumentContext<Node, Element, Text> test = ContextProviders.createTestPojoContext( DocProviders.POJO.parse(initialContent).asOperation(), null, null, null, DocumentSchema.NO_SCHEMA_CONSTRAINTS); MutableDocument<Node, Element, Text> doc = test.document(); RangeTracker tracker = new RangeTracker(test.localAnnotations()); int end = doc.size(); // collapsed at the start, through mutation: tracker.trackRange(new Range(end, end)); assertEquals(new Range(end, end), tracker.getRange()); doc.insertText(doc.locate(3), "a"); assertEquals(new Range(end + 1, end + 1), tracker.getRange()); doc.insertText(doc.locate(doc.size()), "x"); assertEquals(new Range(end + 2, end + 2), tracker.getRange()); }
/** Check that a collapsed range at doc.size() behaves correctly. */ public void testCollapsedEndRange() { String initialContent = "abc"; TestDocumentContext<Node, Element, Text> test = ContextProviders.createTestPojoContext( DocProviders.POJO.parse(initialContent).asOperation(), null, null, null, DocumentSchema.NO_SCHEMA_CONSTRAINTS); MutableDocument<Node, Element, Text> doc = test.document(); RangeTracker tracker = new RangeTracker(test.localAnnotations()); int end = doc.size(); // collapsed at the start, through mutation: tracker.trackRange(new Range(end, end)); assertEquals(new Range(end, end), tracker.getRange()); doc.insertText(doc.locate(3), "a"); assertEquals(new Range(end + 1, end + 1), tracker.getRange()); doc.insertText(doc.locate(doc.size()), "x"); assertEquals(new Range(end + 2, end + 2), tracker.getRange()); }
assertFalse(Annotations.guardedResetAnnotation(doc, 3, doc.size(), "a", null)); assertFalse(Annotations.guardedResetAnnotation(doc, 0, doc.size(), "a", null)); assertFalse(check.wasChanged()); doc.setAnnotation(0, doc.size(), "a", "b"); check.reset(); assertFalse(Annotations.guardedResetAnnotation(doc, 0, doc.size(), "a", "b")); assertFalse(check.wasChanged()); doc.setAnnotation(0, doc.size(), "a", null); check.reset(); assertTrue(Annotations.guardedResetAnnotation(doc, 3, 6, "a", "b"));
assertFalse(Annotations.guardedResetAnnotation(doc, 3, doc.size(), "a", null)); assertFalse(Annotations.guardedResetAnnotation(doc, 0, doc.size(), "a", null)); assertFalse(check.wasChanged()); doc.setAnnotation(0, doc.size(), "a", "b"); check.reset(); assertFalse(Annotations.guardedResetAnnotation(doc, 0, doc.size(), "a", "b")); assertFalse(check.wasChanged()); doc.setAnnotation(0, doc.size(), "a", null); check.reset(); assertTrue(Annotations.guardedResetAnnotation(doc, 3, 6, "a", "b"));
public void testFindingImplicitTitleClearsFromInvalidDocuments() { getDoc("<body><line/>Some text.<line/>Some more text</body>"); TitleHelper.maybeFindAndSetImplicitTitle(doc); assertEquals("Some text.", TitleHelper.extractTitle(doc)); doc.emptyElement(doc.getDocumentElement()); doc.appendXml(XmlStringBuilder.createText("Blah. Blah").wrap("x")); TitleHelper.maybeFindAndSetImplicitTitle(doc); assertEquals("", TitleHelper.extractTitle(doc)); assertFalse(TitleHelper.hasExplicitTitle(doc)); assertEquals(-1, doc.firstAnnotationChange(0, doc.size(), TitleHelper.TITLE_KEY, null)); }
public void testFindingImplicitTitleClearsFromInvalidDocuments() { getDoc("<body><line/>Some text.<line/>Some more text</body>"); TitleHelper.maybeFindAndSetImplicitTitle(doc); assertEquals("Some text.", TitleHelper.extractTitle(doc)); doc.emptyElement(doc.getDocumentElement()); doc.appendXml(XmlStringBuilder.createText("Blah. Blah").wrap("x")); TitleHelper.maybeFindAndSetImplicitTitle(doc); assertEquals("", TitleHelper.extractTitle(doc)); assertFalse(TitleHelper.hasExplicitTitle(doc)); assertEquals(-1, doc.firstAnnotationChange(0, doc.size(), TitleHelper.TITLE_KEY, null)); }
doc.deleteRange(l(0), l(doc.size())); checkEvents(cd(0, 10, dl(st("p", NA), tt("abc"), st("b", NA), tt("def"), et("b"), et("p")))); elementsOuter(doc, DocHelper.getLastChildElement(doc, doc.getDocumentElement()))); doc.deleteRange(l(0), l(doc.size())); checkEvents( cd(0, 10, dl(st("p", NA), tt("abc"), st("b", NA), tt("def"), et("b"), et("p"))), doc.deleteRange(l(0), l(doc.size())); checkEvents(td(0, txt)); deletions.addAll( elementsOuter(doc, DocHelper.getLastChildElement(doc, doc.getDocumentElement()))); doc.deleteRange(l(0), l(doc.size())); checkEvents( cd(0, 10, dl(st("p", NA), tt("abc"), st("b", NA), tt("def"), et("b"), et("p"))),
doc.deleteRange(l(0), l(doc.size())); checkEvents(cd(0, 10, dl(st("p", NA), tt("abc"), st("b", NA), tt("def"), et("b"), et("p")))); elementsOuter(doc, DocHelper.getLastChildElement(doc, doc.getDocumentElement()))); doc.deleteRange(l(0), l(doc.size())); checkEvents( cd(0, 10, dl(st("p", NA), tt("abc"), st("b", NA), tt("def"), et("b"), et("p"))), doc.deleteRange(l(0), l(doc.size())); checkEvents(td(0, txt)); deletions.addAll( elementsOuter(doc, DocHelper.getLastChildElement(doc, doc.getDocumentElement()))); doc.deleteRange(l(0), l(doc.size())); checkEvents( cd(0, 10, dl(st("p", NA), tt("abc"), st("b", NA), tt("def"), et("b"), et("p"))),
/** Test whether left/right alignment of annotation retrieval is working. */ public void testGetAlignedAnnotation() { // setup: ab[cdef]gh MutableDocument<Node, Element, Text> doc = ContextProviders.createTestPojoContext( "abcdefgh", null, null, null, DocumentSchema.NO_SCHEMA_CONSTRAINTS).document(); doc.setAnnotation(2, 6, "X", "Y"); // check left-align at start, middle, and end and after of non-null annotation assertEquals(Annotations.getAlignedAnnotation(doc, 2, "X", true), null); assertEquals(Annotations.getAlignedAnnotation(doc, 4, "X", true), "Y"); assertEquals(Annotations.getAlignedAnnotation(doc, 6, "X", true), "Y"); assertEquals(Annotations.getAlignedAnnotation(doc, 8, "X", true), null); // check right-align at start, middle, and end of non-null annotation assertEquals(Annotations.getAlignedAnnotation(doc, 0, "X", false), null); assertEquals(Annotations.getAlignedAnnotation(doc, 2, "X", false), "Y"); assertEquals(Annotations.getAlignedAnnotation(doc, 4, "X", false), "Y"); assertEquals(Annotations.getAlignedAnnotation(doc, 6, "X", false), null); // Boundary cases - left aligning at start, right aligning at end assertNull(Annotations.getAlignedAnnotation(doc, 0, "X", true)); assertNull(Annotations.getAlignedAnnotation(doc, doc.size(), "X", false)); }
/** Test whether left/right alignment of annotation retrieval is working. */ public void testGetAlignedAnnotation() { // setup: ab[cdef]gh MutableDocument<Node, Element, Text> doc = ContextProviders.createTestPojoContext( "abcdefgh", null, null, null, DocumentSchema.NO_SCHEMA_CONSTRAINTS).document(); doc.setAnnotation(2, 6, "X", "Y"); // check left-align at start, middle, and end and after of non-null annotation assertEquals(Annotations.getAlignedAnnotation(doc, 2, "X", true), null); assertEquals(Annotations.getAlignedAnnotation(doc, 4, "X", true), "Y"); assertEquals(Annotations.getAlignedAnnotation(doc, 6, "X", true), "Y"); assertEquals(Annotations.getAlignedAnnotation(doc, 8, "X", true), null); // check right-align at start, middle, and end of non-null annotation assertEquals(Annotations.getAlignedAnnotation(doc, 0, "X", false), null); assertEquals(Annotations.getAlignedAnnotation(doc, 2, "X", false), "Y"); assertEquals(Annotations.getAlignedAnnotation(doc, 4, "X", false), "Y"); assertEquals(Annotations.getAlignedAnnotation(doc, 6, "X", false), null); // Boundary cases - left aligning at start, right aligning at end assertNull(Annotations.getAlignedAnnotation(doc, 0, "X", true)); assertNull(Annotations.getAlignedAnnotation(doc, doc.size(), "X", false)); }
/***/ public void testAnnotationBoundaryHelpers() { MutableDocument<Node, Element, Text> doc = ContextProviders.createTestPojoContext( "abcdefgh", null, null, null, DocumentSchema.NO_SCHEMA_CONSTRAINTS).document(); int s = doc.size(); assertEquals(s, Annotations.firstAnnotationBoundary(doc, 0, s, "a", null)); assertEquals(0, Annotations.lastAnnotationBoundary(doc, 0, s, "a", null)); assertEquals(s - 1, Annotations.firstAnnotationBoundary(doc, 1, s - 1, "a", null)); assertEquals(1, Annotations.lastAnnotationBoundary(doc, 1, s - 1, "a", null)); assertEquals(0, Annotations.firstAnnotationBoundary(doc, 0, s, "a", "x")); assertEquals(s, Annotations.lastAnnotationBoundary(doc, 0, s, "a", "x")); assertEquals(1, Annotations.firstAnnotationBoundary(doc, 1, s - 1, "a", "")); assertEquals(s - 1, Annotations.lastAnnotationBoundary(doc, 1, s - 1, "a", "")); doc.setAnnotation(2, 3, "a", ""); doc.setAnnotation(4, 5, "a", ""); assertEquals(2, Annotations.firstAnnotationBoundary(doc, 0, s, "a", null)); assertEquals(5, Annotations.lastAnnotationBoundary(doc, 0, s, "a", null)); assertEquals(2, Annotations.firstAnnotationBoundary(doc, 1, s - 1, "a", null)); assertEquals(5, Annotations.lastAnnotationBoundary(doc, 1, s - 1, "a", null)); assertEquals(0, Annotations.firstAnnotationBoundary(doc, 0, s, "a", "x")); assertEquals(s, Annotations.lastAnnotationBoundary(doc, 0, s, "a", "x")); assertEquals(1, Annotations.firstAnnotationBoundary(doc, 1, s - 1, "a", "y")); assertEquals(s - 1, Annotations.lastAnnotationBoundary(doc, 1, s - 1, "a", "y")); assertEquals(0, Annotations.firstAnnotationBoundary(doc, 0, s, "a", "")); assertEquals(s, Annotations.lastAnnotationBoundary(doc, 0, s, "a", "")); assertEquals(1, Annotations.firstAnnotationBoundary(doc, 1, s - 1, "a", "")); assertEquals(s - 1, Annotations.lastAnnotationBoundary(doc, 1, s - 1, "a", "")); }
/***/ public void testAnnotationBoundaryHelpers() { MutableDocument<Node, Element, Text> doc = ContextProviders.createTestPojoContext( "abcdefgh", null, null, null, DocumentSchema.NO_SCHEMA_CONSTRAINTS).document(); int s = doc.size(); assertEquals(s, Annotations.firstAnnotationBoundary(doc, 0, s, "a", null)); assertEquals(0, Annotations.lastAnnotationBoundary(doc, 0, s, "a", null)); assertEquals(s - 1, Annotations.firstAnnotationBoundary(doc, 1, s - 1, "a", null)); assertEquals(1, Annotations.lastAnnotationBoundary(doc, 1, s - 1, "a", null)); assertEquals(0, Annotations.firstAnnotationBoundary(doc, 0, s, "a", "x")); assertEquals(s, Annotations.lastAnnotationBoundary(doc, 0, s, "a", "x")); assertEquals(1, Annotations.firstAnnotationBoundary(doc, 1, s - 1, "a", "")); assertEquals(s - 1, Annotations.lastAnnotationBoundary(doc, 1, s - 1, "a", "")); doc.setAnnotation(2, 3, "a", ""); doc.setAnnotation(4, 5, "a", ""); assertEquals(2, Annotations.firstAnnotationBoundary(doc, 0, s, "a", null)); assertEquals(5, Annotations.lastAnnotationBoundary(doc, 0, s, "a", null)); assertEquals(2, Annotations.firstAnnotationBoundary(doc, 1, s - 1, "a", null)); assertEquals(5, Annotations.lastAnnotationBoundary(doc, 1, s - 1, "a", null)); assertEquals(0, Annotations.firstAnnotationBoundary(doc, 0, s, "a", "x")); assertEquals(s, Annotations.lastAnnotationBoundary(doc, 0, s, "a", "x")); assertEquals(1, Annotations.firstAnnotationBoundary(doc, 1, s - 1, "a", "y")); assertEquals(s - 1, Annotations.lastAnnotationBoundary(doc, 1, s - 1, "a", "y")); assertEquals(0, Annotations.firstAnnotationBoundary(doc, 0, s, "a", "")); assertEquals(s, Annotations.lastAnnotationBoundary(doc, 0, s, "a", "")); assertEquals(1, Annotations.firstAnnotationBoundary(doc, 1, s - 1, "a", "")); assertEquals(s - 1, Annotations.lastAnnotationBoundary(doc, 1, s - 1, "a", "")); }