OLD | NEW |
| (Empty) |
1 #!/usr/bin/env python | |
2 # Copyright (c) 2015 The Chromium Authors. All rights reserved. | |
3 # Use of this source code is governed by a BSD-style license that can be | |
4 # found in the LICENSE file. | |
5 | |
6 | |
7 from grit.format.policy_templates.writers import xml_formatted_writer | |
8 from xml.dom import minidom | |
9 from xml.sax import saxutils as xml_escape | |
10 | |
11 | |
12 def GetWriter(config): | |
13 '''Factory method for creating AndroidPolicyWriter objects. | |
14 See the constructor of TemplateWriter for description of | |
15 arguments. | |
16 ''' | |
17 return AndroidPolicyWriter(['android'], config) | |
18 | |
19 | |
20 def _EscapeResource(resource): | |
21 '''Escape the resource for usage in an Android resource XML file. | |
22 This includes standard XML escaping as well as those specific to Android. | |
23 ''' | |
24 if type(resource) == int: | |
25 return str(resource) | |
26 return xml_escape.escape(resource, {"'": "\\'", '"': '\\"', '\\': '\\\\'}) | |
27 | |
28 | |
29 class AndroidPolicyWriter(xml_formatted_writer.XMLFormattedWriter): | |
30 '''Outputs localized Android Resource XML files. | |
31 The policy strings are localized and exposed as string resources for | |
32 consumption through Android's App restriction Schema. | |
33 ''' | |
34 | |
35 # DOM root node of the generated XML document. | |
36 _doc = None | |
37 # The resources node contains all resource 'string' and 'string-array' | |
38 # elements. | |
39 _resources = None | |
40 | |
41 def AddStringResource(self, name, string): | |
42 '''Add a string resource of the given name. | |
43 ''' | |
44 string_node = self._doc.createElement('string') | |
45 string_node.setAttribute('name', name) | |
46 string_node.appendChild(self._doc.createTextNode(_EscapeResource(string))) | |
47 self._resources.appendChild(string_node) | |
48 | |
49 def AddStringArrayResource(self, name, string_items): | |
50 '''Add a string-array resource of the given name and | |
51 elements from string_items. | |
52 ''' | |
53 string_array_node = self._doc.createElement('string-array') | |
54 string_array_node.setAttribute('name', name) | |
55 self._resources.appendChild(string_array_node) | |
56 for item in string_items: | |
57 string_node = self._doc.createElement('item') | |
58 string_node.appendChild(self._doc.createTextNode(_EscapeResource(item))) | |
59 string_array_node.appendChild(string_node) | |
60 | |
61 def PreprocessPolicies(self, policy_list): | |
62 return self.FlattenGroupsAndSortPolicies(policy_list) | |
63 | |
64 def CanBeRecommended(self, policy): | |
65 return False | |
66 | |
67 def IsDeprecatedPolicySupported(self, policy): | |
68 return True | |
69 | |
70 def IsFuturePolicySupported(self, policy): | |
71 return True | |
72 | |
73 def WritePolicy(self, policy): | |
74 name = policy['name'] | |
75 self.AddStringResource(name + 'Title', policy['caption']) | |
76 | |
77 # Get the first line of the policy description. | |
78 description = policy['desc'].split('\n', 1)[0] | |
79 self.AddStringResource(name + 'Desc', description) | |
80 | |
81 items = policy.get('items') | |
82 if items is not None: | |
83 entries = [ item['caption'] for item in items ] | |
84 values = [ item['value'] for item in items ] | |
85 self.AddStringArrayResource(name + 'Entries', entries) | |
86 self.AddStringArrayResource(name + 'Values', values) | |
87 | |
88 def BeginTemplate(self): | |
89 comment_text = 'DO NOT MODIFY THIS FILE DIRECTLY!\n' \ | |
90 'IT IS GENERATED FROM policy_templates.json.' | |
91 comment_node = self._doc.createComment(comment_text) | |
92 self._doc.insertBefore(comment_node, self._resources) | |
93 | |
94 def Init(self): | |
95 impl = minidom.getDOMImplementation() | |
96 self._doc = impl.createDocument(None, 'resources', None) | |
97 self._resources = self._doc.documentElement | |
98 | |
99 def GetTemplateText(self): | |
100 return self.ToPrettyXml(self._doc) | |
OLD | NEW |