DokeosConverter.java 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. //
  2. // DokeosConverter using JODConverter - Java OpenDocument Converter
  3. // Eric Marguin <e.marguin@elixir-interactive.com>
  4. //
  5. // This library is free software; you can redistribute it and/or
  6. // modify it under the terms of the GNU Lesser General Public
  7. // License as published by the Free Software Foundation; either
  8. // version 2.1 of the License, or (at your option) any later version.
  9. //
  10. // This library is distributed in the hope that it will be useful,
  11. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. // Lesser General Public License for more details.
  14. // http://www.gnu.org/copyleft/lesser.html
  15. //
  16. import java.io.File;
  17. import java.net.ConnectException;
  18. import org.apache.commons.cli.CommandLine;
  19. import org.apache.commons.cli.CommandLineParser;
  20. import org.apache.commons.cli.HelpFormatter;
  21. import org.apache.commons.cli.Option;
  22. import org.apache.commons.cli.Options;
  23. import org.apache.commons.cli.PosixParser;
  24. import org.apache.commons.io.FilenameUtils;
  25. import com.artofsolving.jodconverter.DocumentConverter;
  26. import com.artofsolving.jodconverter.openoffice.connection.OpenOfficeConnection;
  27. import com.artofsolving.jodconverter.openoffice.connection.SocketOpenOfficeConnection;
  28. import com.artofsolving.jodconverter.openoffice.converter.OpenOfficeDocumentConverter;
  29. /**
  30. * Command line tool to convert a document into a different format.
  31. * <p>
  32. * Usage: can convert a single file
  33. *
  34. * <pre>
  35. * ConvertDocument test.odt test.pdf
  36. * </pre>
  37. *
  38. * or multiple files at once by specifying the output format
  39. *
  40. * <pre>
  41. * ConvertDocument -f pdf test1.odt test2.odt
  42. * ConvertDocument -f pdf *.odt
  43. * </pre>
  44. */
  45. public class DokeosConverter {
  46. private static final Option OPTION_OUTPUT_FORMAT = new Option("f", "output-format", true, "output format (e.g. pdf)");
  47. private static final Option OPTION_PORT = new Option("p", "port", true, "OpenOffice.org port");
  48. private static final Option OPTION_VERBOSE = new Option("v", "verbose", false, "verbose");
  49. private static final Option OPTION_DOKEOS_MODE = new Option("d", "dokeos-mode", true, "use oogie or woogie");
  50. private static final Option OPTION_WIDTH = new Option("w", "width", true, "width");
  51. private static final Option OPTION_HEIGHT = new Option("h", "height", true, "height");
  52. private static final Options OPTIONS = initOptions();
  53. private static final int EXIT_CODE_CONNECTION_FAILED = 1;
  54. private static final int EXIT_CODE_CONVERSION_FAILED = 2;
  55. private static final int EXIT_CODE_TOO_FEW_ARGS = 255;
  56. private static Options initOptions() {
  57. Options options = new Options();
  58. options.addOption(OPTION_OUTPUT_FORMAT);
  59. options.addOption(OPTION_PORT);
  60. options.addOption(OPTION_VERBOSE);
  61. options.addOption(OPTION_DOKEOS_MODE);
  62. options.addOption(OPTION_WIDTH);
  63. options.addOption(OPTION_HEIGHT);
  64. return options;
  65. }
  66. public static void main(String[] arguments) throws Exception {
  67. CommandLineParser commandLineParser = new PosixParser();
  68. CommandLine commandLine = commandLineParser.parse(OPTIONS, arguments);
  69. int port = SocketOpenOfficeConnection.DEFAULT_PORT;
  70. if (commandLine.hasOption(OPTION_PORT.getOpt())) {
  71. port = Integer.parseInt(commandLine.getOptionValue(OPTION_PORT.getOpt()));
  72. }
  73. String outputFormat = null;
  74. if (commandLine.hasOption(OPTION_OUTPUT_FORMAT.getOpt())) {
  75. outputFormat = commandLine.getOptionValue(OPTION_OUTPUT_FORMAT.getOpt());
  76. }
  77. boolean verbose = false;
  78. if (commandLine.hasOption(OPTION_VERBOSE.getOpt())) {
  79. verbose = true;
  80. }
  81. String dokeosMode = "woogie";
  82. if (commandLine.hasOption(OPTION_DOKEOS_MODE.getOpt())) {
  83. dokeosMode = commandLine.getOptionValue(OPTION_DOKEOS_MODE.getOpt());
  84. }
  85. int width = 800;
  86. if (commandLine.hasOption(OPTION_WIDTH.getOpt())) {
  87. width = Integer.parseInt(commandLine.getOptionValue(OPTION_WIDTH.getOpt()));
  88. }
  89. int height = 600;
  90. if (commandLine.hasOption(OPTION_HEIGHT.getOpt())) {
  91. height = Integer.parseInt(commandLine.getOptionValue(OPTION_HEIGHT.getOpt()));
  92. }
  93. String[] fileNames = commandLine.getArgs();
  94. if ((outputFormat == null && fileNames.length != 2 && dokeosMode!=null) || fileNames.length < 1) {
  95. String syntax = "convert [options] input-file output-file; or\n"
  96. + "[options] -f output-format input-file [input-file...]";
  97. HelpFormatter helpFormatter = new HelpFormatter();
  98. helpFormatter.printHelp(syntax, OPTIONS);
  99. System.exit(EXIT_CODE_TOO_FEW_ARGS);
  100. }
  101. OpenOfficeConnection connection = new DokeosSocketOfficeConnection(port);
  102. try {
  103. if (verbose) {
  104. System.out.println("-- connecting to OpenOffice.org on port " + port);
  105. }
  106. connection.connect();
  107. } catch (ConnectException officeNotRunning) {
  108. System.err
  109. .println("ERROR: connection failed. Please make sure OpenOffice.org is running and listening on port "
  110. + port + ".");
  111. System.exit(EXIT_CODE_CONNECTION_FAILED);
  112. }
  113. try {
  114. // choose the good constructor to deal with the conversion
  115. DocumentConverter converter;
  116. if(dokeosMode.equals("oogie")){
  117. converter = new OogieDocumentConverter(connection, new DokeosDocumentFormatRegistry(), width, height);
  118. }
  119. else if(dokeosMode.equals("woogie")){
  120. converter = new WoogieDocumentConverter(connection, new DokeosDocumentFormatRegistry(), width, height);
  121. }
  122. else {
  123. converter = new OpenOfficeDocumentConverter(connection);
  124. }
  125. if (outputFormat == null) {
  126. File inputFile = new File(fileNames[0]);
  127. File outputFile = new File(fileNames[1]);
  128. convertOne(converter, inputFile, outputFile, verbose);
  129. } else {
  130. for (int i = 0; i < fileNames.length; i++) {
  131. File inputFile = new File(fileNames[i]);
  132. File outputFile = new File(FilenameUtils.getFullPath(fileNames[i])
  133. + FilenameUtils.getBaseName(fileNames[i]) + "." + outputFormat);
  134. convertOne(converter, inputFile, outputFile, verbose);
  135. }
  136. }
  137. }
  138. catch (com.artofsolving.jodconverter.openoffice.connection.OpenOfficeException e)
  139. {
  140. connection.disconnect();
  141. System.err.println("ERROR: conversion failed.");
  142. System.exit(EXIT_CODE_CONVERSION_FAILED);
  143. }
  144. finally {
  145. if (verbose) {
  146. System.out.println("-- disconnecting");
  147. }
  148. connection.disconnect();
  149. }
  150. }
  151. private static void convertOne(DocumentConverter converter, File inputFile, File outputFile, boolean verbose) {
  152. if (verbose) {
  153. System.out.println("-- converting " + inputFile + " to " + outputFile);
  154. }
  155. converter.convert(inputFile, outputFile);
  156. }
  157. }