@flipova/foundation 1.14.0 → 2.0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +175 -0
- package/README.md +53 -178
- package/components/ui/accordion/ANIMATION_CUSTOMIZATION.md +162 -0
- package/components/ui/accordion/AccordionAnimatedHeight.tsx +56 -0
- package/components/ui/accordion/AccordionAnimatedIcon.tsx +41 -0
- package/components/ui/accordion/AnimatedHeight.tsx +56 -0
- package/components/ui/accordion/AnimatedIcon.tsx +41 -0
- package/components/ui/accordion/MIGRATION_SUMMARY.md +250 -0
- package/components/ui/accordion/USAGE_EXAMPLE.tsx +179 -0
- package/components/ui/accordion/animation-config.ts +32 -0
- package/components/ui/accordion/index.tsx +253 -0
- package/components/ui/actionsheet/index.tsx +511 -0
- package/components/ui/alert/index.tsx +158 -0
- package/components/ui/alert-dialog/index.tsx +254 -0
- package/components/ui/avatar/index.tsx +139 -0
- package/components/ui/badge/index.tsx +163 -0
- package/components/ui/bottomsheet/index.tsx +477 -0
- package/components/ui/box/index.tsx +19 -0
- package/components/ui/box/index.web.tsx +19 -0
- package/components/ui/box/styles.tsx +10 -0
- package/components/ui/button/index.tsx +264 -0
- package/components/ui/calendar/creator.tsx +366 -0
- package/components/ui/calendar/index.tsx +462 -0
- package/components/ui/calendar/styles.tsx +140 -0
- package/components/ui/card/index.tsx +23 -0
- package/components/ui/card/index.web.tsx +26 -0
- package/components/ui/card/styles.tsx +16 -0
- package/components/ui/center/index.tsx +22 -0
- package/components/ui/center/index.web.tsx +20 -0
- package/components/ui/center/styles.tsx +8 -0
- package/components/ui/checkbox/index.tsx +184 -0
- package/components/ui/date-time-picker/creator.tsx +109 -0
- package/components/ui/date-time-picker/index.tsx +585 -0
- package/components/ui/date-time-picker/index.web.tsx +217 -0
- package/components/ui/date-time-picker/styles.tsx +56 -0
- package/components/ui/divider/index.tsx +40 -0
- package/components/ui/drawer/index.tsx +362 -0
- package/components/ui/fab/index.tsx +215 -0
- package/components/ui/flat-list/index.tsx +21 -0
- package/components/ui/form-control/index.tsx +352 -0
- package/components/ui/gluestack-ui-provider/config.ts +72 -0
- package/components/ui/gluestack-ui-provider/index.tsx +47 -0
- package/components/ui/gluestack-ui-provider/index.web.tsx +96 -0
- package/components/ui/gluestack-ui-provider/script.ts +19 -0
- package/components/ui/gluestack-ui-provider/tokens.d.ts +17 -0
- package/components/ui/gluestack-ui-provider/tokens.js +188 -0
- package/components/ui/gluestack-ui-provider/useGluestackColors.ts +82 -0
- package/components/ui/grid/index.tsx +283 -0
- package/components/ui/grid/index.web.tsx +66 -0
- package/components/ui/grid/styles.tsx +15 -0
- package/components/ui/heading/index.tsx +220 -0
- package/components/ui/heading/index.web.tsx +202 -0
- package/components/ui/heading/styles.tsx +43 -0
- package/components/ui/hstack/index.tsx +27 -0
- package/components/ui/hstack/index.web.tsx +26 -0
- package/components/ui/hstack/styles.tsx +25 -0
- package/components/ui/icon/index.tsx +1588 -0
- package/components/ui/icon/index.web.tsx +1573 -0
- package/components/ui/image/index.tsx +48 -0
- package/components/ui/image-background/index.tsx +25 -0
- package/components/ui/image-viewer/index.tsx +743 -0
- package/components/ui/image-viewer/index.web.tsx +302 -0
- package/components/ui/index.ts +58 -0
- package/components/ui/input/index.tsx +125 -0
- package/components/ui/input-accessory-view/index.tsx +2 -0
- package/components/ui/keyboard-avoiding-view/index.tsx +2 -0
- package/components/ui/link/index.tsx +102 -0
- package/components/ui/liquid-glass/index.tsx +74 -0
- package/components/ui/menu/index.tsx +186 -0
- package/components/ui/modal/index.tsx +237 -0
- package/components/ui/popover/index.tsx +272 -0
- package/components/ui/portal/index.tsx +17 -0
- package/components/ui/pressable/index.tsx +39 -0
- package/components/ui/progress/index.tsx +83 -0
- package/components/ui/radio/index.tsx +217 -0
- package/components/ui/refresh-control/index.tsx +2 -0
- package/components/ui/safe-area-view/index.tsx +2 -0
- package/components/ui/scroll-view/index.tsx +2 -0
- package/components/ui/section-list/index.tsx +2 -0
- package/components/ui/select/index.tsx +277 -0
- package/components/ui/select/select-actionsheet.tsx +562 -0
- package/components/ui/skeleton/index.tsx +105 -0
- package/components/ui/skeleton/index.web.tsx +103 -0
- package/components/ui/skeleton/styles.tsx +35 -0
- package/components/ui/slider/index.tsx +181 -0
- package/components/ui/spinner/index.tsx +40 -0
- package/components/ui/status-bar/index.tsx +2 -0
- package/components/ui/switch/index.tsx +41 -0
- package/components/ui/table/index.tsx +210 -0
- package/components/ui/table/index.web.tsx +157 -0
- package/components/ui/table/styles.tsx +44 -0
- package/components/ui/tabs/TabsAnimatedIndicator.tsx +160 -0
- package/components/ui/tabs/animation-config.ts +3 -0
- package/components/ui/tabs/creator.tsx +206 -0
- package/components/ui/tabs/index.tsx +484 -0
- package/components/ui/text/index.tsx +48 -0
- package/components/ui/text/index.web.tsx +45 -0
- package/components/ui/text/styles.tsx +47 -0
- package/components/ui/textarea/index.tsx +94 -0
- package/components/ui/toast/index.tsx +252 -0
- package/components/ui/tooltip/index.tsx +131 -0
- package/components/ui/view/index.tsx +2 -0
- package/components/ui/virtualized-list/index.tsx +2 -0
- package/components/ui/vstack/index.tsx +28 -0
- package/components/ui/vstack/index.web.tsx +27 -0
- package/components/ui/vstack/styles.tsx +25 -0
- package/design/canonical.index.txt +53 -0
- package/design/documentation.xml +519 -0
- package/design/manifest.xml +90 -0
- package/design/schema.xsd +639 -0
- package/design/themes.xml +204 -0
- package/design/tokens.xml +510 -0
- package/design/tools/design.js +165 -0
- package/design/tools/npm.js +65 -0
- package/design/tools/requirements.txt +35 -0
- package/design/tools/sources/.checker/__init__.py +81 -0
- package/design/tools/sources/.checker/checks.py +198 -0
- package/design/tools/sources/.checker/cli.py +154 -0
- package/design/tools/sources/.checker/io.py +242 -0
- package/design/tools/sources/.checker/model.py +144 -0
- package/design/tools/sources/.checker/registry.py +115 -0
- package/design/tools/sources/.checker/report.py +21 -0
- package/design/tools/sources/.docgen/__init__.py +45 -0
- package/design/tools/sources/.docgen/cli.py +101 -0
- package/design/tools/sources/.docgen/model.py +147 -0
- package/design/tools/sources/.docgen/reference.py +97 -0
- package/design/tools/sources/.docgen/site.py +145 -0
- package/design/tools/sources/.docgen/xsddoc.py +329 -0
- package/design/tools/sources/.generate/__init__.py +35 -0
- package/design/tools/sources/.generate/cli.py +76 -0
- package/design/tools/sources/.generate/common.py +33 -0
- package/design/tools/sources/.generate/config.py +119 -0
- package/design/tools/sources/.generate/model.py +51 -0
- package/design/tools/sources/.generate/tokens.py +141 -0
- package/design/tools/sources/.linter/__init__.py +9 -0
- package/design/tools/sources/.linter/cli.py +44 -0
- package/design/tools/sources/.linter/discovery.py +30 -0
- package/design/tools/sources/.linter/errors.py +45 -0
- package/design/tools/sources/.linter/linter.py +68 -0
- package/design/tools/sources/.xmleditor/__init__.py +74 -0
- package/design/tools/sources/.xmleditor/blocks.py +185 -0
- package/design/tools/sources/.xmleditor/cli.py +141 -0
- package/design/tools/sources/.xmleditor/editor.py +906 -0
- package/design/tools/sources/.xmleditor/model.py +708 -0
- package/design/tools/sources/.xmleditor/widgets.py +1832 -0
- package/design/tools/sources/.xmleditor/xsdmodel.py +753 -0
- package/design/tools/sources/checker.py +55 -0
- package/design/tools/sources/docgen.py +66 -0
- package/design/tools/sources/generate.py +48 -0
- package/design/tools/sources/linter.py +54 -0
- package/design/tools/sources/ruff.toml +12 -0
- package/design/tools/sources/xmleditor.py +62 -0
- package/design/tools/unix/_venv_python.sh +16 -0
- package/design/tools/unix/check.sh +13 -0
- package/design/tools/unix/doc.sh +14 -0
- package/design/tools/unix/gen.sh +14 -0
- package/design/tools/unix/lint.sh +10 -0
- package/design/tools/unix/run.sh +20 -0
- package/design/tools/unix/verify.sh +12 -0
- package/design/tools/unix/xmledit.sh +12 -0
- package/design/tools/windows/check.cmd +12 -0
- package/design/tools/windows/doc.cmd +12 -0
- package/design/tools/windows/gen.cmd +11 -0
- package/design/tools/windows/lint.cmd +9 -0
- package/design/tools/windows/run.cmd +26 -0
- package/design/tools/windows/verify.cmd +10 -0
- package/design/tools/windows/xmledit.cmd +10 -0
- package/dist/chunk-LV4UIEQN.js +9730 -0
- package/dist/chunk-LV4UIEQN.js.map +1 -0
- package/dist/chunk-WNVU3BM2.mjs +9401 -0
- package/dist/chunk-WNVU3BM2.mjs.map +1 -0
- package/dist/index.d.mts +40 -10
- package/dist/index.d.ts +40 -10
- package/dist/index.js +726 -414
- package/dist/index.mjs +1 -5
- package/dist/ui/index.d.mts +6931 -3070
- package/dist/ui/index.d.ts +6931 -3070
- package/dist/ui/index.js +779 -352
- package/dist/ui/index.mjs +1 -2
- package/package.json +66 -149
- package/dist/chunk-2XORADW6.js +0 -24552
- package/dist/chunk-2XORADW6.js.map +0 -1
- package/dist/chunk-52BHHF5M.js +0 -46
- package/dist/chunk-52BHHF5M.js.map +0 -1
- package/dist/chunk-5647JY7Z.mjs +0 -100
- package/dist/chunk-5647JY7Z.mjs.map +0 -1
- package/dist/chunk-5N5HC4ZA.mjs +0 -3
- package/dist/chunk-5N5HC4ZA.mjs.map +0 -1
- package/dist/chunk-7QFZMGL6.js +0 -8583
- package/dist/chunk-7QFZMGL6.js.map +0 -1
- package/dist/chunk-ASZEP37D.js +0 -423
- package/dist/chunk-ASZEP37D.js.map +0 -1
- package/dist/chunk-BH5SU2JQ.mjs +0 -8282
- package/dist/chunk-BH5SU2JQ.mjs.map +0 -1
- package/dist/chunk-BO65GO53.mjs +0 -422
- package/dist/chunk-BO65GO53.mjs.map +0 -1
- package/dist/chunk-BSVXX52E.mjs +0 -413
- package/dist/chunk-BSVXX52E.mjs.map +0 -1
- package/dist/chunk-DNV5UBNF.mjs +0 -3
- package/dist/chunk-DNV5UBNF.mjs.map +0 -1
- package/dist/chunk-EDTAMUO2.js +0 -8310
- package/dist/chunk-EDTAMUO2.js.map +0 -1
- package/dist/chunk-IJTI6EX4.js +0 -4
- package/dist/chunk-IJTI6EX4.js.map +0 -1
- package/dist/chunk-M3YLASM2.js +0 -428
- package/dist/chunk-M3YLASM2.js.map +0 -1
- package/dist/chunk-OXKOGLXE.js +0 -4
- package/dist/chunk-OXKOGLXE.js.map +0 -1
- package/dist/chunk-RQXQK3ZO.mjs +0 -8486
- package/dist/chunk-RQXQK3ZO.mjs.map +0 -1
- package/dist/chunk-SR25V4EA.mjs +0 -40
- package/dist/chunk-SR25V4EA.mjs.map +0 -1
- package/dist/chunk-UB34WFEQ.js +0 -106
- package/dist/chunk-UB34WFEQ.js.map +0 -1
- package/dist/chunk-ZBRWXKDF.mjs +0 -24431
- package/dist/chunk-ZBRWXKDF.mjs.map +0 -1
- package/dist/cli/ds.d.mts +0 -1
- package/dist/cli/ds.d.ts +0 -1
- package/dist/cli/ds.js +0 -602
- package/dist/cli/ds.js.map +0 -1
- package/dist/cli/ds.mjs +0 -579
- package/dist/cli/ds.mjs.map +0 -1
- package/dist/cli/flipova.d.mts +0 -1
- package/dist/cli/flipova.d.ts +0 -1
- package/dist/cli/flipova.js +0 -161
- package/dist/cli/flipova.js.map +0 -1
- package/dist/cli/flipova.mjs +0 -154
- package/dist/cli/flipova.mjs.map +0 -1
- package/dist/config/index.d.mts +0 -123
- package/dist/config/index.d.ts +0 -123
- package/dist/config/index.js +0 -28
- package/dist/config/index.js.map +0 -1
- package/dist/config/index.mjs +0 -3
- package/dist/config/index.mjs.map +0 -1
- package/dist/createTheme-DQeX3s0I.d.mts +0 -78
- package/dist/createTheme-Y4jfz4-u.d.ts +0 -78
- package/dist/index-CuaJA3gM.d.mts +0 -78
- package/dist/index-CuaJA3gM.d.ts +0 -78
- package/dist/registry/index.d.mts +0 -10160
- package/dist/registry/index.d.ts +0 -10160
- package/dist/registry/index.js +0 -392
- package/dist/registry/index.js.map +0 -1
- package/dist/registry/index.mjs +0 -3
- package/dist/registry/index.mjs.map +0 -1
- package/dist/theme/index.d.mts +0 -35
- package/dist/theme/index.d.ts +0 -35
- package/dist/theme/index.js +0 -28
- package/dist/theme/index.js.map +0 -1
- package/dist/theme/index.mjs +0 -3
- package/dist/theme/index.mjs.map +0 -1
- package/dist/tokens/index.d.mts +0 -423
- package/dist/tokens/index.d.ts +0 -423
- package/dist/tokens/index.js +0 -44
- package/dist/tokens/index.js.map +0 -1
- package/dist/tokens/index.mjs +0 -3
- package/dist/tokens/index.mjs.map +0 -1
- package/dist/types/index.d.mts +0 -202
- package/dist/types/index.d.ts +0 -202
- package/dist/types/index.js +0 -4
- package/dist/types/index.js.map +0 -1
- package/dist/types/index.mjs +0 -3
- package/dist/types/index.mjs.map +0 -1
- package/dist/web/index.d.mts +0 -316
- package/dist/web/index.d.ts +0 -316
- package/dist/web/index.js +0 -1500
- package/dist/web/index.js.map +0 -1
- package/dist/web/index.mjs +0 -1396
- package/dist/web/index.mjs.map +0 -1
|
@@ -0,0 +1,753 @@
|
|
|
1
|
+
"""XSD introspection — the single source of truth for the visual editor.
|
|
2
|
+
|
|
3
|
+
Nothing in the editor hardcodes a document structure: every root element,
|
|
4
|
+
allowed child block, occurrence range, attribute, default/fixed value,
|
|
5
|
+
enumeration and documentation string is read from `design/schema.xsd` at
|
|
6
|
+
runtime. Any XML that validates against the schema is therefore editable
|
|
7
|
+
(and creatable) without a single code change.
|
|
8
|
+
|
|
9
|
+
Supported XSD constructs (all used by design/schema.xsd):
|
|
10
|
+
xs:element (top-level, local, ref=, inline complexType/simpleType)
|
|
11
|
+
xs:complexType (named + anonymous), mixed=, xs:sequence/choice/all,
|
|
12
|
+
xs:group (named, ref=), xs:attributeGroup (named, ref=),
|
|
13
|
+
xs:complexContent/xs:extension (base chain), xs:simpleContent/xs:extension,
|
|
14
|
+
xs:attribute (use=, default=, fixed=, inline simpleType),
|
|
15
|
+
xs:simpleType (restriction base=, xs:enumeration, xs:pattern),
|
|
16
|
+
xs:any, xs:annotation/xs:documentation.
|
|
17
|
+
"""
|
|
18
|
+
|
|
19
|
+
from __future__ import annotations
|
|
20
|
+
|
|
21
|
+
from collections import OrderedDict
|
|
22
|
+
from dataclasses import dataclass, field
|
|
23
|
+
from pathlib import Path
|
|
24
|
+
|
|
25
|
+
from lxml import etree
|
|
26
|
+
|
|
27
|
+
XS = "http://www.w3.org/2001/XMLSchema"
|
|
28
|
+
XSI = "http://www.w3.org/2001/XMLSchema-instance"
|
|
29
|
+
UNBOUNDED: int | None = None # sentinel: maxOccurs="unbounded"
|
|
30
|
+
|
|
31
|
+
#: xs:group / xs:attributeGroup cycles are cut after this many expansions.
|
|
32
|
+
_MAX_GROUP_DEPTH = 24
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
def xs(tag: str) -> str:
|
|
36
|
+
"""Qualify a local name in the XML Schema namespace."""
|
|
37
|
+
return f"{{{XS}}}{tag}"
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
def localname(tag: str) -> str:
|
|
41
|
+
"""Bare name of a possibly namespace-qualified tag ('' for comments/PIs)."""
|
|
42
|
+
if not isinstance(tag, str):
|
|
43
|
+
return ""
|
|
44
|
+
return tag.rsplit("}", 1)[-1] if "}" in tag else tag
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
def _occurs(node: etree._Element, attr: str, default: int) -> int | None:
|
|
48
|
+
raw = node.get(attr)
|
|
49
|
+
if raw is None:
|
|
50
|
+
return default
|
|
51
|
+
if raw == "unbounded":
|
|
52
|
+
return UNBOUNDED
|
|
53
|
+
try:
|
|
54
|
+
return int(raw)
|
|
55
|
+
except ValueError:
|
|
56
|
+
return default
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
def _doc_text(node: etree._Element) -> str:
|
|
60
|
+
"""First xs:annotation/xs:documentation text, whitespace-normalised."""
|
|
61
|
+
ann = node.find(xs("annotation"))
|
|
62
|
+
if ann is None:
|
|
63
|
+
return ""
|
|
64
|
+
parts = [d.text for d in ann.findall(xs("documentation")) if d.text]
|
|
65
|
+
return " ".join(" ".join(parts).split())
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
def node_key(node: etree._Element) -> str:
|
|
69
|
+
"""Stable identity for a node inside its document.
|
|
70
|
+
|
|
71
|
+
``id(node)`` must never be used to memoise lxml elements: proxies are
|
|
72
|
+
created on demand and freed aggressively, so the same id can be handed
|
|
73
|
+
out again for a different node. The document path is stable and unique.
|
|
74
|
+
"""
|
|
75
|
+
tree = node.getroottree()
|
|
76
|
+
if tree is not None:
|
|
77
|
+
return tree.getpath(node)
|
|
78
|
+
return f"{node.tag}@{node.sourceline}"
|
|
79
|
+
|
|
80
|
+
|
|
81
|
+
|
|
82
|
+
def _mul(a: int | None, b: int | None) -> int | None:
|
|
83
|
+
"""Combine two occurrence counts (None means unbounded)."""
|
|
84
|
+
if a is None or b is None:
|
|
85
|
+
return UNBOUNDED
|
|
86
|
+
return a * b
|
|
87
|
+
|
|
88
|
+
|
|
89
|
+
def format_occurs(min_occurs: int, max_occurs: int | None) -> str:
|
|
90
|
+
"""Human-readable occurrence range: ``1..1``, ``0..1``, ``1..*``."""
|
|
91
|
+
hi = "*" if max_occurs is UNBOUNDED else str(max_occurs)
|
|
92
|
+
return f"{min_occurs}..{hi}"
|
|
93
|
+
|
|
94
|
+
|
|
95
|
+
# ---------------------------------------------------------------------------
|
|
96
|
+
# Block model — the vocabulary the GUI manipulates
|
|
97
|
+
# ---------------------------------------------------------------------------
|
|
98
|
+
|
|
99
|
+
|
|
100
|
+
@dataclass
|
|
101
|
+
class AttributeSpec:
|
|
102
|
+
"""One ``xs:attribute`` a block accepts."""
|
|
103
|
+
|
|
104
|
+
name: str
|
|
105
|
+
type_name: str = "xs:string"
|
|
106
|
+
use: str = "optional" # required | optional | prohibited
|
|
107
|
+
default: str | None = None
|
|
108
|
+
fixed: str | None = None
|
|
109
|
+
enumerations: list[str] = field(default_factory=list)
|
|
110
|
+
pattern: str | None = None
|
|
111
|
+
documentation: str = ""
|
|
112
|
+
|
|
113
|
+
@property
|
|
114
|
+
def required(self) -> bool:
|
|
115
|
+
return self.use == "required"
|
|
116
|
+
|
|
117
|
+
@property
|
|
118
|
+
def read_only(self) -> bool:
|
|
119
|
+
"""``@fixed`` pins the value: the editor must not offer free editing."""
|
|
120
|
+
return self.fixed is not None
|
|
121
|
+
|
|
122
|
+
@property
|
|
123
|
+
def initial(self) -> str:
|
|
124
|
+
"""Value materialised on a freshly created block."""
|
|
125
|
+
if self.fixed is not None:
|
|
126
|
+
return self.fixed
|
|
127
|
+
if self.default is not None:
|
|
128
|
+
return self.default
|
|
129
|
+
return ""
|
|
130
|
+
|
|
131
|
+
@property
|
|
132
|
+
def is_boolean(self) -> bool:
|
|
133
|
+
return self.type_name in ("xs:boolean", "boolean")
|
|
134
|
+
|
|
135
|
+
|
|
136
|
+
@dataclass
|
|
137
|
+
class LeafSpec:
|
|
138
|
+
"""Simple (text-only) content: the block is a value slot."""
|
|
139
|
+
|
|
140
|
+
base: str = "xs:string"
|
|
141
|
+
enumerations: list[str] = field(default_factory=list)
|
|
142
|
+
pattern: str | None = None
|
|
143
|
+
documentation: str = ""
|
|
144
|
+
|
|
145
|
+
|
|
146
|
+
@dataclass
|
|
147
|
+
class BlockSpec:
|
|
148
|
+
"""A reusable block definition resolved from the schema.
|
|
149
|
+
|
|
150
|
+
Both global ``xs:element`` declarations and local particles resolve to
|
|
151
|
+
this single shape, so the GUI has exactly one notion of "block" and can
|
|
152
|
+
nest any schema-valid combination without a special case anywhere.
|
|
153
|
+
"""
|
|
154
|
+
|
|
155
|
+
name: str
|
|
156
|
+
key: str = ""
|
|
157
|
+
type_name: str = ""
|
|
158
|
+
documentation: str = ""
|
|
159
|
+
attributes: list[AttributeSpec] = field(default_factory=list)
|
|
160
|
+
children: list[ParticleSpec] = field(default_factory=list)
|
|
161
|
+
leaf: LeafSpec | None = None
|
|
162
|
+
mixed: bool = False
|
|
163
|
+
allows_any: bool = False
|
|
164
|
+
is_root: bool = False
|
|
165
|
+
|
|
166
|
+
@property
|
|
167
|
+
def is_leaf(self) -> bool:
|
|
168
|
+
return self.leaf is not None and not self.children
|
|
169
|
+
|
|
170
|
+
@property
|
|
171
|
+
def is_container(self) -> bool:
|
|
172
|
+
return bool(self.children)
|
|
173
|
+
|
|
174
|
+
@property
|
|
175
|
+
def kind(self) -> str:
|
|
176
|
+
"""Profile category (derived, never hardcoded in the GUI)."""
|
|
177
|
+
if self.is_leaf:
|
|
178
|
+
return "value"
|
|
179
|
+
if self.mixed:
|
|
180
|
+
return "text"
|
|
181
|
+
if self.is_container:
|
|
182
|
+
return "container"
|
|
183
|
+
return "empty"
|
|
184
|
+
|
|
185
|
+
@property
|
|
186
|
+
def required_attributes(self) -> list[AttributeSpec]:
|
|
187
|
+
return [a for a in self.attributes if a.required]
|
|
188
|
+
|
|
189
|
+
def child(self, name: str) -> ParticleSpec | None:
|
|
190
|
+
for particle in self.children:
|
|
191
|
+
if particle.name == name:
|
|
192
|
+
return particle
|
|
193
|
+
return None
|
|
194
|
+
|
|
195
|
+
def attribute(self, name: str) -> AttributeSpec | None:
|
|
196
|
+
for attr in self.attributes:
|
|
197
|
+
if attr.name == name:
|
|
198
|
+
return attr
|
|
199
|
+
return None
|
|
200
|
+
|
|
201
|
+
@property
|
|
202
|
+
def summary(self) -> str:
|
|
203
|
+
"""One-line description of the block's shape (type + child count)."""
|
|
204
|
+
parts = []
|
|
205
|
+
if self.type_name:
|
|
206
|
+
parts.append(self.type_name)
|
|
207
|
+
if self.children:
|
|
208
|
+
parts.append(f"{len(self.children)} child block(s)")
|
|
209
|
+
elif self.leaf is not None:
|
|
210
|
+
parts.append(self.leaf.base)
|
|
211
|
+
return " · ".join(parts)
|
|
212
|
+
|
|
213
|
+
|
|
214
|
+
@dataclass
|
|
215
|
+
class ParticleSpec:
|
|
216
|
+
"""A block slot inside a content model, carrying its occurrence range."""
|
|
217
|
+
|
|
218
|
+
block: BlockSpec
|
|
219
|
+
min_occurs: int = 1
|
|
220
|
+
max_occurs: int | None = 1
|
|
221
|
+
via_choice: bool = False
|
|
222
|
+
via_ref: bool = False
|
|
223
|
+
|
|
224
|
+
@property
|
|
225
|
+
def name(self) -> str:
|
|
226
|
+
return self.block.name
|
|
227
|
+
|
|
228
|
+
@property
|
|
229
|
+
def repeatable(self) -> bool:
|
|
230
|
+
return self.max_occurs is None or self.max_occurs > 1
|
|
231
|
+
|
|
232
|
+
@property
|
|
233
|
+
def required(self) -> bool:
|
|
234
|
+
return self.min_occurs >= 1
|
|
235
|
+
|
|
236
|
+
@property
|
|
237
|
+
def occurs_label(self) -> str:
|
|
238
|
+
return format_occurs(self.min_occurs, self.max_occurs)
|
|
239
|
+
|
|
240
|
+
@property
|
|
241
|
+
def cardinality(self) -> str:
|
|
242
|
+
"""UML-ish cardinality hint shown on the diagram connector."""
|
|
243
|
+
if self.max_occurs is UNBOUNDED:
|
|
244
|
+
return f"{self.min_occurs}..*"
|
|
245
|
+
if self.min_occurs == self.max_occurs:
|
|
246
|
+
return str(self.min_occurs)
|
|
247
|
+
return self.occurs_label
|
|
248
|
+
|
|
249
|
+
|
|
250
|
+
# ---------------------------------------------------------------------------
|
|
251
|
+
# Schema model
|
|
252
|
+
# ---------------------------------------------------------------------------
|
|
253
|
+
|
|
254
|
+
|
|
255
|
+
@dataclass
|
|
256
|
+
class SchemaModel:
|
|
257
|
+
"""Everything the editor knows, read from ``schema.xsd`` at runtime."""
|
|
258
|
+
|
|
259
|
+
path: Path
|
|
260
|
+
target_namespace: str = ""
|
|
261
|
+
element_form_default: str = "unqualified"
|
|
262
|
+
attribute_form_default: str = "unqualified"
|
|
263
|
+
roots: list[BlockSpec] = field(default_factory=list)
|
|
264
|
+
blocks: dict[str, BlockSpec] = field(default_factory=dict)
|
|
265
|
+
_root_index: dict[str, BlockSpec] = field(default_factory=dict, repr=False)
|
|
266
|
+
|
|
267
|
+
# -- lookup ----------------------------------------------------------
|
|
268
|
+
|
|
269
|
+
@property
|
|
270
|
+
def root_names(self) -> list[str]:
|
|
271
|
+
return [b.name for b in self.roots]
|
|
272
|
+
|
|
273
|
+
def root(self, name: str) -> BlockSpec | None:
|
|
274
|
+
return self._root_index.get(name)
|
|
275
|
+
|
|
276
|
+
def block(self, name: str) -> BlockSpec | None:
|
|
277
|
+
"""Resolve a block by qualified key, named type, or plain name.
|
|
278
|
+
|
|
279
|
+
Element names repeat across content models, so the qualified ``key``
|
|
280
|
+
(``Type.element``) is authoritative; the plain-name lookup exists for
|
|
281
|
+
palette/CLI convenience and returns the first match.
|
|
282
|
+
"""
|
|
283
|
+
if name in self.blocks:
|
|
284
|
+
return self.blocks[name]
|
|
285
|
+
if name in self._root_index:
|
|
286
|
+
return self._root_index[name]
|
|
287
|
+
for candidate in self._root_index.values():
|
|
288
|
+
if candidate.type_name == name:
|
|
289
|
+
return candidate
|
|
290
|
+
for candidate in self.blocks.values():
|
|
291
|
+
if candidate.type_name == name:
|
|
292
|
+
return candidate
|
|
293
|
+
for candidate in self.blocks.values():
|
|
294
|
+
if candidate.name == name:
|
|
295
|
+
return candidate
|
|
296
|
+
return None
|
|
297
|
+
|
|
298
|
+
def blocks_named(self, name: str) -> list[BlockSpec]:
|
|
299
|
+
"""Every block declaration sharing a local name (context-free query)."""
|
|
300
|
+
found = [b for b in self._root_index.values() if b.name == name]
|
|
301
|
+
found.extend(b for b in self.blocks.values() if b.name == name)
|
|
302
|
+
return found
|
|
303
|
+
|
|
304
|
+
def block_for_element(self, element_name: str) -> BlockSpec | None:
|
|
305
|
+
"""Map an XML element name (possibly namespace-qualified) to a block."""
|
|
306
|
+
bare = element_name.rsplit("}", 1)[-1] if "}" in element_name else element_name
|
|
307
|
+
return self._root_index.get(bare) or self.blocks.get(bare)
|
|
308
|
+
|
|
309
|
+
# -- profile ---------------------------------------------------------
|
|
310
|
+
|
|
311
|
+
def profile(self) -> OrderedDict[str, list[BlockSpec]]:
|
|
312
|
+
"""Block profile: categories derived purely from the schema shape.
|
|
313
|
+
|
|
314
|
+
This is what feeds the palette; adding a construct to the XSD grows
|
|
315
|
+
the profile automatically.
|
|
316
|
+
"""
|
|
317
|
+
reachable: OrderedDict[str, BlockSpec] = OrderedDict()
|
|
318
|
+
repeatable: OrderedDict[str, BlockSpec] = OrderedDict()
|
|
319
|
+
|
|
320
|
+
def visit(block: BlockSpec) -> None:
|
|
321
|
+
ident = block.key or block.name
|
|
322
|
+
if ident in reachable:
|
|
323
|
+
return
|
|
324
|
+
reachable[ident] = block
|
|
325
|
+
for particle in block.children:
|
|
326
|
+
if particle.repeatable:
|
|
327
|
+
repeatable[particle.block.key or particle.name] = particle.block
|
|
328
|
+
visit(particle.block)
|
|
329
|
+
|
|
330
|
+
for root in self.roots:
|
|
331
|
+
visit(root)
|
|
332
|
+
|
|
333
|
+
root_names = {b.name for b in self.roots}
|
|
334
|
+
categories: OrderedDict[str, list[BlockSpec]] = OrderedDict()
|
|
335
|
+
categories["Roots"] = list(self.roots)
|
|
336
|
+
containers = [b for b in reachable.values() if b.is_container and b.name not in root_names]
|
|
337
|
+
if containers:
|
|
338
|
+
categories["Containers"] = containers
|
|
339
|
+
values = [b for b in reachable.values() if b.is_leaf and b.name not in root_names]
|
|
340
|
+
if values:
|
|
341
|
+
categories["Values"] = values
|
|
342
|
+
texts = [b for b in reachable.values() if b.mixed and b.name not in root_names]
|
|
343
|
+
if texts:
|
|
344
|
+
categories["Text (mixed)"] = texts
|
|
345
|
+
repeats = [b for b in repeatable.values() if b.name not in root_names]
|
|
346
|
+
if repeats:
|
|
347
|
+
categories["Repeatable (collections)"] = repeats
|
|
348
|
+
return categories
|
|
349
|
+
|
|
350
|
+
def profile_blocks(self) -> list[BlockSpec]:
|
|
351
|
+
"""Flat, de-duplicated list of every block in the profile."""
|
|
352
|
+
seen: OrderedDict[str, BlockSpec] = OrderedDict()
|
|
353
|
+
for blocks in self.profile().values():
|
|
354
|
+
for block in blocks:
|
|
355
|
+
seen.setdefault(block.key or block.name, block)
|
|
356
|
+
return list(seen.values())
|
|
357
|
+
|
|
358
|
+
|
|
359
|
+
# ---------------------------------------------------------------------------
|
|
360
|
+
# Loader: XSD -> SchemaModel
|
|
361
|
+
# ---------------------------------------------------------------------------
|
|
362
|
+
|
|
363
|
+
|
|
364
|
+
class _SchemaLoader:
|
|
365
|
+
"""Walks an XSD document once and produces the immutable block index."""
|
|
366
|
+
|
|
367
|
+
def __init__(self, root: etree._Element) -> None:
|
|
368
|
+
self.root = root
|
|
369
|
+
self.target_namespace = root.get("targetNamespace", "")
|
|
370
|
+
self.element_form_default = root.get("elementFormDefault", "unqualified")
|
|
371
|
+
self.attribute_form_default = root.get("attributeFormDefault", "unqualified")
|
|
372
|
+
|
|
373
|
+
self.elements = self._index("element")
|
|
374
|
+
self.complex_types = self._index("complexType")
|
|
375
|
+
self.simple_types = self._index("simpleType")
|
|
376
|
+
self.groups = self._index("group")
|
|
377
|
+
self.attribute_groups = self._index("attributeGroup")
|
|
378
|
+
|
|
379
|
+
self._nodes: dict[str, BlockSpec] = {}
|
|
380
|
+
self._types: dict[str, BlockSpec] = {}
|
|
381
|
+
self._named: dict[str, BlockSpec] = {}
|
|
382
|
+
self._leaves: dict[str, LeafSpec] = {}
|
|
383
|
+
self._all: OrderedDict[str, BlockSpec] = OrderedDict()
|
|
384
|
+
|
|
385
|
+
# -- index helpers ---------------------------------------------------
|
|
386
|
+
|
|
387
|
+
def _index(self, tag: str) -> dict[str, etree._Element]:
|
|
388
|
+
found: dict[str, etree._Element] = {}
|
|
389
|
+
for node in self.root.findall(xs(tag)):
|
|
390
|
+
name = node.get("name")
|
|
391
|
+
if name:
|
|
392
|
+
found[name] = node
|
|
393
|
+
return found
|
|
394
|
+
|
|
395
|
+
# -- entry point -----------------------------------------------------
|
|
396
|
+
|
|
397
|
+
def build(self, path: Path) -> SchemaModel:
|
|
398
|
+
roots: list[BlockSpec] = []
|
|
399
|
+
for node in self.root.findall(xs("element")):
|
|
400
|
+
name = node.get("name")
|
|
401
|
+
if not name:
|
|
402
|
+
continue
|
|
403
|
+
roots.append(self.element_block(node, name, is_root=True))
|
|
404
|
+
|
|
405
|
+
# An element block whose named type is recursive (type -> nested
|
|
406
|
+
# element -> same type) adopts the type block while it is still under
|
|
407
|
+
# construction, so its snapshot misses every particle/attribute added
|
|
408
|
+
# after the recursive reference (e.g. NodeTreeType's own <node>
|
|
409
|
+
# particle and its @role/@element attributes). Every root has been
|
|
410
|
+
# walked by now: re-adopt from the finished type blocks to refresh
|
|
411
|
+
# those stale snapshots. Non-recursive blocks re-adopt identical
|
|
412
|
+
# content, so the pass is idempotent.
|
|
413
|
+
for block in self._nodes.values():
|
|
414
|
+
if not block.type_name:
|
|
415
|
+
continue
|
|
416
|
+
final = self._named.get(block.type_name)
|
|
417
|
+
if final is not None and final is not block:
|
|
418
|
+
self._adopt(block, final)
|
|
419
|
+
|
|
420
|
+
model = SchemaModel(
|
|
421
|
+
path=path,
|
|
422
|
+
target_namespace=self.target_namespace,
|
|
423
|
+
element_form_default=self.element_form_default,
|
|
424
|
+
attribute_form_default=self.attribute_form_default,
|
|
425
|
+
roots=roots,
|
|
426
|
+
blocks=dict(self._all),
|
|
427
|
+
)
|
|
428
|
+
model._root_index = {b.name: b for b in roots}
|
|
429
|
+
for block in self._types.values():
|
|
430
|
+
if block.key:
|
|
431
|
+
model.blocks.setdefault(block.key, block)
|
|
432
|
+
return model
|
|
433
|
+
|
|
434
|
+
def _register(self, block: BlockSpec) -> BlockSpec:
|
|
435
|
+
if block.name:
|
|
436
|
+
self._all.setdefault(block.key or block.name, block)
|
|
437
|
+
return block
|
|
438
|
+
|
|
439
|
+
# -- elements --------------------------------------------------------
|
|
440
|
+
|
|
441
|
+
def element_block(
|
|
442
|
+
self,
|
|
443
|
+
node: etree._Element,
|
|
444
|
+
name: str,
|
|
445
|
+
is_root: bool = False,
|
|
446
|
+
parent_key: str = "",
|
|
447
|
+
) -> BlockSpec:
|
|
448
|
+
key = node_key(node)
|
|
449
|
+
cached = self._nodes.get(key)
|
|
450
|
+
if cached is not None:
|
|
451
|
+
if is_root:
|
|
452
|
+
cached.is_root = True
|
|
453
|
+
return cached
|
|
454
|
+
|
|
455
|
+
qualified = name if (is_root or not parent_key) else f"{parent_key}.{name}"
|
|
456
|
+
type_name = node.get("type") or ""
|
|
457
|
+
block = BlockSpec(
|
|
458
|
+
name=name,
|
|
459
|
+
key=qualified,
|
|
460
|
+
type_name=type_name,
|
|
461
|
+
documentation=_doc_text(node),
|
|
462
|
+
is_root=is_root,
|
|
463
|
+
)
|
|
464
|
+
# Registered before the content model is walked so a recursive
|
|
465
|
+
# reference (element -> ... -> same element) resolves to this very
|
|
466
|
+
# object instead of recursing forever.
|
|
467
|
+
self._nodes[key] = block
|
|
468
|
+
|
|
469
|
+
if type_name:
|
|
470
|
+
if type_name in self.complex_types:
|
|
471
|
+
named_type = self.complex_type_block(
|
|
472
|
+
self.complex_types[type_name], type_name, named=True)
|
|
473
|
+
self._adopt(block, named_type)
|
|
474
|
+
elif type_name in self.simple_types:
|
|
475
|
+
block.leaf = self.simple_type_leaf(self.simple_types[type_name])
|
|
476
|
+
else:
|
|
477
|
+
block.leaf = LeafSpec(base=type_name)
|
|
478
|
+
else:
|
|
479
|
+
inline_complex = node.find(xs("complexType"))
|
|
480
|
+
inline_simple = node.find(xs("simpleType"))
|
|
481
|
+
if inline_complex is not None:
|
|
482
|
+
self._adopt(block, self.complex_type_block(inline_complex, name))
|
|
483
|
+
elif inline_simple is not None:
|
|
484
|
+
block.leaf = self.simple_type_leaf(inline_simple)
|
|
485
|
+
|
|
486
|
+
return self._register(block)
|
|
487
|
+
|
|
488
|
+
def _adopt(self, block: BlockSpec, base: BlockSpec) -> None:
|
|
489
|
+
"""Copy an anonymous/named type's shape onto an element block."""
|
|
490
|
+
block.attributes = list(base.attributes)
|
|
491
|
+
block.children = list(base.children)
|
|
492
|
+
block.mixed = base.mixed
|
|
493
|
+
block.allows_any = base.allows_any
|
|
494
|
+
if base.leaf is not None:
|
|
495
|
+
block.leaf = base.leaf
|
|
496
|
+
|
|
497
|
+
# -- complex types ---------------------------------------------------
|
|
498
|
+
|
|
499
|
+
def complex_type_block(
|
|
500
|
+
self, node: etree._Element, type_name: str, named: bool = False
|
|
501
|
+
) -> BlockSpec:
|
|
502
|
+
"""Resolve a complexType (named or anonymous inline).
|
|
503
|
+
|
|
504
|
+
Anonymous inline types are cached by node path, never by name: two
|
|
505
|
+
content models may declare the same element name (``fixedShape`` in
|
|
506
|
+
the header contract and in the logic contract) with different shapes,
|
|
507
|
+
and a name-keyed cache would silently merge them.
|
|
508
|
+
"""
|
|
509
|
+
cache_key = node_key(node)
|
|
510
|
+
cached = self._types.get(cache_key)
|
|
511
|
+
if cached is not None:
|
|
512
|
+
return cached
|
|
513
|
+
if named:
|
|
514
|
+
existing = self._named.get(type_name)
|
|
515
|
+
if existing is not None:
|
|
516
|
+
# Recursive type (e.g. NodeType -> node -> NodeType): hand back
|
|
517
|
+
# the block still under construction so nesting stays complete
|
|
518
|
+
# instead of returning an empty stub that hides allowed blocks.
|
|
519
|
+
return existing
|
|
520
|
+
|
|
521
|
+
block = BlockSpec(name=type_name, key=type_name if named else "",
|
|
522
|
+
type_name=type_name,
|
|
523
|
+
documentation=_doc_text(node),
|
|
524
|
+
mixed=(node.get("mixed") == "true"))
|
|
525
|
+
self._types[cache_key] = block
|
|
526
|
+
if named:
|
|
527
|
+
self._named[type_name] = block
|
|
528
|
+
|
|
529
|
+
complex_content = node.find(xs("complexContent"))
|
|
530
|
+
simple_content = node.find(xs("simpleContent"))
|
|
531
|
+
|
|
532
|
+
if complex_content is not None:
|
|
533
|
+
if complex_content.get("mixed") == "true":
|
|
534
|
+
block.mixed = True
|
|
535
|
+
derived = complex_content.find(xs("extension"))
|
|
536
|
+
if derived is None:
|
|
537
|
+
derived = complex_content.find(xs("restriction"))
|
|
538
|
+
if derived is not None:
|
|
539
|
+
self._merge_base(block, derived.get("base"))
|
|
540
|
+
self._read_particles(block, derived, 1, 1)
|
|
541
|
+
self._read_attributes(block, derived)
|
|
542
|
+
elif simple_content is not None:
|
|
543
|
+
derived = simple_content.find(xs("extension"))
|
|
544
|
+
if derived is None:
|
|
545
|
+
derived = simple_content.find(xs("restriction"))
|
|
546
|
+
if derived is not None:
|
|
547
|
+
block.leaf = self._leaf_from_base(derived.get("base"))
|
|
548
|
+
self._read_attributes(block, derived)
|
|
549
|
+
else:
|
|
550
|
+
self._read_particles(block, node, 1, 1)
|
|
551
|
+
self._read_attributes(block, node)
|
|
552
|
+
return block
|
|
553
|
+
|
|
554
|
+
|
|
555
|
+
def _merge_base(self, block: BlockSpec, base_name: str | None) -> None:
|
|
556
|
+
"""Inherit attributes/particles from a named base type (xs:extension)."""
|
|
557
|
+
if not base_name:
|
|
558
|
+
return
|
|
559
|
+
if base_name in self.complex_types:
|
|
560
|
+
base = self.complex_type_block(
|
|
561
|
+
self.complex_types[base_name], base_name, named=True)
|
|
562
|
+
known = {a.name for a in block.attributes}
|
|
563
|
+
block.attributes.extend(a for a in base.attributes if a.name not in known)
|
|
564
|
+
block.children.extend(base.children)
|
|
565
|
+
block.allows_any = block.allows_any or base.allows_any
|
|
566
|
+
if base.mixed:
|
|
567
|
+
block.mixed = True
|
|
568
|
+
elif base_name in self.simple_types:
|
|
569
|
+
block.leaf = self.simple_type_leaf(self.simple_types[base_name])
|
|
570
|
+
else:
|
|
571
|
+
block.leaf = LeafSpec(base=base_name)
|
|
572
|
+
|
|
573
|
+
# -- attributes ------------------------------------------------------
|
|
574
|
+
|
|
575
|
+
def _read_attributes(self, block: BlockSpec, node: etree._Element) -> None:
|
|
576
|
+
known = {a.name for a in block.attributes}
|
|
577
|
+
for attr_node in list(node.findall(xs("attribute"))):
|
|
578
|
+
spec = self.attribute_spec(attr_node)
|
|
579
|
+
if spec is not None and spec.name not in known:
|
|
580
|
+
block.attributes.append(spec)
|
|
581
|
+
known.add(spec.name)
|
|
582
|
+
for group_node in node.findall(xs("attributeGroup")):
|
|
583
|
+
ref = group_node.get("ref")
|
|
584
|
+
group = self.attribute_groups.get(ref) if ref else group_node
|
|
585
|
+
if group is None:
|
|
586
|
+
continue
|
|
587
|
+
for attr_node in group.findall(xs("attribute")):
|
|
588
|
+
spec = self.attribute_spec(attr_node)
|
|
589
|
+
if spec is not None and spec.name not in known:
|
|
590
|
+
block.attributes.append(spec)
|
|
591
|
+
known.add(spec.name)
|
|
592
|
+
|
|
593
|
+
def attribute_spec(self, node: etree._Element) -> AttributeSpec | None:
|
|
594
|
+
name = node.get("name")
|
|
595
|
+
if not name:
|
|
596
|
+
return None
|
|
597
|
+
type_name = node.get("type") or "xs:string"
|
|
598
|
+
spec = AttributeSpec(
|
|
599
|
+
name=name,
|
|
600
|
+
type_name=type_name,
|
|
601
|
+
use=node.get("use", "optional"),
|
|
602
|
+
default=node.get("default"),
|
|
603
|
+
fixed=node.get("fixed"),
|
|
604
|
+
documentation=_doc_text(node),
|
|
605
|
+
)
|
|
606
|
+
if type_name in self.simple_types:
|
|
607
|
+
leaf = self.simple_type_leaf(self.simple_types[type_name])
|
|
608
|
+
spec.enumerations = list(leaf.enumerations)
|
|
609
|
+
spec.pattern = leaf.pattern
|
|
610
|
+
inline = node.find(xs("simpleType"))
|
|
611
|
+
if inline is not None:
|
|
612
|
+
leaf = self.simple_type_leaf(inline)
|
|
613
|
+
spec.enumerations = list(leaf.enumerations)
|
|
614
|
+
spec.pattern = leaf.pattern
|
|
615
|
+
return spec
|
|
616
|
+
|
|
617
|
+
def _leaf_from_base(self, base_name: str | None) -> LeafSpec:
|
|
618
|
+
if base_name and base_name in self.simple_types:
|
|
619
|
+
return self.simple_type_leaf(self.simple_types[base_name])
|
|
620
|
+
return LeafSpec(base=base_name or "xs:string")
|
|
621
|
+
|
|
622
|
+
# -- simple types ----------------------------------------------------
|
|
623
|
+
|
|
624
|
+
def simple_type_leaf(self, node: etree._Element) -> LeafSpec:
|
|
625
|
+
key = node_key(node)
|
|
626
|
+
cached = self._leaves.get(key)
|
|
627
|
+
if cached is not None:
|
|
628
|
+
return cached
|
|
629
|
+
|
|
630
|
+
leaf = LeafSpec(documentation=_doc_text(node))
|
|
631
|
+
restriction = node.find(xs("restriction"))
|
|
632
|
+
if restriction is not None:
|
|
633
|
+
leaf.base = restriction.get("base") or "xs:string"
|
|
634
|
+
leaf.enumerations = self._enumerations(restriction)
|
|
635
|
+
pattern_node = restriction.find(xs("pattern"))
|
|
636
|
+
if pattern_node is not None:
|
|
637
|
+
leaf.pattern = pattern_node.get("value")
|
|
638
|
+
nested = restriction.find(xs("simpleType"))
|
|
639
|
+
if nested is not None and not leaf.enumerations:
|
|
640
|
+
inner = self.simple_type_leaf(nested)
|
|
641
|
+
leaf.enumerations = list(inner.enumerations)
|
|
642
|
+
leaf.pattern = leaf.pattern or inner.pattern
|
|
643
|
+
union = node.find(xs("union"))
|
|
644
|
+
if union is not None:
|
|
645
|
+
leaf.base = "union"
|
|
646
|
+
for member in union.findall(xs("simpleType")):
|
|
647
|
+
leaf.enumerations.extend(self.simple_type_leaf(member).enumerations)
|
|
648
|
+
for member_name in (union.get("memberTypes") or "").split():
|
|
649
|
+
if member_name in self.simple_types:
|
|
650
|
+
leaf.enumerations.extend(
|
|
651
|
+
self.simple_type_leaf(self.simple_types[member_name]).enumerations)
|
|
652
|
+
if node.find(xs("list")) is not None:
|
|
653
|
+
leaf.base = "list"
|
|
654
|
+
|
|
655
|
+
self._leaves[key] = leaf
|
|
656
|
+
return leaf
|
|
657
|
+
|
|
658
|
+
def _enumerations(self, restriction: etree._Element) -> list[str]:
|
|
659
|
+
values: list[str] = []
|
|
660
|
+
for enum_node in restriction.findall(xs("enumeration")):
|
|
661
|
+
value = enum_node.get("value")
|
|
662
|
+
if value is not None:
|
|
663
|
+
values.append(value)
|
|
664
|
+
return values
|
|
665
|
+
|
|
666
|
+
# -- particles -------------------------------------------------------
|
|
667
|
+
|
|
668
|
+
def _read_particles(
|
|
669
|
+
self,
|
|
670
|
+
block: BlockSpec,
|
|
671
|
+
node: etree._Element,
|
|
672
|
+
mult_min: int,
|
|
673
|
+
mult_max: int | None,
|
|
674
|
+
via_choice: bool = False,
|
|
675
|
+
depth: int = 0,
|
|
676
|
+
) -> None:
|
|
677
|
+
"""Flatten a content model, multiplying occurrence ranges on the way.
|
|
678
|
+
|
|
679
|
+
Nested ``xs:sequence`` / ``xs:choice`` / ``xs:group ref`` are resolved
|
|
680
|
+
so the GUI sees one flat list of blocks per parent, each with its real
|
|
681
|
+
cardinality — no schema construct is left unimplemented.
|
|
682
|
+
"""
|
|
683
|
+
if depth > _MAX_GROUP_DEPTH:
|
|
684
|
+
return
|
|
685
|
+
for child in node:
|
|
686
|
+
tag = localname(child.tag)
|
|
687
|
+
if tag in ("sequence", "all", "choice"):
|
|
688
|
+
child_min = _mul(mult_min, _occurs(child, "minOccurs", 1))
|
|
689
|
+
child_max = _mul(mult_max, _occurs(child, "maxOccurs", 1))
|
|
690
|
+
self._read_particles(block, child, child_min or 0, child_max,
|
|
691
|
+
via_choice or tag == "choice", depth + 1)
|
|
692
|
+
elif tag == "group":
|
|
693
|
+
ref = child.get("ref")
|
|
694
|
+
group_node = self.groups.get(ref) if ref else child
|
|
695
|
+
if group_node is None:
|
|
696
|
+
continue
|
|
697
|
+
child_min = _mul(mult_min, _occurs(child, "minOccurs", 1))
|
|
698
|
+
child_max = _mul(mult_max, _occurs(child, "maxOccurs", 1))
|
|
699
|
+
self._read_particles(block, group_node, child_min or 0, child_max,
|
|
700
|
+
via_choice, depth + 1)
|
|
701
|
+
elif tag == "element":
|
|
702
|
+
particle = self._particle(child, mult_min, mult_max, via_choice, block.key)
|
|
703
|
+
if particle is not None and block.child(particle.name) is None:
|
|
704
|
+
block.children.append(particle)
|
|
705
|
+
elif tag == "any":
|
|
706
|
+
block.allows_any = True
|
|
707
|
+
|
|
708
|
+
def _particle(
|
|
709
|
+
self,
|
|
710
|
+
node: etree._Element,
|
|
711
|
+
mult_min: int,
|
|
712
|
+
mult_max: int | None,
|
|
713
|
+
via_choice: bool,
|
|
714
|
+
parent_key: str = "",
|
|
715
|
+
) -> ParticleSpec | None:
|
|
716
|
+
ref = node.get("ref")
|
|
717
|
+
name = ref or node.get("name")
|
|
718
|
+
if not name:
|
|
719
|
+
return None
|
|
720
|
+
|
|
721
|
+
if ref:
|
|
722
|
+
target = self.elements.get(ref)
|
|
723
|
+
if target is None:
|
|
724
|
+
return None
|
|
725
|
+
block = self.element_block(target, ref)
|
|
726
|
+
else:
|
|
727
|
+
block = self.element_block(node, name, parent_key=parent_key)
|
|
728
|
+
|
|
729
|
+
min_occurs = _mul(_occurs(node, "minOccurs", 1), mult_min) or 0
|
|
730
|
+
if via_choice:
|
|
731
|
+
# In an xs:choice the alternatives are optional individually: the
|
|
732
|
+
# choice as a whole requires one of them, never each of them.
|
|
733
|
+
min_occurs = 0
|
|
734
|
+
max_occurs = _mul(_occurs(node, "maxOccurs", 1), mult_max)
|
|
735
|
+
return ParticleSpec(
|
|
736
|
+
block=block,
|
|
737
|
+
min_occurs=min_occurs,
|
|
738
|
+
max_occurs=max_occurs,
|
|
739
|
+
via_choice=via_choice,
|
|
740
|
+
via_ref=bool(ref),
|
|
741
|
+
)
|
|
742
|
+
|
|
743
|
+
|
|
744
|
+
def load_schema(path: Path) -> SchemaModel:
|
|
745
|
+
"""Parse a schema once and return its immutable, fully-expanded model."""
|
|
746
|
+
parser = etree.XMLParser(remove_comments=False, resolve_entities=False)
|
|
747
|
+
tree = etree.parse(str(path), parser)
|
|
748
|
+
loader = _SchemaLoader(tree.getroot())
|
|
749
|
+
return loader.build(Path(path).resolve())
|
|
750
|
+
|
|
751
|
+
|
|
752
|
+
|
|
753
|
+
|