Package jazzparser :: Package utils :: Module domxml
[hide private]
[frames] | no frames]

Source Code for Module jazzparser.utils.domxml

 1  """Handy XML processing utility functions. 
 2   
 3  Various XML processing utilities, using minidom, that are used in  
 4  various places throughout the code. 
 5   
 6  """ 
 7  """ 
 8  ============================== License ======================================== 
 9   Copyright (C) 2008, 2010-12 University of Edinburgh, Mark Granroth-Wilding 
10    
11   This file is part of The Jazz Parser. 
12    
13   The Jazz Parser is free software: you can redistribute it and/or modify 
14   it under the terms of the GNU General Public License as published by 
15   the Free Software Foundation, either version 3 of the License, or 
16   (at your option) any later version. 
17    
18   The Jazz Parser is distributed in the hope that it will be useful, 
19   but WITHOUT ANY WARRANTY; without even the implied warranty of 
20   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
21   GNU General Public License for more details. 
22    
23   You should have received a copy of the GNU General Public License 
24   along with The Jazz Parser.  If not, see <http://www.gnu.org/licenses/>. 
25   
26  ============================ End license ====================================== 
27   
28  """ 
29  __author__ = "Mark Granroth-Wilding <mark.granroth-wilding@ed.ac.uk>"  
30   
31  from xml.dom import minidom 
32   
33 -class XmlReadError(Exception):
34 pass
35
36 -def attrs_to_dict(attrs):
37 """ 38 Converts a minidom NamedNodeMap that represents the attributes 39 of a node into a dictionary. The keys are attribute names. 40 The values are the attributes' string values. 41 """ 42 return dict([(str(attr.name),attr.value) for attr in attrs.values()])
43
44 -def remove_unwanted_elements(node_list):
45 """ 46 Minidom node lists include entries for carriage returns, for 47 some reason. This function removes these from a list. 48 49 """ 50 return [node for node in node_list \ 51 if (node.nodeType != minidom.Node.TEXT_NODE) and \ 52 (node.nodeType != minidom.Node.COMMENT_NODE)]
53
54 -def get_single_element_by_tag_name(node, tag_name, optional=False):
55 """ 56 Returns an element that is a child of the given node and that 57 has the tag name given. This method is used where it is assumed 58 that one such tag exists. 59 If there is none, an exception is 60 raised. If there is more than one, the first is returned. 61 62 @return: the child of node with tag name tag_name 63 64 """ 65 from jazzparser.grammar import GrammarReadError 66 tags = node.getElementsByTagName(tag_name) 67 if len(tags) == 0: 68 if optional: 69 return None 70 else: 71 raise XmlReadError, "No %s tag found" % tag_name 72 73 return tags[0]
74
75 -def require_attrs(node, attrs):
76 """ 77 Checks for the existence of the named attributes on the given 78 node and raises an exception if they're not there. 79 80 Returns a tuple of their values if they're all found. 81 82 """ 83 return tuple([require_attr(node, attr) for attr in attrs])
84
85 -def require_attr(node, attr):
86 """ 87 Checks for the existence of the named attribute on the given 88 node and raises an exception if it's not there. 89 90 Returns its value if it is there. 91 92 """ 93 element = node.attributes.getNamedItem(attr) 94 if element is None: 95 raise XmlReadError, "required attribute '%s' was not found "\ 96 "on %s node: %s" % (attr, node.nodeName, node.toxml()) 97 return element.value
98