private int countHits(DocumentHitIterator it) { int res = 0; while (it.next() != null) { res++; } return res; }
private int countHits(DocumentHitIterator it) { int res = 0; while (it.next() != null) { res++; } return res; }
/** * Inserts elements after the position specified by the hitIterator. * * @param operation the operation that wants to insert elements. * @param doc the document to insert elements in. * @param view the {@link ApiView} of that document. * @param hitIterator the iterator over the places where to insert. * @param modifyAction the action that specifies what to insert. * @throws InvalidRequestException if something goes wrong. */ private void insertAfter(OperationRequest operation, Document doc, ApiView view, DocumentHitIterator hitIterator, DocumentModifyAction modifyAction) throws InvalidRequestException { int valueIndex = 0; Range range = hitIterator.next(); while (range != null) { int insertAt = range.getEnd(); int inserted = insertInto(operation, doc, view, insertAt, modifyAction, valueIndex); hitIterator.shift(insertAt, inserted); valueIndex++; range = hitIterator.next(); } }
/** * Inserts elements at the position specified by the hitIterator. * * @param operation the operation that wants to insert elements. * @param doc the document to insert elements in. * @param view the {@link ApiView} of that document. * @param hitIterator the iterator over the places where to insert. * @param modifyAction the action that specifies what to insert. * @throws InvalidRequestException if something goes wrong. */ private void insert(OperationRequest operation, Document doc, ApiView view, DocumentHitIterator hitIterator, DocumentModifyAction modifyAction) throws InvalidRequestException { int valueIndex = 0; Range range = hitIterator.next(); while (range != null) { int insertAt = range.getStart(); int inserted = insertInto(operation, doc, view, insertAt, modifyAction, valueIndex); hitIterator.shift(insertAt, inserted); valueIndex++; range = hitIterator.next(); } }
/** * Deletes ranges of elements from a document as specified by the iterator. * * @param operation the operation to execute. * @param view the view of the document. * @param hitIterator iterates over the ranges of elements to delete. * @throws InvalidRequestException if the specified range was invalid. */ private void delete(OperationRequest operation, ApiView view, DocumentHitIterator hitIterator) throws InvalidRequestException { Range range = hitIterator.next(); while (range != null) { int start = range.getStart(); int end = range.getEnd(); if (start == 0) { // Can't delete the first new line. start = 1; } if (start >= end) { throw new InvalidRequestException( "Invalid range specified, " + start + ">=" + end, operation); } // Delete using the view. view.delete(start, end); // Shift the iterator to match the updated document. hitIterator.shift(start, end - start); range = hitIterator.next(); } }
/** * Clears the annotation for the given ranges of the document as indicated by * the {@link DocumentModifyAction}. * * @param operation the operation to execute. * @param doc the document to annotate. * @param view the view of the document. * @param hitIterator iterates over the ranges to remove the annotation from, * specified in {@link ApiView} offset. * @param modifyAction the {@link DocumentModifyAction} specifying what the * key of the annotation is annotation is. * @throws InvalidRequestException if the annotation could not be set. */ private void clearAnnotation(OperationRequest operation, Document doc, ApiView view, DocumentHitIterator hitIterator, DocumentModifyAction modifyAction) throws InvalidRequestException { Preconditions.checkArgument(modifyAction.getModifyHow() == ModifyHow.CLEAR_ANNOTATION, "This method only supports CLEAR_ANNOTATION"); String annotationKey = modifyAction.getAnnotationKey(); Range range = hitIterator.next(); while (range != null) { int start = view.transformToXmlOffset(range.getStart()); int end = view.transformToXmlOffset(range.getEnd()); setDocumentAnnotation(operation, doc, start, end, annotationKey, null); range = hitIterator.next(); } }
public void testBasicElementMatcher() { String xml = "text <gadget></gadget> <something/> <gadget/> hello"; ApiView apiView = createApiViewFromXml(xml); DocumentHitIterator it = new DocumentHitIterator.ElementMatcher( apiView, ElementType.GADGET, Collections.<String, String>emptyMap(), -1); List<Integer> hits = Lists.newArrayList(); assertEquals(xml.indexOf("<gadget>") + 1, it.next().getStart()); assertEquals(10, it.next().getStart()); // Elements get counted as 1 assertNull(it.next()); }
public void testBasicElementMatcher() { String xml = "text <gadget></gadget> <something/> <gadget/> hello"; ApiView apiView = createApiViewFromXml(xml); DocumentHitIterator it = new DocumentHitIterator.ElementMatcher( apiView, ElementType.GADGET, Collections.<String, String>emptyMap(), -1); List<Integer> hits = Lists.newArrayList(); assertEquals(xml.indexOf("<gadget>") + 1, it.next().getStart()); assertEquals(10, it.next().getStart()); // Elements get counted as 1 assertNull(it.next()); }
/** * Annotates the given ranges of the document as indicated by the * {@link DocumentModifyAction}. * * @param operation the operation to execute. * @param doc the document to annotate. * @param view the view of the document. * @param hitIterator iterates over the ranges to annotate, specified in * {@link ApiView} offset. * @param modifyAction the {@link DocumentModifyAction} specifying what the * annotation is. * @throws InvalidRequestException if the annotation could not be set. */ private void annotate(OperationRequest operation, Document doc, ApiView view, DocumentHitIterator hitIterator, DocumentModifyAction modifyAction) throws InvalidRequestException { Preconditions.checkArgument( modifyAction.getModifyHow() == ModifyHow.ANNOTATE, "This method only supports ANNOTATE"); String annotationKey = modifyAction.getAnnotationKey(); int valueIndex = 0; Range range = hitIterator.next(); while (range != null) { int start = view.transformToXmlOffset(range.getStart()); int end = view.transformToXmlOffset(range.getEnd()); setDocumentAnnotation( operation, doc, start, end, annotationKey, modifyAction.getValue(valueIndex)); valueIndex++; range = hitIterator.next(); } }
/** * Updates elements in the document. * <b>Note</b>: Only gadget elements are supported, for now. * * @param operation the operation the operation that wants to update elements. * @param doc the document to update elements in. * @param view the {@link ApiView} of that document. * @param hitIterator the iterator over the places where to update elements. * @param modifyAction the action that specifies what to update. * @throws InvalidRequestException if something goes wrong. */ private void updateElement(OperationRequest operation, Document doc, ApiView view, DocumentHitIterator hitIterator, DocumentModifyAction modifyAction) throws InvalidRequestException { Range range = null; for (int index = 0; ((range = hitIterator.next()) != null); ++index) { Element element = modifyAction.getElement(index); if (element != null) { if (element.isGadget()) { int xmlStart = view.transformToXmlOffset(range.getStart()); Doc.E docElem = Point.elementAfter(doc, doc.locate(xmlStart)); updateExistingGadgetElement(doc, docElem, element); } else { // TODO (Yuri Z.) Updating other elements. throw new UnsupportedOperationException( "Can't update other elements than gadgets at the moment"); } } } }
public void testTextMatcherDeletingTheFirstChar() { ApiView apiView = createApiViewFromXml("1 1 1"); DocumentHitIterator it = new DocumentHitIterator.TextMatcher(apiView, "1", -1); for (int i = 0; i < 3; i++) { Range r = it.next(); assertEquals(i + 1, r.getStart()); apiView.delete(1, 2); it.shift(1, -1); } assertNull(it.next()); }
public void testTextMatcherWithoutShift() { ApiView apiView = createApiViewFromXml("1 1 1"); DocumentHitIterator it = new DocumentHitIterator.TextMatcher(apiView, "1", -1); for (int i = 0; i < 3; i++) { Range r = it.next(); assertEquals(i * 2 + 1, r.getStart()); assertEquals(i * 2 + 2, r.getEnd()); } assertNull(it.next()); }
public void testTextMatcherDeletingTheFirstChar() { ApiView apiView = createApiViewFromXml("1 1 1"); DocumentHitIterator it = new DocumentHitIterator.TextMatcher(apiView, "1", -1); for (int i = 0; i < 3; i++) { Range r = it.next(); assertEquals(i + 1, r.getStart()); apiView.delete(1, 2); it.shift(1, -1); } assertNull(it.next()); }
public void testTextMatcherWithoutShift() { ApiView apiView = createApiViewFromXml("1 1 1"); DocumentHitIterator it = new DocumentHitIterator.TextMatcher(apiView, "1", -1); for (int i = 0; i < 3; i++) { Range r = it.next(); assertEquals(i * 2 + 1, r.getStart()); assertEquals(i * 2 + 2, r.getEnd()); } assertNull(it.next()); }
/** * Pretend we're shifting in the manner of INSERT_AFTER */ public void testTextMatcherShiftInsertAfter() { ApiView apiView = createApiViewFromXml("1 1 1"); DocumentHitIterator it = new DocumentHitIterator.TextMatcher(apiView, "1", -1); for (int i = 0; i < 3; i++) { Range r = it.next(); assertEquals(i * 3 + 1, r.getStart()); apiView.insert(r.getEnd(), "1"); it.shift(r.getEnd(), 1); } assertNull(it.next()); }
/** * Pretend we're shifting in the manner of INSERT_AFTER */ public void testTextMatcherShiftInsertAfter() { ApiView apiView = createApiViewFromXml("1 1 1"); DocumentHitIterator it = new DocumentHitIterator.TextMatcher(apiView, "1", -1); for (int i = 0; i < 3; i++) { Range r = it.next(); assertEquals(i * 3 + 1, r.getStart()); apiView.insert(r.getEnd(), "1"); it.shift(r.getEnd(), 1); } assertNull(it.next()); }