View Javadoc

1   /*
2    * Copyright 2000-2001,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  package org.apache.jetspeed.om.profile;
18  
19  import org.apache.jetspeed.om.profile.Portlets;
20  import org.apache.jetspeed.om.profile.Entry;
21  import java.util.Iterator;
22  
23  /***
24   * This class represents a loaded PSML document in memory, providing
25   * all facilities for finding and updating specific parts of the 
26   * document.
27   *
28   * @author <a href="mailto:raphael@apache.org">RaphaŽl Luta</a>
29   * @version $Id: BasePSMLDocument.java,v 1.9 2004/02/23 03:05:01 jford Exp $
30   */
31  public class BasePSMLDocument implements PSMLDocument
32  {
33      /***
34       * The name of this PSML document, will be typically the URL of the file
35       * for file-based implementations
36       */
37      private String name = null;
38  
39      /***
40       * The PortletSet descriptions that make up this document
41       */
42      private Portlets portlets = null;
43  
44      /***
45       * Construct a new empty PSMLDocument
46       */
47      public BasePSMLDocument()
48      {
49          // empty constructor
50      }
51  
52      /***
53       * Construct a new named PSMLDocument associated with the specified
54       * PSML portlet set description
55       *
56       * @param name the name of this document
57       * @param portlets the PSML memory structure
58       */
59      public BasePSMLDocument( String name, Portlets portlets )
60      {
61          this.name = name;
62          this.portlets = portlets;
63      }
64      
65      /***
66       * Return the name of this document
67       */
68      public final String getName()
69      {
70          return this.name;
71      }
72          
73      /***
74       * Sets a new name for this document
75       * 
76       * @param name the new document name
77       */
78      public final void setName(String name)
79      {
80          this.name = name;
81      }
82  
83      /***
84       * Return ths portlet set PSML description of this document
85       *
86       * @return a PSML object model hierarchy, or null if none is 
87       * defined for this document
88       */
89      public final Portlets getPortlets()
90      {
91          return this.portlets;
92      }
93  
94      /***
95       * Sets a new PSML object model for this document
96       * 
97       * @param portlets the PSML object model
98       */
99      public final void setPortlets(Portlets portlets)
100     {
101         this.portlets = portlets;
102     }
103 
104     /*** Returns the first entry in the current PSML resource corresponding 
105      *  to the given portlet name
106      * 
107      *  @param name the portlet name to seek
108      *  @return the found entry description or null
109      */
110     public Entry getEntry(String name)
111     {
112         return getEntry(this.portlets, name);
113     }
114 
115     /*** Returns the first entry in the current PSML resource corresponding 
116      *  to the given entry id
117      * 
118      *  @param entryId the portlet's entry id to seek
119      *  @return the found entry description or null
120      */
121     public Entry getEntryById(String entryId)
122     {
123         return getEntryById(this.portlets, entryId);
124     }
125 
126     /*** Returns the first portlets element in the current PSML resource corresponding 
127      *  to the given name
128      * 
129      *  @param name the portlets name to seek
130      *  @return the found portlets description or null
131      */
132     public Portlets getPortlets(String name)
133     {        
134         Portlets p = getPortlets(this.portlets, name);
135         
136         if (p == null)
137         {
138             //maybe name is a position...
139             try
140             {
141                 p = getPortlets(Integer.parseInt(name));
142             }
143             catch (NumberFormatException e)
144             {
145                 // this will happen if name is not parseable, ignore
146             }
147         }
148         
149         return p;
150     }
151 
152     /*** Returns the first portlets element in the current PSML resource corresponding 
153      *  to the given id
154      * 
155      *  @param name the portlets id to seek
156      *  @return the found portlets description or null
157      */
158     public Portlets getPortletsById(String portletId)
159     {        
160         Portlets p = getPortletsById(this.portlets, portletId);
161         return p;
162     }
163 
164     /*** Returns the first portlets element in the current PSML resource 
165      *  found at the specified position. The position is computed using
166      *  a left-most tree traversal algorithm of the existing portlets (thus
167      *  not counting other entry objects)
168      * 
169      *  @param position the sought position
170      *  @return the found portlets object or null if we did not find such an
171      *  object
172      */
173     public Portlets getPortlets(int position)
174     {
175         return getPortlets(this.portlets, position, 0);
176     }
177 
178     /*** Returns the first entry in the specified PSML resource corresponding 
179      *  to the given portlet name
180      * 
181      *  @param portlets the PSML description to look into
182      *  @param name the portlet name to seek
183      *  @return the found entry description or null
184      */
185     public static Entry getEntry(Portlets portlets, String name)
186     {
187         Entry entry = null;
188 
189         for (Iterator it1 = portlets.getEntriesIterator(); it1.hasNext(); )
190         {
191             entry = (Entry) it1.next();
192             if (entry.getParent().equals (name))
193                  return (entry);
194         }
195 
196         entry = null;
197 
198         for (Iterator it2 = portlets.getPortletsIterator(); it2.hasNext(); )
199         {
200             Portlets p = (Portlets) it2.next();
201 
202             entry = getEntry(p, name);
203 
204             if (entry != null)
205                  break;
206         }
207 
208         return (entry);
209     }
210 
211     /*** Returns the first entry in the specified PSML resource corresponding 
212      *  to the given portlet Id
213      * 
214      *  @param portlets the PSML description to look into
215      *  @param entryId the portlet's entry id to seek
216      *  @return the found entry description or null
217      */
218     public static Entry getEntryById(Portlets portlets, String entryId)
219     {
220         Entry entry = null;
221 
222         for (Iterator it1 = portlets.getEntriesIterator(); it1.hasNext(); )
223         {
224             entry = (Entry) it1.next();
225             if ((entry.getId()!=null) && entry.getId().equals (entryId))
226                  return (entry);
227         }
228 
229         entry = null;
230 
231         for (Iterator it2 = portlets.getPortletsIterator(); it2.hasNext(); )
232         {
233             Portlets p = (Portlets) it2.next();
234 
235             entry = getEntryById(p, entryId);
236 
237             if (entry != null)
238                  break;
239         }
240 
241         return (entry);
242     }
243 
244     /*** Returns the first portlets element in the specified PSML resource corresponding 
245      *  to the given Id
246      * 
247      *  @param portlets the PSML description to look into
248      *  @param portletId the portlet's id to seek
249      *  @return the found portlets description or null
250      */
251     public static Portlets getPortletsById(Portlets portlets, String portletId)
252     {
253         Portlets entry = portlets;
254         
255         if ( (entry.getId()!=null) && entry.getId().equals(portletId) )
256         {
257             return entry;
258         }
259 
260         entry = null;
261         
262         for (Iterator it2 = portlets.getPortletsIterator(); it2.hasNext(); )
263         {
264             Portlets p = (Portlets) it2.next();
265 
266             entry = getPortletsById(p, portletId);
267 
268             if (entry != null) break;
269         }
270 
271         return (entry);
272     }
273 
274     /*** Returns the first portlets element in the specified PSML resource corresponding 
275      *  to the given name
276      * 
277      *  @param portlets the PSML description to look into
278      *  @param name the portlets name to seek
279      *  @return the found portlets description or null
280      */
281     public static Portlets getPortlets(Portlets portlets, String name)
282     {
283         Portlets entry = portlets;
284         
285         if ( (entry.getName()!=null) && entry.getName().equals(name) )
286         {
287             return entry;
288         }
289 
290         entry = null;
291         
292         for (Iterator it2 = portlets.getPortletsIterator(); it2.hasNext(); )
293         {
294             Portlets p = (Portlets) it2.next();
295 
296             entry = getPortlets(p, name);
297 
298             if (entry != null) break;
299         }
300 
301         return (entry);
302     }
303     /*** Returns the first portlets element in the specified PSML resource 
304      *  in the given position
305      * 
306      *  @param portlets the PSML description to look into
307      *  @param position the position to look for
308      *  @param count the numbering for the portlets passed as parameter
309      *  @return the found portlets description or null
310      */
311     public static Portlets getPortlets(Portlets portlets, int position, int count)
312     {
313         // sanity check, we never look after the sought position
314         if (position < count)
315         {
316             return null;
317         }
318         
319         // the base portlets is the one we're looking for...
320         if (position == count)
321         {
322             return portlets;
323         }
324 
325         // we need to recurse in the children
326         Portlets result = null;
327         
328         for (Iterator it2 = portlets.getPortletsIterator(); it2.hasNext(); )
329         {
330             Portlets p = (Portlets) it2.next();
331             count++;
332             result = getPortlets(p, position, count);
333 
334             if (result != null) break;
335         }
336 
337         return result;
338     }
339     /***
340      * Remove the Entry in the specified PSML resource corresponding 
341      * to the given portlet Id
342      * 
343      * @param entryId the portlet's entry id to seek
344      */
345     public boolean removeEntryById(String entryId)
346     {        
347         return removeEntryById(this.portlets, entryId);
348     }
349 
350     /***
351      * Remove the Entry in the specified PSML resource corresponding 
352      * to the given portlet Id
353      * 
354      * @param portlets the PSML description to look into
355      * @param entryId the portlet's entry id to seek
356      */
357     public static boolean removeEntryById(Portlets portlets, String entryId)
358     {
359         for (int i=0; i < portlets.getEntryCount(); i++)
360         {
361             if ( entryId.equals(portlets.getEntry(i).getId()) )
362             {
363                 portlets.removeEntry(i);
364                 return true;
365             }
366         }
367 
368         for (Iterator it2 = portlets.getPortletsIterator(); it2.hasNext(); )
369         {
370             Portlets p = (Portlets) it2.next();
371 
372             if (removeEntryById(p, entryId) == true)
373                 return true;
374         }
375 
376         return false;
377     }
378 
379     /***
380      * Create a clone of this object
381      */
382     public Object clone()
383         throws java.lang.CloneNotSupportedException
384     {
385         Object cloned = super.clone();
386         
387         // clone the portlets
388         ((BasePSMLDocument)cloned).portlets = ((this.portlets == null) ? null : (Portlets) this.portlets.clone());
389         
390         return cloned;
391     }
392 }
393