1#************************************************************** 2# 3# Licensed to the Apache Software Foundation (ASF) under one 4# or more contributor license agreements. See the NOTICE file 5# distributed with this work for additional information 6# regarding copyright ownership. The ASF licenses this file 7# to you under the Apache License, Version 2.0 (the 8# "License"); you may not use this file except in compliance 9# with the License. You may obtain a copy of the License at 10# 11# http://www.apache.org/licenses/LICENSE-2.0 12# 13# Unless required by applicable law or agreed to in writing, 14# software distributed under the License is distributed on an 15# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16# KIND, either express or implied. See the License for the 17# specific language governing permissions and limitations 18# under the License. 19# 20#************************************************************** 21 22# usage: make-controls.py -- (re)generate the validator's negative-control packages in fixtures/control/ 23import os, zipfile 24 25C = os.path.join(os.path.dirname(os.path.abspath(__file__)), "fixtures", "control") 26MANI = ('<?xml version="1.0" encoding="UTF-8"?>\n<manifest:manifest xmlns:manifest="urn:oasis:names:tc:opendocument:xmlns:manifest:1.0" manifest:version="1.2">' 27 '<manifest:file-entry manifest:full-path="/" manifest:version="1.2" manifest:media-type="application/vnd.oasis.opendocument.text"/>' 28 '<manifest:file-entry manifest:full-path="content.xml" manifest:media-type="text/xml"/></manifest:manifest>\n') 29MATH = ('<?xml version="1.0" encoding="UTF-8"?>\n<!DOCTYPE math:math PUBLIC "-//OpenOffice.org//DTD Modified W3C MathML 1.01//EN" "math.dtd">\n' 30 '<math:math xmlns:math="http://www.w3.org/1998/Math/MathML"><math:semantics><math:mi>x</math:mi></math:semantics></math:math>\n') 31 32 33def mk(name, ver, body, extra=None): 34 content = ('<?xml version="1.0" encoding="UTF-8"?>\n<office:document-content xmlns:office="urn:oasis:names:tc:opendocument:xmlns:office:1.0" ' 35 'xmlns:text="urn:oasis:names:tc:opendocument:xmlns:text:1.0" office:version="%s"><office:body><office:text>%s</office:text></office:body></office:document-content>\n') % (ver, body) 36 with zipfile.ZipFile(os.path.join(C, name + ".odt"), "w") as z: 37 z.writestr(zipfile.ZipInfo("mimetype"), "application/vnd.oasis.opendocument.text", compress_type=zipfile.ZIP_STORED) 38 z.writestr("content.xml", content, compress_type=zipfile.ZIP_DEFLATED) 39 z.writestr("META-INF/manifest.xml", MANI, compress_type=zipfile.ZIP_DEFLATED) 40 for n, t in (extra or {}).items(): 41 z.writestr(n, t, compress_type=zipfile.ZIP_DEFLATED) 42 43 44OFFNS = ('xmlns:office="urn:oasis:names:tc:opendocument:xmlns:office:1.0" xmlns:text="urn:oasis:names:tc:opendocument:xmlns:text:1.0" ' 45 'xmlns:style="urn:oasis:names:tc:opendocument:xmlns:style:1.0" xmlns:chart="urn:oasis:names:tc:opendocument:xmlns:chart:1.0" ' 46 'xmlns:svg="urn:oasis:names:tc:opendocument:xmlns:svg-compatible:1.0" xmlns:dc="http://purl.org/dc/elements/1.1/" ' 47 'xmlns:meta="urn:oasis:names:tc:opendocument:xmlns:meta:1.0"') 48 49 50def mkdoc(name, ext, mime, streams): 51 """A package whose streams are given whole: {"content.xml": (root element, body)}, written with office:version 1.2.""" 52 mani = MANI.replace("application/vnd.oasis.opendocument.text", mime) 53 with zipfile.ZipFile(os.path.join(C, name + "." + ext), "w") as z: 54 z.writestr(zipfile.ZipInfo("mimetype"), mime, compress_type=zipfile.ZIP_STORED) 55 for n, (root, body) in streams.items(): 56 z.writestr(n, '<?xml version="1.0" encoding="UTF-8"?>\n<%s %s office:version="1.2">%s</%s>\n' % (root, OFFNS, body, root), compress_type=zipfile.ZIP_DEFLATED) 57 z.writestr("META-INF/manifest.xml", mani, compress_type=zipfile.ZIP_DEFLATED) 58 59 60os.makedirs(C, exist_ok=True) 61mk("good", "1.2", "<text:p>hi</text:p>") 62mk("bad", "1.2", "<text:bogus/>") 63mk("wrongver", "1.1", "<text:p>hi</text:p>") 64# text:drop-down is new in ODF 1.3 (OFFICE-3881): invalid in 1.2, valid in 1.3 and 1.4 65mk("new13", "1.2", '<text:p><text:drop-down text:name="d"><text:label text:value="a"/></text:drop-down></text:p>') 66# ODF 1.2 Part 1 2.2.1 B.4: an embedded object whose content.xml is MathML 67mk("mathobj", "1.2", "<text:p>hi</text:p>", {"Object 1/content.xml": MATH}) 68# ODF 1.2 Part 1 2.2.1 D.1/D.2: checked by idcheck.py, not by the schema 69mk("goodid", "1.2", '<text:p xml:id="a1">x</text:p><text:p xml:id=" b-2.c ">y</text:p>') 70mk("dupid", "1.2", '<text:p xml:id="a1">x</text:p><text:p xml:id=" a1 ">y</text:p>') 71mk("badid", "1.2", '<text:p xml:id="1abc">x</text:p>') 72mk("badid2", "1.2", '<text:p xml:id="a b">x</text:p>') 73mk("badns", "1.2", "<text:p><zz:span/></text:p>") 74# Part 3 2.2.1 F: signature files are held to the same rules (validate.py does not look at them) 75mk("badsig", "1.2", "<text:p>hi</text:p>", {"META-INF/documentsignatures.xml": '<a><b xml:id="x"/><b xml:id="x"/></a>'}) 76# Part 1 2.2.2 B.1: valid as an extended document only (foreign attribute, foreign element in a paragraph, foreign element elsewhere) 77mk("extforeign", "1.2", '<text:p xmlns:foo="urn:foo" foo:x="1">a<foo:y>b</foo:y>c</text:p><foo:w xmlns:foo="urn:foo">z<text:p>q</text:p></foo:w>') 78TEXT = "application/vnd.oasis.opendocument.text" 79# meta:creator-initials in office:annotation is new in ODF 1.3; baseinit is the same note without it 80ANN = '<office:body><office:text><text:p><office:annotation><dc:creator>A</dc:creator>%s<text:p>n</text:p></office:annotation>x</text:p></office:text></office:body>' 81for n, ini in (("baseinit", ""), ("newinit", "<meta:creator-initials>AB</meta:creator-initials>")): 82 mkdoc(n, "odt", TEXT, {"content.xml": ("office:document-content", ANN % ini)}) 83# chart:coordinate-region (inner plot position) is new in 1.3; baseregion is the same chart without it 84CHART = "application/vnd.oasis.opendocument.chart" 85for n, region in (("baseregion", ""), ("newregion", '<chart:coordinate-region svg:x="1cm" svg:y="1cm" svg:width="5cm" svg:height="4cm"/>')): 86 mkdoc(n, "odc", CHART, {"content.xml": ("office:document-content", '<office:body><office:chart><chart:chart chart:class="chart:bar"><chart:plot-area>%s</chart:plot-area></chart:chart></office:chart></office:body>' % region)}) 87# 1.3 makes style:scale-to, style:scale-to-pages and the pair scale-to-X/scale-to-Y mutually exclusive (OFFICE-3857); 1.2 has no scale-to-X/Y 88for n, attrs in (("scaleone", 'style:scale-to="80%"'), ("scalexy", 'style:scale-to-X="2" style:scale-to-Y="3"'), ("scaleboth", 'style:scale-to-X="2" style:scale-to-Y="3" style:scale-to="80%"')): 89 mkdoc(n, "odt", TEXT, {"content.xml": ("office:document-content", "<office:body><office:text><text:p>hi</text:p></office:text></office:body>"), 90 "styles.xml": ("office:document-styles", '<office:automatic-styles><style:page-layout style:name="pm1"><style:page-layout-properties %s/></style:page-layout></office:automatic-styles>' % attrs)}) 91