/************************************************************************ * * The Contents of this file are made available subject to the terms of * * - GNU Lesser General Public License Version 2.1 * * Sun Microsystems Inc., October, 2000 * * GNU Lesser General Public License Version 2.1 * ============================================= * Copyright 2000 by Sun Microsystems, Inc. * 901 San Antonio Road, Palo Alto, CA 94303, USA * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License version 2.1, as published by the Free Software Foundation. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, * MA 02111-1307 USA * * The Initial Developer of the Original Code is: Sun Microsystems, Inc. * * Copyright: 2000 by Sun Microsystems, Inc. * * All Rights Reserved. * * Contributor(s): _______________________________________ * * ************************************************************************/ // This version is adapted for Writer2LaTeX // Change: The first document added will become the "master document" 2006/10/05 // Version 1.0 (2008-11-24) package writer2latex.xmerge; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.util.Vector; import java.util.Iterator; import writer2latex.api.ConverterResult; import writer2latex.api.OutputFile; /** *
ConvertData
is used as a container for passing
* OutputFile
objects in and out of the Convert
* class. The ConvertData
contains a String
* name and a Vector
of OutputFile
objects.
OutputFile
objects.
*/
private Vector v = new Vector();
/** Master doc */
private OutputFile masterDoc = null;
/**
* Name of the ConvertData
object.
*/
private String name;
/**
* Resets ConvertData. This empties all OutputFile
* objects from this class. This allows reuse of a
* ConvertData
.
*/
public void reset() {
name = null;
v.removeAllElements();
}
/**
* Returns the OutputFile
name.
*
* @return The OutputFile
name.
*/
public String getName() {
return name;
}
/**
* Sets the OutputFile
name.
*
* @param docName The name of the OutputFile
.
*/
public void setName(String docName) {
name = docName;
}
/**
* Adds a OutputFile
to the vector.
*
* @param doc The OutputFile
to add.
*/
public void addDocument(OutputFile doc) {
if (v.size()==0) { masterDoc = doc; }
v.add(doc);
}
/** Get the master document
* @return OutputFile
the master document
*/
public OutputFile getMasterDocument() {
return masterDoc;
}
/** Check if a given document is the master document
* @param doc The OutputFile
to check
* @return true if this is the master document
*/
public boolean isMasterDocument(OutputFile doc) {
return doc == masterDoc;
}
/**
* Gets an Iterator
to access the Vector
* of OutputFile
objects
*
* @return The Iterator
to access the
* Vector
of OutputFile
objects.
*/
public Iterator iterator() {
return v.iterator();
}
/**
* Gets the number of OutputFile
objects currently stored
*
* @return The number of OutputFile
objects currently
* stored.
*/
public int getNumDocuments() {
return (v.size());
}
public void write(File dir) throws IOException {
if (dir!=null && !dir.exists()) throw new IOException("Directory does not exist");
Iterator docEnum = iterator();
while (docEnum.hasNext()) {
OutputFile docOut = (OutputFile) docEnum.next();
String sDirName = "";
String sFileName = docOut.getFileName();
File subdir = dir;
int nSlash = sFileName.indexOf("/");
if (nSlash>-1) {
sDirName = sFileName.substring(0,nSlash);
sFileName = sFileName.substring(nSlash+1);
subdir = new File(dir,sDirName);
if (!subdir.exists()) { subdir.mkdir(); }
}
File outfile = new File (subdir,sFileName);
FileOutputStream fos = new FileOutputStream(outfile);
docOut.write(fos);
fos.flush();
fos.close();
}
}
}