1 """
2 Parser for the entity layer in KAF/NAF
3 """
4
5
6 from lxml import etree
7 from lxml.objectify import dump
8 from references_data import *
9 from external_references_data import *
10
11
13 """
14 This class encapsulates the entity element in KAF/NAF
15 """
16 - def __init__(self,node=None,type='NAF'):
17 """
18 Constructor of the object
19 @type node: xml Element or None (to create and empty one)
20 @param node: this is the node of the element. If it is None it will create a new object
21 @type type: string
22 @param type: the type of the object (KAF or NAF)
23 """
24 self.type = type
25 if node is None:
26 self.node = etree.Element('entity')
27 else:
28 self.node = node
29
38
40 """
41 Returns the node of the element
42 @rtype: xml Element
43 @return: the node of the element
44 """
45 return self.node
46
48 """
49 Returns the identifier of the element
50 @rtype: string
51 @return: the identifier of the entity
52 """
53 if self.type == 'NAF':
54 return self.node.get('id')
55 elif self.type == 'KAF':
56 return self.node.get('eid')
57
59 """
60 Sets the identifier for the entity
61 @type i: string
62 @param i: entity identifier
63 """
64 if self.type == 'NAF':
65 self.node.set('id',i)
66 elif self.type == 'KAF':
67 self.node.set('eid',i)
68
70 """
71 Returns the type of the entity
72 @rtype: string
73 @return: the type of the entity
74 """
75 return self.node.get('type')
76
78 self.node.set('type',t)
79
81 """
82 Returns the references of the entity
83 @rtype: L{Creferences}
84 @return: list of references (iterator)
85 """
86 for ref_node in self.node.findall('references'):
87 yield Creferences(ref_node)
88
90 """
91 Adds a reference to the layer
92 @type ref: L{Creferences}
93 @param ref: a reference object
94 """
95 self.node.append(ref.get_node())
96
98 """
99 Adds an external reference to the entity
100 @param ext_ref: the external reference object
101 @type ext_ref: L{CexternalReference}
102 """
103
104 node_ext_refs = self.node.find('externalReferences')
105 ext_refs = None
106 if node_ext_refs == None:
107 ext_refs = CexternalReferences()
108 self.node.append(ext_refs.get_node())
109 else:
110 ext_refs = CexternalReferences(node_ext_refs)
111
112 ext_refs.add_external_reference(ext_ref)
113
114
116 """
117 Returns the external references of the element
118 @rtype: L{CexternalReference}
119 @return: the external references (iterator)
120 """
121 node = self.node.find('externalReferences')
122 if node is not None:
123 ext_refs = CexternalReferences(node)
124 for ext_ref in ext_refs:
125 yield ext_ref
126
128 """
129 Returns the source of the entity
130 @rtype: string
131 @return: the source of the entity
132 """
133 return self.node.get('source')
134
136 self.node.set('source',t)
137
139 """
140 This class encapsulates the entity layer in KAF/NAF
141 """
142 - def __init__(self,node=None,type='NAF'):
143 """
144 Constructor of the object
145 @type node: xml Element or None (to create and empty one)
146 @param node: this is the node of the element. If it is None it will create a new object
147 @type type: string
148 @param type: the type of the object (KAF or NAF)
149 """
150 self.type = type
151 self.map_entity_id_to_node = {}
152 if node is None:
153 self.node = etree.Element('entities')
154 else:
155 self.node = node
156 for entity_obj in self:
157 self.map_entity_id_to_node[entity_obj.get_id()] = entity_obj.get_node()
158
159
162
164 """
165 Returns the entity object for the given entity identifier
166 @type entity_id: string
167 @param entity_id: the token identifier
168 @rtype: L{Centity}
169 @return: the entity object
170 """
171 entity_node = self.map_entity_id_to_node.get(entity_id)
172 if entity_node is not None:
173 return Centity(node=entity_node,type=self.type)
174 else:
175 for entity_node in self.__get_entity_nodes():
176 if self.type == 'NAF':
177 label_id = 'id'
178 elif self.type == 'KAF':
179 label_id = 'eid'
180 if entity_node.get(label_id) == entity_id:
181 return Centity(node=entity_node, type=self.type)
182 return None
183
185 """
186 Adds an external reference to a entity specified by the entity identifier
187 @param entity_id: the entity identifier
188 @type entity_id: string
189 @param ext_ref: the external reference
190 @type ext_ref: L{CexternalReference}
191 """
192 node_entity = self.map_entity_id_to_node.get(entity_id)
193 if node_entity is not None:
194 entity = Centity(node_entity,self.type)
195 entity.add_external_reference(ext_ref)
196 else:
197 print>>sys.stderr,'Trying to add a reference to the entity',entity_id,'but can not be found in this file'
198
199
201 """
202 Returns the node of the element
203 @rtype: xml Element
204 @return: the node of the element
205 """
206 return self.node
207
209 """
210 Converts the layer from KAF to NAF
211 """
212 if self.type == 'NAF':
213 for node in self.__get_entity_nodes():
214 node.set('eid',node.get('id'))
215 del node.attrib['id']
216
218 """
219 Converts the layer from NAF to KAF
220 """
221 if self.type == 'KAF':
222 for node in self.__get_entity_nodes():
223 node.set('id',node.get('eid'))
224 del node.attrib['eid']
225
227 for ent_node in self.node.findall('entity'):
228 yield ent_node
229
231 """
232 Iterator that returns the entities of the layer
233 @rtype: L{Centity}
234 @return: list of entities (iterator)
235 """
236 for ent_node in self.__get_entity_nodes():
237 yield Centity(ent_node,self.type)
238
239
241 return dump(self.node)
242