Search in sources :

Example 1 with XPathHelper

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;
}
Also used : QueryImpl(ddf.catalog.operation.impl.QueryImpl) Metacard(ddf.catalog.data.Metacard) SourceResponse(ddf.catalog.operation.SourceResponse) XPathHelper(ddf.util.XPathHelper) QueryRequestImpl(ddf.catalog.operation.impl.QueryRequestImpl) CatalogFacade(org.codice.ddf.commands.catalog.facade.CatalogFacade) DateTime(org.joda.time.DateTime) Result(ddf.catalog.data.Result)

Example 2 with XPathHelper

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);
    }
}
Also used : XPathHelper(ddf.util.XPathHelper) NodeList(org.w3c.dom.NodeList) XPathExpressionException(javax.xml.xpath.XPathExpressionException) Test(org.junit.Test)

Example 3 with XPathHelper

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);
    }
}
Also used : XPathHelper(ddf.util.XPathHelper) XPathExpressionException(javax.xml.xpath.XPathExpressionException) Test(org.junit.Test)

Example 4 with XPathHelper

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);
    }
}
Also used : XPathHelper(ddf.util.XPathHelper) NodeList(org.w3c.dom.NodeList) Document(org.w3c.dom.Document) XPathExpressionException(javax.xml.xpath.XPathExpressionException) Test(org.junit.Test)

Example 5 with XPathHelper

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);
    }
}
Also used : XPathHelper(ddf.util.XPathHelper) NodeList(org.w3c.dom.NodeList) XPathExpressionException(javax.xml.xpath.XPathExpressionException) Test(org.junit.Test)

Aggregations

XPathHelper (ddf.util.XPathHelper)10 XPathExpressionException (javax.xml.xpath.XPathExpressionException)8 Test (org.junit.Test)6 NodeList (org.w3c.dom.NodeList)6 Document (org.w3c.dom.Document)3 Metacard (ddf.catalog.data.Metacard)2 Result (ddf.catalog.data.Result)2 Element (org.w3c.dom.Element)2 Node (org.w3c.dom.Node)2 BinaryContent (ddf.catalog.data.BinaryContent)1 SourceResponse (ddf.catalog.operation.SourceResponse)1 QueryImpl (ddf.catalog.operation.impl.QueryImpl)1 QueryRequestImpl (ddf.catalog.operation.impl.QueryRequestImpl)1 CatalogTransformerException (ddf.catalog.transform.CatalogTransformerException)1 QueryResponseTransformer (ddf.catalog.transform.QueryResponseTransformer)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1