1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870
|
#!/usr/bin/python
# (C) Copyright 2003-2006 Nuxeo SAS <http://nuxeo.com>
# (C) Copyright 2002 Eric Bellot <ebellot@netcourrier.com>
#
# Authors:
# M.-A. Darche (Nuxeo)
# Ruslan Spivak (Nuxeo)
# Eric Bellot <ebellot@netcourrier.com>
# Laurent Godard (lgodard@indesko.com)
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2 as published
# by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
# 02111-1307, USA.
#
# See ``COPYING`` for more information
#
# $Id: ooo2dbk 50612 2006-12-15 11:15:52Z madarche $
import zipfile
import os, os.path, sys
from string import join, split, find
import codecs
from xml.dom import minidom
import time, base64
import re
import xml.sax
import shutil
import locale
from optparse import OptionParser
VERSION = '2.0'
CONF_FILE_NAME = 'ooo2dbk.xml'
# OpenOffice.org canonical XML files
OOO_META_FILE_NAME = 'meta.xml'
OOO_STYLES_FILE_NAME = 'styles.xml'
OOO_CONTENT_FILE_NAME = 'content.xml'
DOCBOOK_FILE_SUFFIX = '.docb.xml'
# ZIP entries paths are stored in "code page 437" encoding (cp437).
# One cannot use UTF-8 for the ZIP entries paths.
# Read [ 878120 ] Zipfile archive name can't be unicode
# https://sourceforge.net/tracker/?func=detail&atid=105470&aid=878120&group_id=5470
ZIP_FILE_ENCODING = 'cp437'
# Initialization (attempt to remove some global statements)
oooVersion = 'ooo1'
docbookXSL = None
verbose = True
zipfile_target = None
docbook_top_element = 'book'
process_ole_objects = False
def execArgs():
"""Analyze command line arguments.
"""
usage = "usage: %prog [options] openoffice.org-file"
parser = OptionParser(usage=usage, version="%prog " + VERSION)
parser.add_option('-z', '--zipfile',
action='store',
dest='zipfile_target',
type='string',
metavar='FILE',
default=None,
help="Use FILE as the filename for "
"the generated ZIP archive")
parser.add_option('-d', '--dbkfile',
action='store',
dest='dbkfile',
type='string',
metavar='FILE',
default=None,
help="Use FILE as the filename for "
"the generated DocBook XML file. "
"This option has no effect if the "
"-z/--zipfile option is used.")
parser.add_option('-b', '--book',
action='store_true',
dest='book',
default=False,
help="Produce a DocBook XML book. "
"This is the default.")
parser.add_option('-a', '--article',
action='store_true',
dest='article',
default=False,
help="Produce a DocBook XML article.")
parser.add_option('--ole',
action='store_true',
dest='ole',
default=False,
help="Include potential OLE objects as images in the "
"resulting DocBook XML document. This option needs that "
"a listening OpenOffice.org instance be running.")
parser.add_option('-c', '--config',
action='store',
dest='config',
type='string',
metavar='FILE',
default=None,
help="Use FILE as the file path for the program configuration file. "
"Defaults to the global /etc/%s configuration file or to the "
"%s configuration file in the ooo2dbk executable directory."
% (CONF_FILE_NAME, CONF_FILE_NAME))
parser.add_option('-x', '--xslt',
action='store',
dest='xslt',
type='string',
metavar='FILE',
default=None,
help="Use FILE as the file path for the XSLT stylesheet. "
"Defaults to the ooo2dbk ooo2dbk.xsl stylesheet.")
parser.add_option('-m', '--cmdxslt',
action='store',
dest='cmdxslt',
type='string',
metavar='NAME',
default=None,
help="Use command NAME as the XSLT processor. "
"Available command names are defined in the "
"ooo2dbk configuration file. "
"Defaults to xsltproc.")
parser.add_option('-f', '--flatxml',
action='store_false',
dest='flatxml',
default=True,
help="Preserve the intermediate OpenOffice.org "
"XML file (global.xml)")
parser.add_option('-v', '--verbose',
action='store_true',
dest='verbose',
default=False,
help="Print additional information to stdout "
"when running conversion")
if len(sys.argv) < 2:
parser.print_help()
# Command line syntax errors return the error code "2"
sys.exit(2)
(options, args) = parser.parse_args()
if len(args) != 1:
parser.error("incorrect number of arguments")
ooo_file_path = args[0]
if options.book:
docbook_top_element = 'book'
elif options.article:
docbook_top_element = 'article'
else:
docbook_top_element = 'book'
xslParams = []
xslParams.append('topElementName')
xslParams.append(docbook_top_element)
process_ole_objects = options.ole
convert(ooo_file_path,
conf_file_path=options.config,
command=options.cmdxslt,
xslt_file_path=options.xslt,
xslParams=xslParams,
docbook_file_path=options.dbkfile,
deltemp=options.flatxml,
verbose=options.verbose,
zipfile_target=options.zipfile_target,
docbook_top_element=docbook_top_element,
process_ole_objects=options.ole,
docbookXSL=None,
)
return
# ---------
# Utilities
# ---------
def getModulePath():
"""Return the path of the directory in which the ooo2dbk executable resides.
"""
moduleFullname = os.path.abspath(execArgs.func_code.co_filename)
modulePath = os.path.split(moduleFullname)[0]
return modulePath
def fileExist(file):
if file != '':
return os.path.isfile(file)
else:
print "Bad filename: ", file
sys.exit(1)
def writeFile(file,strContent):
b = open(file, 'w')
b.write(strContent)
b.close()
def verifSys():
"""Current system identification.
"""
global preferred_encoding
preferred_encoding = locale.getpreferredencoding()
global currentSys
if sys.platform == 'win32':
currentSys = 'windows'
else:
currentSys = 'unix'
def verifPath(path):
"""Syntax path verification.
"""
global currentSys
if currentSys == 'windows':
modPathWin = re.compile(r"^(([a-zA-Z]:\\)?|(\.\.\\)*)([^\?:/\*\"<>\|]+[^\s\?:/\*\"<>\|]\\)*[^\?:/\*\"<>\|]+(\.[a-zA-Z0-9]+)?$")
verifPath = modPathWin.match(path)
if currentSys == 'unix':
modPathUnix = re.compile(r"^(~|(\.\./)*)?([^\\\?:\*\"<>\|]+[^\\\s\?:\*\"<>\|]/)*[^\\\?:\*\"<>\|]+(\.[a-zA-Z0-9]+)*$")
verifPath = modPathUnix.match(path)
if verifPath == None:
print "Bad path :\n", path
sys.exit(1)
else:
return verifPath.group()
def createDirectory(path):
drive = ''
if currentSys == 'windows' and os.path.isabs(path):
dualWin = os.path.splitdrive(path)
drive = dualWin[0]
path = dualWin[1][1:]
listDir = split(path, os.sep)
i = 1
while i <= len(listDir):
testPath = join(listDir[:i], os.sep)
if drive != '':
testPath = join([drive, testPath], os.sep)
if os.path.isdir(testPath) == 0:
os.mkdir(testPath)
i += 1
# --------
# SETTINGS
# --------
def getXSLfile(oooVersion):
# Using the XSLT stylesheets specified in the config.xml file
stylesheet_file_name = getConfigValue('xslt-stylesheet',
'stylesheetPath',
name=oooVersion)
stylesheet_file_name = verifPath(stylesheet_file_name)
if stylesheet_file_name == None:
print "Bad filename %s for 'xslt-stylesheet' %s in 'config.xml'" % (
stylesheet_file_name,
oooVersion )
xsltfile = os.path.join(getModulePath(), stylesheet_file_name)
return xsltfile
def setConfFileSettings(conf_file_path=None):
global configXML
global configElts, imgRelDir, imgRootName
global oooserver_host, oooserver_port
global ole_img_format, ole2img_script_path, ooopython_path
# Configuration file
# look at options.config priorities for parameters file
# 1- c file.xml
# 2- /etc/ooo2dbk.xml
# 3- ooo2dbk.xml in the cuurent directory
if conf_file_path is not None:
configXML = conf_file_path
else:
conf_file_path_global = os.path.join('/etc', CONF_FILE_NAME)
if os.path.isfile(conf_file_path_global):
configXML = conf_file_path_global
else:
configXML = os.path.join(getModulePath(), CONF_FILE_NAME)
configParse = minidom.parse(configXML)
configDocElt = configParse.documentElement
eltsParse = configDocElt.childNodes
configElts = []
for node in eltsParse:
if node.nodeType == node.ELEMENT_NODE:
lenAtt = node.attributes.length
dictAtt = {}
i = 0
while i < lenAtt:
att = node.attributes.item(i)
dictAtt[att.name] = att.value
i += 1
tupleElt = (node.nodeName, dictAtt)
configElts.append(tupleElt)
# Images relative directory
imgRelDir = getConfigValue('images', 'imagesRelativeDirectory')
verifPathIRD = re.match(r"^[a-zA-Z0-9]+$", imgRelDir)
if verifPathIRD == None:
msg = ("Only one depth relative directory (no '" + os.sep + "') "
"and only alphanum chars for 'imagesRelativeDirectory' in 'config.xml'\n"
"Actual name is : '" + imgRelDir + "'")
print msg
sys.exit(1)
# Images root name
imgRootName = getConfigValue('images', 'imageNameRoot')
verifPathIR = re.match(r"^[a-zA-Z0-9]+$", imgRootName)
if verifPathIR == None:
print "Only alphanum chars for 'imageNameRoot' in 'config.xml'"
print "Actual name is :", imgRootName
sys.exit(1)
oooserver_host = getConfigValue('oooserver', 'host')
oooserver_port = getConfigValue('oooserver', 'port')
ole_img_format = getConfigValue('ole', 'imgFormat')
ole2img_script_path = getConfigValue('ole', 'scriptPath')
ooopython_path = getConfigValue('ooopython', 'path')
def getConfigValue(element, attribute, name=''):
"""
Return from the config.xml file the value of the specified attribute
('command', 'param-syntax', etc.) for the specified element type
'xslt-command', 'xslt-stylesheet', etc.) with its 'name' attribute having
the name value.
"""
global configElts
value = ''
i = len(configElts) - 1
while i >= 0 :
elt = configElts[i]
if name != '':
if elt[0] == element and elt[1]['name'] == name:
value = elt[1][attribute]
else:
# We take the default element
if elt[0] == element:
value = elt[1][attribute]
i = i - 1
if value != '':
return value
else:
if name != '':
print ("There isn't any value for this parameter. "
"There should be an error in your config.xml.")
sys.exit(1)
def setUserSettings(ooofile, docbook, command, imagesrew, deltemp, dtd,
xslt_file_path, xslParams, verbose):
global docOOoSXW, docbookXML, globalXML
global imgRelDir, imgAbsDir, rewriteImg
global XSLCmdTemplate, dtdPublic, dtdSystem, XSLParams
# OpenOffice.org filename
ooofile = verifPath(ooofile)
if fileExist(ooofile) == 0:
errorMsg = ("\n>> ERROR : Incorrect OpenOffice.org file : \n>> "
+ ooofile + "\n")
print errorMsg
sys.exit(1)
else:
docOOoSXW = ooofile
# DocBook filename
if docbook is not None:
docbook = verifPath(docbook)
path = os.path.split(docbook)[0]
docbookXML = docbook
else:
OOoSplit = os.path.split(docOOoSXW)
#path = OOoSplit[0]
# This line will result producing subobjects(images) and
# OOo & DocBook xml under directory where ooo2dbk.py resides
#path = os.path.abspath(os.path.dirname(__file__))
# This line will result producing subobjects(images) and
# OOo & DocBook xml under directory from which ooo2dbk.py was launched
path = os.getcwd()
rootName = os.path.splitext(OOoSplit[1])[0]
docbookXML = os.path.join(path, rootName) + DOCBOOK_FILE_SUFFIX
# Replace spaces in Writer document name with '_'
docbookXML = re.sub('\s', '_', docbookXML)
# Destination directory
if path != '' and os.path.isdir(path) == 0:
createDirectory(path)
# Temporary files names
if deltemp == 0:
globalXML = os.path.join(path, 'global.xml')
else:
import tempfile
tempfile.tempdir = path
globalXML = tempfile.mktemp('g.xml')
# Images Directory
imgAbsDir = os.path.join(toUnicode(path), imgRelDir)
# Force image rewriting (0|1)
rewriteImg = imagesrew
# XSL processor command
if command is not None:
XSLCmdTemplate = getConfigValue('xslt-command', 'command', command)
else:
XSLCmdTemplate = getConfigValue('xslt-command', 'command')
# DTD
if dtd is not None:
dtdPublic = getConfigValue('dtd', 'doctype-public', dtd)
dtdSystem = getConfigValue('dtd', 'doctype-system', dtd)
else:
dtdPublic = getConfigValue('dtd', 'doctype-public')
dtdSystem = getConfigValue('dtd', 'doctype-system')
# XSLT stylesheet
if xslt_file_path is not None:
docbookXSL = xslt_file_path
# XSLT Params
if xslParams is not None:
if command is not None:
param_syntax = getConfigValue('xslt-command', 'param-syntax',
command)
else:
param_syntax = getConfigValue('xslt-command', 'param-syntax')
# Retrieve the XSLT params and set them according to the param syntax.
# This is done because XSLT processors have different command line
# options.
XSLParams = ("%s" % (param_syntax)) % tuple(xslParams)
else:
XSLParams = ' '
if verbose:
print " - xslParams = %s" % xslParams
print " - param_syntax = %s" % param_syntax
print " - XSLParams = %s" % XSLParams
def initializeSets(ooo_file_path, docbook, command, imagesrew, deltemp, dtd,
conf_file_path, xslt_file_path, xslParams, verbose):
verifSys()
setConfFileSettings(conf_file_path)
setUserSettings(ooo_file_path, docbook, command, imagesrew, deltemp, dtd,
xslt_file_path, xslParams, verbose)
# --------------------
# Conversion functions
# --------------------
def extractOooArchive(docOOoSXW, XMLFile):
"""Generic XML files extraction.
"""
# Checking that the OOo file is truly of the ZIP format
if zipfile.is_zipfile(docOOoSXW):
zip_file = zipfile.ZipFile(docOOoSXW, 'r')
# Listing the file content
contentListZip = zip_file.namelist()
# Checking that a "content.xml" file is truly present
for i in contentListZip:
if i == XMLFile:
# If "content.xml" is truly present, we open it.
# The result, "docOOoXML" is the content as text.
docOOoXMLExist = 1
strOOoXML = zip_file.read(XMLFile)
zip_file.close()
return strOOoXML
def listChildNodes(docOOoSXW, XMLFile, ooo_file_path, verbose):
"""Extract and parse Zip XML files for concat.
"""
# Extract and parse XML file
strXML = extractOooArchive(docOOoSXW, XMLFile)
XMLparse = minidom.parseString(strXML)
rootNode = XMLparse.documentElement
vChildNodes = rootNode.childNodes
# Images treatment
if XMLFile == OOO_CONTENT_FILE_NAME:
global dictImg, myZip, numImg, dictNamespace
numImg = 0
dictImg = {}
dictNamespace = {}
myZip = zipfile.ZipFile(docOOoSXW, 'r')
# Creating the directory where the images will be dropped.
# The exported OLE images go in this directory too.
if not (os.path.exists(imgAbsDir)
and os.path.isdir(imgAbsDir)):
os.mkdir(imgAbsDir)
if process_ole_objects:
cmd = (('%s %s --target "%s" '
'--oooserverhost %s --oooserverport %s '
'--format %s "%s"')
% (
ooopython_path,
ole2img_script_path,
imgAbsDir,
oooserver_host, oooserver_port,
ole_img_format, ooo_file_path))
if verbose:
print cmd
os.system(cmd)
replaceImageNode(vChildNodes)
myZip.close()
# Extract all root element's childs
listChildElts = []
for node in vChildNodes:
if node.nodeType == node.ELEMENT_NODE:
listChildElts.append(node)
return listChildElts
def replaceImageNode(vChildNodes):
"""Replace the incorporated images links by the new images links
and extract and copy all incorporated images.
XXX: Why renaming images (apart from making their path relative)?
Please add comment if you know.
"""
global numImg
for node in vChildNodes:
if node.nodeName == 'draw:image':
hRefValue = node.attributes['xlink:href'].value
if find(hRefValue, 'Pictures/', 0) != -1:
nameImgOld = os.path.split(hRefValue)[1]
# XXX: What is this block for? Please add comment if you know.
if dictImg.has_key(nameImgOld):
node.attributes['xlink:href'].value = dictImg[nameImgOld]
else:
extImg = os.path.splitext(nameImgOld)[1]
numImg += 1
nameImgNew = imgRootName + "%03i" % numImg + extImg
hrefImgNew = os.path.join(imgRelDir, nameImgNew)
pathImgNew = os.path.join(imgAbsDir, nameImgNew)
if hRefValue.startswith('#'):
# OOo 1
pathImgZip = hRefValue[1:]
else:
# OOo 2
pathImgZip = hRefValue
zipImg = myZip.read(pathImgZip)
if os.path.isfile(pathImgNew) and rewriteImg:
os.remove(pathImgNew)
if not os.path.isfile(pathImgNew):
imgNew = open(pathImgNew, 'wb')
imgNew.write(zipImg)
imgNew.close()
dictImg[nameImgOld] = hrefImgNew
node.attributes['xlink:href'].value = dictImg[nameImgOld]
else:
pass
# XXX: What is this block for? Please add comment if you know.
if node.hasChildNodes():
wChilNodes = node.childNodes
replaceImageNode(wChilNodes)
def getGlobalRootHead(sourcefile, XMLFile):
strXML = extractOooArchive(docOOoSXW, XMLFile)
XMLparse = minidom.parseString(strXML)
rootNode = XMLparse.documentElement
if rootNode.attributes['xmlns:office'].value == 'http://openoffice.org/2000/office':
oooVersion = 'ooo1'
elif rootNode.attributes['xmlns:office'].value == 'urn:oasis:names:tc:opendocument:xmlns:office:1.0':
oooVersion = 'ooo2'
if oooVersion == 'ooo1':
# OpenOffice.org 1.x
globalRootHead = """\
<?xml version="1.0" encoding="UTF-8"?>
<office:document xmlns:office="http://openoffice.org/2000/office"
xmlns:style="http://openoffice.org/2000/style"
xmlns:text="http://openoffice.org/2000/text"
xmlns:table="http://openoffice.org/2000/table"
xmlns:draw="http://openoffice.org/2000/drawing"
xmlns:fo="http://www.w3.org/1999/XSL/Format"
xmlns:xlink="http://www.w3.org/1999/xlink"
xmlns:number="http://openoffice.org/2000/datastyle"
xmlns:svg="http://www.w3.org/2000/svg"
xmlns:chart="http://openoffice.org/2000/chart"
xmlns:dr3d="http://openoffice.org/2000/dr3d"
xmlns:math="http://www.w3.org/1998/Math/MathML"
xmlns:form="http://openoffice.org/2000/form"
xmlns:script="http://openoffice.org/2000/script"
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:meta="http://openoffice.org/2000/meta"
office:class="text"
office:version="1.0">
"""
elif oooVersion == 'ooo2':
# OpenOffice.org 2.x - OpenDocument
globalRootHead = """\
<?xml version="1.0" encoding="UTF-8"?>
<office:document
xmlns:office="urn:oasis:names:tc:opendocument:xmlns:office:1.0"
xmlns:style="urn:oasis:names:tc:opendocument:xmlns:style:1.0"
xmlns:text="urn:oasis:names:tc:opendocument:xmlns:text:1.0"
xmlns:table="urn:oasis:names:tc:opendocument:xmlns:table:1.0"
xmlns:draw="urn:oasis:names:tc:opendocument:xmlns:drawing:1.0"
xmlns:fo="urn:oasis:names:tc:opendocument:xmlns:xsl-fo-compatible:1.0"
xmlns:xlink="http://www.w3.org/1999/xlink"
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:meta="urn:oasis:names:tc:opendocument:xmlns:meta:1.0"
xmlns:number="urn:oasis:names:tc:opendocument:xmlns:datastyle:1.0"
xmlns:svg="urn:oasis:names:tc:opendocument:xmlns:svg-compatible:1.0"
xmlns:chart="urn:oasis:names:tc:opendocument:xmlns:chart:1.0"
xmlns:dr3d="urn:oasis:names:tc:opendocument:xmlns:dr3d:1.0"
xmlns:math="http://www.w3.org/1998/Math/MathML"
xmlns:form="urn:oasis:names:tc:opendocument:xmlns:form:1.0"
xmlns:script="urn:oasis:names:tc:opendocument:xmlns:script:1.0"
xmlns:ooo="http://openoffice.org/2004/office"
xmlns:ooow="http://openoffice.org/2004/writer"
xmlns:oooc="http://openoffice.org/2004/calc"
xmlns:dom="http://www.w3.org/2001/xml-events"
xmlns:xforms="http://www.w3.org/2002/xforms"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
office:version="1.0">
"""
globalRootFoot = """\
</office:document>
"""
return globalRootHead, globalRootFoot, oooVersion
def createGlobalXML(globalFile, ooo_file_path, verbose):
"""
Create a global XML file by concatening the different XML files contained
within a .sxw OOo file (meta.xml, styles.xml, content.xml).
"""
# First let's delete any previous images directory, because if we don't
# delete it there might be a previous directory with content in it and we
# don't want to get this unrequested content in a generated archive.
if os.path.exists(imgAbsDir):
shutil.rmtree(imgAbsDir)
globalRootHead, globalRootFoot, oooVersion = getGlobalRootHead(docOOoSXW,
OOO_META_FILE_NAME)
globalRootStr = globalRootHead + globalRootFoot
globalStrParse = minidom.parseString(globalRootStr)
globalRoot = globalStrParse.documentElement
metaListElts = listChildNodes(docOOoSXW, OOO_META_FILE_NAME, ooo_file_path, verbose)
stylesListElts = listChildNodes(docOOoSXW, OOO_STYLES_FILE_NAME, ooo_file_path, verbose)
contentListElts = listChildNodes(docOOoSXW, OOO_CONTENT_FILE_NAME, ooo_file_path, verbose)
globalListElts = metaListElts + stylesListElts + contentListElts
for node in globalListElts:
globalRoot.appendChild(node)
strXML = globalRoot.toxml()
listLine = split(strXML, '\n')[1:]
strXMLNS = join([globalRootHead, join(listLine, u"\n")], u"\n")
fileXML = codecs.open(globalFile, 'w', 'utf-8')
fileXML.write(strXMLNS)
fileXML.close()
return oooVersion
def tempFilesDelete(deltemp):
if deltemp == 1:
os.remove(globalXML)
def getXsltCommand(input_file_path, output_file_path, stylesheet, verbose):
"""Return the actual XSLT processing command.
"""
global XSLCmdTemplate
cmd = XSLCmdTemplate
gListVar = ['%o', '%i', '%s', '%p', '%y', '%v']
listVar = []
for var in gListVar:
if find(cmd, var) != -1:
listVar.append(var)
for var in listVar:
varSplit = split(cmd, var)
if var == '%o':
# Note that the file path has to be protected by "" in case it
# contains special characters such as spaces.
varSplit = '%s"%s"%s' % (varSplit[0], toUnicode(output_file_path), varSplit[1])
elif var == '%i':
# Note that the file path has to be protected by "" in case it
# contains special characters such as spaces.
varSplit = '%s"%s"%s' % (varSplit[0], toUnicode(input_file_path), varSplit[1])
elif var == '%s':
varSplit = '%s"%s"%s' % (varSplit[0], stylesheet, varSplit[1])
elif var == '%p':
varSplit = '%s%s%s' % (varSplit[0], dtdPublic, varSplit[1])
elif var == '%y':
varSplit = '%s%s%s' % (varSplit[0], dtdSystem, varSplit[1])
elif var == '%v':
varSplit = '%s%s%s' % (varSplit[0], XSLParams, varSplit[1])
cmd = join(varSplit, '')
if verbose:
print cmd
return cmd.encode(preferred_encoding)
def toUnicode(s):
return unicode(s, preferred_encoding)
def o2dConvert(input, output, stylesheet, verbose):
"""Generic conversion.
"""
startTime = time.time()
os.system(getXsltCommand(input, output, stylesheet,verbose))
endTime = time.time()
duration = round(endTime - startTime, 2)
# -------------
# User commands
# -------------
def createDocbookArchive(zipfile_target):
pjoin = os.path.join
psplit = os.path.split
psplitext = os.path.splitext
pbasename = os.path.basename
arch_dest_dir = psplit(zipfile_target)[0]
arch_top_dir = psplitext(pbasename(zipfile_target))[0]
arch_path = pjoin(arch_dest_dir, arch_top_dir + '.zip')
arch = zipfile.ZipFile(arch_path, 'w', zipfile.ZIP_DEFLATED)
docbook_fname = pbasename(docbookXML)
docbook_path_in_arch = pjoin(arch_top_dir, docbook_fname)
# ZIP entries paths are stored in "code page 437" encoding (cp437).
# One cannot use UTF-8 for the ZIP entries paths.
docbook_path_in_arch_enc = toUnicode(docbook_path_in_arch).encode(ZIP_FILE_ENCODING)
arch.write(docbookXML, docbook_path_in_arch_enc)
# Adding in the arch the images contained in the original OOo arch
if os.path.exists(imgAbsDir):
for img_name in os.listdir(imgAbsDir):
img_path = pjoin(imgAbsDir, img_name)
img_path_in_arch = pjoin(arch_top_dir, 'images', img_name)
# ZIP entries paths are stored in "code page 437" encoding (cp437).
# One cannot use UTF-8 for the ZIP entries paths.
img_path_in_arch_enc = img_path_in_arch.encode(ZIP_FILE_ENCODING)
arch.write(img_path, img_path_in_arch_enc)
arch.close()
# Remove created DocBook XML and subobjects, if any
os.remove(docbookXML)
if os.path.exists(imgAbsDir):
shutil.rmtree(imgAbsDir)
def convert(ooo_file_path,
command=None,
docbook_file_path=None,
imagesrew=1,
deltemp=1,
dtd=None,
conf_file_path=None,
xslt_file_path=None,
xslParams=None,
verbose=False,
zipfile_target=False,
docbook_top_element='book',
process_ole_objects=False,
docbookXSL=None,
):
"""Convert OpenOffice.org Writer file to DocBook XML.
"""
startTime = time.time()
if verbose:
print " 1 - Command line options"
print " - OOo2DBK config file : %s" % conf_file_path
print " - OpenOffice.org file : %s" % ooo_file_path
print " - DocBook file : %s" % docbook_file_path
print " - top element is : %s" % docbook_top_element
print " - process OLE objects : %s" % process_ole_objects
initializeSets(ooo_file_path, docbook_file_path, command, imagesrew,
deltemp, dtd, conf_file_path, xslt_file_path, xslParams,
verbose)
ooo_file_path = toUnicode(ooo_file_path)
if docbook_file_path is not None:
docbook_file_path = toUnicode(docbook_file_path)
if xslt_file_path is not None:
xslt_file_path = toUnicode(xslt_file_path)
endTime = time.time()
duration = round(endTime - startTime, 2)
if verbose:
print " ==>", duration, "sec.\n"
print " 2 - Unzip and concat OpenOffice.org XML files"
startTime = time.time()
oooVersion = createGlobalXML(globalXML, ooo_file_path, verbose)
endTime = time.time()
duration = round(endTime - startTime, 2)
if verbose:
print " - Detected file format: %s" % (oooVersion)
print " ==>", duration, "sec.\n"
print " 3 - Initialization (configuration file and computed options)"
if docbookXSL is None:
# Get XSLT file to use from configuration file
docbookXSL = getXSLfile(oooVersion)
if verbose:
global configXML
print " - preferred encoding : %s" % preferred_encoding
print " - OOo2DBK config file : %s" % configXML
print " - XSLT file : %s" % docbookXSL
print " - OpenOffice.org file path : %s" % docOOoSXW
print " - DocBook file path : %s" % docbookXML
if process_ole_objects:
print " - oooserver host : %s" % oooserver_host
print " - oooserver port : %s" % oooserver_port
print " - exported OLE image format : %s" % ole_img_format
print " - OOo Python path: %s" % ooopython_path
print "\n 4 - DocBook file creation"
startTime = time.time()
o2dConvert(globalXML, docbookXML, docbookXSL, verbose)
tempFilesDelete(deltemp)
endTime = time.time()
duration = round(endTime - startTime, 2)
if verbose:
print " ==>", duration, "sec.\n"
print "Conversion completed\n"
if zipfile_target:
createDocbookArchive(zipfile_target)
if verbose:
print "Zip archive created\n"
# Shell conversion
if __name__ == "__main__":
execArgs()
|