def instantiate(self, context): context.push_string_writer(errors=False) for primitive in self: primitive.instantiate(context) writer = context.pop_writer() name = writer.get_result() prefix, local = splitqname(name) if prefix: namespace = self.namespaces[prefix] else: namespace = None context.namespaces = self.namespaces targets = self.select.evaluate_as_nodeset(context) if not targets: raise XUpdateError(XUpdateError.INVALID_SELECT) for target in targets: parent = target.xml_parent if target.xml_type == tree.attribute.xml_type: parent.xml_attributes[namespace, name] = target.xml_value elif target.xml_type == tree.processing_instruction.xml_type: pi = tree.processing_instruction(name, target.xml_data) parent.xml_replace-child(pi, target) elif target.xml_type == tree.element.xml_type: #FIXME: Use regular constructor. No more DOM factory element = tree.element(namespace, name) # Copy any existing attributes to the newly created element if target.xml_attributes: for (ns, qname), value in target.xml_attributes.iteritems(): element.xml_attributes[ns, qname] = value # Now copy any children as well while target.xml_first_child: element.xml_append(target.xml_first_child) parent.xml_replace(target, element) return
def test_pi_inmutable_properties(self): pi = tree.processing_instruction( u'xml-stylesheet', u'href="classic.xsl" type="text/xml"') inmutable = ['xml_following_sibling', 'xml_preceding_sibling'] for prop in inmutable: self.assertRaises(AttributeError, setattr, pi, "%s" % prop, u'spam') return
def test_pi_inmutable_properties(self): pi = tree.processing_instruction( u'xml-stylesheet', u'href="classic.xsl" type="text/xml"') inmutable = ['xml_following_sibling', 'xml_preceding_sibling'] for prop in inmutable: self.assertRaises(AttributeError, setattr, pi, "%s" % prop, u'spam') return
def test_pi_no_longer(self): pi = tree.processing_instruction( u'xml-stylesheet', u'href="classic.xsl" type="text/xml"') old_methods = ['nodeName', 'nodeValue', 'target', 'data', 'previousSibling','nextSibling'] for old_method in old_methods: self.assertRaises(AttributeError, getattr, pi, "%s" % old_method)
def test_pi_no_longer(self): pi = tree.processing_instruction( u'xml-stylesheet', u'href="classic.xsl" type="text/xml"') old_methods = [ 'nodeName', 'nodeValue', 'target', 'data', 'previousSibling', 'nextSibling' ] for old_method in old_methods: self.assertRaises(AttributeError, getattr, pi, "%s" % old_method)
def test_create_new_doc_pi(self): EXPECTED = '<?xml-stylesheet href="classic.xsl" type="text/xml"?><A a="b">One</A>' doc = tree.entity() doc.xml_append(tree.processing_instruction(u'xml-stylesheet', u'href="classic.xsl" type="text/xml"')) A = tree.element(None, u'A') A.xml_attributes[u'a'] = u'b' A.xml_append(tree.text(u'One')) doc.xml_append(A) self.compare_output(doc, XMLDECL+EXPECTED) return
def test_create_new_doc_pi(self): EXPECTED = '<?xml-stylesheet href="classic.xsl" type="text/xml"?><A a="b">One</A>' doc = tree.entity() doc.xml_append( tree.processing_instruction(u'xml-stylesheet', u'href="classic.xsl" type="text/xml"')) A = tree.element(None, u'A') A.xml_attributes[u'a'] = u'b' A.xml_append(tree.text(u'One')) doc.xml_append(A) self.compare_output(doc, XMLDECL + EXPECTED) return
def test_pi_valid_properties(self): ### REVIEW new properties new_operations = ['xml_data', 'xml_target', 'xml_following_sibling', 'xml_preceding_sibling'] pi = tree.processing_instruction( u'xml-stylesheet', u'href="classic.xsl" type="text/xml"') new_methods = [attr for attr in dir(pi) if attr.startswith('xml')] for operation in new_operations: self.assertTrue(operation in new_methods) return
def test_pi_valid_properties(self): ### REVIEW new properties new_operations = [ 'xml_data', 'xml_target', 'xml_following_sibling', 'xml_preceding_sibling' ] pi = tree.processing_instruction( u'xml-stylesheet', u'href="classic.xsl" type="text/xml"') new_methods = [attr for attr in dir(pi) if attr.startswith('xml')] for operation in new_operations: self.assertTrue(operation in new_methods) return
def test_reading_building(self): doc = tree.entity() doc.xml_append(tree.processing_instruction(u'xml-stylesheet', u'href="classic.xsl" type="text/xml"')) A = tree.element(None, u'A') A.xml_attributes[u'a'] = u'b' A.xml_append(tree.text(u'One')) doc.xml_append(A) doc.xml_append(tree.comment(u"This is easy")) doc2 = amara.parse('docs/whatsnew_doc1.xml') output1 = cStringIO.StringIO() output2 = cStringIO.StringIO() xml_print(doc, stream=output1) xml_print(doc2, stream=output2) self.assertEqual(output1.getvalue(), output2.getvalue()) return
def test_reading_building(self): doc = tree.entity() doc.xml_append( tree.processing_instruction(u'xml-stylesheet', u'href="classic.xsl" type="text/xml"')) A = tree.element(None, u'A') A.xml_attributes[u'a'] = u'b' A.xml_append(tree.text(u'One')) doc.xml_append(A) doc.xml_append(tree.comment(u"This is easy")) doc2 = amara.parse('docs/whatsnew_doc1.xml') output1 = cStringIO.StringIO() output2 = cStringIO.StringIO() xml_print(doc, stream=output1) xml_print(doc2, stream=output2) self.assertEqual(output1.getvalue(), output2.getvalue()) return