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);
}
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;
}
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;
}
Aggregations