@Override public DocInitialization asOperation() { return getDocument().asOperation(); }
private void consumeNindo(Nindo op) throws OperationException { indexedCopy.consume(indexed.consumeAndReturnInvertible(op)); }
Preconditions.checkPositionIndex(effectivePos, doc.size()); if (effectivePos == 0) { return null; if (effectivePos == doc.size()) { return null; if (effectivePos == doc.size() - 1) { E root = doc.getDocumentElement(); assert root != null; return root; Point<N> point = doc.locate(effectivePos); E elementHere = Point.enclosingElement(doc, doc.locate(effectivePos)); E elementNext = Point.enclosingElement(doc, doc.locate(effectivePos + 1)); if (elementHere == null) { return null; if (doc.getParentElement(elementHere) != elementNext) { return null; } else {
private static <N, E extends N, T extends N> E nthEnclosingElement(IndexedDocument<N, E, T> doc, int pos, int depth) { assert depth >= 0; E e = Point.enclosingElement(doc, doc.locate(pos)); for (int i = 0; i < depth; i++) { assert e != null; e = doc.getParentElement(e); } return e; }
IndexedDocument<Node, Element, Text> copy = DocProviders.POJO.build(indexedDoc.asOperation(), DocumentSchema.NO_SCHEMA_CONSTRAINTS); copy.consume(indexedDoc.asOperation()); DocOp op = indexedDoc.consumeAndReturnInvertible(nindo); copy.consume(op); Nindo nindo = RandomNindoGenerator.generate(random, ANNO_PARAMS, ConversationSchemas.BLIP_SCHEMA_CONSTRAINTS, copy); DocOp op = indexedDoc.consumeAndReturnInvertible(nindo); copy.consume(op); checkRender(cxt); } catch (RuntimeException e) { Nindo nindo = RandomNindoGenerator.generate(random, GENERAL_PARAMS, ConversationSchemas.BLIP_SCHEMA_CONSTRAINTS, copy); DocOp op = indexedDoc.consumeAndReturnInvertible(nindo); copy.consume(op); checkRender(cxt); } catch (RuntimeException e) { System.out.println("During structural+annotation mutations: " + i + " " + e); System.out.println(DocOpUtil.toXmlString(indexedDoc.asOperation())); throw e;
Point<Node> point = doc.locate(boundaryLocation); boolean detectSplitNecessary = point.isInTextNode() && if (point.getTextOffset() == 0) { nodeAfter = point.getContainer(); nodeBefore = doc.getPreviousSibling(nodeAfter); } else { nodeBefore = point.getContainer(); nodeAfter = doc.getNextSibling(nodeBefore); Text textNodeBefore = doc.asText(nodeBefore); Text textNodeAfter = doc.asText(nodeAfter); switch (pointBias) { case 0: Node n = DocHelper.ensureNodeBoundaryReturnNextNode(boundaryPoint, doc, doc); if (nextNodeLocation >= 0) { assertEquals(nextNodeLocation, doc.getLocation(n)); } else { assertNull(n); assertEquals(boundaryLocation, doc.getLocation(point2)); if (!splitNecessary && !boundaryPoint.isInTextNode()) {
DocumentSchema.NO_SCHEMA_CONSTRAINTS); indexedDoc.consume(DocProviders.POJO.parse(docXml).asOperation()); Point<Node> n = indexedDoc.locate(nodeLocation);
private static <N, E extends N, T extends N> int remainingCharactersInElement( IndexedDocument<N, E, T> doc, int pos) { if (pos >= doc.size()) { return 0; } Point<N> deletionPoint = doc.locate(pos); if (!deletionPoint.isInTextNode()) { return 0; } int offsetWithinNode = deletionPoint.getTextOffset(); N container = deletionPoint.getContainer(); assert doc.getNodeType(container) == NodeType.TEXT_NODE; int nodeLength = DocHelper.getItemSize(doc, container); int remainingChars = nodeLength - offsetWithinNode; assert remainingChars >= 0; while (true) { N next = doc.getNextSibling(container); if (next == null || doc.getNodeType(next) != NodeType.TEXT_NODE) { break; } remainingChars += DocHelper.getItemSize(doc, next); container = next; } return remainingChars; }
if (testAsOperation) { Nindo nindo = RandomNindoGenerator.generate(random, params, constraints, doc); DocOp docOp = doc.consumeAndReturnInvertible(nindo); docs[i] = DocProviders.POJO.build(doc.asOperation(), constraints); } else if (testNindos) { Nindo nindo = RandomNindoGenerator.generate(random, params, constraints, doc); DocOp docOp = doc.consumeAndReturnInvertible(nindo); nindos[i] = nindo; nindos[NUM_OPS * 2 - i - 1] = Nindo.fromDocOp(DocOpInverter.invert(docOp), true);
/** * Test that sample locations are being mapped correctly. */ public void testSampleLocations1() { IndexedDocument<Node, Element, Text> document = createEmptyDocument(); try { document.consume(element("p")); document.consume(characters("a", 1, 1)); Node testNode = document.getDocumentElement().getFirstChild(); assertNotNull(testNode); testNode = testNode.getFirstChild(); assertNotNull(testNode); assertEquals(1, document.getLocation(testNode)); assertEquals(1, testNode.getIndexingContainer().size()); document.consume(characters("b", 2, 1)); assertEquals(1, document.getLocation(testNode)); assertEquals(2, testNode.getIndexingContainer().size()); document.consume(characters("c", 3, 1)); assertEquals(1, document.getLocation(testNode)); assertEquals(3, testNode.getIndexingContainer().size()); } catch (OperationException e) { fail(e.toString()); } }
private void checkFindCharacter(String docXml, int start, String ch, boolean forward, int expectedLocation) { IndexedDocument<Node, Element, Text> doc = DocProviders.POJO.parse(docXml); Tx<Node> startPoint = DocHelper.normalizePoint(doc.locate(start), doc).asTextPoint(); assert startPoint != null : "Invalid start point"; Point<Node> found = TextLocator.findCharacter(doc, startPoint, ch, forward); assertNotNull(found); assertEquals(expectedLocation, doc.getLocation(found)); }
@Override public void consume(Nindo op) { try { DocOp docOp = doc.consumeAndReturnInvertible(op); if (outputSink != null) { outputSink.consume(docOp); } } catch (OperationException oe) { throw new OperationRuntimeException("DocProviders trivial sequencer consume failed.", oe); } } };
private void testHelper(String initialContent, int start, int end, String expectedContent) { IndexedDocument<Node, Element, Text> parse = DocProviders.POJO.parse(initialContent); SubTreeXmlRenderer<Node, Element, Text> renderer = new SubTreeXmlRenderer<Node, Element, Text>(parse); Element nearestCommonAncestor = parse.getDocumentElement(); XmlStringBuilder rendered = renderer.renderRange(parse.locate(start), parse.locate(end)); assertEquals(expectedContent, rendered.toString()); } }
public void testCountChildrenReturnsZeroWhenThereAreNoChildren() { IndexedDocument<Node, Element, Text> doc = DocProviders.POJO.parse(""); assertEquals(0, DocHelper.countChildren(doc, doc.getDocumentElement())); }
private Pair<Nindo, IndexedDocument<Node, Element, Text>> applyTokensToEmptyDoc( RichTextTokenizer tokens) { IndexedDocument<Node, Element, Text> doc = DocProviders.POJO.parse("<body><line/></body>"); Point<Node> insertAt = doc.locate(3); Nindo.Builder builder = new Nindo.Builder(); builder.skip(3); new RichTextMutationBuilder().applyMutations(tokens, builder, doc, insertAt.getContainer()); Nindo nindo = builder.build(); try { doc.consumeAndReturnInvertible(nindo); } catch (OperationException e) { fail("Operation Exception " + e); } return new Pair<Nindo, IndexedDocument<Node,Element,Text>>(nindo, doc); }
public void testNoRedundantSkips() throws OperationException { IndexedDocument<Node, Element, Text> doc = DocProviders.POJO.parse("abcdefghijkl"); Nindo.Builder b; b = new Nindo.Builder(); b.skip(1); b.startAnnotation("a", "1"); b.skip(1); b.startAnnotation("b", "1"); b.skip(1); b.endAnnotation("a"); b.skip(1); b.startAnnotation("c", "1"); b.skip(1); b.endAnnotation("c"); b.skip(1); b.endAnnotation("b"); b.skip(1); b.startAnnotation("c", "1"); b.skip(1); b.endAnnotation("c"); checkApply(doc, b.build()); b = new Nindo.Builder(); b.startAnnotation("z", "1"); b.skip(doc.size()); b.endAnnotation("z"); DocOp docOp = doc.consumeAndReturnInvertible(b.build()); assertEquals(3, docOp.size()); }
@Override public void consume(DocOp op) throws OperationException { // Should throw exceptions if the op is invalid. copy.consume(op); } });
public void testSplitTextNeverReturnsSibling() { TestDocumentContext<Node, Element, Text> cxt = ContextProviders.createTestPojoContext( DocProviders.POJO.parse("ab").asOperation(), null, null, null, DocumentSchema.NO_SCHEMA_CONSTRAINTS); TextNodeOrganiser<Text> organiser = cxt.textNodeOrganiser(); MutableDocument<Node, Element, Text> doc = cxt.document(); Text first = (Text) doc.getFirstChild(doc.getDocumentElement()); Text text = organiser.splitText(first, 1); LocalDocument<Node, Element, Text> local = cxt.annotatableContent(); Element tr = local.transparentCreate("l", Attributes.EMPTY_MAP, doc.getDocumentElement(), text); local.transparentMove(tr, text, null, null); assertNull(cxt.getIndexedDoc().splitText(first, 1)); assertNull(organiser.splitText(first, 1)); assertSame(first, organiser.splitText(first, 0)); assertSame(first, organiser.splitText(first, 0)); assertEquals("a<l>b</l>", XmlStringBuilder.innerXml(local).toString()); assertEquals("ab", XmlStringBuilder.innerXml(doc).toString()); }
private List<Point<Node>> splitTextNodes(IndexedDocument<Node, Element, Text> doc) { List<Point<Node>> splitPoints = new ArrayList<Point<Node>>(); final List<Text> toSplit = new ArrayList<Text>(); DocHelper.traverse(doc, doc.getDocumentElement(), new NodeAction<Node>() { public void apply(Node node) { if (node instanceof Text) { Text t = (Text) node; if (t.getData().contains("^")) { toSplit.add(t); } } } }); for (Text t : toSplit) { while (t != null && t.getData().contains("^")) { t = doc.splitText(t, t.getData().indexOf("^") + 1); splitPoints.add(Point.before(doc, t)); } } return splitPoints; }
/** * Creates an automaton that corresponds to the set of all possible operations * on the given document under the given schema constraints. */ public NindoAutomaton(DocumentSchema schemaConstraints, IndexedDocument<N, E, T> doc) { this.schemaConstraints = schemaConstraints; this.doc = doc; this.resultingLength = doc.size(); }