Coverage Report - org.apache.myfaces.application.TreeStructureManager
 
Classes in this File Line Coverage Branch Coverage Complexity
TreeStructureManager
0%
0/51
0%
0/22
0
TreeStructureManager$TreeStructComponent
0%
0/14
N/A
0
 
 1  
 /*
 2  
  * Licensed to the Apache Software Foundation (ASF) under one
 3  
  * or more contributor license agreements.  See the NOTICE file
 4  
  * distributed with this work for additional information
 5  
  * regarding copyright ownership.  The ASF licenses this file
 6  
  * to you under the Apache License, Version 2.0 (the
 7  
  * "License"); you may not use this file except in compliance
 8  
  * with the License.  You may obtain a copy of the License at
 9  
  *
 10  
  *   http://www.apache.org/licenses/LICENSE-2.0
 11  
  *
 12  
  * Unless required by applicable law or agreed to in writing,
 13  
  * software distributed under the License is distributed on an
 14  
  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 15  
  * KIND, either express or implied.  See the License for the
 16  
  * specific language governing permissions and limitations
 17  
  * under the License.
 18  
  */
 19  
 package org.apache.myfaces.application;
 20  
 
 21  
 import org.apache.myfaces.shared_impl.util.ClassUtils;
 22  
 
 23  
 import javax.faces.component.UIComponent;
 24  
 import javax.faces.component.UIViewRoot;
 25  
 import java.io.Serializable;
 26  
 import java.util.ArrayList;
 27  
 import java.util.Iterator;
 28  
 import java.util.List;
 29  
 import java.util.Map;
 30  
 
 31  
 /**
 32  
  * @author Manfred Geiler (latest modification by $Author: skitching $)
 33  
  * @version $Revision: 684459 $ $Date: 2008-08-10 06:13:56 -0500 (Sun, 10 Aug 2008) $
 34  
  */
 35  
 public class TreeStructureManager
 36  
 {
 37  
     //private static final Log log = LogFactory.getLog(TreeStructureManager.class);
 38  
 
 39  
     //private FacesContext _facesContext;
 40  
 
 41  
     public TreeStructureManager()
 42  0
     {
 43  
         //_facesContext = facesContext;
 44  0
     }
 45  
 
 46  
     public Object buildTreeStructureToSave(UIViewRoot viewRoot)
 47  
     {
 48  0
         return internalBuildTreeStructureToSave(viewRoot);
 49  
     }
 50  
 
 51  
     private TreeStructComponent internalBuildTreeStructureToSave(UIComponent component)
 52  
     {
 53  0
         TreeStructComponent structComp = new TreeStructComponent(component.getClass().getName(),
 54  
                                                                  component.getId());
 55  
 
 56  
         //children
 57  0
         if (component.getChildCount() > 0)
 58  
         {
 59  0
             List childList = component.getChildren();
 60  0
             List<TreeStructComponent> structChildList = new ArrayList<TreeStructComponent>();
 61  0
             for (int i = 0, len = childList.size(); i < len; i++)
 62  
             {
 63  0
                 UIComponent child = (UIComponent)childList.get(i);
 64  0
                 if (!child.isTransient())
 65  
                 {
 66  0
                     TreeStructComponent structChild = internalBuildTreeStructureToSave(child);
 67  0
                     structChildList.add(structChild);
 68  
                 }
 69  
             }
 70  0
             TreeStructComponent[] childArray = structChildList.toArray(new TreeStructComponent[structChildList.size()]);
 71  0
             structComp.setChildren(childArray);
 72  
         }
 73  
 
 74  
         //facets
 75  0
         Map<String, UIComponent> facetMap = component.getFacets();
 76  0
         if (!facetMap.isEmpty())
 77  
         {
 78  0
             List<Object[]> structFacetList = new ArrayList<Object[]>();
 79  0
             for (Iterator it = facetMap.entrySet().iterator(); it.hasNext(); )
 80  
             {
 81  0
                 Map.Entry entry = (Map.Entry)it.next();
 82  0
                 UIComponent child = (UIComponent)entry.getValue();
 83  0
                 if (!child.isTransient())
 84  
                 {
 85  0
                     String facetName = (String)entry.getKey();
 86  0
                     TreeStructComponent structChild = internalBuildTreeStructureToSave(child);
 87  0
                     structFacetList.add(new Object[] {facetName, structChild});
 88  
                 }
 89  0
             }
 90  0
             Object[] facetArray = structFacetList.toArray(new Object[structFacetList.size()]);
 91  0
             structComp.setFacets(facetArray);
 92  
         }
 93  
 
 94  0
         return structComp;
 95  
     }
 96  
 
 97  
 
 98  
     public UIViewRoot restoreTreeStructure(Object treeStructRoot)
 99  
     {
 100  0
         if (treeStructRoot instanceof TreeStructComponent)
 101  
         {
 102  0
             return (UIViewRoot)internalRestoreTreeStructure((TreeStructComponent)treeStructRoot);
 103  
         }
 104  
         
 105  
         
 106  0
         throw new IllegalArgumentException("TreeStructure of type " + treeStructRoot.getClass().getName() + " is not supported.");
 107  
         
 108  
     }
 109  
 
 110  
     private UIComponent internalRestoreTreeStructure(TreeStructComponent treeStructComp)
 111  
     {
 112  0
         String compClass = treeStructComp.getComponentClass();
 113  0
         String compId = treeStructComp.getComponentId();
 114  0
         UIComponent component = (UIComponent)ClassUtils.newInstance(compClass);
 115  0
         component.setId(compId);
 116  
 
 117  
         //children
 118  0
         TreeStructComponent[] childArray = treeStructComp.getChildren();
 119  0
         if (childArray != null)
 120  
         {
 121  0
             List<UIComponent> childList = component.getChildren();
 122  0
             for (int i = 0, len = childArray.length; i < len; i++)
 123  
             {
 124  0
                 UIComponent child = internalRestoreTreeStructure(childArray[i]);
 125  0
                 childList.add(child);
 126  
             }
 127  
         }
 128  
 
 129  
         //facets
 130  0
         Object[] facetArray = treeStructComp.getFacets();
 131  0
         if (facetArray != null)
 132  
         {
 133  0
             Map<String, UIComponent> facetMap = component.getFacets();
 134  0
             for (int i = 0, len = facetArray.length; i < len; i++)
 135  
             {
 136  0
                 Object[] tuple = (Object[])facetArray[i];
 137  0
                 String facetName = (String)tuple[0];
 138  0
                 TreeStructComponent structChild = (TreeStructComponent)tuple[1];
 139  0
                 UIComponent child = internalRestoreTreeStructure(structChild);
 140  0
                 facetMap.put(facetName, child);
 141  
             }
 142  
         }
 143  
 
 144  0
         return component;
 145  
     }
 146  
 
 147  
 
 148  
     public static class TreeStructComponent
 149  
             implements Serializable
 150  
     {
 151  
         private static final long serialVersionUID = 5069109074684737231L;
 152  
         private String _componentClass;
 153  
         private String _componentId;
 154  0
         private TreeStructComponent[] _children = null;    // Array of children
 155  0
         private Object[] _facets = null;                   // Array of Array-tuples with Facetname and TreeStructComponent
 156  
 
 157  
         TreeStructComponent(String componentClass, String componentId)
 158  0
         {
 159  0
             _componentClass = componentClass;
 160  0
             _componentId = componentId;
 161  0
         }
 162  
 
 163  
         public String getComponentClass()
 164  
         {
 165  0
             return _componentClass;
 166  
         }
 167  
 
 168  
         public String getComponentId()
 169  
         {
 170  0
             return _componentId;
 171  
         }
 172  
 
 173  
         void setChildren(TreeStructComponent[] children)
 174  
         {
 175  0
             _children = children;
 176  0
         }
 177  
 
 178  
         TreeStructComponent[] getChildren()
 179  
         {
 180  0
             return _children;
 181  
         }
 182  
 
 183  
         Object[] getFacets()
 184  
         {
 185  0
             return _facets;
 186  
         }
 187  
 
 188  
         void setFacets(Object[] facets)
 189  
         {
 190  0
             _facets = facets;
 191  0
         }
 192  
     }
 193  
 
 194  
 }