@Override public Range createInstance(Type type) { return new Range(-1, -1); } }
/** * Returns the first hit. * * @return an instance of {@link BlipContent}, that represents the first hit. */ public BlipContent value() { BlipContent result = null; if (iterator.hasNext()) { Range range = iterator.next(); if (range.getEnd() - range.getStart() == 1 && blip.getElements().containsKey(range.getStart())) { result = blip.getElements().get(range.getStart()); } else { result = Plaintext.of(blip.getContent().substring(range.getStart(), range.getEnd())); } } iterator.reset(); return result; }
@Override public String toString() { StringBuilder res = new StringBuilder("Annotation("); if (name != null) { res.append(name); res.append(','); } if (value != null) { res.append(value); res.append(','); } res.append(range.toString()); return res.toString(); } }
/** * Constructor. * * @param name the name of this annotation. * @param value the value of this annotation. * @param range the range of this annotation. */ public Annotation(String name, String value, Range range) { this.name = name; this.value = value; this.range = new Range(range.getStart(), range.getEnd()); }
/** * 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(); } }
/** * 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(); } }
/** * Shifts this annotation by {@code shiftAmount} if it is on a range that * is after or covers the given position. * * @param position the anchor position. * @param shiftAmount the amount to shift the annotation range. */ public void shift(int position, int shiftAmount) { int start = range.getStart(); if (start >= position) { start += shiftAmount; } int end = range.getEnd(); if (end >= position) { end += shiftAmount; } range = new Range(start, end); }
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()); }
/** * Returns all matches. * * @return a list of {@link BlipContent} that represents the hits. */ public List<BlipContent> values() { List<BlipContent> result = new ArrayList<BlipContent>(); while (iterator.hasNext()) { Range range = iterator.next(); if (range.getEnd() - range.getStart() == 1 && blip.getElements().containsKey(range.getStart())) { result.add(blip.getElements().get(range.getStart())); } else { result.add(Plaintext.of(blip.getContent().substring(range.getStart(), range.getEnd()))); } } iterator.reset(); return result; }
/** * Constructor. * * @param name the name of this annotation. * @param value the value of this annotation. * @param start the starting index of this annotation. * @param end the end index of this annotation. */ public Annotation(String name, String value, int start, int end) { this.name = name; this.value = value; this.range = new Range(start, end); }
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()); }
@Override public Range next() { if (hitsLeft == 0) { throw new NoSuchElementException(); } int index = getNextIndex(); if (index == -1) { throw new NoSuchElementException(); } hitsLeft--; position = index; return new Range(position, position + rangeSize); }
/** * 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"); } } } }
List<Annotation> newList = new ArrayList<Annotation>(); for (Annotation existing : store.get(name)) { if (start > existing.getRange().getEnd() || end < existing.getRange().getStart()) { newList.add(existing); } else if (start < existing.getRange().getStart() && end > existing.getRange().getEnd()) { continue; } else { if (existing.getRange().getStart() < start) { newList.add(new Annotation(existing.getName(), existing.getValue(), existing.getRange().getStart(), start)); if (existing.getRange().getEnd() > end) { newList.add(new Annotation(existing.getName(), existing.getValue(), end, existing.getRange().getEnd()));
@Override public Range next() { if (hitsLeft == 0) { return null; } hitsLeft--; String searchIn = apiView.apiContents(); int next = searchIn.indexOf(searchFor, from + 1); if (next == -1) { return null; } from = next; return new Range(from, from + searchFor.length()); }
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()); }
String name = annotation.getName(); String value = annotation.getValue(); int start = annotation.getRange().getStart(); int end = annotation.getRange().getEnd(); if (end < existingList.get(j).getRange().getStart()) { break; if (end == existingList.get(j).getRange().getStart() && value.equals(existingList.get(j).getValue())) { end = existingList.get(j).getRange().getEnd(); existingList.remove(j--);
/** * Constructs an instance representing an explicitly set range. * * @param blip the blip to represent. * @param start the start index of the range. * @param end the end index of the range. * @return an instance of blip references. */ public static BlipContentRefs range(Blip blip, int start, int end) { return new BlipContentRefs(blip, new BlipIterator.SingleshotIterator(blip, start, end), Parameter.of(ParamsProperty.RANGE, new Range(start, end))); }
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()); }