KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jahia > content > ContentTree


1 package org.jahia.content;
2
3 import java.util.ArrayList JavaDoc;
4 import java.util.Iterator JavaDoc;
5 import java.util.Stack JavaDoc;
6
7 import org.jahia.exceptions.JahiaException;
8 import org.jahia.services.fields.ContentPageField;
9 import org.jahia.services.usermanager.JahiaUser;
10 import org.jahia.services.version.EntryLoadRequest;
11
12 /**
13  * <p>Title: Class to traverse Jahia's Content Tree.</p>
14  * <p>Description: This class define a default implementation for traversing
15  * Jahia's Content Tree.</p>
16  * <p>Copyright: Copyright (c) 2002</p>
17  * <p>Company: </p>
18  * @author Khue Nguyen
19  * @version 1.0
20  */

21
22 public class ContentTree {
23
24     private ContentObject rootContentObject;
25
26     // Stack of contentTree status for handling iteration through the tree
27
private Stack JavaDoc contentTreeStatusStack = new Stack JavaDoc();
28
29     /**
30      * The root content object of the three
31      *
32      * @param rootContentObject
33      */

34     public ContentTree(ContentObject aRootContentObject) {
35
36         this.rootContentObject = aRootContentObject;
37     }
38
39     /**
40      * Returns the root content object
41      *
42      * @return
43      */

44     public ContentObject getRootContentObject(){
45         return this.rootContentObject;
46     }
47
48     /**
49      * Iterate through the entire Tree
50      *
51      * @param Visitor
52      */

53     public void iterate(ContentTreeVisitorInterface visitor)
54     throws JahiaException {
55         this.contentTreeStatusStack = new Stack JavaDoc();
56         iterate(this.rootContentObject,visitor,0);
57     }
58
59     /**
60      * The default implementation for gettings the childs of a given
61      * Content object
62      *
63      * @param contentObject
64      * @param user
65      * @param loadRequest
66      * @param operationMode
67      * @return
68      * @throws JahiaException
69      */

70     public ArrayList JavaDoc getContentChilds(ContentObject contentObject,
71                                       JahiaUser user,
72                                       EntryLoadRequest loadRequest,
73                                       String JavaDoc operationMode)
74     throws JahiaException {
75         ArrayList JavaDoc childs
76                 = contentObject.getChilds(user, loadRequest);
77         return childs;
78     }
79
80     /**
81      * Returns the contentTreeStatusStack
82      *
83      * @return
84      */

85     public Stack JavaDoc getContentTreeStatusStack(){
86         return this.contentTreeStatusStack;
87     }
88
89     /**
90      * Iterate through the entire Tree
91      *
92      * @param contentObject
93      * @param visitor
94      * @param currentPageLevel
95      * @throws JahiaException
96      */

97     private void iterate(ContentObject contentObject,
98                         ContentTreeVisitorInterface visitor,
99                         int currentPageLevel)
100     throws JahiaException {
101
102         int descendingPageLevel = visitor.getDescendingPageLevel();
103
104         ContentTreeStatusInterface contentTreeStatus =
105                 visitor.getContentTreeStatus(contentObject,currentPageLevel);
106
107         this.contentTreeStatusStack.push(contentTreeStatus);
108
109         if ( descendingPageLevel != -1
110              && currentPageLevel>descendingPageLevel ){
111
112             // process last ContentPageField for which we don't want to go further
113
visitor.processLastContentPageField(contentObject, currentPageLevel);
114             // stop iterate further
115
} else {
116             // processing root object
117
visitor.processContentObjectBeforeChilds(contentObject,
118                 currentPageLevel);
119
120             // processing childs object
121
if (visitor.withChildsContent()
122                 && contentTreeStatus.continueWithChilds()) {
123                 ArrayList JavaDoc childs = visitor.getChilds(contentObject,
124                     currentPageLevel);
125                 Iterator JavaDoc childsIterator = childs.iterator();
126                 ContentObject childContentObject;
127                 while (childsIterator.hasNext()
128                        && contentTreeStatus.continueWithChilds()) {
129                     childContentObject = (ContentObject) childsIterator.next();
130                     if (childContentObject instanceof ContentPageField) {
131                         iterate(childContentObject, visitor,
132                                 currentPageLevel + 1);
133                     }
134                     else {
135                         iterate(childContentObject, visitor, currentPageLevel);
136                     }
137                 }
138             }
139             if (contentTreeStatus.continueAfterChilds()) {
140                 visitor.processContentObjectAfterChilds(contentObject,
141                     currentPageLevel);
142             } else {
143                 visitor.stopProcessContentObjectAfterChilds(contentObject,
144                     currentPageLevel);
145             }
146         }
147         // remove the status
148
this.contentTreeStatusStack.pop();
149     }
150 }
151
Popular Tags