📄 templatemanager.java
字号:
String clientLanguage = ((HttpServletRequest) pageContext.getRequest()).getLocale().toString(); JarInputStream jarStream = null; try { JarEntry entry; InputStream inputStream = pageContext.getServletContext().getResourceAsStream(I18NRESOURCE_PATH); jarStream = new JarInputStream(inputStream); while ((entry = jarStream.getNextJarEntry()) != null) { String name = entry.getName(); if (!entry.isDirectory() && name.startsWith(I18NRESOURCE_PREFIX) && name.endsWith(I18NRESOURCE_SUFFIX)) { name = name.substring(I18NRESOURCE_PREFIX.length(), name.lastIndexOf(I18NRESOURCE_SUFFIX)); Locale locale = new Locale(name.substring(0, 2), ((name.indexOf("_") == -1) ? "" : name.substring(3, 5))); String defaultLanguage = ""; if (clientLanguage.startsWith(name)) { defaultLanguage = LocaleSupport.getLocalizedMessage(pageContext, I18NDEFAULT_LOCALE); } resultMap.put(name, locale.getDisplayName(locale) + " " + defaultLanguage); } } } catch (IOException ioe) { if (log.isDebugEnabled()) log.debug("Could not search jar file '" + I18NRESOURCE_PATH + "'for properties files due to an IOException: \n" + ioe.getMessage()); } finally { if( jarStream != null ) { try { jarStream.close(); } catch(IOException e) {} } } return resultMap; } /** * List all available timeformats, read from the jspwiki.properties * * @param pageContext * @return map of TimeFormats * @since 2.7.x */ public Map listTimeFormats(PageContext pageContext) { WikiContext context = WikiContext.findContext( pageContext ); Properties props = m_engine.getWikiProperties(); ArrayList<String> tfArr = new ArrayList<String>(40); LinkedHashMap<String,String> resultMap = new LinkedHashMap<String,String>(); /* filter timeformat properties */ for (Enumeration e = props.propertyNames(); e.hasMoreElements();) { String name = (String) e.nextElement(); if (name.startsWith(TIMEFORMATPROPERTIES)) { tfArr.add(name); } } /* fetch actual formats */ if (tfArr.size() == 0) /* * no props found - make sure some default * formats are avail */ { tfArr.add("dd-MMM-yy"); tfArr.add("d-MMM-yyyy"); tfArr.add("EEE, dd-MMM-yyyy, zzzz"); } else { Collections.sort(tfArr); for (int i = 0; i < tfArr.size(); i++) { tfArr.set(i, props.getProperty(tfArr.get(i))); } } String prefTimeZone = Preferences.getPreference( context, "TimeZone" ); //TimeZone tz = TimeZone.getDefault(); TimeZone tz = TimeZone.getTimeZone(prefTimeZone); /*try { tz.setRawOffset(Integer.parseInt(prefTimeZone)); } catch (Exception e) { }*/ Date d = new Date(); // current date try { // dummy format pattern SimpleDateFormat fmt = Preferences.getDateFormat( context, TimeFormat.DATETIME ); fmt.setTimeZone(tz); for (int i = 0; i < tfArr.size(); i++) { try { String f = tfArr.get(i); fmt.applyPattern(f); resultMap.put(f, fmt.format(d)); } catch (IllegalArgumentException e) { } // skip parameter } } catch (IllegalArgumentException e) { } // skip parameter return resultMap; } /** * List all timezones, with special marker for server timezone * * @param pageContext * @return map of TimeZones * @since 2.7.x */ public Map listTimeZones(PageContext pageContext) { LinkedHashMap<String,String> resultMap = new LinkedHashMap<String,String>(); String[][] tzs = { { "GMT-12", "Enitwetok, Kwajalien" }, { "GMT-11", "Nome, Midway Island, Samoa" }, { "GMT-10", "Hawaii" }, { "GMT-9", "Alaska" }, { "GMT-8", "Pacific Time" }, { "GMT-7", "Mountain Time" }, { "GMT-6", "Central Time, Mexico City" }, { "GMT-5", "Eastern Time, Bogota, Lima, Quito" }, { "GMT-4", "Atlantic Time, Caracas, La Paz" }, { "GMT-3:30", "Newfoundland" }, { "GMT-3", "Brazil, Buenos Aires, Georgetown, Falkland Is." }, { "GMT-2", "Mid-Atlantic, Ascention Is., St Helena" }, { "GMT-1", "Azores, Cape Verde Islands" }, { "GMT", "Casablanca, Dublin, Edinburgh, London, Lisbon, Monrovia" }, { "GMT+1", "Berlin, Brussels, Copenhagen, Madrid, Paris, Rome" }, { "GMT+2", "Helsinki, Athens, Kaliningrad, South Africa, Warsaw" }, { "GMT+3", "Baghdad, Riyadh, Moscow, Nairobi" }, { "GMT+3:30", "Tehran" }, { "GMT+4", "Adu Dhabi, Baku, Muscat, Tbilisi" }, { "GMT+4:30", "Kabul" }, { "GMT+5", "Islamabad, Karachi, Tashkent" }, { "GMT+5:30", "Bombay, Calcutta, Madras, New Delhi" }, { "GMT+6", "Almaty, Colomba, Dhakra" }, { "GMT+7", "Bangkok, Hanoi, Jakarta" }, { "GMT+8", "Beijing, Hong Kong, Perth, Singapore, Taipei" }, { "GMT+9", "Osaka, Sapporo, Seoul, Tokyo, Yakutsk" }, { "GMT+9:30", "Adelaide, Darwin" }, { "GMT+10", "Melbourne, Papua New Guinea, Sydney, Vladivostok" }, { "GMT+11", "Magadan, New Caledonia, Solomon Islands" }, { "GMT+12", "Auckland, Wellington, Fiji, Marshall Island" } }; java.util.TimeZone servertz = java.util.TimeZone.getDefault(); for( int i = 0; i < tzs.length; i++ ) { String tzID = tzs[i][0]; java.util.TimeZone tz = java.util.TimeZone.getTimeZone(tzID); String serverTimeZone = ""; if( servertz.getRawOffset() == tz.getRawOffset() ) { serverTimeZone = LocaleSupport.getLocalizedMessage(pageContext, I18NSERVER_TIMEZONE); tzID = servertz.getID(); } resultMap.put(tzID, "(" + tzs[i][0] + ") "+tzs[i][1] + " " + serverTimeZone); } return resultMap; } /** * Always returns a valid property map. */ /* private Properties getTemplateProperties( String templateName ) throws IOException { Properties p = new Properties(); ServletContext context = m_engine.getServletContext(); InputStream propertyStream = context.getResourceAsStream(getPath(templateName)+PROPERTYFILE); if( propertyStream != null ) { p.load( propertyStream ); propertyStream.close(); } else { log.debug("Template '"+templateName+"' does not have a propertyfile '"+PROPERTYFILE+"'."); } return p; }*/ /** * Returns the include resources marker for a given type. This is in a * HTML or Javascript comment format. * * @param context the wiki context * @param type the marker * @return the generated marker comment */ public static String getMarker(WikiContext context, String type ) { if( type.equals(RESOURCE_JSLOCALIZEDSTRINGS) ) { return getJSLocalizedStrings( context ); } else if( type.equals(RESOURCE_JSFUNCTION) ) { return "/* INCLUDERESOURCES ("+type+") */"; } return "<!-- INCLUDERESOURCES ("+type+") -->"; } /** * Extract all i18n strings in the javascript domain. (javascript.*) * Returns a javascript snippet which defines the LoacalizedStings array. * * @param wiki context * @return Javascript snippet which defines the LocaliedStrings array * @author Dirk Frederickx * @since 2.5.108 */ private static String getJSLocalizedStrings( WikiContext context ) { StringBuffer sb = new StringBuffer(); sb.append( "var LocalizedStrings = {\n"); ResourceBundle rb = context.getBundle("templates.default"); boolean first = true; for( Enumeration en = rb.getKeys(); en.hasMoreElements(); ) { String key = (String)en.nextElement(); if( key.startsWith("javascript") ) { if( first ) { first = false; } else { sb.append( ",\n" ); } sb.append( "\""+key+"\":\""+rb.getString(key)+"\"" ); } } sb.append("\n};\n"); return( sb.toString() ); } /** * Adds a resource request to the current request context. * The content will be added at the resource-type marker * (see IncludeResourcesTag) in WikiJSPFilter. * <p> * The resources can be of different types. For RESOURCE_SCRIPT and RESOURCE_STYLESHEET * this is an URI path to the resource (a script file or an external stylesheet) * that needs to be included. For RESOURCE_INLINECSS * the resource should be something that can be added between <style></style> in the * header file (commonheader.jsp). For RESOURCE_JSFUNCTION it is the name of the Javascript * function that should be run at page load. * <p> * The IncludeResourceTag inserts code in the template files, which is then filled * by the WikiFilter after the request has been rendered but not yet sent to the recipient. * <p> * Note that ALL resource requests get rendered, so this method does not check if * the request already exists in the resources. Therefore, if you have a plugin which * makes a new resource request every time, you'll end up with multiple resource requests * rendered. It's thus a good idea to make this request only once during the page * life cycle. * * @param ctx The current wiki context * @param type What kind of a request should be added? * @param resource The resource to add. */ @SuppressWarnings("unchecked") public static void addResourceRequest( WikiContext ctx, String type, String resource ) { HashMap<String,Vector<String>> resourcemap = (HashMap<String,Vector<String>>) ctx.getVariable( RESOURCE_INCLUDES ); if( resourcemap == null ) { resourcemap = new HashMap<String,Vector<String>>(); } Vector<String> resources = resourcemap.get( type ); if( resources == null ) { resources = new Vector<String>(); } String resourceString = null; if( type.equals(RESOURCE_SCRIPT) ) { resourceString = "<script type='text/javascript' src='"+resource+"'></script>"; } else if( type.equals(RESOURCE_STYLESHEET) ) { resourceString = "<link rel='stylesheet' type='text/css' href='"+resource+"' />"; } else if( type.equals(RESOURCE_INLINECSS) ) { resourceString = "<style type='text/css'>\n"+resource+"\n</style>\n"; } else if( type.equals(RESOURCE_JSFUNCTION) ) { resourceString = resource; } else if( type.equals(RESOURCE_HTTPHEADER) ) { resourceString = resource; } if( resourceString != null ) { resources.add( resourceString ); } log.debug("Request to add a resource: "+resourceString); resourcemap.put( type, resources ); ctx.setVariable( RESOURCE_INCLUDES, resourcemap ); } /** * Returns resource requests for a particular type. If there are no resources, * returns an empty array. * * @param ctx WikiContext * @param type The resource request type * @return a String array for the resource requests */ @SuppressWarnings("unchecked") public static String[] getResourceRequests( WikiContext ctx, String type ) { HashMap<String,Vector<String>> hm = (HashMap<String,Vector<String>>) ctx.getVariable( RESOURCE_INCLUDES ); if( hm == null ) return new String[0]; Vector<String> resources = hm.get( type ); if( resources == null ) return new String[0]; String[] res = new String[resources.size()]; return resources.toArray( res ); } /** * Returns all those types that have been requested so far. * * @param ctx the wiki context * @return the array of types requested */ @SuppressWarnings("unchecked") public static String[] getResourceTypes( WikiContext ctx ) { String[] res = new String[0]; if( ctx != null ) { HashMap<String,String> hm = (HashMap<String,String>) ctx.getVariable( RESOURCE_INCLUDES ); if( hm != null ) { Set<String> keys = hm.keySet(); res = keys.toArray( res ); } } return res; } /** * Returns an empty collection, since at the moment the TemplateManager * does not manage any modules. * * @return {@inheritDoc} */ public Collection modules() { return new ArrayList(); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -