📄 templatemanager.java
字号:
/* JSPWiki - a JSP-based WikiWiki clone. Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */package com.ecyrd.jspwiki.ui;import java.io.IOException;import java.io.InputStream;import java.text.SimpleDateFormat;import java.util.*;import java.util.jar.JarEntry;import java.util.jar.JarInputStream;import javax.servlet.ServletContext;import javax.servlet.http.HttpServletRequest;import javax.servlet.jsp.PageContext;import javax.servlet.jsp.jstl.fmt.LocaleSupport;import org.apache.commons.lang.StringUtils;import org.apache.log4j.Logger;import com.ecyrd.jspwiki.InternalWikiException;import com.ecyrd.jspwiki.WikiContext;import com.ecyrd.jspwiki.WikiEngine;import com.ecyrd.jspwiki.modules.ModuleManager;import com.ecyrd.jspwiki.preferences.Preferences;import com.ecyrd.jspwiki.preferences.Preferences.TimeFormat;/** * This class takes care of managing JSPWiki templates. This class also provides * the ResourceRequest mechanism. * * @since 2.1.62 */public class TemplateManager extends ModuleManager{ private static final String SKIN_DIRECTORY = "skins"; /** * Requests a JavaScript function to be called during window.onload. Value is {@value}. */ public static final String RESOURCE_JSFUNCTION = "jsfunction"; /** * Requests a JavaScript associative array with all localized strings. */ public static final String RESOURCE_JSLOCALIZEDSTRINGS = "jslocalizedstrings"; /** * Requests a stylesheet to be inserted. Value is {@value}. */ public static final String RESOURCE_STYLESHEET = "stylesheet"; /** * Requests a script to be loaded. Value is {@value}. */ public static final String RESOURCE_SCRIPT = "script"; /** * Requests inlined CSS. Value is {@value}. */ public static final String RESOURCE_INLINECSS = "inlinecss"; /** The default directory for the properties. Value is {@value}. */ public static final String DIRECTORY = "templates"; /** The name of the default template. Value is {@value}. */ public static final String DEFAULT_TEMPLATE = "default"; /** Name of the file that contains the properties. */ public static final String PROPERTYFILE = "template.properties"; /** Location of I18N Resource bundles, and path prefix and suffixes */ public static final String I18NRESOURCE_PATH = "/WEB-INF/lib/JSPWiki.jar"; public static final String I18NRESOURCE_PREFIX = "templates/default_"; public static final String I18NRESOURCE_SUFFIX = ".properties"; /** I18N string to mark the default locale */ public static final String I18NDEFAULT_LOCALE = "prefs.user.language.default"; /** I18N string to mark the server timezone */ public static final String I18NSERVER_TIMEZONE = "prefs.user.timezone.server"; /** Prefix of the default timeformat properties. */ public static final String TIMEFORMATPROPERTIES = "jspwiki.defaultprefs.timeformat."; /** * The name under which the resource includes map is stored in the * WikiContext. */ public static final String RESOURCE_INCLUDES = "jspwiki.resourceincludes"; // private Cache m_propertyCache; protected static final Logger log = Logger.getLogger(TemplateManager.class); /** Requests a HTTP header. Value is {@value}. */ public static final String RESOURCE_HTTPHEADER = "httpheader"; /** * Creates a new TemplateManager. There is typically one manager per engine. * * @param engine The owning engine. * @param properties The property list used to initialize this. */ public TemplateManager( WikiEngine engine, Properties properties ) { super(engine); // // Uses the unlimited cache. // // m_propertyCache = new Cache( true, false ); } /** * Check the existence of a template. */ // FIXME: Does not work yet public boolean templateExists( String templateName ) { ServletContext context = m_engine.getServletContext(); InputStream in = context.getResourceAsStream( getPath(templateName)+"ViewTemplate.jsp"); if( in != null ) { try { in.close(); } catch (IOException e) { } return true; } return false; } /** * Tries to locate a given resource from the template directory. If the * given resource is not found under the current name, returns the * path to the corresponding one in the default template. * * @param sContext The servlet context * @param name The name of the resource * @return The name of the resource which was found. */ private static String findResource( ServletContext sContext, String name ) { InputStream is = sContext.getResourceAsStream( name ); if( is == null ) { String defname = makeFullJSPName( DEFAULT_TEMPLATE, removeTemplatePart(name) ); is = sContext.getResourceAsStream( defname ); if( is != null ) name = defname; else name = null; } if( is != null ) { try { is.close(); } catch( IOException e ) {} } return name; } /** * Attempts to find a resource from the given template, and if it's not found * attempts to locate it from the default template. * @param sContext * @param template * @param name * @return */ private static String findResource( ServletContext sContext, String template, String name ) { if( name.charAt(0) == '/' ) { // This is already a full path return findResource( sContext, name ); } String fullname = makeFullJSPName( template, name ); return findResource( sContext, fullname ); } /** * An utility method for finding a JSP page. It searches only under * either current context or by the absolute name. * * @param pageContext the JSP PageContext * @param name The name of the JSP page to look for (e.g "Wiki.jsp") * @return The context path to the resource */ public String findJSP( PageContext pageContext, String name ) { ServletContext sContext = pageContext.getServletContext(); return findResource( sContext, name ); } /** * Removes the template part of a name. */ private static final String removeTemplatePart( String name ) { int idx = 0; if( name.startsWith( "/" ) ) idx = 1; idx = name.indexOf('/', idx); if( idx != -1 ) { idx = name.indexOf('/', idx+1); // Find second "/" if( idx != -1 ) { name = name.substring( idx+1 ); } } log.info( "Final name = "+name ); return name; } /** * Returns the full name (/templates/foo/bar) for name=bar, template=foo. * * @param template The name of the template. * @param name The name of the resource. * @return The full name for a template. */ private static final String makeFullJSPName( String template, String name ) { return "/"+DIRECTORY+"/"+template+"/"+name; } /** * Attempts to locate a resource under the given template. If that template * does not exist, or the page does not exist under that template, will * attempt to locate a similarly named file under the default template. * <p> * Even though the name suggests only JSP files can be located, but in fact * this method can find also other resources than JSP files. * * @param pageContext The JSP PageContext * @param template From which template we should seek initially? * @param name Which resource are we looking for (e.g. "ViewTemplate.jsp") * @return path to the JSP page; null, if it was not found. */ public String findJSP( PageContext pageContext, String template, String name ) { if( name == null || template == null ) { log.fatal("findJSP() was asked to find a null template or name ("+template+","+name+")."+ " JSP page '"+ ((HttpServletRequest)pageContext.getRequest()).getRequestURI()+"'"); throw new InternalWikiException("Illegal arguments to findJSP(); please check logs."); } return findResource( pageContext.getServletContext(), template, name ); } /** * Attempts to locate a resource under the given template. This matches the * functionality findJSP(), but uses the WikiContext as the argument. If there * is no servlet context (i.e. this is embedded), will just simply return * a best-guess. * <p> * This method is typically used to locate any resource, including JSP pages, images, * scripts, etc. * * @since 2.6 * @param ctx the wiki context * @param template the name of the template to use * @param name the name of the resource to fine * @return the path to the resource */ public String findResource( WikiContext ctx, String template, String name ) { if( m_engine.getServletContext() != null ) { return findResource( m_engine.getServletContext(), template, name ); } return getPath(template)+"/"+name; } /** * Returns a property, as defined in the template. The evaluation * is lazy, i.e. the properties are not loaded until the template is * actually used for the first time. */ /* public String getTemplateProperty( WikiContext context, String key ) { String template = context.getTemplate(); try { Properties props = (Properties)m_propertyCache.getFromCache( template, -1 ); if( props == null ) { try { props = getTemplateProperties( template ); m_propertyCache.putInCache( template, props ); } catch( IOException e ) { log.warn("IO Exception while reading template properties",e); return null; } } return props.getProperty( key ); } catch( NeedsRefreshException ex ) { // FIXME return null; } }*/ /** * Returns an absolute path to a given template. */ private static final String getPath( String template ) { return "/"+DIRECTORY+"/"+template+"/"; } /** * Lists the skins available under this template. Returns an * empty Set, if there are no extra skins available. Note that * this method does not check whether there is anything actually * in the directories, it just lists them. This may change * in the future. * * @param pageContext the JSP PageContext * @param template The template to search * @return Set of Strings with the skin names. * @since 2.3.26 */ @SuppressWarnings("unchecked") public Set listSkins( PageContext pageContext, String template ) { String place = makeFullJSPName( template, SKIN_DIRECTORY ); ServletContext sContext = pageContext.getServletContext(); Set<String> skinSet = sContext.getResourcePaths( place ); TreeSet<String> resultSet = new TreeSet<String>(); if( log.isDebugEnabled() ) log.debug( "Listings skins from "+place ); if( skinSet != null ) { String[] skins = {}; skins = skinSet.toArray(skins); for (int i = 0; i < skins.length; i++) { String[] s = StringUtils.split(skins[i], "/"); if (s.length > 2 && skins[i].endsWith("/")) { String skinName = s[s.length - 1]; resultSet.add(skinName); if (log.isDebugEnabled()) log.debug("...adding skin '" + skinName + "'"); } } } return resultSet; } /** * List all installed i18n language properties * * @param pageContext * @return map of installed Languages (with help of Juan Pablo Santos Rodriguez) * @since 2.7.x */ public Map listLanguages(PageContext pageContext) { LinkedHashMap<String,String> resultMap = new LinkedHashMap<String,String>();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -