[Zope3-checkins] CVS: Zope3/src/zope/app/dublincore - dcterms.py:1.5 xmlmetadata.py:1.3

Fred L. Drake, Jr. fred at zope.com
Wed Aug 27 01:43:55 EDT 2003


Update of /cvs-repository/Zope3/src/zope/app/dublincore
In directory cvs.zope.org:/tmp/cvs-serv28142

Modified Files:
	dcterms.py xmlmetadata.py 
Log Message:
The internal data structures of the serializer/de-serializer now use the
(namespaceURI, localName) to identify XML elements; prefix assignment
occurs during serialization.


=== Zope3/src/zope/app/dublincore/dcterms.py 1.4 => 1.5 ===
--- Zope3/src/zope/app/dublincore/dcterms.py:1.4	Fri Aug 22 09:09:37 2003
+++ Zope3/src/zope/app/dublincore/dcterms.py	Wed Aug 27 00:43:55 2003
@@ -184,7 +184,8 @@
     }
 
 element_to_name = {}
-for name, (qname, unused) in name_to_element.iteritems():
+for name, (qname, attrs) in name_to_element.iteritems():
     prefix, localname = qname.split(":")
     elem_name = _prefix_to_ns[prefix], localname
     element_to_name[elem_name] = name
+    name_to_element[name] = (elem_name, attrs)


=== Zope3/src/zope/app/dublincore/xmlmetadata.py 1.2 => 1.3 ===
--- Zope3/src/zope/app/dublincore/xmlmetadata.py:1.2	Fri Aug 22 09:03:19 2003
+++ Zope3/src/zope/app/dublincore/xmlmetadata.py	Wed Aug 27 00:43:55 2003
@@ -34,11 +34,12 @@
 
 def dumpString(mapping):
     sio = StringIO()
-    sio.write("<?xml version='1.0' encoding='utf-8'?>\n"
-              "<metadata\n"
-              "  xmlns:dc='http://purl.org/dc/elements/1.1/'\n"
-              "  xmlns:dcterms='http://purl.org/dc/terms/'\n"
-              "  xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'>\n")
+    nsmap = {
+        # prefix: [uri, used],
+        dcterms.DC_NS:      ("dc", False),
+        dcterms.DCTERMS_NS: ("dcterms", False),
+        dcterms.XSI_NS:     ("xsi", False),
+        }
     items = mapping.items()
     items.sort()
     prev = None
@@ -50,16 +51,35 @@
             prev = group
         if name in dcterms.name_to_element:
             element, t = dcterms.name_to_element[name]
+            nsuri, localname = element
+            if nsuri not in nsmap:
+                prefix = "ns%d" % (len(nsmap) - 2)
+                nsmap[nsuri] = prefix, True
+            else:
+                prefix, used = nsmap[nsuri]
+                if not used:
+                    nsmap[nsuri] = prefix, True
+            qname = "%s:%s" % (prefix, localname)
             if not type:
                 type = t
             if type:
                 type = " xsi:type=" + quoteattr(type)
+                nsmap[dcterms.XSI_NS] = "xsi", True
             for value in values:
                 sio.write("  <%s%s>\n    %s\n  </%s>\n"
-                          % (element, type, _encode_string(value), element))
+                          % (qname, type, _encode_string(value), qname))
         else:
             raise RuntimeError("could not serialize %r metadata element"
                                % name)
+    content = sio.getvalue()
+    sio = StringIO()
+    sio.write("<?xml version='1.0' encoding='utf-8'?>\n"
+              "<metadata")
+    for uri, (prefix, used) in nsmap.iteritems():
+        if used:
+            sio.write("\n  xmlns:%s=%s" % (prefix, quoteattr(uri)))
+    sio.write(">\n")
+    sio.write(content)
     sio.write("</metadata>\n")
     return sio.getvalue()
 




More information about the Zope3-Checkins mailing list