26sys.path.append(os.path.dirname(__file__))
34if sys.version_info[0] == 3:
41_xml11_illegal_ranges = (
50_xml10_illegal_ranges = _xml11_illegal_ranges + (
59_xml_discouraged_ranges = (
64if sys.maxunicode >= 0x10000:
69 _xml_discouraged_ranges = _xml_discouraged_ranges + (
85 (0x10fffe, 0x10ffff,),
91 return re.compile(
u"[{}]".format(
93 u"{}-{}".format(re.escape(
unichr(lo)), re.escape(
unichr(hi)))
94 for lo, hi
in range_seq
104 return _xml_filtered_chars_re.sub(
u"\ufffd", data)
118 data = data.replace(
"&",
"&")
119 data = data.replace(
"<",
"<")
120 data = data.replace(
">",
">")
121 for chars, entity
in entities.items():
122 data = data.replace(chars, entity)
141 entities[
'\n']=
' '
142 entities[
'\r']=
''
146 data =
'"%s"' % data.replace(
'"',
""")
157 fields = qualifiedName.split(
':', 1)
161 return (
None, fields[0])
164 return nsdict.setdefault(namespace,
"ns" + str(len(nsdict)))
177 previousSibling =
None
190 return self.childNodes
194 return self.childNodes[0]
198 return self.childNodes[-1]
204 if newChild.nodeType
not in self._child_node_types:
206 if newChild.parentNode
is not None:
207 newChild.parentNode.removeChild(newChild)
212 index = self.childNodes.index(refChild)
214 raise xml.dom.NotFoundErr()
215 self.childNodes.insert(index, newChild)
216 newChild.nextSibling = refChild
217 refChild.previousSibling = newChild
219 node = self.childNodes[index-1]
220 node.nextSibling = newChild
221 newChild.previousSibling = node
223 newChild.previousSibling =
None
224 newChild.parentNode = self
231 if newChild.nodeType == self.DOCUMENT_FRAGMENT_NODE:
232 for c
in tuple(newChild.childNodes):
236 if newChild.nodeType
not in self._child_node_types:
238 if newChild.parentNode
is not None:
239 newChild.parentNode.removeChild(newChild)
241 newChild.nextSibling =
None
249 self.childNodes.remove(oldChild)
251 raise xml.dom.NotFoundErr()
252 if oldChild.nextSibling
is not None:
253 oldChild.nextSibling.previousSibling = oldChild.previousSibling
254 if oldChild.previousSibling
is not None:
255 oldChild.previousSibling.nextSibling = oldChild.nextSibling
256 oldChild.nextSibling = oldChild.previousSibling =
None
257 if self.ownerDocument:
258 self.ownerDocument.remove_from_caches(oldChild)
259 oldChild.parentNode =
None
264 for c
in self.childNodes:
270 for c
in self.childNodes:
274defproperty(Node,
"firstChild", doc=
"First child node, or None.")
275defproperty(Node,
"lastChild", doc=
"Last child node, or None.")
279 childNodes = self.childNodes
281 last = childNodes[-1]
282 node.__dict__[
"previousSibling"] = last
283 last.__dict__[
"nextSibling"] = node
284 childNodes.append(node)
285 node.__dict__[
"parentNode"] = self
293 childNodes = EmptyNodeList()
305 raise xml.dom.HierarchyRequestErr(
306 self.tagName +
" nodes cannot have children")
313 raise xml.dom.HierarchyRequestErr(
314 self.tagName +
" nodes do not have children")
318 raise xml.dom.NotFoundErr(
319 self.tagName +
" nodes do not have children")
323 raise xml.dom.HierarchyRequestErr(
324 self.tagName +
" nodes do not have children")
327 nodeType = Node.TEXT_NODE
345 nodeType = Node.CDATA_SECTION_NODE
353 f.write(
'<![CDATA[%s]]>' % self.
data.replace(
']]>',
']]>]]><![CDATA['))
363 nodeType = Node.ELEMENT_NODE
369 _child_node_types = (Node.ELEMENT_NODE,
370 Node.PROCESSING_INSTRUCTION_NODE,
373 Node.CDATA_SECTION_NODE,
374 Node.ENTITY_REFERENCE_NODE)
376 def __init__(self, attributes=None, text=None, cdata=None, qname=None, qattributes=None, check_grammar=True, **args):
377 if qname
is not None:
379 assert(hasattr(self,
'qname'))
387 if cdata
is not None:
391 if allowed_attrs
is not None:
392 allowed_args = [ a[1].lower().replace(
'-',
'')
for a
in allowed_attrs]
396 for attr, value
in attributes.items():
400 for attr, value
in qattributes.items():
402 if allowed_attrs
is not None:
404 for arg
in args.keys():
407 for arg
in args.keys():
409 if not check_grammar:
412 required = grammar.required_attributes.get(self.
qname)
416 raise AttributeError(
"Required attribute missing: %s in <%s>" % (r[1].lower().replace(
'-',
''), self.
tagNametagName))
423 for ns,p
in nsdict.items():
424 if p == prefix:
return ns
431 if namespace
is None: namespace =
""
438 return grammar.allowed_attributes.get(self.
qname)
442 for child
in element.childNodes:
464 if check_grammar
and self.
qname not in grammar.allows_text:
474 if check_grammar
and self.
qname not in grammar.allows_text:
482 if allowed_attrs
is None:
483 if type(attr) == type(()):
484 prefix, localname = attr
487 raise AttributeError(
"Unable to add simple attribute - use (namespace, localpart)")
490 allowed_args = [ a[1].lower().replace(
'-',
'')
for a
in allowed_attrs]
491 if check_grammar
and attr
not in allowed_args:
492 raise AttributeError(
"Attribute %s is not allowed in <%s>" % ( attr, self.
tagNametagName))
493 i = allowed_args.index(attr)
494 self.
removeAttrNS(allowed_attrs[i][0], allowed_attrs[i][1])
504 if attr ==
'parent' and value
is not None:
505 value.addElement(self)
508 if allowed_attrs
is None:
509 if type(attr) == type(()):
510 prefix, localname = attr
513 raise AttributeError(
"Unable to add simple attribute - use (namespace, localpart)")
516 allowed_args = [ a[1].lower().replace(
'-',
'')
for a
in allowed_attrs]
517 if check_grammar
and attr
not in allowed_args:
518 raise AttributeError(
"Attribute %s is not allowed in <%s>" % ( attr, self.
tagNametagName))
519 i = allowed_args.index(attr)
520 self.
setAttrNS(allowed_attrs[i][0], allowed_attrs[i][1], value)
534 self.
attributes[(namespace, localpart)] = c.convert((namespace, localpart), value, self)
547 result = self.
attributes.get((namespace, localpart))
550 (type(namespace), type(namespace), type(namespace) == \
551 type(b
""), type(b
""), type(b
""))
or
552 (type(namespace), type(namespace), type(namespace) == \
553 type(
u""), type(
u""), type(
u""))
565 if allowed_attrs
is None:
566 if type(attr) == type(()):
567 prefix, localname = attr
570 raise AttributeError(
"Unable to get simple attribute - use (namespace, localpart)")
573 allowed_args = [ a[1].lower().replace(
'-',
'')
for a
in allowed_attrs]
574 i = allowed_args.index(attr)
575 return self.
getAttrNS(allowed_attrs[i][0], allowed_attrs[i][1])
580 for namespace, prefix
in self.
namespaces.items():
581 f.write(
u' xmlns:' + prefix +
u'="'+
_sanitize(str(namespace))+
'"')
598 for namespace, prefix
in self.
namespaces.items():
599 f.write(
u' xmlns:' + prefix +
u'="'+
_sanitize(str(namespace))+
u'"')
606 element.toXml(level+1,f)
612 if self.
qname == obj.qname:
613 accumulator.append(self)
615 if e.nodeType == Node.ELEMENT_NODE:
616 accumulator = e._getElementsByObj(obj, accumulator)
621 obj = element(check_grammar=
False)
626 obj = element(check_grammar=
False)
627 return self.
qname == obj.qname
toXml(self, level, f)
Generate XML output of the node.
Mixin that makes childless-ness easy to implement and avoids the complexity of the Node methods that ...
removeChild(self, oldChild)
Raises an error.
replaceChild(self, newChild, oldChild)
Raises an error.
appendChild(self, node)
Raises an error.
insertBefore(self, newChild, refChild)
Raises an error.
Creates a arbitrary element and is intended to be subclassed not used on its own.
__init__(self, attributes=None, text=None, cdata=None, qname=None, qattributes=None, check_grammar=True, **args)
get_nsprefix(self, namespace)
Odfpy maintains a list of known namespaces.
getAttrNS(self, namespace, localpart)
gets an attribute, given a namespace and a key
setAttribute(self, attr, value, check_grammar=True)
Add an attribute to the element This is sort of a convenience method.
getElementsByType(self, element)
Gets elements based on the type, which is function from text.py, draw.py etc.
removeAttrNS(self, namespace, localpart)
_getElementsByObj(self, obj, accumulator)
toXml(self, level, f)
Generate an XML stream out of the tree structure.
addElement(self, element, check_grammar=True)
adds an element to an Element
addCDATA(self, cdata, check_grammar=True)
Adds CDATA to an element Setting check_grammar=False turns off grammar checking.
write_close_tag(self, level, f)
setAttrNS(self, namespace, localpart, value)
Add an attribute to the element In case you need to add an attribute the library doesn't know about t...
_setOwnerDoc(self, element)
isInstanceOf(self, element)
This is a check to see if the object is an instance of a type.
addText(self, text, check_grammar=True)
Adds text to an element Setting check_grammar=False turns off grammar checking.
write_open_tag(self, level, f)
removeAttribute(self, attr, check_grammar=True)
Removes an attribute by name.
get_knownns(self, prefix)
Odfpy maintains a list of known namespaces.
getAttribute(self, attr)
Get an attribute value.
Complains if you add an element to a parent where it is not allowed.
super class for more specific nodes
insertBefore(self, newChild, refChild)
Inserts the node newChild before the existing child node refChild.
removeChild(self, oldChild)
Removes the child node indicated by oldChild from the list of children, and returns it.
hasChildNodes(self)
Tells whether this element has any children; text nodes, subelements whatever.
appendChild(self, newChild)
Adds the node newChild to the end of the list of children of this node.
tagName
The DOM does not clearly specify what to return in this case.
toXml(self, level, f)
Write XML in UTF-8.
_sanitize(data, entities={})
_quoteattr(data, entities={})
Escape and quote an attribute value.
_handle_unrepresentable(data)
_nssplit(qualifiedName)
Split a qualified name into namespace part and local part.
_escape(data, entities={})
Escape &, <, and > in a string of data.
_range_seq_to_re(range_seq)
_append_child(self, node)