You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

VBAMacroExtractor.java 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /* ====================================================================
  2. Licensed to the Apache Software Foundation (ASF) under one or more
  3. contributor license agreements. See the NOTICE file distributed with
  4. this work for additional information regarding copyright ownership.
  5. The ASF licenses this file to You under the Apache License, Version 2.0
  6. (the "License"); you may not use this file except in compliance with
  7. the License. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. ==================================================================== */
  15. package org.apache.poi.poifs.macros;
  16. import java.io.File;
  17. import java.io.FileNotFoundException;
  18. import java.io.FileOutputStream;
  19. import java.io.IOException;
  20. import java.io.OutputStreamWriter;
  21. import java.util.Map;
  22. import java.util.Map.Entry;
  23. import org.apache.poi.util.StringUtil;
  24. /**
  25. * This tool extracts out the source of all VBA Modules of an office file,
  26. * both OOXML (eg XLSM) and OLE2/POIFS (eg DOC), to STDOUT or a directory.
  27. *
  28. * @since 3.15-beta2
  29. */
  30. public class VBAMacroExtractor {
  31. public static void main(String[] args) throws IOException {
  32. if (args.length == 0) {
  33. System.err.println("Use:");
  34. System.err.println(" VBAMacroExtractor <office.doc> [output]");
  35. System.err.println();
  36. System.err.println("If an output directory is given, macros are written there");
  37. System.err.println("Otherwise they are output to the screen");
  38. System.exit(1);
  39. }
  40. File input = new File(args[0]);
  41. File output = null;
  42. if (args.length > 1) {
  43. output = new File(args[1]);
  44. }
  45. VBAMacroExtractor extractor = new VBAMacroExtractor();
  46. extractor.extract(input, output);
  47. }
  48. /**
  49. * Extracts the VBA modules from a macro-enabled office file and writes them
  50. * to files in <tt>outputDir</tt>.
  51. *
  52. * Creates the <tt>outputDir</tt>, directory, including any necessary but
  53. * nonexistent parent directories, if <tt>outputDir</tt> does not exist.
  54. * If <tt>outputDir</tt> is null, writes the contents to standard out instead.
  55. *
  56. * @param input the macro-enabled office file.
  57. * @param outputDir the directory to write the extracted VBA modules to.
  58. * @param extension file extension of the extracted VBA modules
  59. * @since 3.15-beta2
  60. */
  61. public void extract(File input, File outputDir, String extension) throws IOException {
  62. if (! input.exists()) throw new FileNotFoundException(input.toString());
  63. System.err.print("Extracting VBA Macros from " + input + " to ");
  64. if (outputDir != null) {
  65. if (!outputDir.exists() && !outputDir.mkdirs()) {
  66. throw new IOException("Output directory " + outputDir + " could not be created");
  67. }
  68. System.err.println(outputDir);
  69. } else {
  70. System.err.println("STDOUT");
  71. }
  72. final Map<String,String> macros;
  73. try (VBAMacroReader reader = new VBAMacroReader(input)) {
  74. macros = reader.readMacros();
  75. }
  76. final String divider = "---------------------------------------";
  77. for (Entry<String, String> entry : macros.entrySet()) {
  78. String moduleName = entry.getKey();
  79. String moduleCode = entry.getValue();
  80. if (outputDir == null) {
  81. System.out.println(divider);
  82. System.out.println(moduleName);
  83. System.out.println();
  84. System.out.println(moduleCode);
  85. } else {
  86. File out = new File(outputDir, moduleName + extension);
  87. try (FileOutputStream fout = new FileOutputStream(out);
  88. OutputStreamWriter fwriter = new OutputStreamWriter(fout, StringUtil.UTF8)) {
  89. fwriter.write(moduleCode);
  90. }
  91. System.out.println("Extracted " + out);
  92. }
  93. }
  94. if (outputDir == null) {
  95. System.out.println(divider);
  96. }
  97. }
  98. /**
  99. * Extracts the VBA modules from a macro-enabled office file and writes them
  100. * to <tt>.vba</tt> files in <tt>outputDir</tt>.
  101. *
  102. * Creates the <tt>outputDir</tt>, directory, including any necessary but
  103. * nonexistent parent directories, if <tt>outputDir</tt> does not exist.
  104. * If <tt>outputDir</tt> is null, writes the contents to standard out instead.
  105. *
  106. * @param input the macro-enabled office file.
  107. * @param outputDir the directory to write the extracted VBA modules to.
  108. * @since 3.15-beta2
  109. */
  110. public void extract(File input, File outputDir) throws IOException {
  111. extract(input, outputDir, ".vba");
  112. }
  113. }