WAVE-311 Updates Snippets.collatetextForOps to accept custom function to modify the result on new lines.
Project: http://git-wip-us.apache.org/repos/asf/incubator-wave/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-wave/commit/6c7bc7a0 Tree: http://git-wip-us.apache.org/repos/asf/incubator-wave/tree/6c7bc7a0 Diff: http://git-wip-us.apache.org/repos/asf/incubator-wave/diff/6c7bc7a0 Branch: refs/heads/master Commit: 6c7bc7a0564b01d2dded92ce9ed5d6db40c4515a Parents: deb1854 Author: Yuri Zelikov <[email protected]> Authored: Fri Apr 25 23:27:21 2014 +0300 Committer: Yuri Zelikov <[email protected]> Committed: Wed Aug 27 19:17:55 2014 +0300 ---------------------------------------------------------------------- src/org/waveprotocol/box/common/Snippets.java | 29 ++++++- .../server/waveserver/SolrWaveIndexerImpl.java | 88 ++++---------------- 2 files changed, 41 insertions(+), 76 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-wave/blob/6c7bc7a0/src/org/waveprotocol/box/common/Snippets.java ---------------------------------------------------------------------- diff --git a/src/org/waveprotocol/box/common/Snippets.java b/src/org/waveprotocol/box/common/Snippets.java index e3a4a1d..52a9323 100644 --- a/src/org/waveprotocol/box/common/Snippets.java +++ b/src/org/waveprotocol/box/common/Snippets.java @@ -19,6 +19,7 @@ package org.waveprotocol.box.common; +import com.google.common.base.Function; import com.google.common.collect.Lists; import org.waveprotocol.wave.model.document.operation.AnnotationBoundaryMap; @@ -44,6 +45,15 @@ import java.util.Set; */ public final class Snippets { + private final static Function<StringBuilder, Void> DEFAULT_LINE_MODIFIER = + new Function<StringBuilder, Void>() { + + @Override + public Void apply(StringBuilder resultBuilder) { + resultBuilder.append(" "); + return null; + } + }; public static final int DIGEST_SNIPPET_LENGTH = 140; /** @@ -79,9 +89,12 @@ public final class Snippets { * Concatenates all of the text of the specified docops into a single String. * * @param documentops the document operations to concatenate. + * @param func the function that will be applied on result when + * DocumentConstants.LINE event is be encountered during parsing. * @return A String containing the characters from the operations. */ - public static String collateTextForOps(Iterable<DocOp> documentops) { + public static String collateTextForOps(Iterable<? extends DocOp> documentops, + final Function<StringBuilder, Void> func) { final StringBuilder resultBuilder = new StringBuilder(); for (DocOp docOp : documentops) { docOp.apply(InitializationCursorAdapter.adapt(new DocOpCursor() { @@ -97,7 +110,7 @@ public final class Snippets { @Override public void elementStart(String type, Attributes attrs) { if (type.equals(DocumentConstants.LINE)) { - resultBuilder.append(" "); + func.apply(resultBuilder); } } @@ -134,10 +147,20 @@ public final class Snippets { } /** + * Concatenates all of the text of the specified docops into a single String. + * + * @param documentops the document operations to concatenate. + * @return A String containing the characters from the operations. + */ + public static String collateTextForOps(Iterable<? extends DocOp> documentops) { + return collateTextForOps(documentops, DEFAULT_LINE_MODIFIER); + } + + /** * Returns a snippet or null. */ public static String renderSnippet(final ReadableWaveletData wavelet, - final int maxSnippetLength) { + final int maxSnippetLength) { final StringBuilder sb = new StringBuilder(); Set<String> docsIds = wavelet.getDocumentIds(); long newestLmt = -1; http://git-wip-us.apache.org/repos/asf/incubator-wave/blob/6c7bc7a0/src/org/waveprotocol/box/server/waveserver/SolrWaveIndexerImpl.java ---------------------------------------------------------------------- diff --git a/src/org/waveprotocol/box/server/waveserver/SolrWaveIndexerImpl.java b/src/org/waveprotocol/box/server/waveserver/SolrWaveIndexerImpl.java index 3a2fcd0..de3178e 100644 --- a/src/org/waveprotocol/box/server/waveserver/SolrWaveIndexerImpl.java +++ b/src/org/waveprotocol/box/server/waveserver/SolrWaveIndexerImpl.java @@ -19,7 +19,9 @@ package org.waveprotocol.box.server.waveserver; +import com.google.common.base.Function; import com.google.common.base.Preconditions; +import com.google.common.collect.Lists; import com.google.common.util.concurrent.ListenableFuture; import com.google.common.util.concurrent.ListenableFutureTask; import com.google.gson.JsonArray; @@ -36,7 +38,7 @@ import org.apache.commons.httpclient.methods.RequestEntity; import org.apache.commons.httpclient.methods.StringRequestEntity; import org.apache.http.HttpStatus; import org.waveprotocol.box.common.DeltaSequence; -import org.waveprotocol.box.common.DocumentConstants; +import org.waveprotocol.box.common.Snippets; import org.waveprotocol.box.server.robots.util.ConversationUtil; import org.waveprotocol.wave.model.conversation.ObservableConversation; import org.waveprotocol.wave.model.conversation.ObservableConversationBlip; @@ -44,12 +46,7 @@ import org.waveprotocol.wave.model.conversation.ObservableConversationView; import org.waveprotocol.wave.model.conversation.TitleHelper; import org.waveprotocol.wave.model.conversation.WaveletBasedConversation; import org.waveprotocol.wave.model.document.Document; -import org.waveprotocol.wave.model.document.operation.AnnotationBoundaryMap; -import org.waveprotocol.wave.model.document.operation.Attributes; -import org.waveprotocol.wave.model.document.operation.AttributesUpdate; -import org.waveprotocol.wave.model.document.operation.DocOp; -import org.waveprotocol.wave.model.document.operation.DocOpCursor; -import org.waveprotocol.wave.model.document.operation.impl.InitializationCursorAdapter; +import org.waveprotocol.wave.model.document.operation.DocInitialization; import org.waveprotocol.wave.model.id.IdUtil; import org.waveprotocol.wave.model.id.WaveId; import org.waveprotocol.wave.model.id.WaveletId; @@ -90,71 +87,6 @@ PerUserWaveViewBus.Listener { private final ReadableWaveletDataProvider waveletDataProvider; - /*- - * copied with modifications from - * org.waveprotocol.box.common.Snippets.collateTextForOps(Iterable<DocOp>) - * - * replaced white space character with new line - */ - /** - * Concatenates all of the text of the specified docops into a single String. - * - * @param documentops the document operations to concatenate. - * @return A String containing the characters from the operations. - */ - public static String readText(ReadableBlipData doc) { - - final StringBuilder resultBuilder = new StringBuilder(); - - DocOp docOp = doc.getContent().asOperation(); - docOp.apply(InitializationCursorAdapter.adapt(new DocOpCursor() { - @Override - public void characters(String s) { - resultBuilder.append(s); - } - - @Override - public void annotationBoundary(AnnotationBoundaryMap map) { - } - - @Override - public void elementStart(String type, Attributes attrs) { - if (type.equals(DocumentConstants.LINE)) { - resultBuilder.append("\n"); - } - } - - @Override - public void elementEnd() { - } - - @Override - public void retain(int itemCount) { - } - - @Override - public void deleteCharacters(String chars) { - } - - @Override - public void deleteElementStart(String type, Attributes attrs) { - } - - @Override - public void deleteElementEnd() { - } - - @Override - public void replaceAttributes(Attributes oldAttrs, Attributes newAttrs) { - } - - @Override - public void updateAttributes(AttributesUpdate attrUpdate) { - } - })); - - return resultBuilder.toString(); - } @Inject public SolrWaveIndexerImpl(WaveMap waveMap, WaveletProvider waveletProvider, @@ -253,7 +185,17 @@ PerUserWaveViewBus.Listener { continue; } - String text = readText(document); + Iterable<DocInitialization> ops = Lists.newArrayList( + document.getContent().asOperation()); + String text = Snippets.collateTextForOps(ops, new Function<StringBuilder, Void>() { + + @Override + public Void apply(StringBuilder resultBuilder) { + resultBuilder.append("\n"); + return null; + } + + }); JsonArray participantsJson = new JsonArray(); for (ParticipantId participant : wavelet.getParticipants()) {
