Search in sources :

Example 1 with SearchResult

use of org.apache.wiki.search.SearchResult in project jspwiki by apache.

the class Search method renderResults.

private String renderResults(Collection<SearchResult> results, WikiContext context, int maxItems) {
    WikiEngine engine = context.getEngine();
    Element table = XhtmlUtil.element(XHTML.table);
    // table.setAttribute(XHTML.ATTR_border,"0");
    // table.setAttribute(XHTML.ATTR_cellpadding,"4");
    table.setAttribute(XHTML.ATTR_class, "wikitable search-result");
    Element row = XhtmlUtil.element(XHTML.tr);
    table.addContent(row);
    Element th1 = XhtmlUtil.element(XHTML.th, "Page");
    th1.setAttribute(XHTML.ATTR_width, "30%");
    th1.setAttribute(XHTML.ATTR_align, "left");
    row.addContent(th1);
    Element th2 = XhtmlUtil.element(XHTML.th, "Score");
    th2.setAttribute(XHTML.ATTR_align, "left");
    row.addContent(th2);
    int idx = 0;
    for (Iterator<SearchResult> i = results.iterator(); i.hasNext() && idx++ <= maxItems; ) {
        SearchResult sr = i.next();
        row = XhtmlUtil.element(XHTML.tr);
        Element name = XhtmlUtil.element(XHTML.td);
        name.setAttribute(XHTML.ATTR_width, "30%");
        name.addContent(XhtmlUtil.link(context.getURL(WikiContext.VIEW, sr.getPage().getName()), engine.beautifyTitle(sr.getPage().getName())));
        row.addContent(name);
        row.addContent(XhtmlUtil.element(XHTML.td, "" + sr.getScore()));
        table.addContent(row);
    }
    if (results.isEmpty()) {
        row = XhtmlUtil.element(XHTML.tr);
        Element td = XhtmlUtil.element(XHTML.td);
        td.setAttribute(XHTML.ATTR_colspan, "2");
        Element b = XhtmlUtil.element(XHTML.b, "No results");
        td.addContent(b);
        row.addContent(td);
        table.addContent(row);
    }
    return XhtmlUtil.serialize(table);
}
Also used : Element(org.jdom2.Element) SearchResult(org.apache.wiki.search.SearchResult) WikiEngine(org.apache.wiki.WikiEngine)

Example 2 with SearchResult

use of org.apache.wiki.search.SearchResult in project jspwiki by apache.

the class SearchResultIteratorTag method nextResult.

private int nextResult() {
    if (m_iterator != null && m_iterator.hasNext() && m_count++ < m_maxItems) {
        SearchResult r = (SearchResult) m_iterator.next();
        // Create a wiki context for the result
        WikiEngine engine = m_wikiContext.getEngine();
        HttpServletRequest request = m_wikiContext.getHttpRequest();
        Command command = PageCommand.VIEW.targetedCommand(r.getPage());
        WikiContext context = new WikiContext(engine, request, command);
        // Stash it in the page context
        pageContext.setAttribute(WikiTagBase.ATTR_CONTEXT, context, PageContext.REQUEST_SCOPE);
        pageContext.setAttribute(getId(), r);
        return EVAL_BODY_BUFFERED;
    }
    return SKIP_BODY;
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) WikiContext(org.apache.wiki.WikiContext) Command(org.apache.wiki.ui.Command) PageCommand(org.apache.wiki.ui.PageCommand) SearchResult(org.apache.wiki.search.SearchResult) WikiEngine(org.apache.wiki.WikiEngine)

Example 3 with SearchResult

use of org.apache.wiki.search.SearchResult in project jspwiki by apache.

the class AbstractFileProvider method findPages.

/**
 * Iterates through all WikiPages, matches them against the given query,
 * and returns a Collection of SearchResult objects.
 *
 * @param query {@inheritDoc}
 * @return {@inheritDoc}
 */
public Collection findPages(QueryItem[] query) {
    File wikipagedir = new File(m_pageDirectory);
    TreeSet<SearchResult> res = new TreeSet<SearchResult>(new SearchResultComparator());
    SearchMatcher matcher = new SearchMatcher(m_engine, query);
    File[] wikipages = wikipagedir.listFiles(new WikiFileFilter());
    for (int i = 0; i < wikipages.length; i++) {
        FileInputStream input = null;
        // log.debug("Searching page "+wikipages[i].getPath() );
        String filename = wikipages[i].getName();
        int cutpoint = filename.lastIndexOf(FILE_EXT);
        String wikiname = filename.substring(0, cutpoint);
        wikiname = unmangleName(wikiname);
        try {
            input = new FileInputStream(wikipages[i]);
            String pagetext = FileUtil.readContents(input, m_encoding);
            SearchResult comparison = matcher.matchPageContent(wikiname, pagetext);
            if (comparison != null) {
                res.add(comparison);
            }
        } catch (IOException e) {
            log.error("Failed to read " + filename, e);
        } finally {
            IOUtils.closeQuietly(input);
        }
    }
    return res;
}
Also used : TreeSet(java.util.TreeSet) SearchResultComparator(org.apache.wiki.search.SearchResultComparator) SearchResult(org.apache.wiki.search.SearchResult) IOException(java.io.IOException) File(java.io.File) FileInputStream(java.io.FileInputStream) SearchMatcher(org.apache.wiki.search.SearchMatcher)

Aggregations

SearchResult (org.apache.wiki.search.SearchResult)3 WikiEngine (org.apache.wiki.WikiEngine)2 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 IOException (java.io.IOException)1 TreeSet (java.util.TreeSet)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 WikiContext (org.apache.wiki.WikiContext)1 SearchMatcher (org.apache.wiki.search.SearchMatcher)1 SearchResultComparator (org.apache.wiki.search.SearchResultComparator)1 Command (org.apache.wiki.ui.Command)1 PageCommand (org.apache.wiki.ui.PageCommand)1 Element (org.jdom2.Element)1