KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.sapia.magnet.domain;
2
3 // Import of Sun's JDK classes
4
// ---------------------------
5
import java.io.File JavaDoc;
6 import java.net.MalformedURLException JavaDoc;
7 import java.util.Collection JavaDoc;
8 import java.util.TreeSet JavaDoc;
9 import java.util.Iterator JavaDoc;
10
11 // Import of Apache's Ant classes
12
// --------------------------------
13
import org.apache.tools.ant.DirectoryScanner;
14
15 // Import of Sapia's Corus classes
16
// --------------------------------
17
import org.sapia.magnet.render.RenderingException;
18
19
20 /**
21  *
22  *
23  * @author Jean-Cedric Desrochers
24  *
25  * <dl>
26  * <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>
27  * <dt><b>License:</b><dd>Read the license.txt file of the jar or visit the
28  * <a HREF="http://www.sapia-oss.org/license.html" target="sapia-license">license page</a> at the Sapia OSS web site</dd></dt>
29  * </dl>
30  */

31 public class FileProtocolHandler implements ProtocolHandlerIF {
32
33   /**
34    * Handle the rendering of the path object for a specific protocol by
35    * resolving the resources of the path.
36    *
37    * @param aPath The path object to render.
38    * @param aSortingOrder The sorting order of the collection to return.
39    * @return The collection of <CODE>Resource</CODE> objects.
40    * @exception RenderingException If an error occurs while resolving the path.
41    */

42   public Collection JavaDoc resolveResources(Path aPath, String JavaDoc aSortingOrder) throws RenderingException {
43     // Validate the arguments
44
if (aPath == null) {
45     } else if (!aPath.getProtocol().equals(Path.PROTOCOL_FILE)) {
46       throw new IllegalArgumentException JavaDoc("The protocol of the path is not 'file' but " + aPath.getProtocol());
47     } else if (aPath.getDirectory() == null) {
48       throw new IllegalArgumentException JavaDoc("The directory of the path passed in is null");
49     }
50
51     File JavaDoc aTargetDirectory = new File JavaDoc(aPath.getDirectory());
52     if (!aTargetDirectory.exists() || !aTargetDirectory.isDirectory()) {
53       // as a fallback, look it the user.dir
54
aTargetDirectory = new File JavaDoc(System.getProperty("user.dir") + File.separator + aPath.getDirectory());
55       if (!aTargetDirectory.exists() || !aTargetDirectory.isDirectory()) {
56         throw new RenderingException("The target directory '" + aPath.getDirectory() + "' is invalid");
57       }
58     }
59
60     // Create the resources for the included directories
61
TreeSet JavaDoc someResources;
62     if (aSortingOrder != null && aSortingOrder.equals(Path.SORTING_ASCENDING)) {
63       someResources = new TreeSet JavaDoc(new Resource.AscendingComparator());
64     } else if (aSortingOrder != null && aSortingOrder.equals(Path.SORTING_DESCENDING)) {
65       someResources = new TreeSet JavaDoc(new Resource.DescendingComparator());
66     } else {
67       someResources = new TreeSet JavaDoc();
68     }
69
70     if (aPath.getIncludes().size() == 0) {
71       // Create a resouce for the directory only
72
StringBuffer JavaDoc pathStr = new StringBuffer JavaDoc();
73       
74             pathStr.append(aTargetDirectory.getAbsolutePath()).append(File.separator);
75             try{
76         someResources.add(new Resource(new File JavaDoc(pathStr.toString()).toURL().toExternalForm(), 0));
77             }catch(MalformedURLException JavaDoc e){
78                 throw new RenderingException("Invalid path: " + pathStr.toString(), e);
79             }
80     } else {
81       // Create the directory scanner
82
DirectoryScanner aScanner = new DirectoryScanner();
83       aScanner.setBasedir(aTargetDirectory);
84       aScanner.setCaseSensitive(true);
85       aScanner.setFollowSymlinks(true);
86
87       // Add the inclusion patterns
88
String JavaDoc[] someIncludes = new String JavaDoc[aPath.getIncludes().size()];
89       int anIndex = 0;
90       for (Iterator JavaDoc it = aPath.getIncludes().iterator(); it.hasNext(); ) {
91         someIncludes[anIndex++] = ((Include) it.next()).getPattern();
92       }
93       aScanner.setIncludes(someIncludes);
94   
95       // Add the exclusion patterns
96
String JavaDoc[] someExcludes = new String JavaDoc[aPath.getExcludes().size()];
97       anIndex = 0;
98       for (Iterator JavaDoc it = aPath.getExcludes().iterator(); it.hasNext(); ) {
99         someExcludes[anIndex++] = ((Exclude) it.next()).getPattern();
100       }
101       aScanner.setExcludes(someExcludes);
102   
103       // Scan the directory
104
aScanner.scan();
105   
106       // Process the results
107
anIndex = 0;
108       String JavaDoc[] someDirectories = aScanner.getIncludedDirectories();
109       for (int i = 0; i < someDirectories.length; i++) {
110         StringBuffer JavaDoc pathStr = new StringBuffer JavaDoc();
111                 pathStr .append(aTargetDirectory.getAbsolutePath()).append(File.separator).
112               append(someDirectories[i]).append(File.separator);
113         try{
114           someResources.add(new Resource(new File JavaDoc(pathStr.toString()).toURL().toExternalForm(), anIndex++));
115         }catch(MalformedURLException JavaDoc e){
116                     throw new RenderingException("Invalid path: " + pathStr.toString(), e);
117         }
118       }
119   
120       // Create the resources for the included files
121
String JavaDoc[] someFiles = aScanner.getIncludedFiles();
122       for (int i = 0; i < someFiles.length; i++) {
123         StringBuffer JavaDoc pathStr = new StringBuffer JavaDoc();
124         pathStr.append(aTargetDirectory.getAbsolutePath()).append(File.separator).
125               append(someFiles[i]);
126         try{
127           someResources.add(new Resource(new File JavaDoc(pathStr.toString()).toURL().toExternalForm(), anIndex++));
128         }catch(MalformedURLException JavaDoc e){
129                     throw new RenderingException("Invalid path: " + pathStr.toString(), e);
130         }
131       }
132     }
133     
134     return someResources;
135   }
136 }
137
Popular Tags