1 7 package org.ejtools.archive.io; 8 9 import java.io.File ; 10 import java.io.FileOutputStream ; 11 import java.io.IOException ; 12 import java.util.Iterator ; 13 import java.util.Stack ; 14 15 import org.ejtools.archive.Archive; 16 import org.ejtools.archive.Entry; 17 18 24 public class DirectoryWriter implements Writer 25 { 26 27 private Stack outDirs = new Stack (); 28 29 30 35 public DirectoryWriter(File basedir) 36 { 37 this.pushURI(basedir); 38 } 39 40 41 46 public void visit(Archive archive) 47 { 48 File uri = this.getURI(); 49 if (uri == null) 50 { 51 throw new IllegalStateException ("No initial uri"); 52 } 53 54 for (Iterator iterator = archive.iterator(); iterator.hasNext(); ) 56 { 57 Entry entry = (Entry) iterator.next(); 58 59 if (entry.isArchive()) 60 { 61 File newURI = new File (uri, entry.getURI()); 62 this.pushURI(newURI); 63 entry.accept(this); 64 this.popURI(); 65 } 66 else 67 { 68 entry.accept(this); 69 } 70 } 71 } 72 73 74 79 public void visit(Entry entry) 80 { 81 try 82 { 83 File uri = this.getURI(); 84 if (uri == null) 85 { 86 throw new IllegalStateException ("No initial uri"); 87 } 88 89 File file = new File (uri, entry.getURI()); 91 file.getParentFile().mkdirs(); 92 FileOutputStream fos = new FileOutputStream (file); 93 fos.write(entry.getContent()); 94 fos.close(); 95 } 96 catch (IOException ioe) 97 { 98 ioe.printStackTrace(); 99 } 100 } 101 102 103 108 protected File getURI() 109 { 110 return (File ) this.outDirs.lastElement(); 111 } 112 113 114 119 protected File popURI() 120 { 121 if (this.outDirs.isEmpty()) 122 { 123 return null; 124 } 125 else 126 { 127 return (File ) this.outDirs.pop(); 128 } 129 } 130 131 132 137 protected void pushURI(File uri) 138 { 139 this.outDirs.push(uri); 140 } 141 } 142 | Popular Tags |