Author: jmorliaguet
Date: Sun Apr 30 12:31:31 2006
New Revision: 2992

Modified:
   cpsskins/branches/paris-sprint-2006/setup/README.txt
   cpsskins/branches/paris-sprint-2006/setup/interfaces.py
   cpsskins/branches/paris-sprint-2006/setup/io.py
Log:

- added an option to write some fields such as 'title' and 'description'
  as attributes instead of child elements.

  cf http://www.w3schools.com/xml/xml_attributes.asp

  typically these are metadata fields



Modified: cpsskins/branches/paris-sprint-2006/setup/README.txt
==============================================================================
--- cpsskins/branches/paris-sprint-2006/setup/README.txt        (original)
+++ cpsskins/branches/paris-sprint-2006/setup/README.txt        Sun Apr 30 
12:31:31 2006
@@ -499,11 +499,12 @@
 
     >>> from cpsskins.setup.interfaces import IDOMAdapter
 
-    >>> def toXML(obj, title):
+    >>> def toXML(obj, title=u'resources', attributes=()):
     ...     document = dom.createDocument(None, title, None)
     ...     doc = document.documentElement
     ...     exporter = getMultiAdapter((obj, doc), IDOMAdapter)
     ...     exporter.setDocument(document)
+    ...     exporter.writeAsAttributes(attributes)
     ...     exporter.save()
     ...     return document.toprettyxml(indent=u'  ', encoding=u'utf-8')
 
@@ -605,22 +606,16 @@
     >>> slot = Slot(u'Some slot')
     >>> pageblock[u'cell1'][u'slot'] = slot
 
-    >>> print toXML(pageblock, u'elements')
+    >>> print toXML(pageblock, u'elements', (u'title', u'description'))
     <?xml version="1.0" encoding="utf-8"?>
     <elements>
-      <pageblock uri="cpsskins://canvas-pageblock:12345">
-        <title value="Some page block"/>
-        <cell uri="cpsskins://canvas-cell:12345">
-          <title value="Some cell 1"/>
-          <slot uri="cpsskins://canvas-slot:">
-            <title value="Some slot"/>
-            <description value=""/>
+      <pageblock title="Some page block" 
uri="cpsskins://canvas-pageblock:12345">
+        <cell title="Some cell 1" uri="cpsskins://canvas-cell:12345">
+          <slot description value="" title="Some slot" 
uri="cpsskins://canvas-slot:"/>
             <slot value=""/>
           </slot>
         </cell>
-        <cell uri="cpsskins://canvas-cell:12345">
-          <title value="Some cell 2"/>
-        </cell>
+        <cell title="Some cell 2" uri="cpsskins://canvas-cell:12345"/>
       </pageblock>
     </elements>
     <BLANKLINE>
@@ -633,34 +628,29 @@
     >>> root = getRootFolder()
     >>> theme = addThemeSkeleton(tmutil)
 
-    >>> print toXML(theme, u'themes')
+    >>> print toXML(theme, u'themes', (u'title', ))
     <?xml version="1.0" encoding="utf-8"?>
     <themes>
-      <theme uri="cpsskins://canvas-theme:12345">
-        <title value="A theme"/>
-        <themepage uri="cpsskins://canvas-themepage:12345">
-          <title value="A page"/>
-          <pageblock uri="cpsskins://canvas-pageblock:12345">
-            <title value="A page block"/>
-            <cell uri="cpsskins://canvas-cell:12345">
-              <title value="A cell"/>
-            </cell>
+      <theme title="A theme" uri="cpsskins://canvas-theme:12345">
+        <themepage title="A page" uri="cpsskins://canvas-themepage:12345">
+          <pageblock title="A page block" 
uri="cpsskins://canvas-pageblock:12345">
+            <cell title="A cell" uri="cpsskins://canvas-cell:12345"/>
           </pageblock>
         </themepage>
       </theme>
     </themes>
     <BLANKLINE>
 
+
 Export of portlets
 ------------------
 
     >>> portlet = Actions(title=u'Action portlet', category=u'zmi_views')
 
-    >>> print toXML(portlet, u'portlets')
+    >>> print toXML(portlet, u'portlets',  (u'title', ))
     <?xml version="1.0" encoding="utf-8"?>
     <portlets>
-      <portlet uri="cpsskins://canvas-portlet-standard.actions:12345">
-        <title value="Action portlet"/>
+      <portlet title="Action portlet" 
uri="cpsskins://canvas-portlet-standard.actions:12345">
         <category value="zmi_views"/>
       </portlet>
     </portlets>

Modified: cpsskins/branches/paris-sprint-2006/setup/interfaces.py
==============================================================================
--- cpsskins/branches/paris-sprint-2006/setup/interfaces.py     (original)
+++ cpsskins/branches/paris-sprint-2006/setup/interfaces.py     Sun Apr 30 
12:31:31 2006
@@ -103,12 +103,17 @@
 
     mapping = Attribute(u"Mapping between old and new URIs")
 
+    attributes = Attribute(u"The list of fields written as attributes.")
+
     def setDocument(document):
         """Set the document element"""
 
     def getDocument():
         """Get the document element"""
 
+    def writeAsAttributes():
+        """Set the list of fields written as attributes."""
+
     def save():
         """Save data as a DOM fragment."""
 

Modified: cpsskins/branches/paris-sprint-2006/setup/io.py
==============================================================================
--- cpsskins/branches/paris-sprint-2006/setup/io.py     (original)
+++ cpsskins/branches/paris-sprint-2006/setup/io.py     Sun Apr 30 12:31:31 2006
@@ -35,6 +35,7 @@
     """
     document = None
     mapping = {}
+    attributes = ()
 
     def __init__(self, context, node):
         self.context = context
@@ -49,6 +50,9 @@
                 "use setDocument(document) to set the document element.")
         return self.document
 
+    def writeAsAttributes(self, attributes):
+        self.attributes = attributes
+
     def save(self):
         """Save data in the DOM fragment.
         """
@@ -76,8 +80,8 @@
         # properties
         schema = IType(context).getContentType()
 
-        for name, field in zope.schema.getFieldsInOrder(schema):
-            obj = getattr(context, name)
+        for attr, field in zope.schema.getFieldsInOrder(schema):
+            obj = getattr(context, attr)
 
             field_io = IFieldIO(field, None)
             if field_io is not None:
@@ -85,10 +89,16 @@
             else:
                 value = obj
 
-            prop_el = document.createElement(name)
-            prop_el.setAttribute(u'value', value)
+            # set the property as an attribute,
+            if attr in self.attributes:
+                element_el.setAttribute(attr, value)
+
+            # or as a child elements
+            else:
+                prop_el = document.createElement(attr)
+                prop_el.setAttribute(u'value', value)
 
-            element_el.appendChild(prop_el)
+                element_el.appendChild(prop_el)
 
         node.appendChild(element_el)
 
@@ -98,6 +108,7 @@
                 exporter = getMultiAdapter((context[child], element_el),
                                             IDOMAdapter)
                 exporter.setDocument(document)
+                exporter.writeAsAttributes(self.attributes)
                 exporter.save()
 
     def load(self):
-- 
http://lists.nuxeo.com/mailman/listinfo/z3lab-checkins

Reply via email to