Back to index

python3.2  3.2.2
Public Member Functions | Private Member Functions | Private Attributes
xml.sax.saxutils.XMLGenerator Class Reference
Inheritance diagram for xml.sax.saxutils.XMLGenerator:
Inheritance graph
[legend]
Collaboration diagram for xml.sax.saxutils.XMLGenerator:
Collaboration graph
[legend]

List of all members.

Public Member Functions

def __init__
def startDocument
def startPrefixMapping
def endPrefixMapping
def startElement
def endElement
def startElementNS
def endElementNS
def characters
def ignorableWhitespace
def processingInstruction
def setDocumentLocator
def endDocument
def skippedEntity

Private Member Functions

def _write
def _qname
def _finish_pending_start_element

Private Attributes

 _out
 _ns_contexts
 _current_context
 _undeclared_ns_maps
 _encoding
 _short_empty_elements
 _pending_start_element

Detailed Description

Definition at line 79 of file saxutils.py.


Constructor & Destructor Documentation

def xml.sax.saxutils.XMLGenerator.__init__ (   self,
  out = None,
  encoding = "iso-8859-1",
  short_empty_elements = False 
)

Definition at line 81 of file saxutils.py.

00081 
00082     def __init__(self, out=None, encoding="iso-8859-1", short_empty_elements=False):
00083         if out is None:
00084             import sys
00085             out = sys.stdout
00086         handler.ContentHandler.__init__(self)
00087         self._out = out
00088         self._ns_contexts = [{}] # contains uri -> prefix dicts
00089         self._current_context = self._ns_contexts[-1]
00090         self._undeclared_ns_maps = []
00091         self._encoding = encoding
00092         self._short_empty_elements = short_empty_elements
00093         self._pending_start_element = False

Here is the caller graph for this function:


Member Function Documentation

def xml.sax.saxutils.XMLGenerator._finish_pending_start_element (   self,
  endElement = False 
) [private]

Definition at line 117 of file saxutils.py.

00117 
00118     def _finish_pending_start_element(self,endElement=False):
00119         if self._pending_start_element:
00120             self._write('>')
00121             self._pending_start_element = False

Here is the call graph for this function:

Here is the caller graph for this function:

def xml.sax.saxutils.XMLGenerator._qname (   self,
  name 
) [private]
Builds a qualified name from a (ns_url, localname) pair

Definition at line 100 of file saxutils.py.

00100 
00101     def _qname(self, name):
00102         """Builds a qualified name from a (ns_url, localname) pair"""
00103         if name[0]:
00104             # Per http://www.w3.org/XML/1998/namespace, The 'xml' prefix is
00105             # bound by definition to http://www.w3.org/XML/1998/namespace.  It
00106             # does not need to be declared and will not usually be found in
00107             # self._current_context.
00108             if 'http://www.w3.org/XML/1998/namespace' == name[0]:
00109                 return 'xml:' + name[1]
00110             # The name is in a non-empty namespace
00111             prefix = self._current_context[name[0]]
00112             if prefix:
00113                 # If it is not the default namespace, prepend the prefix
00114                 return prefix + ":" + name[1]
00115         # Return the unqualified name
00116         return name[1]

Here is the caller graph for this function:

def xml.sax.saxutils.XMLGenerator._write (   self,
  text 
) [private]

Definition at line 94 of file saxutils.py.

00094 
00095     def _write(self, text):
00096         if isinstance(text, str):
00097             self._out.write(text)
00098         else:
00099             self._out.write(text.encode(self._encoding, _error_handling))

Here is the caller graph for this function:

def xml.sax.saxutils.XMLGenerator.characters (   self,
  content 
)
Receive notification of character data.

The Parser will call this method to report each chunk of
character data. SAX parsers may return all contiguous
character data in a single chunk, or they may split it into
several chunks; however, all of the characters in any single
event must come from the same external entity so that the
Locator provides useful information.

Reimplemented from xml.sax.handler.ContentHandler.

Definition at line 179 of file saxutils.py.

00179 
00180     def characters(self, content):
00181         if content:
00182             self._finish_pending_start_element()
00183             self._write(escape(content))

Here is the call graph for this function:

def xml.sax.handler.ContentHandler.endDocument (   self) [inherited]
Receive notification of the end of a document.

The SAX parser will invoke this method only once, and it will
be the last method invoked during the parse. The parser shall
not invoke this method until it has either abandoned parsing
(because of an unrecoverable error) or reached the end of
input.

Definition at line 87 of file handler.py.

00087 
00088     def endDocument(self):
00089         """Receive notification of the end of a document.
00090 
00091         The SAX parser will invoke this method only once, and it will
00092         be the last method invoked during the parse. The parser shall
00093         not invoke this method until it has either abandoned parsing
00094         (because of an unrecoverable error) or reached the end of
00095         input."""

def xml.sax.saxutils.XMLGenerator.endElement (   self,
  name 
)
Signals the end of an element in non-namespace mode.

The name parameter contains the name of the element type, just
as with the startElement event.

Reimplemented from xml.sax.handler.ContentHandler.

Definition at line 147 of file saxutils.py.

00147 
00148     def endElement(self, name):
00149         if self._pending_start_element:
00150             self._write('/>')
00151             self._pending_start_element = False
00152         else:
00153             self._write('</%s>' % name)

Here is the call graph for this function:

def xml.sax.saxutils.XMLGenerator.endElementNS (   self,
  name,
  qname 
)
Signals the end of an element in namespace mode.

The name parameter contains the name of the element type, just
as with the startElementNS event.

Reimplemented from xml.sax.handler.ContentHandler.

Definition at line 172 of file saxutils.py.

00172 
00173     def endElementNS(self, name, qname):
00174         if self._pending_start_element:
00175             self._write('/>')
00176             self._pending_start_element = False
00177         else:
00178             self._write('</%s>' % self._qname(name))

Here is the call graph for this function:

def xml.sax.saxutils.XMLGenerator.endPrefixMapping (   self,
  prefix 
)
End the scope of a prefix-URI mapping.

See startPrefixMapping for details. This event will always
occur after the corresponding endElement event, but the order
of endPrefixMapping events is not otherwise guaranteed.

Reimplemented from xml.sax.handler.ContentHandler.

Definition at line 133 of file saxutils.py.

00133 
00134     def endPrefixMapping(self, prefix):
00135         self._current_context = self._ns_contexts[-1]
00136         del self._ns_contexts[-1]

def xml.sax.saxutils.XMLGenerator.ignorableWhitespace (   self,
  whitespace 
)
Receive notification of ignorable whitespace in element content.

Validating Parsers must use this method to report each chunk
of ignorable whitespace (see the W3C XML 1.0 recommendation,
section 2.10): non-validating parsers may also use this method
if they are capable of parsing and using content models.

SAX parsers may return all contiguous whitespace in a single
chunk, or they may split it into several chunks; however, all
of the characters in any single event must come from the same
external entity, so that the Locator provides useful
information.

Reimplemented from xml.sax.handler.ContentHandler.

Definition at line 184 of file saxutils.py.

00184 
00185     def ignorableWhitespace(self, content):
00186         if content:
00187             self._finish_pending_start_element()
00188             self._write(content)

Here is the call graph for this function:

def xml.sax.saxutils.XMLGenerator.processingInstruction (   self,
  target,
  data 
)
Receive notification of a processing instruction.

The Parser will invoke this method once for each processing
instruction found: note that processing instructions may occur
before or after the main document element.

A SAX parser should never report an XML declaration (XML 1.0,
section 2.8) or a text declaration (XML 1.0, section 4.3.1)
using this method.

Reimplemented from xml.sax.handler.ContentHandler.

Definition at line 189 of file saxutils.py.

00189 
00190     def processingInstruction(self, target, data):
00191         self._finish_pending_start_element()
00192         self._write('<?%s %s?>' % (target, data))
00193 

Here is the call graph for this function:

def xml.sax.handler.ContentHandler.setDocumentLocator (   self,
  locator 
) [inherited]
Called by the parser to give the application a locator for
locating the origin of document events.

SAX parsers are strongly encouraged (though not absolutely
required) to supply a locator: if it does so, it must supply
the locator to the application by invoking this method before
invoking any of the other methods in the DocumentHandler
interface.

The locator allows the application to determine the end
position of any document-related event, even if the parser is
not reporting an error. Typically, the application will use
this information for reporting its own errors (such as
character content that does not match an application's
business rules). The information returned by the locator is
probably not sufficient for use with a search engine.

Note that the locator will return correct information only
during the invocation of the events in this interface. The
application should not attempt to use it at any other time.

Definition at line 57 of file handler.py.

00057 
00058     def setDocumentLocator(self, locator):
00059         """Called by the parser to give the application a locator for
00060         locating the origin of document events.
00061 
00062         SAX parsers are strongly encouraged (though not absolutely
00063         required) to supply a locator: if it does so, it must supply
00064         the locator to the application by invoking this method before
00065         invoking any of the other methods in the DocumentHandler
00066         interface.
00067 
00068         The locator allows the application to determine the end
00069         position of any document-related event, even if the parser is
00070         not reporting an error. Typically, the application will use
00071         this information for reporting its own errors (such as
00072         character content that does not match an application's
00073         business rules). The information returned by the locator is
00074         probably not sufficient for use with a search engine.
00075 
00076         Note that the locator will return correct information only
00077         during the invocation of the events in this interface. The
00078         application should not attempt to use it at any other time."""
00079         self._locator = locator

def xml.sax.handler.ContentHandler.skippedEntity (   self,
  name 
) [inherited]
Receive notification of a skipped entity.

The Parser will invoke this method once for each entity
skipped. Non-validating processors may skip entities if they
have not seen the declarations (because, for example, the
entity was declared in an external DTD subset). All processors
may skip external entities, depending on the values of the
http://xml.org/sax/features/external-general-entities and the
http://xml.org/sax/features/external-parameter-entities
properties.

Definition at line 193 of file handler.py.

00193 
00194     def skippedEntity(self, name):
00195         """Receive notification of a skipped entity.
00196 
00197         The Parser will invoke this method once for each entity
00198         skipped. Non-validating processors may skip entities if they
00199         have not seen the declarations (because, for example, the
00200         entity was declared in an external DTD subset). All processors
00201         may skip external entities, depending on the values of the
00202         http://xml.org/sax/features/external-general-entities and the
00203         http://xml.org/sax/features/external-parameter-entities
00204         properties."""
00205 
00206 
00207 # ===== DTDHandler =====

Receive notification of the beginning of a document.

The SAX parser will invoke this method only once, before any
other methods in this interface or in DTDHandler (except for
setDocumentLocator).

Reimplemented from xml.sax.handler.ContentHandler.

Definition at line 124 of file saxutils.py.

00124 
00125     def startDocument(self):
00126         self._write('<?xml version="1.0" encoding="%s"?>\n' %
00127                         self._encoding)

Here is the call graph for this function:

def xml.sax.saxutils.XMLGenerator.startElement (   self,
  name,
  attrs 
)
Signals the start of an element in non-namespace mode.

The name parameter contains the raw XML 1.0 name of the
element type as a string and the attrs parameter holds an
instance of the Attributes class containing the attributes of
the element.

Reimplemented from xml.sax.handler.ContentHandler.

Definition at line 137 of file saxutils.py.

00137 
00138     def startElement(self, name, attrs):
00139         self._finish_pending_start_element()
00140         self._write('<' + name)
00141         for (name, value) in attrs.items():
00142             self._write(' %s=%s' % (name, quoteattr(value)))
00143         if self._short_empty_elements:
00144             self._pending_start_element = True
00145         else:
00146             self._write(">")

Here is the call graph for this function:

def xml.sax.saxutils.XMLGenerator.startElementNS (   self,
  name,
  qname,
  attrs 
)
Signals the start of an element in namespace mode.

The name parameter contains the name of the element type as a
(uri, localname) tuple, the qname parameter the raw XML 1.0
name used in the source document, and the attrs parameter
holds an instance of the Attributes class containing the
attributes of the element.

The uri part of the name tuple is None for elements which have
no namespace.

Reimplemented from xml.sax.handler.ContentHandler.

Definition at line 154 of file saxutils.py.

00154 
00155     def startElementNS(self, name, qname, attrs):
00156         self._finish_pending_start_element()
00157         self._write('<' + self._qname(name))
00158 
00159         for prefix, uri in self._undeclared_ns_maps:
00160             if prefix:
00161                 self._out.write(' xmlns:%s="%s"' % (prefix, uri))
00162             else:
00163                 self._out.write(' xmlns="%s"' % uri)
00164         self._undeclared_ns_maps = []
00165 
00166         for (name, value) in attrs.items():
00167             self._write(' %s=%s' % (self._qname(name), quoteattr(value)))
00168         if self._short_empty_elements:
00169             self._pending_start_element = True
00170         else:
00171             self._write(">")

Here is the call graph for this function:

def xml.sax.saxutils.XMLGenerator.startPrefixMapping (   self,
  prefix,
  uri 
)
Begin the scope of a prefix-URI Namespace mapping.

The information from this event is not necessary for normal
Namespace processing: the SAX XML reader will automatically
replace prefixes for element and attribute names when the
http://xml.org/sax/features/namespaces feature is true (the
default).

There are cases, however, when applications need to use
prefixes in character data or in attribute values, where they
cannot safely be expanded automatically; the
start/endPrefixMapping event supplies the information to the
application to expand prefixes in those contexts itself, if
necessary.

Note that start/endPrefixMapping events are not guaranteed to
be properly nested relative to each-other: all
startPrefixMapping events will occur before the corresponding
startElement event, and all endPrefixMapping events will occur
after the corresponding endElement event, but their order is
not guaranteed.

Reimplemented from xml.sax.handler.ContentHandler.

Definition at line 128 of file saxutils.py.

00128 
00129     def startPrefixMapping(self, prefix, uri):
00130         self._ns_contexts.append(self._current_context.copy())
00131         self._current_context[uri] = prefix
00132         self._undeclared_ns_maps.append((prefix, uri))


Member Data Documentation

Definition at line 88 of file saxutils.py.

Definition at line 90 of file saxutils.py.

Definition at line 87 of file saxutils.py.

Definition at line 86 of file saxutils.py.

Definition at line 92 of file saxutils.py.

Definition at line 91 of file saxutils.py.

Definition at line 89 of file saxutils.py.


The documentation for this class was generated from the following file: