|
|||||||||||||||||||
| Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
| DirectoryWriter.java | 0% | 0% | 0% | 0% |
|
||||||||||||||
| 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;
|
|
| 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 |
/**
|
|
| 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 outDirs = new Stack(); |
|
| 28 |
|
|
| 29 |
|
|
| 30 |
/**
|
|
| 31 |
* Build the Writer on a directory.
|
|
| 32 |
*
|
|
| 33 |
* @param basedir The root directory
|
|
| 34 |
*/
|
|
| 35 | 0 |
public DirectoryWriter(File basedir)
|
| 36 |
{
|
|
| 37 | 0 |
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 | 0 |
public void visit(Archive archive) |
| 47 |
{
|
|
| 48 | 0 |
File uri = this.getURI();
|
| 49 | 0 |
if (uri == null) |
| 50 |
{
|
|
| 51 | 0 |
throw new IllegalStateException("No initial uri"); |
| 52 |
} |
|
| 53 |
|
|
| 54 |
// For each archive, output its content
|
|
| 55 | 0 |
for (Iterator iterator = archive.iterator(); iterator.hasNext(); )
|
| 56 |
{
|
|
| 57 | 0 |
Entry entry = (Entry) iterator.next(); |
| 58 |
|
|
| 59 | 0 |
if (entry.isArchive())
|
| 60 |
{
|
|
| 61 | 0 |
File newURI = new File(uri, entry.getURI());
|
| 62 | 0 |
this.pushURI(newURI);
|
| 63 | 0 |
entry.accept(this);
|
| 64 | 0 |
this.popURI();
|
| 65 |
} |
|
| 66 |
else
|
|
| 67 |
{
|
|
| 68 | 0 |
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 | 0 |
public void visit(Entry entry) |
| 80 |
{
|
|
| 81 | 0 |
try
|
| 82 |
{
|
|
| 83 | 0 |
File uri = this.getURI();
|
| 84 | 0 |
if (uri == null) |
| 85 |
{
|
|
| 86 | 0 |
throw new IllegalStateException("No initial uri"); |
| 87 |
} |
|
| 88 |
|
|
| 89 |
// Create the file and dump the content
|
|
| 90 | 0 |
File file = new File(uri, entry.getURI());
|
| 91 | 0 |
file.getParentFile().mkdirs(); |
| 92 | 0 |
FileOutputStream fos = new FileOutputStream(file);
|
| 93 | 0 |
fos.write(entry.getContent()); |
| 94 | 0 |
fos.close(); |
| 95 |
} |
|
| 96 |
catch (IOException ioe)
|
|
| 97 |
{
|
|
| 98 | 0 |
ioe.printStackTrace(); |
| 99 |
} |
|
| 100 |
} |
|
| 101 |
|
|
| 102 |
|
|
| 103 |
/**
|
|
| 104 |
* Gets the stream attribute of the FileMapper object
|
|
| 105 |
*
|
|
| 106 |
* @return The stream value
|
|
| 107 |
*/
|
|
| 108 | 0 |
protected File getURI()
|
| 109 |
{
|
|
| 110 | 0 |
return (File) 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 | 0 |
protected File popURI()
|
| 120 |
{
|
|
| 121 | 0 |
if (this.outDirs.isEmpty()) |
| 122 |
{
|
|
| 123 | 0 |
return null; |
| 124 |
} |
|
| 125 |
else
|
|
| 126 |
{
|
|
| 127 | 0 |
return (File) this.outDirs.pop(); |
| 128 |
} |
|
| 129 |
} |
|
| 130 |
|
|
| 131 |
|
|
| 132 |
/**
|
|
| 133 |
* Description of the Method
|
|
| 134 |
*
|
|
| 135 |
* @param uri Description of the Parameter
|
|
| 136 |
*/
|
|
| 137 | 0 |
protected void pushURI(File uri) |
| 138 |
{
|
|
| 139 | 0 |
this.outDirs.push(uri);
|
| 140 |
} |
|
| 141 |
} |
|
| 142 |
|
|
||||||||||