KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ejtools > archive > io > DirectoryWriter


1 /*
2  * EJTools, the Enterprise Java Tools
3  *
4  * Distributable under LGPL license.
5  * See terms of license at www.gnu.org.
6  */

7 package org.ejtools.archive.io;
8
9 import java.io.File JavaDoc;
10 import java.io.FileOutputStream JavaDoc;
11 import java.io.IOException JavaDoc;
12 import java.util.Iterator JavaDoc;
13 import java.util.Stack JavaDoc;
14
15 import org.ejtools.archive.Archive;
16 import org.ejtools.archive.Entry;
17
18 /**
19  * Writer implementation based on file-system directory. Each Archive will be output as a directory, and each Entry as a file.
20  *
21  * @author Laurent Etiemble
22  * @version $Revision: 1.2 $
23  */

24 public class DirectoryWriter implements Writer
25 {
26    /** Stacks of the current output directories */
27    private Stack JavaDoc outDirs = new Stack JavaDoc();
28
29
30    /**
31     * Build the Writer on a directory.
32     *
33     * @param basedir The root directory
34     */

35    public DirectoryWriter(File JavaDoc basedir)
36    {
37       this.pushURI(basedir);
38    }
39
40
41    /**
42     * Visit an archive. Each Archive will be output as a directory.
43     *
44     * @param archive The archive to visit
45     */

46    public void visit(Archive archive)
47    {
48       File JavaDoc uri = this.getURI();
49       if (uri == null)
50       {
51          throw new IllegalStateException JavaDoc("No initial uri");
52       }
53
54       // For each archive, output its content
55
for (Iterator JavaDoc iterator = archive.iterator(); iterator.hasNext(); )
56       {
57          Entry entry = (Entry) iterator.next();
58
59          if (entry.isArchive())
60          {
61             File JavaDoc newURI = new File JavaDoc(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    /**
75     * Visit an entry. Each Entry will be output as a file.
76     *
77     * @param entry The entry to visit
78     */

79    public void visit(Entry entry)
80    {
81       try
82       {
83          File JavaDoc uri = this.getURI();
84          if (uri == null)
85          {
86             throw new IllegalStateException JavaDoc("No initial uri");
87          }
88
89          // Create the file and dump the content
90
File JavaDoc file = new File JavaDoc(uri, entry.getURI());
91          file.getParentFile().mkdirs();
92          FileOutputStream JavaDoc fos = new FileOutputStream JavaDoc(file);
93          fos.write(entry.getContent());
94          fos.close();
95       }
96       catch (IOException JavaDoc ioe)
97       {
98          ioe.printStackTrace();
99       }
100    }
101
102
103    /**
104     * Gets the stream attribute of the FileMapper object
105     *
106     * @return The stream value
107     */

108    protected File JavaDoc getURI()
109    {
110       return (File JavaDoc) this.outDirs.lastElement();
111    }
112
113
114    /**
115     * Gets the stream attribute of the FileMapper object
116     *
117     * @return Description of the Return Value
118     */

119    protected File JavaDoc popURI()
120    {
121       if (this.outDirs.isEmpty())
122       {
123          return null;
124       }
125       else
126       {
127          return (File JavaDoc) this.outDirs.pop();
128       }
129    }
130
131
132    /**
133     * Description of the Method
134     *
135     * @param uri Description of the Parameter
136     */

137    protected void pushURI(File JavaDoc uri)
138    {
139       this.outDirs.push(uri);
140    }
141 }
142
Popular Tags