2 XML tag writer utilities.
7 from __future__ import absolute_import
8 #Init has to be imported first because it has code to workaround the python bug where relative imports don't work if the module is imported as a main module.
14 __author__ = 'Enrique Perez (perez_enrique@yahoo.com)'
15 __credits__ = 'Nophead <http://hydraraptor.blogspot.com/>\nArt of Illusion <http://www.artofillusion.org/>'
16 __date__ = '$Date: 2008/21/04 $'
17 __license__ = 'GNU Affero General Public License http://www.gnu.org/licenses/agpl.html'
20 def addBeginEndInnerXMLTag(attributes, depth, innerText, localName, output, text=''):
21 'Add the begin and end xml tag and the inner text if any.'
22 if len( innerText ) > 0:
23 addBeginXMLTag(attributes, depth, localName, output, text)
24 output.write( innerText )
25 addEndXMLTag(depth, localName, output)
27 addClosedXMLTag(attributes, depth, localName, output, text)
29 def addBeginXMLTag(attributes, depth, localName, output, text=''):
30 'Add the begin xml tag.'
31 depthStart = '\t' * depth
32 output.write('%s<%s%s>%s\n' % (depthStart, localName, getAttributesString(attributes), text))
34 def addClosedXMLTag(attributes, depth, localName, output, text=''):
35 'Add the closed xml tag.'
36 depthStart = '\t' * depth
37 attributesString = getAttributesString(attributes)
39 output.write('%s<%s%s >%s</%s>\n' % (depthStart, localName, attributesString, text, localName))
41 output.write('%s<%s%s />\n' % (depthStart, localName, attributesString))
43 def addEndXMLTag(depth, localName, output):
44 'Add the end xml tag.'
45 depthStart = '\t' * depth
46 output.write('%s</%s>\n' % (depthStart, localName))
48 def addXMLFromLoopComplexZ(attributes, depth, loop, output, z):
50 addBeginXMLTag(attributes, depth, 'path', output)
51 for pointComplexIndex in xrange(len(loop)):
52 pointComplex = loop[pointComplexIndex]
53 addXMLFromXYZ(depth + 1, pointComplexIndex, output, pointComplex.real, pointComplex.imag, z)
54 addEndXMLTag(depth, 'path', output)
56 def addXMLFromObjects(depth, objects, output):
57 'Add xml from objects.'
58 for object in objects:
59 object.addXML(depth, output)
61 def addXMLFromVertexes(depth, output, vertexes):
63 for vertexIndex in xrange(len(vertexes)):
64 vertex = vertexes[vertexIndex]
65 addXMLFromXYZ(depth + 1, vertexIndex, output, vertex.x, vertex.y, vertex.z)
67 def addXMLFromXYZ(depth, index, output, x, y, z):
68 'Add xml from x, y & z.'
69 attributes = {'index' : str(index)}
71 attributes['x'] = str(x)
73 attributes['y'] = str(y)
75 attributes['z'] = str(z)
76 addClosedXMLTag(attributes, depth, 'vertex', output)
78 def compareAttributeKeyAscending(key, otherKey):
79 'Get comparison in order to sort attribute keys in ascending order, with the id key first and name second.'
86 if otherKey == 'name':
90 return int(key > otherKey)
92 def getAttributesString(attributes):
93 'Add the closed xml tag.'
95 attributesKeys = attributes.keys()
96 attributesKeys.sort(compareAttributeKeyAscending)
97 for attributesKey in attributesKeys:
98 valueString = str(attributes[attributesKey])
99 if "'" in valueString:
100 attributesString += ' %s="%s"' % (attributesKey, valueString)
102 attributesString += " %s='%s'" % (attributesKey, valueString)
103 return attributesString
105 def getBeginGeometryXMLOutput(elementNode=None):
106 'Get the beginning of the string representation of this boolean geometry object info.'
107 output = getBeginXMLOutput()
109 if elementNode != None:
110 documentElement = elementNode.getDocumentElement()
111 attributes = documentElement.attributes
112 addBeginXMLTag(attributes, 0, 'fabmetheus', output)
115 def getBeginXMLOutput():
116 'Get the beginning of the string representation of this object info.'
117 output = cStringIO.StringIO()
118 output.write("<?xml version='1.0' ?>\n")
121 def getDictionaryWithoutList(dictionary, withoutList):
122 'Get the dictionary without the keys in the list.'
123 dictionaryWithoutList = {}
124 for key in dictionary:
125 if key not in withoutList:
126 dictionaryWithoutList[key] = dictionary[key]
127 return dictionaryWithoutList
129 def getEndGeometryXMLString(output):
130 'Get the string representation of this object info.'
131 addEndXMLTag(0, 'fabmetheus', output)
132 return output.getvalue()