KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > forms > formmodel > tree > SourceTreeModel


1 /*
2  * Copyright 1999-2005 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 at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * 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 and
14  * limitations under the License.
15  */

16 package org.apache.cocoon.forms.formmodel.tree;
17
18 import java.util.ArrayList JavaDoc;
19 import java.util.Collection JavaDoc;
20 import java.util.HashMap JavaDoc;
21 import java.util.Iterator JavaDoc;
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 /**
30  * A {@link TreeModel} that builds a hierarchy of <code>TraversableSource</code>s.
31  *
32  * @version $Id: SourceTreeModel.java 290436 2005-09-20 12:42:36Z sylvain $
33  */

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     /** optimization hint: don't filter child collections if there are no patterns */
44     private boolean hasPatterns = false;
45
46     private TraversableSource rootSource;
47
48     private String JavaDoc rootURL;
49     private SourceResolver resolver;
50
51     public SourceTreeModel(SourceResolver resolver, String JavaDoc 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 JavaDoc getRoot() {
69         if (this.rootSource == null) {
70             try {
71                 this.rootSource = (TraversableSource) this.resolver.resolveURI(this.rootURL);
72             } catch (Exception JavaDoc e) {
73                 throw new CascadingRuntimeException("Cannot resolve " + this.rootURL, e);
74             }
75         }
76         return this.rootSource;
77     }
78
79     public Collection JavaDoc getChildren(Object JavaDoc parent) {
80         if (parent instanceof TraversableSource) {
81             TraversableSource dir = (TraversableSource)parent;
82             try {
83                 // Return children if it's a collection, null otherwise
84
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 JavaDoc filterChildren(Collection JavaDoc coll) {
94         if (!this.hasPatterns) {
95             return coll;
96         }
97         
98         ArrayList JavaDoc result = new ArrayList JavaDoc();
99         Iterator JavaDoc iter = coll.iterator();
100         while(iter.hasNext()) {
101             TraversableSource src = (TraversableSource)iter.next();
102
103             // Does it match the patterns?
104
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 JavaDoc name = src.getName();
122         
123         //FIXME: match allowed a null Map very recently. Replace it by null once 2.1.8 is out,
124
// we will gain a few cycles.
125
HashMap JavaDoc junk = new HashMap JavaDoc();
126         
127         // check include patterns
128
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         // check exclude patterns
139
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 JavaDoc obj) {
151         return !(obj instanceof TraversableSource) || !((TraversableSource)obj).isCollection();
152     }
153
154     public String JavaDoc getChildKey(Object JavaDoc parent, Object JavaDoc child) {
155         return ((TraversableSource)child).getName();
156     }
157
158     public Object JavaDoc getChild(Object JavaDoc parent, String JavaDoc 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 JavaDoc 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 JavaDoc getNode(TreePath path) {
189         // FIXME: can be heavily optimized by building a new URL from the path elements.
190
return helper.getNode(path);
191     }
192 }
193
Popular Tags