use of ddf.util.XPathHelper in project ddf by codice.
the class SearchCommand method executeSearchStore.
private Object executeSearchStore(Filter filter) throws Exception {
String formatString = "%1$-33s %2$-26s %3$-" + TITLE_MAX_LENGTH + "s %4$-" + EXCERPT_MAX_LENGTH + "s%n";
CatalogFacade catalogProvider = getCatalog();
QueryImpl query = new QueryImpl(filter);
query.setRequestsTotalResultsCount(true);
if (numberOfItems > -1) {
query.setPageSize(numberOfItems);
}
long start = System.currentTimeMillis();
SourceResponse response = catalogProvider.query(new QueryRequestImpl(query));
long end = System.currentTimeMillis();
int size = 0;
if (response.getResults() != null) {
size = response.getResults().size();
}
console.println();
console.printf(" %d result(s) out of %s%d%s in %3.3f seconds", (size), Ansi.ansi().fg(Ansi.Color.CYAN).toString(), response.getHits(), Ansi.ansi().reset().toString(), (end - start) / MS_PER_SECOND);
console.printf(formatString, "", "", "", "");
printHeaderMessage(String.format(formatString, ID, DATE, TITLE, EXCERPT));
for (Result result : response.getResults()) {
Metacard metacard = result.getMetacard();
String title = (metacard.getTitle() != null ? metacard.getTitle() : "N/A");
String excerpt = "N/A";
String modifiedDate = "";
if (searchPhrase != null) {
if (metacard.getMetadata() != null) {
XPathHelper helper = new XPathHelper(metacard.getMetadata());
String indexedText = helper.getDocument().getDocumentElement().getTextContent();
indexedText = indexedText.replaceAll("\\r\\n|\\r|\\n", " ");
String normalizedSearchPhrase = searchPhrase.replaceAll("\\*", "");
int index = -1;
if (caseSensitive) {
index = indexedText.indexOf(normalizedSearchPhrase);
} else {
index = indexedText.toLowerCase().indexOf(normalizedSearchPhrase.toLowerCase());
}
if (index != -1) {
int contextLength = (EXCERPT_MAX_LENGTH - normalizedSearchPhrase.length() - 8) / 2;
excerpt = "..." + indexedText.substring(Math.max(index - contextLength, 0), index);
excerpt = excerpt + Ansi.ansi().fg(Ansi.Color.GREEN).toString();
excerpt = excerpt + indexedText.substring(index, index + normalizedSearchPhrase.length());
excerpt = excerpt + Ansi.ansi().reset().toString();
excerpt = excerpt + indexedText.substring(index + normalizedSearchPhrase.length(), Math.min(indexedText.length(), index + normalizedSearchPhrase.length() + contextLength)) + "...";
}
}
}
if (metacard.getModifiedDate() != null) {
modifiedDate = new DateTime(metacard.getModifiedDate().getTime()).toString(DATETIME_FORMATTER);
}
console.printf(formatString, metacard.getId(), modifiedDate, title.substring(0, Math.min(title.length(), TITLE_MAX_LENGTH)), excerpt);
}
return null;
}
use of ddf.util.XPathHelper in project ddf by codice.
the class XPathHelperTest method testXPathHelperWithNamespaceTextPath.
@Test
public void testXPathHelperWithNamespaceTextPath() throws Exception {
try {
String xmlString = getFileContentsAsString(TEST_DATA_PATH + INPUT_FILE);
XPathHelper xHelper = new XPathHelper(xmlString);
NodeList nodeList = (NodeList) xHelper.evaluate("//abc:fileTitle", XPathConstants.NODESET, new MockNamespaceResolver());
LOGGER.debug("testXPathHelper_WithNamespaceTextPath() - nodeList length = {}", nodeList.getLength());
assertEquals(1, nodeList.getLength());
} catch (Exception e1) {
LOGGER.error("Exception thrown during testXPathHelper_WithNamespaceTextPath", e1);
}
}
use of ddf.util.XPathHelper in project ddf by codice.
the class XPathHelperTest method testXPathHelperNoTitle.
@Test
public void testXPathHelperNoTitle() throws Exception {
try {
String xmlString = getFileContentsAsString(TEST_DATA_PATH + "IngestMetadata_NoTitle.xml");
XPathHelper xHelper = new XPathHelper(xmlString);
String title = (String) xHelper.evaluate("//ns1:title", new MockNamespaceResolver());
LOGGER.debug("testXPathHelper_NoTitle() - title = [{}]", title);
assertNotNull(title);
assertTrue(title.length() == 0);
} catch (XPathExpressionException e1) {
LOGGER.error("Exception thrown during testXPathHelper_NoTitle", e1);
}
}
use of ddf.util.XPathHelper in project ddf by codice.
the class XPathHelperTest method testXPathHelperWithDocument.
@Test
public void testXPathHelperWithDocument() throws Exception {
try {
Document document = getDocument(INPUT_FILE);
XPathHelper xHelper = new XPathHelper(document);
NodeList nodeList = (NodeList) xHelper.evaluate(XPATH_EXPRESSION, XPathConstants.NODESET, new MockNamespaceResolver());
LOGGER.debug("testXPathHelper_WithDocument() - nodeList length = {}", nodeList.getLength());
assertEquals(6, nodeList.getLength());
} catch (Exception e1) {
LOGGER.error("Exception thrown during testXPathHelper_WithDocument", e1);
}
}
use of ddf.util.XPathHelper in project ddf by codice.
the class XPathHelperTest method testXPathHelperWithXmlFile.
@Test
public void testXPathHelperWithXmlFile() throws Exception {
try {
String xmlString = getFileContentsAsString(TEST_DATA_PATH + INPUT_FILE);
XPathHelper xHelper = new XPathHelper(xmlString);
NodeList nodeList = (NodeList) xHelper.evaluate(XPATH_EXPRESSION, XPathConstants.NODESET, new MockNamespaceResolver());
LOGGER.debug("testXPathHelper_WithXmlFile() - nodeList length = {}", nodeList.getLength());
assertEquals(6, nodeList.getLength());
} catch (Exception e1) {
LOGGER.error("Exception thrown during testXPathHelper_WithXmlFile", e1);
}
}
Aggregations