@@ -27,6 +27,7 @@ class XmlSchema:
2727 '1' : ('1' , '1' ),
2828 '+' : ('1' , 'unbounded' ),
2929 '*' : ('0' , 'unbounded' ),
30+ '-1' : ('0' , 'unbounded' ),
3031 }
3132
3233 def __init__ (self , sdf_root_dir : str ):
@@ -38,16 +39,15 @@ def _indent_lines(self, lines: List[str], indent) -> List[str]:
3839 def _get_attribute (self , element : ElementTree .Element , attrib : str ) -> Optional [str ]:
3940 return element .attrib [attrib ] if attrib in element .attrib else None
4041
41- def _is_std_type (self , element : ElementTree . Element ) -> bool :
42- return self . _get_attribute ( element , 'type' ) in self .SDF_TYPES_TO_XSD_STD_TYPES .keys ()
42+ def _is_std_type (self , sdf_type : str ) -> bool :
43+ return sdf_type in self .SDF_TYPES_TO_XSD_STD_TYPES .keys ()
4344
44- def _xsd_type_string (self , element : ElementTree .Element ) -> Optional [str ]:
45- if self ._is_std_type (element ):
46- sdf_type = self ._get_attribute (element , 'type' )
47- if sdf_type :
48- xsd_type = self .SDF_TYPES_TO_XSD_STD_TYPES [sdf_type ]
49- return 'xsd:' + xsd_type
50- return None
45+ def _xsd_type_string (self , sdf_type : str ) -> Optional [str ]:
46+ if self ._is_std_type (sdf_type ):
47+ xsd_type = self .SDF_TYPES_TO_XSD_STD_TYPES [sdf_type ]
48+ return 'xsd:' + xsd_type
49+ else :
50+ return None
5151
5252 def _print_documentation (self , element : ElementTree .Element ) -> List [str ]:
5353 lines = []
@@ -85,31 +85,37 @@ def _print_include_ref(self, element: ElementTree.Element) -> List[str]:
8585 print (f'Invalid sdf included { filename } ' )
8686 return lines
8787
88- def _print_element (self , element : ElementTree .Element ) -> List [str ]:
88+ def _print_plugin_element (self , element : ElementTree .Element ) -> List [str ]:
8989 lines = []
90-
9190 # Short circuit for plugin.sdf copy_data element
9291 if 'copy_data' in element .attrib :
9392 lines .append ('<xsd:sequence>' )
9493 lines .append (" <xsd:any minOccurs='0' maxOccurs='unbounded' processContents='lax'/>" )
9594 lines .append ('</xsd:sequence>' )
96- return lines
95+ return lines
96+
97+ def _print_element (self , element : ElementTree .Element ) -> List [str ]:
98+ lines = []
9799
98100 elem_name = self ._get_attribute (element , 'name' )
99- elem_type = self ._xsd_type_string (element )
101+ elem_type = self ._get_attribute (element , 'type' )
100102 elem_reqd = self ._get_attribute (element , 'required' )
101103
102- minOccurs , maxOccurs = self .SDF_REQUIRED_TO_MIN_MAX_OCCURS [ elem_reqd ]
103- lines . append ( f"<xsd:choice minOccurs=' { minOccurs } ' maxOccurs=' { maxOccurs } '>" )
104+ if elem_type and self ._is_std_type ( elem_type ):
105+ elem_type = self . _xsd_type_string ( elem_type )
104106
105- if elem_type :
107+ if elem_reqd :
108+ minOccurs , maxOccurs = self .SDF_REQUIRED_TO_MIN_MAX_OCCURS [elem_reqd ]
109+ lines .append (f"<xsd:choice minOccurs='{ minOccurs } ' maxOccurs='{ maxOccurs } '>" )
110+
111+ if elem_type is None :
106112 lines .append (f"<xsd:element name='{ elem_name } '>" )
107113 lines .extend (self ._indent_lines (self ._print_documentation (element ), 2 ))
108114 lines .append (" <xsd:complexType>" )
109115 lines .append (" <xsd:choice maxOccurs='unbounded'>" )
110116
111117 for e in element .findall ('element' ):
112- lines .extend (self ._indent_lines (self ._print_element (e ), 2 ))
118+ lines .extend (self ._indent_lines (self ._print_element (e ), 6 ))
113119
114120 lines .append (" </xsd:choice>" )
115121
@@ -129,19 +135,22 @@ def _print_attribute(self, element: ElementTree.Element) -> List[str]:
129135 lines = []
130136
131137 elem_name = self ._get_attribute (element , 'name' )
132- elem_type = self ._xsd_type_string (element )
138+ elem_type = self ._get_attribute (element , 'type' )
133139 elem_reqd = self ._get_attribute (element , 'required' )
134140 elem_default = self ._get_attribute (element , 'default' )
135141
142+ if elem_type and self ._is_std_type (elem_type ):
143+ elem_type = self ._xsd_type_string (elem_type )
144+
136145 use = ""
137146 default = ""
138147
139148 if elem_reqd == "1" :
140149 use = "use='required'"
141150 elif elem_reqd == "0" :
142151 use = "use='optional'"
143- if elem_default :
144- default = f"default='{ elem_default } ';' "
152+ if elem_default is not None :
153+ default = f"default='{ elem_default } '"
145154
146155 lines .append (f"<xsd:attribute name='{ elem_name } ' type='{ elem_type } ' { use } { default } >" )
147156 lines .extend (self ._indent_lines (self ._print_documentation (element ), 2 ))
@@ -150,7 +159,10 @@ def _print_attribute(self, element: ElementTree.Element) -> List[str]:
150159
151160 def _print_xsd (self , element : ElementTree .Element ) -> List [str ]:
152161 lines = []
162+
153163 elem_name = self ._get_attribute (element , 'name' )
164+ elem_type = self ._get_attribute (element , 'type' )
165+
154166 elements = element .findall ('element' )
155167 attributes = element .findall ('attribute' )
156168 includes = element .findall ('include' )
@@ -170,7 +182,10 @@ def _print_xsd(self, element: ElementTree.Element) -> List[str]:
170182 lines .append (" <xsd:choice maxOccurs='unbounded'>" )
171183
172184 for child_element in elements :
173- lines .extend (self ._indent_lines (self ._print_element (child_element ), 4 ))
185+ if 'copy_data' in child_element .attrib :
186+ lines .extend (self ._indent_lines (self ._print_plugin_element (child_element ), 4 ))
187+ else :
188+ lines .extend (self ._indent_lines (self ._print_element (child_element ), 6 ))
174189
175190 for include_element in includes :
176191 lines .extend (self ._indent_lines (self ._print_include_ref (include_element ), 6 ))
@@ -184,10 +199,11 @@ def _print_xsd(self, element: ElementTree.Element) -> List[str]:
184199 lines .append (" </xsd:complexType>" )
185200 lines .append (f"</xsd:element>" )
186201 else :
187- elem_type = self ._xsd_type_string ( element )
188-
189- if elem_type is None :
202+ if elem_type and self ._is_std_type ( elem_type ):
203+ elem_type = self . _xsd_type_string ( elem_type )
204+ else :
190205 elem_type = ""
206+
191207 lines .append (f"<xsd:element name='{ elem_name } '{ elem_type } />" )
192208 return lines
193209
0 commit comments