Skip to content
This repository has been archived by the owner on May 4, 2023. It is now read-only.

Commit

Permalink
use new parseToString for simpler handling.
Browse files Browse the repository at this point in the history
  • Loading branch information
Robert Newson committed Dec 21, 2009
1 parent 864375b commit 0e03eb2
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 19 deletions.
18 changes: 6 additions & 12 deletions src/main/java/com/github/rnewson/couchdb/lucene/Tika.java
Expand Up @@ -20,12 +20,11 @@

import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;

import org.apache.commons.io.IOUtils;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.apache.lucene.document.Document;
import org.apache.tika.exception.TikaException;
import org.apache.tika.metadata.DublinCore;
import org.apache.tika.metadata.HttpHeaders;
import org.apache.tika.metadata.Metadata;
Expand All @@ -47,23 +46,18 @@ public void parse(final InputStream in, final String contentType, final String f
throws IOException {
final Metadata md = new Metadata();
md.set(HttpHeaders.CONTENT_TYPE, contentType);
final Reader reader = tika.parse(in, md);

final String body;
try {
try {
body = IOUtils.toString(reader);
} finally {
reader.close();
}
// Add body text.
doc.add(text(fieldName, tika.parseToString(in, md), false));
} catch (final IOException e) {
log.warn("Failed to index an attachment.", e);
return;
} catch (final TikaException e) {
log.warn("Failed to parse an attachment.", e);
return;
}

// Add body text.
doc.add(text(fieldName, body, false));

// Add DC attributes.
addDublinCoreAttributes(md, doc);
}
Expand Down
Expand Up @@ -99,7 +99,7 @@ public static void jsFunction_add(final Context cx, final Scriptable thisObj, fi
if (args[0] == null) {
throw Context.reportRuntimeError("first argument must be non-null.");
}

if (args[0] instanceof Undefined) {
// Ignore
return;
Expand Down Expand Up @@ -186,12 +186,7 @@ private void addAttachment(final RhinoAttachment attachment, final String id, fi

public Void handleResponse(final HttpResponse response) throws ClientProtocolException, IOException {
final HttpEntity entity = response.getEntity();
final InputStream in = entity.getContent();
try {
Tika.INSTANCE.parse(in, entity.getContentType().getValue(), attachment.fieldName, out);
} finally {
in.close();
}
Tika.INSTANCE.parse(entity.getContent(), entity.getContentType().getValue(), attachment.fieldName, out);
return null;
}
};
Expand Down

0 comments on commit 0e03eb2

Please sign in to comment.