2 XML tag writer utilities.
7 from __future__ import absolute_import
11 if sys.version_info[0] < 3:
14 import io as cStringIO
17 __author__ = 'Enrique Perez (perez_enrique@yahoo.com)'
18 __credits__ = 'Nophead <http://hydraraptor.blogspot.com/>\nArt of Illusion <http://www.artofillusion.org/>'
19 __date__ = '$Date: 2008/21/04 $'
20 __license__ = 'GNU Affero General Public License http://www.gnu.org/licenses/agpl.html'
23 def addBeginEndInnerXMLTag(attributes, depth, innerText, localName, output, text=''):
24 'Add the begin and end xml tag and the inner text if any.'
25 if len( innerText ) > 0:
26 addBeginXMLTag(attributes, depth, localName, output, text)
27 output.write( innerText )
28 addEndXMLTag(depth, localName, output)
30 addClosedXMLTag(attributes, depth, localName, output, text)
32 def addBeginXMLTag(attributes, depth, localName, output, text=''):
33 'Add the begin xml tag.'
34 depthStart = '\t' * depth
35 output.write('%s<%s%s>%s\n' % (depthStart, localName, getAttributesString(attributes), text))
37 def addClosedXMLTag(attributes, depth, localName, output, text=''):
38 'Add the closed xml tag.'
39 depthStart = '\t' * depth
40 attributesString = getAttributesString(attributes)
42 output.write('%s<%s%s >%s</%s>\n' % (depthStart, localName, attributesString, text, localName))
44 output.write('%s<%s%s />\n' % (depthStart, localName, attributesString))
46 def addEndXMLTag(depth, localName, output):
47 'Add the end xml tag.'
48 depthStart = '\t' * depth
49 output.write('%s</%s>\n' % (depthStart, localName))
51 def addXMLFromLoopComplexZ(attributes, depth, loop, output, z):
53 addBeginXMLTag(attributes, depth, 'path', output)
54 for pointComplexIndex in xrange(len(loop)):
55 pointComplex = loop[pointComplexIndex]
56 addXMLFromXYZ(depth + 1, pointComplexIndex, output, pointComplex.real, pointComplex.imag, z)
57 addEndXMLTag(depth, 'path', output)
59 def addXMLFromObjects(depth, objects, output):
60 'Add xml from objects.'
61 for object in objects:
62 object.addXML(depth, output)
64 def addXMLFromVertexes(depth, output, vertexes):
66 for vertexIndex in xrange(len(vertexes)):
67 vertex = vertexes[vertexIndex]
68 addXMLFromXYZ(depth + 1, vertexIndex, output, vertex.x, vertex.y, vertex.z)
70 def addXMLFromXYZ(depth, index, output, x, y, z):
71 'Add xml from x, y & z.'
72 attributes = {'index' : str(index)}
74 attributes['x'] = str(x)
76 attributes['y'] = str(y)
78 attributes['z'] = str(z)
79 addClosedXMLTag(attributes, depth, 'vertex', output)
81 def compareAttributeKeyAscending(key, otherKey):
82 'Get comparison in order to sort attribute keys in ascending order, with the id key first and name second.'
89 if otherKey == 'name':
93 return int(key > otherKey)
95 def getAttributesString(attributes):
96 'Add the closed xml tag.'
98 attributesKeys = attributes.keys()
99 attributesKeys.sort(compareAttributeKeyAscending)
100 for attributesKey in attributesKeys:
101 valueString = str(attributes[attributesKey])
102 if "'" in valueString:
103 attributesString += ' %s="%s"' % (attributesKey, valueString)
105 attributesString += " %s='%s'" % (attributesKey, valueString)
106 return attributesString
108 def getBeginGeometryXMLOutput(elementNode=None):
109 'Get the beginning of the string representation of this boolean geometry object info.'
110 output = getBeginXMLOutput()
112 if elementNode != None:
113 documentElement = elementNode.getDocumentElement()
114 attributes = documentElement.attributes
115 addBeginXMLTag(attributes, 0, 'fabmetheus', output)
118 def getBeginXMLOutput():
119 'Get the beginning of the string representation of this object info.'
120 output = cStringIO.StringIO()
121 output.write("<?xml version='1.0' ?>\n")
124 def getDictionaryWithoutList(dictionary, withoutList):
125 'Get the dictionary without the keys in the list.'
126 dictionaryWithoutList = {}
127 for key in dictionary:
128 if key not in withoutList:
129 dictionaryWithoutList[key] = dictionary[key]
130 return dictionaryWithoutList
132 def getEndGeometryXMLString(output):
133 'Get the string representation of this object info.'
134 addEndXMLTag(0, 'fabmetheus', output)
135 return output.getvalue()