From 8af696bc6a3151a23424453e2488c3f07b186246 Mon Sep 17 00:00:00 2001 From: Georgy Litvinov Date: Wed, 19 Feb 2020 08:46:40 +0100 Subject: [PATCH] Added mime type check, refactoring --- src/main/java/writer2latex/Application.java | 90 +++++++++++---------- 1 file changed, 48 insertions(+), 42 deletions(-) diff --git a/src/main/java/writer2latex/Application.java b/src/main/java/writer2latex/Application.java index cdebd4d..7c6a68d 100644 --- a/src/main/java/writer2latex/Application.java +++ b/src/main/java/writer2latex/Application.java @@ -99,52 +99,14 @@ public final class Application { // Convert the directory or file private void doConversion() { - // Step 1: Say hello... - String sOutputFormat; - //if (MIMETypes.LATEX.equals(sTargetMIME)) { sOutputFormat = "LaTeX"; } - //else if (MIMETypes.BIBTEX.equals(sTargetMIME)) { sOutputFormat = "BibTeX"; } - //else { sOutputFormat = "xhtml"; } - sOutputFormat = "xhtml"; - System.out.println(); - System.out.println("This is Writer2" + sOutputFormat + - ", Version " + ConverterFactory.getVersion() + - " (" + ConverterFactory.getDate() + ")"); - System.out.println(); - System.out.println("Starting conversion..."); - - // Step 2: Examine source - File source = new File(sSource); - if (!source.exists()) { - System.out.println("I'm sorry, I can't find "+sSource); - System.exit(1); - } - if (!source.canRead()) { - System.out.println("I'm sorry, I can't read "+sSource); - System.exit(1); - } + sayHello(); + File source = new File(sSource); + examineSource(source); boolean bBatch = source.isDirectory(); // Step 3: Examine target 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())); - } - } - } + target = examineTarget(source, bBatch); // Step 4: Create converters Converter converter = ConverterFactory.createConverter(sTargetMIME); @@ -309,6 +271,49 @@ public final class Application { System.out.println("Done!"); } + private File examineTarget(File source, boolean bBatch) { + 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())); + } + } + } + return target; + } + + private void examineSource(File source) { + if (!source.exists()) { + System.out.println("I'm sorry, I can't find "+sSource); + System.exit(1); + } + if (!source.canRead()) { + System.out.println("I'm sorry, I can't read "+sSource); + System.exit(1); + } + } + + private void sayHello() { + System.out.println(); + System.out.println("This is w2phtml, Version " + ConverterFactory.getVersion() + + " (" + ConverterFactory.getDate() + ")"); + System.out.println(); + System.out.println("Starting conversion..."); + } + /** * Display usage. @@ -364,6 +369,7 @@ public final class Application { else if ("-xhtml+mathml".equals(sArg)) { sTargetMIME = MIMETypes.XHTML_MATHML; } else if ("-epub".equals(sArg)) { sTargetMIME = MIMETypes.EPUB; } else if ("-epub3".equals(sArg)) { sTargetMIME = MIMETypes.EPUB3; } + else if ("-rdf".equals(sArg)) { sTargetMIME = MIMETypes.RDF; } else if ("-recurse".equals(sArg)) { bRecurse = true; } else if ("-ultraclean".equals(sArg)) { configFileNames.add("*ultraclean.xml"); } else if ("-clean".equals(sArg)) { configFileNames.add("*clean.xml"); }