diff --git a/src/main/java/w2phtml/Application.java b/src/main/java/w2phtml/Application.java index c267444..85f0856 100644 --- a/src/main/java/w2phtml/Application.java +++ b/src/main/java/w2phtml/Application.java @@ -37,7 +37,7 @@ import java.util.Hashtable; import java.util.Set; import java.util.Vector; -import w2phtml.api.BatchConverter; +import pro.litvinovg.w2phtml.gui.ConfigurationWindow; import w2phtml.api.Converter; import w2phtml.api.ConverterFactory; import w2phtml.api.ConverterResult; @@ -83,7 +83,11 @@ public final class Application { * @param args The argument passed on the command line. */ public static final void main (String[] args){ - try { + if (args.length == 0) { + showUsage(""); + ConfigurationWindow.runGUI(null); + } else { + try { //long time = System.currentTimeMillis(); Application app = new Application(); app.parseCommandLine(args); @@ -93,6 +97,8 @@ public final class Application { String msg = ex.getMessage(); showUsage(msg); } + } + } // Convert the directory or file @@ -100,11 +106,10 @@ public final class Application { sayHello(); File source = new File(sSource); examineSource(source); - boolean bBatch = source.isDirectory(); // Step 3: Examine target File target; - target = examineTarget(source, bBatch); + target = examineTarget(source); // Step 4: Create converters Converter converter = ConverterFactory.createConverter(sTargetMIME); @@ -113,26 +118,13 @@ public final class Application { System.exit(1); } - BatchConverter batchCv = null; - if (bBatch) { - batchCv = ConverterFactory.createBatchConverter(MIMETypes.XHTML); - if (batchCv==null) { - System.out.println("Failed to create batch converter"); - System.exit(1); - } - batchCv.setConverter(converter); - } - + // Step 5a: Read template if (sTemplateFileName!=null) { try { System.out.println("Reading template "+sTemplateFileName); byte [] templateBytes = Misc.inputStreamToByteArray(new FileInputStream(sTemplateFileName)); converter.readTemplate(new ByteArrayInputStream(templateBytes)); - if (batchCv!=null) { - // Currently we use the same template for the directory and the files - batchCv.readTemplate(new ByteArrayInputStream(templateBytes)); - } } catch (FileNotFoundException e) { System.out.println("--> This file does not exist!"); @@ -204,10 +196,7 @@ public final class Application { try { byte[] configBytes = Misc.inputStreamToByteArray(new FileInputStream(sConfigFileName)); converter.getConfig().read(new ByteArrayInputStream(configBytes)); - if (bBatch) { - // Currently we use the same configuration for the directory and the files - batchCv.getConfig().read(new ByteArrayInputStream(configBytes)); - } + } catch (IOException e) { System.err.println("--> Failed to read the configuration!"); @@ -222,16 +211,11 @@ public final class Application { String sKey = keys.nextElement(); String sValue = (String) options.get(sKey); converter.getConfig().setOption(sKey,sValue); - if (batchCv!=null) { - batchCv.getConfig().setOption(sKey,sValue); - } } // Step 8: Perform conversion - if (bBatch) { - batchCv.convert(source,target,bRecurse, new BatchHandlerImpl()); - } - else { + + System.out.println("Converting "+source.getPath()); ConverterResult dataOut = null; @@ -263,33 +247,24 @@ public final class Application { System.exit(1); } - } + // Step 9: Say goodbye! System.out.println("Done!"); } - private File examineTarget(File source, boolean bBatch) { + private File examineTarget(File source) { File target; - if (bBatch) { - if (sTarget==null) { - target=source; - } - else { - target = new File(sTarget); - } - } - else { - if (sTarget==null) { - target = new File(source.getParent(),Misc.removeExtension(source.getName())); - } - else { - target = new File(sTarget); - if (sTarget.endsWith(File.separator)) { - target = new File(target,Misc.removeExtension(source.getName())); - } - } + + if (sTarget == null) { + target = new File(source.getParent(), Misc.removeExtension(source.getName())); + } else { + target = new File(sTarget); + if (sTarget.endsWith(File.separator)) { + target = new File(target, Misc.removeExtension(source.getName())); + } } + return target; } diff --git a/src/main/java/w2phtml/api/BatchConverter.java b/src/main/java/w2phtml/api/BatchConverter.java deleted file mode 100644 index 78b4a8d..0000000 --- a/src/main/java/w2phtml/api/BatchConverter.java +++ /dev/null @@ -1,95 +0,0 @@ -/************************************************************************ - * - * BatchConverter.java - * - * Copyright: 2002-2008 by Henrik Just - * - * This file is part of Writer2LaTeX. - * - * Writer2LaTeX is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * Writer2LaTeX 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 General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with Writer2LaTeX. If not, see . - * - * Version 1.0 (2008-11-23) - * - */ - -package w2phtml.api; - -import java.io.File; -import java.io.InputStream; -import java.io.IOException; - -/** This is an interface for a converter, which offers conversion of - * all OpenDocument (or OpenOffice.org 1.x) documents in a directory - * (and optionally subdirectories), creating index pages in a specific format. - * Instances of this interface are created using the - * {@link ConverterFactory} - */ -public interface BatchConverter { - - /** Get the configuration interface for this batch converter - * - * @return the configuration - */ - public Config getConfig(); - - /** Define a Converter implementation to use for - * conversion of the individual documents. - * If no converter is given, the convert method cannot - * convert documents (but can still create index pages). - * - * @param converter the Converter to use - */ - public void setConverter(Converter converter); - - /** Read a template to use as a base for the index pages. - * The format of the template depends on the BatchConverter - * implementation. - * - * @param is an InputStream from which to read the template - * @throws IOException if some exception occurs while reading the template - */ - public void readTemplate(InputStream is) throws IOException; - - /** Read a template to use as a base for the index pages. - * The format of the template depends on the BatchConverter - * implementation. - * - * @param file the file from which to read the template - * @throws IOException if the file does not exist or some exception occurs - * while reading the template - */ - public void readTemplate(File file) throws IOException; - - /** Create an index page with specific entries - * - * @param sHeading a heading describing the index page - * @param entries an array of IndexPageEntry objects (null entries - * are allowed, and will be ignored) describing the individual directories - * and documents - */ - public OutputFile createIndexFile(String sHeading, IndexPageEntry[] entries); - - /** Convert a directory using the given Converter (if none is given, - * all files will be ignored). - * This method fails silently if you haven't set a converter. - * - * @param source a File representing the directory to convert - * @param target a File representing the directory to contain - * the converted documents - * @param bRecurse determines wether or not to recurse into subdirectories - * @param handler a BatchHandler - */ - public void convert(File source, File target, boolean bRecurse, BatchHandler handler); - -} diff --git a/src/main/java/w2phtml/api/ConverterFactory.java b/src/main/java/w2phtml/api/ConverterFactory.java index 61037bd..dfb7075 100644 --- a/src/main/java/w2phtml/api/ConverterFactory.java +++ b/src/main/java/w2phtml/api/ConverterFactory.java @@ -122,23 +122,7 @@ public class ConverterFactory { return converter instanceof Converter ? (Converter) converter : null; } - /**

Create a BatchConverter implementation which supports - * conversion into the specified MIME type

- *

The only currently supported MIME type is text/html - * (XHTML 1.0 strict)

- * - * @param sMIME the MIME type of the target format - * @return the required BatchConverter or null if a converter - * for the requested MIME type could not be created - */ - public static BatchConverter createBatchConverter(String sMIME) { - Object converter = null; - if (MIMETypes.XHTML.equals(sMIME)) { - converter = createInstance("w2phtml.xhtml.BatchConverterImpl"); - } - return converter instanceof BatchConverter ? (BatchConverter) converter : null; - } - + /** Create a StarMathConverter implementation * * @return the converter diff --git a/src/main/java/w2phtml/base/BatchConverterBase.java b/src/main/java/w2phtml/base/BatchConverterBase.java deleted file mode 100644 index c283dcd..0000000 --- a/src/main/java/w2phtml/base/BatchConverterBase.java +++ /dev/null @@ -1,180 +0,0 @@ -/************************************************************************ - * - * BatchConverterBase.java - * - * Copyright: 2002-2014 by Henrik Just - * - * This file is part of Writer2LaTeX. - * - * Writer2LaTeX is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * Writer2LaTeX 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 General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with Writer2LaTeX. If not, see . - * - * Version 1.4 (2014-08-27) - * - */ - -package w2phtml.base; - -import java.io.File; -import java.io.FileOutputStream; -import java.io.FileNotFoundException; -import java.io.IOException; - -import w2phtml.api.BatchConverter; -import w2phtml.api.BatchHandler; -import w2phtml.api.Converter; -import w2phtml.api.ConverterResult; -import w2phtml.api.IndexPageEntry; -import w2phtml.api.OutputFile; -import w2phtml.util.Misc; - -/** - * Abstract base implementation of writer2latex.api.BatchConverter. - * 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. - * - * Version 1.2 (2010-04-23) - * - */ - -package w2phtml.xhtml; - -import java.io.File; -import java.io.FileInputStream; -import java.io.InputStream; -import java.io.IOException; -import java.util.Locale; -import java.text.Collator; -import org.w3c.dom.Element; - -import w2phtml.api.IndexPageEntry; -import w2phtml.api.OutputFile; -import w2phtml.base.BatchConverterBase; -import w2phtml.xhtml.l10n.L10n; - -/** - * Implementation of writer2latex.api.BatchConverter for - * xhtml 1.0 strict - */ -public class BatchConverter extends BatchConverterBase { - - private XhtmlConfig config; - private XhtmlDocument template; - - private String sDefaultLang; - private String sDefaultCountry; - private L10n l10n; - - public BatchConverter() { - super(); - config = new XhtmlConfig(); - template = null; - - l10n = new L10n(); - sDefaultLang = System.getProperty("user.language"); - sDefaultCountry = System.getProperty("user.country"); - l10n.setLocale(sDefaultLang, sDefaultCountry); - } - - // Implementation of the remaining (xhtml specific) parts of the interface - - public w2phtml.api.Config getConfig() { - return config; - } - - public void readTemplate(InputStream is) throws IOException { - template = new XhtmlDocument("Template",XhtmlDocument.XHTML10); - try { - template.read(is); - } - catch (IOException e) { - template = null; - throw e; - } - } - - public void readTemplate(File file) throws IOException { - readTemplate(new FileInputStream(file)); - } - - protected String getIndexFileName() { - return "index.html"; - } - - public OutputFile createIndexFile(String sHeading, IndexPageEntry[] entries) { - // Create the index page (with header/footer or from template) - XhtmlDocument htmlDoc = new XhtmlDocument("index",XhtmlDocument.XHTML10); - htmlDoc.setConfig(config); - if (template!=null) { htmlDoc.readFromTemplate(template); } - else { htmlDoc.createHeaderFooter(); } - - org.w3c.dom.Document htmlDOM = htmlDoc.getContentDOM(); - - Element head = htmlDoc.getHeadNode(); - if (head!=null) { - // Declare charset (we need this for xhtml because we have no ) - Element meta = htmlDOM.createElement("meta"); - meta.setAttribute("http-equiv","Content-Type"); - meta.setAttribute("content","text/html; charset="+htmlDoc.getEncoding().toLowerCase()); - head.appendChild(meta); - - // Add link to stylesheet - if (config.xhtmlCustomStylesheet().length()>0) { - Element htmlStyle = htmlDOM.createElement("link"); - htmlStyle.setAttribute("rel","stylesheet"); - htmlStyle.setAttribute("type","text/css"); - htmlStyle.setAttribute("media","all"); - htmlStyle.setAttribute("href",config.xhtmlCustomStylesheet()); - head.appendChild(htmlStyle); - } - } - - // Add uplink to header and footer - Element header = htmlDoc.getHeaderNode(); - if (header!=null) { - if (config.getXhtmlUplink().length()>0) { - Element a = htmlDOM.createElement("a"); - a.setAttribute("href",config.getXhtmlUplink()); - a.appendChild(htmlDOM.createTextNode(l10n.get(L10n.UP))); - header.appendChild(a); - } - else { - header.appendChild(htmlDOM.createTextNode(l10n.get(L10n.UP))); - } - } - - Element footer = htmlDoc.getFooterNode(); - if (footer!=null) { - if (config.getXhtmlUplink().length()>0) { - Element a = htmlDOM.createElement("a"); - a.setAttribute("href",config.getXhtmlUplink()); - a.appendChild(htmlDOM.createTextNode(l10n.get(L10n.UP))); - footer.appendChild(a); - } - else { - footer.appendChild(htmlDOM.createTextNode(l10n.get(L10n.UP))); - } - } - - // Add title and heading - Element title = htmlDoc.getTitleNode(); - if (title!=null) { - title.appendChild(htmlDOM.createTextNode(sHeading)); - } - Element h1 = htmlDOM.createElement("h1"); - htmlDoc.getContentNode().appendChild(h1); - h1.appendChild(htmlDOM.createTextNode(sHeading)); - - // Sort the entries - int nLen = entries.length; - Collator collator = Collator.getInstance(new Locale(sDefaultLang,sDefaultCountry)); - for (int i = 0; i 0) { - entries[i] = entryj; - entries[j] = entryi; - } - } - } - } - } - - // Insert directory entries - boolean bUseIcon = config.getXhtmlDirectoryIcon().length()>0; - for (int i=0; i0; - for (int i=0; i