1 12 13 package com.openedit.webui.tree; 14 15 import java.io.ByteArrayOutputStream ; 16 import java.io.File ; 17 import java.io.InputStream ; 18 import java.util.ArrayList ; 19 import java.util.Collections ; 20 import java.util.HashSet ; 21 import java.util.Iterator ; 22 import java.util.List ; 23 import java.util.Set ; 24 import java.util.StringTokenizer ; 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 43 public class RepositoryTreeNode extends DefaultWebTreeNode implements Comparable 44 { 45 protected Set fieldIgnoreTypes; 47 protected Repository fieldRepository; 48 protected ContentItem fieldContentItem; 49 50 private static final Log log = LogFactory.getLog(RepositoryTreeNode.class); 51 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 extractName( ContentItem inItem ) 67 { 68 String 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 88 public RepositoryTreeNode getChild(String inName) 89 { 90 for (Iterator 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 106 public List getChildren() 107 { 108 if (fieldChildren == null) 109 { 110 fieldChildren = new ArrayList (); 111 reloadChildren(); 112 } 113 114 return fieldChildren; 115 } 116 117 120 public int compareTo(Object 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 146 public RepositoryTreeNode findNode(String inPath) 147 { 148 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 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 190 public void reloadChildren() 191 { 192 getChildren().clear(); 193 194 List directories = new ArrayList (); 195 List files = new ArrayList (); 196 197 if (getContentItem().isFolder()) 198 { 199 List childItems = getChildItems(); 200 ContentItem childItem = null; 201 String path = null; 202 for ( Iterator iterator = childItems.iterator(); iterator.hasNext(); ) 203 { 204 Object 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 name = childItem.getPath(); 212 boolean okToAdd = true; 213 214 for (Iterator iter = getIgnoreTypes().iterator(); iter.hasNext();) 216 { 217 String key = (String ) 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 Collections.sort(directories); 246 Collections.sort(files); 247 getChildren().addAll(directories); 248 getChildren().addAll(files); 249 } 250 } 251 252 protected List getChildItems() 253 { 254 List items = new ArrayList (); 255 if ( getContentItem().isFolder() ) 256 { 257 try 258 { 259 if ( getContentItem() instanceof FileItem) 260 { 261 FileItem item = (FileItem)getContentItem(); 262 File [] 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 271 ByteArrayOutputStream outputStream = new ByteArrayOutputStream (); 272 InputStream inputStream = getContentItem().getInputStream(); 273 OutputFiller filler = new OutputFiller(); 274 filler.fill( inputStream, outputStream ); 275 String listing = outputStream.toString(); 276 StringTokenizer st = new StringTokenizer ( listing ); 277 while( st.hasMoreTokens() ) 278 { 279 items.add( getRepository().get( getContentItem().getPath() + "/" + st.nextToken() ) ); 280 } 281 } 282 } 283 catch( Exception e ) 284 { 285 log.error(e); 286 throw new OpenEditRuntimeException(e); 287 } 288 } 289 return items; 290 } 291 292 protected void setIgnoreTypes(Set inIgnoreTypes) 293 { 294 fieldIgnoreTypes = inIgnoreTypes; 295 } 296 297 protected Set getIgnoreTypes() 298 { 299 if (fieldIgnoreTypes == null) 300 { 301 fieldIgnoreTypes = new HashSet (); 302 fieldIgnoreTypes.add("CVS"); 303 } 304 return fieldIgnoreTypes; 305 } 306 307 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 |