Package fabmetheus_utilities :: Module xml_simple_writer
[hide private]
[frames] | no frames]

Source Code for Module fabmetheus_utilities.xml_simple_writer

  1  """ 
  2  XML tag writer utilities. 
  3   
  4  """ 
  5   
  6  import StringIO 
  7   
  8   
  9  __author__ = 'Enrique Perez (perez_enrique@yahoo.com)' 
 10  __credits__ = 'Nophead <http://hydraraptor.blogspot.com/>\nArt of Illusion <http://www.artofillusion.org/>' 
 11  __date__ = '$Date: 2008/21/04 $' 
 12  __license__ = 'GNU Affero General Public License http://www.gnu.org/licenses/agpl.html' 
 13   
 14   
15 -def addBeginEndInnerXMLTag(attributes, depth, innerText, localName, output, text=''):
16 'Add the begin and end xml tag and the inner text if any.' 17 if len( innerText ) > 0: 18 addBeginXMLTag(attributes, depth, localName, output, text) 19 output.write( innerText ) 20 addEndXMLTag(depth, localName, output) 21 else: 22 addClosedXMLTag(attributes, depth, localName, output, text)
23
24 -def addBeginXMLTag(attributes, depth, localName, output, text=''):
25 'Add the begin xml tag.' 26 depthStart = '\t' * depth 27 output.write('%s<%s%s>%s\n' % (depthStart, localName, getAttributesString(attributes), text))
28
29 -def addClosedXMLTag(attributes, depth, localName, output, text=''):
30 'Add the closed xml tag.' 31 depthStart = '\t' * depth 32 attributesString = getAttributesString(attributes) 33 if len(text) > 0: 34 output.write('%s<%s%s >%s</%s>\n' % (depthStart, localName, attributesString, text, localName)) 35 else: 36 output.write('%s<%s%s />\n' % (depthStart, localName, attributesString))
37
38 -def addEndXMLTag(depth, localName, output):
39 'Add the end xml tag.' 40 depthStart = '\t' * depth 41 output.write('%s</%s>\n' % (depthStart, localName))
42
43 -def addXMLFromLoopComplexZ(attributes, depth, loop, output, z):
44 'Add xml from loop.' 45 addBeginXMLTag(attributes, depth, 'path', output) 46 for pointComplexIndex in xrange(len(loop)): 47 pointComplex = loop[pointComplexIndex] 48 addXMLFromXYZ(depth + 1, pointComplexIndex, output, pointComplex.real, pointComplex.imag, z) 49 addEndXMLTag(depth, 'path', output)
50
51 -def addXMLFromObjects(depth, objects, output):
52 'Add xml from objects.' 53 for object in objects: 54 object.addXML(depth, output)
55
56 -def addXMLFromVertexes(depth, output, vertexes):
57 'Add xml from loop.' 58 for vertexIndex in xrange(len(vertexes)): 59 vertex = vertexes[vertexIndex] 60 addXMLFromXYZ(depth + 1, vertexIndex, output, vertex.x, vertex.y, vertex.z)
61
62 -def addXMLFromXYZ(depth, index, output, x, y, z):
63 'Add xml from x, y & z.' 64 attributes = {'index' : str(index)} 65 if x != 0.0: 66 attributes['x'] = str(x) 67 if y != 0.0: 68 attributes['y'] = str(y) 69 if z != 0.0: 70 attributes['z'] = str(z) 71 addClosedXMLTag(attributes, depth, 'vertex', output)
72
73 -def compareAttributeKeyAscending(key, otherKey):
74 'Get comparison in order to sort attribute keys in ascending order, with the id key first and name second.' 75 if key == 'id': 76 return - 1 77 if otherKey == 'id': 78 return 1 79 if key == 'name': 80 return - 1 81 if otherKey == 'name': 82 return 1 83 if key < otherKey: 84 return - 1 85 return int(key > otherKey)
86
87 -def getAttributesString(attributes):
88 'Add the closed xml tag.' 89 attributesString = '' 90 attributesKeys = attributes.keys() 91 attributesKeys.sort(compareAttributeKeyAscending) 92 for attributesKey in attributesKeys: 93 valueString = str(attributes[attributesKey]) 94 if "'" in valueString: 95 attributesString += ' %s="%s"' % (attributesKey, valueString) 96 else: 97 attributesString += " %s='%s'" % (attributesKey, valueString) 98 return attributesString
99
100 -def getBeforeRootOutput(xmlParser):
101 'Get the output before the root and the root xml.' 102 output = StringIO.StringIO() 103 output.write(xmlParser.beforeRoot) 104 xmlParser.getRoot().addXML(0, output) 105 return output.getvalue()
106
107 -def getBeginGeometryXMLOutput(xmlElement=None):
108 'Get the beginning of the string representation of this boolean geometry object info.' 109 output = getBeginXMLOutput() 110 attributes = {} 111 if xmlElement != None: 112 root = xmlElement.getRoot() 113 attributes = root.attributeDictionary 114 addBeginXMLTag(attributes, 0, 'fabmetheus', output) 115 return output
116
117 -def getBeginXMLOutput():
118 'Get the beginning of the string representation of this object info.' 119 output = StringIO.StringIO() 120 output.write("<?xml version='1.0' ?>\n") 121 return output
122
123 -def getDictionaryWithoutList(dictionary, withoutList):
124 'Get the dictionary without the keys in the list.' 125 dictionaryWithoutList = {} 126 for key in dictionary: 127 if key not in withoutList: 128 dictionaryWithoutList[key] = dictionary[key] 129 return dictionaryWithoutList
130
131 -def getEndGeometryXMLString(output):
132 'Get the string representation of this object info.' 133 addEndXMLTag(0, 'fabmetheus', output) 134 return output.getvalue()
135