home *** CD-ROM | disk | FTP | other *** search
- # Source Generated with Decompyle++
- # File: in.pyc (Python 2.6)
-
- import xml.dom as xml
- from xml.dom.minicompat import *
- from namespaces import nsdict
- import grammar
- from attrconverters import AttrConverters
-
- def _escape(data, entities = { }):
- data = data.replace('&', '&')
- data = data.replace('<', '<')
- data = data.replace('>', '>')
- for chars, entity in entities.items():
- data = data.replace(chars, entity)
-
- return data
-
-
- def _quoteattr(data, entities = { }):
- entities['\n'] = '
'
- entities['\r'] = ''
- data = _escape(data, entities)
- if '"' in data:
- if "'" in data:
- data = '"%s"' % data.replace('"', '"')
- else:
- data = "'%s'" % data
- else:
- data = '"%s"' % data
- return data
-
-
- def _nssplit(qualifiedName):
- fields = qualifiedName.split(':', 1)
- if len(fields) == 2:
- return fields
- return (None, fields[0])
-
-
- def _nsassign(namespace):
- return nsdict.setdefault(namespace, 'ns' + str(len(nsdict)))
-
-
- class IllegalChild(StandardError):
- pass
-
-
- class IllegalText(StandardError):
- pass
-
-
- class Node(xml.dom.Node):
- parentNode = None
- nextSibling = None
- previousSibling = None
-
- def hasChildNodes(self):
- if self.childNodes:
- return True
- return False
-
-
- def _get_childNodes(self):
- return self.childNodes
-
-
- def _get_firstChild(self):
- if self.childNodes:
- return self.childNodes[0]
-
-
- def _get_lastChild(self):
- if self.childNodes:
- return self.childNodes[-1]
-
-
- def insertBefore(self, newChild, refChild):
- if newChild.nodeType not in self._child_node_types:
- raise IllegalChild, '%s cannot be child of %s' % (newChild.tagName, self.tagName)
- newChild.nodeType not in self._child_node_types
- if newChild.parentNode is not None:
- newChild.parentNode.removeChild(newChild)
-
- if refChild is None:
- self.appendChild(newChild)
- else:
-
- try:
- index = self.childNodes.index(refChild)
- except ValueError:
- raise xml.dom.NotFoundErr()
-
- self.childNodes.insert(index, newChild)
- newChild.nextSibling = refChild
- refChild.previousSibling = newChild
- if index:
- node = self.childNodes[index - 1]
- node.nextSibling = newChild
- newChild.previousSibling = node
- else:
- newChild.previousSibling = None
- newChild.parentNode = self
- return newChild
-
-
- def appendChild(self, node):
- if node.nodeType == self.DOCUMENT_FRAGMENT_NODE:
- for c in tuple(node.childNodes):
- self.appendChild(c)
-
- return node
- if node.nodeType not in self._child_node_types:
- raise IllegalChild, '<%s> is not allowed in %s' % (node.tagName, self.tagName)
- node.nodeType not in self._child_node_types
- if node.parentNode is not None:
- node.parentNode.removeChild(node)
-
- _append_child(self, node)
- node.nextSibling = None
- return node
-
-
- def removeChild(self, oldChild):
-
- try:
- self.childNodes.remove(oldChild)
- except ValueError:
- raise xml.dom.NotFoundErr()
-
- if oldChild.nextSibling is not None:
- oldChild.nextSibling.previousSibling = oldChild.previousSibling
-
- if oldChild.previousSibling is not None:
- oldChild.previousSibling.nextSibling = oldChild.nextSibling
-
- oldChild.nextSibling = None
- oldChild.previousSibling = None
- if self.ownerDocument:
- self.ownerDocument.clear_caches()
-
- oldChild.parentNode = None
- return oldChild
-
-
- def __str__(self):
- val = []
- for c in self.childNodes:
- val.append(str(c))
-
- return ''.join(val)
-
-
- def __unicode__(self):
- val = []
- for c in self.childNodes:
- val.append(unicode(c))
-
- return u''.join(val)
-
-
- defproperty(Node, 'firstChild', doc = 'First child node, or None.')
- defproperty(Node, 'lastChild', doc = 'Last child node, or None.')
-
- def _append_child(self, node):
- childNodes = self.childNodes
- if childNodes:
- last = childNodes[-1]
- node.__dict__['previousSibling'] = last
- last.__dict__['nextSibling'] = node
-
- childNodes.append(node)
- node.__dict__['parentNode'] = self
-
-
- class Childless:
- attributes = None
- childNodes = EmptyNodeList()
- firstChild = None
- lastChild = None
-
- def _get_firstChild(self):
- pass
-
-
- def _get_lastChild(self):
- pass
-
-
- def appendChild(self, node):
- raise xml.dom.HierarchyRequestErr(self.tagName + ' nodes cannot have children')
-
-
- def hasChildNodes(self):
- return False
-
-
- def insertBefore(self, newChild, refChild):
- raise xml.dom.HierarchyRequestErr(self.tagName + ' nodes do not have children')
-
-
- def removeChild(self, oldChild):
- raise xml.dom.NotFoundErr(self.tagName + ' nodes do not have children')
-
-
- def replaceChild(self, newChild, oldChild):
- raise xml.dom.HierarchyRequestErr(self.tagName + ' nodes do not have children')
-
-
-
- class Text(Childless, Node):
- nodeType = Node.TEXT_NODE
- tagName = 'Text'
-
- def __init__(self, data):
- self.data = data
-
-
- def __str__(self):
- return self.data.encode()
-
-
- def __unicode__(self):
- return self.data
-
-
- def toXml(self, level, f):
- if self.data:
- f.write(_escape(unicode(self.data).encode('utf-8')))
-
-
-
-
- class CDATASection(Childless, Text):
- nodeType = Node.CDATA_SECTION_NODE
-
- def toXml(self, level, f):
- if self.data:
- f.write('<![CDATA[%s]]>' % self.data)
-
-
-
-
- class Element(Node):
- nodeType = Node.ELEMENT_NODE
- namespaces = { }
- _child_node_types = (Node.ELEMENT_NODE, Node.PROCESSING_INSTRUCTION_NODE, Node.COMMENT_NODE, Node.TEXT_NODE, Node.CDATA_SECTION_NODE, Node.ENTITY_REFERENCE_NODE)
-
- def __init__(self, attributes = None, text = None, cdata = None, qname = None, qattributes = None, check_grammar = True, **args):
- if qname is not None:
- self.qname = qname
-
- self.ownerDocument = None
- self.childNodes = []
- self.allowed_children = grammar.allowed_children.get(self.qname)
- prefix = self.get_nsprefix(self.qname[0])
- self.tagName = prefix + ':' + self.qname[1]
- if text is not None:
- self.addText(text)
-
- if cdata is not None:
- self.addCDATA(cdata)
-
- allowed_attrs = self.allowed_attributes()
- self.attributes = { }
- if attributes:
- for attr, value in attributes.items():
- self.setAttribute(attr, value)
-
-
- if qattributes:
- for attr, value in qattributes.items():
- self.setAttrNS(attr[0], attr[1], value)
-
-
- if allowed_attrs is not None:
- for arg in args.keys():
- self.setAttribute(arg, args[arg])
-
- else:
- for arg in args.keys():
- self.attributes[arg] = args[arg]
-
- if not check_grammar:
- return None
- required = grammar.required_attributes.get(self.qname)
- if required:
- for r in required:
- if self.getAttrNS(r[0], r[1]) is None:
- raise AttributeError, 'Required attribute missing: %s in <%s>' % (r[1].lower().replace('-', ''), self.tagName)
- self.getAttrNS(r[0], r[1]) is None
-
-
-
-
- def get_nsprefix(self, namespace):
- if namespace is None:
- namespace = ''
-
- prefix = _nsassign(namespace)
- if not self.namespaces.has_key(namespace):
- self.namespaces[namespace] = prefix
-
- return prefix
-
-
- def allowed_attributes(self):
- return grammar.allowed_attributes.get(self.qname)
-
-
- def _setOwnerDoc(self, element):
- element.ownerDocument = self.ownerDocument
- for child in element.childNodes:
- self._setOwnerDoc(child)
-
-
-
- def addElement(self, element, check_grammar = True):
- if check_grammar and self.allowed_children is not None:
- if element.qname not in self.allowed_children:
- raise IllegalChild, '<%s> is not allowed in <%s>' % (element.tagName, self.tagName)
- element.qname not in self.allowed_children
-
- self.appendChild(element)
- self._setOwnerDoc(element)
- if self.ownerDocument:
- self.ownerDocument.rebuild_caches(element)
-
-
-
- def addText(self, text, check_grammar = True):
- if check_grammar and self.qname not in grammar.allows_text:
- raise IllegalText, 'The <%s> element does not allow text' % self.tagName
- self.qname not in grammar.allows_text
- if text != '':
- self.appendChild(Text(text))
-
-
-
- def addCDATA(self, cdata, check_grammar = True):
- if check_grammar and self.qname not in grammar.allows_text:
- raise IllegalText, 'The <%s> element does not allow text' % self.tagName
- self.qname not in grammar.allows_text
- self.appendChild(CDATASection(cdata))
-
-
- def removeAttribute(self, attr, check_grammar = True):
- allowed_attrs = self.allowed_attributes()
- if allowed_attrs is None:
- if type(attr) == type(()):
- (prefix, localname) = attr
- self.removeAttrNS(prefix, localname)
- else:
- raise AttributeError, 'Unable to add simple attribute - use (namespace, localpart)'
- type(attr) == type(())
- allowed_args = [ a[1].lower().replace('-', '') for a in allowed_attrs ]
- if check_grammar and attr not in allowed_args:
- raise AttributeError, 'Attribute %s is not allowed in <%s>' % (attr, self.tagName)
- attr not in allowed_args
- i = allowed_args.index(attr)
- self.removeAttrNS(allowed_attrs[i][0], allowed_attrs[i][1])
-
-
- def setAttribute(self, attr, value, check_grammar = True):
- allowed_attrs = self.allowed_attributes()
- if allowed_attrs is None:
- if type(attr) == type(()):
- (prefix, localname) = attr
- self.setAttrNS(prefix, localname, value)
- else:
- raise AttributeError, 'Unable to add simple attribute - use (namespace, localpart)'
- type(attr) == type(())
- allowed_args = [ a[1].lower().replace('-', '') for a in allowed_attrs ]
- if check_grammar and attr not in allowed_args:
- raise AttributeError, 'Attribute %s is not allowed in <%s>' % (attr, self.tagName)
- attr not in allowed_args
- i = allowed_args.index(attr)
- self.setAttrNS(allowed_attrs[i][0], allowed_attrs[i][1], value)
-
-
- def setAttrNS(self, namespace, localpart, value):
- allowed_attrs = self.allowed_attributes()
- prefix = self.get_nsprefix(namespace)
- c = AttrConverters()
- self.attributes[prefix + ':' + localpart] = c.convert((namespace, localpart), value, self.qname)
-
-
- def getAttrNS(self, namespace, localpart):
- prefix = self.get_nsprefix(namespace)
- return self.attributes.get(prefix + ':' + localpart)
-
-
- def removeAttrNS(self, namespace, localpart):
- prefix = self.get_nsprefix(namespace)
- del self.attributes[prefix + ':' + localpart]
-
-
- def getAttribute(self, attr):
- allowed_attrs = self.allowed_attributes()
- if allowed_attrs is None:
- if type(attr) == type(()):
- (prefix, localname) = attr
- return self.getAttrNS(prefix, localname)
- raise AttributeError, 'Unable to get simple attribute - use (namespace, localpart)'
- allowed_attrs is None
- allowed_args = [ a[1].lower().replace('-', '') for a in allowed_attrs ]
- i = allowed_args.index(attr)
- return self.getAttrNS(allowed_attrs[i][0], allowed_attrs[i][1])
-
-
- def write_open_tag(self, level, f):
- f.write('<' + self.tagName)
- if level == 0:
- for namespace, prefix in self.namespaces.items():
- f.write(' xmlns:' + prefix + '="' + _escape(str(namespace)) + '"')
-
-
- for attkey in self.attributes.keys():
- f.write(' ' + _escape(str(attkey)) + '=' + _quoteattr(unicode(self.attributes[attkey]).encode('utf-8')))
-
- f.write('>')
-
-
- def write_close_tag(self, level, f):
- f.write('</' + self.tagName + '>')
-
-
- def toXml(self, level, f):
- f.write('<' + self.tagName)
- if level == 0:
- for namespace, prefix in self.namespaces.items():
- f.write(' xmlns:' + prefix + '="' + _escape(str(namespace)) + '"')
-
-
- for attkey in self.attributes.keys():
- f.write(' ' + _escape(str(attkey)) + '=' + _quoteattr(unicode(self.attributes[attkey]).encode('utf-8')))
-
- if self.childNodes:
- f.write('>')
- for element in self.childNodes:
- element.toXml(level + 1, f)
-
- f.write('</' + self.tagName + '>')
- else:
- f.write('/>')
-
-
- def _getElementsByObj(self, obj, accumulator):
- if self.qname == obj.qname:
- accumulator.append(self)
-
- for e in self.childNodes:
- if e.nodeType == Node.ELEMENT_NODE:
- accumulator = e._getElementsByObj(obj, accumulator)
- continue
-
- return accumulator
-
-
- def getElementsByType(self, element):
- obj = element(check_grammar = False)
- return self._getElementsByObj(obj, [])
-
-
- def isInstanceOf(self, element):
- obj = element(check_grammar = False)
- return self.qname == obj.qname
-
-
-