1 | """ |
---|
2 | DublinCore 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.namespaces import dcns as base |
---|
9 | from Products.Marshall.namespaces.dcns import normalizer |
---|
10 | |
---|
11 | from quintagroup.transmogrifier.namespaces.util import has_ctrlchars |
---|
12 | |
---|
13 | |
---|
14 | class DCAttribute(base.DCAttribute): |
---|
15 | |
---|
16 | def serialize(self, dom, parent_node, instance): |
---|
17 | values = self.get(instance) |
---|
18 | if not values: |
---|
19 | return False |
---|
20 | |
---|
21 | for value in values: |
---|
22 | elname = "%s:%s"%(self.namespace.prefix, self.name) |
---|
23 | node = dom.createElementNS(base.DublinCore.xmlns, elname) |
---|
24 | |
---|
25 | # try to get 'utf-8' encoded string |
---|
26 | if isinstance(value, unicode): |
---|
27 | value = value.encode('utf-8') |
---|
28 | elif IBaseUnit.isImplementedBy(value): |
---|
29 | value = value.getRaw(encoding='utf-8') |
---|
30 | else: |
---|
31 | value = str(value) |
---|
32 | |
---|
33 | if isinstance(value, str) and has_ctrlchars(value): |
---|
34 | value = value.encode('base64') |
---|
35 | attr = dom.createAttributeNS(base.DublinCore.xmlns, |
---|
36 | 'transfer_encoding') |
---|
37 | attr.value = 'base64' |
---|
38 | node.setAttributeNode(attr) |
---|
39 | value_node = dom.createCDATASection(value) |
---|
40 | else: |
---|
41 | value_node = dom.createTextNode(value) |
---|
42 | |
---|
43 | node.appendChild(value_node) |
---|
44 | node.normalize() |
---|
45 | parent_node.appendChild(node) |
---|
46 | return True |
---|
47 | |
---|
48 | def processXmlValue(self, context, value): |
---|
49 | value = value and value.strip() |
---|
50 | if not value: |
---|
51 | return |
---|
52 | |
---|
53 | # decode node value if needed |
---|
54 | te = context.node.get('transfer_encoding', None) |
---|
55 | if te is not None: |
---|
56 | value = value.decode(te) |
---|
57 | |
---|
58 | data = context.getDataFor(self.namespace.xmlns) |
---|
59 | if self.many: |
---|
60 | data.setdefault(self.name, []).append(value) |
---|
61 | else: |
---|
62 | data[self.name]=value |
---|
63 | |
---|
64 | class DublinCore(base.DublinCore): |
---|
65 | |
---|
66 | attributes = ( |
---|
67 | DCAttribute('title', 'Title', 'setTitle', |
---|
68 | process=(normalizer.space, normalizer.newline)), |
---|
69 | |
---|
70 | DCAttribute('description', 'Description', 'setDescription', |
---|
71 | process=(normalizer.space,)), |
---|
72 | |
---|
73 | DCAttribute('subject', 'Subject', 'setSubject', many=True), |
---|
74 | DCAttribute('contributor', 'Contributors', 'setContributors', |
---|
75 | many=True), |
---|
76 | # this attr diverges from cmfdefault.dublincore |
---|
77 | DCAttribute('creator', 'Creators', 'setCreators', many=True), |
---|
78 | DCAttribute('rights', 'Rights', 'setRights'), |
---|
79 | DCAttribute('language', 'Language', 'setLanguage') |
---|
80 | ) |
---|