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 at7 * 8 * http://www.apache.org/licenses/LICENSE-2.09 * 10 * Unless required by applicable law or agreed to in writing, software11 * 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 and14 * limitations under the License.15 */1617packageorg.apache.jetspeed.om.profile;
1819import org.apache.jetspeed.om.profile.Portlets;
20import org.apache.jetspeed.om.profile.Entry;
21import java.util.Iterator;
2223/***24 * This class represents a loaded PSML document in memory, providing25 * 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 */31publicclassBasePSMLDocument implements PSMLDocument32 {
33/***34 * The name of this PSML document, will be typically the URL of the file35 * for file-based implementations36 */37private String name = null;
3839/***40 * The PortletSet descriptions that make up this document41 */42privatePortlets portlets = null;
4344/***45 * Construct a new empty PSMLDocument46 */47publicBasePSMLDocument()
48 {
49// empty constructor50 }
5152/***53 * Construct a new named PSMLDocument associated with the specified54 * PSML portlet set description55 *56 * @param name the name of this document57 * @param portlets the PSML memory structure58 */59publicBasePSMLDocument( String name, Portlets portlets )
60 {
61this.name = name;
62this.portlets = portlets;
63 }
6465/***66 * Return the name of this document67 */68publicfinal String getName()
69 {
70returnthis.name;
71 }
7273/***74 * Sets a new name for this document75 * 76 * @param name the new document name77 */78publicfinalvoid setName(String name)
79 {
80this.name = name;
81 }
8283/***84 * Return ths portlet set PSML description of this document85 *86 * @return a PSML object model hierarchy, or null if none is 87 * defined for this document88 */89publicfinalPortlets getPortlets()
90 {
91returnthis.portlets;
92 }
9394/***95 * Sets a new PSML object model for this document96 * 97 * @param portlets the PSML object model98 */99publicfinalvoid setPortlets(Portlets portlets)
100 {
101this.portlets = portlets;
102 }
103104/*** Returns the first entry in the current PSML resource corresponding 105 * to the given portlet name106 * 107 * @param name the portlet name to seek108 * @return the found entry description or null109 */110publicEntry getEntry(String name)
111 {
112return getEntry(this.portlets, name);
113 }
114115/*** Returns the first entry in the current PSML resource corresponding 116 * to the given entry id117 * 118 * @param entryId the portlet's entry id to seek119 * @return the found entry description or null120 */121publicEntry getEntryById(String entryId)
122 {
123return getEntryById(this.portlets, entryId);
124 }
125126/*** Returns the first portlets element in the current PSML resource corresponding 127 * to the given name128 * 129 * @param name the portlets name to seek130 * @return the found portlets description or null131 */132publicPortlets getPortlets(String name)
133 {
134Portlets p = getPortlets(this.portlets, name);
135136if (p == null)
137 {
138//maybe name is a position...139try140 {
141 p = getPortlets(Integer.parseInt(name));
142 }
143catch (NumberFormatException e)
144 {
145// this will happen if name is not parseable, ignore146 }
147 }
148149return p;
150 }
151152/*** Returns the first portlets element in the current PSML resource corresponding 153 * to the given id154 * 155 * @param name the portlets id to seek156 * @return the found portlets description or null157 */158publicPortlets getPortletsById(String portletId)
159 {
160Portlets p = getPortletsById(this.portlets, portletId);
161return p;
162 }
163164/*** Returns the first portlets element in the current PSML resource 165 * found at the specified position. The position is computed using166 * a left-most tree traversal algorithm of the existing portlets (thus167 * not counting other entry objects)168 * 169 * @param position the sought position170 * @return the found portlets object or null if we did not find such an171 * object172 */173publicPortlets getPortlets(int position)
174 {
175return getPortlets(this.portlets, position, 0);
176 }
177178/*** Returns the first entry in the specified PSML resource corresponding 179 * to the given portlet name180 * 181 * @param portlets the PSML description to look into182 * @param name the portlet name to seek183 * @return the found entry description or null184 */185publicstaticEntry getEntry(Portlets portlets, String name)
186 {
187Entry entry = null;
188189for (Iterator it1 = portlets.getEntriesIterator(); it1.hasNext(); )
190 {
191 entry = (Entry) it1.next();
192if (entry.getParent().equals (name))
193return (entry);
194 }
195196 entry = null;
197198for (Iterator it2 = portlets.getPortletsIterator(); it2.hasNext(); )
199 {
200Portlets p = (Portlets) it2.next();
201202 entry = getEntry(p, name);
203204if (entry != null)
205break;
206 }
207208return (entry);
209 }
210211/*** Returns the first entry in the specified PSML resource corresponding 212 * to the given portlet Id213 * 214 * @param portlets the PSML description to look into215 * @param entryId the portlet's entry id to seek216 * @return the found entry description or null217 */218publicstaticEntry getEntryById(Portlets portlets, String entryId)
219 {
220Entry entry = null;
221222for (Iterator it1 = portlets.getEntriesIterator(); it1.hasNext(); )
223 {
224 entry = (Entry) it1.next();
225if ((entry.getId()!=null) && entry.getId().equals (entryId))
226return (entry);
227 }
228229 entry = null;
230231for (Iterator it2 = portlets.getPortletsIterator(); it2.hasNext(); )
232 {
233Portlets p = (Portlets) it2.next();
234235 entry = getEntryById(p, entryId);
236237if (entry != null)
238break;
239 }
240241return (entry);
242 }
243244/*** Returns the first portlets element in the specified PSML resource corresponding 245 * to the given Id246 * 247 * @param portlets the PSML description to look into248 * @param portletId the portlet's id to seek249 * @return the found portlets description or null250 */251publicstaticPortlets getPortletsById(Portlets portlets, String portletId)
252 {
253Portlets entry = portlets;
254255if ( (entry.getId()!=null) && entry.getId().equals(portletId) )
256 {
257return entry;
258 }
259260 entry = null;
261262for (Iterator it2 = portlets.getPortletsIterator(); it2.hasNext(); )
263 {
264Portlets p = (Portlets) it2.next();
265266 entry = getPortletsById(p, portletId);
267268if (entry != null) break;
269 }
270271return (entry);
272 }
273274/*** Returns the first portlets element in the specified PSML resource corresponding 275 * to the given name276 * 277 * @param portlets the PSML description to look into278 * @param name the portlets name to seek279 * @return the found portlets description or null280 */281publicstaticPortlets getPortlets(Portlets portlets, String name)
282 {
283Portlets entry = portlets;
284285if ( (entry.getName()!=null) && entry.getName().equals(name) )
286 {
287return entry;
288 }
289290 entry = null;
291292for (Iterator it2 = portlets.getPortletsIterator(); it2.hasNext(); )
293 {
294Portlets p = (Portlets) it2.next();
295296 entry = getPortlets(p, name);
297298if (entry != null) break;
299 }
300301return (entry);
302 }
303/*** Returns the first portlets element in the specified PSML resource 304 * in the given position305 * 306 * @param portlets the PSML description to look into307 * @param position the position to look for308 * @param count the numbering for the portlets passed as parameter309 * @return the found portlets description or null310 */311publicstaticPortlets getPortlets(Portlets portlets, int position, int count)
312 {
313// sanity check, we never look after the sought position314if (position < count)
315 {
316returnnull;
317 }
318319// the base portlets is the one we're looking for...320if (position == count)
321 {
322return portlets;
323 }
324325// we need to recurse in the children326Portlets result = null;
327328for (Iterator it2 = portlets.getPortletsIterator(); it2.hasNext(); )
329 {
330Portlets p = (Portlets) it2.next();
331 count++;
332 result = getPortlets(p, position, count);
333334if (result != null) break;
335 }
336337return result;
338 }
339/***340 * Remove the Entry in the specified PSML resource corresponding 341 * to the given portlet Id342 * 343 * @param entryId the portlet's entry id to seek344 */345publicboolean removeEntryById(String entryId)
346 {
347return removeEntryById(this.portlets, entryId);
348 }
349350/***351 * Remove the Entry in the specified PSML resource corresponding 352 * to the given portlet Id353 * 354 * @param portlets the PSML description to look into355 * @param entryId the portlet's entry id to seek356 */357publicstaticboolean removeEntryById(Portlets portlets, String entryId)
358 {
359for (int i=0; i < portlets.getEntryCount(); i++)
360 {
361if ( entryId.equals(portlets.getEntry(i).getId()) )
362 {
363 portlets.removeEntry(i);
364returntrue;
365 }
366 }
367368for (Iterator it2 = portlets.getPortletsIterator(); it2.hasNext(); )
369 {
370Portlets p = (Portlets) it2.next();
371372if (removeEntryById(p, entryId) == true)
373returntrue;
374 }
375376return false;
377 }
378379/***380 * Create a clone of this object381 */382public Object clone()
383 throws java.lang.CloneNotSupportedException
384 {
385 Object cloned = super.clone();
386387// clone the portlets388 ((BasePSMLDocument)cloned).portlets = ((this.portlets == null) ? null : (Portlets) this.portlets.clone());
389390return cloned;
391 }
392 }
393