Lucene 실전 개발 수기(4)--- PDF/excel/doc 형식의 문서에 색인 만들기

3810 단어 htmlExcelLucene
위에서 언급한 코드는 다음과 같습니다.
DocHander docHander = DocHanderFactory.buildDocHander(fileName);   
           
        attachDocument = docHander.getDocument(attach);   

 
다음은 세부 사항을 살펴보겠습니다.
 
추상 클래스 DocHander 코드:
public abstract class DocHander {
	public static String FIELD_CONTENT = "contents";		
	
	public abstract Document getDocument(byte[] inputByte) throws Exception;	
	
	protected Document addContent(Document document, String content){		
		document.add(new Field(DocHander.FIELD_CONTENT, content ,Field.Store.YES,Field.Index.TOKENIZED));
		return document;
	}	
	
}

 
이제 Factory 클래스 DocHanderFactory 코드를 살펴보겠습니다.
public abstract class DocHanderFactory {
	
	public static DocHander buildDocHander(String fileName){
		DocHander docHander = null;
		if (fileName.toLowerCase().endsWith(".doc")){
			docHander = new WordDocHander();
		}
		else if(fileName.toLowerCase().endsWith(".xls")){
			docHander = new ExcelDocHander();
		}
		else if(fileName.toLowerCase().endsWith(".pdf")){
			docHander = new PdfDocHander();
		}
		else if(fileName.toLowerCase().endsWith(".html") || fileName.toLowerCase().endsWith(".htm")){
			docHander = new HtmlDocHander();
		}
		else{
			docHander = new TxtDocHander();
		}
		return docHander;
	}
}

 
다음은 WordDocHander, Excel DocHander, PdfDocHander의 코드를 붙여 놓았습니다. 다른 사람이 포장해 주었기 때문에 간단하게 썼습니다. 감사합니다.
public class WordDocHander extends DocHander {

	public Document getDocument(byte[] inputByte) throws IOException {
		InputStream inputStream = new ByteArrayInputStream(inputByte);
		// TODO Auto-generated method stub		
		Document document = new Document();
		WordExtractor extractor = new WordExtractor(inputStream);		
		addContent(document,extractor.getText());
		return document;
	}
}
public class ExcelDocHander extends DocHander {

	public Document getDocument(byte[] inputByte) throws IOException {
		// TODO Auto-generated method stub
		InputStream inputStream = new ByteArrayInputStream(inputByte);
		Document document = new Document();
		HSSFWorkbook wb = new HSSFWorkbook(inputStream);
		ExcelExtractor extractor = new ExcelExtractor(wb);

		extractor.setFormulasNotResults(true);
		extractor.setIncludeSheetNames(false);
		String content = extractor.getText();

		return addContent(document, content);
	}

}
public class PdfDocHander extends DocHander {

	public Document getDocument(byte[] inputByte) throws IOException {		
//		Document document = LucenePDFDocument.getDocument(inputStream);// , 
		InputStream inputStream = new ByteArrayInputStream(inputByte);
		Document document = new Document();
		PDDocument pdfDocument = PDDocument.load(inputStream );
		try {
	        if( pdfDocument.isEncrypted() )
	        {
	            //Just try using the default password and move on            
				pdfDocument.decrypt( "" );					
	        }//create a writer where to append the text content.
	        StringWriter writer = new StringWriter();
	        PDFTextStripper stripper = new PDFTextStripper();        
	        stripper.writeText( pdfDocument, writer );
	        String contents = writer.getBuffer().toString();
	        super.addContent(document, contents);
		} catch (CryptographyException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();		
			throw new IOException( "Error decrypting document: " + e );
		} catch (InvalidPasswordException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			throw new IOException( "Error decrypting document: " + e );
		}
        
		return document;
	}

}

좋은 웹페이지 즐겨찾기