def load_xml(request): # get the original string xmlString = request.session['xmlTemplateCompose'] # reset the string request.session['newXmlTemplateCompose'] = xmlString request.session['includedTypesCompose'] = [] xsltPath = os.path.join(settings.SITE_ROOT, 'static', 'resources', 'xsl', 'xsd2html.xsl') xslt = etree.parse(xsltPath) transform = etree.XSLT(xslt) xmlTree = "" if xmlString != "": request.session['namespacesCompose'] = common.get_namespaces(BytesIO(str(xmlString))) for prefix, url in request.session['namespacesCompose'].items(): if url == SCHEMA_NAMESPACE: request.session['defaultPrefixCompose'] = prefix break dom = etree.parse(BytesIO(xmlString.encode('utf-8'))) annotations = dom.findall(".//{}annotation".format(LXML_SCHEMA_NAMESPACE)) for annotation in annotations: annotation.getparent().remove(annotation) newdom = transform(dom) xmlTree = str(newdom) # store the current includes includes = dom.findall("{}include".format(LXML_SCHEMA_NAMESPACE)) for el_include in includes: if 'schemaLocation' in el_include.attrib: request.session['includedTypesCompose'].append(el_include.attrib['schemaLocation']) response_dict = {'XMLHolder': xmlTree} return HttpResponse(json.dumps(response_dict), content_type='application/javascript')
def _get_module(self, request): self.selected = [] # get the values of the enumeration xml_doc_tree_str = request.session['xmlDocTree'] xml_doc_tree = etree.fromstring(xml_doc_tree_str) namespaces = common.get_namespaces(BytesIO(str(xml_doc_tree_str))) # get the element where the module is attached xsd_element = xml_doc_tree.xpath(request.GET['xsd_xpath'], namespaces=namespaces)[0] xsd_element_type = xsd_element.attrib['type'] # remove ns prefix if present if ':' in xsd_element_type: xsd_element_type = xsd_element_type.split(':')[1] xpath_type = "./{0}simpleType[@name='{1}']".format(LXML_SCHEMA_NAMESPACE, xsd_element_type) elementType = xml_doc_tree.find(xpath_type) enumeration_list = elementType.findall('./{0}restriction/{0}enumeration'.format(LXML_SCHEMA_NAMESPACE)) for enumeration in enumeration_list: self.options[enumeration.attrib['value']] = enumeration.attrib['value'] if 'data' in request.GET: data = request.GET['data'] # get XML to reload reload_data = etree.fromstring("<root>" + data + "</root>") for child in reload_data: self.selected.append(child.text.strip()) return CheckboxesModule.get_module(self, request)
def load_xml(request): # get the original string xmlString = request.session['xmlTemplateCompose'] # reset the string request.session['newXmlTemplateCompose'] = xmlString request.session['includedTypesCompose'] = [] xsltPath = os.path.join(settings.SITE_ROOT, 'static', 'resources', 'xsl', 'xsd2html.xsl') xslt = etree.parse(xsltPath) transform = etree.XSLT(xslt) xmlTree = "" if (xmlString != ""): request.session['namespacesCompose'] = common.get_namespaces(BytesIO(str(xmlString))) for prefix, url in request.session['namespacesCompose'].items(): if (url == "{http://www.w3.org/2001/XMLSchema}"): request.session['defaultPrefixCompose'] = prefix break dom = etree.parse(BytesIO(xmlString.encode('utf-8'))) annotations = dom.findall(".//{http://www.w3.org/2001/XMLSchema}annotation") for annotation in annotations: annotation.getparent().remove(annotation) newdom = transform(dom) xmlTree = str(newdom) # store the current includes includes = dom.findall("{http://www.w3.org/2001/XMLSchema}include") for el_include in includes: if 'schemaLocation' in el_include.attrib: request.session['includedTypesCompose'].append(el_include.attrib['schemaLocation']) response_dict = {'XMLHolder': xmlTree} return HttpResponse(json.dumps(response_dict), content_type='application/javascript')
def _get_module(self, request): self.selected = [] # get the values of the enumeration xml_doc_tree_str = request.session['xmlDocTree'] xml_doc_tree = etree.fromstring(xml_doc_tree_str) namespaces = common.get_namespaces(BytesIO(str(xml_doc_tree_str))) # get the element where the module is attached xsd_element = xml_doc_tree.xpath(request.GET['xsd_xpath'], namespaces=namespaces)[0] xsd_element_type = xsd_element.attrib['type'] # remove ns prefix if present if ':' in xsd_element_type: xsd_element_type = xsd_element_type.split(':')[1] xpath_type = "./{0}simpleType[@name='{1}']".format( LXML_SCHEMA_NAMESPACE, xsd_element_type) elementType = xml_doc_tree.find(xpath_type) enumeration_list = elementType.findall( './{0}restriction/{0}enumeration'.format(LXML_SCHEMA_NAMESPACE)) for enumeration in enumeration_list: self.options[ enumeration.attrib['value']] = enumeration.attrib['value'] if 'data' in request.GET: data = request.GET['data'] # get XML to reload reload_data = etree.fromstring("<root>" + data + "</root>") for child in reload_data: self.selected.append(child.text.strip()) return CheckboxesModule.get_module(self, request)
def _get_flatten_schema_and_namespaces(template_name): schemas = Template.objects(title=template_name) schema_id = TemplateVersion.objects().get(pk=schemas[0].templateVersion).current schema = Template.objects().get(pk=schema_id) flattener = XSDFlattenerURL(schema.content.encode('utf-8')) ref_xml_schema_content = flattener.get_flat() # find the namespaces namespaces = common.get_namespaces(BytesIO(schema.content.encode('utf-8'))) return ref_xml_schema_content, namespaces
def dashboard_modules(request): template = loader.get_template('dashboard/my_dashboard_modules.html') object_id = request.GET.get('id', None) object_type = request.GET.get('type', None) if object_id is not None: try: if object_type == 'Template': db_object = Template.objects.get(pk=object_id) elif object_type == 'Type': db_object = Type.objects.get(pk=object_id) else: raise AttributeError('Type parameter unrecognized') xslt_path = os.path.join(settings.SITE_ROOT, 'static', 'resources', 'xsl', 'xsd2html4modules.xsl') xslt = etree.parse(xslt_path) transform = etree.XSLT(xslt) dom = etree.parse(BytesIO(db_object.content.encode('utf-8'))) annotations = dom.findall( ".//{http://www.w3.org/2001/XMLSchema}annotation") for annotation in annotations: annotation.getparent().remove(annotation) newdom = transform(dom) xsd_tree = str(newdom) request.session['moduleTemplateID'] = object_id request.session['moduleTemplateContent'] = db_object.content namespaces = common.get_namespaces(BytesIO(str(db_object.content))) for prefix, url in namespaces.iteritems(): if url == SCHEMA_NAMESPACE: request.session['moduleDefaultPrefix'] = prefix break context = RequestContext( request, { 'xsdTree': xsd_tree, 'modules': Module.objects, 'object_type': object_type }) return HttpResponse(template.render(context)) except: return redirect('/') else: return redirect('/')
def modules(request): template = loader.get_template('admin/modules.html') id = request.GET.get('id', None) if id is not None: try: object = Template.objects.get(pk=id) xsltPath = os.path.join(settings.SITE_ROOT, 'static', 'resources', 'xsl', 'xsd2html4modules.xsl') xslt = etree.parse(xsltPath) transform = etree.XSLT(xslt) dom = etree.parse(BytesIO(object.content.encode('utf-8'))) annotations = dom.findall( ".//{http://www.w3.org/2001/XMLSchema}annotation") for annotation in annotations: annotation.getparent().remove(annotation) newdom = transform(dom) xsdTree = str(newdom) request.session['moduleTemplateID'] = id request.session['moduleTemplateContent'] = object.content request.session['moduleNamespaces'] = common.get_namespaces( BytesIO(str(object.content))) for prefix, url in request.session['moduleNamespaces'].items(): if (url == "{http://www.w3.org/2001/XMLSchema}"): request.session['moduleDefaultPrefix'] = prefix break context = RequestContext(request, { 'xsdTree': xsdTree, 'modules': Module.objects }) return HttpResponse(template.render(context)) except: return redirect('/') else: return redirect('/')
def modules(request): template = loader.get_template('admin/modules.html') id = request.GET.get('id', None) if id is not None: try: object = Template.objects.get(pk=id) xsltPath = os.path.join(settings.SITE_ROOT, 'static', 'resources', 'xsl', 'xsd2html4modules.xsl') xslt = etree.parse(xsltPath) transform = etree.XSLT(xslt) dom = etree.parse(BytesIO(object.content.encode('utf-8'))) annotations = dom.findall(".//{http://www.w3.org/2001/XMLSchema}annotation") for annotation in annotations: annotation.getparent().remove(annotation) newdom = transform(dom) xsdTree = str(newdom) request.session['moduleTemplateID'] = id request.session['moduleTemplateContent'] = object.content request.session['moduleNamespaces'] = common.get_namespaces(BytesIO(str(object.content))) for prefix, url in request.session['moduleNamespaces'].items(): if (url == "{http://www.w3.org/2001/XMLSchema}"): request.session['moduleDefaultPrefix'] = prefix break context = RequestContext(request, { 'xsdTree': xsdTree, 'modules': Module.objects }) return HttpResponse(template.render(context)) except: return redirect('/') else: return redirect('/')
def insert_element_sequence(request): try: type_id = request.POST['typeID'] client_type_name = request.POST['typeName'] xpath = request.POST['xpath'] xml_tree_str = request.session['newXmlTemplateCompose'] # build the dom tree of the schema being built xsd_tree = etree.parse(BytesIO(xml_tree_str.encode('utf-8'))) # get namespaces information for the schema namespaces = get_namespaces(BytesIO(str(xml_tree_str))) default_prefix = get_default_prefix(namespaces) # get target namespace information target_namespace, target_namespace_prefix = get_target_namespace( namespaces, xsd_tree) # build xpath to element xpath = xpath.replace(default_prefix + ":", LXML_SCHEMA_NAMESPACE) if type_id == 'built_in_type': type_name = default_prefix + ':' + client_type_name xsd_tree.find(xpath).append( etree.Element("{}element".format(LXML_SCHEMA_NAMESPACE), attrib={ 'type': type_name, 'name': client_type_name })) # validate XML schema error = validate_xml_schema(xsd_tree) new_xsd_str = etree.tostring(xsd_tree) else: # get the type being included type_object = Type.objects().get(pk=type_id) type_xsd_tree = etree.parse( BytesIO(type_object.content.encode('utf-8'))) # get namespaces information for the type type_namespaces = get_namespaces(BytesIO(str(type_object.content))) type_target_namespace, type_target_namespace_prefix = get_target_namespace( type_namespaces, type_xsd_tree) # get the type from the included/imported file # If there is a complex type element_type = type_xsd_tree.find( "{}complexType".format(LXML_SCHEMA_NAMESPACE)) if element_type is None: # If there is a simple type element_type = type_xsd_tree.find( "{}simpleType".format(LXML_SCHEMA_NAMESPACE)) type_name = element_type.attrib["name"] if type_target_namespace is not None: ns_type_name = "{0}:{1}".format(type_target_namespace_prefix, type_name) else: if target_namespace is not None: ns_type_name = "{0}:{1}".format(target_namespace_prefix, type_name) else: ns_type_name = '{}'.format(type_name) nsmap = {type_target_namespace_prefix: type_target_namespace} update_nsmap = False # get link to the type to include include_url = getSchemaLocation(str(type_id)) # Schema without target namespace if target_namespace is None: # Type without target namespace if type_target_namespace is None: # add include xsd_tree.getroot().insert( 0, etree.Element( "{}include".format(LXML_SCHEMA_NAMESPACE), attrib={'schemaLocation': include_url})) # add element xsd_tree.find(xpath).append( etree.Element( "{}element".format(LXML_SCHEMA_NAMESPACE), attrib={ 'type': type_name, 'name': client_type_name })) # Type with target namespace else: # add import xsd_tree.getroot().insert( 0, etree.Element("{}import".format(LXML_SCHEMA_NAMESPACE), attrib={ 'schemaLocation': include_url, 'namespace': type_target_namespace })) # create the element to add element = etree.Element( "{}element".format(LXML_SCHEMA_NAMESPACE), attrib={ 'name': client_type_name, 'type': ns_type_name }, ) # add the element xsd_tree.find(xpath).append(element) update_nsmap = True # Schema with target namespace else: # Type without target namespace if type_target_namespace is None: # add include xsd_tree.getroot().insert( 0, etree.Element( "{}include".format(LXML_SCHEMA_NAMESPACE), attrib={'schemaLocation': include_url})) # add element xsd_tree.find(xpath).append( etree.Element( "{}element".format(LXML_SCHEMA_NAMESPACE), attrib={ 'name': client_type_name, 'type': ns_type_name })) # Type with target namespace else: # Same target namespace as base template if target_namespace == type_target_namespace: # add include xsd_tree.getroot().insert( 0, etree.Element( "{}include".format(LXML_SCHEMA_NAMESPACE), attrib={'schemaLocation': include_url})) # add element xsd_tree.find(xpath).append( etree.Element( "{}element".format(LXML_SCHEMA_NAMESPACE), attrib={ 'name': client_type_name, 'type': ns_type_name })) # Different target namespace as base template else: # add import xsd_tree.getroot().insert( 0, etree.Element( "{}import".format(LXML_SCHEMA_NAMESPACE), attrib={ 'schemaLocation': include_url, 'namespace': type_target_namespace })) # create the element to add element = etree.Element( "{}element".format(LXML_SCHEMA_NAMESPACE), attrib={ 'name': client_type_name, 'type': ns_type_name }, ) # add the element xsd_tree.find(xpath).append(element) update_nsmap = True # add the id of the type if not already present if include_url not in request.session['includedTypesCompose']: request.session['includedTypesCompose'].append(include_url) if update_nsmap: root = xsd_tree.getroot() root_nsmap = root.nsmap if type_target_namespace_prefix in root_nsmap.keys() and\ root_nsmap[type_target_namespace_prefix] != type_target_namespace: raise MDCSError( 'The namespace prefix is already declared for a different namespace.' ) else: root_nsmap[ type_target_namespace_prefix] = type_target_namespace new_root = etree.Element(root.tag, nsmap=root_nsmap) new_root[:] = root[:] # validate XML schema error = validate_xml_schema(new_root) new_xsd_str = etree.tostring(new_root) else: # validate XML schema error = validate_xml_schema(xsd_tree) new_xsd_str = etree.tostring(xsd_tree) if error is not None: raise MDCSError(error) # save the tree in the session request.session['newXmlTemplateCompose'] = new_xsd_str except Exception, e: return HttpResponseBadRequest(e.message, content_type='application/javascript')
def insert_element_sequence(request): try: type_id = request.POST['typeID'] client_type_name = request.POST['typeName'] xpath = request.POST['xpath'] xml_tree_str = request.session['newXmlTemplateCompose'] # build the dom tree of the schema being built xsd_tree = etree.parse(BytesIO(xml_tree_str.encode('utf-8'))) # get namespaces information for the schema namespaces = get_namespaces(BytesIO(str(xml_tree_str))) default_prefix = get_default_prefix(namespaces) target_namespace, target_namespace_prefix = get_target_namespace(namespaces, xsd_tree) # get the type being included type_object = Type.objects().get(pk=type_id) type_xsd_tree = etree.parse(BytesIO(type_object.content.encode('utf-8'))) # get namespaces information for the type type_namespaces = get_namespaces(BytesIO(str(type_object.content))) type_target_namespace, type_target_namespace_prefix = get_target_namespace(type_namespaces, type_xsd_tree) # get the type from the included/imported file # If there is a complex type element_type = type_xsd_tree.find("{}complexType".format(LXML_SCHEMA_NAMESPACE)) if element_type is None: # If there is a simple type element_type = type_xsd_tree.find("{}simpleType".format(LXML_SCHEMA_NAMESPACE)) type_name = element_type.attrib["name"] if type_target_namespace is not None: ns_type_name = "{0}:{1}".format(type_target_namespace_prefix, type_name) else: if target_namespace is not None: ns_type_name = "{0}:{1}".format(target_namespace_prefix, type_name) else: ns_type_name = '{}'.format(type_name) nsmap = {type_target_namespace_prefix: type_target_namespace} update_nsmap = False # build xpath to element xpath = xpath.replace(default_prefix + ":", LXML_SCHEMA_NAMESPACE) # get link to the type to include include_url = getSchemaLocation(str(type_id)) # Schema without target namespace if target_namespace is None: # Type without target namespace if type_target_namespace is None: # add include xsd_tree.getroot().insert(0, etree.Element("{}include".format(LXML_SCHEMA_NAMESPACE), attrib={'schemaLocation': include_url})) # add element xsd_tree.find(xpath).append(etree.Element("{}element".format(LXML_SCHEMA_NAMESPACE), attrib={'type': type_name, 'name': client_type_name})) # Type with target namespace else: # add import xsd_tree.getroot().insert(0, etree.Element("{}import".format(LXML_SCHEMA_NAMESPACE), attrib={'schemaLocation': include_url, 'namespace': type_target_namespace})) # create the element to add element = etree.Element("{}element".format(LXML_SCHEMA_NAMESPACE), attrib={'name': client_type_name, 'type': ns_type_name}, ) # add the element xsd_tree.find(xpath).append(element) update_nsmap = True # Schema with target namespace else: # Type without target namespace if type_target_namespace is None: # add include xsd_tree.getroot().insert(0, etree.Element("{}include".format(LXML_SCHEMA_NAMESPACE), attrib={'schemaLocation': include_url})) # add element xsd_tree.find(xpath).append(etree.Element("{}element".format(LXML_SCHEMA_NAMESPACE), attrib={'name': client_type_name, 'type': ns_type_name})) # Type with target namespace else: # Same target namespace as base template if target_namespace == type_target_namespace: # add include xsd_tree.getroot().insert(0, etree.Element("{}include".format(LXML_SCHEMA_NAMESPACE), attrib={'schemaLocation': include_url})) # add element xsd_tree.find(xpath).append(etree.Element("{}element".format(LXML_SCHEMA_NAMESPACE), attrib={'name': client_type_name, 'type': ns_type_name})) # Different target namespace as base template else: # add import xsd_tree.getroot().insert(0, etree.Element("{}import".format(LXML_SCHEMA_NAMESPACE), attrib={'schemaLocation': include_url, 'namespace': type_target_namespace})) # create the element to add element = etree.Element("{}element".format(LXML_SCHEMA_NAMESPACE), attrib={'name': client_type_name, 'type': ns_type_name}, ) # add the element xsd_tree.find(xpath).append(element) update_nsmap = True # add the id of the type if not already present if include_url not in request.session['includedTypesCompose']: request.session['includedTypesCompose'].append(include_url) if update_nsmap: root = xsd_tree.getroot() root_nsmap = root.nsmap if type_target_namespace_prefix in root_nsmap.keys() and\ root_nsmap[type_target_namespace_prefix] != type_target_namespace: raise MDCSError('The namespace prefix is already declared for a different namespace.') else: root_nsmap[type_target_namespace_prefix] = type_target_namespace new_root = etree.Element(root.tag, nsmap=root_nsmap) new_root[:] = root[:] # validate XML schema error = validate_xml_schema(new_root) new_xsd_str = etree.tostring(new_root) else: # validate XML schema error = validate_xml_schema(xsd_tree) new_xsd_str = etree.tostring(xsd_tree) if error is not None: raise MDCSError(error) # save the tree in the session request.session['newXmlTemplateCompose'] = new_xsd_str except Exception, e: return HttpResponseBadRequest(e.message, content_type='application/javascript')
def _get_module(self, request): # Get HTML of the module with open(os.path.join(TEMPLATES_PATH, 'fancy_tree.html'), 'r') as template_file: template_content = template_file.read() template = Template(template_content) # *** GET RELOAD DATA *** # If data are provided to reload the module if 'data' in request.GET: # get the data data = request.GET['data'] # build tree of data to reload (need a root element because data is a list of xml elements) reload_data = etree.fromstring("<root>" + data + "</root>") # Iterate xml elements for reload_data_element in list(reload_data): try: # The xml element to be reloaded is the child child = reload_data_element[0] # remove namespace form tag name if present: {namespace}tag tag_name = child.tag if "}" in tag_name: end_namespace_index = tag_name.index("}") tag_name = tag_name[end_namespace_index + 1:] # get the content of the xml element child_text = etree.tostring(child) # remove namespace from xml value if present: <tag xmlns="">value</tag> if "xmlns=" in child_text: end_tag_index = child_text.index(">") xml_value = child_text[:len(tag_name) + 1] + child_text[end_tag_index:] else: xml_value = child_text # add selected value to list self.selected.append(xml_value) except (IndexError, Exception): pass # *** GET POSSIBLE VALUES FROM ENUMERATION AND BUILD FANCY TREE *** # load XSD string xml_doc_tree_str = request.session['xmlDocTree'] # build XSD tree xml_doc_tree = etree.fromstring(xml_doc_tree_str) # get namespaces used in the schema namespaces = common.get_namespaces(BytesIO(str(xml_doc_tree_str))) # get the element where the module is attached xsd_element = xml_doc_tree.xpath(request.GET['xsd_xpath'], namespaces=namespaces)[0] # get the type name of the element xsd_element_type = xsd_element.attrib['type'] # remove ns prefix if present if ':' in xsd_element_type: xsd_element_type = xsd_element_type.split(':')[1] # xpath to the type in the schema xpath_type = "./{0}complexType[@name='{1}']".format( LXML_SCHEMA_NAMESPACE, xsd_element_type) # find the type in the schema element_type = xml_doc_tree.find(xpath_type) # look for the choice in the type choice_element = element_type.find( "./{0}choice".format(LXML_SCHEMA_NAMESPACE)) # get all elements of the choice choice_children = list(choice_element) # declare an empty tree tree = OrderedDict() # iterate trough all options to build the tree for choice_child in choice_children: # get choice element name choice_child_name = choice_child.attrib["name"] # get choice element type choice_child_type_name = choice_child.attrib["type"] # remove ns prefix if present if ':' in choice_child_type_name: choice_child_type_name = choice_child_type_name.split(':')[1] # get the xpath of the choice element type choice_child_type_xpath = "./{0}simpleType[@name='{1}']".format( LXML_SCHEMA_NAMESPACE, choice_child_type_name) # find the type in the schema using the xpath choice_child_type = xml_doc_tree.find(choice_child_type_xpath) # get the list of possible values for this choice list_enumeration = choice_child_type.findall( ".//{0}enumeration".format(LXML_SCHEMA_NAMESPACE)) # build the fancy tree for this choice element tree = _build_tree(tree, choice_child_name, list_enumeration, self.selected) # declare empty source data for the client fancy tree source_data = OrderedDict() for root, child_tree in sorted(tree.iteritems()): # add tree to source data source_data.update(child_tree) # set context with printed fancy tree, to be loaded on the page context = Context({ 'source_data': print_tree(source_data), 'module_id': request.GET['module_id'] }) return template.render(context)