#**************************************************************
#
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
#
#**************************************************************
# usage: make-controls.py -- (re)generate the validator's negative-control packages in fixtures/control/
import os, zipfile
C = os.path.join(os.path.dirname(os.path.abspath(__file__)), "fixtures", "control")
MANI = ('\n'
''
'\n')
MATH = ('\n\n'
'x\n')
def mk(name, ver, body, extra=None):
content = ('\n%s\n') % (ver, body)
with zipfile.ZipFile(os.path.join(C, name + ".odt"), "w") as z:
z.writestr(zipfile.ZipInfo("mimetype"), "application/vnd.oasis.opendocument.text", compress_type=zipfile.ZIP_STORED)
z.writestr("content.xml", content, compress_type=zipfile.ZIP_DEFLATED)
z.writestr("META-INF/manifest.xml", MANI, compress_type=zipfile.ZIP_DEFLATED)
for n, t in (extra or {}).items():
z.writestr(n, t, compress_type=zipfile.ZIP_DEFLATED)
OFFNS = ('xmlns:office="urn:oasis:names:tc:opendocument:xmlns:office:1.0" xmlns:text="urn:oasis:names:tc:opendocument:xmlns:text:1.0" '
'xmlns:style="urn:oasis:names:tc:opendocument:xmlns:style:1.0" xmlns:chart="urn:oasis:names:tc:opendocument:xmlns:chart:1.0" '
'xmlns:svg="urn:oasis:names:tc:opendocument:xmlns:svg-compatible:1.0" xmlns:dc="http://purl.org/dc/elements/1.1/" '
'xmlns:meta="urn:oasis:names:tc:opendocument:xmlns:meta:1.0"')
def mkdoc(name, ext, mime, streams):
"""A package whose streams are given whole: {"content.xml": (root element, body)}, written with office:version 1.2."""
mani = MANI.replace("application/vnd.oasis.opendocument.text", mime)
with zipfile.ZipFile(os.path.join(C, name + "." + ext), "w") as z:
z.writestr(zipfile.ZipInfo("mimetype"), mime, compress_type=zipfile.ZIP_STORED)
for n, (root, body) in streams.items():
z.writestr(n, '\n<%s %s office:version="1.2">%s%s>\n' % (root, OFFNS, body, root), compress_type=zipfile.ZIP_DEFLATED)
z.writestr("META-INF/manifest.xml", mani, compress_type=zipfile.ZIP_DEFLATED)
os.makedirs(C, exist_ok=True)
mk("good", "1.2", "hi")
mk("bad", "1.2", "")
mk("wrongver", "1.1", "hi")
# text:drop-down is new in ODF 1.3 (OFFICE-3881): invalid in 1.2, valid in 1.3 and 1.4
mk("new13", "1.2", '')
# ODF 1.2 Part 1 2.2.1 B.4: an embedded object whose content.xml is MathML
mk("mathobj", "1.2", "hi", {"Object 1/content.xml": MATH})
# ODF 1.2 Part 1 2.2.1 D.1/D.2: checked by idcheck.py, not by the schema
mk("goodid", "1.2", 'xy')
mk("dupid", "1.2", 'xy')
mk("badid", "1.2", 'x')
mk("badid2", "1.2", 'x')
mk("badns", "1.2", "")
# Part 3 2.2.1 F: signature files are held to the same rules (validate.py does not look at them)
mk("badsig", "1.2", "hi", {"META-INF/documentsignatures.xml": ''})
# Part 1 2.2.2 B.1: valid as an extended document only (foreign attribute, foreign element in a paragraph, foreign element elsewhere)
mk("extforeign", "1.2", 'abczq')
TEXT = "application/vnd.oasis.opendocument.text"
# meta:creator-initials in office:annotation is new in ODF 1.3; baseinit is the same note without it
ANN = 'A%snx'
for n, ini in (("baseinit", ""), ("newinit", "AB")):
mkdoc(n, "odt", TEXT, {"content.xml": ("office:document-content", ANN % ini)})
# chart:coordinate-region (inner plot position) is new in 1.3; baseregion is the same chart without it
CHART = "application/vnd.oasis.opendocument.chart"
for n, region in (("baseregion", ""), ("newregion", '')):
mkdoc(n, "odc", CHART, {"content.xml": ("office:document-content", '%s' % region)})
# 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
for 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%"')):
mkdoc(n, "odt", TEXT, {"content.xml": ("office:document-content", "hi"),
"styles.xml": ("office:document-styles", '' % attrs)})