You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@myfaces.apache.org by cj...@apache.org on 2009/08/10 15:38:59 UTC

svn commit: r802790 - in /myfaces/core/trunk/impl/src/main/java/org/apache/myfaces/view/facelets: FaceletViewDeclarationLanguage.java compiler/TagLibraryConfig.java

Author: cjhoward
Date: Mon Aug 10 13:38:59 2009
New Revision: 802790

URL: http://svn.apache.org/viewvc?rev=802790&view=rev
Log:
MYFACES-2302 - fixes for custom Facelet taglibs

Modified:
    myfaces/core/trunk/impl/src/main/java/org/apache/myfaces/view/facelets/FaceletViewDeclarationLanguage.java
    myfaces/core/trunk/impl/src/main/java/org/apache/myfaces/view/facelets/compiler/TagLibraryConfig.java

Modified: myfaces/core/trunk/impl/src/main/java/org/apache/myfaces/view/facelets/FaceletViewDeclarationLanguage.java
URL: http://svn.apache.org/viewvc/myfaces/core/trunk/impl/src/main/java/org/apache/myfaces/view/facelets/FaceletViewDeclarationLanguage.java?rev=802790&r1=802789&r2=802790&view=diff
==============================================================================
--- myfaces/core/trunk/impl/src/main/java/org/apache/myfaces/view/facelets/FaceletViewDeclarationLanguage.java (original)
+++ myfaces/core/trunk/impl/src/main/java/org/apache/myfaces/view/facelets/FaceletViewDeclarationLanguage.java Mon Aug 10 13:38:59 2009
@@ -95,24 +95,40 @@
     public final static long DEFAULT_REFRESH_PERIOD = 2;
 
     public final static String DEFAULT_CHARACTER_ENCODING = "UTF-8";
+    
+    //public final static String PARAM_BUFFER_SIZE = "javax.faces.FACELETS_BUFFER_SIZE";
+    
+    public final static String PARAM_BUFFER_SIZE = "javax.faces.FACELETS_BUFFER_SIZE";
+    
+    private final static String PARAM_BUFFER_SIZE_DEPRECATED = "facelets.BUFFER_SIZE";
 
-    public final static String PARAM_BUFFER_SIZE = "facelets.BUFFER_SIZE";
-
-    public final static String PARAM_BUILD_BEFORE_RESTORE = "facelets.BUILD_BEFORE_RESTORE";
-
-    public final static String PARAM_DECORATORS = "facelets.DECORATORS";
+    private final static String PARAM_BUILD_BEFORE_RESTORE = "facelets.BUILD_BEFORE_RESTORE";
+    
+    public final static String PARAM_DECORATORS = "javax.faces.FACELETS_DECORATORS";
+    
+    private final static String PARAM_DECORATORS_DEPRECATED = "facelets.DECORATORS";
 
     public final static String PARAM_ENCODING = "facelets.Encoding";
 
-    public final static String PARAM_LIBRARIES = "facelets.LIBRARIES";
-
-    public final static String PARAM_REFRESH_PERIOD = "facelets.REFRESH_PERIOD";
-
-    public final static String PARAM_RESOURCE_RESOLVER = "facelets.RESOURCE_RESOLVER";
-
-    public final static String PARAM_SKIP_COMMENTS = "facelets.SKIP_COMMENTS";
+    public final static String PARAM_LIBRARIES = "javax.faces.FACELETS_LIBRARIES";
+    
+    private final static String PARAM_LIBRARIES_DEPRECATED = "facelets.LIBRARIES";
 
-    public final static String PARAM_VIEW_MAPPINGS = "facelets.VIEW_MAPPINGS";
+    public final static String PARAM_REFRESH_PERIOD = "javax.faces.FACELETS_REFRESH_PERIOD";
+    
+    private final static String PARAM_REFRESH_PERIOD_DEPRECATED = "facelets.REFRESH_PERIOD";
+    
+    public final static String PARAM_RESOURCE_RESOLVER = "javax.faces.FACELETS_RESOURCE_RESOLVER";
+    
+    private final static String PARAM_RESOURCE_RESOLVER_DEPRECATED = "facelets.RESOURCE_RESOLVER";
+    
+    public final static String PARAM_SKIP_COMMENTS = "javax.faces.FACELETS_SKIP_COMMENTS";
+    
+    private final static String PARAM_SKIP_COMMENTS_DEPRECATED = "facelets.SKIP_COMMENTS";
+    
+    public final static String PARAM_VIEW_MAPPINGS = "javax.faces.FACELETS_VIEW_MAPPINGS";
+    
+    private final static String PARAM_VIEW_MAPPINGS_DEPRECATED = "facelets.VIEW_MAPPINGS";
     
     /**
      * Marker to indicate tag handlers the view currently being built is using
@@ -594,10 +610,10 @@
         ExternalContext eContext = context.getExternalContext();
 
         // refresh period
-        long refreshPeriod = _getLongParameter(eContext, PARAM_REFRESH_PERIOD, DEFAULT_REFRESH_PERIOD);
+        long refreshPeriod = _getLongParameter(eContext, PARAM_REFRESH_PERIOD, PARAM_REFRESH_PERIOD_DEPRECATED, DEFAULT_REFRESH_PERIOD);
 
         // resource resolver
-        ResourceResolver resolver = _getInstanceParameter(eContext, PARAM_RESOURCE_RESOLVER, null);
+        ResourceResolver resolver = _getInstanceParameter(eContext, PARAM_RESOURCE_RESOLVER, PARAM_RESOURCE_RESOLVER_DEPRECATED, null);
         if (resolver == null)
         {
             resolver = new DefaultResourceResolver();
@@ -884,7 +900,7 @@
      */
     protected void loadDecorators(FacesContext context, Compiler compiler)
     {
-        String param = _getStringParameter(context.getExternalContext(), PARAM_DECORATORS);
+        String param = _getStringParameter(context.getExternalContext(), PARAM_DECORATORS, PARAM_DECORATORS_DEPRECATED);
         if (param != null)
         {
             for (String decorator : param.split(";"))
@@ -917,7 +933,7 @@
     {
         ExternalContext eContext = context.getExternalContext();
 
-        String param = _getStringParameter(eContext, PARAM_LIBRARIES);
+        String param = _getStringParameter(eContext, PARAM_LIBRARIES, PARAM_LIBRARIES_DEPRECATED);
         if (param != null)
         {
             for (String library : param.split(";"))
@@ -957,7 +973,7 @@
         ExternalContext eContext = context.getExternalContext();
 
         // skip comments?
-        compiler.setTrimmingComments(_getBooleanParameter(eContext, PARAM_SKIP_COMMENTS, false));
+        compiler.setTrimmingComments(_getBooleanParameter(eContext, PARAM_SKIP_COMMENTS, PARAM_SKIP_COMMENTS_DEPRECATED, false));
     }
 
     /**
@@ -989,6 +1005,8 @@
      *            the application's external context
      * @param name
      *            the init parameter's name
+     * @param deprecatedName
+     *            the init parameter's deprecated name.
      * @param defaultValue
      *            the default value to return in case the parameter was not set
      * 
@@ -997,9 +1015,9 @@
      * @throws NullPointerException
      *             if context or name is <code>null</code>
      */
-    private boolean _getBooleanParameter(ExternalContext context, String name, boolean defaultValue)
+    private boolean _getBooleanParameter(ExternalContext context, String name, String deprecatedName, boolean defaultValue)
     {
-        String param = _getStringParameter(context, name);
+        String param = _getStringParameter(context, name, deprecatedName);
         if (param == null)
         {
             return defaultValue;
@@ -1020,6 +1038,8 @@
      *            the application's external context
      * @param name
      *            the init parameter's name
+     * @param deprecatedName
+     *            the init parameter's deprecated name.
      * @param defaultValue
      *            the default value to return in case the parameter was not set
      * 
@@ -1028,9 +1048,9 @@
      * @throws NullPointerException
      *             if context or name is <code>null</code>
      */
-    private int _getIntegerParameter(ExternalContext context, String name, int defaultValue)
+    private int _getIntegerParameter(ExternalContext context, String name, String deprecatedName, int defaultValue)
     {
-        String param = _getStringParameter(context, name);
+        String param = _getStringParameter(context, name, deprecatedName);
         if (param == null)
         {
             return defaultValue;
@@ -1076,6 +1096,8 @@
      *            the application's external context
      * @param name
      *            the init parameter's name
+     * @param deprecatedName
+     *            the init parameter's deprecated name.
      * @param defaultValue
      *            the default value to return in case the parameter was not set
      * 
@@ -1085,9 +1107,9 @@
      *             if context or name is <code>null</code>
      */
     @SuppressWarnings("unchecked")
-    private <T> T _getInstanceParameter(ExternalContext context, String name, T defaultValue)
+    private <T> T _getInstanceParameter(ExternalContext context, String name, String deprecatedName, T defaultValue)
     {
-        String param = _getStringParameter(context, name);
+        String param = _getStringParameter(context, name, deprecatedName);
         if (param == null)
         {
             return defaultValue;
@@ -1115,6 +1137,8 @@
      *            the application's external context
      * @param name
      *            the init parameter's name
+     * @param deprecatedName
+     *            the init parameter's deprecated name.
      * @param defaultValue
      *            the default value to return in case the parameter was not set
      * 
@@ -1123,9 +1147,9 @@
      * @throws NullPointerException
      *             if context or name is <code>null</code>
      */
-    private long _getLongParameter(ExternalContext context, String name, long defaultValue)
+    private long _getLongParameter(ExternalContext context, String name, String deprecatedName, long defaultValue)
     {
-        String param = _getStringParameter(context, name);
+        String param = _getStringParameter(context, name, deprecatedName);
         if (param == null)
         {
             return defaultValue;
@@ -1146,15 +1170,23 @@
      *            the application's external context
      * @param name
      *            the init parameter's name
-     * 
+     * @param deprecatedName
+     *            the init parameter's deprecated name.
+     *
      * @return the parameter if it was specified and was not empty, <code>null</code> otherwise
      * 
      * @throws NullPointerException
      *             if context or name is <code>null</code>
      */
-    private String _getStringParameter(ExternalContext context, String name)
+    private String _getStringParameter(ExternalContext context, String name, String deprecatedName)
     {
         String param = context.getInitParameter(name);
+        
+        if ((param == null) && (deprecatedName != null))
+        {
+            param = context.getInitParameter (deprecatedName);
+        }
+        
         if (param == null)
         {
             return null;
@@ -1171,7 +1203,7 @@
 
     private void _initializeBuffer(ExternalContext context)
     {
-        _bufferSize = _getIntegerParameter(context, PARAM_BUFFER_SIZE, -1);
+        _bufferSize = _getIntegerParameter(context, PARAM_BUFFER_SIZE, PARAM_BUFFER_SIZE_DEPRECATED, -1);
     }
 
     private void _initializeMode(ExternalContext context)
@@ -1179,10 +1211,10 @@
         // In jsf 2.0 this code evolve as PartialStateSaving feature
         //_buildBeforeRestore = _getBooleanParameter(context, PARAM_BUILD_BEFORE_RESTORE, false);
         _partialStateSaving = _getBooleanParameter(context, 
-                StateManager.PARTIAL_STATE_SAVING_PARAM_NAME, false);
+                StateManager.PARTIAL_STATE_SAVING_PARAM_NAME, null, false);
         
         String [] viewIds = StringUtils.splitShortString(_getStringParameter(context,
-                StateManager.FULL_STATE_SAVING_VIEW_IDS_PARAM_NAME), ',');
+                StateManager.FULL_STATE_SAVING_VIEW_IDS_PARAM_NAME, null), ',');
         
         if (viewIds.length > 0)
         {

Modified: myfaces/core/trunk/impl/src/main/java/org/apache/myfaces/view/facelets/compiler/TagLibraryConfig.java
URL: http://svn.apache.org/viewvc/myfaces/core/trunk/impl/src/main/java/org/apache/myfaces/view/facelets/compiler/TagLibraryConfig.java?rev=802790&r1=802789&r2=802790&view=diff
==============================================================================
--- myfaces/core/trunk/impl/src/main/java/org/apache/myfaces/view/facelets/compiler/TagLibraryConfig.java (original)
+++ myfaces/core/trunk/impl/src/main/java/org/apache/myfaces/view/facelets/compiler/TagLibraryConfig.java Mon Aug 10 13:38:59 2009
@@ -131,7 +131,48 @@
             this.addFunction(name, method);
         }
     }
+    
+    /*
+     * We need this class to do a quick check on a facelets taglib document to see if it's
+     * a pre-2.0 document.  If it is, we really need to construct a DTD validating, non-namespace
+     * aware parser. Otherwise, we have to construct a schema validating, namespace-aware parser.
+     */
+    
+    private static class VersionCheckHandler extends DefaultHandler
+    {
+        private boolean version20OrLater;
+        
+        public boolean isVersion20OrLater ()
+        {
+            return this.version20OrLater;
+        }
+        
+        @Override
+        public void startElement (String uri, String localName, String name, Attributes attributes) throws SAXException
+        {
+            if (name.equals ("facelet-taglib"))
+            {
+                int length = attributes.getLength();
+                
+                for (int i = 0; i < length; ++i)
+                {
+                    if (attributes.getLocalName (i).equals ("version"))
+                    {
+                        // This document has a "version" attribute in the <facelet-taglib> element, so
+                        // it must be a 2.0 or later document as this attribute was never required before.
 
+                        this.version20OrLater = true;
+                    }
+                }
+                
+                // Throw a dummy parsing exception to terminate parsing as there really isn't any need to go any
+                // further.
+                
+                throw new SAXException();
+            }
+        }
+    }
+    
     private static class LibraryHandler extends DefaultHandler
     {
         private final URL source;
@@ -454,7 +495,7 @@
         {
             is = url.openStream();
             LibraryHandler handler = new LibraryHandler(url);
-            SAXParser parser = createSAXParser(handler);
+            SAXParser parser = createSAXParser(handler, url);
             parser.parse(is, handler);
             t = handler.getLibrary();
         }
@@ -499,18 +540,97 @@
         }
     }
 
-    private static final SAXParser createSAXParser(LibraryHandler handler) throws SAXException,
+    private static final SAXParser createSAXParser(LibraryHandler handler, URL url) throws SAXException,
             ParserConfigurationException
     {
         SAXParserFactory factory = SAXParserFactory.newInstance();
-        factory.setNamespaceAware(false);
-        factory.setFeature("http://xml.org/sax/features/validation", true);
-        factory.setValidating(true);
+        
+        // We have to make two different parsers depending on whether or not the facelets taglib
+        // document is version 2.0 or later.  If pre-version 2.0, then the parser must not be
+        // namespace-aware and must be DTD validating.  If verison 2.0 or later, the parser must be
+        // namespace-aware and must be schema validating.
+        
+        if (!isTaglibDocument20OrLater (url))
+        {
+            factory.setNamespaceAware(false);
+            factory.setFeature("http://xml.org/sax/features/validation", true);
+            factory.setValidating(true);
+        }
+        
+        else
+        {
+            // TODO: CJH: the Facelets project does not make the 2.0 schema available via their
+            // CVS.  The schema is also not available at its URL.  For now, 2.0 documents will simply
+            // have no validation.  We must get a copy of their schema once they release it and set up
+            // the parser to use it.
+            
+            factory.setNamespaceAware(true);
+            factory.setFeature("http://xml.org/sax/features/validation", false);
+            factory.setValidating(false);
+        }
+        
         SAXParser parser = factory.newSAXParser();
         XMLReader reader = parser.getXMLReader();
         reader.setErrorHandler(handler);
         reader.setEntityResolver(handler);
         return parser;
     }
-
+    
+    private static final boolean isTaglibDocument20OrLater (URL url)
+    {
+        InputStream input = null;
+        boolean result = false;
+        
+        try
+        {
+            SAXParserFactory factory = SAXParserFactory.newInstance();
+            SAXParser parser;
+            VersionCheckHandler handler = new VersionCheckHandler();
+            
+            // We need to create a non-validating, non-namespace aware parser used to simply check
+            // which version of the facelets taglib document we are dealing with.
+
+            factory.setNamespaceAware(false);
+            factory.setFeature("http://xml.org/sax/features/validation", false);
+            factory.setValidating(false);
+            
+            parser = factory.newSAXParser();
+            
+            input = url.openStream();
+            
+            try
+            {
+                parser.parse (input, handler);
+            }
+            
+            catch (SAXException e)
+            {
+                // This is as a result of our aborted parse, so ignore.
+            }
+            
+            result = handler.isVersion20OrLater();
+        }
+        
+        catch (Throwable e)
+        {
+            // Most likely a result of our aborted parse, so ignore.
+        }
+        
+        finally
+        {
+            if (input != null)
+            {
+                try
+                {
+                    input.close();
+                }
+                
+                catch (Throwable e)
+                {
+                }
+            }
+        }
+        
+        return result;
+    }
 }