KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > opencms > search > extractors > CmsExtractorPdf


1 /*
2  * File : $Source: /usr/local/cvs/opencms/src/org/opencms/search/extractors/CmsExtractorPdf.java,v $
3  * Date : $Date: 2005/07/29 12:13:00 $
4  * Version: $Revision: 1.9 $
5  *
6  * This library is part of OpenCms -
7  * the Open Source Content Mananagement System
8  *
9  * Copyright (c) 2005 Alkacon Software GmbH (http://www.alkacon.com)
10  *
11  * This library is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU Lesser General Public
13  * License as published by the Free Software Foundation; either
14  * version 2.1 of the License, or (at your option) any later version.
15  *
16  * This library is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19  * Lesser General Public License for more details.
20  *
21  * For further information about Alkacon Software GmbH, please see the
22  * company website: http://www.alkacon.com
23  *
24  * For further information about OpenCms, please see the
25  * project website: http://www.opencms.org
26  *
27  * You should have received a copy of the GNU Lesser General Public
28  * License along with this library; if not, write to the Free Software
29  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30  */

31
32 package org.opencms.search.extractors;
33
34 import org.opencms.util.CmsStringUtil;
35
36 import java.io.InputStream JavaDoc;
37 import java.util.HashMap JavaDoc;
38 import java.util.Map JavaDoc;
39
40 import org.pdfbox.encryption.DocumentEncryption;
41 import org.pdfbox.pdfparser.PDFParser;
42 import org.pdfbox.pdmodel.PDDocument;
43 import org.pdfbox.pdmodel.PDDocumentInformation;
44 import org.pdfbox.util.PDFTextStripper;
45
46 /**
47  * Extracts the text form a PDF document.<p>
48  *
49  * @author Alexander Kandzior
50  *
51  * @version $Revision: 1.9 $
52  *
53  * @since 6.0.0
54  */

55 public final class CmsExtractorPdf extends A_CmsTextExtractor {
56
57     /** Static member instance of the extractor. */
58     private static final CmsExtractorPdf INSTANCE = new CmsExtractorPdf();
59
60     /**
61      * Hide the public constructor.<p>
62      */

63     private CmsExtractorPdf() {
64
65         // noop
66
}
67
68     /**
69      * Returns an instance of this text extractor.<p>
70      *
71      * @return an instance of this text extractor
72      */

73     public static I_CmsTextExtractor getExtractor() {
74
75         return INSTANCE;
76     }
77
78     /**
79      * @see org.opencms.search.extractors.I_CmsTextExtractor#extractText(java.io.InputStream, java.lang.String)
80      */

81     public I_CmsExtractionResult extractText(InputStream JavaDoc in, String JavaDoc encoding) throws Exception JavaDoc {
82
83         PDDocument pdfDocument = null;
84
85         try {
86             PDFParser parser = new PDFParser(in);
87             parser.parse();
88
89             pdfDocument = parser.getPDDocument();
90             
91             // check for encryption
92
if (pdfDocument.isEncrypted()) {
93                 DocumentEncryption decryptor = new DocumentEncryption(pdfDocument);
94                 // try using the default password
95
decryptor.decryptDocument("");
96             }
97
98             // create PDF stripper
99
PDFTextStripper stripper = new PDFTextStripper();
100             PDDocumentInformation info = pdfDocument.getDocumentInformation();
101
102             Map JavaDoc metaInfo = new HashMap JavaDoc();
103             // append document meta data to content
104
String JavaDoc meta;
105             meta = info.getTitle();
106             if (CmsStringUtil.isNotEmpty(meta)) {
107                 metaInfo.put(I_CmsExtractionResult.META_TITLE, meta);
108             }
109             meta = info.getKeywords();
110             if (CmsStringUtil.isNotEmpty(meta)) {
111                 metaInfo.put(I_CmsExtractionResult.META_KEYWORDS, meta);
112             }
113             meta = info.getSubject();
114             if (CmsStringUtil.isNotEmpty(meta)) {
115                 metaInfo.put(I_CmsExtractionResult.META_SUBJECT, meta);
116             }
117             // extract other available meta information
118
meta = info.getAuthor();
119             if (CmsStringUtil.isNotEmpty(meta)) {
120                 metaInfo.put(I_CmsExtractionResult.META_AUTHOR, meta);
121             }
122             meta = info.getCreator();
123             if (CmsStringUtil.isNotEmpty(meta)) {
124                 metaInfo.put(I_CmsExtractionResult.META_CREATOR, meta);
125             }
126             meta = info.getProducer();
127             if (CmsStringUtil.isNotEmpty(meta)) {
128                 metaInfo.put(I_CmsExtractionResult.META_PRODUCER, meta);
129             }
130             if (info.getCreationDate() != null) {
131                 metaInfo.put(I_CmsExtractionResult.META_DATE_CREATED, info.getCreationDate().getTime());
132             }
133             if (info.getModificationDate() != null) {
134                 metaInfo.put(I_CmsExtractionResult.META_DATE_LASTMODIFIED, info.getModificationDate().getTime());
135             }
136
137             // add the main document text
138
String JavaDoc result = stripper.getText(pdfDocument);
139
140             // free some memory
141
stripper = null;
142             info = null;
143             
144             // return the final result
145
return new CmsExtractionResult(result, metaInfo);
146
147         } finally {
148             if (pdfDocument != null) {
149                 pdfDocument.close();
150             }
151         }
152     }
153 }
154
Popular Tags