1 | import org.greenstone.gsdl3.util.GSEntityResolver;
|
---|
2 |
|
---|
3 | import org.xml.sax.Attributes;
|
---|
4 | import org.xml.sax.helpers.DefaultHandler;
|
---|
5 | import org.xml.sax.InputSource;
|
---|
6 | import org.xml.sax.SAXException;
|
---|
7 | //import org.xml.sax.SAXParseException;
|
---|
8 | import javax.xml.parsers.SAXParser;
|
---|
9 | import javax.xml.parsers.SAXParserFactory;
|
---|
10 |
|
---|
11 | import org.apache.lucene.document.Document;
|
---|
12 | import org.apache.lucene.document.Field;
|
---|
13 | //import org.apache.lucene.document.DateField;
|
---|
14 | import org.apache.lucene.index.IndexWriter;
|
---|
15 | import org.apache.lucene.analysis.standard.StandardAnalyzer;
|
---|
16 | //import org.apache.lucene.analysis.SimpleAnalyzer;
|
---|
17 |
|
---|
18 | import java.util.Stack;
|
---|
19 | import java.io.FileInputStream;
|
---|
20 | import java.io.File;
|
---|
21 | import java.net.URL;
|
---|
22 |
|
---|
23 |
|
---|
24 |
|
---|
25 | public class Indexer extends DefaultHandler {
|
---|
26 | IndexWriter writer = null;
|
---|
27 | SAXParser sax_parser = null;
|
---|
28 | Stack stack = null;
|
---|
29 | String path = "";
|
---|
30 | String current_node = "";
|
---|
31 | String current_contents = "";
|
---|
32 | Document current_doc = null;
|
---|
33 | String scope = "";
|
---|
34 | protected String file_id = null;
|
---|
35 | private String base_path = null;
|
---|
36 | /** pass in true if want to create a new index, false if want to use the existing one */
|
---|
37 | public Indexer (File index_dir, boolean create) {
|
---|
38 | try {
|
---|
39 | stack = new Stack();
|
---|
40 | SAXParserFactory sax_factory = SAXParserFactory.newInstance();
|
---|
41 | sax_parser = sax_factory.newSAXParser();
|
---|
42 | writer = new IndexWriter(index_dir.getPath(), new StandardAnalyzer(), create);
|
---|
43 | if (create) {
|
---|
44 | writer.optimize();
|
---|
45 | }
|
---|
46 |
|
---|
47 | } catch (Exception e) {
|
---|
48 |
|
---|
49 | }
|
---|
50 | }
|
---|
51 |
|
---|
52 | /** index one document */
|
---|
53 | public void index (String file_id, File file) {
|
---|
54 | this.file_id = file_id;
|
---|
55 | this.path = "";
|
---|
56 | this.base_path = file.getPath();
|
---|
57 | this.base_path = this.base_path.substring(0, this.base_path.lastIndexOf(File.separatorChar));
|
---|
58 | try {
|
---|
59 | sax_parser.parse(new InputSource(new FileInputStream(file)), this);
|
---|
60 | }
|
---|
61 | catch (Exception e) {
|
---|
62 | println("parse error:");
|
---|
63 | e.printStackTrace();
|
---|
64 | }
|
---|
65 | }
|
---|
66 |
|
---|
67 | /** optimise the index */
|
---|
68 | public void finish() {
|
---|
69 | try {
|
---|
70 | writer.optimize();
|
---|
71 | writer.close();
|
---|
72 | } catch (Exception e) {}
|
---|
73 | }
|
---|
74 |
|
---|
75 | protected void println(String s) { System.out.println(s); }
|
---|
76 |
|
---|
77 | public void startDocument() throws SAXException {
|
---|
78 | println("Starting to index " + file_id);
|
---|
79 | }
|
---|
80 | public void endDocument() throws SAXException {
|
---|
81 | println("... indexing finished.");
|
---|
82 | }
|
---|
83 | public void startElement(String uri, String localName, String qName, Attributes atts) throws SAXException {
|
---|
84 | path = appendPathLink(path, qName, atts);
|
---|
85 | if (XMLTagInfo.isScopable(qName)) {
|
---|
86 | scope = qName;
|
---|
87 | }
|
---|
88 | if (XMLTagInfo.isIndexable(qName)) {
|
---|
89 | pushOnStack();
|
---|
90 | current_node = qName;
|
---|
91 | System.out.println("going to index "+qName );
|
---|
92 | String node_id = "";
|
---|
93 | String id = "<"+qName;
|
---|
94 | for (int i=0; i<atts.getLength(); i++) {
|
---|
95 | String name = atts.getQName(i);
|
---|
96 | String value = atts.getValue(i);
|
---|
97 | if (name!=null && value != null) {
|
---|
98 | id += " "+name+"="+value;
|
---|
99 | }
|
---|
100 | if (name.equals("gs3:id")) {
|
---|
101 | node_id = value;
|
---|
102 | }
|
---|
103 | }
|
---|
104 | id += "/>";
|
---|
105 |
|
---|
106 | if (scope.equals(qName)) {
|
---|
107 | current_doc.add(Field.UnIndexed("nodeID", this.file_id+"."+qName));
|
---|
108 | } else {
|
---|
109 | current_doc.add(Field.UnIndexed("nodeID", this.file_id+"."+scope+"."+qName+"."+node_id));
|
---|
110 | }
|
---|
111 | }
|
---|
112 | }
|
---|
113 | public void endElement(String uri, String localName, String qName) throws SAXException {
|
---|
114 | if (XMLTagInfo.isIndexable(qName) && qName.equals(current_node)) {
|
---|
115 | current_doc.add(Field.UnStored("content", current_contents));
|
---|
116 | try {
|
---|
117 | writer.addDocument(current_doc);
|
---|
118 | } catch (java.io.IOException e) {
|
---|
119 | e.printStackTrace();
|
---|
120 | }
|
---|
121 | popOffStack();
|
---|
122 | }
|
---|
123 |
|
---|
124 | path = removePathLink(path);
|
---|
125 | }
|
---|
126 |
|
---|
127 | public void characters(char ch[], int start, int length) throws SAXException {
|
---|
128 | String data = new String(ch, start, length).trim();
|
---|
129 | if (data.length() > 0 ) {
|
---|
130 | current_contents += data;
|
---|
131 | }
|
---|
132 | }
|
---|
133 |
|
---|
134 | protected String appendPathLink(String path, String qName, Attributes atts) {
|
---|
135 |
|
---|
136 | path = path + "/"+qName;
|
---|
137 | if (atts.getLength()>0) {
|
---|
138 | String id = atts.getValue("gs3:id");
|
---|
139 | if (id != null) {
|
---|
140 | path += "[@gs3:id='"+id+"']";
|
---|
141 | }
|
---|
142 | }
|
---|
143 | return path;
|
---|
144 | }
|
---|
145 | protected String removePathLink(String path) {
|
---|
146 |
|
---|
147 | int i=path.lastIndexOf('/');
|
---|
148 | if (i==-1) {
|
---|
149 | path="";
|
---|
150 | } else {
|
---|
151 | path = path.substring(0, i);
|
---|
152 | }
|
---|
153 | return path;
|
---|
154 | }
|
---|
155 | /** these are what we save on the stack */
|
---|
156 | private class MyDocument {
|
---|
157 |
|
---|
158 | public Document doc = null;
|
---|
159 | public String contents = null;
|
---|
160 | public String tagname = "";
|
---|
161 |
|
---|
162 | }
|
---|
163 | protected void pushOnStack() {
|
---|
164 | if (current_doc != null) {
|
---|
165 | MyDocument save = new MyDocument();
|
---|
166 | save.doc = current_doc;
|
---|
167 | save.contents = current_contents;
|
---|
168 | save.tagname = current_node;
|
---|
169 | stack.push(save);
|
---|
170 | }
|
---|
171 | current_doc = new Document();
|
---|
172 | current_contents = "";
|
---|
173 | current_node = "";
|
---|
174 | }
|
---|
175 |
|
---|
176 | protected void popOffStack() {
|
---|
177 | if (!stack.empty()) {
|
---|
178 | MyDocument saved = (MyDocument)stack.pop();
|
---|
179 | current_doc = saved.doc;
|
---|
180 | current_contents = saved.contents;
|
---|
181 | current_node = saved.tagname;
|
---|
182 | } else {
|
---|
183 | current_doc = new Document();
|
---|
184 | current_contents = "";
|
---|
185 | current_node = "";
|
---|
186 | }
|
---|
187 | }
|
---|
188 |
|
---|
189 | public InputSource resolveEntity (String public_id, String system_id) {
|
---|
190 |
|
---|
191 | if (system_id.startsWith("file://")) {
|
---|
192 | return new InputSource(system_id);
|
---|
193 | }
|
---|
194 | if (!system_id.startsWith(File.separator)) {
|
---|
195 | system_id = base_path+File.separatorChar+system_id;
|
---|
196 | }
|
---|
197 | return new InputSource("file://"+system_id);
|
---|
198 | }
|
---|
199 |
|
---|
200 | }
|
---|
201 |
|
---|
202 |
|
---|