KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * File : $Source: /usr/local/cvs/opencms/src/org/opencms/search/extractors/CmsExtractorRtf.java,v $
3  * Date : $Date: 2005/06/23 11:11:28 $
4  * Version: $Revision: 1.7 $
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 java.io.StringReader JavaDoc;
35 import java.util.regex.Pattern JavaDoc;
36
37 import javax.swing.text.Document JavaDoc;
38 import javax.swing.text.rtf.RTFEditorKit JavaDoc;
39
40 /**
41  * Extracts the text form a RTF document.<p>
42  *
43  * @author Alexander Kandzior
44  *
45  * @version $Revision: 1.7 $
46  *
47  * @since 6.0.0
48  */

49 public final class CmsExtractorRtf extends A_CmsTextExtractor {
50
51     /** Static member instance of the extractor. */
52     private static final CmsExtractorRtf INSTANCE = new CmsExtractorRtf();
53
54     /** Pattern used to remove {\*\ts...} RTF keywords, which cause NPE in Java 1.4. */
55     private static final Pattern JavaDoc TS_REMOVE_PATTERN = Pattern.compile("\\{\\\\\\*\\\\ts[^\\}]*\\}", Pattern.DOTALL);
56
57     /**
58      * Hide the public constructor.<p>
59      */

60     private CmsExtractorRtf() {
61
62         // noop
63
}
64
65     /**
66      * Returns an instance of this text extractor.<p>
67      *
68      * @return an instance of this text extractor
69      */

70     public static I_CmsTextExtractor getExtractor() {
71
72         return INSTANCE;
73     }
74
75     /**
76      * @see org.opencms.search.extractors.I_CmsTextExtractor#extractText(byte[], java.lang.String)
77      */

78     public I_CmsExtractionResult extractText(byte[] content, String JavaDoc encoding) throws Exception JavaDoc {
79
80         // RTF always uses ASCII, so we don't need to care about the encoding
81
String JavaDoc input = new String JavaDoc(content);
82
83         // workaround to remove RTF keywords that cause a NPE in Java 1.4
84
// this is a known bug in Java 1.4 that was fixed in 1.5
85
// please see http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=5042109 for the official bug report
86
input = TS_REMOVE_PATTERN.matcher(input).replaceAll("");
87
88         // use build in RTF parser from Swing API
89
RTFEditorKit JavaDoc rtfEditor = new RTFEditorKit JavaDoc();
90         Document JavaDoc doc = rtfEditor.createDefaultDocument();
91         rtfEditor.read(new StringReader JavaDoc(input), doc, 0);
92
93         String JavaDoc result = doc.getText(0, doc.getLength());
94         result = removeControlChars(result);
95
96         return new CmsExtractionResult(result);
97     }
98
99     // this would be the better implementation if the bug mentioned above did not exist in 1.4
100
//
101
// /**
102
// * @see org.opencms.search.extractors.I_CmsTextExtractor#extractText(java.io.InputStream, java.lang.String)
103
// */
104
// public I_CmsExtractionResult extractText(InputStream in, String encoding) throws Exception {
105
//
106
// // use build in RTF parser from Swing API
107
// RTFEditorKit rtfEditor = new RTFEditorKit();
108
// Document doc = rtfEditor.createDefaultDocument();
109
// rtfEditor.read(in, doc, 0);
110
//
111
// String result = doc.getText(0, doc.getLength());
112
// result = removeControlChars(result);
113
//
114
// return new CmsExtractionResult(result);
115
// }
116

117 }
Popular Tags