private static void sampleContent(Document d) { d.emptyElement(d.getDocumentElement()); d.appendXml(XmlStringBuilder.createFromXmlString("<body><line></line>Hello World</body>")); }
private void parse(Document doc) { E bodyElement = Blips.getBody(doc); if (bodyElement != null) { N child = doc.getFirstChild(bodyElement); while (child != null) { T asText = doc.asText(child); int xmlPos = doc.getLocation(child); if (asText != null) { bits.add(new Bit(doc.getData(asText), xmlPos)); } else { E xmlElement = doc.asElement(child); if (xmlElement != null) { Element element = ElementSerializer.xmlToApiElement(doc, xmlElement, wavelet); // element can be null, but we still want to note that there // was something unknown. N next = doc.getNextSibling(child); int xmlSize; if (next != null) { xmlSize = doc.getLocation(next) - xmlPos; } else { // At the end of the document. XmlSize is the rest. xmlSize = doc.size() - 1 - xmlPos; } bits.add(new Bit(element, xmlPos, xmlSize)); } } child = doc.getNextSibling(child); } } }
/** * Creates and initialises a blip object in the wavelet. * * @param content initial content for the new blip, or {@code null} for * default content */ Blip createBlip(DocInitialization content) { Blip blip = wavelet.createBlip(idGenerator.newBlipId()); if (content != null) { blip.getContent().hackConsume(Nindo.fromDocOp(content, false)); } else { Document doc = blip.getContent(); doc.insertXml(Point.<Doc.N> end(doc.getDocumentElement()), Blips.INITIAL_CONTENT); } return blip; }
/** * Erases the content of the provided document. * * @param wavelet the wavelet containing the document to erase * @param documentId the id of the document to erase */ public static void clearDocument(Wavelet wavelet, String documentId) { Document document = wavelet.getDocument(documentId); if (document.size() != 0) { document.emptyElement(document.getDocumentElement()); } }
/** * Checks whether a manifest could be created on a document. */ public static boolean documentHasManifest(Document doc) { // True if the document has a top-level <MANIFEST_TOP_TAG> element. // The schema implies this is the only possible top element. Doc.E top = DocHelper.getFirstChildElement(doc, doc.getDocumentElement()); return (top != null) && doc.getTagName(top).equals(MANIFEST_TOP_TAG); }
private void erase(Document doc) { doc.emptyElement(doc.getDocumentElement()); }
doc.setElementAttribute(existingElement, "url", url); for (Doc.N child = doc.getFirstChild(existingElement); child != null; child = doc.getNextSibling(child)) { Doc.E childAsElement = doc.asElement(child); if (childAsElement != null) { String key = doc.getTagName(childAsElement); if (key.equals("state")) { key = key + " " + doc.getAttributes(childAsElement).get("name"); doc.deleteNode(child); } else { if (child != null) { if (tag.equals("state")) { doc.setElementAttribute(child, "value", val); } else { doc.emptyElement(child); Point<Doc.N> point = Point.<Doc.N> inElement(child, null); doc.insertText(point, val); doc.insertXml(Point.<Doc.N> inElement(existingElement, null), xml);
void populate(ConversationThread thread, FakeThreadView threadUi) { for (BlipBuilder blipBuilder : blipBuilders) { ConversationBlip blip = thread.appendBlip(); blip.getContent().insertText(blip.getContent().size() - 1, "Blip " + blipCount++); blipBuilder.populate(blip, threadUi.insertBlipBefore(null, blip)); } }
private void restore(Document doc) { // No comment. UncheckedDocOpBuffer builder = new UncheckedDocOpBuffer(); doc.toInitialization().apply(builder); DocOp state = builder.finish(); DocOp erasure = DocOpInverter.invert(state); DocOp restoration; try { restoration = Composer.compose(erasure, state); } catch (OperationException e) { // If the code above fails, then there is a bug in the operation code, not // these tests. Fail with an exception, not with a JUnit fail(). throw new RuntimeException(e); } doc.hackConsume(Nindo.fromDocOp(restoration, false)); } }
public static Map<Integer, Element> serialize(Document doc, Wavelet wavelet) { Map<Integer, Element> result = Maps.newHashMap(); ApiView apiView = new ApiView(doc, wavelet); Doc.N node = Blips.getBody(doc); if (node != null) { // The node is the body; we're after its children node = doc.getFirstChild(node); } while (node != null) { E element = doc.asElement(node); if (element != null) { Element apiElement = xmlToApiElement(doc, element, wavelet); if (apiElement != null) { result.put(apiView.transformToTextOffset(doc.getLocation(element)), apiElement); } } node = doc.getNextSibling(node); } return result; }
/** * Inserts an inline reply anchor element in the blip document. * * @param threadId id of the reply thread * @param location location at which to insert anchor */ private void createInlineReplyAnchor(final String threadId, final int location) { blip.getContent().with(new Action() { @Override public <N, E extends N, T extends N> void exec(MutableDocument<N, E, T> doc) { Point<N> point = doc.locate(location); doc.createElement(point, Blips.THREAD_INLINE_ANCHOR_TAGNAME, Collections.singletonMap(Blips.THREAD_INLINE_ANCHOR_ID_ATTR, threadId)); } }); }
/** * Methods that checks that the new blip was actually created and stored in * the context. As well as that it checks its contents */ private ConversationBlip checkAndGetNewBlip( OperationContextImpl context, ObservableConversation conversation, JsonRpcResponse response) throws InvalidRequestException { // Retrieve the blip using the context so that the temp blip storage is // checked ConversationBlip newBlip = context.getBlip(conversation, TEMP_BLIP_ID); assertEquals("The response didn't contain the id of the new blip", newBlip.getId(), response.getData().get(ParamsProperty.NEW_BLIP_ID)); String actualContent = newBlip.getContent().toXmlString(); assertTrue("Expected the new blip to contain the contens as specified in the operation", actualContent.contains(NEW_BLIP_CONTENT)); return newBlip; } }
public void testClearAnnotatation() throws Exception { Document doc = getRootBlip().getContent(); doc.setAnnotation(CONTENT_START_XML, CONTENT_START_XML + 1, ANNOTATION_KEY, ANNOTATION_VALUE); String annotation = getRootBlip().getContent().getAnnotation(CONTENT_START_XML, ANNOTATION_KEY); assertEquals("Expected the text to be annotated", ANNOTATION_VALUE, annotation); OperationRequest operation = operationRequest(OperationType.DOCUMENT_MODIFY, rootBlipId, Parameter.of(ParamsProperty.MODIFY_ACTION, new DocumentModifyAction(ModifyHow.CLEAR_ANNOTATION, NO_VALUES, ANNOTATION_KEY, NO_ELEMENTS, NO_BUNDLED_ANNOTATIONS, false)), Parameter.of(ParamsProperty.INDEX, CONTENT_START_TEXT)); service.execute(operation, helper.getContext(), ALEX); assertNull("Expected this text not to be annotated", getRootBlip().getContent().getAnnotation(CONTENT_START_XML, ANNOTATION_KEY)); }
int location = doc.getLocation(Point.after(doc, line)); ConversationBlip newBlip = parentBlip.addReplyThread(location).appendBlip(); context.putBlip(blipData.getBlipId(), newBlip);
public void insert(int pos, Element element) { XmlStringBuilder xml = ElementSerializer.apiElementToXml(element); int beforeSize = doc.size(); Pair<Integer, Integer> where = locate(pos); int index = where.first; Point<Doc.N> point = doc.locate(last.xmlPos + last.xmlSize); doc.insertXml(point, xml); bits.add(new Bit(element, last.xmlPos + last.xmlSize, doc.size() - beforeSize)); return; Point<Doc.N> point = doc.locate(bit.xmlPos + offset); doc.insertXml(point, xml); int xmlSize = doc.size() - beforeSize; if (bit.string != null && offset > 0) { shift(index + 1, xmlSize);
/** * Sets the annotation for a document. * * @param operation the operation requesting the annotation to be set. * @param doc the document to change the annotation in. * @param start where the annotation should start. * @param end where the annotation should end. * @param key the key of the annotation. * @param value the value of the annotation. * @throws InvalidRequestException if the annotation could not be set. */ private void setDocumentAnnotation( OperationRequest operation, Document doc, int start, int end, String key, String value) throws InvalidRequestException { try { doc.setAnnotation(start, end, key, value); } catch (IndexOutOfBoundsException e) { throw new InvalidRequestException( "Can't set annotation for out of bounds indices " + e.getMessage(), operation, e); } }
Preconditions.checkArgument(xmlOffset <= doc.size());
@Override public Element fromXml(Document doc, E element, Wavelet wavelet) { Map<String, String> properties = Maps.newHashMap(); String attachmentId = doc.getAttribute(element, ATTACHMENT_STR); if (attachmentId != null) { properties.put(Attachment.ATTACHMENT_ID, attachmentId); } String caption = getCaption(doc, element); if (caption != null) { properties.put(Attachment.CAPTION, caption); } if (wavelet != null && attachmentId != null) { Document attachmentDataDoc = wavelet.getDocument(IdConstants.ATTACHMENT_METADATA_PREFIX + "+" + attachmentId); if (attachmentDataDoc != null) { String dataDocument = attachmentDataDoc.toXmlString(); if (dataDocument != null) { properties.put(Attachment.MIME_TYPE, extractValue(dataDocument, MIME_TYPE_PATTERN)); properties.put(Attachment.ATTACHMENT_URL, ElementSerializer.attachmentDownloadHostUrl + getAttachmentUrl(dataDocument)); } } } return new Attachment(properties, null); }
/** * Extracts all annotations that span inside the body tag of the given * document. * * @param doc the document to get the annotations from. * @param apiView provides a utility function to convert an xml offset point * into text offset. * @return the annotations represented as a list of {@link Annotation}. */ private static List<Annotation> extractAnnotations(Document doc, ApiView apiView) { List<Annotation> result = Lists.newArrayList(); for (RangedAnnotation<String> annotation : doc.rangedAnnotations(0, doc.size(), null)) { if (annotation.key() != null && annotation.value() != null) { int start = apiView.transformToTextOffset(annotation.start()); int end = apiView.transformToTextOffset(annotation.end()); result.add(new Annotation(annotation.key(), annotation.value(), new Range(start, end))); } } return result; } }
/** * Copies a document from one wavelet to another, appending the contents if the * destination document already exists. * * @param sourceWavelet the source wavelet * @param destWavelet the destination wavelet * @param docId the id of the document to copy */ public static void copyDocument(Wavelet sourceWavelet, Wavelet destWavelet, String docId) { Document document = sourceWavelet.getDocument(docId); DocInitialization docInit = document.toInitialization(); // TODO(user): add a createDocument method to Wavelet so that we can push this ugliness // down the stack. ObservableDocument destDoc = destWavelet.getDocument(docId); destDoc.hackConsume(Nindo.fromDocOp(docInit, false /* don't remove skip */)); }