@Override public int size() { return doc.size(); }
@Override public int length() { return doc.size(); }
/** * @return The title from the given document */ @SuppressWarnings("deprecation") public static <N, E extends N, T extends N> String extractTitle(ReadableWDocument<N, E, T> doc) { int start = doc.firstAnnotationChange(0, doc.size(), TITLE_KEY, null); if (start == -1) { return ""; } String explicitValue = doc.getAnnotation(start, TITLE_KEY); if (!explicitValue.isEmpty()) { return explicitValue; } return DocHelper.getText(doc, start, Annotations.firstAnnotationBoundary(doc, start, doc.size(), TITLE_KEY, AUTO_VALUE)); }
@Override public AnnotationMap annotationsAt(final int pos) { Preconditions.checkElementIndex(pos, doc.size()); class AnnoMap extends HashMap<String, String> implements AnnotationMap { @Override public AnnotationMap updateWith(AnnotationsUpdate mutation) { return new AnnotationMapImpl(this).updateWith(mutation); } @Override public AnnotationMap updateWithNoCompatibilityCheck(AnnotationsUpdate mutation) { return new AnnotationMapImpl(this).updateWithNoCompatibilityCheck(mutation); } } final AnnoMap annotations = new AnnoMap(); doc.knownKeys().each(new Proc() { @Override public void apply(String key) { String value = doc.getAnnotation(pos, key); if (value != null) { annotations.put(key, value); } } }); return annotations; }
@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(); } }
@Override public void setAnnotation(int start, int end, String key, String value) { Annotations.checkPersistentKey(key); Preconditions.checkPositionIndexes(start, end, doc.size()); if (start == end) { return; } try { begin(); consume(Nindo.setAnnotation(start, end, key, value)); } finally { end(); } }
public static <N, T extends N> int getItemSize(ReadableWDocument<N, ?, T> doc, N node) { // Short circuit if it's a text node, implementation is simpler T textNode = doc.asText(node); if (textNode != null) { return doc.getLength(textNode); } // Otherwise, calculate two locations and subtract N parent = doc.getParentElement(node); if (parent == null) { // Requesting size of the document root. // TODO(danilatos/anorth) This would change if we have multiple roots. noteCodeThatWillBreakWithMultipleRoots(); return doc.size(); } N next = doc.getNextSibling(node); int locationAfter = next != null ? doc.getLocation(next) : doc.getLocation(Point.end(parent)); return locationAfter - doc.getLocation(node); }
Preconditions.checkPositionIndexes(rangeStart, rangeEnd, doc.size()); AnnotationBuilder<N, E, T> ab = new AnnotationBuilder<N, E, T>(doc, rangeStart, rangeEnd, key);
int remainder = doc.size() - removeSize - addPosition; // remainder after the insert