📄 addresource.java
字号:
/* * Copyright 2004 The Apache Software Foundation. * * Licensed 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 org.apache.myfaces.component.html.util;import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;import java.io.PrintWriter;import java.text.ParseException;import java.text.SimpleDateFormat;import java.util.Calendar;import java.util.Date;import java.util.Iterator;import java.util.LinkedHashSet;import java.util.ResourceBundle;import java.util.Set;import javax.faces.context.FacesContext;import javax.faces.context.ResponseWriter;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import org.apache.commons.logging.Log;import org.apache.commons.logging.LogFactory;import org.apache.myfaces.renderkit.html.HTML;/** * This is a utility class to render link to resources used by custom components. * Mostly used to avoid having to include <script src="..."></script> * in the head of the pages before using a component. * * @author Sylvain Vieujot (latest modification by $Author: svieujot $) * @version $Revision: 1.23 $ $Date: 2005/03/14 18:25:36 $ * $Log: AddResource.java,v $ * Revision 1.23 2005/03/14 18:25:36 svieujot * ExtensionsFilter : Set last modified header (and us it in the URL instead of cacheKey). * * Revision 1.22 2005/03/14 17:49:32 svieujot * Cleanup. * * Revision 1.21 2005/03/14 15:58:42 svieujot * Added caching to the ExtensionsFilter * * Revision 1.20 2005/02/22 08:41:50 matzew * Patch for the new tree component form Sean Schofield * * Revision 1.19 2005/02/16 00:50:37 oros * SF issue #1043331: replaced all by the corresponding numeric entity   so safari users will be happy, too, with MyFaces output * * Revision 1.18 2005/02/08 12:13:39 svieujot * Bugfix : Serve xsl files with the text/xml content type. * * Revision 1.17 2004/12/27 04:11:11 mmarinschek * Data Table stores the state of facets of children; script tag is rendered with type attribute instead of language attribute, popup works better as a column in a data table * * Revision 1.16 2004/12/24 14:11:50 svieujot * Return resource relative mapped path when given null context. * * Revision 1.15 2004/12/17 13:19:10 mmarinschek * new component jsValueChangeListener * * Revision 1.14 2004/12/06 01:02:02 svieujot * Write the response in the log messages (mainly to debug problems due to filters order). * * Revision 1.13 2004/12/03 21:20:09 svieujot * Add type="text/css" for inline styles. * * Revision 1.12 2004/12/03 21:15:21 svieujot * define AdditionalHeaderInfoToRender.equals to prevent several include of the same header info. * * Revision 1.11 2004/12/03 20:50:52 svieujot * Minor bugfix, and add <script ... defer="true"> capability. * * Revision 1.10 2004/12/03 20:27:51 svieujot * Add capability to add inline style to the header. * * Revision 1.9 2004/12/02 22:26:23 svieujot * Simplify the AddResource methods * * Revision 1.8 2004/12/02 11:53:27 svieujot * Replace java 1.5 code by 1.4 version. * * Revision 1.7 2004/12/02 02:20:55 svieujot * Bugfix : render the head elements in the same order as they were added (use a LinkedHashSet). * * Revision 1.6 2004/12/02 02:07:22 svieujot * Make the Extensions filter work with resource hierarchies. * A small concession had to be made though : * The ExtensionsFilter must have the (additional) /faces/* * * Revision 1.5 2004/12/02 00:25:34 oros * i18n issues * some slight refactorings * * Revision 1.4 2004/12/01 22:12:51 svieujot * Add xml and xsl content types. * * Revision 1.3 2004/12/01 20:29:22 svieujot * Add javadoc. * * Revision 1.2 2004/12/01 20:25:10 svieujot * Make the Extensions filter support css and image resources. * Convert the popup calendar to use this new filter. * * Revision 1.1 2004/12/01 16:32:03 svieujot * Convert the Multipart filter in an ExtensionsFilter that provides an additional facility to include resources in a page. * Tested only with javascript resources right now, but should work fine with images too. * Some work to do to include css resources. * The popup component has been converted to use this new Filter. * */public class AddResource { private static final Log log = LogFactory.getLog(AddResource.class); private static final String COMPONENTS_PACKAGE = "org.apache.myfaces.custom."; private static final String RESOURCE_VIRTUAL_PATH = "/faces/myFacesExtensionResource"; private static final String ADDITIONAL_HEADER_INFO_REQUEST_ATTRUBITE_NAME = "myFacesHeaderResource2Render"; // Methodes to Add resources /** * Adds the given Javascript resource to the document body. */ public static void addJavaScriptHere(Class componentClass, String resourceFileName, FacesContext context) throws IOException{ ResponseWriter writer = context.getResponseWriter(); writer.startElement(HTML.SCRIPT_ELEM,null); writer.writeAttribute(HTML.SCRIPT_TYPE_ATTR,HTML.SCRIPT_TYPE_TEXT_JAVASCRIPT,null); writer.writeURIAttribute(HTML.SRC_ATTR, getResourceMappedPath(componentClass, resourceFileName, context), null); writer.endElement(HTML.SCRIPT_ELEM); } /** * Adds the given Javascript resource to the document Header. * If the script is already has already been referenced, it's added only once. */ public static void addJavaScriptToHeader(Class componentClass, String resourceFileName, FacesContext context){ addJavaScriptToHeader(componentClass, resourceFileName, false, context); } /** * Adds the given Javascript resource to the document Header. * If the script is already has already been referenced, it's added only once. */ public static void addJavaScriptToHeader(Class componentClass, String resourceFileName, boolean defer, FacesContext context){ AdditionalHeaderInfoToRender jsInfo = new AdditionalHeaderInfoToRender(AdditionalHeaderInfoToRender.TYPE_JS, componentClass, resourceFileName, defer); addAdditionalHeaderInfoToRender(context, jsInfo ); } /** * Adds the given Style Sheet to the document Header. * If the style sheet is already has already been referenced, it's added only once. */ public static void addStyleSheet(Class componentClass, String resourceFileName, FacesContext context){ AdditionalHeaderInfoToRender cssInfo = new AdditionalHeaderInfoToRender(AdditionalHeaderInfoToRender.TYPE_CSS, componentClass, resourceFileName); addAdditionalHeaderInfoToRender(context, cssInfo ); } /** * Adds the given Style Sheet to the document Header. * If the style sheet is already has already been referenced, it's added only once. */ public static void addInlineStyleToHeader(String inlineStyle, FacesContext context){ AdditionalHeaderInfoToRender cssInfo = new AdditionalHeaderInfoToRender(AdditionalHeaderInfoToRender.TYPE_CSS_INLINE, inlineStyle); addAdditionalHeaderInfoToRender(context, cssInfo ); } /** * Get the Path used to retrieve an internal resource for a custom component. * Example : You can use this to initialize javascript scripts so that they know the path of some other resources * (image, css, ...). * <code> * AddResource.addJavaScriptOncePerPage(HtmlCalendarRenderer.class, "popcalendar.js", facesContext, * "jscalendarSetImageDirectory("+AddResource.getResourceMappedPath(HtmlCalendarRenderer.class, "DB", facesContext)+")"); * </code> * * Note : set context to null if you want the path after the application context path. */ public static String getResourceMappedPath(Class componentClass, String resourceFileName, FacesContext context){ HttpServletRequest request = null; if( context != null ) request = (HttpServletRequest)context.getExternalContext().getRequest(); return getResourceMappedPath( getComponentName(componentClass), resourceFileName, request); } private static String getResourceMappedPath(String componentName, String resourceFileName, HttpServletRequest request){ String contextPath = ""; if( request != null ) contextPath = request.getContextPath(); return contextPath+RESOURCE_VIRTUAL_PATH+"/"+componentName+'/'+getCacheKey()+'/'+resourceFileName; } private static long getCacheKey(){ return getLastModified(); } private static Date lastModified = null; private static long getLastModified(){ if( lastModified == null ){ final String format = "yyyy-MM-dd HH:mm:ss Z"; // Must match the one used in the build file final String bundleName = AddResource.class.getName(); ResourceBundle resources = ResourceBundle.getBundle( bundleName ); String sLastModified = resources.getString("lastModified"); try { lastModified = new SimpleDateFormat(format).parse( sLastModified ); } catch (ParseException e) { lastModified = new Date(); log.error("Unparsable lastModified : "+sLastModified); } } return lastModified.getTime(); } public static boolean isResourceMappedPath(HttpServletRequest request){ return request.getRequestURI().indexOf( RESOURCE_VIRTUAL_PATH ) != -1; } /** * Decodes the path to return the requested componentName & resourceFileName * String[0] == componentName * String[1] == resourceFileName */ private static String[] getResourceInfoFromPath(HttpServletRequest request){ String uri = request.getRequestURI(); String componentNameStartsAfter = RESOURCE_VIRTUAL_PATH+'/'; int posStartComponentName = uri.indexOf( componentNameStartsAfter )+componentNameStartsAfter.length(); int posEndComponentName = uri.indexOf("/", posStartComponentName); String componentName = uri.substring(posStartComponentName, posEndComponentName);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -