diff options
author | Thorsten Behrens <tbehrens@novell.com> | 2010-06-03 21:31:09 +0200 |
---|---|---|
committer | Thorsten Behrens <tbehrens@novell.com> | 2010-06-03 21:31:09 +0200 |
commit | dc183778b9ba1ed65aee799a4bcc6e6763cc7cc3 (patch) | |
tree | f6b101ae770a7be50928a22ebe521bece134e96b | |
parent | 5fb3c27ed365d0d4a82fca13b91a0ec2cb2c97ea (diff) |
Added binding-generation for odf
-rw-r--r-- | .gitignore | 1 | ||||
-rw-r--r-- | examples/odf/dumpsample.py | 344 | ||||
-rw-r--r-- | examples/odf/genbindings.sh | 32 | ||||
-rwxr-xr-x | examples/odf/test.sh | 6 |
4 files changed, 383 insertions, 0 deletions
@@ -22,6 +22,7 @@ examples/ooxml-strict/sml examples/ooxml-strict/wml examples/ooxml-strict/pml examples/ooxml-strict/props +examples/odf/odf examples/geocoder/GeoCoder.py examples/geocoder/GeoCoder.wsdl examples/geocoder/test.log diff --git a/examples/odf/dumpsample.py b/examples/odf/dumpsample.py new file mode 100644 index 0000000..3969831 --- /dev/null +++ b/examples/odf/dumpsample.py @@ -0,0 +1,344 @@ +import sys +import xml.sax +import pyxb.utils.domutils as domutils +import pyxb.utils.saxdom +import pyxb.binding.saxer +import re + +import dml.dml +import pml.pml +import props.props +import sml.sml +import wml.wml + +import os +import zipfile +import StringIO +from exceptions import TypeError +import xml.etree.ElementTree as ElemTree + +# 3-tuples of media type, root schema uri, and relationship type uri +# (contains all xml-ish file types of ooxml) +xml_type_map = [ + ("application/vnd.openxmlformats-officedocument.wordprocessingml.comments+xml", + "http://schemas.openxmlformats.org/wordprocessingml/2006/main", + "http://schemas.openxmlformats.org/officeDocument/2006/relationships/comments"), + + ("application/vnd.openxmlformats-officedocument.wordprocessingml.settings+xml", + "http://schemas.openxmlformats.org/wordprocessingml/2006/main", + "http://schemas.openxmlformats.org/officeDocument/2006/relationships/settings"), + + ("application/vnd.openxmlformats-officedocument.wordprocessingml.endnotes+xml", + "http://schemas.openxmlformats.org/wordprocessingml/2006/main", + "http://schemas.openxmlformats.org/officeDocument/2006/relationships/endnotes"), + + ("application/vnd.openxmlformats-officedocument.wordprocessingml.fontTable+xml", + "http://schemas.openxmlformats.org/wordprocessingml/2006/main", + "http://schemas.openxmlformats.org/officeDocument/2006/relationships/fontTable"), + + ("application/vnd.openxmlformats-officedocument.wordprocessingml.footer+xml", + "http://schemas.openxmlformats.org/wordprocessingml/2006/main", + "http://schemas.openxmlformats.org/officeDocument/2006/relationships/footer"), + + ("application/vnd.openxmlformats-officedocument.wordprocessingml.footnotes+xml", + "http://schemas.openxmlformats.org/wordprocessingml/2006/main", + "http://schemas.openxmlformats.org/officeDocument/2006/relationships/footnotes"), + + ("application/vnd.openxmlformats-officedocument.wordprocessingml.document.glossary+xml", + "http://schemas.openxmlformats.org/wordprocessingml/2006/main", + "http://schemas.openxmlformats.org/officeDocument/2006/relationships/glossaryDocument"), + + ("application/vnd.openxmlformats-officedocument.wordprocessingml.header+xml", + "http://schemas.openxmlformats.org/wordprocessingml/2006/main", + "http://schemas.openxmlformats.org/officeDocument/2006/relationships/header"), + + ("application/vnd.openxmlformats-officedocument.wordprocessingml.numbering+xml", + "http://schemas.openxmlformats.org/wordprocessingml/2006/main", + "http://schemas.openxmlformats.org/officeDocument/2006/relationships/numbering"), + + ("application/vnd.openxmlformats-officedocument.wordprocessingml.styles+xml", + "http://schemas.openxmlformats.org/wordprocessingml/2006/main", + "http://schemas.openxmlformats.org/officeDocument/2006/relationships/styles"), + + ("application/vnd.openxmlformats-officedocument.wordprocessingml.webSettings+xml", + "http://schemas.openxmlformats.org/wordprocessingml/2006/main", + "http://schemas.openxmlformats.org/officeDocument/2006/relationships/webSettings"), + + ("application/vnd.openxmlformats-officedocument.spreadsheetml.calcChain+xml", + "http://schemas.openxmlformats.org/spreadsheetml/2006/main", + "http://schemas.openxmlformats.org/officeDocument/2006/relationships/calcChain"), + + ("application/vnd.openxmlformats-officedocument.spreadsheetml.chartsheet+xml", + "http://schemas.openxmlformats.org/spreadsheetml/2006/main", + "http://schemas.openxmlformats.org/officeDocument/2006/relationships/chartsheet"), + + ("application/vnd.openxmlformats-officedocument.spreadsheetml.comments+xml", + "http://schemas.openxmlformats.org/spreadsheetml/2006/main", + "http://schemas.openxmlformats.org/officeDocument/2006/relationships/comments"), + + ("application/vnd.openxmlformats-officedocument.spreadsheetml.connections+xml", + "http://schemas.openxmlformats.org/spreadsheetml/2006/main", + "http://schemas.openxmlformats.org/officeDocument/2006/relationships/connections"), + + ("application/xml", + "http://schemas.openxmlformats.org/spreadsheetml/2006/main", + "http://schemas.openxmlformats.org/officeDocument/2006/relationships/xmlMaps"), + + ("application/vnd.openxmlformats-officedocument.spreadsheetml.dialogsheet+xml", + "http://schemas.openxmlformats.org/spreadsheetml/2006/main", + "http://schemas.openxmlformats.org/officeDocument/2006/relationships/dialogsheet"), + + ("application/vnd.openxmlformats-officedocument.drawing+xml", + "http://schemas.openxmlformats.org/drawingml/2006/spreadsheetDrawing", + "http://schemas.openxmlformats.org/officeDocument/2006/relationships/drawing"), + + ("application/vnd.openxmlformats-officedocument.spreadsheetml.externalLink+xml", + "http://schemas.openxmlformats.org/spreadsheetml/2006/main", + "http://schemas.openxmlformats.org/officeDocument/2006/relationships/externalLink"), + + ("application/vnd.openxmlformats-officedocument.spreadsheetml.sheetMetadata+xml", + "http://schemas.openxmlformats.org/spreadsheetml/2006/main", + "http://schemas.openxmlformats.org/officeDocument/2006/relationships/sheetMetadata"), + + ("application/vnd.openxmlformats-officedocument.spreadsheetml.pivotTable+xml", + "http://schemas.openxmlformats.org/spreadsheetml/2006/main", + "http://schemas.openxmlformats.org/officeDocument/2006/relationships/pivotTable"), + + ("application/vnd.openxmlformats-officedocument.spreadsheetml.pivotCacheDefinition+xml", + "http://schemas.openxmlformats.org/spreadsheetml/2006/main", + "http://schemas.openxmlformats.org/officeDocument/2006/relationships/pivotCacheDefinition"), + + ("application/vnd.openxmlformats-officedocument.spreadsheetml.pivotCacheRecords+xml", + "http://schemas.openxmlformats.org/spreadsheetml/2006/main", + "http://schemas.openxmlformats.org/officeDocument/2006/relationships/"), + + ("application/vnd.openxmlformats-officedocument.spreadsheetml.queryTable+xml", + "http://schemas.openxmlformats.org/spreadsheetml/2006/main", + "http://schemas.openxmlformats.org/officeDocument/2006/relationships/queryTable"), + + ("application/vnd.openxmlformats-officedocument.spreadsheetml.sharedStrings+xml", + "http://schemas.openxmlformats.org/spreadsheetml/2006/main", + "http://schemas.openxmlformats.org/officeDocument/2006/relationships/sharedStrings"), + + ("application/vnd.openxmlformats-officedocument.spreadsheetml.revisionHeaders+xml", + "http://schemas.openxmlformats.org/spreadsheetml/2006/main", + "http://schemas.openxmlformats.org/officeDocument/2006/relationships/revisionHeaders"), + + ("application/vnd.openxmlformats-officedocument.spreadsheetml.revisionLog+xml", + "http://schemas.openxmlformats.org/spreadsheetml/2006/main", + "http://schemas.openxmlformats.org/officeDocument/2006/relationships/revisionLog"), + + ("application/vnd.openxmlformats-officedocument.spreadsheetml.userNames+xml", + "http://schemas.openxmlformats.org/spreadsheetml/2006/main", + "http://schemas.openxmlformats.org/officeDocument/2006/relationships/usernames"), + + ("application/vnd.openxmlformats-officedocument.spreadsheetml.tableSingleCells+xml", + "http://schemas.openxmlformats.org/spreadsheetml/2006/main", + "http://schemas.openxmlformats.org/officeDocument/2006/relationships/tableSingleCells"), + + ("application/vnd.openxmlformats-officedocument.spreadsheetml.styles+xml", + "http://schemas.openxmlformats.org/spreadsheetml/2006/mains", + "http://schemas.openxmlformats.org/officeDocument/2006/relationships/styles"), + + ("application/vnd.openxmlformats-officedocument.spreadsheetml.table+xml", + "http://schemas.openxmlformats.org/spreadsheetml/2006/main", + "http://schemas.openxmlformats.org/officeDocument/2006/relationships/table"), + + ("application/vnd.openxmlformats-officedocument.spreadsheetml.volatileDependencies+xml", + "http://schemas.openxmlformats.org/spreadsheetml/2006/main", + "http://schemas.openxmlformats.org/officeDocument/2006/relationships/volatileDependencies"), + + ("application/vnd.openxmlformats-officedocument.spreadsheetml.worksheet+xml", + "http://schemas.openxmlformats.org/spreadsheetml/2006/main", + "http://schemas.openxmlformats.org/officeDocument/2006/relationships/worksheet"), + + ("application/vnd.openxmlformats-officedocument.presentationml.commentAuthors+xml", + "http://schemas.openxmlformats.org/presentationml/2006/main", + "http://schemas.openxmlformats.org/officeDocument/2006/relationships/commentAuthors"), + + ("application/vnd.openxmlformats-officedocument.presentationml.comments+xml", + "http://schemas.openxmlformats.org/presentationml/2006/main", + "http://schemas.openxmlformats.org/officeDocument/2006/relationships/comments"), + + ("application/vnd.openxmlformats-officedocument.presentationml.handoutMaster+xml", + "http://schemas.openxmlformats.org/presentationml/2006/main", + "http://schemas.openxmlformats.org/officeDocument/2006/relationships/handoutMaster"), + + ("application/vnd.openxmlformats-officedocument.presentationml.notesMaster+xml", + "http://schemas.openxmlformats.org/presentationml/2006/main", + "http://schemas.openxmlformats.org/officeDocument/2006/relationships/notesMaster"), + + ("application/vnd.openxmlformats-officedocument.presentationml.notesSlide+xml", + "http://schemas.openxmlformats.org/presentationml/2006/main", + "http://schemas.openxmlformats.org/officeDocument/2006/relationships/notesSlide"), + + ("application/vnd.openxmlformats-officedocument.presentationml.presentation.main+xml", + "http://schemas.openxmlformats.org/presentationml/2006/main", + "http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument"), + + ("application/vnd.openxmlformats-officedocument.presentationml.presProps+xml", + "http://schemas.openxmlformats.org/presentationml/2006/main", + "http://schemas.openxmlformats.org/officeDocument/2006/relationships/presProps"), + + ("application/vnd.openxmlformats-officedocument.presentationml.slide+xml", + "http://schemas.openxmlformats.org/presentationml/2006/main", + "http://schemas.openxmlformats.org/officeDocument/2006/relationships/slide"), + + ("application/vnd.openxmlformats-officedocument.presentationml.slideLayout+xml", + "http://schemas.openxmlformats.org/presentationml/2006/main", + "http://schemas.openxmlformats.org/officeDocument/2006/relationships/slideLayout"), + + ("application/vnd.openxmlformats-officedocument.presentationml.slideMaster+xml", + "http://schemas.openxmlformats.org/presentationml/2006/main", + "http://schemas.openxmlformats.org/officeDocument/2006/relationships/slideMaster"), + + ("application/vnd.openxmlformats-officedocument.presentationml.slideUpdateInfo+xml", + "http://schemas.openxmlformats.org/presentationml/2006/main", + "http://schemas.openxmlformats.org/officeDocument/2006/relationships/slideUpdateInfo"), + + ("application/vnd.openxmlformats-officedocument.presentationml.viewProps+xml", + "http://schemas.openxmlformats.org/presentationml/2006/main", + "http://schemas.openxmlformats.org/officeDocument/2006/relationships/viewProps"), + + ("application/vnd.openxmlformats-officedocument.drawingml.chart+xml", + "http://schemas.openxmlformats.org/drawingml/2006/chart", + "http://schemas.openxmlformats.org/officeDocument/2006/relationships/chart"), + + ("application/vnd.openxmlformats-officedocument.drawingml.chartshapes+xml", + "http://schemas.openxmlformats.org/drawingml/2006/chart", + "http://schemas.openxmlformats.org/officeDocument/2006/relationships/chartUserShapes"), + + ("application/vnd.openxmlformats-officedocument.drawingml.diagramColors+xml", + "http://schemas.openxmlformats.org/drawingml/2006/diagram", + "http://schemas.openxmlformats.org/officeDocument/2006/relationships/diagramColors"), + + ("application/vnd.openxmlformats-officedocument.drawingml.diagramData+xml", + "http://schemas.openxmlformats.org/drawingml/2006/diagram", + "http://schemas.openxmlformats.org/officeDocument/2006/relationships/diagramData"), + + ("application/vnd.openxmlformats-officedocument.drawingml.diagramLayout+xml", + "http://schemas.openxmlformats.org/drawingml/2006/diagram", + "http://schemas.openxmlformats.org/officeDocument/2006/relationships/diagramLayout"), + + ("application/vnd.openxmlformats-officedocument.drawingml.diagramStyle+xml", + "http://schemas.openxmlformats.org/drawingml/2006/diagram", + "http://schemas.openxmlformats.org/officeDocument/2006/relationships/diagramQuickStyle"), + + ("application/vnd.openxmlformats-officedocument.theme+xml", + "http://schemas.openxmlformats.org/drawingml/2006/main", + "http://schemas.openxmlformats.org/officeDocument/2006/relationships/theme"), + + ("application/vnd.openxmlformats-officedocument.themeOverride+xml", + "http://schemas.openxmlformats.org/drawingml/2006/main", + "http://schemas.openxmlformats.org/officeDocument/2006/relationships/themeOverride"), + + ("application/vnd.openxmlformats-officedocument.presentationml.tableStyles+xml", + "http://schemas.openxmlformats.org/drawingml/2006/main", + "http://schemas.openxmlformats.org/officeDocument/2006/relationships/tableStyles"), + + ("application/xml", + "http://schemas.openxmlformats.org/officeDocument/2006/additionalCharacteristics", + "http://schemas.openxmlformats.org/officeDocument/2006/relationships/customXml"), + + ("application/xml", + "http://schemas.openxmlformats.org/officeDocument/2006/bibliography", + "http://schemas.openxmlformats.org/officeDocument/2006/relationships/customXml"), + + ("application/vnd.openxmlformats-officedocument.customXmlProperties+xml", + "http://schemas.openxmlformats.org/officeDocument/2006/customXmlDataProps", + "http://schemas.openxmlformats.org/officeDocument/2006/relationships/customXmlProps"), + + ("application/vnd.openxmlformats-package.digital-signature-xmlsignature+xml", + "http://schemas.openxmlformats.org/package/2006/digital-signature", + "http://schemas.openxmlformats.org/package/2006/relationships/digital-signature/signature"), + +# no schema +# ("application/vnd.openxmlformats-package.core-properties+xml", +# "http://schemas.openxmlformats.org/package/2006/metadata/core-properties", +# "http://schemas.openxmlformats.org/officedocument/2006/relationships/metadata/core-properties"), + + ("application/vnd.openxmlformats-officedocument.custom-properties+xml", + "http://schemas.openxmlformats.org/officeDocument/2006/custom-properties", + "http://schemas.openxmlformats.org/officeDocument/2006/relationships/custom-properties"), + + ("application/vnd.openxmlformats-officedocument.extended-properties+xml", + "http://schemas.openxmlformats.org/officeDocument/2006/extended-properties", + "http://schemas.openxmlformats.org/officeDocument/2006/relationships/extended-properties") +] + +def createContentTypeMap (xml): + defaultMap={} + overrideMap={} + tree = ElemTree.XML(xml) + ns=lambda s: "{http://schemas.openxmlformats.org/package/2006/content-types}"+s + for elem in tree.findall(ns("Default")): + defaultMap[elem.get("Extension")] = elem.get("ContentType") + for elem in tree.findall(ns("Override")): + overrideMap[elem.get("PartName")] = elem.get("ContentType") + return (defaultMap, overrideMap) + +def validateType (content_types, path, type_str): + extension = os.path.splitext(path)[1] + if isinstance(type_str,str): + comp = lambda s,pat: s != pat + else: + # assume regular expression + comp = lambda s,pat: re.match(pat,s) != None + + if path in content_types[1]: + if comp(content_types[1][path], type_str): + raise TypeError("Wrong content type for %s" % path) + elif extension in content_types[0]: + if comp(content_types[0][extension], type_str): + raise TypeError("Wrong content type for %s" % path) + +def findContentType (content_types, path): + extension = os.path.splitext(path)[1] + if path in content_types[1]: + return content_types[1][path] + elif extension in content_types[0]: + return content_types[0][extension] + +def readFragment (package, content_types, path, type_str): + validateType(content_types, path, type_str) + return ElemTree.XML(package.read(path)) + +def readFragmentIfXml (package, content_types, path, type_map): + media_type = findContentType(content_types, "/"+path) + print "media type for fragment %s is %s" % (fragment, media_type) + for elem in type_map: + if elem[0] == media_type: + print "matching media type found, schema root is %s, relationship type uri is %s" % (elem[1], elem[2]) + saxer = pyxb.binding.saxer.make_parser(location_base=path) + handler = saxer.getContentHandler() + handler.__trace = True + saxer.parse(StringIO.StringIO(package.read(path))) + sax_instance = handler.rootObject() + + for i in sax_instance.iterateBinding([re.compile('.*CT_R'), re.compile('.*rsidR.*')]): + i() + print sax_instance.toxml() + print + i() + print sax_instance.toxml() + print + i() + print sax_instance.toxml() + print + + return None + +if len(sys.argv) > 1: + package = zipfile.ZipFile(sys.argv[1]) + + # populate media type maps + (defaultMap,overrideMap) = createContentTypeMap( + package.read("[Content_Types].xml")) + + # scan all contained files, load those matching one of xml_type_map's + # xml media types + for fragment in package.namelist(): + readFragmentIfXml(package, + (defaultMap,overrideMap), + fragment, + xml_type_map) diff --git a/examples/odf/genbindings.sh b/examples/odf/genbindings.sh new file mode 100644 index 0000000..0d6a937 --- /dev/null +++ b/examples/odf/genbindings.sh @@ -0,0 +1,32 @@ +###################################################### + +# odf + +URI='opendocument.xsd' +PREFIX0='odf' +PREFIX1='dmldiagram' +PREFIX2='dmlcompat' +PREFIX3='dmlpicture' +PREFIX4='dmllockedcanvas' +PREFIX5='dmlchart' + +rm -rf odf +mkdir odf ; cd odf +pyxbgen \ + --schema-root=.. \ + --no-write-for-customization \ + -m "${PREFIX0}" \ + -m "${PREFIX1}" \ + -m "${PREFIX2}" \ + -m "${PREFIX3}" \ + -m "${PREFIX4}" \ + -m "${PREFIX5}" \ + -u "${URI}" \ + -r +if [ ! -f ${PREFIX0}.py ] ; then + echo "from raw.${PREFIX0} import *" > ${PREFIX0}.py +fi +touch __init__.py + +cd .. + diff --git a/examples/odf/test.sh b/examples/odf/test.sh new file mode 100755 index 0000000..52920df --- /dev/null +++ b/examples/odf/test.sh @@ -0,0 +1,6 @@ +PYXB_ROOT=${PYXB_ROOT:-../..} +export PYTHONPATH=${PYXB_ROOT} +export PATH=${PYXB_ROOT}/scripts:${PATH} + +sh genbindings.sh +#python dumpsample.py |