Coverage for gws-app/gws/plugin/xml_helper/__init__.py: 100%
33 statements
« prev ^ index » next coverage.py v7.11.0, created at 2025-10-16 23:09 +0200
« prev ^ index » next coverage.py v7.11.0, created at 2025-10-16 23:09 +0200
1"""XML helper."""
3from typing import Optional
4import gws
5import gws.lib.xmlx
7gws.ext.new.helper('xml')
10class NamespaceConfig(gws.Config):
11 """XML Namespace configuration."""
13 xmlns: str
14 """Default prefix for this Namespace."""
15 uri: gws.Url
16 """Namespace uri."""
17 schemaLocation: Optional[gws.Url]
18 """Namespace schema location."""
19 version: str = ''
20 """Namespace version."""
21 extendsGml: bool = True
22 """Namespace schema extends the GML3 schema."""
25class Config(gws.Config):
26 """XML helper."""
28 namespaces: list[NamespaceConfig]
29 """List of custom namespaces for XML generation."""
32class Object(gws.Node):
33 namespaces: list[gws.XmlNamespace]
35 def configure(self):
36 self.namespaces = []
37 for c in self.cfg('namespaces', default=[]):
38 self.add_namespace(c)
40 def add_namespace(self, cfg: NamespaceConfig) -> gws.XmlNamespace:
41 """Add a custom namespace for XML generation."""
43 xmlns = cfg.get('xmlns')
44 ns = gws.XmlNamespace(
45 xmlns=xmlns,
46 uid=cfg.get('uid') or xmlns,
47 uri=cfg.get('uri'),
48 schemaLocation=cfg.get('schemaLocation') or '',
49 version=cfg.get('version') or '',
50 extendsGml=cfg.get('extendsGml') or True,
51 )
53 gws.lib.xmlx.namespace.register(ns)
54 self.namespaces.append(ns)
56 return ns
58 def activate(self):
59 for ns in self.namespaces:
60 gws.lib.xmlx.namespace.register(ns)