Filter example - string document filter
This simple example shows the code for a simple filter plugin, which implements a string document filter.
The example below shows a simple filter implementation and corresponding tests.
Example
In this example plain text documents (based on having a .txt
file extension) have their content converted to lower case.
The example implements the filter and filter tests
This example implements the StringDocumentFilter
. We are required to implement canFilter()
, (pre filter check) used to check the document extension is .txt
, as well as filterAsStringDocument()
which contains the logic for the filter.
This example also has shows simple test methods.
DocumentFilterLowercaseTextDocument.java
package com.example.pluginexamples;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import com.funnelback.filter.api.FilterContext;
import com.funnelback.filter.api.FilterResult;
import com.funnelback.filter.api.documents.NoContentDocument;
import com.funnelback.filter.api.documents.StringDocument;
import com.funnelback.filter.api.filters.PreFilterCheck;
import com.funnelback.filter.api.filters.StringDocumentFilter;
public class DocumentFilterLowercaseTextDocument implements StringDocumentFilter {
private static final Logger log = LogManager.getLogger(DocumentFilterLowercaseTextDocument.class);
@Override
public PreFilterCheck canFilter(NoContentDocument document, FilterContext filterContext) {
//Only lower case documents that end in ".txt" for example file://file/share/foo.txt
if(document.getURI().getPath().endsWith(".txt")) { (1)
return PreFilterCheck.ATTEMPT_FILTER;
}
log.debug("Skipping filter as the URL path of: '"+document.getURI() + "' does not end with '.txt'");
return PreFilterCheck.SKIP_FILTER;
}
@Override
public FilterResult filterAsStringDocument(StringDocument document, FilterContext filterContext) {
//Create a lower cased copy of the content
String lowerCasedContent = document.getContentAsString().toLowerCase(); (2)
//Create a new document with the new lower cased content.
StringDocument filteredDocument = document.cloneWithStringContent(document.getDocumentType(), lowerCasedContent); (3)
return FilterResult.of(filteredDocument); (4)
}
}
1 | Run the filter if the document that we are processing has a .txt file extension. |
2 | Reads the document content as a string and converts it to lowercase. |
3 | Clones the document and updates it with the new (lower-cased) content. |
4 | Returns the cloned document, which is passed to the next filter in the chain. |
The following Java code implements unit tests for the filter above.
DocumentFilterLowercaseTextDocumentTest.java
package com.example.pluginexamples;
import org.junit.Assert;
import org.junit.Test;
import com.funnelback.filter.api.FilterResult;
import com.funnelback.filter.api.documents.StringDocument;
import com.funnelback.filter.api.mock.MockDocuments;
import com.funnelback.filter.api.mock.MockFilterContext;
import java.net.URI;
import static com.funnelback.filter.api.DocumentType.*;
public class DocumentFilterLowercaseTextDocumentTest {
@Test
public void lowerCasingTest() throws Exception {
MockFilterContext mockContext = MockFilterContext.getEmptyContext();
//Create a txt document with all upper case letters.
StringDocument inputDoc = MockDocuments.mockEmptyStringDoc()
.cloneWithURI(new URI("file://file/share/foo.txt"))
.cloneWithStringContent(MIME_UNKNOWN, "HELLO");
//Create and run the filter.
DocumentFilterLowercaseTextDocument lcFilterTest = new DocumentFilterLowercaseTextDocument();
FilterResult res = lcFilterTest.filter(inputDoc, mockContext);
//Get the resulting document.
StringDocument filteredDocument = (StringDocument) res.getFilteredDocuments().get(0);
Assert.assertEquals("Content should have been converted to lower case",
"hello", filteredDocument.getContentAsString());
}
@Test
public void nonTxtDocumentsAreSkippedTest() throws Exception {
MockFilterContext mockContext = MockFilterContext.getEmptyContext();
//Create a txt document with all upper case letters.
StringDocument inputDoc = MockDocuments.mockEmptyStringDoc()
.cloneWithURI(new URI("file://file/share/foo.cfg"))
.cloneWithStringContent(MIME_UNKNOWN, "Hi");
//Create and run the filter.
DocumentFilterLowercaseTextDocument skipFilterTest = new DocumentFilterLowercaseTextDocument();
FilterResult res = skipFilterTest.filter(inputDoc, mockContext);
Assert.assertTrue("Filter should not have been run on a file which does not have '.txt' extension",
res.isSkipped());
}
}