You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@myfaces.apache.org by so...@apache.org on 2014/04/30 18:09:51 UTC

svn commit: r1591378 - in /myfaces/trinidad/trunk: trinidad-api/src/main/java/org/apache/myfaces/trinidad/skin/ trinidad-impl/src/main/java/org/apache/myfaces/trinidadinternal/skin/

Author: sobryan
Date: Wed Apr 30 16:09:51 2014
New Revision: 1591378

URL: http://svn.apache.org/r1591378
Log:
TRINIDAD-2467 - FacesMessageWrapper and Skin Addition enhancements

Thanks for the patch Venkata

Modified:
    myfaces/trinidad/trunk/trinidad-api/src/main/java/org/apache/myfaces/trinidad/skin/SkinAddition.java
    myfaces/trinidad/trunk/trinidad-impl/src/main/java/org/apache/myfaces/trinidadinternal/skin/SkinImpl.java
    myfaces/trinidad/trunk/trinidad-impl/src/main/java/org/apache/myfaces/trinidadinternal/skin/SkinUtils.java

Modified: myfaces/trinidad/trunk/trinidad-api/src/main/java/org/apache/myfaces/trinidad/skin/SkinAddition.java
URL: http://svn.apache.org/viewvc/myfaces/trinidad/trunk/trinidad-api/src/main/java/org/apache/myfaces/trinidad/skin/SkinAddition.java?rev=1591378&r1=1591377&r2=1591378&view=diff
==============================================================================
--- myfaces/trinidad/trunk/trinidad-api/src/main/java/org/apache/myfaces/trinidad/skin/SkinAddition.java (original)
+++ myfaces/trinidad/trunk/trinidad-api/src/main/java/org/apache/myfaces/trinidad/skin/SkinAddition.java Wed Apr 30 16:09:51 2014
@@ -45,7 +45,7 @@ import javax.faces.el.ValueBinding;
  * The other elements are used to create a SkinAddition object.
  * </p>
  */
-public class SkinAddition
+public class SkinAddition implements Comparable
 {
 
   /**
@@ -260,6 +260,48 @@ public class SkinAddition
   }
 
   /**
+   * Compares two skinning additions for the purposes of ordering.  Currently this
+   * method compares the stylesheet name.  If both names are null, then they are
+   * considered equal.  If one of the names is null and the other isn't, the null
+   * value is always less then the real value.  Other then that the values will be
+   * returned according to the string's natural order.
+   * 
+   * @param t
+   * @return
+   * 
+   * @throws NullPointerException if t is null
+   * @throws ClassCastException id t is not an instance of StyleSheetAddition
+   */
+  public int compareTo(Object t)
+  {
+    String comparedName = ((SkinAddition)t).getStyleSheetName();
+    String thisName     = getStyleSheetName();
+    
+    
+    if(null == thisName)
+    {
+      if(null == comparedName)
+      {
+        //Both stylesheet names are null, so they are equal
+        return 0;
+      }
+      
+      //This stylesheet name is null and the other isn't.  This is always less
+      return -1;
+    }
+    
+    if(null == comparedName)
+    {
+      //Compared object is null, this one isn't
+      return 1;
+    }
+    
+    int result = thisName.compareTo(comparedName);
+    
+    return result;
+  }
+
+  /**
    * convinience builder for SkinAddition
    * does not support the deprecated ValueBinding for translationSource
    */

Modified: myfaces/trinidad/trunk/trinidad-impl/src/main/java/org/apache/myfaces/trinidadinternal/skin/SkinImpl.java
URL: http://svn.apache.org/viewvc/myfaces/trinidad/trunk/trinidad-impl/src/main/java/org/apache/myfaces/trinidadinternal/skin/SkinImpl.java?rev=1591378&r1=1591377&r2=1591378&view=diff
==============================================================================
--- myfaces/trinidad/trunk/trinidad-impl/src/main/java/org/apache/myfaces/trinidadinternal/skin/SkinImpl.java (original)
+++ myfaces/trinidad/trunk/trinidad-impl/src/main/java/org/apache/myfaces/trinidadinternal/skin/SkinImpl.java Wed Apr 30 16:09:51 2014
@@ -279,7 +279,12 @@ abstract public class SkinImpl extends S
      {
        _skinAdditions = new ArrayList<SkinAddition>();
      }
-     _skinAdditions.add(skinAddition);
+   
+    //The following code will insert SkinAddition objects in order according to
+    //comparable.  This yields log(n) performance for ArrayList which is as good
+    //as it gets for this type of insertion.
+    int insertionPoint = Collections.binarySearch(_skinAdditions, skinAddition, null);
+    _skinAdditions.add((insertionPoint > -1) ? insertionPoint : (-insertionPoint) - 1, skinAddition);
   }
 
   /**

Modified: myfaces/trinidad/trunk/trinidad-impl/src/main/java/org/apache/myfaces/trinidadinternal/skin/SkinUtils.java
URL: http://svn.apache.org/viewvc/myfaces/trinidad/trunk/trinidad-impl/src/main/java/org/apache/myfaces/trinidadinternal/skin/SkinUtils.java?rev=1591378&r1=1591377&r2=1591378&view=diff
==============================================================================
--- myfaces/trinidad/trunk/trinidad-impl/src/main/java/org/apache/myfaces/trinidadinternal/skin/SkinUtils.java (original)
+++ myfaces/trinidad/trunk/trinidad-impl/src/main/java/org/apache/myfaces/trinidadinternal/skin/SkinUtils.java Wed Apr 30 16:09:51 2014
@@ -29,9 +29,14 @@ import java.net.URLConnection;
 import java.util.ArrayList;
 import java.util.Collections;
 import java.util.Enumeration;
+
 import java.util.HashSet;
 import java.util.Iterator;
+
+import java.util.LinkedHashMap;
+import java.util.LinkedList;
 import java.util.List;
+
 import java.util.Map;
 import java.util.Set;
 import java.util.Stack;
@@ -66,6 +71,8 @@ import org.apache.myfaces.trinidadintern
 import org.apache.myfaces.trinidadinternal.skin.parse.XMLConstants;
 import org.apache.myfaces.trinidadinternal.style.StyleContext;
 
+import org.apache.myfaces.trinidadinternal.style.xml.parse.StyleSheetDocument;
+import org.apache.myfaces.trinidadinternal.util.JsonUtils;
 import org.xml.sax.InputSource;
 
 
@@ -78,6 +85,21 @@ import org.xml.sax.InputSource;
 public class SkinUtils
 {
   /**
+   * An enumeration which determined how much information to return in the skin
+   * debug info.
+   */
+
+  public static enum DebugInfoLevel 
+  {
+    TERSE,
+    NORMAL,
+    VERBOSE;
+
+    @SuppressWarnings("compatibility:-5980442644319693066")
+    private static final long serialVersionUID = 1L;
+  }
+
+  /**
    * Tries to retrieve the default FacesContext
    * @param context FacesContext object or null
    * @return the same context object if not null, or the default FacesContext
@@ -199,75 +221,145 @@ public class SkinUtils
     return _resolveReferenceIcon(skin, refIcon, null);
   }
 
+  /**
+   * Returns skin debug information of DebugInfoLevel.NORMAL.
+   * 
+   * @param skin
+   * @return
+   */
   public static String getSkinDebugInfo(Skin skin)
   {
+    return getSkinDebugInfo(skin, DebugInfoLevel.NORMAL);
+  }
+  
+  /**
+   * Returns a JSON object containing debug information for a skin depending on
+   * the DebugInfoLevel.  This will return a information on the supplied skin and
+   * any skin it extends so that information on the skin heirarcy can be 
+   * obtained.  If DebugInfoLevel.VERBOSE is specified, this will return a class
+   * containing the following information:
+   * 
+   * id: the skin id
+   * family: the skin family
+   * version: the skin version
+   * renderkit: the skin render kit
+   * documentId: the current document checksum
+   * document: the full path to the css document
+   * features: a map of name/value pairs for the skinning features
+   * additions: a list of skinning additions (see below) for this skin
+   * parent: the json object for the stylesheet this skin extends (if any)
+   * 
+   * Skinning additions consist of the following properties:
+   * 
+   * document: the full path to the css document
+   * documentId: the skin addition checksum
+   * 
+   * The 'parent' property is only provided if the skin is a skin extension
+   * and the documentId in both the skin additions as well as the main skin object
+   * is provided only if we have a valid RenderingContext.  
+   * 
+   * If DebugInfoLevel is NORMAL, all of the 'document' properties will contain a
+   * location WITHIN the current classpath as opposed to the full resource path.
+   * 
+   * If DebugInfoLevel is TERSE, then information on the document, features, and
+   * additions will not be provided for any object.
+   * 
+   * This information is in JSON format so that it can be quickly parsed and compared
+   * to other skin information.
+   * 
+   * @param skin
+   * @param level
+   * @return
+   */
+  public static String getSkinDebugInfo(Skin skin, DebugInfoLevel level)
+  {
     assert (null != skin);
-    RenderingContext rc = RenderingContext.getCurrentInstance();
     StringBuilder sb = new StringBuilder();
-
-    sb.append("[Id: ")
-      .append(skin.getId())
-      .append(" Family: ")
-      .append(skin.getFamily())
-      .append(" Version: ")
-      .append(skin.getVersion().getName())
-      .append(" Renderkit: ")
-      .append(skin.getRenderKitId())
-      .append(" StylesheetId: ")
-      .append(skin.getStyleSheetDocumentId(rc))
-      .append(" Features: { ");
-
-    boolean first = false;
-    for (Map.Entry<String, String> entry : skin.getSkinFeatures().entrySet())
+    
+    try
     {
-      if (!first)
-      {
-        sb.append(", ");
-      }
-      else
-      {
-        first = false;
-      }
-
-      sb.append("k:")
-        .append(entry.getKey())
-        .append(" v:")
-        .append(entry.getValue());
+      JsonUtils.writeObject(sb, _getDebugInfoMap(skin, level), false);
+    }
+    catch (IOException e)
+    {
+    //We should never hit this because we control the object and the socket, but
+    //if we do, we should wrap it in a runtime exception.
+    throw new RuntimeException(e);
+    }
+      
+    return sb.toString();
+  }
+  
+  static private Map<String, Object> _getDebugInfoMap(Skin skin, DebugInfoLevel level)
+  {
+    assert (null != skin);
+    RenderingContext rc = RenderingContext.getCurrentInstance();
+    StringBuilder sb = new StringBuilder();
+      
+    //The Map which will be converted into JSON
+    Map<String, Object> m = new LinkedHashMap<String, Object>();
+    
+    m.put("id", skin.getId());
+    m.put("family", skin.getFamily());
+    m.put("version", skin.getVersion().getName());
+    m.put("renderkit", skin.getRenderKitId());
+    
+    if(null != rc)
+    {
+      m.put("documentId", skin.getStyleSheetDocumentId(rc));
     }
 
-    sb.append(" } ");
-
-    if (rc instanceof CoreRenderingContext)
+    //Skip all of this information for terse logging
+    if(!DebugInfoLevel.TERSE.equals(level))
     {
-      sb.append(" Additions: {");
+      m.put("document", _getStyleSheetLocation(skin.getStyleSheetName(), level));
+      m.put("features", skin.getSkinFeatures());
 
-      StyleContext sctx = ((CoreRenderingContext) rc).getStyleContext();
+      StyleContext sctx =(null != rc && rc instanceof CoreRenderingContext)?((CoreRenderingContext)rc).getStyleContext():null;  
       List<SkinAddition> additions = skin.getSkinAdditions();
 
-      first = true;
+      List<Map<String,String>> additionList = new LinkedList<Map<String,String>>();
+      m.put("additions",additionList);
+        
       for (SkinAddition addition : additions)
       {
-        if (!first)
+        Map<String,String> additionMap = new LinkedHashMap<String,String>();
+        additionList.add(additionMap);
+        
+        String styleSheetName = addition.getStyleSheetName();
+        additionMap.put("document", _getStyleSheetLocation(styleSheetName, level));
+        if(null != sctx)
+        {
+          StyleSheetEntry entry = StyleSheetEntry.createEntry(sctx, styleSheetName);
+          
+          String documentId="Unknown - Entry could not be created";
+          if(null != entry)
         {
-          sb.append(", ");
+            StyleSheetDocument document = entry.getDocument();
+            
+            documentId = (null == document)?"Unknown - Document could not be created":document.getDocumentId(sctx);
+          }
+          
+          additionMap.put("documentId", documentId);
+        }
+      }
         }
-        else
+    
+    skin = _findParentSkin(skin);
+    if(null != skin)
         {
-          first = false;
+      m.put("parent", _getDebugInfoMap(skin, level));
         }
 
-        String styleSheetName = addition.getStyleSheetName();
-        sb.append("\"")
-          .append(styleSheetName)
-          .append("\"(");
-        StyleSheetEntry entry = StyleSheetEntry.createEntry(sctx, styleSheetName);
-        sb.append(entry.getDocument().getDocumentId(sctx))
-          .append(")");
-      }
-      sb.append("}");
+    return m;
     }
 
-    return sb.append("]").toString();
+  static private String _getStyleSheetLocation(String location, DebugInfoLevel level)
+  {
+    URL url = ClassLoaderUtils.getResource(location);
+    String loc = (null == url)?location:url.getPath();
+    
+    return DebugInfoLevel.VERBOSE.equals(level)? loc:location;
   }
 
   /**
@@ -412,6 +504,24 @@ public class SkinUtils
     return manager;
   }
 
+  //This will unwrap the skin of any RequestSkinWrappers and look for a SkinExtension.
+  //If one is found, it will return the parent skin.
+  static private Skin _findParentSkin(Skin skin)
+  {
+    //First lets unwrap
+    while(skin instanceof RequestSkinWrapper)
+    {
+      skin = ((RequestSkinWrapper)skin).getWrappedSkin();
+    }
+    
+    if(skin instanceof SkinExtension)
+    {
+      return ((SkinExtension)skin).getBaseSkin();
+    }
+    
+    return null;
+  }
+
   // Returns a singleton instance of the default ParserManager
   static private ParserManager _getDefaultManager()
   {