]>
git.frykholm.com Git - friends.git/blob - friends/rd/xrd.py
2 from xml
.dom
.minidom
import getDOMImplementation
, parseString
, Node
4 from rd
.core
import RD
, Element
, Link
, Property
, Title
6 XRD_NAMESPACE
= "http://docs.oasis-open.org/ns/xri/xrd-1.0"
11 for node
in root
.childNodes
:
12 if node
.nodeType
== Node
.TEXT_NODE
and node
.nodeValue
:
13 text
+= node
.nodeValue
15 text
+= _get_text(node
)
16 return text
.strip() or None
23 def expires_handler(node
, obj
):
24 obj
.expires
= isodate
.parse_datetime(_get_text(node
))
26 def subject_handler(node
, obj
):
27 obj
.subject
= _get_text(node
)
29 def alias_handler(node
, obj
):
30 obj
.aliases
.append(_get_text(node
))
32 def property_handler(node
, obj
):
33 obj
.properties
.append(Property(node
.getAttribute('type'), _get_text(node
)))
35 def title_handler(node
, obj
):
36 obj
.titles
.append(Title(_get_text(node
), node
.getAttribute('xml:lang')))
38 def link_handler(node
, obj
):
40 l
.rel
= node
.getAttribute('rel')
41 l
.type = node
.getAttribute('type')
42 l
.href
= node
.getAttribute('href')
43 l
.template
= node
.getAttribute('template')
47 'Expires': expires_handler
,
48 'Subject': subject_handler
,
49 'Alias': alias_handler
,
50 'Property': property_handler
,
52 'Title': title_handler
,
55 def unknown_handler(node
, obj
):
56 obj
.elements
.append(Element(
58 value
=_get_text(node
),
61 def handle_node(node
, obj
):
62 handler
= handlers
.get(node
.nodeName
, unknown_handler
)
63 if handler
and node
.nodeType
== node
.ELEMENT_NODE
:
66 doc
= parseString(content
)
67 root
= doc
.documentElement
69 rd
= RD(root
.getAttribute('xml:id'))
71 for name
, value
in list(root
.attributes
.items()):
73 rd
.attributes
.append((name
, value
))
75 for node
in root
.childNodes
:
77 if node
.nodeName
== 'Link':
79 for child
in node
.childNodes
:
80 handle_node(child
, link
)
87 doc
= getDOMImplementation().createDocument(XRD_NAMESPACE
, "XRD", None)
88 root
= doc
.documentElement
89 root
.setAttribute('xmlns', XRD_NAMESPACE
)
92 root
.setAttribute('xml:id', xrd
.xml_id
)
94 for attr
in xrd
.attributes
:
95 root
.setAttribute(attr
.name
, attr
.value
)
98 node
= doc
.createElement('Expires')
99 node
.appendChild(doc
.createTextNode(xrd
.expires
.isoformat()))
100 root
.appendChild(node
)
103 node
= doc
.createElement('Subject')
104 node
.appendChild(doc
.createTextNode(xrd
.subject
))
105 root
.appendChild(node
)
107 for alias
in xrd
.aliases
:
108 node
= doc
.createElement('Alias')
109 node
.appendChild(doc
.createTextNode(alias
))
110 root
.appendChild(node
)
112 for prop
in xrd
.properties
:
113 node
= doc
.createElement('Property')
114 node
.setAttribute('type', prop
.type)
116 node
.appendChild(doc
.createTextNode(str(prop
.value
)))
118 node
.setAttribute('xsi:nil', 'true')
119 root
.appendChild(node
)
121 for element
in xrd
.elements
:
122 node
= doc
.createElement(element
.name
)
123 node
.appendChild(doc
.createTextNode(element
.value
))
124 root
.appendChild(node
)
126 for link
in xrd
.links
:
128 if link
.href
and link
.template
:
129 raise ValueError('only one of href or template attributes may be specified')
131 link_node
= doc
.createElement('Link')
134 link_node
.setAttribute('rel', link
.rel
)
137 link_node
.setAttribute('type', link
.type)
140 link_node
.setAttribute('href', link
.href
)
143 link_node
.setAttribute('template', link
.template
)
145 for title
in link
.titles
:
146 node
= doc
.createElement('Title')
147 node
.appendChild(doc
.createTextNode(str(title
)))
149 node
.setAttribute('xml:lang', title
.lang
)
150 link_node
.appendChild(node
)
152 for prop
in link
.properties
:
153 node
= doc
.createElement('Property')
154 node
.setAttribute('type', prop
.type)
156 node
.appendChild(doc
.createTextNode(str(prop
.value
)))
158 node
.setAttribute('xsi:nil', 'true')
159 link_node
.appendChild(node
)
161 root
.appendChild(link_node
)