[1393] | 1 | """ |
---|
| 2 | Archetypes Marshall namespace but which can safely handle |
---|
| 3 | Control Characters for you |
---|
| 4 | """ |
---|
| 5 | |
---|
| 6 | from Products.Archetypes.interfaces.base import IBaseUnit |
---|
| 7 | |
---|
| 8 | from Products.Marshall import config |
---|
| 9 | from Products.Marshall.handlers.atxml import XmlNamespace |
---|
| 10 | from Products.Marshall.handlers.atxml import SchemaAttribute |
---|
| 11 | from Products.Marshall.handlers.atxml import getRegisteredNamespaces |
---|
| 12 | from Products.Marshall.exceptions import MarshallingException |
---|
| 13 | from Products.Marshall import utils |
---|
| 14 | |
---|
| 15 | from Products.Marshall.namespaces import atns as base |
---|
| 16 | |
---|
| 17 | from quintagroup.transmogrifier.namespaces.util import has_ctrlchars |
---|
| 18 | |
---|
| 19 | |
---|
| 20 | class ATAttribute(base.ATAttribute): |
---|
| 21 | |
---|
| 22 | def serialize(self, dom, parent_node, instance, options={}): |
---|
| 23 | |
---|
| 24 | values = self.get(instance) |
---|
| 25 | if not values: |
---|
| 26 | return |
---|
| 27 | |
---|
| 28 | is_ref = self.isReference(instance) |
---|
| 29 | |
---|
| 30 | for value in values: |
---|
| 31 | node = dom.createElementNS(self.namespace.xmlns, "field") |
---|
| 32 | name_attr = dom.createAttribute("name") |
---|
| 33 | name_attr.value = self.name |
---|
| 34 | node.setAttributeNode(name_attr) |
---|
| 35 | |
---|
| 36 | # try to get 'utf-8' encoded string |
---|
| 37 | if isinstance(value, unicode): |
---|
| 38 | value = value.encode('utf-8') |
---|
| 39 | elif IBaseUnit.isImplementedBy(value): |
---|
| 40 | value = value.getRaw(encoding='utf-8') |
---|
| 41 | else: |
---|
| 42 | value = str(value) |
---|
| 43 | |
---|
| 44 | if is_ref: |
---|
| 45 | if config.HANDLE_REFS: |
---|
| 46 | ref_node = dom.createElementNS(self.namespace.xmlns, |
---|
| 47 | 'reference') |
---|
| 48 | uid_node = dom.createElementNS(self.namespace.xmlns, |
---|
| 49 | 'uid') |
---|
| 50 | value = response.createTextNode(value) |
---|
| 51 | uid_node.append(value) |
---|
| 52 | ref_node.append(uid_node) |
---|
| 53 | node.append(ref_node) |
---|
| 54 | elif isinstance(value, str) and has_ctrlchars(value): |
---|
| 55 | value = value.encode('base64') |
---|
| 56 | attr = dom.createAttributeNS(self.namespace.xmlns, |
---|
| 57 | 'transfer_encoding') |
---|
| 58 | attr.value = 'base64' |
---|
| 59 | node.setAttributeNode(attr) |
---|
| 60 | value_node = dom.createCDATASection(value) |
---|
| 61 | node.appendChild(value_node) |
---|
| 62 | else: |
---|
| 63 | value_node = dom.createTextNode(value) |
---|
| 64 | node.appendChild(value_node) |
---|
| 65 | |
---|
| 66 | node.normalize() |
---|
| 67 | parent_node.appendChild(node) |
---|
| 68 | |
---|
| 69 | return True |
---|
| 70 | |
---|
| 71 | def processXmlValue(self, context, value ): |
---|
| 72 | value = value.strip() |
---|
| 73 | if not value: |
---|
| 74 | return |
---|
| 75 | |
---|
| 76 | # decode node value if needed |
---|
| 77 | te = context.node.get('transfer_encoding', None) |
---|
| 78 | if te is not None: |
---|
| 79 | value = value.decode(te) |
---|
| 80 | |
---|
| 81 | data = context.getDataFor(self.namespace.xmlns) |
---|
| 82 | if data.has_key(self.name): |
---|
| 83 | svalues = data[self.name] |
---|
| 84 | if not isinstance(svalues, list): |
---|
| 85 | data[self.name] = svalues = [svalues] |
---|
| 86 | svalues.append(value) |
---|
| 87 | return |
---|
| 88 | else: |
---|
| 89 | data[self.name] = value |
---|
| 90 | |
---|
| 91 | class Archetypes(base.Archetypes): |
---|
| 92 | |
---|
| 93 | def getAttributeByName(self, schema_name, context=None): |
---|
| 94 | if context is not None and schema_name not in self.at_fields: |
---|
| 95 | if not context.instance.Schema().has_key(schema_name): |
---|
| 96 | return |
---|
| 97 | raise AssertionError, \ |
---|
| 98 | "invalid attribute %s"%(schema_name) |
---|
| 99 | |
---|
| 100 | if schema_name in self.at_fields: |
---|
| 101 | return self.at_fields[schema_name] |
---|
| 102 | |
---|
| 103 | attribute = ATAttribute(schema_name) |
---|
| 104 | attribute.setNamespace(self) |
---|
| 105 | |
---|
| 106 | return attribute |
---|