def write(self, xsd_file_path, vocable_file_path, xml_root):
		#print('writing xml to vocable file ...')
		xml_parser = XMLParser()
		if xml_parser.validate_tree(xsd_file_path, xml_root):
			try:
				with open(vocable_file_path, 'w') as file:
					file.write("<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n\n")
					file.write(etree.tostring(xml_root, xml_declaration=False, pretty_print=True, encoding="unicode"))
			except IOError:
				print("Error while writing in log file!")			
		else:
			print("Tree is invalid.")
			raise XMLInvalidException('Invalid XML!')
	def test_write_vocable_file_failing (self):
		print('current working directory:', os.getcwd())
		
		vocable_file_path = self.create_test_vocables_file()
		print('--- using vocable file path:', vocable_file_path)
		
		list_of_words = ['你','好','吗','我','很']
		words_list_file_path = self.create_test_words_file(list_of_words)
		
		xsd_file_path = self.project_directory + 'res/xld-vocables-schema.xsd'
		
		attribute_name = 'chapters'
		attribute_value = 'HSK1-2012'
		words_attribute_name = 'secondLanguageTranslations'
		
		
		xml_parser = XMLParser()
		xld_attribute_adder = XLDAttributeAdder(vocable_file_path, xsd_file_path, words_list_file_path)
		
		xml_root = xml_parser.get_xml_element_tree_root(xsd_file_path, vocable_file_path)
		
		xld_attribute_adder.add_values_to_attribute_of_vocables(attribute_name, attribute_value, words_attribute_name)
		
		# adding invalid tags with texts
		invalid_vocable_element = etree.SubElement(xml_root, 'vocable')
		abc_element = etree.SubElement(invalid_vocable_element, 'abc')
		abc_element.text = 'abc'
		unknowntag_element = etree.SubElement(invalid_vocable_element, 'unknowntag')
		unknowntag_element.text = 'unknowntag'
		
		# the tree should be invalid at this point
		assert xml_parser.validate_tree(xsd_file_path, xml_root) == False, 'Your validation does not work properly.'
		
		# if the xml is not valid anymore when reading the file, the write function might have done something wrong
		try:
			VocableFileWriter.write(VocableFileWriter, xsd_file_path, vocable_file_path, xml_root)
		except XMLInvalidException as exception:
			assert True
		else:
			assert False
		
		
		assert os.path.isfile(vocable_file_path), 'no file exists'
		
		xml_root = xml_parser.get_xml_element_tree_root(xsd_file_path, vocable_file_path)
 def write(self, xsd_file_path, vocable_file_path, xml_root):
     #print('writing xml to vocable file ...')
     xml_parser = XMLParser()
     if xml_parser.validate_tree(xsd_file_path, xml_root):
         try:
             with open(vocable_file_path, 'w') as file:
                 file.write(
                     "<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n\n")
                 file.write(
                     etree.tostring(xml_root,
                                    xml_declaration=False,
                                    pretty_print=True,
                                    encoding="unicode"))
         except IOError:
             print("Error while writing in log file!")
     else:
         print("Tree is invalid.")
         raise XMLInvalidException('Invalid XML!')
    def test_write_vocable_file_failing(self):
        print("current working directory:", os.getcwd())

        vocable_file_path = self.create_test_vocables_file()
        print("--- using vocable file path:", vocable_file_path)

        list_of_words = ["你", "好", "吗", "我", "很"]
        words_list_file_path = self.create_test_words_file(list_of_words)

        xsd_file_path = self.project_directory + "res/xld-vocables-schema.xsd"

        attribute_name = "chapters"
        attribute_value = "HSK1-2012"
        words_attribute_name = "secondLanguageTranslations"

        xml_parser = XMLParser()
        xld_attribute_adder = XLDAttributeAdder(vocable_file_path, xsd_file_path, words_list_file_path)

        xml_root = xml_parser.get_xml_element_tree_root(xsd_file_path, vocable_file_path)

        xld_attribute_adder.add_values_to_attribute_of_vocables(attribute_name, attribute_value, words_attribute_name)

        # adding invalid tags with texts
        invalid_vocable_element = etree.SubElement(xml_root, "vocable")
        abc_element = etree.SubElement(invalid_vocable_element, "abc")
        abc_element.text = "abc"
        unknowntag_element = etree.SubElement(invalid_vocable_element, "unknowntag")
        unknowntag_element.text = "unknowntag"

        # the tree should be invalid at this point
        assert xml_parser.validate_tree(xsd_file_path, xml_root) == False, "Your validation does not work properly."

        # if the xml is not valid anymore when reading the file, the write function might have done something wrong
        try:
            VocableFileWriter.write(VocableFileWriter, xsd_file_path, vocable_file_path, xml_root)
        except XMLInvalidException as exception:
            assert True
        else:
            assert False

        assert os.path.isfile(vocable_file_path), "no file exists"

        xml_root = xml_parser.get_xml_element_tree_root(xsd_file_path, vocable_file_path)