1 30 package test.pdfbox.util; 31 32 import java.io.File ; 33 import java.io.FilenameFilter ; 34 import java.io.FileOutputStream ; 35 import java.io.OutputStream ; 36 import java.io.OutputStreamWriter ; 37 import java.io.Writer ; 38 39 import junit.framework.Test; 40 import junit.framework.TestCase; 41 import junit.framework.TestSuite; 42 43 import org.pdfbox.pdmodel.PDDocument; 44 45 import org.pdfbox.util.PDFTextStripper; 46 47 53 public class TestTextStripperPerformance extends TestCase 54 { 55 60 public TestTextStripperPerformance( String name ) 61 { 62 super( name ); 63 } 64 65 68 public void setUp() 69 { 70 } 71 72 73 80 public void doTestFile(File file, boolean bLogResult) 81 throws Exception 82 { 83 84 PDFTextStripper stripper = new PDFTextStripper(); 85 OutputStream os = null; 86 Writer writer = null; 87 PDDocument document = null; 88 try 89 { 90 document = PDDocument.load(file); 91 92 File outFile = new File (file.getParentFile().getParentFile(), "output/" + file.getName() + ".txt"); 93 os = new FileOutputStream (outFile); 94 writer = new OutputStreamWriter (os); 95 96 stripper.writeText(document, writer); 97 } 98 finally 99 { 100 if( writer != null ) 101 { 102 writer.close(); 103 } 104 if( os != null ) 105 { 106 os.close(); 107 } 108 if( document != null ) 109 { 110 document.close(); 111 } 112 } 113 } 114 115 120 public void testExtract() 121 throws Exception 122 { 123 String filename = System.getProperty("test.pdfbox.util.TextStripper.file"); 124 File testDir = new File ("test/input"); 125 126 if ((filename == null) || (filename.length() == 0)) 127 { 128 File [] testFiles = testDir.listFiles(new FilenameFilter () 129 { 130 public boolean accept(File dir, String name) 131 { 132 return (name.endsWith(".pdf")); 133 } 134 }); 135 136 for (int n = 0; n < testFiles.length; n++) 137 { 138 doTestFile(testFiles[n], false); 139 } 140 } 141 else 142 { 143 } 145 } 146 147 152 public static Test suite() 153 { 154 return new TestSuite( TestTextStripperPerformance.class ); 155 } 156 157 162 public static void main( String [] args ) 163 { 164 String [] arg = {TestTextStripperPerformance.class.getName() }; 165 junit.textui.TestRunner.main( arg ); 166 } 167 } | Popular Tags |