KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > nutch > parse > rtf > RTFParserDelegateImpl


1 /* Copyright (c) 2004 The Nutch Organization. All rights reserved. */
2 /* Use subject to the conditions in http://www.nutch.org/LICENSE.txt. */
3
4 package net.nutch.parse.rtf;
5
6 import com.etranslate.tm.processing.rtf.RTFParserDelegate;
7
8 import java.util.Arrays JavaDoc;
9 import java.util.List JavaDoc;
10 import java.util.Properties JavaDoc;
11
12 /**
13  * A parser delegate for handling rtf events.
14  * @author Andy Hedges
15  */

16 public class RTFParserDelegateImpl implements RTFParserDelegate {
17
18   String JavaDoc tabs = "";
19   Properties JavaDoc metadata = new Properties JavaDoc();
20
21   String JavaDoc[] META_NAMES_TEXT = {"title", "subject", "author", "manager",
22                               "company", "operator", "category", "keywords",
23                               "comment", "doccomm", "hlinkbase"};
24   String JavaDoc[] META_NAMES_DATE = {"creatim", "creatim", "printim", "buptim"};
25
26   String JavaDoc metaName = "";
27   List JavaDoc metaNamesText = Arrays.asList(META_NAMES_TEXT);
28   List JavaDoc metaNamesDate = Arrays.asList(META_NAMES_DATE);
29   boolean isMetaTextValue = false;
30   boolean isMetaDateValue = false;
31   String JavaDoc content = "";
32   boolean justOpenedGroup = false;
33   boolean ignoreMode = false;
34
35   public void text(String JavaDoc text, String JavaDoc style, int context) {
36     justOpenedGroup = false;
37     if (isMetaTextValue && context == IN_INFO) {
38       metadata.setProperty(metaName, text);
39       isMetaTextValue = false;
40     } else if (context == IN_DOCUMENT && !ignoreMode) {
41       content += text;
42     }
43   }
44
45   public void controlSymbol(String JavaDoc controlSymbol, int context) {
46     if("\\*".equals(controlSymbol) && justOpenedGroup){
47       ignoreMode = true;
48     }
49     justOpenedGroup = false;
50   }
51
52   public void controlWord(String JavaDoc controlWord, int value, int context) {
53     justOpenedGroup = false;
54     controlWord = controlWord.substring(1);
55     switch (context) {
56       case IN_INFO:
57         if (metaNamesText.contains(controlWord)) {
58           isMetaTextValue = true;
59           metaName = controlWord;
60         } else if (metaNamesDate.contains(controlWord)) {
61           //TODO: collect up the dates
62
}
63         break;
64       case IN_DOCUMENT:
65         //System.out.println(controlWord);
66
break;
67     }
68   }
69
70   public void openGroup(int depth) {
71     justOpenedGroup = true;
72   }
73
74   public void closeGroup(int depth) {
75     justOpenedGroup = false;
76     ignoreMode = false;
77   }
78
79   public void styleList(List JavaDoc styles) {
80   }
81
82   public void startDocument() {
83   }
84
85   public void endDocument() {
86   }
87
88   public String JavaDoc getText() {
89     return content;
90   }
91
92   public Properties JavaDoc getMetaData() {
93     return metadata;
94   }
95 }
96
Popular Tags