KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > openedit > webui > tree > RepositoryTreeNode


1 /*
2 Copyright (c) 2003 eInnovation Inc. All rights reserved
3
4 This library is free software; you can redistribute it and/or modify it under the terms
5 of the GNU Lesser General Public License as published by the Free Software Foundation;
6 either version 2.1 of the License, or (at your option) any later version.
7
8 This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
9 without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10 See the GNU Lesser General Public License for more details.
11 */

12
13 package com.openedit.webui.tree;
14
15 import java.io.ByteArrayOutputStream JavaDoc;
16 import java.io.File JavaDoc;
17 import java.io.InputStream JavaDoc;
18 import java.util.ArrayList JavaDoc;
19 import java.util.Collections JavaDoc;
20 import java.util.HashSet JavaDoc;
21 import java.util.Iterator JavaDoc;
22 import java.util.List JavaDoc;
23 import java.util.Set JavaDoc;
24 import java.util.StringTokenizer JavaDoc;
25
26 import org.apache.commons.logging.Log;
27 import org.apache.commons.logging.LogFactory;
28 import org.openedit.repository.ContentItem;
29 import org.openedit.repository.Repository;
30 import org.openedit.repository.RepositoryException;
31 import org.openedit.repository.filesystem.FileItem;
32
33 import com.openedit.OpenEditRuntimeException;
34 import com.openedit.util.OutputFiller;
35 import com.openedit.util.PathUtilities;
36
37
38 /**
39  * This class represents a node in a {@link RepositoryTreeModel}.
40  *
41  * @author Matt Avery, mavery@einnovation.com
42  */

43 public class RepositoryTreeNode extends DefaultWebTreeNode implements Comparable JavaDoc
44 {
45     //public static final DefaultWebTreeNode ERROR_NODE = new DefaultWebTreeNode( "Error accessing tree node." );
46
protected Set JavaDoc fieldIgnoreTypes;
47     protected Repository fieldRepository;
48     protected ContentItem fieldContentItem;
49
50     private static final Log log = LogFactory.getLog(RepositoryTreeNode.class);
51     /**
52      * Create a new <code>PageTreeNode</code>.
53      *
54      * @param inFile The file that this node represents
55      * @param inPath The path to the file, relative to the site context root
56      * @throws RepositoryException
57      */

58     public RepositoryTreeNode( Repository inRepository, ContentItem inContentItem )
59     {
60         super( extractName( inContentItem ) );
61         fieldRepository = inRepository;
62         fieldContentItem = inContentItem;
63         setLeaf(!getContentItem().isFolder());
64     }
65     
66     private static String JavaDoc extractName( ContentItem inItem )
67     {
68         String JavaDoc path = inItem.getPath();
69         if ( "/".equals( path ) )
70         {
71             return path;
72         }
73         int lastSlashIndex = path.lastIndexOf('/');
74         if ( lastSlashIndex < 0 )
75         {
76             return path;
77         }
78         return path.substring( lastSlashIndex + 1 );
79     }
80
81     /**
82      * Get the first child of this node with the given name.
83      *
84      * @param inName The node name
85      *
86      * @return The node, or <code>null</code> if no such child could be found
87      */

88     public RepositoryTreeNode getChild(String JavaDoc inName)
89     {
90         for (Iterator JavaDoc iter = getChildren().iterator(); iter.hasNext();)
91         {
92             RepositoryTreeNode child = (RepositoryTreeNode) iter.next();
93
94             if ((child.getName() != null) && child.getName().equals(inName))
95             {
96                 return child;
97             }
98         }
99
100         return null;
101     }
102
103     /* (non-Javadoc)
104      * @see DefaultWebTreeNode#getChildren()
105      */

106     public List JavaDoc getChildren()
107     {
108         if (fieldChildren == null)
109         {
110             fieldChildren = new ArrayList JavaDoc();
111             reloadChildren();
112         }
113
114         return fieldChildren;
115     }
116
117     /* (non-Javadoc)
118      * @see java.lang.Comparable#compareTo(Object)
119      */

120     public int compareTo(Object JavaDoc o)
121     {
122         if (o == null)
123         {
124             return 1;
125         }
126
127         if (o instanceof RepositoryTreeNode)
128         {
129             RepositoryTreeNode node = (RepositoryTreeNode) o;
130
131             return getContentItem().getPath().compareTo(node.getContentItem().getPath());
132         }
133         else
134         {
135             return 0;
136         }
137     }
138
139     /**
140      * Find the descendant of this node with the given path.
141      *
142      * @param inPath The path to find
143      *
144      * @return The node at the given path, or <code>null</code> if it could not be found
145      */

146     public RepositoryTreeNode findNode(String JavaDoc inPath)
147     {
148         // Quick initial checks...
149
if (!inPath.startsWith("/"))
150         {
151             inPath = "/" + inPath;
152         }
153
154         if (inPath.equals("") || inPath.equals("/"))
155         {
156             return this;
157         }
158
159         int beforeSlashIndex = 0;
160
161         if (inPath.startsWith("/"))
162         {
163             beforeSlashIndex = 1;
164         }
165
166         int nextSlashIndex = inPath.indexOf('/', beforeSlashIndex);
167
168         if (nextSlashIndex < 0)
169         {
170             nextSlashIndex = inPath.length();
171         }
172
173         String JavaDoc childName = inPath.substring(beforeSlashIndex, nextSlashIndex);
174
175         RepositoryTreeNode child = getChild(childName);
176
177         if (child == null)
178         {
179             return null;
180         }
181         else
182         {
183             return child.findNode(inPath.substring(nextSlashIndex));
184         }
185     }
186
187     /**
188      * Reload the children of this page tree node.
189      */

190     public void reloadChildren()
191     {
192         getChildren().clear();
193
194         List JavaDoc directories = new ArrayList JavaDoc();
195         List JavaDoc files = new ArrayList JavaDoc();
196
197         if (getContentItem().isFolder())
198         {
199             List JavaDoc childItems = getChildItems();
200             ContentItem childItem = null;
201             String JavaDoc path = null;
202             for ( Iterator JavaDoc iterator = childItems.iterator(); iterator.hasNext(); )
203             {
204                 Object JavaDoc obj = iterator.next();
205                 if ( !(obj instanceof ContentItem))
206                 {
207                     throw new OpenEditRuntimeException("Could not find class " + ContentItem.class + " " + obj.getClass());
208                 }
209                 childItem = (ContentItem) obj;
210
211                 String JavaDoc name = childItem.getPath();
212                 boolean okToAdd = true;
213
214                 //we want to ignore some files in this directory ie. CVS
215
for (Iterator JavaDoc iter = getIgnoreTypes().iterator(); iter.hasNext();)
216                 {
217                     String JavaDoc key = (String JavaDoc) iter.next();
218
219                     if (PathUtilities.match(name, key))
220                     {
221                         okToAdd = false;
222                         break;
223                     }
224                 }
225
226                 if (okToAdd)
227                 {
228
229                     RepositoryTreeNode child = createNode( childItem );
230                     child.setParent(this);
231
232                     if (childItem.isFolder())
233                     {
234                         directories.add(child);
235                     }
236                     else
237                     {
238                         files.add(child);
239                     }
240                 }
241             }
242
243             // Make sure the files appear in lexicographically increasing
244
// order, with all the directories appearing before all the files.
245
Collections.sort(directories);
246             Collections.sort(files);
247             getChildren().addAll(directories);
248             getChildren().addAll(files);
249         }
250     }
251
252     protected List JavaDoc getChildItems()
253     {
254         List JavaDoc items = new ArrayList JavaDoc();
255         if ( getContentItem().isFolder() )
256         {
257             try
258             {
259             if ( getContentItem() instanceof FileItem)
260             {
261                 FileItem item = (FileItem)getContentItem();
262                 File JavaDoc[] files = item.getFile().listFiles();
263                 for (int i = 0; i < files.length; i++)
264                 {
265                     items.add( getRepository().get( getContentItem().getPath() + "/" + files[i].getName() ) );
266                 }
267             }
268             else
269             {
270                 /*yuck! this fails with spaces */
271                 ByteArrayOutputStream JavaDoc outputStream = new ByteArrayOutputStream JavaDoc();
272                 InputStream JavaDoc inputStream = getContentItem().getInputStream();
273                 OutputFiller filler = new OutputFiller();
274                 filler.fill( inputStream, outputStream );
275                 String JavaDoc listing = outputStream.toString();
276                 StringTokenizer JavaDoc st = new StringTokenizer JavaDoc( listing );
277                 while( st.hasMoreTokens() )
278                 {
279                     items.add( getRepository().get( getContentItem().getPath() + "/" + st.nextToken() ) );
280                 }
281             }
282             }
283             catch( Exception JavaDoc e )
284             {
285                 log.error(e);
286                 throw new OpenEditRuntimeException(e);
287             }
288         }
289         return items;
290     }
291
292     protected void setIgnoreTypes(Set JavaDoc inIgnoreTypes)
293     {
294         fieldIgnoreTypes = inIgnoreTypes;
295     }
296
297     protected Set JavaDoc getIgnoreTypes()
298     {
299         if (fieldIgnoreTypes == null)
300         {
301             fieldIgnoreTypes = new HashSet JavaDoc();
302             fieldIgnoreTypes.add("CVS");
303         }
304         return fieldIgnoreTypes;
305     }
306
307     /**
308      * Method createPageTreeNode.
309      *
310      * @param childFile
311      * @param path
312      *
313      * @return PageTreeNode
314      */

315     protected RepositoryTreeNode createNode(ContentItem childItem)
316     {
317         RepositoryTreeNode node = new RepositoryTreeNode( getRepository(), childItem);
318         node.setIgnoreTypes(getIgnoreTypes());
319         return node;
320     }
321
322     public ContentItem getContentItem()
323     {
324         return fieldContentItem;
325     }
326     public void setContentItem( ContentItem contentItem )
327     {
328         fieldContentItem = contentItem;
329     }
330     public Repository getRepository()
331     {
332         return fieldRepository;
333     }
334     public void setRepository( Repository repository )
335     {
336         fieldRepository = repository;
337     }
338 }
339
Popular Tags