libxslt.py 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. # Copyright (c) MetaCommunications, Inc. 2003-2007
  2. #
  3. # Distributed under the Boost Software License, Version 1.0.
  4. # (See accompanying file LICENSE_1_0.txt or copy at
  5. # http://www.boost.org/LICENSE_1_0.txt)
  6. import utils.makedirs
  7. import utils.rename
  8. import os.path
  9. import os
  10. import sys
  11. def xslt_param( path, replace_spaces = 1 ):
  12. path = path.replace( '\\', '/' )
  13. if sys.platform == 'win32' and replace_spaces:
  14. path = path.replace( ' ', '%20' )
  15. return path
  16. def libxslt( log, xml_file, xsl_file, output_file, parameters = None ):
  17. utils.makedirs( os.path.dirname( output_file ) )
  18. if sys.platform == 'win32':
  19. os.chdir( os.path.dirname( xsl_file ) )
  20. transform_command = 'xsltproc'
  21. transform_command = transform_command + ' -o ' + '"%s"' % xslt_param( output_file )
  22. if parameters is not None:
  23. for i in parameters:
  24. if parameters[i]:
  25. parameters[i] = xslt_param( parameters[i] )
  26. transform_command = transform_command + ' --param %s "\'%s\'" ' % ( i, parameters[ i ] )
  27. transform_command = transform_command + ' "%s" ' % xslt_param( xsl_file )
  28. transform_command = transform_command + ' "%s" ' % xslt_param( xml_file )
  29. log( transform_command )
  30. rc = os.system( transform_command )
  31. if rc != 0:
  32. raise Exception( '"%s" failed with return code %d' % ( transform_command, rc ) )
  33. output_file = xslt_param( output_file, 0 )
  34. xlst_output_file = xslt_param( output_file )
  35. if output_file != xlst_output_file and os.path.exists( xlst_output_file ):
  36. utils.rename( log, xlst_output_file, output_file )
粤ICP备19079148号