19 |
19 |
|
20 |
20 |
import jsonschema
|
21 |
21 |
|
22 |
22 |
from .logging import ignore_loggers
|
23 |
23 |
|
24 |
24 |
with ignore_loggers('xmlschema'):
|
25 |
25 |
import xmlschema
|
26 |
26 |
|
|
27 |
try:
|
|
28 |
import xmlschema.names as names # xmschema >= 1.1
|
|
29 |
except ModuleNotFoundError:
|
|
30 |
import xmlschema.qnames as names # xmschema < 1.1
|
|
31 |
|
27 |
32 |
|
28 |
33 |
def text_content(node):
|
29 |
34 |
"""Extract text content from node and all its children. Equivalent to
|
30 |
35 |
xmlNodeGetContent from libxml."""
|
31 |
36 |
|
32 |
37 |
if node is None:
|
33 |
38 |
return ''
|
34 |
39 |
|
... | ... | |
122 |
127 |
mapped = self.transformer.type_map[xsd_element.type.name]
|
123 |
128 |
if hasattr(self.transformer, 'encode_%s' % mapped):
|
124 |
129 |
obj = getattr(self.transformer, 'encode_%s' % mapped)(obj)
|
125 |
130 |
return super(TransformConverter, self).element_encode(obj, xsd_element, level=level)
|
126 |
131 |
|
127 |
132 |
|
128 |
133 |
class JSONSchemaFromXMLSchema(object):
|
129 |
134 |
SIMPLE_TYPE_MAPPING = {
|
130 |
|
xmlschema.qnames.XSD_STRING: 'string',
|
131 |
|
xmlschema.qnames.XSD_INTEGER: 'integer',
|
132 |
|
xmlschema.qnames.XSD_INT: 'integer',
|
133 |
|
xmlschema.qnames.XSD_POSITIVE_INTEGER: 'integer',
|
134 |
|
xmlschema.qnames.XSD_UNSIGNED_INT: 'integer',
|
135 |
|
xmlschema.qnames.XSD_NON_NEGATIVE_INTEGER: 'integer',
|
136 |
|
xmlschema.qnames.XSD_BOOLEAN: 'boolean',
|
137 |
|
xmlschema.qnames.XSD_DOUBLE: 'number',
|
138 |
|
xmlschema.qnames.XSD_DECIMAL: 'number',
|
|
135 |
names.XSD_STRING: 'string',
|
|
136 |
names.XSD_INTEGER: 'integer',
|
|
137 |
names.XSD_INT: 'integer',
|
|
138 |
names.XSD_POSITIVE_INTEGER: 'integer',
|
|
139 |
names.XSD_UNSIGNED_INT: 'integer',
|
|
140 |
names.XSD_NON_NEGATIVE_INTEGER: 'integer',
|
|
141 |
names.XSD_BOOLEAN: 'boolean',
|
|
142 |
names.XSD_DOUBLE: 'number',
|
|
143 |
names.XSD_DECIMAL: 'number',
|
139 |
144 |
}
|
140 |
145 |
|
141 |
146 |
def __init__(self, xml_schema, root_element):
|
142 |
147 |
if not isinstance(xml_schema, xmlschema.XMLSchema):
|
143 |
148 |
with ignore_loggers('xmlschema'):
|
144 |
149 |
xml_schema = xmlschema.XMLSchema(xml_schema, converter=TransformConverter(transformer=self))
|
145 |
150 |
self.xml_schema = xml_schema
|
146 |
151 |
self.root_element = root_element
|
... | ... | |
188 |
193 |
if simple_type.white_space not in ('collapse', 'preserve'):
|
189 |
194 |
raise NotImplementedError(simple_type)
|
190 |
195 |
schema = OrderedDict(cls.simpletype_to_jsonschema(simple_type.base_type))
|
191 |
196 |
for validator in simple_type.validators:
|
192 |
197 |
if isinstance(validator, xmlschema.validators.XsdEnumerationFacets):
|
193 |
198 |
schema['enum'] = validator.enumeration
|
194 |
199 |
elif (
|
195 |
200 |
isinstance(validator, xmlschema.validators.XsdMinLengthFacet)
|
196 |
|
and simple_type.base_type.name == xmlschema.qnames.XSD_STRING
|
|
201 |
and simple_type.base_type.name == names.XSD_STRING
|
197 |
202 |
):
|
198 |
203 |
schema['minLength'] = validator.value
|
199 |
204 |
elif (
|
200 |
205 |
isinstance(validator, xmlschema.validators.XsdMaxLengthFacet)
|
201 |
|
and simple_type.base_type.name == xmlschema.qnames.XSD_STRING
|
|
206 |
and simple_type.base_type.name == names.XSD_STRING
|
202 |
207 |
):
|
203 |
208 |
schema['maxLength'] = validator.value
|
204 |
209 |
elif (
|
205 |
210 |
isinstance(validator, xmlschema.validators.XsdLengthFacet)
|
206 |
|
and simple_type.base_type.name == xmlschema.qnames.XSD_STRING
|
|
211 |
and simple_type.base_type.name == names.XSD_STRING
|
207 |
212 |
):
|
208 |
213 |
schema['minLength'] = validator.value
|
209 |
214 |
schema['maxLength'] = validator.value
|
210 |
215 |
elif isinstance(validator, xmlschema.validators.XsdMinInclusiveFacet):
|
211 |
216 |
schema['minimum'] = validator.value
|
212 |
217 |
elif isinstance(validator, xmlschema.validators.XsdMaxInclusiveFacet):
|
213 |
218 |
schema['maximum'] = validator.value
|
214 |
219 |
elif (
|
215 |
220 |
isinstance(validator, xmlschema.validators.XsdTotalDigitsFacet)
|
216 |
|
and simple_type.base_type.name == xmlschema.qnames.XSD_DECIMAL
|
|
221 |
and simple_type.base_type.name == names.XSD_DECIMAL
|
217 |
222 |
):
|
218 |
223 |
schema['exclusiveMaximum'] = 10**validator.value
|
219 |
224 |
elif (
|
220 |
225 |
isinstance(validator, xmlschema.validators.XsdFractionDigitsFacet)
|
221 |
|
and simple_type.base_type.name == xmlschema.qnames.XSD_DECIMAL
|
|
226 |
and simple_type.base_type.name == names.XSD_DECIMAL
|
222 |
227 |
):
|
223 |
228 |
schema['multipleOf'] = 1 / 10.0**validator.value
|
224 |
229 |
else:
|
225 |
230 |
raise NotImplementedError(validator)
|
226 |
231 |
add_patterns()
|
227 |
232 |
return schema
|
228 |
233 |
|
229 |
234 |
if isinstance(simple_type, xmlschema.validators.XsdUnion):
|
... | ... | |
331 |
336 |
base_schema = cls.type_to_jsonschema(xmltype.base_type)
|
332 |
337 |
if not xmltype.attributes:
|
333 |
338 |
schema = base_schema
|
334 |
339 |
else:
|
335 |
340 |
cls.attributegroup_to_jsonschema(xmltype.attributes)
|
336 |
341 |
schema['properties']['$'] = base_schema
|
337 |
342 |
return schema
|
338 |
343 |
else:
|
339 |
|
if xmltype.has_mixed_content() or xmltype.name == xmlschema.qnames.XSD_ANY_TYPE:
|
|
344 |
if xmltype.has_mixed_content() or xmltype.name == names.XSD_ANY_TYPE:
|
340 |
345 |
raise NotImplementedError(xmltype)
|
341 |
346 |
|
342 |
347 |
schema = OrderedDict({'type': 'object'})
|
343 |
348 |
schema['additionalProperties'] = False
|
344 |
349 |
if xmltype.attributes:
|
345 |
350 |
cls.attributegroup_to_jsonschema(xmltype.attributes, schema)
|
346 |
351 |
cls.group_to_jsonschema(xmltype.content_type, schema)
|
347 |
352 |
return schema
|
348 |
|
-
|