/** * Obtains a DocumentMutation from the builder. * * This method may do some error checking on the mutation and throw an * exception if it is ill-formed. * * @return The built DocumentMutation. */ public Nindo build() { return new Nindo(mutationList); }
/** * Creates a document mutation that deletes an empty element at a given * location. * * @param elementLocation The location of the empty element to delete. * @return The document mutation. */ public static Nindo deleteElement(int elementLocation) { List<MutationComponent> mutationList = new ArrayList<MutationComponent>(2); // It may be impossible to delete the root element, but let's check // that elsewhere. if (elementLocation > 0) { mutationList.add(new Skip(elementLocation)); } mutationList.add(DeleteElementStart.INSTANCE); mutationList.add(DeleteElementEnd.INSTANCE); return new Nindo(mutationList); }
/** * Creates a document mutation that removes an attribute of an element. * * @param location The location of the element whose attribute is to be * removed. * @param name The name of the attribute to remove. * @return The document mutation. */ public static Nindo removeAttribute(int location, String name) { List<MutationComponent> mutationList = new ArrayList<MutationComponent>(2); if (location > 0) { mutationList.add(new Skip(location)); } mutationList.add(new UpdateAttributes(Collections.singletonMap(name, (String)null))); return new Nindo(mutationList); }
/** * Creates a document mutation that sets all the attributes of an element. * * @param location The location of the element whose attributes are to be set. * @param attributes The attributes that the element should have. * @return The document mutation. */ public static Nindo replaceAttributes(int location, Attributes attributes) { List<MutationComponent> mutationList = new ArrayList<MutationComponent>(2); if (location > 0) { mutationList.add(new Skip(location)); } mutationList.add(new ReplaceAttributes(attributes)); return new Nindo(mutationList); }
/** * Creates a document mutation that deletes the characters denoted by the * given range. * * @param start The start of the range of the characters to delete. * @param end The end of the range of the characters to delete. * @return The document mutation. */ public static Nindo deleteCharacters(int start, int end) { List<MutationComponent> mutationList = new ArrayList<MutationComponent>(2); mutationList.add(new Skip(start)); mutationList.add(new DeleteCharacters(end - start)); return new Nindo(mutationList); }
/** * Creates a document mutation that inserts the given characters at the given * location. * * @param location The location at which to insert characters. * @param characters The characters to insert. * @return The document mutation. */ public static Nindo insertCharacters(int location, String characters) { assert !characters.isEmpty(); List<MutationComponent> mutationList = new ArrayList<MutationComponent>(2); mutationList.add(new Skip(location)); mutationList.add(new Characters(characters)); return new Nindo(mutationList); }
/** * Creates a document mutation that sets an attribute of an element. * * @param location The location of the element whose attribute is to be set. * @param name The name of the attribute to set. * @param value The value to which to set the attribute. * @return The document mutation. */ public static Nindo setAttribute(int location, String name, String value) { List<MutationComponent> mutationList = new ArrayList<MutationComponent>(2); if (location > 0) { mutationList.add(new Skip(location)); } mutationList.add(new UpdateAttributes(Collections.singletonMap(name, value))); return new Nindo(mutationList); }
/** * Creates a document mutation that inserts an element at the given location. * * @param location The location at which to insert the element. * @param tagName The tag name of the element. * @param attributes The attributes of the element. * @return The document mutation. */ public static Nindo insertElement(int location, String tagName, Attributes attributes) { List<MutationComponent> mutationList = new ArrayList<MutationComponent>(4); if (location > 0) { mutationList.add(new Skip(location)); } mutationList.add(new ElementStart(tagName, attributes)); mutationList.add(ElementEnd.INSTANCE); return new Nindo(mutationList); }
/** * Creates a document mutation that sets an annotation over a range. * * @param start The location of the start of the range on which the annotation * is to be set. * @param end The location of the end of the range on which the annotation is * to be set. * @param key The annotation key. * @param value The annotation value. * @return The document mutation. */ public static Nindo setAnnotation(int start, int end, String key, String value) { List<MutationComponent> mutationList = new ArrayList<MutationComponent>(0); if (start != end) { Preconditions.checkPositionIndexes(start, end, Integer.MAX_VALUE); if (start > 0) { mutationList.add(new Skip(start)); } mutationList.add(new StartAnnotation(key, value)); mutationList.add(new Skip(end - start)); mutationList.add(new EndAnnotation(key)); } return new Nindo(mutationList); }