Initial commit of OpenSPARC T2 architecture model.
[OpenSPARC-T2-SAM] / sam-t2 / devtools / v8plus / html / python / lib / module-sgmllib.html
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<link rel="STYLESHEET" href="lib.css" type='text/css' />
<link rel="SHORTCUT ICON" href="../icons/pyfav.png" type="image/png" />
<link rel='start' href='../index.html' title='Python Documentation Index' />
<link rel="first" href="lib.html" title='Python Library Reference' />
<link rel='contents' href='contents.html' title="Contents" />
<link rel='index' href='genindex.html' title='Index' />
<link rel='last' href='about.html' title='About this document...' />
<link rel='help' href='about.html' title='About this document...' />
<link rel="next" href="module-htmllib.html" />
<link rel="prev" href="module-HTMLParser.html" />
<link rel="parent" href="markup.html" />
<link rel="next" href="module-htmllib.html" />
<meta name='aesop' content='information' />
<title>13.2 sgmllib -- Simple SGML parser</title>
</head>
<body>
<DIV CLASS="navigation">
<div id='top-navigation-panel' xml:id='top-navigation-panel'>
<table align="center" width="100%" cellpadding="0" cellspacing="2">
<tr>
<td class='online-navigation'><a rel="prev" title="13.1.1 Example HTML Parser"
href="htmlparser-example.html"><img src='../icons/previous.png'
border='0' height='32' alt='Previous Page' width='32' /></A></td>
<td class='online-navigation'><a rel="parent" title="13. Structured Markup Processing"
href="markup.html"><img src='../icons/up.png'
border='0' height='32' alt='Up One Level' width='32' /></A></td>
<td class='online-navigation'><a rel="next" title="13.3 htmllib "
href="module-htmllib.html"><img src='../icons/next.png'
border='0' height='32' alt='Next Page' width='32' /></A></td>
<td align="center" width="100%">Python Library Reference</td>
<td class='online-navigation'><a rel="contents" title="Table of Contents"
href="contents.html"><img src='../icons/contents.png'
border='0' height='32' alt='Contents' width='32' /></A></td>
<td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png'
border='0' height='32' alt='Module Index' width='32' /></a></td>
<td class='online-navigation'><a rel="index" title="Index"
href="genindex.html"><img src='../icons/index.png'
border='0' height='32' alt='Index' width='32' /></A></td>
</tr></table>
<div class='online-navigation'>
<b class="navlabel">Previous:</b>
<a class="sectref" rel="prev" href="htmlparser-example.html">13.1.1 Example HTML Parser</A>
<b class="navlabel">Up:</b>
<a class="sectref" rel="parent" href="markup.html">13. Structured Markup Processing</A>
<b class="navlabel">Next:</b>
<a class="sectref" rel="next" href="module-htmllib.html">13.3 htmllib </A>
</div>
<hr /></div>
</DIV>
<!--End of Navigation Panel-->
<H1><A NAME="SECTION0015200000000000000000">
13.2 <tt class="module">sgmllib</tt> --
Simple SGML parser</A>
</H1>
<P>
<A NAME="module-sgmllib"></A>
<P>
<a id='l2h-4280' xml:id='l2h-4280'></a>
<P>
This module defines a class <tt class="class">SGMLParser</tt> which serves as the
basis for parsing text files formatted in SGML (Standard Generalized
Mark-up Language). In fact, it does not provide a full SGML parser
-- it only parses SGML insofar as it is used by HTML, and the module
only exists as a base for the <tt class="module"><a href="module-htmllib.html">htmllib</a></tt> module. Another
HTML parser which supports XHTML and offers a somewhat different
interface is available in the <tt class="module"><a href="module-HTMLParser.html">HTMLParser</a></tt> module.
<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
<td><nobr><b><span class="typelabel">class</span>&nbsp;<tt id='l2h-4260' xml:id='l2h-4260' class="class">SGMLParser</tt></b>(</nobr></td>
<td><var></var>)</td></tr></table></dt>
<dd>
The <tt class="class">SGMLParser</tt> class is instantiated without arguments.
The parser is hardcoded to recognize the following
constructs:
<P>
<UL>
<LI>Opening and closing tags of the form
"<tt class="samp">&lt;<var>tag</var> <var>attr</var>="<var>value</var>" ...&gt;</tt>" and
"<tt class="samp">&lt;/<var>tag</var>&gt;</tt>", respectively.
<P>
</LI>
<LI>Numeric character references of the form "<tt class="samp">&amp;#<var>name</var>;</tt>".
<P>
</LI>
<LI>Entity references of the form "<tt class="samp">&amp;<var>name</var>;</tt>".
<P>
</LI>
<LI>SGML comments of the form "<tt class="samp">&lt;!-&#45;<var>text</var>-&#45;&gt;</tt>". Note that
spaces, tabs, and newlines are allowed between the trailing
"<tt class="samp">&gt;</tt>" and the immediately preceding "<tt class="samp">-&#45;</tt>".
<P>
</LI>
</UL>
</dl>
<P>
A single exception is defined as well:
<P>
<dl><dt><b><span class="typelabel">exception</span>&nbsp;<tt id='l2h-4261' xml:id='l2h-4261' class="exception">SGMLParseError</tt></b></dt>
<dd>
Exception raised by the <tt class="class">SGMLParser</tt> class when it encounters an
error while parsing.
<span class="versionnote">New in version 2.1.</span>
</dd></dl>
<P>
<tt class="class">SGMLParser</tt> instances have the following methods:
<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
<td><nobr><b><tt id='l2h-4262' xml:id='l2h-4262' class="method">reset</tt></b>(</nobr></td>
<td><var></var>)</td></tr></table></dt>
<dd>
Reset the instance. Loses all unprocessed data. This is called
implicitly at instantiation time.
</dl>
<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
<td><nobr><b><tt id='l2h-4263' xml:id='l2h-4263' class="method">setnomoretags</tt></b>(</nobr></td>
<td><var></var>)</td></tr></table></dt>
<dd>
Stop processing tags. Treat all following input as literal input
(CDATA). (This is only provided so the HTML tag
<code>&lt;PLAINTEXT&gt;</code> can be implemented.)
</dl>
<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
<td><nobr><b><tt id='l2h-4264' xml:id='l2h-4264' class="method">setliteral</tt></b>(</nobr></td>
<td><var></var>)</td></tr></table></dt>
<dd>
Enter literal mode (CDATA mode).
</dl>
<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
<td><nobr><b><tt id='l2h-4265' xml:id='l2h-4265' class="method">feed</tt></b>(</nobr></td>
<td><var>data</var>)</td></tr></table></dt>
<dd>
Feed some text to the parser. It is processed insofar as it consists
of complete elements; incomplete data is buffered until more data is
fed or <tt class="method">close()</tt> is called.
</dl>
<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
<td><nobr><b><tt id='l2h-4266' xml:id='l2h-4266' class="method">close</tt></b>(</nobr></td>
<td><var></var>)</td></tr></table></dt>
<dd>
Force processing of all buffered data as if it were followed by an
end-of-file mark. This method may be redefined by a derived class to
define additional processing at the end of the input, but the
redefined version should always call <tt class="method">close()</tt>.
</dl>
<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
<td><nobr><b><tt id='l2h-4267' xml:id='l2h-4267' class="method">get_starttag_text</tt></b>(</nobr></td>
<td><var></var>)</td></tr></table></dt>
<dd>
Return the text of the most recently opened start tag. This should
not normally be needed for structured processing, but may be useful in
dealing with HTML ``as deployed'' or for re-generating input with
minimal changes (whitespace between attributes can be preserved,
etc.).
</dl>
<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
<td><nobr><b><tt id='l2h-4268' xml:id='l2h-4268' class="method">handle_starttag</tt></b>(</nobr></td>
<td><var>tag, method, attributes</var>)</td></tr></table></dt>
<dd>
This method is called to handle start tags for which either a
<tt class="method">start_<var>tag</var>()</tt> or <tt class="method">do_<var>tag</var>()</tt> method has been
defined. The <var>tag</var> argument is the name of the tag converted to
lower case, and the <var>method</var> argument is the bound method which
should be used to support semantic interpretation of the start tag.
The <var>attributes</var> argument is a list of <code>(<var>name</var>,
<var>value</var>)</code> pairs containing the attributes found inside the tag's
<code>&lt;&gt;</code> brackets. The <var>name</var> has been translated to lower case
and double quotes and backslashes in the <var>value</var> have been interpreted.
For instance, for the tag <code>&lt;A HREF="http://www.cwi.nl/"&gt;</code>, this
method would be called as "<tt class="samp">unknown_starttag('a', [('href',
'http://www.cwi.nl/')])</tt>". The base implementation simply calls
<var>method</var> with <var>attributes</var> as the only argument.
</dl>
<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
<td><nobr><b><tt id='l2h-4269' xml:id='l2h-4269' class="method">handle_endtag</tt></b>(</nobr></td>
<td><var>tag, method</var>)</td></tr></table></dt>
<dd>
This method is called to handle endtags for which an
<tt class="method">end_<var>tag</var>()</tt> method has been defined. The
<var>tag</var> argument is the name of the tag converted to lower case, and
the <var>method</var> argument is the bound method which should be used to
support semantic interpretation of the end tag. If no
<tt class="method">end_<var>tag</var>()</tt> method is defined for the closing element,
this handler is not called. The base implementation simply calls
<var>method</var>.
</dl>
<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
<td><nobr><b><tt id='l2h-4270' xml:id='l2h-4270' class="method">handle_data</tt></b>(</nobr></td>
<td><var>data</var>)</td></tr></table></dt>
<dd>
This method is called to process arbitrary data. It is intended to be
overridden by a derived class; the base class implementation does
nothing.
</dl>
<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
<td><nobr><b><tt id='l2h-4271' xml:id='l2h-4271' class="method">handle_charref</tt></b>(</nobr></td>
<td><var>ref</var>)</td></tr></table></dt>
<dd>
This method is called to process a character reference of the form
"<tt class="samp">&amp;#<var>ref</var>;</tt>". In the base implementation, <var>ref</var> must
be a decimal number in the
range 0-255. It translates the character to ASCII and calls the
method <tt class="method">handle_data()</tt> with the character as argument. If
<var>ref</var> is invalid or out of range, the method
<code>unknown_charref(<var>ref</var>)</code> is called to handle the error. A
subclass must override this method to provide support for named
character entities.
</dl>
<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
<td><nobr><b><tt id='l2h-4272' xml:id='l2h-4272' class="method">handle_entityref</tt></b>(</nobr></td>
<td><var>ref</var>)</td></tr></table></dt>
<dd>
This method is called to process a general entity reference of the
form "<tt class="samp">&amp;<var>ref</var>;</tt>" where <var>ref</var> is an general entity
reference. It looks for <var>ref</var> in the instance (or class)
variable <tt class="member">entitydefs</tt> which should be a mapping from entity
names to corresponding translations. If a translation is found, it
calls the method <tt class="method">handle_data()</tt> with the translation;
otherwise, it calls the method <code>unknown_entityref(<var>ref</var>)</code>.
The default <tt class="member">entitydefs</tt> defines translations for
<code>&amp;amp;</code>, <code>&amp;apos</code>, <code>&amp;gt;</code>, <code>&amp;lt;</code>, and
<code>&amp;quot;</code>.
</dl>
<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
<td><nobr><b><tt id='l2h-4273' xml:id='l2h-4273' class="method">handle_comment</tt></b>(</nobr></td>
<td><var>comment</var>)</td></tr></table></dt>
<dd>
This method is called when a comment is encountered. The
<var>comment</var> argument is a string containing the text between the
"<tt class="samp">&lt;!-&#45;</tt>" and "<tt class="samp">-&#45;&gt;</tt>" delimiters, but not the delimiters
themselves. For example, the comment "<tt class="samp">&lt;!-&#45;text-&#45;&gt;</tt>" will
cause this method to be called with the argument <code>'text'</code>. The
default method does nothing.
</dl>
<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
<td><nobr><b><tt id='l2h-4274' xml:id='l2h-4274' class="method">handle_decl</tt></b>(</nobr></td>
<td><var>data</var>)</td></tr></table></dt>
<dd>
Method called when an SGML declaration is read by the parser. In
practice, the <code>DOCTYPE</code> declaration is the only thing observed in
HTML, but the parser does not discriminate among different (or broken)
declarations. Internal subsets in a <code>DOCTYPE</code> declaration are
not supported. The <var>data</var> parameter will be the entire contents
of the declaration inside the <code>&lt;!</code>...<code>&gt;</code> markup. The
default implementation does nothing.
</dl>
<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
<td><nobr><b><tt id='l2h-4275' xml:id='l2h-4275' class="method">report_unbalanced</tt></b>(</nobr></td>
<td><var>tag</var>)</td></tr></table></dt>
<dd>
This method is called when an end tag is found which does not
correspond to any open element.
</dl>
<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
<td><nobr><b><tt id='l2h-4276' xml:id='l2h-4276' class="method">unknown_starttag</tt></b>(</nobr></td>
<td><var>tag, attributes</var>)</td></tr></table></dt>
<dd>
This method is called to process an unknown start tag. It is intended
to be overridden by a derived class; the base class implementation
does nothing.
</dl>
<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
<td><nobr><b><tt id='l2h-4277' xml:id='l2h-4277' class="method">unknown_endtag</tt></b>(</nobr></td>
<td><var>tag</var>)</td></tr></table></dt>
<dd>
This method is called to process an unknown end tag. It is intended
to be overridden by a derived class; the base class implementation
does nothing.
</dl>
<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
<td><nobr><b><tt id='l2h-4278' xml:id='l2h-4278' class="method">unknown_charref</tt></b>(</nobr></td>
<td><var>ref</var>)</td></tr></table></dt>
<dd>
This method is called to process unresolvable numeric character
references. Refer to <tt class="method">handle_charref()</tt> to determine what is
handled by default. It is intended to be overridden by a derived
class; the base class implementation does nothing.
</dl>
<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
<td><nobr><b><tt id='l2h-4279' xml:id='l2h-4279' class="method">unknown_entityref</tt></b>(</nobr></td>
<td><var>ref</var>)</td></tr></table></dt>
<dd>
This method is called to process an unknown entity reference. It is
intended to be overridden by a derived class; the base class
implementation does nothing.
</dl>
<P>
Apart from overriding or extending the methods listed above, derived
classes may also define methods of the following form to define
processing of specific tags. Tag names in the input stream are case
independent; the <var>tag</var> occurring in method names must be in lower
case:
<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
<td><nobr><b>start_<var>tag</var></b>(</nobr></td>
<td><var>attributes</var>)</td></tr></table></dt>
<dd>
This method is called to process an opening tag <var>tag</var>. It has
preference over <tt class="method">do_<var>tag</var>()</tt>. The
<var>attributes</var> argument has the same meaning as described for
<tt class="method">handle_starttag()</tt> above.
</dl>
<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
<td><nobr><b>do_<var>tag</var></b>(</nobr></td>
<td><var>attributes</var>)</td></tr></table></dt>
<dd>
This method is called to process an opening tag <var>tag</var> that does
not come with a matching closing tag. The <var>attributes</var> argument
has the same meaning as described for <tt class="method">handle_starttag()</tt> above.
</dl>
<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
<td><nobr><b>end_<var>tag</var></b>(</nobr></td>
<td><var></var>)</td></tr></table></dt>
<dd>
This method is called to process a closing tag <var>tag</var>.
</dl>
<P>
Note that the parser maintains a stack of open elements for which no
end tag has been found yet. Only tags processed by
<tt class="method">start_<var>tag</var>()</tt> are pushed on this stack. Definition of an
<tt class="method">end_<var>tag</var>()</tt> method is optional for these tags. For tags
processed by <tt class="method">do_<var>tag</var>()</tt> or by <tt class="method">unknown_tag()</tt>, no
<tt class="method">end_<var>tag</var>()</tt> method must be defined; if defined, it will
not be used. If both <tt class="method">start_<var>tag</var>()</tt> and
<tt class="method">do_<var>tag</var>()</tt> methods exist for a tag, the
<tt class="method">start_<var>tag</var>()</tt> method takes precedence.
<DIV CLASS="navigation">
<div class='online-navigation'>
<p></p><hr />
<table align="center" width="100%" cellpadding="0" cellspacing="2">
<tr>
<td class='online-navigation'><a rel="prev" title="13.1.1 Example HTML Parser"
href="htmlparser-example.html"><img src='../icons/previous.png'
border='0' height='32' alt='Previous Page' width='32' /></A></td>
<td class='online-navigation'><a rel="parent" title="13. Structured Markup Processing"
href="markup.html"><img src='../icons/up.png'
border='0' height='32' alt='Up One Level' width='32' /></A></td>
<td class='online-navigation'><a rel="next" title="13.3 htmllib "
href="module-htmllib.html"><img src='../icons/next.png'
border='0' height='32' alt='Next Page' width='32' /></A></td>
<td align="center" width="100%">Python Library Reference</td>
<td class='online-navigation'><a rel="contents" title="Table of Contents"
href="contents.html"><img src='../icons/contents.png'
border='0' height='32' alt='Contents' width='32' /></A></td>
<td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png'
border='0' height='32' alt='Module Index' width='32' /></a></td>
<td class='online-navigation'><a rel="index" title="Index"
href="genindex.html"><img src='../icons/index.png'
border='0' height='32' alt='Index' width='32' /></A></td>
</tr></table>
<div class='online-navigation'>
<b class="navlabel">Previous:</b>
<a class="sectref" rel="prev" href="htmlparser-example.html">13.1.1 Example HTML Parser</A>
<b class="navlabel">Up:</b>
<a class="sectref" rel="parent" href="markup.html">13. Structured Markup Processing</A>
<b class="navlabel">Next:</b>
<a class="sectref" rel="next" href="module-htmllib.html">13.3 htmllib </A>
</div>
</div>
<hr />
<span class="release-info">Release 2.4.2, documentation updated on 28 September 2005.</span>
</DIV>
<!--End of Navigation Panel-->
<ADDRESS>
See <i><a href="about.html">About this document...</a></i> for information on suggesting changes.
</ADDRESS>
</BODY>
</HTML>