Back to index

python3.2  3.2.2
Classes | Functions | Variables
xml.sax.saxutils Namespace Reference

Classes

class  XMLGenerator
class  XMLFilterBase

Functions

def __dict_replace
def escape
def unescape
def quoteattr
def prepare_input_source

Variables

string _error_handling = "xmlcharrefreplace"

Detailed Description

\
A library of useful helper classes to the SAX classes, for the
convenience of application and driver writers.

Function Documentation

def xml.sax.saxutils.__dict_replace (   s,
  d 
) [private]
Replace substrings of a string using a dictionary.

Definition at line 19 of file saxutils.py.

00019 
00020 def __dict_replace(s, d):
00021     """Replace substrings of a string using a dictionary."""
00022     for key, value in d.items():
00023         s = s.replace(key, value)
00024     return s

Here is the caller graph for this function:

def xml.sax.saxutils.escape (   data,
  entities = {} 
)
Escape &, <, and > in a string of data.

You can escape other strings of data by passing a dictionary as
the optional entities parameter.  The keys and values must all be
strings; each key will be replaced with its corresponding value.

Definition at line 25 of file saxutils.py.

00025 
00026 def escape(data, entities={}):
00027     """Escape &, <, and > in a string of data.
00028 
00029     You can escape other strings of data by passing a dictionary as
00030     the optional entities parameter.  The keys and values must all be
00031     strings; each key will be replaced with its corresponding value.
00032     """
00033 
00034     # must do ampersand first
00035     data = data.replace("&", "&amp;")
00036     data = data.replace(">", "&gt;")
00037     data = data.replace("<", "&lt;")
00038     if entities:
00039         data = __dict_replace(data, entities)
00040     return data

Here is the call graph for this function:

Here is the caller graph for this function:

def xml.sax.saxutils.prepare_input_source (   source,
  base = "" 
)
This function takes an InputSource and an optional base URL and
returns a fully resolved InputSource object ready for reading.

Definition at line 305 of file saxutils.py.

00305 
00306 def prepare_input_source(source, base=""):
00307     """This function takes an InputSource and an optional base URL and
00308     returns a fully resolved InputSource object ready for reading."""
00309 
00310     if isinstance(source, str):
00311         source = xmlreader.InputSource(source)
00312     elif hasattr(source, "read"):
00313         f = source
00314         source = xmlreader.InputSource()
00315         source.setByteStream(f)
00316         if hasattr(f, "name"):
00317             source.setSystemId(f.name)
00318 
00319     if source.getByteStream() is None:
00320         sysid = source.getSystemId()
00321         basehead = os.path.dirname(os.path.normpath(base))
00322         sysidfilename = os.path.join(basehead, sysid)
00323         if os.path.isfile(sysidfilename):
00324             source.setSystemId(sysidfilename)
00325             f = open(sysidfilename, "rb")
00326         else:
00327             source.setSystemId(urllib.parse.urljoin(base, sysid))
00328             f = urllib.request.urlopen(source.getSystemId())
00329 
00330         source.setByteStream(f)
00331 
00332     return source

Here is the call graph for this function:

def xml.sax.saxutils.quoteattr (   data,
  entities = {} 
)
Escape and quote an attribute value.

Escape &, <, and > in a string of data, then quote it for use as
an attribute value.  The \" character will be escaped as well, if
necessary.

You can escape other strings of data by passing a dictionary as
the optional entities parameter.  The keys and values must all be
strings; each key will be replaced with its corresponding value.

Definition at line 55 of file saxutils.py.

00055 
00056 def quoteattr(data, entities={}):
00057     """Escape and quote an attribute value.
00058 
00059     Escape &, <, and > in a string of data, then quote it for use as
00060     an attribute value.  The \" character will be escaped as well, if
00061     necessary.
00062 
00063     You can escape other strings of data by passing a dictionary as
00064     the optional entities parameter.  The keys and values must all be
00065     strings; each key will be replaced with its corresponding value.
00066     """
00067     entities = entities.copy()
00068     entities.update({'\n': '&#10;', '\r': '&#13;', '\t':'&#9;'})
00069     data = escape(data, entities)
00070     if '"' in data:
00071         if "'" in data:
00072             data = '"%s"' % data.replace('"', "&quot;")
00073         else:
00074             data = "'%s'" % data
00075     else:
00076         data = '"%s"' % data
00077     return data
00078 

Here is the call graph for this function:

Here is the caller graph for this function:

def xml.sax.saxutils.unescape (   data,
  entities = {} 
)
Unescape &amp;, &lt;, and &gt; in a string of data.

You can unescape other strings of data by passing a dictionary as
the optional entities parameter.  The keys and values must all be
strings; each key will be replaced with its corresponding value.

Definition at line 41 of file saxutils.py.

00041 
00042 def unescape(data, entities={}):
00043     """Unescape &amp;, &lt;, and &gt; in a string of data.
00044 
00045     You can unescape other strings of data by passing a dictionary as
00046     the optional entities parameter.  The keys and values must all be
00047     strings; each key will be replaced with its corresponding value.
00048     """
00049     data = data.replace("&lt;", "<")
00050     data = data.replace("&gt;", ">")
00051     if entities:
00052         data = __dict_replace(data, entities)
00053     # must do ampersand last
00054     return data.replace("&amp;", "&")

Here is the call graph for this function:


Variable Documentation

Definition at line 14 of file saxutils.py.