]>
Commit | Line | Data |
---|---|---|
1 | #---------------------------------------------------------------------------- | |
2 | # Name: xmlmarshaller.py | |
3 | # Purpose: | |
4 | # | |
5 | # Author: John Spurling | |
6 | # | |
7 | # Created: 7/28/04 | |
8 | # CVS-ID: $Id$ | |
9 | # Copyright: (c) 2004-2005 ActiveGrid, Inc. | |
10 | # License: wxWindows License | |
11 | #---------------------------------------------------------------------------- | |
12 | import __builtin__ | |
13 | import sys | |
14 | from types import * | |
15 | import logging | |
16 | import xml.sax | |
17 | import xml.sax.handler | |
18 | import xml.sax.saxutils as saxutils | |
19 | from activegrid.util.lang import * | |
20 | import activegrid.util.aglogging as aglogging | |
21 | ||
22 | MODULE_PATH = "__main__" | |
23 | ||
24 | ## ToDO remove maxOccurs "unbounded" resolves to -1 hacks after bug 177 is fixed | |
25 | ||
26 | """ | |
27 | Special attributes that we recognize: | |
28 | ||
29 | name: __xmlname__ | |
30 | type: string | |
31 | description: the name of the xml element for the marshalled object | |
32 | ||
33 | name: __xmlattributes__ | |
34 | type: tuple or list | |
35 | description: the name(s) of the Lang string attribute(s) to be | |
36 | marshalled as xml attributes instead of nested xml elements. currently | |
37 | these can only be strings since there"s not a way to get the type | |
38 | information back when unmarshalling. | |
39 | ||
40 | name: __xmlexclude__ | |
41 | type: tuple or list | |
42 | description: the name(s) of the lang attribute(s) to skip when | |
43 | marshalling. | |
44 | ||
45 | name: __xmlrename__ | |
46 | type: dict | |
47 | description: describes an alternate Lang <-> XML name mapping. | |
48 | Normally the name mapping is the identity function. __xmlrename__ | |
49 | overrides that. The keys are the Lang names, the values are their | |
50 | associated XML names. | |
51 | ||
52 | name: __xmlflattensequence__ | |
53 | type: dict, tuple, or list | |
54 | description: the name(s) of the Lang sequence attribute(s) whose | |
55 | items are to be marshalled as a series of xml elements (with an | |
56 | optional keyword argument that specifies the element name to use) as | |
57 | opposed to containing them in a separate sequence element, e.g.: | |
58 | ||
59 | myseq = (1, 2) | |
60 | <!-- normal way of marshalling --> | |
61 | <myseq> | |
62 | <item objtype="int">1</item> | |
63 | <item objtype="int">2</item> | |
64 | </myseq> | |
65 | <!-- with __xmlflattensequence__ set to {"myseq": "squish"} --> | |
66 | <squish objtype="int">1</squish> | |
67 | <squish objtype="int">2</squish> | |
68 | ||
69 | name: __xmlnamespaces__ | |
70 | type: dict | |
71 | description: a dict of the namespaces that the object uses. Each item | |
72 | in the dict should consist of a prefix,url combination where the key is | |
73 | the prefix and url is the value, e.g.: | |
74 | ||
75 | __xmlnamespaces__ = { "xsd":"http://www.w3c.org/foo.xsd" } | |
76 | ||
77 | name: __xmldefaultnamespace__ | |
78 | type: String | |
79 | description: the prefix of a namespace defined in __xmlnamespaces__ that | |
80 | should be used as the default namespace for the object. | |
81 | ||
82 | name: __xmlattrnamespaces__ | |
83 | type: dict | |
84 | description: a dict assigning the Lang object"s attributes to the namespaces | |
85 | defined in __xmlnamespaces__. Each item in the dict should consist of a | |
86 | prefix,attributeList combination where the key is the prefix and the value is | |
87 | a list of the Lang attribute names. e.g.: | |
88 | ||
89 | __xmlattrnamespaces__ = { "ag":["firstName", "lastName", "addressLine1", "city"] } | |
90 | ||
91 | name: __xmlattrgroups__ | |
92 | type: dict | |
93 | description: a dict specifying groups of attributes to be wrapped in an enclosing tag. | |
94 | The key is the name of the enclosing tag; the value is a list of attributes to include | |
95 | within it. e.g. | |
96 | ||
97 | __xmlattrgroups__ = {"name": ["firstName", "lastName"], "address": ["addressLine1", "city", "state", "zip"]} | |
98 | ||
99 | """ | |
100 | ||
101 | global xmlMarshallerLogger | |
102 | xmlMarshallerLogger = logging.getLogger("activegrid.util.xmlmarshaller.marshal") | |
103 | xmlMarshallerLogger.setLevel(aglogging.LEVEL_WARN) | |
104 | # INFO : low-level info | |
105 | # DEBUG : debugging info | |
106 | ||
107 | global knownGlobalTypes | |
108 | ||
109 | ################################################################################ | |
110 | # | |
111 | # module exceptions | |
112 | # | |
113 | ################################################################################ | |
114 | ||
115 | class Error(Exception): | |
116 | """Base class for errors in this module.""" | |
117 | pass | |
118 | ||
119 | class UnhandledTypeException(Error): | |
120 | """Exception raised when attempting to marshal an unsupported | |
121 | type. | |
122 | """ | |
123 | def __init__(self, typename): | |
124 | self.typename = typename | |
125 | def __str__(self): | |
126 | return "%s is not supported for marshalling." % str(self.typename) | |
127 | ||
128 | class XMLAttributeIsNotStringType(Error): | |
129 | """Exception raised when an object"s attribute is specified to be | |
130 | marshalled as an XML attribute of the enclosing object instead of | |
131 | a nested element. | |
132 | """ | |
133 | def __init__(self, attrname, typename): | |
134 | self.attrname = attrname | |
135 | self.typename = typename | |
136 | def __str__(self): | |
137 | return """%s was set to be marshalled as an XML attribute | |
138 | instead of a nested element, but the object"s type is %s, not | |
139 | string.""" % (self.attrname, self.typename) | |
140 | ||
141 | class MarshallerException(Exception): | |
142 | pass | |
143 | ||
144 | ################################################################################ | |
145 | # | |
146 | # constants and such | |
147 | # | |
148 | ################################################################################ | |
149 | ||
150 | XMLNS = "xmlns" | |
151 | XMLNS_PREFIX = XMLNS + ":" | |
152 | XMLNS_PREFIX_LENGTH = len(XMLNS_PREFIX) | |
153 | ||
154 | BASETYPE_ELEMENT_NAME = "item" | |
155 | DICT_ITEM_NAME = "qqDictItem" | |
156 | DICT_ITEM_KEY_NAME = "key" | |
157 | DICT_ITEM_VALUE_NAME = "value" | |
158 | ||
159 | # This list doesn"t seem to be used. | |
160 | # Internal documentation or useless? You make the call! | |
161 | ##MEMBERS_TO_SKIP = ("__module__", "__doc__", "__xmlname__", "__xmlattributes__", | |
162 | ## "__xmlexclude__", "__xmlflattensequence__", "__xmlnamespaces__", | |
163 | ## "__xmldefaultnamespace__", "__xmlattrnamespaces__", | |
164 | ## "__xmlattrgroups__") | |
165 | ||
166 | ################################################################################ | |
167 | # | |
168 | # classes and functions | |
169 | # | |
170 | ################################################################################ | |
171 | ||
172 | def setattrignorecase(object, name, value): | |
173 | if (name not in object.__dict__): | |
174 | namelow = name.lower() | |
175 | for attr in object.__dict__: | |
176 | if attr.lower() == namelow: | |
177 | object.__dict__[attr] = value | |
178 | return | |
179 | object.__dict__[name] = value | |
180 | ||
181 | def getComplexType(obj): | |
182 | if (hasattr(obj, "__xsdcomplextype__")): | |
183 | return obj.__xsdcomplextype__ | |
184 | return None | |
185 | ||
186 | def _objectfactory(objname, objargs=None, xsname=None): | |
187 | "dynamically create an object based on the objname and return it." | |
188 | ||
189 | if not isinstance(objargs, list): | |
190 | objargs = [objargs] | |
191 | ||
192 | ## print "[objectfactory] xsname [%s]; objname [%s]" % (xsname, objname) | |
193 | ||
194 | # (a) deal with tagName:knownTypes mappings | |
195 | if (xsname != None): | |
196 | objclass = knownGlobalTypes.get(xsname) | |
197 | if (objclass != None): | |
198 | if (objargs != None): | |
199 | return objclass(*objargs) | |
200 | else: | |
201 | return objclass() | |
202 | ||
203 | # (b) next with intrinisic types | |
204 | if objname == "str" or objname == "unicode": # don"t strip: blanks are significant | |
205 | if len(objargs) > 0: | |
206 | return saxutils.unescape(objargs[0]).encode() | |
207 | else: | |
208 | return "" | |
209 | elif objname == "bool": | |
210 | return not objargs[0].lower() == "false" | |
211 | elif objname in ("float", "int", "long"): | |
212 | ## objargs = [x.strip() for x in objargs] | |
213 | return __builtin__.__dict__[objname](*objargs) | |
214 | elif objname == "None": | |
215 | return None | |
216 | ||
217 | # (c) objtype=path...module.class | |
218 | # split the objname into the typename and module path, | |
219 | # importing the module if need be. | |
220 | ## print "[objectfactory] creating an object of type %s and value %s, xsname=%s" % (objname, objargs, xsname) | |
221 | objtype = objname.split(".")[-1] | |
222 | pathlist = objname.split(".") | |
223 | modulename = ".".join(pathlist[0:-1]) | |
224 | ## print "[objectfactory] object [%s] %s(%r)" % (objname, objtype, objargs) | |
225 | ||
226 | try: | |
227 | if modulename: | |
228 | module = __import__(modulename) | |
229 | for name in pathlist[1:-1]: | |
230 | module = module.__dict__[name] | |
231 | elif __builtin__.__dict__.has_key(objname): | |
232 | module = __builtin__ | |
233 | else: | |
234 | raise MarshallerException("Could not find class %s" % objname) | |
235 | if objargs: | |
236 | return module.__dict__[objtype](*objargs) | |
237 | else: | |
238 | return module.__dict__[objtype]() | |
239 | except KeyError: | |
240 | raise MarshallerException("Could not find class %s" % objname) | |
241 | ||
242 | class Element: | |
243 | ||
244 | def __init__(self, name, attrs=None): | |
245 | self.name = name | |
246 | self.attrs = attrs | |
247 | self.content = "" | |
248 | self.children = [] | |
249 | ||
250 | def getobjtype(self): | |
251 | objtype = self.attrs.get("objtype") | |
252 | if (objtype == None): | |
253 | if (len(self.children) > 0): | |
254 | objtype = "dict" | |
255 | else: | |
256 | objtype = "str" | |
257 | return objtype | |
258 | ||
259 | def __str__(self): | |
260 | print " name = ", self.name, "; attrs = ", self.attrs, "number of children = ", len(self.children) | |
261 | i = -1 | |
262 | for child in self.children: | |
263 | i = i + 1 | |
264 | childClass = child.__class__.__name__ | |
265 | print " Child ", i, " class: ",childClass | |
266 | ||
267 | ||
268 | class XMLObjectFactory(xml.sax.ContentHandler): | |
269 | def __init__(self): | |
270 | self.rootelement = None | |
271 | self.elementstack = [] | |
272 | xml.sax.handler.ContentHandler.__init__(self) | |
273 | ||
274 | def __str__(self): | |
275 | print "-----XMLObjectFactory Dump-------------------------------" | |
276 | if (self.rootelement == None): | |
277 | print "rootelement is None" | |
278 | else: | |
279 | print "rootelement is an object" | |
280 | i = -1 | |
281 | print "length of elementstack is: ", len(self.elementstack) | |
282 | for e in self.elementstack: | |
283 | i = i + 1 | |
284 | print "elementstack[", i, "]: " | |
285 | str(e) | |
286 | print "-----end XMLObjectFactory--------------------------------" | |
287 | ||
288 | ## ContentHandler methods | |
289 | def startElement(self, name, attrs): | |
290 | ## print "startElement for name: ", name | |
291 | if name.find(":") > -1: # Strip namespace prefixes for now until actually looking them up in xsd | |
292 | name = name[name.find(":") + 1:] | |
293 | ## for attrname in attrs.getNames(): | |
294 | ## print "%s: %s" % (attrname, attrs.getValue(attrname)) | |
295 | element = Element(name, attrs.copy()) | |
296 | self.elementstack.append(element) | |
297 | ## print self.elementstack | |
298 | ||
299 | def characters(self, content): | |
300 | ## print "got content: %s (%s)" % (content, type(content)) | |
301 | if (content != None): | |
302 | self.elementstack[-1].content += content | |
303 | ||
304 | def endElement(self, name): | |
305 | ## print "[endElement] name of element we"re at the end of: %s" % name | |
306 | xsname = name | |
307 | if name.find(":") > -1: # Strip namespace prefixes for now until actually looking them up in xsd | |
308 | name = name[name.find(":") + 1:] | |
309 | oldChildren = self.elementstack[-1].children | |
310 | element = self.elementstack.pop() | |
311 | if ((len(self.elementstack) > 1) and (self.elementstack[-1].getobjtype() == "None")): | |
312 | parentElement = self.elementstack[-2] | |
313 | ## print "[endElement] %s: found parent with objtype==None: using its grandparent" % name | |
314 | elif (len(self.elementstack) > 0): | |
315 | parentElement = self.elementstack[-1] | |
316 | objtype = element.getobjtype() | |
317 | ## print "element objtype is: ", objtype | |
318 | if (objtype == "None"): | |
319 | ## print "[endElement] %s: skipping a (objtype==None) end tag" % name | |
320 | return | |
321 | constructorarglist = [] | |
322 | if (len(element.content) > 0): | |
323 | strippedElementContent = element.content.strip() | |
324 | if (len(strippedElementContent) > 0): | |
325 | constructorarglist.append(element.content) | |
326 | ## print "[endElement] calling objectfactory" | |
327 | obj = _objectfactory(objtype, constructorarglist, xsname) | |
328 | complexType = getComplexType(obj) | |
329 | if (obj != None): | |
330 | if (hasattr(obj, "__xmlname__") and getattr(obj, "__xmlname__") == "sequence"): | |
331 | self.elementstack[-1].children = oldChildren | |
332 | return | |
333 | if (len(element.attrs) > 0) and not isinstance(obj, list): | |
334 | ## print "[endElement] %s: element has attrs and the obj is not a list" % name | |
335 | for attrname, attr in element.attrs.items(): | |
336 | if attrname == XMLNS or attrname.startswith(XMLNS_PREFIX): | |
337 | if attrname.startswith(XMLNS_PREFIX): | |
338 | ns = attrname[XMLNS_PREFIX_LENGTH:] | |
339 | else: | |
340 | ns = "" | |
341 | if not hasattr(obj, "__xmlnamespaces__"): | |
342 | obj.__xmlnamespaces__ = {ns:attr} | |
343 | elif ns not in obj.__xmlnamespaces__: | |
344 | if (hasattr(obj.__class__, "__xmlnamespaces__") | |
345 | and (obj.__xmlnamespaces__ is obj.__class__.__xmlnamespaces__)): | |
346 | obj.__xmlnamespaces__ = dict(obj.__xmlnamespaces__) | |
347 | obj.__xmlnamespaces__[ns] = attr | |
348 | elif not attrname == "objtype": | |
349 | if attrname.find(":") > -1: # Strip namespace prefixes for now until actually looking them up in xsd | |
350 | attrname = attrname[attrname.find(":") + 1:] | |
351 | if (complexType != None): | |
352 | xsdElement = complexType.findElement(attrname) | |
353 | if (xsdElement != None): | |
354 | type = xsdElement.type | |
355 | if (type != None): | |
356 | type = xsdToLangType(type) | |
357 | ### ToDO remove maxOccurs hack after bug 177 is fixed | |
358 | if attrname == "maxOccurs" and attr == "unbounded": | |
359 | attr = "-1" | |
360 | attr = _objectfactory(type, attr) | |
361 | try: | |
362 | setattrignorecase(obj, _toAttrName(obj, attrname), attr) | |
363 | except AttributeError: | |
364 | raise MarshallerException("Error unmarshalling attribute \"%s\" of XML element \"%s\": object type not specified or known" % (attrname, name)) | |
365 | ## obj.__dict__[_toAttrName(obj, attrname)] = attr | |
366 | # stuff any child attributes meant to be in a sequence via the __xmlflattensequence__ | |
367 | flattenDict = {} | |
368 | if hasattr(obj, "__xmlflattensequence__"): | |
369 | flatten = obj.__xmlflattensequence__ | |
370 | ## print "[endElement] %s: obj has __xmlflattensequence__" % name | |
371 | if (isinstance(flatten, dict)): | |
372 | ## print "[endElement] dict with flatten.items: ", flatten.items() | |
373 | for sequencename, xmlnametuple in flatten.items(): | |
374 | if (xmlnametuple == None): | |
375 | flattenDict[sequencename] = sequencename | |
376 | elif (not isinstance(xmlnametuple, (tuple, list))): | |
377 | flattenDict[str(xmlnametuple)] = sequencename | |
378 | else: | |
379 | for xmlname in xmlnametuple: | |
380 | ## print "[endElement]: adding flattenDict[%s] = %s" % (xmlname, sequencename) | |
381 | flattenDict[xmlname] = sequencename | |
382 | else: | |
383 | raise "Invalid type for __xmlflattensequence___ : it must be a dict" | |
384 | ||
385 | # reattach an object"s attributes to it | |
386 | for childname, child in element.children: | |
387 | ## print "[endElement] childname is: ", childname, "; child is: ", child | |
388 | if (childname in flattenDict): | |
389 | sequencename = _toAttrName(obj, flattenDict[childname]) | |
390 | ## print "[endElement] sequencename is: ", sequencename | |
391 | if (not hasattr(obj, sequencename)): | |
392 | ## print "[endElement] obj.__dict__ is: ", obj.__dict__ | |
393 | obj.__dict__[sequencename] = [] | |
394 | sequencevalue = getattr(obj, sequencename) | |
395 | if (sequencevalue == None): | |
396 | obj.__dict__[sequencename] = [] | |
397 | sequencevalue = getattr(obj, sequencename) | |
398 | sequencevalue.append(child) | |
399 | elif (objtype == "list"): | |
400 | obj.append(child) | |
401 | elif isinstance(obj, dict): | |
402 | if (childname == DICT_ITEM_NAME): | |
403 | obj[child[DICT_ITEM_KEY_NAME]] = child[DICT_ITEM_VALUE_NAME] | |
404 | else: | |
405 | obj[childname] = child | |
406 | else: | |
407 | ## print "childname = %s, obj = %s, child = %s" % (childname, repr(obj), repr(child)) | |
408 | try: | |
409 | setattrignorecase(obj, _toAttrName(obj, childname), child) | |
410 | except AttributeError: | |
411 | raise MarshallerException("Error unmarshalling child element \"%s\" of XML element \"%s\": object type not specified or known" % (childname, name)) | |
412 | ## obj.__dict__[_toAttrName(obj, childname)] = child | |
413 | ||
414 | if (complexType != None): | |
415 | for element in complexType.elements: | |
416 | if element.default: | |
417 | elementName = _toAttrName(obj, element.name) | |
418 | if ((elementName not in obj.__dict__) or (obj.__dict__[elementName] == None)): | |
419 | langType = xsdToLangType(element.type) | |
420 | defaultValue = _objectfactory(langType, element.default) | |
421 | obj.__dict__[elementName] = defaultValue | |
422 | ||
423 | ifDefPy() | |
424 | if (isinstance(obj, list)): | |
425 | if ((element.attrs.has_key("mutable")) and (element.attrs.getValue("mutable") == "false")): | |
426 | obj = tuple(obj) | |
427 | endIfDef() | |
428 | ||
429 | if (len(self.elementstack) > 0): | |
430 | ## print "[endElement] appending child with name: ", name, "; objtype: ", objtype | |
431 | parentElement.children.append((name, obj)) | |
432 | ## print "parentElement now has ", len(parentElement.children), " children" | |
433 | else: | |
434 | self.rootelement = obj | |
435 | ||
436 | def getRootObject(self): | |
437 | return self.rootelement | |
438 | ||
439 | def _toAttrName(obj, name): | |
440 | if (hasattr(obj, "__xmlrename__")): | |
441 | for key, val in obj.__xmlrename__.iteritems(): | |
442 | if (name == val): | |
443 | name = key | |
444 | break | |
445 | ## if (name.startswith("__") and not name.endswith("__")): | |
446 | ## name = "_%s%s" % (obj.__class__.__name__, name) | |
447 | return name | |
448 | ||
449 | __typeMappingXsdToLang = { | |
450 | "string": "str", | |
451 | "char": "str", | |
452 | "varchar": "str", | |
453 | "date": "str", # ToDO Need to work out how to create lang date types | |
454 | "boolean": "bool", | |
455 | "decimal": "float", # ToDO Does python have a better fixed point type? | |
456 | "int": "int", | |
457 | "long": "long", | |
458 | "float": "float", | |
459 | "bool": "bool", | |
460 | "str": "str", | |
461 | "unicode": "unicode", | |
462 | "short": "int", | |
463 | "duration": "str", # see above (date) | |
464 | "datetime": "str", # see above (date) | |
465 | "time": "str", # see above (date) | |
466 | "double": "float", | |
467 | } | |
468 | ||
469 | def xsdToLangType(xsdType): | |
470 | langType = __typeMappingXsdToLang.get(xsdType) | |
471 | if (langType == None): | |
472 | raise Exception("Unknown xsd type %s" % xsdType) | |
473 | return langType | |
474 | ||
475 | def _getXmlValue(langValue): | |
476 | if (isinstance(langValue, bool)): | |
477 | return str(langValue).lower() | |
478 | elif (isinstance(langValue, unicode)): | |
479 | return langValue.encode() | |
480 | else: | |
481 | return str(langValue) | |
482 | ||
483 | def unmarshal(xmlstr, knownTypes=None): | |
484 | global knownGlobalTypes | |
485 | if (knownTypes == None): | |
486 | knownGlobalTypes = {} | |
487 | else: | |
488 | knownGlobalTypes = knownTypes | |
489 | objectfactory = XMLObjectFactory() | |
490 | xml.sax.parseString(xmlstr, objectfactory) | |
491 | return objectfactory.getRootObject() | |
492 | ||
493 | ||
494 | def marshal(obj, elementName=None, prettyPrint=False, indent=0, knownTypes=None, encoding=-1): | |
495 | xmlstr = "".join(_marshal(obj, elementName, prettyPrint=prettyPrint, indent=indent, knownTypes=knownTypes)) | |
496 | if (isinstance(encoding, basestring)): | |
497 | return '<?xml version="1.0" encoding="%s"?>\n%s' % (encoding, xmlstr.encode(encoding)) | |
498 | elif (encoding == None): | |
499 | return xmlstr | |
500 | else: | |
501 | return '<?xml version="1.0" encoding="%s"?>\n%s' % (sys.getdefaultencoding(), xmlstr) | |
502 | ||
503 | def _marshal(obj, elementName=None, nameSpacePrefix="", nameSpaces=None, prettyPrint=False, indent=0, knownTypes=None): | |
504 | xmlMarshallerLogger.debug("--> _marshal: elementName=%s, type=%s, obj=%s" % (elementName, type(obj), str(obj))) | |
505 | xmlString = None | |
506 | if prettyPrint or indent: | |
507 | prefix = " "*indent | |
508 | newline = "\n" | |
509 | increment = 4 | |
510 | else: | |
511 | prefix = "" | |
512 | newline = "" | |
513 | increment = 0 | |
514 | ||
515 | ## Determine the XML element name. If it isn"t specified in the | |
516 | ## parameter list, look for it in the __xmlname__ Lang | |
517 | ## attribute, else use the default generic BASETYPE_ELEMENT_NAME. | |
518 | if not nameSpaces: nameSpaces = {} # Need to do this since if the {} is a default parameter it gets shared by all calls into the function | |
519 | nameSpaceAttrs = "" | |
520 | if knownTypes == None: | |
521 | knownTypes = {} | |
522 | if hasattr(obj, "__xmlnamespaces__"): | |
523 | for nameSpaceKey, nameSpaceUrl in getattr(obj, "__xmlnamespaces__").items(): | |
524 | if nameSpaceUrl in asDict(nameSpaces): | |
525 | nameSpaceKey = nameSpaces[nameSpaceUrl] | |
526 | else: | |
527 | ## # TODO: Wait to do this until there is shared for use when going through the object graph | |
528 | ## origNameSpaceKey = nameSpaceKey # Make sure there is no key collision, ie: same key referencing two different URL"s | |
529 | ## i = 1 | |
530 | ## while nameSpaceKey in nameSpaces.values(): | |
531 | ## nameSpaceKey = origNameSpaceKey + str(i) | |
532 | ## i += 1 | |
533 | nameSpaces[nameSpaceUrl] = nameSpaceKey | |
534 | if nameSpaceKey == "": | |
535 | nameSpaceAttrs += ' xmlns="%s" ' % (nameSpaceUrl) | |
536 | else: | |
537 | nameSpaceAttrs += ' xmlns:%s="%s" ' % (nameSpaceKey, nameSpaceUrl) | |
538 | nameSpaceAttrs = nameSpaceAttrs.rstrip() | |
539 | if hasattr(obj, "__xmldefaultnamespace__"): | |
540 | nameSpacePrefix = getattr(obj, "__xmldefaultnamespace__") + ":" | |
541 | if not elementName: | |
542 | if hasattr(obj, "__xmlname__"): | |
543 | elementName = nameSpacePrefix + obj.__xmlname__ | |
544 | else: | |
545 | elementName = nameSpacePrefix + BASETYPE_ELEMENT_NAME | |
546 | else: | |
547 | elementName = nameSpacePrefix + elementName | |
548 | if hasattr(obj, "__xmlsequencer__"): | |
549 | elementAdd = obj.__xmlsequencer__ | |
550 | else: | |
551 | elementAdd = None | |
552 | ||
553 | ## print "marshal: entered with elementName: ", elementName | |
554 | members_to_skip = [] | |
555 | ## Add more members_to_skip based on ones the user has selected | |
556 | ## via the __xmlexclude__ attribute. | |
557 | if hasattr(obj, "__xmlexclude__"): | |
558 | ## print "marshal: found __xmlexclude__" | |
559 | members_to_skip.extend(obj.__xmlexclude__) | |
560 | # Marshal the attributes that are selected to be XML attributes. | |
561 | objattrs = "" | |
562 | className = ag_className(obj) | |
563 | classNamePrefix = "_" + className | |
564 | if hasattr(obj, "__xmlattributes__"): | |
565 | ## print "marshal: found __xmlattributes__" | |
566 | xmlattributes = obj.__xmlattributes__ | |
567 | members_to_skip.extend(xmlattributes) | |
568 | for attr in xmlattributes: | |
569 | internalAttrName = attr | |
570 | ifDefPy() | |
571 | if (attr.startswith("__") and not attr.endswith("__")): | |
572 | internalAttrName = classNamePrefix + attr | |
573 | endIfDef() | |
574 | # Fail silently if a python attribute is specified to be | |
575 | # an XML attribute but is missing. | |
576 | ## print "marshal: processing attribute ", internalAttrName | |
577 | attrs = obj.__dict__ | |
578 | value = attrs.get(internalAttrName) | |
579 | xsdElement = None | |
580 | complexType = getComplexType(obj) | |
581 | if (complexType != None): | |
582 | ## print "marshal: found __xsdcomplextype__" | |
583 | xsdElement = complexType.findElement(attr) | |
584 | if (xsdElement != None): | |
585 | default = xsdElement.default | |
586 | if (default != None): | |
587 | if ((default == value) or (default == _getXmlValue(value))): | |
588 | continue | |
589 | else: | |
590 | if (value == None): | |
591 | continue | |
592 | elif value == None: | |
593 | continue | |
594 | ||
595 | # ToDO remove maxOccurs hack after bug 177 is fixed | |
596 | if attr == "maxOccurs" and value == -1: | |
597 | value = "unbounded" | |
598 | ||
599 | if isinstance(value, bool): | |
600 | if value == True: | |
601 | value = "true" | |
602 | else: | |
603 | value = "false" | |
604 | ||
605 | attrNameSpacePrefix = "" | |
606 | if hasattr(obj, "__xmlattrnamespaces__"): | |
607 | ## print "marshal: found __xmlattrnamespaces__" | |
608 | for nameSpaceKey, nameSpaceAttributes in getattr(obj, "__xmlattrnamespaces__").iteritems(): | |
609 | if nameSpaceKey == nameSpacePrefix[:-1]: # Don't need to specify attribute namespace if it is the same as its element | |
610 | continue | |
611 | if attr in nameSpaceAttributes: | |
612 | attrNameSpacePrefix = nameSpaceKey + ":" | |
613 | break | |
614 | ## if attr.startswith("_"): | |
615 | ## attr = attr[1:] | |
616 | if (hasattr(obj, "__xmlrename__") and attr in asDict(obj.__xmlrename__)): | |
617 | ## print "marshal: found __xmlrename__ (and its attribute)" | |
618 | attr = obj.__xmlrename__[attr] | |
619 | ||
620 | objattrs += ' %s%s="%s"' % (attrNameSpacePrefix, attr, str(value)) | |
621 | ## print "marshal: new objattrs is: ", objattrs | |
622 | ||
623 | if (obj == None): | |
624 | xmlString = [""] | |
625 | elif isinstance(obj, bool): | |
626 | xmlString = ['%s<%s objtype="bool">%s</%s>%s' % (prefix, elementName, obj, elementName, newline)] | |
627 | elif isinstance(obj, int): | |
628 | xmlString = ['%s<%s objtype="int">%s</%s>%s' % (prefix, elementName, str(obj), elementName, newline)] | |
629 | elif isinstance(obj, long): | |
630 | xmlString = ['%s<%s objtype="long">%s</%s>%s' % (prefix, elementName, str(obj), elementName, newline)] | |
631 | elif isinstance(obj, float): | |
632 | xmlString = ['%s<%s objtype="float">%s</%s>%s' % (prefix, elementName, str(obj), elementName, newline)] | |
633 | elif isinstance(obj, unicode): # have to check before basestring - unicode is instance of base string | |
634 | xmlString = ['%s<%s>%s</%s>%s' % (prefix, elementName, saxutils.escape(obj.encode()), elementName, newline)] | |
635 | elif isinstance(obj, basestring): | |
636 | xmlString = ['%s<%s>%s</%s>%s' % (prefix, elementName, saxutils.escape(obj), elementName, newline)] | |
637 | elif isinstance(obj, list): | |
638 | if len(obj) < 1: | |
639 | xmlString = "" | |
640 | else: | |
641 | xmlString = ['%s<%s objtype="list">%s' % (prefix, elementName, newline)] | |
642 | for item in obj: | |
643 | xmlString.extend(_marshal(item, nameSpaces=nameSpaces, indent=indent+increment, knownTypes=knownTypes)) | |
644 | xmlString.append("%s</%s>%s" % (prefix, elementName, newline)) | |
645 | elif isinstance(obj, tuple): | |
646 | if len(obj) < 1: | |
647 | xmlString = "" | |
648 | else: | |
649 | xmlString = ['%s<%s objtype="list" mutable="false">%s' % (prefix, elementName, newline)] | |
650 | for item in obj: | |
651 | xmlString.extend(_marshal(item, nameSpaces=nameSpaces, indent=indent+increment, knownTypes=knownTypes)) | |
652 | xmlString.append("%s</%s>%s" % (prefix, elementName, newline)) | |
653 | elif isinstance(obj, dict): | |
654 | xmlString = ['%s<%s objtype="dict">%s' % (prefix, elementName, newline)] | |
655 | subprefix = prefix + " "*increment | |
656 | subindent = indent + 2*increment | |
657 | for key, val in obj.iteritems(): | |
658 | ## if (isinstance(key, basestring) and key is legal identifier): | |
659 | ## xmlString.extend(_marshal(val, elementName=key, nameSpaces=nameSpaces, indent=subindent, knownTypes=knownTypes)) | |
660 | ## else: | |
661 | xmlString.append("%s<%s>%s" % (subprefix, DICT_ITEM_NAME, newline)) | |
662 | xmlString.extend(_marshal(key, elementName=DICT_ITEM_KEY_NAME, indent=subindent, knownTypes=knownTypes)) | |
663 | xmlString.extend(_marshal(val, elementName=DICT_ITEM_VALUE_NAME, nameSpaces=nameSpaces, indent=subindent, knownTypes=knownTypes)) | |
664 | xmlString.append("%s</%s>%s" % (subprefix, DICT_ITEM_NAME, newline)) | |
665 | xmlString.append("%s</%s>%s" % (prefix, elementName, newline)) | |
666 | else: | |
667 | # Only add the objtype if the element tag is unknown to us. | |
668 | objname = knownTypes.get(elementName) | |
669 | if (objname != None): | |
670 | xmlString = ["%s<%s%s%s" % (prefix, elementName, nameSpaceAttrs, objattrs)] | |
671 | else: | |
672 | xmlString = ['%s<%s%s%s objtype="%s.%s"' % (prefix, elementName, nameSpaceAttrs, objattrs, obj.__class__.__module__, className)] | |
673 | # get the member, value pairs for the object, filtering out the types we don"t support | |
674 | if (elementAdd != None): | |
675 | prefix += increment*" " | |
676 | indent += increment | |
677 | ||
678 | xmlMemberString = [] | |
679 | if hasattr(obj, "__xmlbody__"): | |
680 | xmlbody = getattr(obj, obj.__xmlbody__) | |
681 | if xmlbody != None: | |
682 | xmlMemberString.append(xmlbody) | |
683 | else: | |
684 | if hasattr(obj, "__xmlattrgroups__"): | |
685 | attrGroups = obj.__xmlattrgroups__.copy() | |
686 | if (not isinstance(attrGroups, dict)): | |
687 | raise "__xmlattrgroups__ is not a dict, but must be" | |
688 | for n in attrGroups.iterkeys(): | |
689 | members_to_skip.extend(attrGroups[n]) | |
690 | else: | |
691 | attrGroups = {} | |
692 | # add the list of all attributes to attrGroups | |
693 | eList = obj.__dict__.keys() | |
694 | eList.sort() | |
695 | attrGroups["__nogroup__"] = eList | |
696 | ||
697 | for eName, eList in attrGroups.iteritems(): | |
698 | if (eName != "__nogroup__"): | |
699 | prefix += increment*" " | |
700 | indent += increment | |
701 | xmlMemberString.append('%s<%s objtype="None">%s' % (prefix, eName, newline)) | |
702 | for name in eList: | |
703 | value = obj.__dict__[name] | |
704 | if eName == "__nogroup__" and name in members_to_skip: continue | |
705 | if name.startswith("__") and name.endswith("__"): continue | |
706 | subElementNameSpacePrefix = nameSpacePrefix | |
707 | if hasattr(obj, "__xmlattrnamespaces__"): | |
708 | for nameSpaceKey, nameSpaceValues in getattr(obj, "__xmlattrnamespaces__").iteritems(): | |
709 | if name in nameSpaceValues: | |
710 | subElementNameSpacePrefix = nameSpaceKey + ":" | |
711 | break | |
712 | # handle sequences listed in __xmlflattensequence__ | |
713 | # specially: instead of listing the contained items inside | |
714 | # of a separate list, as God intended, list them inside | |
715 | # the object containing the sequence. | |
716 | if (hasattr(obj, "__xmlflattensequence__") and (value != None) and (name in asDict(obj.__xmlflattensequence__))): | |
717 | xmlnametuple = obj.__xmlflattensequence__[name] | |
718 | if (xmlnametuple == None): | |
719 | xmlnametuple = [name] | |
720 | elif (not isinstance(xmlnametuple, (tuple,list))): | |
721 | xmlnametuple = [str(xmlnametuple)] | |
722 | xmlname = None | |
723 | if (len(xmlnametuple) == 1): | |
724 | xmlname = xmlnametuple[0] | |
725 | ## ix = 0 | |
726 | for seqitem in value: | |
727 | ## xmlname = xmlnametuple[ix] | |
728 | ## ix += 1 | |
729 | ## if (ix >= len(xmlnametuple)): | |
730 | ## ix = 0 | |
731 | xmlMemberString.extend(_marshal(seqitem, xmlname, subElementNameSpacePrefix, nameSpaces=nameSpaces, indent=indent+increment, knownTypes=knownTypes)) | |
732 | else: | |
733 | if (hasattr(obj, "__xmlrename__") and name in asDict(obj.__xmlrename__)): | |
734 | xmlname = obj.__xmlrename__[name] | |
735 | else: | |
736 | xmlname = name | |
737 | xmlMemberString.extend(_marshal(value, xmlname, subElementNameSpacePrefix, nameSpaces=nameSpaces, indent=indent+increment, knownTypes=knownTypes)) | |
738 | if (eName != "__nogroup__"): | |
739 | xmlMemberString.append("%s</%s>%s" % (prefix, eName, newline)) | |
740 | prefix = prefix[:-increment] | |
741 | indent -= increment | |
742 | ||
743 | # if we have nested elements, add them here, otherwise close the element tag immediately. | |
744 | newList = [] | |
745 | for s in xmlMemberString: | |
746 | if (len(s) > 0): newList.append(s) | |
747 | xmlMemberString = newList | |
748 | if len(xmlMemberString) > 0: | |
749 | xmlString.append(">") | |
750 | if hasattr(obj, "__xmlbody__"): | |
751 | xmlString.extend(xmlMemberString) | |
752 | xmlString.append("</%s>%s" % (elementName, newline)) | |
753 | else: | |
754 | xmlString.append(newline) | |
755 | if (elementAdd != None): | |
756 | xmlString.append("%s<%s>%s" % (prefix, elementAdd, newline)) | |
757 | xmlString.extend(xmlMemberString) | |
758 | if (elementAdd != None): | |
759 | xmlString.append("%s</%s>%s" % (prefix, elementAdd, newline)) | |
760 | prefix = prefix[:-increment] | |
761 | indent -= increment | |
762 | xmlString.append("%s</%s>%s" % (prefix, elementName, newline)) | |
763 | else: | |
764 | xmlString.append("/>%s" % newline) | |
765 | ## return xmlString | |
766 | xmlMarshallerLogger.debug("<-- _marshal: %s" % str(xmlString)) | |
767 | return xmlString | |
768 | ||
769 | # A simple test, to be executed when the xmlmarshaller is run standalone | |
770 | class MarshallerPerson: | |
771 | __xmlname__ = "person" | |
772 | __xmlexclude__ = ["fabulousness",] | |
773 | __xmlattributes__ = ("nonSmoker",) | |
774 | __xmlrename__ = {"_phoneNumber": "telephone"} | |
775 | __xmlflattensequence__ = {"favoriteWords": ("vocabulary",)} | |
776 | __xmlattrgroups__ = {"name": ["firstName", "lastName"], "address": ["addressLine1", "city", "state", "zip"]} | |
777 | ||
778 | def setPerson(self): | |
779 | self.firstName = "Albert" | |
780 | self.lastName = "Camus" | |
781 | self.addressLine1 = "23 Absurd St." | |
782 | self.city = "Ennui" | |
783 | self.state = "MO" | |
784 | self.zip = "54321" | |
785 | self._phoneNumber = "808-303-2323" | |
786 | self.favoriteWords = ["angst", "ennui", "existence"] | |
787 | self.phobias = ["war", "tuberculosis", "cars"] | |
788 | self.weight = 150 | |
789 | self.fabulousness = "tres tres" | |
790 | self.nonSmoker = False | |
791 | ||
792 | if isMain(__name__): | |
793 | p1 = MarshallerPerson() | |
794 | p1.setPerson() | |
795 | xmlP1 = marshal(p1, prettyPrint=True, encoding="utf-8") | |
796 | print "\n########################" | |
797 | print "# testPerson test case #" | |
798 | print "########################" | |
799 | print xmlP1 | |
800 | p2 = unmarshal(xmlP1) | |
801 | xmlP2 = marshal(p2, prettyPrint=True, encoding="utf-8") | |
802 | if xmlP1 == xmlP2: | |
803 | print "Success: repeated marshalling yields identical results" | |
804 | else: | |
805 | print "Failure: repeated marshalling yields different results" | |
806 | print xmlP2 |