@Override public MutableDocumentImpl<Node, Element, Text> create(String tagName, Map<String, String> attributes) { IndexedDocument<Node, Element, Text> doc = POJO.create(tagName, attributes); return new MutableDocumentImpl<Node, Element, Text>(createTrivialSequencer(doc), doc); }
/** * Test set attribute overrides and removes old attributes, as opposed to * update. */ public void testSetAttributes() { init("<p t=\"0\" s=\"hi\">hello</p>"); Element e = (Element) doc.getFirstChild(doc.getDocumentElement()); doc.setElementAttributes(e, new AttributesImpl("just", "this")); assertEquals(null, doc.getAttribute(e, "t")); assertEquals(null, doc.getAttribute(e, "s")); assertEquals("this", doc.getAttribute(e, "just")); }
/** Test a simple set and reset annotation */ public void testSetAndResetAnnotation() { init("<p>abcdef</p>"); doc.setAnnotation(0, doc.size(), "style/color", "pato"); doc.resetAnnotation(3, 6, "style/color", "pato"); assertOperationResult( "<p>ab<?a \"style/color\"=\"pato\"?>cde<?a \"style/color\"?>f</p>"); }
@Override public void insertText(int location, String text) { Preconditions.checkPositionIndex(location, size()); // TODO(danilatos): Get the schema constraints from the document // and use the corresponding permitted characters from there. // text = getPermittedCharactersForPoint(point).convertString(text); try { begin(); consume(insertText(text, at(location))); } finally { end(); } }
@Override public void setElementAttributes(E element, Attributes attrs) { Preconditions.checkArgument(element != getDocumentElement(), "Cannot touch root element"); try { begin(); consume(setAttributes(attrs, at(Locator.before(doc, element)))); } finally { end(); } }
@Override public void updateElementAttributes(E element, Map<String, String> attrs) { Preconditions.checkArgument(element != getDocumentElement(), "Cannot touch root element"); try { begin(); consume(updateAttributes(attrs, at(Locator.before(doc, element)))); } finally { end(); } }
/** Test a simple set and unset of an annotation */ public void testSetAndUnsetAnnotation() { init("<p>abcdef</p>"); doc.setAnnotation(3, 6, "style/color", "maisy"); doc.setAnnotation(0, doc.size(), "style/color", null); assertOperationResult("<p>abcdef</p>"); }
/** * Test basic get attribute. */ public void testGetAttributes() { init("<p t=\"0\" s=\"hi\">hello</p>"); Element e = (Element) doc.getFirstChild(doc.getDocumentElement()); assertEquals("0", doc.getAttribute(e, "t")); assertEquals("hi", doc.getAttribute(e, "s")); }
@Override public void setElementAttribute(E element, String name, String value) { String currentValue = getAttribute(element, name); if ((value == null && currentValue == null) || (value != null && value.equals(currentValue))) { // Redundant. Do nothing, no operation. return; } try { begin(); consume(setAttribute(name, value, at(Locator.before(doc, element)))); } finally { end(); } }
public void testMoveNodes() throws Exception { Element root = doc.getDocumentElement().getFirstChild().asElement(); Node from = root.getLastChild(); doc.moveSiblings(Point.start(doc, root), from, null); assertOperationResult("<root><from/><before/></root>"); root = doc.getDocumentElement().getFirstChild().asElement(); Node stuff = root.getFirstChild().getNextSibling(); from = stuff.getNextSibling(); doc.moveSiblings(Point.before(doc, stuff), from, root.getLastChild()); assertOperationResult( "<root><before/><from> child <sub/></from> more <attr x=\"x\" y=\"z\"/> stuff end</root>"); doc.setAnnotation(1, 9, "b", "B"); // around the bs doc.setAnnotation(4, 8, "i", "I"); // around the is doc.setAnnotation(0, 3, "s", "S"); // overlaps the start doc.setAnnotation(7, 12, "e", "E"); // overlaps the end, AND covers the new range root = doc.getDocumentElement().getFirstChild().asElement(); doc.moveSiblings(Point.end((Node) root), root.getFirstChild(), root.getLastChild()); assertOperationResult("<?a \"s\"=\"S\"?><root><?a \"e\"=\"E\" \"s\"?><after/>" + "<?a \"b\"=\"B\" \"e\" \"s\"=\"S\"?><b>b<?a \"s\"?>o<?a \"i\"=\"I\"?><i>ld"
/** * Tests that createChildElement does as it says. */ public void testCreateChildElement() { init("<p>first child</p>"); Element root = doc.getDocumentElement(); doc.createChildElement(root, "child", Collections.<String, String> emptyMap()); assertOperationResult("<p>first child</p><child/>"); }
/** * Test that trying to reset an annotation with an end bigger than the document * an IndexOutOfBoundsException. */ public void testResetAnnotationPastDocEndThrowsException() throws Exception { init("<p>abcdef</p>"); try { doc.resetAnnotation(1, doc.size() + 1, "style/color", "frub"); // Doh - no exception thrown. Fail the test assert false; } catch (IndexOutOfBoundsException iae) { // expected } }
annos.add(new RangedValue<String>(2, 4, "cyril")); annos.add(new RangedValue<String>(5, 6, "tallulah")); doc.resetAnnotationsInRange(0, doc.size(), "style/color", annos); assertOperationResult( "<p>a<?a \"style/color\"=\"cyril\"?>bc<?a \"style/color\"?>d" +
@Override public Range deleteRange(int start, int end) { Preconditions.checkPositionIndexes(start, end, size()); // TODO(davidbyttow/danilatos): Handle this more efficiently. PointRange<N> range = deleteRange(doc.locate(start), doc.locate(end)); return new Range(doc.getLocation(range.getFirst()), doc.getLocation(range.getSecond())); }
public void testDeleteRangeIndices() { String str = "123<b>asdf</b>34<x/>5"; init(str); for (int i = 0; i <= 14; i++) { assertCollapsedAt(i, doc.deleteRange(i, i)); assertResult(str); } // delete start or end tag does nothing assertRangeAt(3, 4, doc.deleteRange(3, 4)); assertResult(str); assertRangeAt(8, 9, doc.deleteRange(8, 9)); assertResult(str); // text only init("12345678"); assertCollapsedAt(1, doc.deleteRange(1, 3)); // middle assertResult("145678"); assertCollapsedAt(0, doc.deleteRange(0, 1)); // start assertResult("45678"); assertCollapsedAt(3, doc.deleteRange(3, 5)); // end assertResult("456"); assertCollapsedAt(0, doc.deleteRange(0, 3)); // all assertResult(""); }
deleteRange(removeStart, removeEnd); try { atomicInsert = Composer.compose(domOp.finish(), annotOp.finish()); hackConsume(Nindo.fromDocOp(atomicInsert, true)); } catch (OperationException e) {
/** * Test that trying to reset an annotation with a negative start throws * an IndexOutOfBoundsException. */ public void testNegativeStartResetAnnotationThrowsException() throws Exception { init("<p>abcdef</p>"); try { doc.resetAnnotation(-1, 4, "style/color", "frub"); // Doh - no exception thrown. Fail the test assert false; } catch (IndexOutOfBoundsException iae) { // expected } }
protected Point<Node> l(int location) { return doc.locate(location); }
/** Test a simple set and unset of an annotation */ public void testSetAndUnsetAnnotation() { init("<p>abcdef</p>"); doc.setAnnotation(3, 6, "style/color", "maisy"); doc.setAnnotation(0, doc.size(), "style/color", null); assertOperationResult("<p>abcdef</p>"); }
/** * Test basic get attribute. */ public void testGetAttributes() { init("<p t=\"0\" s=\"hi\">hello</p>"); Element e = (Element) doc.getFirstChild(doc.getDocumentElement()); assertEquals("0", doc.getAttribute(e, "t")); assertEquals("hi", doc.getAttribute(e, "s")); }