1.1 --- a/libxml2dom/macrolib/macrolib.py Mon Aug 25 22:30:19 2008 +0200
1.2 +++ b/libxml2dom/macrolib/macrolib.py Tue Aug 26 00:17:04 2008 +0200
1.3 @@ -20,6 +20,7 @@
1.4 """
1.5
1.6 import xml.dom
1.7 +from libxml2dom.errors import DOMError
1.8
1.9 # Try the conventional import first.
1.10
1.11 @@ -488,6 +489,9 @@
1.12 "Node type '%s' (%d) not supported." % (_reverseNodeTypes[other.nodeType], other.nodeType)
1.13 )
1.14
1.15 +def Node_getElementById(doc, identifier):
1.16 + return libxml2mod.xmlGetID(doc, identifier)
1.17 +
1.18 def Node_xpath(node, expr, variables=None, namespaces=None):
1.19 expr = from_unicode(expr)
1.20
1.21 @@ -523,16 +527,17 @@
1.22 SERIALIZE_ERR = 82
1.23
1.24 def __repr__(self):
1.25 - return str(self)
1.26 + exctype, excdata = self.args[0:2]
1.27 + return "LSException(%d, %r)" % (exctype, excdata)
1.28
1.29 def __str__(self):
1.30 - exctype = self.args[0]
1.31 + exctype, excdata = self.args[0:2]
1.32 if exctype == self.PARSE_ERR:
1.33 - return "Parse error: LSException(%d)" % exctype
1.34 + return "Parse error: %r" % self
1.35 elif exctype == self.SERIALIZE_ERR:
1.36 - return "Serialize error: LSException(%d)" % exctype
1.37 + return "Serialize error: %r" % self
1.38 else:
1.39 - return Exception.__repr__(self)
1.40 + return repr(self)
1.41
1.42 class XIncludeException(Exception):
1.43
1.44 @@ -560,57 +565,52 @@
1.45 else:
1.46 return parseFile(stream_or_string, html=html, htmlencoding=htmlencoding, unfinished=unfinished)
1.47
1.48 -def parseFile(s, html=0, htmlencoding=None, unfinished=0):
1.49 - # NOTE: Switching off validation and remote DTD resolution.
1.50 +def parseFile(s, html=0, htmlencoding=None, unfinished=0, validate=0, remote=0):
1.51 if not html:
1.52 context = libxml2mod.xmlCreateFileParserCtxt(s)
1.53 - if context is None:
1.54 - raise LSException(LSException.PARSE_ERR)
1.55 - Parser_configure(context)
1.56 - Parser_parse(context)
1.57 - doc = Parser_document(context)
1.58 - if unfinished or Parser_well_formed(context):
1.59 - return doc
1.60 - else:
1.61 - raise LSException(LSException.PARSE_ERR)
1.62 + return _parseXML(context, unfinished, validate, remote)
1.63 else:
1.64 - return libxml2mod.htmlReadFile(s, htmlencoding, HTML_PARSE_NOERROR | HTML_PARSE_NOWARNING | HTML_PARSE_NONET)
1.65 + return libxml2mod.htmlReadFile(s, htmlencoding,
1.66 + HTML_PARSE_NOERROR | HTML_PARSE_NOWARNING | html_net_flag(remote))
1.67
1.68 -def parseString(s, html=0, htmlencoding=None, unfinished=0):
1.69 - # NOTE: Switching off validation and remote DTD resolution.
1.70 +def parseString(s, html=0, htmlencoding=None, unfinished=0, validate=0, remote=0):
1.71 if not html:
1.72 context = libxml2mod.xmlCreateMemoryParserCtxt(s, len(s))
1.73 - if context is None:
1.74 - raise LSException(LSException.PARSE_ERR)
1.75 - Parser_configure(context)
1.76 - Parser_parse(context)
1.77 - doc = Parser_document(context)
1.78 - if unfinished or Parser_well_formed(context):
1.79 - return doc
1.80 - else:
1.81 - raise LSException(LSException.PARSE_ERR)
1.82 + return _parseXML(context, unfinished, validate, remote)
1.83 else:
1.84 # NOTE: URL given as None.
1.85 html_url = None
1.86 return libxml2mod.htmlReadMemory(s, len(s), html_url, htmlencoding,
1.87 - HTML_PARSE_NOERROR | HTML_PARSE_NOWARNING | HTML_PARSE_NONET)
1.88 + HTML_PARSE_NOERROR | HTML_PARSE_NOWARNING | html_net_flag(remote))
1.89
1.90 -def parseURI(uri, html=0, htmlencoding=None, unfinished=0):
1.91 - # NOTE: Switching off validation and remote DTD resolution.
1.92 +def parseURI(uri, html=0, htmlencoding=None, unfinished=0, validate=0, remote=0):
1.93 if not html:
1.94 context = libxml2mod.xmlCreateURLParserCtxt(uri, 0)
1.95 - if context is None:
1.96 - raise LSException(LSException.PARSE_ERR)
1.97 - Parser_configure(context)
1.98 - Parser_parse(context)
1.99 - doc = Parser_document(context)
1.100 - if unfinished or Parser_well_formed(context):
1.101 - return doc
1.102 - else:
1.103 - raise LSException(LSException.PARSE_ERR)
1.104 + return _parseXML(context, unfinished, validate, remote)
1.105 else:
1.106 raise NotImplementedError, "parseURI does not yet support HTML"
1.107
1.108 +def _parseXML(context, unfinished, validate, remote):
1.109 + if context is None:
1.110 + raise LSException(LSException.PARSE_ERR, DOMError(DOMError.SEVERITY_FATAL_ERROR))
1.111 +
1.112 + Parser_configure(context, validate, remote)
1.113 + Parser_parse(context)
1.114 + doc = Parser_document(context)
1.115 +
1.116 + if validate and not Parser_valid(context):
1.117 +
1.118 + # NOTE: May not be the correct exception.
1.119 +
1.120 + raise LSException(LSException.PARSE_ERR,
1.121 + DOMError(DOMError.SEVERITY_FATAL_ERROR, "Document did not validate"))
1.122 +
1.123 + elif unfinished or Parser_well_formed(context):
1.124 + return doc
1.125 + else:
1.126 + raise LSException(LSException.PARSE_ERR,
1.127 + DOMError(DOMError.SEVERITY_FATAL_ERROR, "Document not well-formed"))
1.128 +
1.129 def toString(node, encoding=None, prettyprint=0):
1.130 return libxml2mod.serializeNode(node, encoding, prettyprint)
1.131
1.132 @@ -625,17 +625,37 @@
1.133 HTML_PARSE_NOERROR = 32
1.134 HTML_PARSE_NOWARNING = 64
1.135 HTML_PARSE_NONET = 2048
1.136 +XML_PARSE_DTDVALID = 16
1.137 XML_PARSE_NOERROR = 32
1.138 XML_PARSE_NOWARNING = 64
1.139 XML_PARSE_NONET = 2048
1.140
1.141 +def html_net_flag(remote):
1.142 + if remote:
1.143 + return 0
1.144 + else:
1.145 + return HTML_PARSE_NONET
1.146 +
1.147 +def xml_net_flag(remote):
1.148 + if remote:
1.149 + return 0
1.150 + else:
1.151 + return XML_PARSE_NONET
1.152 +
1.153 +def xml_validate_flag(validate):
1.154 + if validate:
1.155 + return XML_PARSE_DTDVALID
1.156 + else:
1.157 + return 0
1.158 +
1.159 def Parser_push():
1.160 return libxml2mod.xmlCreatePushParser(None, "", 0, None)
1.161
1.162 -def Parser_configure(context):
1.163 +def Parser_configure(context, validate, remote):
1.164 libxml2mod.xmlParserSetPedantic(context, 0)
1.165 - libxml2mod.xmlParserSetValidate(context, 0)
1.166 - libxml2mod.xmlCtxtUseOptions(context, XML_PARSE_NOERROR | XML_PARSE_NOWARNING | XML_PARSE_NONET)
1.167 + #libxml2mod.xmlParserSetValidate(context, validate)
1.168 + libxml2mod.xmlCtxtUseOptions(context,
1.169 + XML_PARSE_NOERROR | XML_PARSE_NOWARNING | xml_net_flag(remote) | xml_validate_flag(validate))
1.170
1.171 def Parser_feed(context, s):
1.172 libxml2mod.xmlParseChunk(context, s, len(s), 1)
1.173 @@ -643,6 +663,9 @@
1.174 def Parser_well_formed(context):
1.175 return libxml2mod.xmlParserGetWellFormed(context)
1.176
1.177 +def Parser_valid(context):
1.178 + return libxml2mod.xmlParserGetIsValid(context)
1.179 +
1.180 def Parser_document(context):
1.181 return libxml2mod.xmlParserGetDoc(context)
1.182