def generate(self, xml_file, parameters = {}):
        for name, xsl_and_output in self.xsl_and_output_list.items():
            xml = xml_file.replace('.xml', xsl_and_output['output'])
            print(xml_file)
            print(xsl_and_output['xsl'])
            print(xml)
            print(xml + '.err')
            
            f = open(xml_file, 'r')
            c = f.read()
            f.close()

            filename = xml_file
            temp = ''
            #print(c[0:200])
            if '<!DOCTYPE' in c:
            	part1 = c[0:c.find('<!DOCTYPE')]
            	part2 = c[c.find('<!DOCTYPE'):]
            	part2 = part2[part2.find('>')+1:]
                
                import tempfile
                import shutil
                ign, temp = tempfile.mkstemp()

                shutil.copyfile(filename, temp)
                
            	f = open(temp,  'w')
            	f.write(part1+part2)
            	f.close()
                filename = temp

            xml_java.transform(filename, xsl_and_output['xsl'], xml, xml + '.err', parameters)

            if len(temp)>0:
                os.unlink(temp)
    def validate(self, report, img_path, new_name = ''):  
        
        report.write('\nValidating:' + self.xml_filename)

        xml_java.replace_dtd_path(self.xml_filename, self.dtd)
        
        if xml_java.validate(self.xml_filename, self.dtd, self.result_filename, self.err_filename):              
            report.write('transform ' + self.xml_filename + ' '+  self.xsl_prep_report + ' '+  self.xml_report)
            if xml_java.transform(self.xml_filename, self.xsl_prep_report, self.xml_report, self.err_filename):
                # Generate self.report.html
                report.write('transform ' + self.xml_report + ' '+  self.xsl_report + ' '+  self.html_report)
                if xml_java.transform(self.xml_report, self.xsl_report, self.html_report, self.err_filename):
                    os.unlink(self.xml_report)
                    report.write('Validation report:' + self.html_report, True, False, True)
                else:
                    report.write('Unable to create validation report: ' + self.html_report, True, True, True)
            else:
                report.write('Unable to generate xml for report: ' + self.xml_report, True, True, True)

            if ':' in img_path:
                img_path = 'file:///' + img_path 
            if type(self.xsl_preview) == type([]):
                xml_java.tranform_in_steps(self.xml_filename, self.dtd, self.xsl_preview, self.html_preview, {'path_img': img_path +'/', 'css':  self.css, 'new_name': new_name})
            else:
                report.write('transform ' + self.xml_filename + ' '+  self.xsl_preview + ' '+  self.html_preview )
                xml_java.transform(self.xml_filename, self.xsl_preview, self.html_preview, self.err_filename, {'path_img': img_path +'/', 'css':  self.css, 'new_name': new_name})
            if os.path.exists(self.html_preview):
                report.write('Preview ' +  self.html_preview, True, False, True)
            else:
                report.write('Unable to create preview: ' + self.html_preview, True, True, True)
            
            report.write('transform ' + self.xml_filename + ' '+  self.xsl_output + ' '+  self.xml_ouput_filename)

            if xml_java.transform(self.xml_filename, self.xsl_output, self.xml_ouput_filename, self.err_filename):
                report.write('Result ' +  self.xml_ouput_filename)
            else:
                report.write('Unable to create result: ' + self.xml_ouput_filename, True, True, True)
        if os.path.isfile(self.result_filename):
            os.unlink(self.result_filename)
        if os.path.exists(self.xml_ouput_filename):
            report.write('END - OK')
            r = True
        else:
            report.write('END - ERROR')
            if not os.path.exists(self.err_filename):
                f = open(self.err_filename, 'w')
                f.write('error')
                f.close()
            report.write('Errors: read ' + self.err_filename, False, True, True)
            r = False
        return r
    def validate_xml_and_style(self, report):          
        is_valid_xml = False
        
        import time

        xml_java.replace_dtd_path(self.xml_filename, self.dtd)        
        if xml_java.validate(self.xml_filename, self.dtd, self.result_filename, self.err_filename):  
            is_valid_xml = True    

            report.write('Transform ' + self.xml_filename + ' + ' + self.xsl_prep_report +  ' => ' + self.xml_report + ' ' + self.err_filename)        
            t = time.time()
            report.write(str(os.path.exists(self.xml_filename)))        
            report.write(str(os.path.exists(self.xsl_prep_report))   )     
            
            if xml_java.transform(self.xml_filename, self.xsl_prep_report, self.xml_report, self.err_filename):
                t1 = time.time()
                report.write(str(t1 - t))
                report.write('Transform ' + self.xml_report + ' + ' + self.xsl_report +  ' => ' + self.html_report + ' ' + self.err_filename)        
                
                report.write(str(os.path.exists(self.xml_report)))        
                report.write(str(os.path.exists(self.xsl_report)))        
                if xml_java.transform(self.xml_report, self.xsl_report, self.html_report, self.err_filename):
                    t2 = time.time()
                    report.write(str(t2 - t1))
                else:
                    t2 = time.time()
                    report.write('Unable to generate ' + self.html_report )
                    report.write(str(t2 - t1))
            else:
                t1 = time.time()
                report.write('Unable to generate ' + self.xml_report)
                report.write(str(t1 - t))
        
        if os.path.isfile(self.result_filename):
            os.unlink(self.result_filename)

        c = ''
        if os.path.exists(self.html_report):
            f = open(self.html_report, 'r')
            c = f.read()
            f.close()
            

        return (is_valid_xml, ('Total of errors = 0' in c))
    def sgmxml2xml(self, sgmxml_filename, xml_filename, err_filename, report):        
        fix_xml(sgmxml_filename)

        r = False
        temp_err_filename = sgmxml_filename.replace('.sgm.xml', '.res1')
        res_filename = sgmxml_filename.replace('.sgm.xml', '.res')

        if not xml_java.validate(sgmxml_filename, '', res_filename, temp_err_filename):
            shutil.copyfile(sgmxml_filename, sgmxml_filename.replace('.sgm.xml', '.sgm.txt'))

            self.fix_xml_tags(sgmxml_filename)
        

        if xml_java.validate(sgmxml_filename, '', res_filename, err_filename):
            r = xml_java.transform(sgmxml_filename, self.xsl_sgml2xml, xml_filename, err_filename)
        if not r:
            report.write('Unable to create ' + xml_filename + ' from ' + sgmxml_filename, True, True, True)
            report.write(err_filename, True, True, True)
        
        if os.path.isfile(temp_err_filename):
            delete(temp_err_filename)
        if os.path.isfile(res_filename):
            delete(res_filename)
        return r