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 22:59 +0200

1"""XML helper.""" 

2 

3from typing import Optional 

4import gws 

5import gws.lib.xmlx 

6 

7gws.ext.new.helper('xml') 

8 

9 

10class NamespaceConfig(gws.Config): 

11 """XML Namespace configuration.""" 

12 

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.""" 

23 

24 

25class Config(gws.Config): 

26 """XML helper.""" 

27 

28 namespaces: list[NamespaceConfig] 

29 """List of custom namespaces for XML generation.""" 

30 

31 

32class Object(gws.Node): 

33 namespaces: list[gws.XmlNamespace] 

34 

35 def configure(self): 

36 self.namespaces = [] 

37 for c in self.cfg('namespaces', default=[]): 

38 self.add_namespace(c) 

39 

40 def add_namespace(self, cfg: NamespaceConfig) -> gws.XmlNamespace: 

41 """Add a custom namespace for XML generation.""" 

42 

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 ) 

52 

53 gws.lib.xmlx.namespace.register(ns) 

54 self.namespaces.append(ns) 

55 

56 return ns 

57 

58 def activate(self): 

59 for ns in self.namespaces: 

60 gws.lib.xmlx.namespace.register(ns)