1 package org.sapia.magnet.domain; 2 3 import java.io.File ; 6 import java.net.MalformedURLException ; 7 import java.util.Collection ; 8 import java.util.TreeSet ; 9 import java.util.Iterator ; 10 11 import org.apache.tools.ant.DirectoryScanner; 14 15 import org.sapia.magnet.render.RenderingException; 18 19 20 31 public class FileProtocolHandler implements ProtocolHandlerIF { 32 33 42 public Collection resolveResources(Path aPath, String aSortingOrder) throws RenderingException { 43 if (aPath == null) { 45 } else if (!aPath.getProtocol().equals(Path.PROTOCOL_FILE)) { 46 throw new IllegalArgumentException ("The protocol of the path is not 'file' but " + aPath.getProtocol()); 47 } else if (aPath.getDirectory() == null) { 48 throw new IllegalArgumentException ("The directory of the path passed in is null"); 49 } 50 51 File aTargetDirectory = new File (aPath.getDirectory()); 52 if (!aTargetDirectory.exists() || !aTargetDirectory.isDirectory()) { 53 aTargetDirectory = new File (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 TreeSet someResources; 62 if (aSortingOrder != null && aSortingOrder.equals(Path.SORTING_ASCENDING)) { 63 someResources = new TreeSet (new Resource.AscendingComparator()); 64 } else if (aSortingOrder != null && aSortingOrder.equals(Path.SORTING_DESCENDING)) { 65 someResources = new TreeSet (new Resource.DescendingComparator()); 66 } else { 67 someResources = new TreeSet (); 68 } 69 70 if (aPath.getIncludes().size() == 0) { 71 StringBuffer pathStr = new StringBuffer (); 73 74 pathStr.append(aTargetDirectory.getAbsolutePath()).append(File.separator); 75 try{ 76 someResources.add(new Resource(new File (pathStr.toString()).toURL().toExternalForm(), 0)); 77 }catch(MalformedURLException e){ 78 throw new RenderingException("Invalid path: " + pathStr.toString(), e); 79 } 80 } else { 81 DirectoryScanner aScanner = new DirectoryScanner(); 83 aScanner.setBasedir(aTargetDirectory); 84 aScanner.setCaseSensitive(true); 85 aScanner.setFollowSymlinks(true); 86 87 String [] someIncludes = new String [aPath.getIncludes().size()]; 89 int anIndex = 0; 90 for (Iterator it = aPath.getIncludes().iterator(); it.hasNext(); ) { 91 someIncludes[anIndex++] = ((Include) it.next()).getPattern(); 92 } 93 aScanner.setIncludes(someIncludes); 94 95 String [] someExcludes = new String [aPath.getExcludes().size()]; 97 anIndex = 0; 98 for (Iterator it = aPath.getExcludes().iterator(); it.hasNext(); ) { 99 someExcludes[anIndex++] = ((Exclude) it.next()).getPattern(); 100 } 101 aScanner.setExcludes(someExcludes); 102 103 aScanner.scan(); 105 106 anIndex = 0; 108 String [] someDirectories = aScanner.getIncludedDirectories(); 109 for (int i = 0; i < someDirectories.length; i++) { 110 StringBuffer pathStr = new StringBuffer (); 111 pathStr .append(aTargetDirectory.getAbsolutePath()).append(File.separator). 112 append(someDirectories[i]).append(File.separator); 113 try{ 114 someResources.add(new Resource(new File (pathStr.toString()).toURL().toExternalForm(), anIndex++)); 115 }catch(MalformedURLException e){ 116 throw new RenderingException("Invalid path: " + pathStr.toString(), e); 117 } 118 } 119 120 String [] someFiles = aScanner.getIncludedFiles(); 122 for (int i = 0; i < someFiles.length; i++) { 123 StringBuffer pathStr = new StringBuffer (); 124 pathStr.append(aTargetDirectory.getAbsolutePath()).append(File.separator). 125 append(someFiles[i]); 126 try{ 127 someResources.add(new Resource(new File (pathStr.toString()).toURL().toExternalForm(), anIndex++)); 128 }catch(MalformedURLException e){ 129 throw new RenderingException("Invalid path: " + pathStr.toString(), e); 130 } 131 } 132 } 133 134 return someResources; 135 } 136 } 137 | Popular Tags |