📄 themeeditoraction.java
字号:
ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { ActionForward forward = mapping.findForward("editTheme"); try { RollerRequest rreq = RollerRequest.getRollerRequest(request); if ( rreq.isUserAuthorizedToEdit() ) { // clear the page cache UserData ud = rreq.getUser(); PageCacheFilter.removeFromCache( request, ud ); ThemeEditorForm teForm = (ThemeEditorForm)form; // clear the places holding onto the template PreviewResourceLoader.clearAllTemplates( ud.getUserName() ); request.getSession().removeAttribute(SESSION_TEMPLATE); teForm.setThemeName("Custom"); } else { forward = mapping.findForward("access-denied"); } } catch (Exception e) { mLogger.error("ERROR in action",e); throw new ServletException(e); } return forward; } /** * Load the Themes from disk ONCE per user session. * * @param rreq * @param errors */ private void loadThemes( RollerRequest rreq, ActionErrors errors, boolean listCustom) { HttpSession session = rreq.getRequest().getSession(false); try { // Figure path to new user templates ServletContext ctx = rreq.getServletContext(); String[] themes = null; if (ctx.getAttribute("themeStore") != null) { themes = (String[]) ctx.getAttribute("themeStore"); } else { RollerContext rollerContext = RollerContext.getRollerContext( ctx ); themes = rollerContext.getThemeNames(); ctx.setAttribute("themeStore", themes); } // need to insert "Custom" as the top theme. // "Custom" means the hand-edited template. if (listCustom) { // probably should use arraycopy here? String[] themes2 = new String[ themes.length+1 ]; themes2[0] = "Custom"; for (int i=1; i<themes2.length; i++) { themes2[i] = themes[i-1]; } themes = themes2; } session.setAttribute( "themes", themes ); } catch (Exception e) { errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("error.editing.user", e.toString())); } } /** * Get the Default Page for the website specified by request. * * @param rreq * @return PageData */ private PageData getDefaultPage(RollerRequest rreq) throws RollerException { try { UserData ud = rreq.getUser(); UserManager mgr = rreq.getRoller().getUserManager(); WebsiteData wd = mgr.getWebsite( ud.getUserName() ); String defaultPageId = wd.getDefaultPageId(); return mgr.retrievePage( defaultPageId ); } catch (Exception e) { mLogger.error("ERROR in action",e); throw new RollerException( e ); } } /** * Loads theme into preview resource loader. * * @param rreq * @param theme * @throws RollerException */ private void setThemePages( RollerRequest rreq, String theme ) throws RollerException { RollerContext rollerContext = RollerContext.getRollerContext(rreq.getRequest()); try { UserData ud = rreq.getUser(); UserManager mgr = rreq.getRoller().getUserManager(); String username = ud.getUserName(); HashMap pages = rollerContext.readThemeMacros(theme); Iterator iter = pages.keySet().iterator(); while ( iter.hasNext() ) { String pageName = (String) iter.next(); String sb = (String)pages.get( pageName ); PageData page = mgr.getPageByName( rreq.getWebsite(), pageName ); if (page != null) { PreviewResourceLoader.setTemplate(page.getId(),sb,username); } else { PreviewResourceLoader.setTemplate(pageName, sb, username); } } } catch (Exception e) { mLogger.error("ERROR in action",e); throw new RollerException( e ); } } /** * Clears users preview theme from the preview resource loader. * * @param rreq * @param theme * @throws RollerException */ private void clearThemePages( RollerRequest rreq, String theme ) throws RollerException { if (mLogger.isDebugEnabled()) { mLogger.debug("theme="+theme); } if (theme == null) return; RollerContext rollerContext = RollerContext.getRollerContext(rreq.getRequest()); try { UserData ud = rreq.getUser(); UserManager mgr = rreq.getRoller().getUserManager(); String username = ud.getUserName(); String themeDir = rollerContext.getThemePath(theme); String[] children = RollerContext.getThemeFilenames(themeDir); // Custom theme won't have any files if (children == null) return; for (int i = 0; i < children.length; i++) { String pageName = children[i].substring( 0,children[i].length()-3); PageData page = mgr.getPageByName(rreq.getWebsite(), pageName); if (page != null) { PreviewResourceLoader.clearTemplate( page.getId() ); } else { PreviewResourceLoader.clearTemplate( pageName ); } } } catch (Exception e) { if (mLogger.isDebugEnabled()) { mLogger.debug("clearThemePages error: ", e); } throw new RollerException( e ); } } /** * Reads theme pages from disk and saves them as pages in website of * the user specified by the RollerRequest. * * @param rreq Request wrapper. * @param theme Name of theme to save. * @throws RollerException */ private void saveThemePages( RollerRequest rreq, String theme ) throws RollerException { RollerContext rollerContext = RollerContext.getRollerContext(rreq.getRequest()); try { UserData ud = rreq.getUser(); UserManager mgr = rreq.getRoller().getUserManager(); String username = ud.getUserName(); WebsiteData website = mgr.getWebsite( username ); HashMap pages = rollerContext.readThemeMacros(theme); Iterator iter = pages.keySet().iterator(); while ( iter.hasNext() ) { String pageName = (String) iter.next(); String pageContent = (String)pages.get( pageName ); PageData page = mgr.getPageByName( rreq.getWebsite(), pageName ); if (page != null) { // User already has page by that name, so overwrite it. page.setTemplate( pageContent ); } else { // User does not have page by that name, so create new page. page = new PageData( null, website, // website pageName, // name pageName, // description pageName, // link pageContent, // template new Date() // updateTime ); mgr.storePage( page ); } } rreq.getRoller().commit(); } catch (Exception e) { mLogger.error("ERROR in action",e); throw new RollerException( e ); } } /** * Read the 'Weblog.vm' file for a theme and return it as a String. * * @param ctx Roller context. * @param theme Name of theme. * @return Theme in the form of a string. * @throws RollerException */ public String readTheme(RollerContext ctx, String theme) throws RollerException { String fileName = "Weblog.vm"; if (themeCache.getFromCache(theme, fileName) != null) { return themeCache.getFromCache(theme, fileName); } String themesDir = RollerRuntimeConfig.getProperty("users.themes.path"); String themeFile = RollerContext.getServletContext( ).getRealPath( "/" + themesDir + "/" + theme + "/" + fileName ); // Import weblog page template from specified theme StringBuffer sb = new StringBuffer(); BufferedReader rdr = null; try { rdr = new BufferedReader( new FileReader(themeFile)); String line = null; while ( null != (line = rdr.readLine()) ) { sb.append( line ); sb.append("\n"); } themeCache.putIntoCache(theme, fileName, sb.toString()); } catch (Exception e) { mLogger.error("themeFile:" + themeFile, e); throw new RollerException( e ); } finally { try { if (rdr != null) rdr.close(); } catch (IOException ioe) { mLogger.warn("unable to close " + themeFile); } } return sb.toString(); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -