summaryrefslogtreecommitdiff
path: root/BCT/PhoneControlsExtractor/PhoneControlsExtractor.py
blob: f4b3d6f4d30536453373efbb6e559adf95e54ea9 (plain)
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
import sys
import getopt
import os
from xml.dom import minidom
import xml.dom

CONTROL_NAMES= ["Button", "CheckBox", "RadioButton"]

# TODO maybe a control is enabled but its parent is not, must take this into account
# TODO a possible solution is to tie the enabled value to that of the parent in the app until it is either overriden
# TODO (by directly manipulating the control's enabled value) or the parent becomes enabled

CONTAINER_CONTROL_NAMES= ["Canvas", "Grid", "StackPanel"]

staticControlsMap= {}
mainPageXAML= None

def showUsage():
  print "PhoneControlsExtractor -- extract control information from phone application pages"
  print "Usage:"
  print "\tPhoneControlsExtractor --pages <application_directory> --output <info_output_file>\n"
  print "Options:"
  print "\t--pages <application_directory>: point to location of .xaml files detailing pages. Short form: -p"
  print "\t--output <info_output_file>: file to write with control info. Short form: -o\n"

def isPageFile(file):
  # not the best way, find out the actual exceptions
  try:
    minidom.parse(file)
    file.seek(0)
    return True
  except Exception:
    return False

def removeBlankElements(xmlNode):
  blankNodes= []
  for child in xmlNode.childNodes:
    if child.nodeType == xml.dom.Node.TEXT_NODE and child.data.strip() == "":
      blankNodes.append(child)
    elif child.hasChildNodes():
      removeBlankElements(child)
	
  for node in blankNodes:
    node.parentNode.removeChild(node)
    node.unlink()

def getControlNodes(xmlNode):
  controlNodes= []
  if (xmlNode.nodeType == xml.dom.Node.ELEMENT_NODE and xmlNode.localName in CONTROL_NAMES):
    controlNodes.insert(0,xmlNode)

  for child in xmlNode.childNodes:
    controlNodes= controlNodes + getControlNodes(child)

  return controlNodes

def addControlToMap(pageXAML, parentPage, controlNode):
  pageControls=[]
  newControl={}
  try:
    pageControls= staticControlsMap[parentPage]
  except KeyError:
    pass
  
  newControl["Type"]= controlNode.localName
  newControl["Name"]= controlNode.getAttribute("Name")
  if (controlNode.hasAttribute("IsEnabled")):
    newControl["IsEnabled"]= controlNode.getAttribute("IsEnabled")
  else:
    newControl["IsEnabled"]= "true"
  
  if (controlNode.hasAttribute("Visibility")):
    newControl["Visibility"]= controlNode.getAttribute("Visibility")
  else:
    newControl["Visibility"]= "Visible"

  # TODO it is possible that more events are of interest, we should add as we discover them in existing applications
  newControl["Click"] = controlNode.getAttribute("Click")
  newControl["Checked"] = controlNode.getAttribute("Checked")
  newControl["Unchecked"] = controlNode.getAttribute("Unchecked")
  newControl["XAML"]= pageXAML
  pageControls.append(newControl)
  staticControlsMap[parentPage]= pageControls

def extractPhoneControlsFromPage(pageXAML):
  # maybe it is not a page file
  pageFile= open(pageXAML, "r")
  if not isPageFile(pageFile):
    return
  pageFileXML= minidom.parse(pageFile)
  pageFile.close()
  removeBlankElements(pageFileXML)
  controls= getControlNodes(pageFileXML)
  ownerPage = None
  for control in controls:
    parent= control
    while not parent == None and ownerPage == None:
      if parent.localName == "PhoneApplicationPage":
        ownerPage= parent.getAttribute("x:Class")
      parent= parent.parentNode
    addControlToMap(pageXAML, ownerPage, control)

def outputPhoneControls(outputFileName):
  outputFile= open(outputFileName, "w")

  # Output format is first line containing only the main page, and then one line per
  # <pageClassName>,<page.xaml file>,<controlClassName>,<controlName (as in field name)>,<IsEnabledValue>,<VisibilityValue>,<ClickValue>,<CheckedValue>,<UncheckedValue>
  outputFile.write(mainPageXAML + "\n")
  for page in staticControlsMap.keys():
    for control in staticControlsMap[page]:
      isEnabled= control["IsEnabled"]
      visibility= control["Visibility"]
      click= control["Click"]
      checked= control["Checked"]
      unchecked= control["Unchecked"]
      pageXAML= control["XAML"]
      # last comma is to account for bpl translation name, that is unknown for now
      outputFile.write(page + "," + os.path.basename(pageXAML) + "," + control["Type"] + "," + control["Name"] + "," + isEnabled + "," + visibility + "," + click + "," + checked + "," + unchecked + ",\n")

  outputFile.close()

def getMainPageXAMLFromManifest(filename):
  file= open(filename, "r");
  manifest= minidom.parse(file)
  file.close()
  # interesting XPath location /App/Tasks/DefaultTask/@NavigationPage
  return manifest.getElementsByTagName("DefaultTask")[0].getAttribute("NavigationPage")

def extractPhoneControls(sourceDir):
  global mainPageXAML
  fileList= [os.path.join(sourceDir, fileName) for fileName in os.listdir(sourceDir) if os.path.splitext(fileName)[1] == ".xaml" or os.path.splitext(fileName)[1] == ".xml"]
  for fileName in fileList:
    if os.path.splitext(fileName)[1] == ".xml" and os.path.splitext(os.path.split(fileName)[1])[0].lower() == "wmappmanifest":
      mainPageXAML= getMainPageXAMLFromManifest(fileName)
      break

  for fileName in fileList:
    extractPhoneControlsFromPage(fileName)

def main():
  pagesDir= ""
  outputFile= ""
  try:
    opts, args= getopt.getopt(sys.argv[1:], "p:o:", ["pages=","output="])
  except geopt.error, msg:
    print msg
    showUsage()
    sys.exit(2)

  if not len(opts) == 2:
    print "Missing options"
    showUsage()
    sys.exit(2)

  for o, a in opts:
    if o in ["-p","--pages"]:
      pagesDir= a
    if o in ["-o", "--output"]:
      outputFile= a

  extractPhoneControls(pagesDir)
  outputPhoneControls(outputFile)

if __name__ == "__main__":
  main()