📄 search_engine.java
字号:
//// ____.// __/\ ______| |__/\. _______// __ .____| | \ | +----+ \// _______| /--| | | - \ _ | : - \_________// \\______: :---| : : | : | \________>// |__\---\_____________:______: :____|____:_____\// /_____|//// . . . i n j a h i a w e t r u s t . . .////// Search_Engine// DJ 20.02.2001//// getInstance()// authoriseRender()// renderLink()// needsJahiaData()// handleActions()//package org.jahia.engines.search;import java.util.*; // HashMapimport javax.servlet.http.*; // HttpSessionimport org.jahia.exceptions.*; // JahiaExceptionimport org.jahia.utils.*; // JahiaConsoleimport org.jahia.data.*; // JahiaData, ConnectionTypesimport org.jahia.params.*; // ParamBeanimport org.jahia.registries.*; // ServicesRegistryimport org.jahia.engines.*; // JahiaEngine interfaceimport org.jahia.data.events.*; // JahiaEventimport org.jahia.services.usermanager.*; // JahiaUserManagerimport org.jahia.services.search.*;import org.jahia.data.search.*;/** * This engine is called when the user wants to do a search on the jahia site. It displays * sorted results using a JSP output which leaves every aspect of the search results design * on the JSP coder. * * This engine doesn't implement any search algorithm, it calls the JahiaSearchService for * that. Instead, this engine is here for interaction between the user and the service. * * The JSP template uses searchResult attribute to get the search results, and searchString * to see what the user searched for. * * @see org.jahia.services.search.JahiaSearchService * @author David Jilli */public class Search_Engine implements JahiaEngine { private static final String TEMPLATE_JSP = "/jsp/jahia/engines/search/searchresult.jsp"; private static final String SEARCH_JSP_NAME = "searchresult.jsp"; private static Search_Engine theObject = null; private String engineName = "search"; private EngineToolBox toolBox; /*** * constructor * */ private Search_Engine() { // JahiaConsole.println( "Engine", "***** Starting Search Engine *****" ); toolBox = EngineToolBox.getInstance(); } // end constructor /*** * returns a single instance of the object * */ public static synchronized Search_Engine getInstance() { if (theObject == null) { theObject = new Search_Engine(); } return theObject; } // end getInstance /*** * authorises engine render * */ public boolean authoriseRender( ParamBean jParams ) { return true; // always allowed to render search } // end authoriseRender /*** * renders link to pop-up window * */ public String renderLink( ParamBean jParams, Object theObj ) throws JahiaException { String theUrl = jParams.composeEngineUrl( engineName, "" ); if (theObj != null) theUrl = theUrl + theObj;/* if (theObj != null) { int pos1 = theUrl.indexOf( "&pid=" ); int pos2 = theUrl.indexOf( "&", pos1 + 1 ); pos2 = (pos2 > -1) ? pos2 : theUrl.length(); theUrl = theUrl.substring(0,pos1) + theUrl.substring(pos2,theUrl.length()) + theObj.toString(); }*/ return jParams.getResponse().encodeURL(theUrl); } // end renderLink /*** * specifies if the engine needs the JahiaData object * */ public boolean needsJahiaData( ParamBean jParams ) { return true; } // end needsJahiaData /*** * handles the engine actions * * @param jParams a ParamBean object * @param jData a JahiaData object (not mandatory) * */ public void handleActions( ParamBean jParams, JahiaData jData ) throws JahiaException { // initalizes the hashmap// HashMap engineMap = new HashMap(); HashMap engineMap = (HashMap)jParams.getRequest().getAttribute( "engineMap" ); if (engineMap == null) engineMap = new HashMap(); processScreen( jParams, jData, engineMap ); // displays the screen toolBox.displayScreen( jParams, engineMap ); } // end handleActions /*** * prepares the screen requested by the user * * @param jParams a ParamBean object * */ public void processScreen( ParamBean jParams, JahiaData jData, HashMap engineMap ) throws JahiaException { // check if a searchresult template exists in the template // directory, else uses the standard template found in the // jsp/engines directory String theTemplate = new String(TEMPLATE_JSP); String fileName = null; if ((jParams!=null) && (jParams.getPage()!=null) && (jParams.getPage().getPageTemplate()!=null)) { fileName = jParams.getPage().getPageTemplate().getSourcePath(); if (fileName.lastIndexOf ("/") != -1) { fileName = fileName.substring (0, fileName.lastIndexOf("/")+1) + SEARCH_JSP_NAME; JahiaConsole.println ("Search_Engine", "Trying to redirect search result to : "+fileName); theTemplate = fileName; } } String theScreen = jParams.getRequest().getParameter( "screen" ); if (theScreen == null) { theScreen = "execute"; } if (jParams.getRequest().getParameter( "from" ) != null) { engineMap.put ("searchResultFrom", Integer.valueOf(jParams.getRequest().getParameter("from"))); } if (engineMap.get ( "searchResultFrom" ) == null) { engineMap.put ("searchResultFrom", new Integer(1)); } String searchString = (String)engineMap.get ("searchString"); if (searchString == null) { searchString = jParams.getRequest().getParameter ("search"); } if(searchString == null) { searchString = ""; } else { searchString = searchString.trim(); } Integer searchFrom = ((Integer)engineMap.get ("searchResultFrom")); if (searchFrom == null) { if (jParams.getRequest().getParameter("from")!=null) { searchFrom = Integer.valueOf(jParams.getRequest().getParameter("from")); } } if (searchFrom == null) { searchFrom = new Integer(1); } Integer searchShow = ((Integer)engineMap.get ("searchShow")); if (searchShow == null) { if (jParams.getRequest().getParameter("show")!=null) { searchShow = Integer.valueOf(jParams.getRequest().getParameter("show")); } } //JahiaConsole.println ("Search_Engine","searchString : > " + (String)engineMap.get ("searchString")); // previous screen if (theScreen.equals("prev")) { if (engineMap.get("searchResultFrom")!=null) engineMap.put ("searchResultFrom", new Integer(((searchFrom.intValue()-searchShow.intValue()<1)?1:(searchFrom.intValue()-searchShow.intValue())) )); searchFrom= new Integer(searchFrom.intValue()-searchShow.intValue()); } // next screen if (theScreen.equals("next")) { if (engineMap.get("searchResultFrom")!=null) engineMap.put ("searchResultFrom", new Integer(searchFrom.intValue()+searchShow.intValue())); searchFrom= new Integer(searchFrom.intValue()+searchShow.intValue()); } String showStr=""; if (searchShow!=null) showStr = "&show="+searchShow; engineMap.put( "searchPrevUrl", renderLink(jParams,showStr+"&screen=prev&search="+searchString+"&from="+searchFrom.intValue()) ); engineMap.put( "searchNextUrl", renderLink(jParams,showStr+"&screen=next&search="+searchString+"&from="+searchFrom.intValue()) ); // execute the search (called the 1st time) if (true) { engineMap.put( "engineOutputFile", theTemplate ); engineMap.put( "jParams", jParams ); engineMap.put( "jData", jData ); engineMap.put( "renderType", new Integer(JahiaEngine.RENDERTYPE_FORWARD) ); engineMap.put( "engineName", engineName ); engineMap.put( "engineUrl", jParams.composeEngineUrl( engineName, "" ) ); engineMap.put( "jahiaBuild", new Integer(jParams.settings().getBuildNumber())); engineMap.put( "javascriptUrl", jParams.settings().getJsHttpPath() ); jParams.getRequest().setAttribute( "engineMap", engineMap ); //JahiaConsole.println("SearchEngine","Searchstring: "+searchString); JahiaSearchResult searchResults = ServicesRegistry.getInstance().getJahiaSearchService().doSearch(searchString, jParams); if(searchResults == null) { searchResults = new JahiaSearchResult(); } engineMap.put ("searchShow", searchShow); engineMap.put ("searchResults", searchResults); engineMap.put ("searchString", searchString); } } // end processScreen}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -