01: package org.eclipse.pde.internal.ui.editor.toc;
02:
03: import java.io.File;
04: import java.io.FileInputStream;
05: import java.io.IOException;
06: import java.nio.CharBuffer;
07: import java.nio.MappedByteBuffer;
08: import java.nio.channels.FileChannel;
09: import java.nio.charset.Charset;
10: import java.util.regex.Matcher;
11: import java.util.regex.Pattern;
12:
13: public class TocHTMLTitleUtil {
14:
15: private static final String whitespace = "[ \\t\\n\\r\\f\\v]*"; //$NON-NLS-1$
16: private static final String titleTag = "[Tt][Ii][Tt][Ll][Ee]"; //$NON-NLS-1$
17:
18: private static Pattern titlePattern = null;
19:
20: private static void initPattern() {
21: StringBuffer buf = new StringBuffer();
22: buf.append('<');
23: buf.append(whitespace);
24: buf.append(titleTag);
25: buf.append('>');
26: buf.append("(.*?)"); //$NON-NLS-1$
27: buf.append('<');
28: buf.append(whitespace);
29: buf.append('/');
30: buf.append(whitespace);
31: buf.append(titleTag);
32: buf.append('>');
33:
34: titlePattern = Pattern.compile(buf.toString());
35: }
36:
37: public static String findTitle(File f) {
38: if (titlePattern == null) {
39: initPattern();
40: }
41:
42: try {
43: FileChannel fc = new FileInputStream(f).getChannel();
44:
45: MappedByteBuffer bb = fc.map(FileChannel.MapMode.READ_ONLY,
46: 0, fc.size());
47:
48: CharBuffer cb = Charset
49: .forName("8859_1").newDecoder().decode(bb); //$NON-NLS-1$
50:
51: Matcher m = titlePattern.matcher(cb);
52: String title = null;
53: if (m.find()) {
54: title = m.group(1);
55: }
56:
57: return title;
58: } catch (IOException e) {
59: return null;
60: }
61: }
62: }
|