[XML-SIG] prepare_input_source and relative path

Sylvain Thénault Sylvain.Thenault at logilab.fr
Fri Apr 8 11:26:50 CEST 2005


the updated patch...
-- 
Sylvain Thénault                               LOGILAB, Paris (France).

http://www.logilab.com   http://www.logilab.fr  http://www.logilab.org

-------------- next part --------------
--- /usr/lib/python2.3/site-packages/_xmlplus/sax/saxutils.py	2004-11-29 13:36:36.000000000 +0100
+++ cvs_work/_xmlplus/sax/saxutils.py	2005-04-08 11:11:36.000000000 +0200
@@ -10,6 +10,8 @@
 import xmlreader
 import sys, _exceptions, saxlib
 
+from xml.Uri import Absolutize, MakeUrllibSafe,IsAbsolute
+
 try:
     _StringTypes = [types.StringType, types.UnicodeType]
 except AttributeError: # 1.5 compatibility:UnicodeType not defined
@@ -510,22 +512,28 @@
         source = xmlreader.InputSource()
         source.setByteStream(f)
         if hasattr(f, "name"):
-            source.setSystemId(f.name)
+            source.setSystemId(absolute_system_id(f.name, base))
 
     if source.getByteStream() is None:
-        sysid = source.getSystemId()
-        if os.path.isfile(sysid):
-            basehead = os.path.split(os.path.normpath(base))[0]
-            source.setSystemId(os.path.join(basehead, sysid))
-            f = open(sysid, "rb")
-        else:
-            source.setSystemId(urlparse.urljoin(base, sysid))
-            f = urllib2.urlopen(source.getSystemId())
-
+        sysid = absolute_system_id(source.getSystemId(), base)
+        source.setSystemId(sysid)
+        f = urllib2.urlopen(sysid)
         source.setByteStream(f)
 
     return source
 
+
+def absolute_system_id(sysid, base=''):
+    # if a base is given, sysid may be relative to it, make the
+    # join before isfile() test
+    if os.path.exists(sysid):
+        sysid = 'file:%s' % os.path.abspath(sysid)
+    elif base:        
+        assert IsAbsolute(base), base
+        sysid = Absolutize(sysid, base)
+    assert IsAbsolute(sysid)
+    return MakeUrllibSafe(sysid)
+    
 # ===========================================================================
 #
 # DEPRECATED SAX 1.0 CLASSES


More information about the XML-SIG mailing list