def _retrieve_data(self, request): """Retrieve module's data Args: request: Returns: """ data = "" if request.method == "GET": if "data" in request.GET: data = request.GET["data"] elif request.method == "POST": if "elementList" in request.POST: element_list = json.loads(request.POST["elementList"]) if len(element_list) > 0: element_list_xml = "" for element in element_list: element_list_xml += "<constituent>" element_list_xml += "<element>" + element[ "name"] + "</element>" element_list_xml += ( "<quantity>" + (XmlEntities().escape_xml_entities(element["qty"]) if AUTO_ESCAPE_XML_ENTITIES else element["qty"]) + "</quantity>") element_list_xml += ( "<purity>" + (XmlEntities().escape_xml_entities(element["pur"]) if AUTO_ESCAPE_XML_ENTITIES else element["pur"]) + "</purity>") element_list_xml += ( "<error>" + (XmlEntities().escape_xml_entities(element["err"]) if AUTO_ESCAPE_XML_ENTITIES else element["err"]) + "</error>") element_list_xml += "</constituent>" # set the data data = element_list_xml return data
def _retrieve_data(self, request): """Retrieve module's data Args: request: Returns: """ data = "" self.data_xml_entities = XmlEntities() if request.method == "GET": if "data" in request.GET: data = request.GET["data"] elif request.method == "POST": if "data" in request.POST: data = request.POST["data"] data = (self.data_xml_entities.escape_xml_entities(data) if AUTO_ESCAPE_XML_ENTITIES else data) return data
def render_restriction(self, element): """Renders a restriction Args: element: Returns: """ content = ["", "", ""] value = element.value for child in element.children: tmp_content = ["", "", ""] if child.tag == "enumeration": tmp_content[1] = value if value is not None else "" if AUTO_ESCAPE_XML_ENTITIES: tmp_content[1] = XmlEntities().escape_xml_entities( tmp_content[1]) value = None # Avoid to copy the value several times elif child.tag == "input": tmp_content[1] = child.value if child.value is not None else "" if AUTO_ESCAPE_XML_ENTITIES: tmp_content[1] = XmlEntities().escape_xml_entities( tmp_content[1]) elif child.tag == "simple_type": tmp_content = self.render_simple_type(child) else: message = "render_restriction: " + child.tag + " not handled" self.warnings.append(message) content[0] = " ".join([content[0], tmp_content[0]]).strip() content[1] += tmp_content[1] content[2] += tmp_content[2] return content
class TextAreaModule(AbstractTextAreaModule): def _retrieve_data(self, request): """Retrieve module's data Args: request: Returns: """ data = "" self.data_xml_entities = XmlEntities() if request.method == "GET": if "data" in request.GET: data = request.GET["data"] elif request.method == "POST": if "data" in request.POST: data = request.POST["data"] data = (self.data_xml_entities.escape_xml_entities(data) if AUTO_ESCAPE_XML_ENTITIES else data) return data def _render_data(self, request): """Return module's data rendering Args: request: Returns: """ # search the XML predefined entities, to display warning if it needed / we add pre escaped search too if (self.data_xml_entities.number_of_subs_made > 0 or len( re.findall(r"((&)|(>)|(<)|(')|("))", self.data)) > 0): return loader.get_template( "core_module_text_area_app/predefined_entities_warning.html" ).template.source else: return ""
def render_attribute(self, element): """Renders an attribute Args: element: Returns: """ attr_key = element.options["name"] attr_list = [] children = [] for child in element.children: if child.tag == "elem-iter": children += child.children else: message = "render_attribute (iteration): " + child.tag + " not handled" self.warnings.append(message) for child in children: attr_value = "" if child.tag == "simple_type": content = self.render_simple_type(child) attr_value = content[1] elif child.tag == "input": attr_value = child.value if child.value is not None else "" if AUTO_ESCAPE_XML_ENTITIES: attr_value = XmlEntities().escape_xml_entities(attr_value) elif child.tag == "module": attr_value = self.render_module(child)[1] else: message = "render_attribute: " + child.tag + " not handled" self.warnings.append(message) # namespaces if "xmlns" in element.options and element.options[ "xmlns"] is not None: # check that element isn't declaring the same namespace xmlns="" parent = self._get_parent_element(element) xmlns = "" if parent is not None: if ("xmlns" in parent.options and parent.options["xmlns"] is not None and parent.options["xmlns"] == element.options["xmlns"]): xmlns = "" else: # parent element is in a different namespace if element.options["xmlns"] != "": # TODO: test ns0 not taken and increment if needed ns_prefix = (element.options["ns_prefix"] if element.options["ns_prefix"] is not None else "ns0") if ns_prefix != "": xmlns = ' xmlns{0}="{1}"'.format( ":" + ns_prefix, element.options["xmlns"]) attr_key = "{0}:{1}".format( ns_prefix, attr_key) else: xmlns = ' xmlns="{0}"'.format( element.options["xmlns"]) else: xmlns = "" if isinstance(attr_value, numbers.Number): attr_value = str(attr_value) attr_list.append(xmlns + " " + attr_key + "='" + attr_value + "'") # TODO: check that sibling attributes are not declaring the # same namespaces else: attr_list.append(attr_key + '="' + attr_value + '"') return " ".join(attr_list)
def render_element(self, element): """Renders an element Args: element: Returns: """ xml_string = "" children = {} child_keys = [] children_number = 0 for child in element.children: if child.tag == "elem-iter": children[child.pk] = child.children child_keys.append(child.pk) if len(child.children) > 0: children_number += 1 else: message = "render_element (iteration): " + child.tag + " not handled" self.warnings.append(message) element_name = element.options["name"] for child_key in child_keys: for child in children[child_key]: content = ["", "", ""] # add XML Schema instance prefix if root if self.isRoot: xsi = ' xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" ' content[0] += xsi self.isRoot = False if child.tag == "complex_type": tmp_content = self.render_complex_type(child) content[0] += tmp_content[0] content[1] += tmp_content[1] content[2] += tmp_content[2] elif child.tag == "input": tmp_content = child.value if child.value is not None else "" content[1] += ( XmlEntities().escape_xml_entities(tmp_content) if AUTO_ESCAPE_XML_ENTITIES else tmp_content) elif child.tag == "simple_type": tmp_content = self.render_simple_type(child) content[0] += tmp_content[0] content[1] += tmp_content[1] content[2] += tmp_content[2] elif child.tag == "module": tmp_content = self.render_module(child) if child.options["multiple"]: content[2] += tmp_content[1] else: content[1] += tmp_content[1] else: message = "render_element: " + child.tag + " not handled" self.warnings.append(message) # namespaces parent = self._get_parent_element(element) if parent is not None: if ("xmlns" in element.options and element.options["xmlns"] is not None): if ("xmlns" in parent.options and element.options["xmlns"] != parent.options["xmlns"]): xmlns = ' xmlns="{}"'.format( element.options["xmlns"]) content[0] += xmlns else: if ("xmlns" in element.options and element.options["xmlns"] is not None): xmlns = ' xmlns="{}"'.format(element.options["xmlns"]) content[0] += xmlns # content[2] has the value returned by a module (the entire # tag, when multiple is True) if content[2] != "": if content[1] != "": raise RendererError( "ERROR: More values than expected were returned " "(Module multiple).") xml_string += content[2] else: xml_string += self._render_xml(element_name, content[0], content[1]) return xml_string
def test_unescaped_without_predefined_xml_entities(self): string = "aaabbbcccdddeee" self.assertTrue( XmlEntities.unescape_xml_entities(string)[0] == "aaabbbcccdddeee") self.assertTrue(XmlEntities.unescape_xml_entities(string)[1] == 0)
def test_unescaped_with_predefined_xml_entities(self): string = "<<">&&<" self.assertTrue( XmlEntities.unescape_xml_entities(string)[0] == '<<">&&<') self.assertTrue(XmlEntities.unescape_xml_entities(string)[1] == 3)