Доки: [MS-DOC] (.doc) в docs/standards/doc + разделение документации docx/doc по подпапкам
PR checks / web (pull_request) Successful in 31s
PR checks / java (pull_request) Successful in 1m6s
Main: tests, deploy, autotests / web (push) Successful in 32s
Main: tests, deploy, autotests / java (push) Successful in 1m4s
Main: tests, deploy, autotests / images-web (push) Successful in 28s
Main: tests, deploy, autotests / images-app (push) Successful in 44s
Main: tests, deploy, autotests / deploy (push) Successful in 1m40s
Main: tests, deploy, autotests / autotests (push) Successful in 40s
Main: tests, deploy, autotests / rollback (push) Has been skipped
PR checks / web (pull_request) Successful in 31s
PR checks / java (pull_request) Successful in 1m6s
Main: tests, deploy, autotests / web (push) Successful in 32s
Main: tests, deploy, autotests / java (push) Successful in 1m4s
Main: tests, deploy, autotests / images-web (push) Successful in 28s
Main: tests, deploy, autotests / images-app (push) Successful in 44s
Main: tests, deploy, autotests / deploy (push) Successful in 1m40s
Main: tests, deploy, autotests / autotests (push) Successful in 40s
Main: tests, deploy, autotests / rollback (push) Has been skipped
This commit was merged in pull request #74.
This commit is contained in:
Executable
+164
@@ -0,0 +1,164 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Валидация XML-частей samples/*.docx против XSD ECMA-376 (transitional).
|
||||
|
||||
Схемы — docs/standards/docx/schema/transitional/ (полный набор ECMA-376 5-го изд.).
|
||||
wml.xsd — word/*.xml (document/styles/settings/header/footer/...);
|
||||
dml-chart.xsd — word/charts/chart*.xml; dml-diagram.xsd — word/diagrams/*;
|
||||
dml-main.xsd — word/theme/theme*.xml.
|
||||
|
||||
Два допущения, оба по ECMA-376 Part 1:
|
||||
* xml:space — wml.xsd/shared-math.xsd ссылаются на атрибут xml:space без
|
||||
импорта XML-namespace (§5.1.2: xml:* — предопределённые атрибуты);
|
||||
XSD-линтеру нужен явный импорт — подмешиваем xml.xsd в копию схемы в tmp.
|
||||
* mc:AlternateContent (§15.2.15) — MC-элементы допускаются в любом
|
||||
расширяемом содержимом и не моделируются схемой; валидируем Fallback-
|
||||
ветку (Choice — расширение, необязательное для conformant-чтения).
|
||||
"""
|
||||
import os, re, sys, shutil, tempfile, zipfile
|
||||
from lxml import etree
|
||||
|
||||
REPO = os.path.dirname(os.path.abspath(__file__))
|
||||
SCHEMA_DIR = os.path.join(REPO, "schema", "transitional")
|
||||
|
||||
XML_XSD = """<?xml version="1.0" encoding="UTF-8"?>
|
||||
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
|
||||
targetNamespace="http://www.w3.org/XML/1998/namespace">
|
||||
<xsd:attribute name="space"><xsd:simpleType>
|
||||
<xsd:restriction base="xsd:NMTOKEN"><xsd:enumeration value="default"/>
|
||||
<xsd:enumeration value="preserve"/></xsd:restriction>
|
||||
</xsd:simpleType></xsd:attribute>
|
||||
<xsd:attribute name="lang" type="xsd:language"/>
|
||||
<xsd:attribute name="base" type="xsd:anyURI"/>
|
||||
<xsd:attribute name="id" type="xsd:ID"/>
|
||||
</xsd:schema>
|
||||
"""
|
||||
|
||||
_cache = {}
|
||||
_tmp = None
|
||||
|
||||
|
||||
def tmpdir():
|
||||
global _tmp
|
||||
if _tmp is None:
|
||||
_tmp = tempfile.mkdtemp(prefix="ecma-xsd-")
|
||||
shutil.copytree(SCHEMA_DIR, _tmp, dirs_exist_ok=True)
|
||||
with open(os.path.join(_tmp, "xml.xsd"), "w") as f:
|
||||
f.write(XML_XSD)
|
||||
for name in ("wml.xsd", "shared-math.xsd"):
|
||||
p = os.path.join(_tmp, name)
|
||||
s = open(p).read()
|
||||
if 'schemaLocation="xml.xsd"' not in s:
|
||||
if "xmlns:xml=" not in s:
|
||||
s = re.sub(r"(<xsd:schema\b[^>]*)(>)",
|
||||
r'\1 xmlns:xml="http://www.w3.org/XML/1998/namespace"\2', s, count=1)
|
||||
s = re.sub(r"(<xsd:schema\b[^>]*>)",
|
||||
r'\1<xsd:import namespace="http://www.w3.org/XML/1998/namespace"'
|
||||
r' schemaLocation="xml.xsd"/>', s, count=1)
|
||||
with open(p, "w") as f:
|
||||
f.write(s)
|
||||
return _tmp
|
||||
|
||||
|
||||
def schema_for(name):
|
||||
if re.match(r"word/(document|footnotes|endnotes|styles|settings|fontTable"
|
||||
r"|webSettings|numbering|(?:header|footer)\d+)\.xml$", name):
|
||||
return "wml.xsd"
|
||||
if re.match(r"word/charts/chart\d+\.xml$", name):
|
||||
return "dml-chart.xsd"
|
||||
# drawing*.xml — dsp: (2008 diagram drawing, [MS-ODRAWXML]) — вне ECMA, не маппится.
|
||||
if re.match(r"word/diagrams/(data|layout|quickStyle|colors)\d+\.xml$", name):
|
||||
return "dml-diagram.xsd"
|
||||
if re.match(r"word/theme/theme\d+\.xml$", name):
|
||||
return "dml-main.xsd"
|
||||
return None
|
||||
|
||||
|
||||
MC = "{http://schemas.openxmlformats.org/markup-compatibility/2006}"
|
||||
|
||||
|
||||
def strip_mc(el):
|
||||
"""MC-обработка по ECMA-376 §15.2.15 перед XSD-валидацией.
|
||||
|
||||
* mc:AlternateContent -> mc:Fallback-дети (§15.2.15.2);
|
||||
* mc:Ignorable (§15.2.15.1) — перечисленные префиксы: consumer, не
|
||||
понимающий namespace, ОБЯЗАН игнорировать его атрибуты и элементы ->
|
||||
снимаем атрибуты и вырезаем элементы этих namespace'ов;
|
||||
* сами mc:*-атрибуты (Ignorable/Preserve/ProcessAttributes) — не часть
|
||||
схемы, снимаем.
|
||||
"""
|
||||
ignorable = set()
|
||||
for e in el.iter():
|
||||
ig = e.get(f"{MC}Ignorable")
|
||||
if ig:
|
||||
for tok in ig.split():
|
||||
ns = e.nsmap.get(tok)
|
||||
if ns:
|
||||
ignorable.add(ns)
|
||||
for e in el.iter():
|
||||
for a in list(e.attrib):
|
||||
ns = a[1:a.index("}")] if a.startswith("{") else ""
|
||||
if a.startswith(MC) or ns in ignorable:
|
||||
del e.attrib[a]
|
||||
for ns in ignorable:
|
||||
for e in list(el.iter(f"{{{ns}}}*")):
|
||||
parent = e.getparent()
|
||||
if parent is not None:
|
||||
parent.remove(e)
|
||||
for ac in list(el.iter(f"{MC}AlternateContent")):
|
||||
parent = ac.getparent()
|
||||
if parent is None:
|
||||
continue
|
||||
fb = ac.find(f"{MC}Fallback")
|
||||
idx = list(parent).index(ac)
|
||||
parent.remove(ac)
|
||||
if fb is not None:
|
||||
for i, child in enumerate(list(fb)):
|
||||
parent.insert(idx + i, child)
|
||||
|
||||
|
||||
def get_schema(fname):
|
||||
if fname not in _cache:
|
||||
_cache[fname] = etree.XMLSchema(etree.parse(os.path.join(tmpdir(), fname)))
|
||||
return _cache[fname]
|
||||
|
||||
|
||||
def validate(docx):
|
||||
problems, skipped = [], []
|
||||
with zipfile.ZipFile(docx) as z:
|
||||
for name in z.namelist():
|
||||
if not name.endswith(".xml"):
|
||||
continue
|
||||
sf = schema_for(name)
|
||||
if sf is None:
|
||||
if name.startswith("word/") and "rels" not in name:
|
||||
skipped.append(name)
|
||||
continue
|
||||
try:
|
||||
doc = etree.fromstring(z.read(name))
|
||||
except etree.XMLSyntaxError as e:
|
||||
problems.append((name, f"not well-formed: {e}"))
|
||||
continue
|
||||
strip_mc(doc)
|
||||
try:
|
||||
schema = get_schema(sf)
|
||||
except Exception as e:
|
||||
problems.append((name, f"schema load failed: {e}"))
|
||||
continue
|
||||
if not schema.validate(doc):
|
||||
for err in schema.error_log:
|
||||
problems.append((name, f"{err.message} (line {err.line})"))
|
||||
return problems, skipped
|
||||
|
||||
|
||||
rc = 0
|
||||
for docx in sys.argv[1:]:
|
||||
problems, skipped = validate(docx)
|
||||
print(("OK " if not problems else "FAIL ") + os.path.basename(docx)
|
||||
+ (f" (без XSD: {', '.join(skipped)})" if skipped else ""))
|
||||
for name, msg in problems[:40]:
|
||||
print(f" {name}: {msg}")
|
||||
if len(problems) > 40:
|
||||
print(f" ... ещё {len(problems)-40}")
|
||||
if problems:
|
||||
rc = 1
|
||||
sys.exit(rc)
|
||||
Reference in New Issue
Block a user