Changeset 1404


Ignore:
Timestamp:
02/01/11 02:03:44 (16 months ago)
Author:
jharrop
Message:

Option to process all docx in a dir

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/docx4j/src/main/java/org/docx4j/samples/StripParts.java

    r1103 r1404  
    2222 
    2323 
     24import java.io.File; 
    2425import java.net.URI; 
    2526import java.util.ArrayList; 
     
    3839public class StripParts { 
    3940         
     41 
     42        static boolean save = true; 
     43        static boolean flatOpcXmlOutput = false; 
     44        static boolean overwriteInputFile = true; 
     45         
     46        static String dir = System.getProperty("user.dir") + "/src/test/resources/AlteredParts/"; 
     47        static String file = "blagh";  // set to null to process all docx in dir 
     48         
     49        static boolean stripPropertiesParts = true; 
     50        static boolean keepStyles = true; 
     51        static boolean defaultToDelete = false; 
     52         
    4053        private static Logger log = Logger.getLogger(StripParts.class);                                          
    41  
     54         
    4255        /** 
    4356         * @param args 
     
    4558        public static void main(String[] args) throws Exception { 
    4659 
    47                 // Do we want to save output?  
    48                 boolean save = true; 
    49                 boolean flatOpcXmlOutput = true; 
    50                  
    51                 String dir = System.getProperty("user.dir") + "/foo/"; 
    52                  
    53                 String file = "bar"; 
    54                 String inputfilepath = dir + file + ".docx"; 
    55                                  
    56                 // If so, whereto? 
    57                 String outputfilepath = null; 
    58                 if (save) { 
    59                         if (flatOpcXmlOutput) { 
    60                                 outputfilepath = dir + file + "_OUT.xml"; 
    61                         } else { 
    62                                 outputfilepath = dir + file + "_OUT.docx";                               
    63                         } 
    64                 } 
     60                if (file==null) { 
     61 
     62                        List<File> filesToProcess = new ArrayList<File>(); 
     63                        File[] filesAndDirs = new File(dir).listFiles(); 
     64                        for (File file : filesAndDirs) { 
     65                                if (file.isFile() 
     66                                                && file.getName().endsWith(".docx") ) { 
     67                                        filesToProcess.add(file); 
     68                                } 
     69                        } 
     70 
     71                        for (File file : filesToProcess) { 
     72                                String outputfilepath = null; 
     73                                if (save && overwriteInputFile) { 
     74                                        outputfilepath = file.getAbsolutePath();  
     75                                } 
     76                                 
     77                                processFile(file, outputfilepath); 
     78                                 
     79                        } 
     80                         
     81                } else { 
     82                        String inputfilepath = dir + file + ".docx"; 
     83         
     84                        // If so, whereto? 
     85                        String outputfilepath = null; 
     86                        if (save) { 
     87                                if (overwriteInputFile) { 
     88                                        outputfilepath = inputfilepath;  
     89                                } else if (flatOpcXmlOutput) { 
     90                                        outputfilepath = dir + file + "_OUT.xml"; 
     91                                } else { 
     92                                        outputfilepath = dir + file + "_OUT.docx";                               
     93                                } 
     94                        } 
     95                         
     96                        processFile(new java.io.File(inputfilepath), outputfilepath); 
     97                } 
     98        } 
     99 
     100        private static void processFile(File inputfile, String outputfilepath) 
     101                        throws Docx4JException { 
    65102                         
    66103                // Open a document from the file system 
    67104                // 1. Load the Package - .docx or Flat OPC .xml 
    68                 WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.load(new java.io.File(inputfilepath));           
     105                WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.load(inputfile);                 
    69106                 
    70107                // List the parts by walking the rels tree 
     
    80117                        System.out.println("Saved stripped to " + outputfilepath); 
    81118                } else { 
    82                         System.out.println("Stripped parts from " + inputfilepath); 
     119                        System.out.println("Stripped parts from " + inputfile.getName() ); 
    83120                } 
    84121        } 
     
    92129                        StringBuilder sb, String indent) 
    93130         throws Docx4JException { 
    94                  
    95                 boolean stripPropertiesParts = true; 
    96                 boolean keepStyles = false; 
    97                 boolean defaultToDelete = false; 
    98                  
     131                                 
    99132                List<Relationship> deletions = new ArrayList<Relationship>(); 
    100133                 
Note: See TracChangeset for help on using the changeset viewer.