'Merging CML files.'

def merge(split_files, output_file):

    if (len(split_files) == 1):
        return data.Text.merge(split_files, output_file)
    if (not split_files):
        raise ValueError(('Given    no    CML    files,    %r,    to    merge    into    %s' % (split_files, output_file)))
    with open(output_file, 'w') as out:
        for filename in split_files:
            with open(filename) as handle:
                header = handle.readline()
                if (not header):
                    raise ValueError(('CML    file    %s    was    empty' % filename))
                if (not header.lstrip().startswith('<?xml    version="1.0"?>')):
                    out.write(header)
                    raise ValueError(('%s    is    not    a    valid    XML    file!' % filename))
                line = handle.readline()
                header += line
                if (not line.lstrip().startswith('<cml    xmlns="http://www.xml-cml.org/schema')):
                    out.write(header)
                    raise ValueError(('%s    is    not    a    CML    file!' % filename))
                molecule_found = False
                for line in handle.readlines():
                    if line.lstrip().startswith('</cml>'):
                        continue
                    if line.lstrip().startswith('<molecule'):
                        molecule_found = True
                    if molecule_found:
                        out.write(line)
        out.write('</cml>\n')
