⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 template_engine.java

📁 java 写的一个新闻发布系统
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
	            if ( ( templateContext != null )	            	&& ( theTemporaryTemplate.getSourcePath().indexOf( templateContext ) != -1 ) ){					theTemporaryTemplate.setSourcePath(						theTemporaryTemplate.getSourcePath().substring(							templateContext.length(),							theTemporaryTemplate.getSourcePath().length()));	            }                engineMap.put( "screen", (String)jParams.getRequest().getParameter("lastscreen"));            }            JahiaConsole.println(CLASS_NAME,"Saving !!" );        } else if (theScreen.equals("cancel")) {            engineMap.put( "engineOutputFile", JahiaEngine.CANCEL_JSP );		}    } // end processCurrentScreen    //--------------------------------------------------------------------------    /**     * inits the engine map     *     * @param jParams, a ParamBean object (with request and response)     * @param JahiaData , the jahia data bean     * @return HashMap, a HashMap object containing all the basic values needed by an engine     */    private HashMap initEngineMap (JahiaData jData)        throws  JahiaException,                JahiaSessionExpirationException    {    	ParamBean jParams = jData.params();        HashMap engineMap = new HashMap();        JahiaPageDefinition theTemplate;        // gets session values        HttpSession theSession = jParams.getSession ();        // tries to find if this is the first screen generated by the engine        String theScreen = jParams.getRequest().getParameter( "screen" );        if (theScreen != null) {            // if no, load the engine map value from the session            engineMap = (HashMap) theSession.getAttribute( ParamBean.SESSION_JAHIA_ENGINEMAP );            if (engineMap == null) {                throw new JahiaSessionExpirationException ();            }            theTemplate = (JahiaPageDefinition) engineMap.get( TEMPLATE_SESSION_NAME );        } else {            // first screen generated by engine -> init sessions            int templateID = -1;            String value = jParams.getRequest().getParameter("templateid");            if ( value != null )            	templateID = Integer.parseInt(value);            theTemplate = ServicesRegistry.getInstance()            			.getJahiaPageTemplateService()            			.lookupPageTemplate( templateID );            theScreen = "edit";            // init the temporary template bean            JahiaPageDefinitionTemp theTemporaryTemplate =            	new JahiaPageDefinitionTemp( theTemplate,            								 (jParams.getSite().getDefaultTemplateID()==theTemplate.getID()) );            // remove template context and the sitekey from the source path            String templateContext = jParams.settings().getTemplatesContext() + jParams.getSite().getSiteKey() + "/";            if ( ( templateContext != null )            	&& ( theTemporaryTemplate.getSourcePath().indexOf( templateContext ) != -1 ) ){				theTemporaryTemplate.setSourcePath(					theTemporaryTemplate.getSourcePath().substring(						templateContext.length(),						theTemporaryTemplate.getSourcePath().length()));            }			// init session			engineMap.put( TEMPLATE_SESSION_NAME, theTemplate );			engineMap.put( TEMPORARY_TEMPLATE_SESSION_NAME, theTemporaryTemplate );			engineMap.put( "jParams", jParams );        }		engineMap.put( "renderType", new Integer(JahiaEngine.RENDERTYPE_FORWARD) );		engineMap.put( "engineName", engineName );		engineMap.put( "engineUrl", jParams.composeEngineUrl( engineName, "?templateid=" + theTemplate.getID() ) );		theSession.setAttribute( ParamBean.SESSION_JAHIA_ENGINEMAP, engineMap );        // sets screen        engineMap.put( "screen", theScreen );        if (theScreen.equals ("save")) {            engineMap.put( "jspSource", "close" );        } else if (theScreen.equals ("apply")) {            engineMap.put( "jspSource", "apply" );        } else if (theScreen.equals ("cancel")) {            engineMap.put( "jspSource", "close" );        } else {            engineMap.put( "jspSource", TEMPLATE_JSP );        }        // sets engineMap for JSPs        jParams.getRequest().setAttribute( "engineName", "Manage Templates" );        jParams.getRequest().setAttribute( "org.jahia.engines.EngineHashMap", engineMap );        jParams.getRequest().setAttribute( "Template_Engine.warningMsg", "" );        return engineMap;    } // end initEngineMap    //--------------------------------------------------------------------------	/**	 * Prepare data to display or retrieves submitted values and store them in session	 *	 */	private boolean processTemplateEdit(ParamBean jParams, int mode, HashMap engineMap){		if ( mode == JahiaEngine.LOAD_MODE ){			// everything is in the session , so do nothing			return true;		} else if ( mode == JahiaEngine.UPDATE_MODE ) {			// check the last screen			String lastScreen = jParams.getRequest().getParameter("lastscreen");			if ( lastScreen == null )				lastScreen = "";			if ( lastScreen.equals("edit") ){				// retrieve submitted data	            JahiaPageDefinitionTemp theTemporaryTemplate = ( JahiaPageDefinitionTemp )	            engineMap.get( TEMPORARY_TEMPLATE_SESSION_NAME );				if ( theTemporaryTemplate == null )					return false; // should not				// get the name				String value = jParams.getRequest().getParameter("templateName");				if ( value != null )					theTemporaryTemplate.setName(value);				// get the source path				value = jParams.getRequest().getParameter("sourcePath");				if ( value != null )					theTemporaryTemplate.setSourcePath(value);				// get the available option				value = jParams.getRequest().getParameter("templateAvailable");				theTemporaryTemplate.setAvailable( value != null );				// get the default option				value = jParams.getRequest().getParameter("templateDefault");				theTemporaryTemplate.setDefault( value != null );			}			return true;		}		return false;	}    //--------------------------------------------------------------------------	/**	 * Save data	 */	private boolean processTemplateSave(ParamBean jParams, HashMap engineMap)	throws JahiaException {		StringBuffer warningMsg = new StringBuffer("");		JahiaPageDefinitionTemp theTemporaryTemplate = ( JahiaPageDefinitionTemp )		engineMap.get( TEMPORARY_TEMPLATE_SESSION_NAME );		JahiaPageDefinition theTemplate = ( JahiaPageDefinition )		engineMap.get( TEMPLATE_SESSION_NAME );		// check data integrity		if ( theTemporaryTemplate.getName() == null				|| theTemporaryTemplate.getName().trim().equals("") )				warningMsg.append("<lu><li>The name is required.<br>");		// check sourcepath		warningMsg.append(checkSourcePath(warningMsg.toString(),jParams,theTemporaryTemplate));		if ( !warningMsg.toString().equals("")	){			warningMsg.append("</lu>");			jParams.getRequest().setAttribute("Template_Engine.warningMsg",warningMsg.toString());			return false;		}		// If everything is ok save new values		theTemplate.setName( theTemporaryTemplate.getName() );		theTemplate.setSourcePath( theTemporaryTemplate.getSourcePath() );		JahiaConsole.println(CLASS_NAME+".processTemplateSave","Source path :" + theTemporaryTemplate.getSourcePath() );		theTemplate.setAvailable( theTemporaryTemplate.isAvailable() );		theTemplate.commitChanges();		// save site's default template if needed		boolean doUpdateSite = true;		if ( (jParams.getSite().getDefaultTemplateID() == theTemplate.getID())				&& !theTemporaryTemplate.isDefault() ){			jParams.getSite().setDefaultTemplateID(-1);		} else if (	theTemporaryTemplate.isDefault() ){			jParams.getSite().setDefaultTemplateID(theTemplate.getID());		} else {			doUpdateSite = false;		}		if ( doUpdateSite )			ServicesRegistry.getInstance()						.getJahiaSitesService().updateSite(jParams.getSite());		return true;	}	/**	 * Returns a warning msg in case of not valid source path	 *	 */	private String checkSourcePath(	String warningMsg, ParamBean jParams, JahiaPageDefinitionTemp tempoPageDef ){		if ( tempoPageDef.getSourcePath() == null					|| tempoPageDef.getSourcePath().trim().equals("") ){					return "<li>The source path is required.<br>";		}		// replace all "\" by "/"		String sourcePath = JahiaTools.replacePattern( tempoPageDef.getSourcePath(),"\\","/");		while ( sourcePath.startsWith("/") || sourcePath.startsWith(".") ){			sourcePath = sourcePath.substring(1,sourcePath.length());		}		sourcePath = JahiaTools.replacePattern( sourcePath,"..","");		sourcePath = JahiaTools.replacePattern( sourcePath,"./","/");		sourcePath = JahiaTools.replacePattern( sourcePath,"/.","/");		sourcePath = JahiaTools.replacePattern( sourcePath,"//","/");		// check if the file exists		String path = jParams.settings().getJahiaTemplatesDiskPath();		path = JahiaTools.replacePattern( path,"\\","/");		if ( !path.endsWith("/") )			path += "/";		File f = new File ( path + jParams.getSite().getSiteKey() + File.separator + sourcePath );		JahiaConsole.println(CLASS_NAME+".checkSourcePath"," Template Path = " + f.getAbsolutePath() );		if ( !f.isFile() )			return "<li>The source file does not exist.<br>";		if ( !f.canRead() )			return "<li>No access allowed to the source file.<br>";		// if everything is ok , save :		if ( warningMsg.equals("") ){			String templateContext = jParams.settings().getTemplatesContext()										+ jParams.getSite().getSiteKey() + "/";			tempoPageDef.setSourcePath( templateContext + sourcePath );		}		return "";	}} // end Template_Engine

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -