View Javadoc

1   package org.apache.portals.bridges.struts.config;
2   
3   import java.io.Serializable;
4   import java.util.ArrayList;
5   import java.util.Enumeration;
6   import java.util.List;
7   
8   import javax.servlet.http.HttpServletRequest;
9   import javax.servlet.http.HttpSession;
10  
11  import org.apache.commons.digester.Digester;
12  
13  public class RenderContextAttributes extends AbstractConfigComponent
14  {
15      private static class AttributeValue implements Serializable
16      {
17          private String  name;
18          private Object  value;
19          
20          public AttributeValue(String name, Object value)
21          {
22              super();
23              this.name = name;
24              this.value = value;
25          }
26          
27          public String getName()
28          {
29              return name;
30          }
31          
32          public Object getValue()
33          {
34              return value;
35          }
36      }
37      
38      public static class Attribute
39      {
40          private String value;
41          private boolean prefix;
42          private boolean keep;
43  
44          public Attribute()
45          {        
46          }
47          
48          public boolean isKeep()
49          {
50              return keep;
51          }
52          
53          public void setKeep(boolean keep)
54          {
55              this.keep = keep;
56          }
57          
58          public boolean isPrefixAttr()
59          {
60              return prefix;
61          }
62  
63          public String getValue()
64          {
65              return value;
66          }
67          
68          public void setName(String value)
69          {
70              this.value = value;
71              this.prefix = false;
72          }
73          
74          public void setPrefix(String value)
75          {
76              this.value = value;
77              this.prefix = true;
78          }
79      }
80      
81      private String name = this.getClass().getName();
82      private Attribute[] namedAttributes;
83      private Attribute[] prefixAttributes;
84      private ArrayList namedAttributesList;
85      private ArrayList prefixAttributesList;
86      
87      public RenderContextAttributes()
88      {
89          namedAttributesList = new ArrayList();
90          prefixAttributesList = new ArrayList();
91      }
92      
93      private Attribute[] createArray(List attributes)
94      {
95          Attribute[] array = null;
96          if ( attributes != null && attributes.size() > 0 )
97          {
98              array = new Attribute[attributes.size()];
99              for ( int i = 0; i < array.length; i++ )
100             {
101                 array[i] = (Attribute)attributes.get(i);
102             }
103         }
104         return array;
105     }
106     
107     public void addAttribute(Attribute attribute)
108     {
109         checkLoaded();
110         
111         if (attribute.isPrefixAttr())
112         {
113             prefixAttributesList.add(attribute);
114         }
115         else
116         {
117             namedAttributesList.add(attribute);            
118         }
119     }
120     
121     public void setName(String name)
122     {
123         checkLoaded();
124         this.name = name;
125     }
126     
127     public void configure(Digester digester)
128     {
129         digester.addRule("config/render-context", new SetParentRule(this));
130         digester.addSetProperties("config/render-context");
131         digester.addObjectCreate("config/render-context/attribute", Attribute.class);
132         digester.addSetProperties("config/render-context/attribute");
133         digester.addSetNext("config/render-context/attribute", "addAttribute");
134         digester.addCallMethod("config/render-context", "afterLoad");
135         
136     }
137     
138     public void afterLoad()
139     {
140         super.afterLoad();
141 
142         namedAttributes = createArray(namedAttributesList);
143         prefixAttributes = createArray(prefixAttributesList);
144         
145         namedAttributesList = null;
146         prefixAttributesList = null;
147     }
148     
149     public void saveAttributes(HttpServletRequest request)
150     {
151         ArrayList keepAttributes = new ArrayList();
152         ArrayList tempAttributes = new ArrayList();
153         ArrayList savedNames = new ArrayList();
154         if ( namedAttributes != null )
155         {
156             for ( int i = 0; i < namedAttributes.length; i++ )
157             {
158                 Object value = request.getAttribute(namedAttributes[i].getValue());
159                 if ( value != null )
160                 {
161                     AttributeValue attributeValue = new AttributeValue(namedAttributes[i].getValue(), value);
162                     savedNames.add(attributeValue.getName());
163                     if ( namedAttributes[i].isKeep() )
164                     {
165                         keepAttributes.add(attributeValue);
166                     }
167                     else
168                     {
169                         tempAttributes.add(attributeValue);
170                     }                    
171                 }
172             }
173         }
174         if ( prefixAttributes != null )
175         {
176             Enumeration names = request.getAttributeNames();
177             while ( names.hasMoreElements() )
178             {
179                 String name = (String)names.nextElement();
180                 for ( int i = 0; i < prefixAttributes.length; i++ )
181                 {
182                     if (!savedNames.contains(name) && name.startsWith(prefixAttributes[i].getValue()))
183                     {
184                         AttributeValue attributeValue = new AttributeValue(name, request.getAttribute(name));
185                         savedNames.add(name);
186                         if (prefixAttributes[i].isKeep())
187                         {
188                             keepAttributes.add(attributeValue);
189                         }
190                         else
191                         {
192                             tempAttributes.add(attributeValue);
193                         }                    
194                     }
195                 }
196             }
197         }
198         if (keepAttributes.size() > 0)
199         {
200             if (tempAttributes.size() > 0)
201             {
202                 keepAttributes.add(null); // indicating subsequent attributeValues are temporarily
203                 keepAttributes.addAll(tempAttributes);
204             }
205             request.getSession().setAttribute(name,keepAttributes);
206         }
207         else if (tempAttributes.size() > 0)
208         {
209             tempAttributes.add(0,null); // indicating subsequent attributeValues are temporarily
210             request.getSession().setAttribute(name,tempAttributes);
211         }
212     }
213     
214     public void clearAttributes(HttpSession session)
215     {
216         session.removeAttribute(name);
217     }
218     
219     public void restoreAttributes(HttpServletRequest request)
220     {
221         ArrayList attributes = (ArrayList)request.getSession().getAttribute(name);
222         if ( attributes != null )
223         {
224             for ( int size = attributes.size(), i = size - 1 ; i > -1; i-- )
225             {
226                 AttributeValue attributeValue = (AttributeValue)attributes.get(i);
227                 if ( attributeValue == null )
228                 {
229                     if ( i == 0 )
230                     {
231                         request.getSession().removeAttribute(name);
232                     }
233                     else
234                     {
235                         // remove this and previously retrieved attributeValues as being temporarily
236                         while (size > i )
237                         {
238                             attributes.remove(--size);
239                         }
240                     }
241                 }
242                 else
243                 {
244                     request.setAttribute(attributeValue.getName(), attributeValue.getValue());
245                 }
246             }
247         }
248     }
249 }