1 16 package org.apache.cocoon.forms.formmodel.tree; 17 18 import java.util.ArrayList ; 19 import java.util.Collection ; 20 import java.util.HashMap ; 21 import java.util.Iterator ; 22 23 import org.apache.avalon.framework.CascadingRuntimeException; 24 import org.apache.cocoon.matching.helpers.WildcardHelper; 25 import org.apache.excalibur.source.SourceException; 26 import org.apache.excalibur.source.SourceResolver; 27 import org.apache.excalibur.source.TraversableSource; 28 29 34 public class SourceTreeModel implements TreeModel { 35 36 private TreeModelHelper helper = new TreeModelHelper(this); 37 38 private int[][] fileIncludePatterns = SourceTreeModelDefinition.NO_PATTERNS; 39 private int[][] fileExcludePatterns = SourceTreeModelDefinition.NO_PATTERNS; 40 private int[][] dirIncludePatterns = SourceTreeModelDefinition.NO_PATTERNS; 41 private int[][] dirExcludePatterns = SourceTreeModelDefinition.NO_PATTERNS; 42 43 44 private boolean hasPatterns = false; 45 46 private TraversableSource rootSource; 47 48 private String rootURL; 49 private SourceResolver resolver; 50 51 public SourceTreeModel(SourceResolver resolver, String rootURL) { 52 this.resolver = resolver; 53 this.rootURL = rootURL; 54 } 55 56 public SourceTreeModel(SourceTreeModelDefinition definition) { 57 this.rootURL = definition.getRootURL(); 58 this.resolver = definition.getResolver(); 59 this.fileIncludePatterns = definition.getFileIncludePatterns(); 60 this.fileExcludePatterns = definition.getFileExcludePatterns(); 61 this.dirIncludePatterns = definition.getDirectoryIncludePatterns(); 62 this.dirExcludePatterns = definition.getDirectoryExcludePatterns(); 63 64 this.hasPatterns = this.fileIncludePatterns != null || this.fileExcludePatterns != null || 65 this.dirIncludePatterns != null || this.dirExcludePatterns != null; 66 } 67 68 public Object getRoot() { 69 if (this.rootSource == null) { 70 try { 71 this.rootSource = (TraversableSource) this.resolver.resolveURI(this.rootURL); 72 } catch (Exception e) { 73 throw new CascadingRuntimeException("Cannot resolve " + this.rootURL, e); 74 } 75 } 76 return this.rootSource; 77 } 78 79 public Collection getChildren(Object parent) { 80 if (parent instanceof TraversableSource) { 81 TraversableSource dir = (TraversableSource)parent; 82 try { 83 return dir.isCollection() ? filterChildren(dir.getChildren()) : null; 85 } catch (SourceException e) { 86 throw new CascadingRuntimeException("getChildren", e); 87 } 88 } else { 89 return null; 90 } 91 } 92 93 private Collection filterChildren(Collection coll) { 94 if (!this.hasPatterns) { 95 return coll; 96 } 97 98 ArrayList result = new ArrayList (); 99 Iterator iter = coll.iterator(); 100 while(iter.hasNext()) { 101 TraversableSource src = (TraversableSource)iter.next(); 102 103 boolean matches = true; 105 if (src.isCollection()) { 106 matches = matches(src, this.dirIncludePatterns, this.dirExcludePatterns); 107 } else { 108 matches = matches(src, this.fileIncludePatterns, this.fileExcludePatterns); 109 } 110 111 if (matches) { 112 result.add(src); 113 } 114 } 115 116 return result; 117 } 118 119 private boolean matches(TraversableSource src, int[][]include, int[][]exclude) { 120 boolean matches = true; 121 String name = src.getName(); 122 123 HashMap junk = new HashMap (); 126 127 if (include != null && include.length > 0) { 129 matches = false; 130 check: for (int i = 0; i < include.length; i++) { 131 if (WildcardHelper.match(junk, name, include[i])) { 132 matches = true; 133 break check; 134 } 135 } 136 } 137 138 if (matches && exclude != null && exclude.length > 0) { 140 check: for (int i = 0; i < exclude.length; i++) { 141 if (WildcardHelper.match(junk, name, exclude[i])) { 142 matches = false; 143 break check; 144 } 145 } 146 } 147 return matches; 148 } 149 150 public boolean isLeaf(Object obj) { 151 return !(obj instanceof TraversableSource) || !((TraversableSource)obj).isCollection(); 152 } 153 154 public String getChildKey(Object parent, Object child) { 155 return ((TraversableSource)child).getName(); 156 } 157 158 public Object getChild(Object parent, String key) { 159 try { 160 return ((TraversableSource)parent).getChild(key); 161 } catch (SourceException e) { 162 throw new CascadingRuntimeException("getChild", e); 163 } 164 } 165 166 public void setRootURL(String url) { 167 if (this.rootSource != null) { 168 this.resolver.release(this.rootSource); 169 this.rootSource = null; 170 } 171 this.rootURL = url; 172 helper.fireTreeStructureChanged(TreePath.ROOT_PATH); 173 } 174 175 public void setRootSource(TraversableSource src) { 176 this.rootSource = src; 177 helper.fireTreeStructureChanged(TreePath.ROOT_PATH); 178 } 179 180 public void addTreeModelListener(TreeModelListener l) { 181 helper.addTreeModelListener(l); 182 } 183 184 public void removeTreeModelListener(TreeModelListener l) { 185 helper.addTreeModelListener(l); 186 } 187 188 public Object getNode(TreePath path) { 189 return helper.getNode(path); 191 } 192 } 193 | Popular Tags |