PyLucene 3.4.0-1 import
[pylucene.git] / samples / LuceneInAction / lia / handlingtypes / xml / DigesterXMLHandler.py
1 # ====================================================================
2 #   Licensed under the Apache License, Version 2.0 (the "License");
3 #   you may not use this file except in compliance with the License.
4 #   You may obtain a copy of the License at
5 #
6 #       http://www.apache.org/licenses/LICENSE-2.0
7 #
8 #   Unless required by applicable law or agreed to in writing, software
9 #   distributed under the License is distributed on an "AS IS" BASIS,
10 #   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 #   See the License for the specific language governing permissions and
12 #   limitations under the License.
13 # ====================================================================
14
15 import os
16
17 from lucene import Document, Field
18 from lia.handlingtypes.xml.Digester import Digester
19
20
21 class DigesterXMLHandler(object):
22
23     def __init__(self):
24
25         self.digester = digester = Digester()
26
27         digester.addSetProperty("address-book/contact", "type", "type")
28         digester.addSetProperty("address-book/contact/name", "name")
29         digester.addSetProperty("address-book/contact/address", "address")
30         digester.addSetProperty("address-book/contact/city", "city")
31         digester.addSetProperty("address-book/contact/province", "province")
32         digester.addSetProperty("address-book/contact/postalcode", "postalcode")
33         digester.addSetProperty("address-book/contact/country", "country")
34         digester.addSetProperty("address-book/contact/telephone", "telephone")
35
36     def indexFile(self, writer, path):
37
38         try:
39             file = open(path)
40         except IOError, e:
41             raise
42         else:
43             props = self.digester.parse(file)
44             doc = Document()
45             doc.add(Field("type", props['type'],
46                           Field.Store.YES, Field.Index.NOT_ANALYZED))
47             doc.add(Field("name", props['name'],
48                           Field.Store.YES, Field.Index.NOT_ANALYZED))
49             doc.add(Field("address", props['address'],
50                           Field.Store.YES, Field.Index.NOT_ANALYZED))
51             doc.add(Field("city", props['city'],
52                           Field.Store.YES, Field.Index.NOT_ANALYZED))
53             doc.add(Field("province", props['province'],
54                           Field.Store.YES, Field.Index.NOT_ANALYZED))
55             doc.add(Field("postalcode", props['postalcode'],
56                           Field.Store.YES, Field.Index.NOT_ANALYZED))
57             doc.add(Field("country", props['country'],
58                           Field.Store.YES, Field.Index.NOT_ANALYZED))
59             doc.add(Field("telephone", props['telephone'],
60                           Field.Store.YES, Field.Index.NOT_ANALYZED))
61             doc.add(Field("filename", os.path.abspath(path),
62                           Field.Store.YES, Field.Index.NOT_ANALYZED))
63             writer.addDocument(doc)
64             file.close()
65
66             return doc