1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
|
import sys, re, vsdotnetxmlparser
class EZXMLError:
pass
class EZXMLElement:
"""
Members:
Name : string
Attrs : list with attributes [(name,value), (name, value), ...]
Children : list of XMLElements
Parent : parent XMLElement
"""
def GetAttributeValue( self, attrName ):
for a in self.Attrs:
if a[0] == attrName:
return a[1]
return None
def RemoveAttribute( self, attrName ):
for i,a in enumerate( self.Attrs ):
if a[0] == attrName:
self.Attrs.pop( i )
return 1
return 0
def RemoveFromParent( self ):
if not self.Parent:
raise EZXMLError
self.Parent.Children.remove( self )
class EZXMLFile:
#
# PUBLIC INTERFACE
#
def __init__( self, data ):
self.Version = ''
self.Encoding = ''
self.RootElement = None
self.__CurElement = None
self.__ElementIndexRE = re.compile( r'<(?P<index>.+)>(?P<name>.+)' )
#p = xml.parsers.expat.ParserCreate()
p = vsdotnetxmlparser.VSDotNetXMLParser()
p.ordered_attributes = 1
p.XmlDeclHandler = self.__xml_decl_handler
p.StartElementHandler = self.__start_element
p.EndElementHandler = self.__end_element
p.Parse( data, 1 )
# Write the current contents to a file.
def WriteFile( self, file ):
file.write( '<?xml version="%s" encoding="%s"?>\r\n' % (self.Version, self.Encoding) )
if self.RootElement:
self.__WriteFile_R( file, self.RootElement, 0 )
"""
Find an element (starting at the root).
If elementName has backslashes in it (like 'VisualStudioProject\Configurations\Tool'), then it will recurse into each element.
Each element can also have an optional count telling which element to find:
'VisualStudioProject\Configurations\<3>Tool'
"""
def GetElement( self, elementName ):
if not self.RootElement:
return None
pathList = elementName.split( '\\' )
return self.__GetElementByPath_R( [self.RootElement], pathList, 0 )
"""
AttributePath is specified the same way as GetElement.
Returns None if the path to the element or attribute doesn't exist.
"""
def GetAttribute( self, attributePath ):
if not self.RootElement:
return None
pathList = attributePath.split( '\\' )
attributeName = pathList.pop()
e = self.__GetElementByPath_R( [self.RootElement], pathList, 0 )
if e:
return e.GetAttributeValue( attributeName )
else:
return None
#
# INTERNAL STUFF
#
def __GetElementByPath_R( self, curElementList, pathList, index ):
indexToFind = 1
indexFound = 0
m = self.__ElementIndexRE.match( pathList[index] )
if m:
nameToFind = m.group('name').upper()
indexToFind = int( m.group('index') )
else:
nameToFind = pathList[index].upper()
for x in curElementList:
if x.Name.upper() == nameToFind:
indexFound += 1
if indexFound == indexToFind:
if index == len( pathList ) - 1:
return x
else:
return self.__GetElementByPath_R( x.Children, pathList, index+1 )
return None
def __WriteFile_R( self, file, curElement, indentLevel ):
tabChars = ""
for i in xrange(0,indentLevel):
tabChars = tabChars + '\t'
file.write( tabChars )
keyListLen = len( curElement.Attrs )
if keyListLen == 0:
file.write( '<%s>\r\n' % (curElement.Name) )
else:
file.write( '<%s\r\n' % (curElement.Name) )
bClosedBlock = 0
for i,e in enumerate( curElement.Attrs ):
file.write( tabChars + '\t' )
file.write( '%s="%s"' % (e[0], e[1]) )
# VS.NET XML files use this special syntax on elements that have attributes and no children.
if len( curElement.Children ) == 0 and i == keyListLen-1 and curElement.Name != 'File':
file.write( '/>\r\n' )
bClosedBlock = 1
elif i == keyListLen-1:
file.write( '>\r\n' )
else:
file.write( '\r\n' )
for child in curElement.Children:
self.__WriteFile_R( file, child, indentLevel+1 )
if not bClosedBlock:
file.write( tabChars )
file.write( '</%s>\r\n' % (curElement.Name) )
# XML parser handler functions
def __xml_decl_handler( self, version, encoding, standalone ):
self.Version = version
self.Encoding = encoding
def __start_element( self, name, attrs ):
e = EZXMLElement()
e.Name = name
e.Attrs = zip( attrs[::2], attrs[1::2] ) # Cool slicing trick.. this takes a list like [1,2,3,4] and makes it into [[1,2], [3,4]]
e.Children = None
e.Parent = self.__CurElement
e.Children = []
if self.__CurElement:
self.__CurElement.Children.append( e )
else:
self.RootElement = e
self.__CurElement = e
def __end_element( self, name ):
self.__CurElement = self.__CurElement.Parent
|