OLD | NEW |
1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
2 # Copyright (c) 2012 The Chromium Authors. All rights reserved. | 2 # Copyright (c) 2012 The Chromium Authors. All rights reserved. |
3 # Use of this source code is governed by a BSD-style license that can be | 3 # Use of this source code is governed by a BSD-style license that can be |
4 # found in the LICENSE file. | 4 # found in the LICENSE file. |
5 | 5 |
6 | 6 |
7 from grit.format.policy_templates.writers import template_writer | 7 from grit.format.policy_templates.writers import template_writer |
8 | 8 |
9 | 9 |
10 class XMLFormattedWriter(template_writer.TemplateWriter): | 10 class XMLFormattedWriter(template_writer.TemplateWriter): |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
46 | 46 |
47 def AddAttribute(self, parent, name, value): | 47 def AddAttribute(self, parent, name, value): |
48 '''Adds a new attribute to the parent Element. If an attribute with the | 48 '''Adds a new attribute to the parent Element. If an attribute with the |
49 given name already exists then it will be replaced. | 49 given name already exists then it will be replaced. |
50 ''' | 50 ''' |
51 doc = parent.ownerDocument | 51 doc = parent.ownerDocument |
52 attribute = doc.createAttribute(name) | 52 attribute = doc.createAttribute(name) |
53 attribute.value = value | 53 attribute.value = value |
54 parent.setAttributeNode(attribute) | 54 parent.setAttributeNode(attribute) |
55 | 55 |
56 def ToPrettyXml(self, doc): | 56 def AddComment(self, parent, comment): |
| 57 '''Adds a comment node.''' |
| 58 parent.appendChild(parent.ownerDocument.createComment(comment)) |
| 59 |
| 60 def ToPrettyXml(self, doc, **kwargs): |
57 # return doc.toprettyxml(indent=' ') | 61 # return doc.toprettyxml(indent=' ') |
58 # The above pretty-printer does not print the doctype and adds spaces | 62 # The above pretty-printer does not print the doctype and adds spaces |
59 # around texts, e.g.: | 63 # around texts, e.g.: |
60 # <string> | 64 # <string> |
61 # value of the string | 65 # value of the string |
62 # </string> | 66 # </string> |
63 # This is problematic both for the OSX Workgroup Manager (plist files) and | 67 # This is problematic both for the OSX Workgroup Manager (plist files) and |
64 # the Windows Group Policy Editor (admx files). What they need instead: | 68 # the Windows Group Policy Editor (admx files). What they need instead: |
65 # <string>value of string</string> | 69 # <string>value of string</string> |
66 # So we use the poor man's pretty printer here. It assumes that there are | 70 # So we use the poor man's pretty printer here. It assumes that there are |
67 # no mixed-content nodes. | 71 # no mixed-content nodes. |
68 # Get all the XML content in a one-line string. | 72 # Get all the XML content in a one-line string. |
69 xml = doc.toxml() | 73 xml = doc.toxml(**kwargs) |
70 # Determine where the line breaks will be. (They will only be between tags.) | 74 # Determine where the line breaks will be. (They will only be between tags.) |
71 lines = xml[1:len(xml) - 1].split('><') | 75 lines = xml[1:len(xml) - 1].split('><') |
72 indent = '' | 76 indent = '' |
73 res = '' | 77 res = '' |
74 # Determine indent for each line. | 78 # Determine indent for each line. |
75 for i, line in enumerate(lines): | 79 for i, line in enumerate(lines): |
76 if line[0] == '/': | 80 if line[0] == '/': |
77 # If the current line starts with a closing tag, decrease indent before | 81 # If the current line starts with a closing tag, decrease indent before |
78 # printing. | 82 # printing. |
79 indent = indent[2:] | 83 indent = indent[2:] |
80 lines[i] = indent + '<' + line + '>' | 84 lines[i] = indent + '<' + line + '>' |
81 if (line[0] not in ['/', '?', '!'] and '</' not in line and | 85 if (line[0] not in ['/', '?', '!'] and '</' not in line and |
82 line[len(line) - 1] != '/'): | 86 line[len(line) - 1] != '/'): |
83 # If the current line starts with an opening tag and does not conatin a | 87 # If the current line starts with an opening tag and does not conatin a |
84 # closing tag, increase indent after the line is printed. | 88 # closing tag, increase indent after the line is printed. |
85 indent += ' ' | 89 indent += ' ' |
86 # Reconstruct XML text from the lines. | 90 # Reconstruct XML text from the lines. |
87 return '\n'.join(lines) | 91 return '\n'.join(lines) |
OLD | NEW |