/** * Creates an operation to insert the XML fragment * "<a href=\"http://www.google.com/\">google</a>". * * @param location The location at which the insert the fragment. * @return The operation. */ private static Nindo structuralSample5(int location) { Attributes attributes = new AttributesImpl( Collections.singletonMap("href", "http://www.google.com/")); Nindo.Builder builder = new Nindo.Builder(); builder.skip(location); builder.elementStart("a", new AttributesImpl(attributes)); builder.characters("google"); builder.elementEnd(); return builder.build(); }
@Override public void deleteCharacters(String chars) { b.deleteCharacters(chars.length()); }
builder.endAnnotation(key); builder.elementEnd(); ++offset;
int size = end.getTextOffset() - start.getTextOffset(); if (size > 0) { builder.deleteCharacters(size); node = doc.getNextSibling(start.getContainer()); if (size > 0) { builder.deleteCharacters(size); T text = doc.asText(node); if (text != null) { builder.deleteCharacters(doc.getData(text).length()); } else { deleteElement(doc.asElement(node), builder); int size = end.getTextOffset(); if (size > 0) { builder.deleteCharacters(size);
case TEXT: if (tokenizer.getData() != null) { builder.characters(PermittedCharacters.BLIP_TEXT.coerceString(tokenizer.getData())); offset += tokenizer.getData().length();
b = new Nindo.Builder(); b.skip(1); b.startAnnotation("a", "1"); b.characters("x"); b.endAnnotation("a"); checkApply(doc, b.build()); b = new Nindo.Builder(); b.skip(1); b.startAnnotation("a", "2"); b.skip(1); b.endAnnotation("a"); checkApply(doc, b.build()); b = new Nindo.Builder(); b.skip(1); b.startAnnotation("a", "2"); b.characters("w"); b.endAnnotation("a"); b.startAnnotation("b", "1"); b.skip(1); b.startAnnotation("a", "3"); b.characters("y"); b.startAnnotation("b", "2"); b.characters("z"); b.endAnnotation("a"); b.endAnnotation("b"); checkApply(doc, b.build());
b = new Nindo.Builder(); b.skip(1); b.startAnnotation("a", "1"); b.characters("x"); b.endAnnotation("a"); checkApply(doc, b.build()); b = new Nindo.Builder(); b.skip(1); b.startAnnotation("a", "2"); b.skip(1); b.endAnnotation("a"); checkApply(doc, b.build()); b = new Nindo.Builder(); b.skip(1); b.startAnnotation("a", "2"); b.characters("w"); b.endAnnotation("a"); b.startAnnotation("b", "1"); b.skip(1); b.startAnnotation("a", "3"); b.characters("y"); b.startAnnotation("b", "2"); b.characters("z"); b.endAnnotation("a"); b.endAnnotation("b"); checkApply(doc, b.build());
b.skip(skipAccum); skipAccum = 0; b.startAnnotation(key, value); b.skip(size); b.endAnnotation(key); dirty = true; } else {
if (!equal(currentValue, value)) { if (skipAccum > 0) { b.skip(skipAccum); skipAccum = 0; b.startAnnotation(key, value); b.skip(size); b.endAnnotation(key); dirty = true; } else {
b.replaceAttributes(attrs); b.elementStart("x", attrs("a", "1")); b.characters("hello"); b.elementStart("y", attrs("b", "2", "c", "3")); b.characters("yeah"); b.elementEnd(); b.elementEnd(); String moreText = "more text"; b.characters(moreText); b.deleteElementStart(); b.deleteCharacters(3); b.deleteElementStart(); b.deleteCharacters(3); b.deleteElementEnd(); b.deleteElementEnd(); String moreText2 = "more"; b.characters(moreText2); consumeNindo(b.build());
b.replaceAttributes(attrs); b.elementStart("x", attrs("a", "1")); b.characters("hello"); b.elementStart("y", attrs("b", "2", "c", "3")); b.characters("yeah"); b.elementEnd(); b.elementEnd(); String moreText = "more text"; b.characters(moreText); b.deleteElementStart(); b.deleteCharacters(3); b.deleteElementStart(); b.deleteCharacters(3); b.deleteElementEnd(); b.deleteElementEnd(); String moreText2 = "more"; b.characters(moreText2); consumeNindo(b.build());
/** * Addes a skip of the given size to the start of the nindo if distance is * positive, reduces initial skips if the distance is negative. * * @return a new nindo */ public static Nindo shift(final int distance, Nindo nindo) { int remaining = Math.max(0, -distance); Builder b = builder(); if (distance > 0) { b.skip(distance); } for (MutationComponent c : nindo.mutationList) { if (remaining > 0) { if (c instanceof Skip) { int skipSize = ((Skip) c).skipSize; int diff = Math.min(remaining, skipSize); remaining -= diff; if (diff < skipSize) { b.skip(skipSize - diff); } } else { Preconditions.illegalArgument("Not enough initial skips in nindo to take off"); } } else { c.apply(b); } } return b.build(); }
private void endAnnotation(Nindo.Builder builder, String annotationKey) { Stack<String> annotationStack = startedAnnotations.get(annotationKey); Preconditions.checkNotNull(annotationStack, "cannot end unstarted annotation"); String current = annotationStack.pop(); // If there are no more entries, and the current annotation is non-null. if (annotationStack.isEmpty() && current != null) { // avoid no-ops if (!isDefaultValue(annotationKey, current)) { builder.endAnnotation(annotationKey); } // Conditionally null out certain keys if they are in a special list if (defaultValueMap.containsKey(annotationKey)) { builder.startAnnotation(annotationKey, defaultValueMap.get(annotationKey)); } else { startedAnnotations.remove(annotationKey); } } else { String nextAnnotation = annotationStack.peek(); if (ValueUtils.notEqual(current, nextAnnotation)) { // There's another entry in the stack and it is different from the // current- // If the entry in the stack is the same as default, just end the annotation. // Otherwise, start the next one. if (isDefaultValue(annotationKey, nextAnnotation)) { builder.endAnnotation(annotationKey); } else { builder.startAnnotation(annotationKey, nextAnnotation); } } } }
switch (parser.getCurrentType()) { case TEXT: builder.characters(parser.getText()); break; case START_ELEMENT: builder.elementStart(parser.getTagName(), new AttributesImpl(CollectionUtils .newJavaMap(parser.getAttributes()))); break; case END_ELEMENT: builder.elementEnd(); break;
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()); }
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 resetAnnotation(int start, int end, String key, String value) { Annotations.checkPersistentKey(key); Preconditions.checkPositionIndexes(start, end, doc.size()); try { begin(); Builder b = new Builder(); if (start > 0) { b.startAnnotation(key, null); b.skip(start); } if (start != end) { b.startAnnotation(key, value); b.skip(end - start); } if (doc.size() != end) { b.startAnnotation(key, null); b.skip(doc.size() - end); } b.endAnnotation(key); consume(b.build()); } finally { end(); } }
/** * Creates an operation to delete the XML fragment * "12<u>3<i>hello</i><b>world</b>4</u>56". * * @param location The start of the range where the fragment to be deleted * resides. * @return The operation. */ private static Nindo structuralDeletionSample2(int location) { Nindo.Builder builder = new Nindo.Builder(); builder.skip(location); builder.deleteCharacters(2); builder.deleteElementStart(); builder.deleteCharacters(1); builder.deleteElementStart(); builder.deleteCharacters(5); builder.deleteElementEnd(); builder.deleteElementStart(); builder.deleteCharacters(5); builder.deleteElementEnd(); builder.deleteCharacters(1); builder.deleteElementEnd(); builder.deleteCharacters(2); return builder.build(); }
public void testRedundantAnnotationsPreserved() throws OperationException { IndexedDocument<Node, Element, Text> doc = DocProviders.POJO.parse("abcdefg"); IndexedDocument<Node, Element, Text> doc2 = DocProviders.POJO.parse("abcdefg"); Nindo.Builder b; b = new Nindo.Builder(); b.startAnnotation("a", "1"); b.skip(7); b.endAnnotation("a"); checkApply(doc2, b.build()); b = new Nindo.Builder(); b.startAnnotation("a", null); b.skip(2); b.startAnnotation("a", "2"); b.skip(2); b.startAnnotation("a", null); b.skip(3); b.endAnnotation("a"); DocOp docOp = doc.consumeAndReturnInvertible(b.build()); doc2.consumeAndReturnInvertible(Nindo.fromDocOp(docOp, true)); assertEquals( DocOpUtil.toXmlString(doc.asOperation()), DocOpUtil.toXmlString(doc2.asOperation())); }