View Javadoc

1   /*
2    * Copyright 2003,2004 The Apache Software Foundation.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  /* 
17  
18   */
19  
20  package org.apache.pluto.portalImpl.om.page.impl;
21  
22  import java.lang.reflect.Constructor;
23  import java.lang.reflect.InvocationTargetException;
24  import java.util.ArrayList;
25  import java.util.Collection;
26  import java.util.Iterator;
27  
28  import javax.servlet.ServletConfig;
29  
30  import org.apache.pluto.portalImpl.om.page.Fragment;
31  import org.apache.pluto.portalImpl.om.page.Navigation;
32  import org.apache.pluto.portalImpl.services.log.Log;
33  import org.apache.pluto.util.StringUtils;
34  
35  public class FragmentImpl implements Fragment, java.io.Serializable {
36  
37      private String name;
38      private String classname;
39      private String type;
40      private Navigation navigation;
41      private ArrayList fragments;
42      private ArrayList properties;
43  
44      public FragmentImpl()
45      {
46          name = "";
47          fragments = new ArrayList();
48          properties = new ArrayList();
49      }
50  
51      // Fragment implementation.
52  
53      public String getName()
54      {
55          return name;
56      }
57  
58      public void setName(String name)
59      {
60          this.name = name;
61      }
62  
63      public String getClassname()
64      {
65          return classname;
66      }
67  
68      public void setClassname(String classname)
69      {
70          this.classname = classname;
71      }
72  
73      public String getType()
74      {
75          return type;
76      }
77  
78      public void setType(String type)
79      {
80          this.type = type;
81      }
82  
83      public Navigation getNavigation()
84      {
85          return navigation;
86      }
87  
88      public void setNavigation(Navigation navigation)
89      {
90          this.navigation = navigation;
91      }
92  
93      public Collection getFragments()
94      {
95          return fragments;
96      }
97  
98      public Collection getProperties()
99      {
100         return properties;
101     }
102 
103     // additional methods.
104     
105     public void setFragments(Collection fragments)
106     {
107         this.fragments = new ArrayList(fragments);
108     }
109 
110     public org.apache.pluto.portalImpl.aggregation.Fragment build(ServletConfig config, 
111                                                                   org.apache.pluto.portalImpl.aggregation.Fragment fragmentParent)
112     throws Exception
113     {        
114         org.apache.pluto.portalImpl.aggregation.navigation.Navigation 
115         nav = fragmentParent.getNavigation();
116         // create navigation class, if navigational information is available
117         if (navigation!=null) {
118             nav = new org.apache.pluto.portalImpl.aggregation.navigation.Navigation(fragmentParent.getNavigation(),
119                                                                                     navigation);
120         }
121 
122         org.apache.pluto.portalImpl.aggregation.Fragment fragment = null;
123         if (classname == null) {
124             if (type == null) {
125                 Log.error("No type defined in pageregistry.xml");
126                 return null;
127             }
128             if (type.equalsIgnoreCase("page")) {
129                 fragment = new org.apache.pluto.portalImpl.aggregation.PageFragment(getName(),
130                                                                                     config,
131                                                                                     fragmentParent,
132                                                                                     this,
133                                                                                     nav);
134             } else if (type.equalsIgnoreCase("row")) {
135                 fragment = new org.apache.pluto.portalImpl.aggregation.RowFragment(getName(),
136                                                                                    config,
137                                                                                    fragmentParent,
138                                                                                    this,
139                                                                                    nav);
140             } else if (type.equalsIgnoreCase("column")) {
141                 fragment = new org.apache.pluto.portalImpl.aggregation.ColumnFragment(getName(),
142                                                                                       config,
143                                                                                       fragmentParent,
144                                                                                       this,
145                                                                                       nav);
146             } else if (type.equalsIgnoreCase("portlet")) {
147                 fragment = new org.apache.pluto.portalImpl.aggregation.PortletFragment(getName(),
148                                                                                        config,
149                                                                                        fragmentParent,
150                                                                                        this,
151                                                                                        nav);
152             } else {
153                 Log.error("Unknown type "+type+" defined in pageregistry.xml");
154                 return null;
155             }
156         } else {
157             // create fragment class
158             Class[] parameterClasses = {
159                 String.class,
160                 ServletConfig.class, 
161                 org.apache.pluto.portalImpl.aggregation.Fragment.class, 
162                 org.apache.pluto.portalImpl.om.page.Fragment.class,
163                 org.apache.pluto.portalImpl.aggregation.navigation.Navigation.class
164             };
165             Constructor constructor = null;
166             try {
167                 constructor = Class.forName(classname).getConstructor(parameterClasses);
168             } catch (NoSuchMethodException e) {
169                 Log.error("Invalid class or classname defined in pageregistry.xml",e);
170                 return null;
171             } catch (ClassNotFoundException e) {
172                 Log.error("Invalid class or classname defined in pageregistry.xml",e);
173                 return null;
174             }
175             Object[] parameters = {
176                 getName(),
177                 config,
178                 fragmentParent,
179                 this,
180                 nav
181             };
182             try {
183                 Object _fragment = constructor.newInstance(parameters);
184                 if (!(_fragment instanceof org.apache.pluto.portalImpl.aggregation.Fragment)) {
185                     Log.error("Invalid class or classname defined in pageregistry.xml");
186                     return null;
187                 }
188                 fragment = (org.apache.pluto.portalImpl.aggregation.Fragment)_fragment;
189             } catch (InstantiationException e) {
190                 Log.error("Invalid class or classname defined in pageregistry.xml",e);
191                 return null;
192             } catch (IllegalAccessException e) {
193                 Log.error("Invalid class or classname defined in pageregistry.xml",e);
194                 return null;
195             } catch (InvocationTargetException e) {
196                 Log.error("Invalid class or classname defined in pageregistry.xml",e);
197                 return null;
198             }
199         }        
200 
201         // bind all data to the newly created navigational object
202         if (navigation!=null) {
203             nav.setLinkedFragment(fragment);
204         }
205 
206         // create next level of fragments
207         Iterator iterator = fragments.iterator();
208 
209         while (iterator.hasNext()) {
210             FragmentImpl fragmentimpl = (FragmentImpl)iterator.next();
211 
212             org.apache.pluto.portalImpl.aggregation.Fragment _fragment = 
213             fragmentimpl.build(config, fragment);
214             if (_fragment != null) {
215                 fragment.addChild(_fragment);
216             }
217         }
218 
219         return fragment;
220     }
221 
222     public String toString()
223     {
224         return toString(0);
225     }
226 
227     public String toString(int indent)
228     {
229         StringBuffer buffer = new StringBuffer(1000);
230         StringUtils.newLine(buffer,indent);
231         buffer.append(getClass().toString()); buffer.append(":");
232         StringUtils.newLine(buffer,indent);
233         buffer.append("{");
234         StringUtils.newLine(buffer,indent);
235         buffer.append("name='");
236         buffer.append(name);
237         buffer.append("'");
238         if (classname!=null) {
239             StringUtils.newLine(buffer,indent);
240             buffer.append("classname='");
241             buffer.append(classname);
242             buffer.append("'");
243         }
244         if (type!=null) {
245             StringUtils.newLine(buffer,indent);
246             buffer.append("type='");
247             buffer.append(type);
248             buffer.append("'");
249         }
250         if (navigation!=null) {
251             StringUtils.newLine(buffer,indent);
252             buffer.append("Navigation:");
253             buffer.append(((NavigationImpl)navigation).toString(indent));
254         }
255         Iterator iterator = properties.iterator();
256         if (iterator.hasNext()) {
257             StringUtils.newLine(buffer,indent);
258             buffer.append("Properties:");
259         }
260         while (iterator.hasNext()) {
261             buffer.append(((PropertyImpl)iterator.next()).toString(indent));
262         }
263         iterator = fragments.iterator();
264         if (iterator.hasNext()) {
265             StringUtils.newLine(buffer,indent);
266             buffer.append("Fragments:");
267         }
268         while (iterator.hasNext()) {
269             buffer.append(((FragmentImpl)iterator.next()).toString(indent+2));
270         }
271         StringUtils.newLine(buffer,indent);
272         buffer.append("}");
273         return buffer.toString();
274     }
275 
276 }