2012-10-30 2 views
1

질문 : 첨부 파일이있는 전자 메일 메시지가 있다고 가정합니다 (JPEG 첨부라고 가정). 어떻게하면 (Tika facade 클래스를 사용하지 않고) 이메일 메시지를 구문 분석하고 고유 한 부분을 반환합니까? a) 이메일 텍스트 내용과 b) 이메일 첨부 파일?Tika - 복합 문서에서 고유 항목 추출

구성 : 티카 1.2 자바 1.7

상세 사항 : 나는 제대로 기본 전자 메일 메시지 형식으로 전자 메일 메시지를 구문 분석 할 수 있었다. 그러나 구문 분석 후, 나는 이메일의 텍스트 내용과 b) 이메일에 첨부 된 내용을 알아야한다. 본인의 데이터베이스에 이러한 항목을 본질적으로 하위 첨부 파일이있는 전자 메일로 저장합니다.

알아낼 수없는 것은 내가 뚜렷한 부분을 "되돌릴"수 있고 부모 이메일에 첨부 파일이 있으며 메일에 참조 된 첨부 파일을 별도로 저장할 수 있다는 것입니다. 이것은 ZipFile 내용 추출과 근본적으로 유사하다고 생각합니다.

코드 예 :

private Message processDocument(String fullfilepath) { 
    try { 
     File filename = new File(fullfilepath) ; 
     return this.processDocument(filename) ; 
    } catch (NullPointerException npe) { 
     Message error = new Message(false) ; 
     error.appendErrorMessage("The file name was null.") ; 
     return error ; 
    } 
} 

private Message processDocument(File filename) { 
    InputStream stream = null; 
    try { 
     stream = new FileInputStream(filename) ; 
    } catch (FileNotFoundException fnfe) { 
     // TODO Auto-generated catch block 
     fnfe.printStackTrace(); 
     System.out.println("FileNotFoundException") ; 
     return diag ; 
    } 

int writelimit = -1 ; 
ContentHandler texthandler = new BodyContentHandler(writelimit); 
this.safehandlerbodytext = new SafeContentHandler(texthandler); 
this.meta = new Metadata() ; 
ParseContext context = new ParseContext() ; 

AutoDetectParser autodetectparser = new AutoDetectParser() ; 

try { 

    autodetectparser.parse(
     stream, 
     texthandler, 
     meta, 
     context) ; 

    this.documenttype = meta.get("Content-Type") ; 

    diag.setSuccessful(true); 

} catch (IOException ioe) { 
    // if the document stream could not be read 
    System.out.println("TikaTextExtractorHelper IOException " + ioe.getMessage()) ; 
    //FIXME -- add real handling 

} catch (SAXException se) { 
    // if the SAX events could not be processed 
    System.out.println("TikaTextExtractorHelper SAXException " + se.getMessage()) ; 
    //FIXME -- add real handling 

} catch (TikaException te) { 
    // if the document could not be parsed 
    System.out.println("TikaTextExtractorHelper TikaException " + te.getMessage()) ; 
    System.out.println("Exception Filename = " + filename.getName()) ; 
    //FIXME -- add real handling 

} 

}

+0

당신이 자식 문서를 잡고 그들을 저장하는 문맥에 파서를 추가 봤어 :

그럼, 당신이 아마하고 싶은 무언가처럼? – Gagravarr

+0

감사합니다. 아니, 나는 이것을 시도하지 않았다. 나는 컨텍스트가 실제로하는 일에 너무 익숙하지 않다. 귀하의 노트를 이해한다면, 다른/다른 파서를 컨텍스트 자체에 추가 할 수 있습니까? 이것이 자녀 결과를 어떻게 반환합니까? 나는 이것을 더 깊이 조사하려고 노력할 것이다. – SaB

답변

1

티카가 포함 된 문서를 명중, 당신은 재귀 파서를 공급하고 있는지 확인하기 위해 ParseContext로 이동합니다. 가지고 있다면 임베디드 리소스를 처리하는 데 사용합니다. 그렇지 않으면 건너 뜁니다.

public static class HandleEmbeddedParser extends AbstractParser { 
    public List<File> found = new ArrayList<File>(); 
    Set<MediaType> getSupportedTypes(ParseContext context) { 
     // Return what you want to handle 
     HashSet<MediaType> types = new HashSet<MediaType>(); 
     types.put(MediaType.application("pdf")); 
     types.put(MediaType.application("zip")); 
     return types; 
    } 
    void parse(
     InputStream stream, ContentHandler handler, 
     Metadata metadata, ParseContext context 
    ) throws IOException { 
     // Do something with the child documents 
     // eg save to disk 
     File f = File.createTempFile("tika","tmp"); 
     found.add(f); 

     FileOutputStream fout = new FileOutputStream(f); 
     IOUtils.copy(stream,fout); 
     fout.close(); 
    } 
} 

ParseContext context = new ParseContext(); 
context.set(Parser.class, new HandleEmbeddedParser(); 
parser.parse(....); 
+1

먼저, 고맙습니다. 둘째로, 나는 재귀가 어디서 들어오는 지에 대해서 아직도 의아해하고있다. 나는 SAX 모델 (SAX에 익숙하지 않다)을보고 다시 시작했다. 티카 파트가 어떻게 어울리는 지 조금 더 잘 이해하고 있다고 생각합니다 (SAX 이해는 핵심 요소입니다). 셋째, 가끔은 그냥 지나치게 바보 같은 짓을합니다. 티카가 이메일 첨부 파일을 제대로 분석하지 못했기 때문에 모든 문제가 발생했습니다. 그러나 나중에 알게되자, 나의 작은 테스트 데이터 세트는 암호화 된 (PGP) 이메일을 가졌다. 이것이 내 첨부 파일이 분석되지 않은 이유입니다. – SaB

+1

Tika가 임베디드 문서를 쳤을 때, 처리를 위해'ParseContext'에 파서 (있는 경우)를 제공합니다 – Gagravarr