Commit 0cdc4894 authored by Nicolas Delaby's avatar Nicolas Delaby

* Fix escaping chars in FormGroup title

 * Use lxml to produce XML view
 * Update XMLToForm to follow escaping changes


git-svn-id: https://svn.erp5.org/repos/public/erp5/trunk@27696 20353a03-c40f-0410-a6d1-a30d3c3de9de
parent b5f9e0c5
from StringIO import StringIO # -*- coding: utf-8 -*-
from cgi import escape from cgi import escape
import types from lxml import etree
from lxml.etree import Element, SubElement, CDATA
from lxml.builder import E
#def write(s):
# if type(s) == type(u''):
# print "Unicode:", repr(s)
def formToXML(form, prologue=1): def formToXML(form, prologue=1):
"""Takes a formulator form and serializes it to an XML representation. """Takes a formulator form and serializes it to an XML representation.
""" """
f = StringIO() form_as_xml = Element('form')
write = f.write
if prologue:
write('<?xml version="1.0"?>\n\n')
write('<form>\n')
# export form settings # export form settings
for field in form.settings_form.get_fields(include_disabled=1): for field in form.settings_form.get_fields(include_disabled=1):
id = field.id id = field.id
value = getattr(form, id) value = getattr(form, id)
if id == 'title': if id == 'unicode_mode':
value = escape(value) if value:
if id == 'unicode_mode': value = 'true'
if value: else:
value = 'true' value = 'false'
else: sub_element = SubElement(form_as_xml, id)
value = 'false' sub_element.text = escape(str(value))
write(' <%s>%s</%s>\n' % (id, value, id)) groups = SubElement(form_as_xml, 'groups')
# export form groups # export form groups
write(' <groups>\n')
for group in form.get_groups(include_empty=1): for group in form.get_groups(include_empty=1):
write(' <group>\n') group_element = SubElement(groups, 'group')
write(' <title>%s</title>\n' % escape(group)) group_element.append(E.title(group))
write(' <fields>\n\n')
for field in form.get_fields_in_group(group, include_disabled=1):
write(' <field><id>%s</id> <type>%s</type>\n' % (field.id, field.meta_type))
write(' <values>\n')
items = field.values.items()
items.sort()
for key, value in items:
if value is None:
continue
if value==True: # XXX Patch
value = 1 # XXX Patch
if value==False: # XXX Patch
value = 0 # XXX Patch
if callable(value): # XXX Patch
write(' <%s type="method">%s</%s>\n' % # XXX Patch
(key, escape(str(value.method_name)), key)) # XXX Patch
elif type(value) == type(1.1):
write(' <%s type="float">%s</%s>\n' % (key, escape(str(value)), key))
elif type(value) == type(1):
write(' <%s type="int">%s</%s>\n' % (key, escape(str(value)), key))
elif type(value) == type([]):
write(' <%s type="list">%s</%s>\n' % (key, escape(str(value)), key))
else:
if type(value) not in (types.StringType, types.UnicodeType):
value = str(value)
write(' <%s>%s</%s>\n' % (key, escape(value), key))
write(' </values>\n')
write(' <tales>\n') fields = SubElement(group_element, 'fields')
items = field.tales.items() for field in form.get_fields_in_group(group, include_disabled=1):
items.sort() field_element = E.field(
for key, value in items: E.id(str(field.id)),
if value: E.type(str(field.meta_type))
write(' <%s>%s</%s>\n' % (key, escape(str(value._text)), key)) )
write(' </tales>\n')
write(' <messages>\n') fields.append(field_element)
for message_key in field.get_error_names(): values_element = SubElement(field_element, 'values')
write(' <message name="%s">%s</message>\n' % items = field.values.items()
(escape(message_key), escape(field.get_error_message(message_key)))) items.sort()
write(' </messages>\n') for key, value in items:
write(' </field>\n') if value is None:
write(' </fields>\n') continue
write(' </group>\n') if value is True: # XXX Patch
write(' </groups>\n') value = 1 # XXX Patch
write('</form>') if value is False: # XXX Patch
value = 0 # XXX Patch
if callable(value): # XXX Patch
value_element = SubElement(values_element, key, type='method')
elif isinstance(value, float):
value_element = SubElement(values_element, key, type='float')
elif isinstance(value, int):
value_element = SubElement(values_element, key, type='int')
elif isinstance(value, list):
value_element = SubElement(values_element, key, type='list')
else:
if not isinstance(value, (str, unicode)):
value = str(value)
value_element = SubElement(values_element, key)
value_element.text = escape(str(value))
tales_element = SubElement(field_element, 'tales')
items = field.tales.items()
items.sort()
for key, value in items:
if value:
tale_element = SubElement(tales_element, key)
tale_element.text = escape(str(value._text))
messages = SubElement(field_element, 'messages')
for message_key in field.get_error_names():
message_element = SubElement(messages, 'message', name=message_key)
message_element.text = escape(field.get_error_message(message_key))
form_as_string = etree.tostring(form_as_xml, encoding='utf-8',
xml_declaration=True, pretty_print=True)
if form.unicode_mode: if form.unicode_mode:
return f.getvalue().encode('UTF-8') return etree.tostring(form_as_xml, encoding='utf-8',
xml_declaration=True, pretty_print=True)
else: else:
return unicode(f.getvalue(), form.stored_encoding).encode('UTF-8') return etree.tostring(form_as_xml, encoding=form.stored_encoding,
xml_declaration=True, pretty_print=True)
# -*- coding: utf-8 -*-
from xml.dom.minidom import parse, parseString, Node from xml.dom.minidom import parse, parseString, Node
from xml.sax.saxutils import unescape
# an extremely simple system for loading in XML into objects # an extremely simple system for loading in XML into objects
class Object: class Object:
...@@ -40,7 +41,7 @@ def attributeToObject(parent, node): ...@@ -40,7 +41,7 @@ def attributeToObject(parent, node):
def textToObject(parent, node): def textToObject(parent, node):
# add this text to parents text content # add this text to parents text content
parent.text += node.data parent.text += unescape(node.data)
def processingInstructionToObject(parent, node): def processingInstructionToObject(parent, node):
# don't do anything with these # don't do anything with these
......
...@@ -165,7 +165,7 @@ class SerializeTestCase(unittest.TestCase): ...@@ -165,7 +165,7 @@ class SerializeTestCase(unittest.TestCase):
form.manage_addField('multi_field', '<Checkbox> Field', 'MultiCheckBoxField') form.manage_addField('multi_field', '<Checkbox> Field', 'MultiCheckBoxField')
form2 = ZMIForm('test2', 'ValueTest') form2 = ZMIForm('test2', 'ValueTest')
xml = formToXML(form) xml = formToXML(form)
XMLToForm(xml, form2) XMLToForm(xml, form2)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment