diff options
author | adrianocrestani <adrianocrestani@13f79535-47bb-0310-9956-ffa450edef68> | 2009-07-05 07:04:56 +0000 |
---|---|---|
committer | adrianocrestani <adrianocrestani@13f79535-47bb-0310-9956-ffa450edef68> | 2009-07-05 07:04:56 +0000 |
commit | 7d544d6b11dc6494170c293e6897ed6d72612d06 (patch) | |
tree | 18f24b6d896fcf0c8cfa5a6a041df082dd7672f3 /branches/sca-java-1.x/modules/domain-search/src/main/java/org/apache/tuscany/sca/domain/search/impl/FileContentResultProcessor.java | |
parent | b4f758f88e8c97be870ba86080d7f0adb6dafb65 (diff) |
applying patch tuscany_2552_phillipe_ramalho_07_04_2009.patch.txt from TUSCANY-2552
git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@791203 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to '')
-rw-r--r-- | branches/sca-java-1.x/modules/domain-search/src/main/java/org/apache/tuscany/sca/domain/search/impl/FileContentResultProcessor.java | 116 |
1 files changed, 116 insertions, 0 deletions
diff --git a/branches/sca-java-1.x/modules/domain-search/src/main/java/org/apache/tuscany/sca/domain/search/impl/FileContentResultProcessor.java b/branches/sca-java-1.x/modules/domain-search/src/main/java/org/apache/tuscany/sca/domain/search/impl/FileContentResultProcessor.java new file mode 100644 index 0000000000..e20a170e19 --- /dev/null +++ b/branches/sca-java-1.x/modules/domain-search/src/main/java/org/apache/tuscany/sca/domain/search/impl/FileContentResultProcessor.java @@ -0,0 +1,116 @@ +package org.apache.tuscany.sca.domain.search.impl; + +import java.io.File; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.IOException; +import java.io.InputStreamReader; +import java.io.Reader; +import java.util.Enumeration; +import java.util.zip.ZipEntry; +import java.util.zip.ZipFile; + +import org.apache.lucene.document.Document; +import org.apache.lucene.search.Query; +import org.apache.tuscany.sca.domain.search.Result; +import org.apache.tuscany.sca.domain.search.ResultFactory; +import org.apache.tuscany.sca.domain.search.ResultProcessor; + +public class FileContentResultProcessor implements ResultProcessor { + + public static class FileContentResultFactory implements + ResultFactory<FileContentResult> { + + public FileContentResultFactory() { + // empty constructor + } + + public FileContentResult createResult(String name) { + return new FileContentResult(name); + } + + public String getName(Document document) { + return document.get(SearchFields.FILE_FIELD); + } + + public String getType() { + return SearchFields.FILE_FIELD; + } + + public FileContentResult createResult(Document document) { + String name = getName(document); + + if (name != null) { + return createResult(name); + } + + return null; + + } + + } + + public Result process(Document document, Query query, Result result) { + + if (result instanceof FileContentResult) { + FileContentResult fileContentResult = (FileContentResult) result; + Reader reader; + + ParentField parentField = new ParentField(document + .get(SearchFields.PARENT_FIELD)); + String parentURI = parentField.getElementURI(parentField + .getElementsCount() - 1); + String name = document.get(SearchFields.FILE_FIELD); + + if (name == null) { + return result; + } + + if (parentURI.indexOf(ZipDocumentProcessor.ARCHIVE_SEPARATOR) != -1) { + String[] locations = parentURI.split(Character + .toString(ZipDocumentProcessor.ARCHIVE_SEPARATOR)); + + try { + ZipFile zip = new ZipFile(new File(locations[0])); + + // TODO: normalize the entry path and check if the entry still exists + ZipEntry entry = zip.getEntry(locations[1].substring(1) + name); + + Enumeration<? extends ZipEntry> entries = zip.entries(); + + while (entries.hasMoreElements()) System.out.println(entries.nextElement().getName()); + + reader = new InputStreamReader(zip.getInputStream(entry)); + + } catch (IOException e) { + return result; + } + + } else { + + try { + reader = new InputStreamReader(new FileInputStream( + new File(parentURI + '/' + name))); + + } catch (FileNotFoundException e) { + return result; + } + + } + + try { + fileContentResult.setContent(HighlightingUtil + .bestFragmentHighlighted(query, reader)); + + } catch (IOException e) { + // ignore content loading, TODO: maybe it return an error + // message as the content + } + + } + + return result; + + } + +} |