Initial import

git-svn-id: svn://svn.code.sf.net/p/writer2latex/code/trunk@5 f0f2a975-2e09-46c8-9428-3b39399b9f3c
This commit is contained in:
henrikjust 2009-02-20 09:37:06 +00:00
parent 75e32b1e8f
commit b0b66fcae9
252 changed files with 49000 additions and 0 deletions

View file

@ -0,0 +1,182 @@
/************************************************************************
*
* BatchConverterBase.java
*
* 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
*
* Copyright: 2002-2008 by Henrik Just
*
* All Rights Reserved.
*
* Version 1.0 (2008-10-15)
*
*/
package writer2latex.base;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import writer2latex.api.BatchConverter;
import writer2latex.api.BatchHandler;
import writer2latex.api.Converter;
import writer2latex.api.ConverterResult;
import writer2latex.api.IndexPageEntry;
import writer2latex.api.OutputFile;
import writer2latex.util.Misc;
/**
* Abstract base implementation of <code>writer2latex.api.BatchConverter</code>.
* The base implementation handles the traversal of directories and files, and
* leaves the handling of indexpages to the subclass.
*/
public abstract class BatchConverterBase implements BatchConverter {
private Converter converter;
public BatchConverterBase() {
converter = null;
}
// Partial implementation of the interface
public void setConverter(Converter converter) {
this.converter = converter;
}
public void convert(File source, File target, boolean bRecurse, BatchHandler handler) {
handler.startConversion();
convertDirectory(source, target, bRecurse, source.getName(), handler);
handler.endConversion();
}
protected abstract String getIndexFileName();
// Convert files and directories in the directory indir
// (return false if conversion has been cancelled by the BatchHandler)
private boolean convertDirectory(File indir, File outdir, boolean bRecurse, String sHeading, BatchHandler handler) {
handler.startDirectory(indir.getPath());
// Step 1: Get the directory
File[] contents = indir.listFiles();
int nLen = contents.length;
IndexPageEntry[] entries = new IndexPageEntry[nLen];
// Step 2: Traverse subdirectories, if allowed
if (bRecurse) {
String sUplink = getConfig().getOption("uplink");
for (int i=0; i<nLen; i++) {
if (contents[i].isDirectory()) {
getConfig().setOption("uplink","../"+getIndexFileName());
File newOutdir = new File(outdir,contents[i].getName());
String sNewHeading = sHeading + " - " + contents[i].getName();
boolean bResult = convertDirectory(contents[i],newOutdir,bRecurse,sNewHeading,handler);
getConfig().setOption("uplink", sUplink);
if (!bResult) { return false; }
// Create entry for this subdirectory
IndexPageEntry entry = new IndexPageEntry(Misc.makeHref(contents[i].getName()+"/"+getIndexFileName()),true);
entry.setDisplayName(contents[i].getName());
entries[i]=entry;
}
}
}
// Step 3: Traverse documents, if we have a converter
if (converter!=null) {
String sUplink = getConfig().getOption("uplink");
for (int i=0; i<nLen; i++) {
if (contents[i].isFile()) {
getConfig().setOption("uplink",getIndexFileName());
String sLinkFile = convertFile(contents[i],outdir,handler);
getConfig().setOption("uplink", sUplink);
if (sLinkFile!=null) {
// Create entry for this file
IndexPageEntry entry = new IndexPageEntry(Misc.makeHref(sLinkFile),false);
entry.setDisplayName(Misc.removeExtension(sLinkFile));
entries[i]=entry;
if (handler.cancel()) { return false; }
}
}
}
}
// Step 4: Create and write out the index file
OutputFile indexFile = createIndexFile(sHeading, entries);
if (!outdir.exists()) { outdir.mkdirs(); }
boolean bSuccess = true;
File outfile = new File(outdir,indexFile.getFileName());
try {
FileOutputStream fos = new FileOutputStream(outfile);
indexFile.write(fos);
fos.flush();
fos.close();
} catch (Exception writeExcept) {
bSuccess = false;
}
handler.endDirectory(indir.getPath(), bSuccess);
return !handler.cancel();
}
// Convert a single file, returning the name of the master file
// Returns null if conversion fails
private String convertFile(File infile, File outdir, BatchHandler handler) {
handler.startFile(infile.getPath());
// Currently we discriminate based on file extension
if (!(infile.getName().endsWith(".odt") || infile.getName().endsWith(".ods") || infile.getName().endsWith(".odp"))) {
handler.endFile(infile.getPath(),false);
return null;
}
// Do conversion
ConverterResult dataOut = null;
try {
// The target file name is always the same as the source
dataOut = converter.convert(infile,Misc.removeExtension(infile.getName()));
}
catch (FileNotFoundException e) {
handler.endFile(infile.getPath(),false);
return null;
}
catch (IOException e) {
handler.endFile(infile.getPath(),false);
return null;
}
// Write out files
if (!outdir.exists()) { outdir.mkdirs(); }
try {
dataOut.write(outdir);
}
catch (IOException e) {
handler.endFile(infile.getPath(),false);
return null;
}
handler.endFile(infile.getPath(),true);
return dataOut.getMasterDocument().getFileName();
}
}

View file

@ -0,0 +1,44 @@
/************************************************************************
*
* BooleanOption.java
*
* 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
*
* Copyright: 2002-2008 by Henrik Just
*
* All Rights Reserved.
*
* Version 1.0 (2008-09-08)
*
*/
package writer2latex.base;
// A BooleanOption interprets the values as booleans
public class BooleanOption extends Option {
private boolean bValue;
public boolean getValue() { return bValue; }
public void setString(String sValue) {
super.setString(sValue);
bValue = "true".equals(sValue);
}
public BooleanOption(String sName, String sDefaultValue) {
super(sName,sDefaultValue);
}
}

View file

@ -0,0 +1,170 @@
/************************************************************************
*
* ConfigBase.java
*
* 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
*
* Copyright: 2002-2008 by Henrik Just
*
* All Rights Reserved.
*
* Version 1.0 (2008-11-22)
*
*/
package writer2latex.base;
/** Base implementation of writer2latex.api.Config
*/
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.IOException;
import java.io.OutputStream;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.Element;
import org.w3c.dom.DOMImplementation;
import writer2latex.xmerge.NewDOMDocument;
public abstract class ConfigBase implements writer2latex.api.Config {
protected abstract int getOptionCount();
protected abstract String getDefaultConfigPath();
protected Option[] options;
public ConfigBase() {
options = new Option[getOptionCount()];
}
public void setOption(String sName,String sValue) {
if (sName!=null && sValue!=null) {
for (int j=0; j<getOptionCount(); j++) {
if (sName.equals(options[j].getName())) {
options[j].setString(sValue);
break;
}
}
}
}
public String getOption(String sName) {
if (sName!=null) {
for (int j=0; j<getOptionCount(); j++) {
if (sName.equals(options[j].getName())) {
return options[j].getString();
}
}
}
return null;
}
public void readDefaultConfig(String sName) throws IllegalArgumentException {
InputStream is = this.getClass().getResourceAsStream(getDefaultConfigPath()+sName);
if (is==null) {
throw new IllegalArgumentException("The internal configuration '"+sName+ "' does not exist");
}
try {
read(is);
}
catch (IOException e) {
// This would imply a bug in the configuration file!
throw new IllegalArgumentException("The internal configuration '"+sName+ "' is invalid");
}
}
/** <p>Read configuration from a specified input stream</p>
* @param is the input stream to read the configuration from
*/
public void read(InputStream is) throws IOException {
NewDOMDocument doc = new NewDOMDocument("config",".xml");
doc.read(is); // may throw an IOException
Document dom = doc.getContentDOM();
if (dom==null) {
throw new IOException("Failed to parse configuration");
}
Node root = dom.getDocumentElement();
Node child = root.getFirstChild();
while (child!=null) {
if (child.getNodeType()==Node.ELEMENT_NODE) {
Element elm = (Element)child;
if (elm.getTagName().equals("option")) {
String sName = elm.getAttribute("name");
String sValue = elm.getAttribute("value");
if (sName!="") { setOption(sName,sValue); }
}
else {
readInner(elm);
}
}
child = child.getNextSibling();
}
}
public void read(File file) throws IOException {
read(new FileInputStream(file));
}
/** Read configuration information from an xml element.
* The subclass must define this to read richer configuration data
*/
protected abstract void readInner(Element elm);
public void write(OutputStream os) throws IOException {
NewDOMDocument doc = new NewDOMDocument("config",".xml");
Document dom = null;
try {
DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = builderFactory.newDocumentBuilder();
DOMImplementation domImpl = builder.getDOMImplementation();
dom = domImpl.createDocument("","config",null);
}
catch (Throwable t) {
t.printStackTrace();
}
Element rootElement = dom.getDocumentElement();
for (int i=0; i<getOptionCount(); i++) {
Element optionNode = dom.createElement("option");
optionNode.setAttribute("name",options[i].getName());
optionNode.setAttribute("value",options[i].getString());
rootElement.appendChild(optionNode);
}
writeInner(dom);
doc.setContentDOM(dom);
doc.write(os); // may throw an IOException
}
public void write(File file) throws IOException {
write(new FileOutputStream(file));
}
/** Write configuration information to an xml document.
* The subclass must define this to write richer configuration data
*/
protected abstract void writeInner(Document dom);
}

View file

@ -0,0 +1,117 @@
/************************************************************************
*
* ConverterBase.java
*
* 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
*
* Copyright: 2002-2008 by Henrik Just
*
* All Rights Reserved.
*
* Version 1.0 (2008-11-23)
*
*/
package writer2latex.base;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.io.IOException;
import writer2latex.api.GraphicConverter;
import writer2latex.api.Converter;
import writer2latex.api.ConverterResult;
import writer2latex.api.OutputFile;
import writer2latex.office.ImageLoader;
import writer2latex.office.MetaData;
import writer2latex.office.OfficeReader;
import writer2latex.xmerge.EmbeddedObject;
import writer2latex.xmerge.ConvertData;
import writer2latex.xmerge.OfficeDocument;
/**<p>Abstract base implementation of <code>writer2latex.api.Converter</code></p>
*/
public abstract class ConverterBase implements Converter {
// Helper
protected GraphicConverter graphicConverter;
// The source document
protected OfficeDocument odDoc;
protected OfficeReader ofr;
protected MetaData metaData;
protected ImageLoader imageLoader;
// The output file(s)
protected String sTargetFileName;
protected ConvertData convertData;
// Constructor
public ConverterBase() {
graphicConverter = null;
convertData = new ConvertData();
}
// Implement the interface
public void setGraphicConverter(GraphicConverter graphicConverter) {
this.graphicConverter = graphicConverter;
}
// Provide a do noting fallback method
public void readTemplate(InputStream is) throws IOException { }
// Provide a do noting fallback method
public void readTemplate(File file) throws IOException { }
public ConverterResult convert(File source, String sTargetFileName) throws FileNotFoundException,IOException {
return convert(new FileInputStream(source), sTargetFileName);
}
public ConverterResult convert(InputStream is, String sTargetFileName) throws IOException {
// Read document
odDoc = new OfficeDocument("InFile");
odDoc.read(is);
ofr = new OfficeReader(odDoc,false);
metaData = new MetaData(odDoc);
imageLoader = new ImageLoader(odDoc,sTargetFileName,true);
imageLoader.setGraphicConverter(graphicConverter);
// Prepare output
this.sTargetFileName = sTargetFileName;
convertData.reset();
convertInner();
return convertData;
}
// The subclass must provide the implementation
public abstract void convertInner() throws IOException;
public MetaData getMetaData() { return metaData; }
public ImageLoader getImageLoader() { return imageLoader; }
public void addDocument(OutputFile doc) { convertData.addDocument(doc); }
public EmbeddedObject getEmbeddedObject(String sHref) {
return odDoc.getEmbeddedObject(sHref);
}
}

View file

@ -0,0 +1,40 @@
/************************************************************************
*
* IntegerOption.java
*
* 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
*
* Copyright: 2002-2008 by Henrik Just
*
* All Rights Reserved.
*
* Version 1.0 (2008-09-08)
*
*/
package writer2latex.base;
// An IntegerOption must always be subclassed (must override setString)
public abstract class IntegerOption extends Option {
protected int nValue;
public int getValue() { return nValue; }
public IntegerOption(String sName, String sDefaultValue) {
super(sName,sDefaultValue);
}
}

View file

@ -0,0 +1,45 @@
/************************************************************************
*
* Option.java
*
* 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
*
* Copyright: 2002-2008 by Henrik Just
*
* All Rights Reserved.
*
* Version 1.0 (2008-09-08)
*
*/
package writer2latex.base;
// The mother of all options; reads and writes string values
public class Option {
protected String sValue;
private String sName;
public void setString(String sValue) { this.sValue = sValue; }
public String getString() { return sValue; }
public String getName() { return sName; }
public Option(String sName, String sDefaultValue) {
this.sName = sName;
setString(sDefaultValue);
}
}

View file

@ -0,0 +1,17 @@
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>The package writer2latex.base</title>
</head>
<body>
<p>This package contains (abstract) base implementations of some of the
interfaces in writer2latex.api</p>
<p>They are intended to be subclassed by converters into specific formats
e.g. LaTeX, xhtml</p>
</body>
</html>