📄 cmstemplatesearch.java
字号:
}
/**
* Builds the html for the search result list for a single page.<p>
*
* @param results the list of result objects to display
* @return the html for the search result list
*/
public String buildSearchResultList(List results) {
StringBuffer result = new StringBuffer(128);
Iterator iterator = results.iterator();
while (iterator.hasNext()) {
// create the output for a single result
CmsSearchResult entry = (CmsSearchResult)iterator.next();
result.append("<div class=\"searchresult\">");
String path = entry.getPath();
// remove the site root from the path of the result
path = getRequestContext().removeSiteRoot(path);
// get the file icon
String fileIcon = getFileIcon(path);
if (CmsStringUtil.isNotEmpty(fileIcon)) {
result.append("<a href=\"");
result.append(link(path));
result.append("\">");
result.append(fileIcon);
result.append("</a> ");
}
result.append("<a href=\"");
result.append(link(path));
result.append("\">");
String title = entry.getTitle();
if (CmsStringUtil.isEmpty(title)) {
// title is not set, show file name instead
title = CmsResource.getName(path);
}
result.append(title);
result.append("</a> (");
result.append(entry.getScore());
result.append("%)<br>");
if (entry.getExcerpt() != null) {
// add the excerpt
result.append(entry.getExcerpt());
}
if (entry.getKeywords() != null) {
// add the keywords
result.append("<br>");
result.append(key("search.keywords"));
result.append(": ");
result.append(entry.getKeywords());
}
if (entry.getDescription() != null) {
// add the file description
result.append("<br>");
result.append(key("search.description"));
result.append(": ");
result.append(entry.getDescription());
}
// add the last modification date of the result
result.append("<br>");
result.append(messages().getDateTime(entry.getDateLastModified().getTime()));
result.append("</div>\n");
}
return result.toString();
}
/**
* Returns the HTML of the file icon for the given resource name or an empty String if no icon can be found.<p>
*
* @param fileName the filename to check
* @return the HTML of the file icon for the given resource name or an empty String
*/
public String getFileIcon(String fileName) {
int lastDot = fileName.lastIndexOf('.');
String extension = "";
// get the file extension
if ((lastDot > 0) && (lastDot < (fileName.length() - 1))) {
extension = fileName.substring(lastDot + 1).toLowerCase();
String iconPath = CmsWorkplace.VFS_PATH_MODULES
+ MODULE_NAME
+ "/resources/icons/ic_app_"
+ extension
+ ".gif";
// check if an icon exists
if (getCmsObject().existsResource(iconPath)) {
StringBuffer result = new StringBuffer(8);
String title = property(CmsPropertyDefinition.PROPERTY_TITLE, iconPath, "");
result.append("<img src=\"");
result.append(link(iconPath));
result.append("\" border=\"0\" alt=\"");
result.append(title);
result.append("\" title=\"");
result.append(title);
result.append("\" align=\"left\" hspace=\"2\">");
return result.toString();
}
}
return "";
}
/**
* Returns the URI of the page calling the search result page.<p>
*
* @return the URI of the page calling the search result page
*/
public String getPageUri() {
return m_pageUri;
}
/**
* Returns the "checked" attribute String if the user checked the "search all" checkbox.<p>
*
* @return the "checked" attribute String or an empty String
*/
public String getSearchAllChecked() {
if (isSearchAll() || "/".equals(getStartFolder())) {
return " checked=\"checked\"";
}
return "";
}
/**
* Returns the list of search results depending on the search root and the form data.<p>
*
* Either returns the results of the entire website or of the search root.<p>
*
* @param search the instanciated search object
* @return the results of the entire website or of the search root
*/
public List getSearchResults(CmsSearch search) {
List result;
if (isSearchAll()) {
// set search root to root folder to get all search results
search.setSearchRoot("/");
} else {
// set search root to start folder
search.setSearchRoot(getStartFolder());
}
String queryString = search.getQuery();
try {
if (CmsStringUtil.isEmptyOrWhitespaceOnly(queryString)) {
// no query String found, return empty list
queryString = "";
search.setQuery("");
result = new ArrayList();
} else {
// first set request context URI to form URI to obtain right page links
getRequestContext().setUri(CmsWorkplace.VFS_PATH_MODULES + MODULE_NAME + "/pages/search.html");
result = search.getSearchResult();
}
} finally {
// reset URI to page
getRequestContext().setUri(getPageUri());
}
return result;
}
/**
* Includes the specified template element with the page URI specified in the request parameter "uri".<p>
*
* After inclusion, the request context URI is reset to the old value.<p>
*
* @param element the element (template selector) to display from the target template
* @throws JspException if including the target fails
*/
public void includeWithPageUri(String element) throws JspException {
String template = property(CmsPropertyDefinition.PROPERTY_TEMPLATE, "search", null);
// include target
include(template, element);
}
/**
* Initialize this bean with the current page context, request and response.<p>
*
* It is required to call one of the init() methods before you can use the
* instance of this bean.
*
* @param context the JSP page context object
* @param req the JSP request
* @param res the JSP response
*/
public void init(PageContext context, HttpServletRequest req, HttpServletResponse res) {
// call initialization of super class
super.init(context, req, res);
// initialize members from request
m_pageUri = req.getParameter(CmsTemplateBean.PARAM_URI);
if (m_pageUri == null) {
m_pageUri = getRequestContext().getUri();
}
m_searchAll = Boolean.valueOf(req.getParameter(PARAM_SEARCHALL)).booleanValue();
// change URI to point at page that called the search
getRequestContext().setUri(m_pageUri);
}
/**
* Returns true if the entire website should be searched.<p>
*
* @return true if the entire website should be searched, otherwise false
*/
public boolean isSearchAll() {
return m_searchAll;
}
/**
* Returns true if the checkbox to search the entire website should be displayed.<p>
*
* @return true if the checkbox to search the entire website should be displayed, otherwise false
*/
public boolean isSearchAllDisplayed() {
return !"/".equals(getStartFolder());
}
/**
* Sets if the entire website should be searched.<p>
*
* @param searchAll true if the entire website should be searched, otherwise false
*/
public void setSearchAll(boolean searchAll) {
m_searchAll = searchAll;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -