1
2
3
4 """
5 This module provides classes for XML representation of data.
6
7 @author: Johannes Schwenk
8 @copyright: 2010, Johannes Schwenk
9 @version: 1.0
10 @date: 2010-09-15
11
12
13 """
14
15 import sys
16
17
18 reload(sys)
19 sys.setdefaultencoding('utf-8')
20
21 from string import Template
22
23 from log import BaseLogger
24
25
26 -class XMLEntry(BaseLogger):
27 """
28 An entry in the XML document that is the CompletionServer's input.
29
30 """
31
32 url = None
33 """
34 @ivar: The url of the entry.
35 @type: string
36
37 """
38
39 title = None
40 """
41 @ivar: The title of the entry as displayed in the search results.
42 @type: string
43
44 """
45
46 content = None
47 """
48 @ivar: The content of the entry.
49 @type: string
50
51 """
52
53 description = None
54 """
55 @ivar: The description for the entry.
56 @type: string
57
58 """
59
60 language = None
61 """
62 @ivar: The language of the entry.
63 @type: string
64
65 """
66
67 created = None
68 """
69 @ivar: The creation date of the entry.
70 @type: string
71
72 """
73
74 modified = None
75 """
76 @ivar: The modification date of the entry.
77 @type: string
78
79 """
80
81 creator = None
82 """
83 @ivar: The author of the entry.
84 @type: string
85
86 """
87
88 portal_type = None
89 """
90 @ivar: The Plone portal_type of the entry.
91 @type: string
92
93 """
94
95 sources = None
96 """
97 @ivar: The sources of the entry.
98 @type: list of strings
99
100 """
101
102 tags = None
103 """
104 @ivar: The tags (aka. keywords) of the entry.
105 @type: list of strings
106
107 """
108
109
110
111 _entry_template = """
112 <entry>
113 <url><![CDATA[${url}]]></url>
114 <title><![CDATA[${title}]]></title>
115 <description><![CDATA[${description}]]></description>
116 <created><![CDATA[${created}]]></created>
117 <modified><![CDATA[${modified}]]></modified>
118 <creator><![CDATA[${creator}]]></creator>
119 <content><![CDATA[${content}]]></content>
120 <language><![CDATA[${language}]]></language>
121 <portal_type><![CDATA[${portal_type}]]></portal_type>${source}${tag}
122 </entry>"""
123 """
124 @cvar: The XML template for an entry.
125 @type: string
126
127 """
128
129 _sources_template = """
130 <source><![CDATA[${source}]]></source>"""
131 """
132 @cvar: Template for a C{<source>} XML tag.
133 @type: string
134
135 """
136
137 _tags_template = """
138 <tag><![CDATA[${tag}]]></tag>"""
139 """
140 @cvar: Template for a C{<tag>} XML tag.
141 @type: string
142
143 """
144
145 - def __init__(self, url, title, content,
146 description=u'', language=u'de', created=u'',
147 creator=u'', portal_type=u'', modified=u'',
148 tags=[],
149 sources=[],):
150 """
151 Initialize the Entry.
152
153 @param url: The URL that references the source for this entry.
154 @param title: The title of this entry as displayed in the search
155 results.
156 @param content: The Entry's content. This is where usually most of the
157 data from the acquisition will be stored.
158 @param description: The Entry's description.
159 @param language: The language of the Entry's content.
160 @param created: Time of creation of the Entry's source.
161 @param creator: The creator of the Entry's source.
162 @param portal_type: The type of the Entry's source. Will be the same
163 as C{portal_type} for plone entries.
164 @param modified: The date of the last modification of the source.
165 @param tags: List of tags (aka. keywords in Plone) that are assigned
166 to the Entry. The words in this list will be weighted more
167 heavily during index creation.
168 @param sources: The names of the sources for this Entry's data.
169
170 @type url: string
171 @type title: string
172 @type content: string
173 @type description: string
174 @type language: string
175 @type created: string or datetime
176 @type creator: string
177 @type portal_type: string
178 @type modified: string or datetime
179 @type tags: list
180 @type sources: list
181
182
183 """
184 self.url = url
185 self.title = title
186 self.content= content
187 self.description = description
188 self.language = language
189 self.created = created
190 self.modified = modified
191 self.creator = creator
192 self.portal_type = portal_type
193 self.sources = sources
194 self.tags = tags
195
196
198 """
199 Return prinatble and readable XML of the Entry.
200
201 @return: Readable and prinatble representation of the entry.
202 @rtype: string
203
204 """
205 src = u''
206 for source in self.sources:
207 src = u'%s%s' % (src,
208 Template(self._sources_template).substitute(source=source))
209 ts = u''
210 for tag in self.tags:
211 ts = u'%s%s' % (ts,
212 Template(self._tags_template).substitute(tag=tag))
213 params = dict(url=self.url, title=self.title, content=self.content,
214 description=self.description, created=self.created,
215 creator=self.creator, portal_type=self.portal_type,
216 language=self.language, tag=ts, source=src,
217 modified=self.modified)
218 return Template(self._entry_template).substitute(params)
219