FileDocCategorySizeDatePackage
TestDifferentRoutes.javaAPI DocApache Poi 3.0.13101Sun Mar 11 12:59:30 GMT 2007org.apache.poi.hwpf.extractor

TestDifferentRoutes

public class TestDifferentRoutes extends TestCase
Test the different routes to extracting text
author
Nick Burch (nick at torchbox dot com)

Fields Summary
private String[]
p_text
private HWPFDocument
doc
Constructors Summary
Methods Summary
protected voidsetUp()

	
         
		String dirname = System.getProperty("HWPF.testdata.path");
		
		String filename = dirname + "/test2.doc";
		doc = new HWPFDocument(new FileInputStream(filename));
    
public voidtestExtractFromModel()
Test model based extraction

    	Range r = doc.getRange();
    	
    	String[] text = new String[r.numParagraphs()];
    	for(int i=0; i < r.numParagraphs(); i++) {
    		Paragraph p = r.getParagraph(i);
    		text[i] = p.text();
    	}
    	
    	assertEquals(p_text.length, text.length);
    	for(int i=0; i<p_text.length; i++) {
    		assertEquals(p_text[i], text[i]);
    	}
    
public voidtestExtractFromTextPieces()
Test textPieces based extraction

    	StringBuffer textBuf = new StringBuffer();
    	
    	Iterator textPieces = doc.getTextTable().getTextPieces().iterator();
    	while (textPieces.hasNext()) {
    		TextPiece piece = (TextPiece) textPieces.next();

    		String encoding = "Cp1252";
    		if (piece.usesUnicode()) {
    			encoding = "UTF-16LE";
    		}
    		String text = new String(piece.getRawBytes(), encoding);
    		textBuf.append(text);
    	}
    	
    	StringBuffer exp = new StringBuffer();
    	for(int i=0; i<p_text.length; i++) {
    		exp.append(p_text[i]);
    	}
    	assertEquals(exp.toString(), textBuf.toString());