OLD | NEW |
(Empty) | |
| 1 # Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 2 # Use of this source code is governed by a BSD-style license that can be |
| 3 # found in the LICENSE file. |
| 4 |
| 5 from xml.dom import minidom |
| 6 from grit.format.policy_templates.writers import xml_formatted_writer |
| 7 |
| 8 |
| 9 def GetWriter(config): |
| 10 '''Factory method for instanciating the ADMLWriter. Every Writer needs a |
| 11 GetWriter method because the TemplateFormatter uses this method to |
| 12 instantiate a Writer. |
| 13 ''' |
| 14 return ADMLWriter(['win'], config) |
| 15 |
| 16 |
| 17 class ADMLWriter(xml_formatted_writer.XMLFormattedWriter): |
| 18 ''' Class for generating an ADML policy template. It is used by the |
| 19 PolicyTemplateGenerator to write the ADML file. |
| 20 ''' |
| 21 |
| 22 # DOM root node of the generated ADML document. |
| 23 _doc = None |
| 24 |
| 25 # The string-table contains all ADML "string" elements. |
| 26 _string_table_elem = None |
| 27 |
| 28 # The presentation-table is the container for presentation elements, that |
| 29 # describe the presentation of Policy-Groups and Policies. |
| 30 _presentation_table_elem = None |
| 31 |
| 32 def _AddString(self, parent, id, text): |
| 33 ''' Adds an ADML "string" element to the passed parent. The following |
| 34 ADML snippet contains an example: |
| 35 |
| 36 <string id="$(id)">$(text)</string> |
| 37 |
| 38 Args: |
| 39 parent: Parent element to which the new "string" element is added. |
| 40 id: ID of the newly created "string" element. |
| 41 text: Value of the newly created "string" element. |
| 42 ''' |
| 43 string_elem = self.AddElement(parent, 'string', {'id': id}) |
| 44 string_elem.appendChild(self._doc.createTextNode(text)) |
| 45 |
| 46 def WritePolicy(self, policy): |
| 47 '''Generates the ADML elements for a Policy. |
| 48 <stringTable> |
| 49 ... |
| 50 <string id="$(policy_group_name)">$(caption)</string> |
| 51 <string id="$(policy_group_name)_Explain">$(description)</string> |
| 52 </stringTable> |
| 53 |
| 54 <presentationTables> |
| 55 ... |
| 56 <presentation id=$(policy_group_name)/> |
| 57 </presentationTables> |
| 58 |
| 59 Args: |
| 60 policy: The Policy to generate ADML elements for. |
| 61 ''' |
| 62 policy_type = policy['type'] |
| 63 policy_name = policy['name'] |
| 64 if 'caption' in policy: |
| 65 policy_caption = policy['caption'] |
| 66 else: |
| 67 policy_caption = policy_name |
| 68 if 'desc' in policy: |
| 69 policy_description = policy['desc'] |
| 70 else: |
| 71 policy_description = policy_name |
| 72 if 'label' in policy: |
| 73 policy_label = policy['label'] |
| 74 else: |
| 75 policy_label = policy_name |
| 76 |
| 77 self._AddString(self._string_table_elem, policy_name, policy_caption) |
| 78 self._AddString(self._string_table_elem, policy_name + '_Explain', |
| 79 policy_description) |
| 80 presentation_elem = self.AddElement( |
| 81 self._presentation_table_elem, 'presentation', {'id': policy_name}) |
| 82 |
| 83 if policy_type == 'main': |
| 84 pass |
| 85 elif policy_type == 'string': |
| 86 textbox_elem = self.AddElement(presentation_elem, 'textBox', |
| 87 {'refId': policy_name}) |
| 88 label_elem = self.AddElement(textbox_elem, 'label') |
| 89 label_elem.appendChild(self._doc.createTextNode(policy_label)) |
| 90 elif policy_type == 'int': |
| 91 textbox_elem = self.AddElement(presentation_elem, 'decimalTextBox', |
| 92 {'refId': policy_name}) |
| 93 textbox_elem.appendChild(self._doc.createTextNode(policy_label + ':')) |
| 94 elif policy_type in ('int-enum', 'string-enum'): |
| 95 for item in policy['items']: |
| 96 self._AddString(self._string_table_elem, item['name'], item['caption']) |
| 97 dropdownlist_elem = self.AddElement(presentation_elem, 'dropdownList', |
| 98 {'refId': policy_name}) |
| 99 dropdownlist_elem.appendChild(self._doc.createTextNode(policy_label)) |
| 100 elif policy_type == 'list': |
| 101 self._AddString(self._string_table_elem, |
| 102 policy_name + 'Desc', |
| 103 policy_caption) |
| 104 listbox_elem = self.AddElement(presentation_elem, 'listBox', |
| 105 {'refId': policy_name + 'Desc'}) |
| 106 listbox_elem.appendChild(self._doc.createTextNode(policy_label)) |
| 107 elif policy_type == 'group': |
| 108 pass |
| 109 else: |
| 110 raise Exception('Unknown policy type %s.' % policy_type) |
| 111 |
| 112 def BeginPolicyGroup(self, group): |
| 113 '''Generates ADML elements for a Policy-Group. For each Policy-Group two |
| 114 ADML "string" elements are added to the string-table. One contains the |
| 115 caption of the Policy-Group and the other a description. A Policy-Group also |
| 116 requires an ADML "presentation" element that must be added to the |
| 117 presentation-table. The "presentation" element is the container for the |
| 118 elements that define the visual presentation of the Policy-Goup's Policies. |
| 119 The following ADML snippet shows an example: |
| 120 |
| 121 Args: |
| 122 group: The Policy-Group to generate ADML elements for. |
| 123 ''' |
| 124 # Add ADML "string" elements to the string-table that are required by a |
| 125 # Policy-Group. |
| 126 self._AddString(self._string_table_elem, group['name'] + '_group', |
| 127 group['caption']) |
| 128 |
| 129 def _AddBaseStrings(self, string_table_elem, build): |
| 130 ''' Adds ADML "string" elements to the string-table that are referenced by |
| 131 the ADMX file but not related to any specific Policy-Group or Policy. |
| 132 ''' |
| 133 self._AddString(string_table_elem, self.config['win_supported_os'], |
| 134 self.messages['win_supported_winxpsp2']['text']) |
| 135 if build == 'chrome': |
| 136 self._AddString(string_table_elem, self.config['win_category_path'][0], |
| 137 'Google') |
| 138 self._AddString(string_table_elem, self.config['win_category_path'][1], |
| 139 self.config['app_name']) |
| 140 elif build == 'chromium': |
| 141 self._AddString(string_table_elem, self.config['win_category_path'][0], |
| 142 self.config['app_name']) |
| 143 |
| 144 def BeginTemplate(self): |
| 145 dom_impl = minidom.getDOMImplementation('') |
| 146 self._doc = dom_impl.createDocument(None, 'policyDefinitionResources', |
| 147 None) |
| 148 policy_definitions_resources_elem = self._doc.documentElement |
| 149 policy_definitions_resources_elem.attributes['revision'] = '1.0' |
| 150 policy_definitions_resources_elem.attributes['schemaVersion'] = '1.0' |
| 151 |
| 152 self.AddElement(policy_definitions_resources_elem, 'displayName') |
| 153 self.AddElement(policy_definitions_resources_elem, 'description') |
| 154 resources_elem = self.AddElement(policy_definitions_resources_elem, |
| 155 'resources') |
| 156 self._string_table_elem = self.AddElement(resources_elem, 'stringTable') |
| 157 self._AddBaseStrings(self._string_table_elem, self.config['build']) |
| 158 self._presentation_table_elem = self.AddElement(resources_elem, |
| 159 'presentationTable') |
| 160 |
| 161 def GetTemplateText(self): |
| 162 # Using "toprettyxml()" confuses the Windows Group Policy Editor |
| 163 # (gpedit.msc) because it interprets whitespace characters in text between |
| 164 # the "string" tags. This prevents gpedit.msc from displaying the category |
| 165 # names correctly. |
| 166 # TODO(markusheintz): Find a better formatting that works with gpedit. |
| 167 return self._doc.toxml() |
OLD | NEW |