KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > sapia > magnet > domain > Path


1 package org.sapia.magnet.domain;
2
3 // Import of Sun's JDK classes
4
// ---------------------------
5
import java.util.ArrayList JavaDoc;
6 import java.util.Collection JavaDoc;
7 import java.util.Iterator JavaDoc;
8 import java.util.List JavaDoc;
9
10 // Import of Sapia's magnet classes
11
// --------------------------------
12
import org.sapia.magnet.render.AbstractRenderable;
13 import org.sapia.magnet.render.MagnetContext;
14 import org.sapia.magnet.render.RenderingException;
15
16
17 /**
18  *
19  *
20  * @author Jean-Cedric Desrochers
21  *
22  * <dl>
23  * <dt><b>Copyright:</b><dd>Copyright &#169; 2002-2003 <a HREF="http://www.sapia-oss.org">Sapia Open Source Software</a>. All Rights Reserved.</dd></dt>
24  * <dt><b>License:</b><dd>Read the license.txt file of the jar or visit the
25  * <a HREF="http://www.sapia-oss.org/license.html" target="sapia-license">license page</a> at the Sapia OSS web site</dd></dt>
26  * </dl>
27  */

28 public class Path extends AbstractRenderable {
29
30   /////////////////////////////////////////////////////////////////////////////////////////
31
////////////////////////////////// CLASS ATTRIBUTES ///////////////////////////////////
32
/////////////////////////////////////////////////////////////////////////////////////////
33

34   /** Defines the FILE protocol value. */
35   public static final String JavaDoc PROTOCOL_FILE = "file";
36
37   /** Defines the HTTP protocol value. */
38   public static final String JavaDoc PROTOCOL_HTTP = "http";
39
40   /** Defines the default protocol. */
41   public static final String JavaDoc DEFAULT_PROTOCOL = "file";
42
43   /** Defines the default host. */
44   public static final String JavaDoc DEFAULT_HOST = "localhost";
45
46   /** Defines the ASCENDING sorting order. */
47   public static final String JavaDoc SORTING_ASCENDING = "ascending";
48
49   /** Defines the DESCENDING sorting order. */
50   public static final String JavaDoc SORTING_DESCENDING = "descending";
51
52   /////////////////////////////////////////////////////////////////////////////////////////
53
///////////////////////////////// INSTANCE ATTRIBUTES /////////////////////////////////
54
/////////////////////////////////////////////////////////////////////////////////////////
55

56   /** The target directory of this path element. */
57   private String JavaDoc _theDirectory;
58
59   /** The host name of this path element. */
60   private String JavaDoc _theHost;
61
62   /** The protocol of this path element. */
63   private String JavaDoc _theProtocol;
64
65   /** The sorting order of this path element. */
66   private String JavaDoc _theSorting;
67
68   /** The list of the includes patterns of this path element. */
69   private List JavaDoc _theIncludePatterns;
70
71   /** The list of the excludes patterns of this path element. */
72   private List JavaDoc _theExcludePatterns;
73
74   /** The collection of selected resources (result of rendering). */
75   private Collection JavaDoc _theSelectedResources;
76
77   /////////////////////////////////////////////////////////////////////////////////////////
78
//////////////////////////////////// CONSTRUCTORS /////////////////////////////////////
79
/////////////////////////////////////////////////////////////////////////////////////////
80

81   /**
82    * Creates a new Path instance.
83    */

84   public Path() {
85     _theProtocol = DEFAULT_PROTOCOL;
86     _theHost = DEFAULT_HOST;
87     _theIncludePatterns = new ArrayList JavaDoc();
88     _theExcludePatterns = new ArrayList JavaDoc();
89   }
90
91   /////////////////////////////////////////////////////////////////////////////////////////
92
////////////////////////////////// ACCESSOR METHODS ///////////////////////////////////
93
/////////////////////////////////////////////////////////////////////////////////////////
94

95   /**
96    * Returns the protocol of this path element.
97    *
98    * @return The protocol of this path element.
99    */

100   public String JavaDoc getProtocol() {
101     return _theProtocol;
102   }
103
104   /**
105    * Returns the host of this path element.
106    *
107    * @return The host of this path element.
108    */

109   public String JavaDoc getHost() {
110     return _theHost;
111   }
112
113   /**
114    * Returns the directory of this path element.
115    *
116    * @return The directory of this path element.
117    */

118   public String JavaDoc getDirectory() {
119     return _theDirectory;
120   }
121
122   /**
123    * Returns the sorting order of this path element.
124    *
125    * @return The sorting order of this path element.
126    */

127   public String JavaDoc getSorting() {
128     return _theSorting;
129   }
130
131   /**
132    * Returns the collection of include pattern of this path element.
133    *
134    * @return The collection of <CODE>Include</CODE> objects.
135    */

136   public Collection JavaDoc getIncludes() {
137     return _theIncludePatterns;
138   }
139
140   /**
141    * Returns the collection of exclude pattern of this path element.
142    *
143    * @return The collection of <CODE>Exclude</CODE> objects.
144    */

145   public Collection JavaDoc getExcludes() {
146     return _theExcludePatterns;
147   }
148
149   /**
150    * Returns the selected resources of this path object after rendering.
151    *
152    * @return The collection of <CODE>Resource</CODE> of this path.
153    */

154   public Collection JavaDoc getSelectedResources() {
155     return _theSelectedResources;
156   }
157
158   /////////////////////////////////////////////////////////////////////////////////////////
159
/////////////////////////////////// MUTATOR METHODS ///////////////////////////////////
160
/////////////////////////////////////////////////////////////////////////////////////////
161

162   /**
163    * Changes the protocol of this path element.
164    *
165    * @param aProtocol The new protocol.
166    */

167   public void setProtocol(String JavaDoc aProtocol) {
168     _theProtocol = aProtocol;
169   }
170
171   /**
172    * Changes the host of this path element.
173    *
174    * @param aHost The new host.
175    */

176   public void setHost(String JavaDoc aHost) {
177     _theHost = aHost;
178   }
179
180   /**
181    * Changes the directory of this path element.
182    *
183    * @param aDirectory The new directory.
184    */

185   public void setDirectory(String JavaDoc aDirectory) {
186     _theDirectory = aDirectory;
187   }
188
189   /**
190    * Changes the sorting order of this path element.
191    *
192    * @param aSorting The new sorting order.
193    */

194   public void setSorting(String JavaDoc aSorting) {
195     _theSorting = aSorting;
196   }
197
198   /**
199    * Adds the include pattern passed in to this path element.
200    *
201    * @param anInclude The include pattern to add.
202    * @exception IllegalArgumentException If the include passed in is null.
203    */

204   public void addInclude(Include anInclude) {
205     if (anInclude == null) {
206       throw new IllegalArgumentException JavaDoc("The include pattern passed in is null");
207     }
208
209     _theIncludePatterns.add(anInclude);
210   }
211
212   /**
213    * Removes the include pattern passed in from this path element.
214    *
215    * @param anInclude The include pattern to remove.
216    * @exception IllegalArgumentException If the include passed in is null.
217    */

218   public void removeInclude(Include anInclude) {
219     if (anInclude == null) {
220       throw new IllegalArgumentException JavaDoc("The include pattern passed in is null");
221     }
222
223     _theIncludePatterns.remove(anInclude);
224   }
225
226   /**
227    * Removes all the include patterns from this path element.
228    */

229   public void clearIncludes() {
230     _theIncludePatterns.clear();
231   }
232
233   /**
234    * Adds the exclude pattern passed in to this path element.
235    *
236    * @param anExclude The exclude pattern to add.
237    * @exception IllegalArgumentException If the exclude passed in is null.
238    */

239   public void addExclude(Exclude anExclude) {
240     if (anExclude == null) {
241       throw new IllegalArgumentException JavaDoc("The exclude pattern passed in is null");
242     }
243
244     _theExcludePatterns.add(anExclude);
245   }
246
247   /**
248    * Removes the exclude pattern passed in from this path element.
249    *
250    * @param anExclude The exclude pattern to remove.
251    * @exception IllegalArgumentException If the exclude passed in is null.
252    */

253   public void removeExclude(Exclude anExclude) {
254     if (anExclude == null) {
255       throw new IllegalArgumentException JavaDoc("The exclude pattern passed in is null");
256     }
257
258     _theExcludePatterns.remove(anExclude);
259   }
260
261   /**
262    * Removes all the exclude patterns from this path element.
263    */

264   public void clearExcludes() {
265     _theExcludePatterns.clear();
266   }
267
268   /////////////////////////////////////////////////////////////////////////////////////////
269
////////////////////////////////// OVERRIDEN METHODS //////////////////////////////////
270
/////////////////////////////////////////////////////////////////////////////////////////
271

272   /**
273    * Renders this objects to the magnet context passed in.
274    *
275    * @param aContext The magnet context to use.
276    * @exception RenderingException If an error occurs while rendering this object.
277    */

278   public void render(MagnetContext aContext) throws RenderingException {
279     // Resolve the path attributes
280
try {
281       _theProtocol = resolveValue(aContext, _theProtocol);
282       _theHost = resolveValue(aContext, _theHost);
283       _theDirectory = resolveValue(aContext, _theDirectory);
284     } catch (RenderingException re) {
285       StringBuffer JavaDoc aBuffer = new StringBuffer JavaDoc();
286       aBuffer.append("Unable to resolve an attribute of the path ").
287               append(" protocol=").append(_theProtocol).
288               append(" host=").append(_theHost).
289               append(" directory=").append(_theDirectory);
290         
291       throw new RenderingException(aBuffer.toString(), re);
292     }
293
294     try {
295       // Rendering the include patterns
296
for (Iterator JavaDoc it = _theIncludePatterns.iterator(); it.hasNext(); ) {
297         Include anInclude = (Include) it.next();
298         anInclude.render(aContext);
299       }
300
301       // Rendering the excludes patterns
302
for (Iterator JavaDoc it = _theExcludePatterns.iterator(); it.hasNext(); ) {
303         Exclude anExclude = (Exclude) it.next();
304         anExclude.render(aContext);
305       }
306
307       // Delegate the resolution of the resource to the protocol handler
308
ProtocolHandlerIF aHandler = HandlerFactory.getInstance().createProtocolHandler(_theProtocol);
309       _theSelectedResources = aHandler.resolveResources(this, _theSorting);
310
311     } catch (RenderingException re) {
312       StringBuffer JavaDoc aBuffer = new StringBuffer JavaDoc();
313       aBuffer.append("Unable to render the patterns of the path ").
314               append(" protocol=").append(_theProtocol).
315               append(" host=").append(_theHost).
316               append(" directory=").append(_theDirectory);
317         
318       throw new RenderingException(aBuffer.toString(), re);
319
320     } catch (ObjectCreationException oce) {
321       StringBuffer JavaDoc aBuffer = new StringBuffer JavaDoc("Unable to create the protocol handler for the path: ");
322       aBuffer.append("protocol=").append(_theProtocol).
323               append(" host=").append(_theHost).
324               append(" directory=").append(_theDirectory);
325       throw new RenderingException(aBuffer.toString(), oce);
326     }
327   }
328
329   /**
330    * Returns a string representation of this path element.
331    *
332    * @return A string representation of this path element.
333    */

334   public String JavaDoc toString() {
335     StringBuffer JavaDoc aBuffer = new StringBuffer JavaDoc(super.toString());
336     aBuffer.append("[protocol=").append(_theProtocol).
337             append(" host=").append(_theHost).
338             append(" directory=").append(_theDirectory).
339             append(" includes=").append(_theIncludePatterns).
340             append(" exludes=").append(_theExcludePatterns).
341             append(" resources=").append(_theSelectedResources).
342             append("]");
343
344     return aBuffer.toString();
345   }
346 }
347
Popular Tags