"""This module contains the core classes of version 2.0 of SAX for Python.This file provides only default classes with absolutely minimumfunctionality, from which drivers and applications can be subclassed.Many of these classes are empty and are included only as documentationof the interfaces.$Id$"""version = '2.0beta'#============================================================================## HANDLER INTERFACES##============================================================================# ===== ERRORHANDLER =====class ErrorHandler:"""Basic interface for SAX error handlers.If you create an object that implements this interface, thenregister the object with your XMLReader, the parser will call themethods in your object to report all warnings and errors. Thereare three levels of errors available: warnings, (possibly)recoverable errors, and unrecoverable errors. All methods take aSAXParseException as the only parameter."""def error(self, exception):"Handle a recoverable error."raise exceptiondef fatalError(self, exception):"Handle a non-recoverable error."raise exceptiondef warning(self, exception):"Handle a warning."print(exception)# ===== CONTENTHANDLER =====class ContentHandler:"""Interface for receiving logical document content events.This is the main callback interface in SAX, and the one mostimportant to applications. The order of events in this interfacemirrors the order of the information in the document."""def __init__(self):self._locator = Nonedef setDocumentLocator(self, locator):"""Called by the parser to give the application a locator forlocating the origin of document events.SAX parsers are strongly encouraged (though not absolutelyrequired) to supply a locator: if it does so, it must supplythe locator to the application by invoking this method beforeinvoking any of the other methods in the DocumentHandlerinterface.The locator allows the application to determine the endposition of any document-related event, even if the parser isnot reporting an error. Typically, the application will usethis information for reporting its own errors (such ascharacter content that does not match an application'sbusiness rules). The information returned by the locator isprobably not sufficient for use with a search engine.Note that the locator will return correct information onlyduring the invocation of the events in this interface. Theapplication should not attempt to use it at any other time."""self._locator = locatordef startDocument(self):"""Receive notification of the beginning of a document.The SAX parser will invoke this method only once, before anyother methods in this interface or in DTDHandler (except forsetDocumentLocator)."""def endDocument(self):"""Receive notification of the end of a document.The SAX parser will invoke this method only once, and it willbe the last method invoked during the parse. The parser shallnot invoke this method until it has either abandoned parsing(because of an unrecoverable error) or reached the end ofinput."""def startPrefixMapping(self, prefix, uri):"""Begin the scope of a prefix-URI Namespace mapping.The information from this event is not necessary for normalNamespace processing: the SAX XML reader will automaticallyreplace prefixes for element and attribute names when thehttp://xml.org/sax/features/namespaces feature is true (thedefault).There are cases, however, when applications need to useprefixes in character data or in attribute values, where theycannot safely be expanded automatically; thestart/endPrefixMapping event supplies the information to theapplication to expand prefixes in those contexts itself, ifnecessary.Note that start/endPrefixMapping events are not guaranteed tobe properly nested relative to each-other: allstartPrefixMapping events will occur before the correspondingstartElement event, and all endPrefixMapping events will occurafter the corresponding endElement event, but their order isnot guaranteed."""def endPrefixMapping(self, prefix):"""End the scope of a prefix-URI mapping.See startPrefixMapping for details. This event will alwaysoccur after the corresponding endElement event, but the orderof endPrefixMapping events is not otherwise guaranteed."""def 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 theelement type as a string and the attrs parameter holds aninstance of the Attributes class containing the attributes ofthe element."""def endElement(self, name):"""Signals the end of an element in non-namespace mode.The name parameter contains the name of the element type, justas with the startElement event."""def 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.0name used in the source document, and the attrs parameterholds an instance of the Attributes class containing theattributes of the element.The uri part of the name tuple is None for elements which haveno namespace."""def endElementNS(self, name, qname):"""Signals the end of an element in namespace mode.The name parameter contains the name of the element type, justas with the startElementNS event."""def characters(self, content):"""Receive notification of character data.The Parser will call this method to report each chunk ofcharacter data. SAX parsers may return all contiguouscharacter data in a single chunk, or they may split it intoseveral chunks; however, all of the characters in any singleevent must come from the same external entity so that theLocator provides useful information."""def ignorableWhitespace(self, whitespace):"""Receive notification of ignorable whitespace in element content.Validating Parsers must use this method to report each chunkof ignorable whitespace (see the W3C XML 1.0 recommendation,section 2.10): non-validating parsers may also use this methodif they are capable of parsing and using content models.SAX parsers may return all contiguous whitespace in a singlechunk, or they may split it into several chunks; however, allof the characters in any single event must come from the sameexternal entity, so that the Locator provides usefulinformation."""def processingInstruction(self, target, data):"""Receive notification of a processing instruction.The Parser will invoke this method once for each processinginstruction found: note that processing instructions may occurbefore 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."""def skippedEntity(self, name):"""Receive notification of a skipped entity.The Parser will invoke this method once for each entityskipped. Non-validating processors may skip entities if theyhave not seen the declarations (because, for example, theentity was declared in an external DTD subset). All processorsmay skip external entities, depending on the values of thehttp://xml.org/sax/features/external-general-entities and thehttp://xml.org/sax/features/external-parameter-entitiesproperties."""# ===== DTDHandler =====class DTDHandler:"""Handle DTD events.This interface specifies only those DTD events required for basicparsing (unparsed entities and attributes)."""def notationDecl(self, name, publicId, systemId):"Handle a notation declaration event."def unparsedEntityDecl(self, name, publicId, systemId, ndata):"Handle an unparsed entity declaration event."# ===== ENTITYRESOLVER =====class EntityResolver:"""Basic interface for resolving entities. If you create an objectimplementing this interface, then register the object with yourParser, the parser will call the method in your object toresolve all external entities. Note that DefaultHandler implementsthis interface with the default behaviour."""def resolveEntity(self, publicId, systemId):"""Resolve the system identifier of an entity and return eitherthe system identifier to read from as a string, or an InputSourceto read from."""return systemId#============================================================================## CORE FEATURES##============================================================================feature_namespaces = "http://xml.org/sax/features/namespaces"# true: Perform Namespace processing (default).# false: Optionally do not perform Namespace processing# (implies namespace-prefixes).# access: (parsing) read-only; (not parsing) read/writefeature_namespace_prefixes = "http://xml.org/sax/features/namespace-prefixes"# true: Report the original prefixed names and attributes used for Namespace# declarations.# false: Do not report attributes used for Namespace declarations, and# optionally do not report original prefixed names (default).# access: (parsing) read-only; (not parsing) read/writefeature_string_interning = "http://xml.org/sax/features/string-interning"# true: All element names, prefixes, attribute names, Namespace URIs, and# local names are interned using the built-in intern function.# false: Names are not necessarily interned, although they may be (default).# access: (parsing) read-only; (not parsing) read/writefeature_validation = "http://xml.org/sax/features/validation"# true: Report all validation errors (implies external-general-entities and# external-parameter-entities).# false: Do not report validation errors.# access: (parsing) read-only; (not parsing) read/writefeature_external_ges = "http://xml.org/sax/features/external-general-entities"# true: Include all external general (text) entities.# false: Do not include external general entities.# access: (parsing) read-only; (not parsing) read/writefeature_external_pes = "http://xml.org/sax/features/external-parameter-entities"# true: Include all external parameter entities, including the external# DTD subset.# false: Do not include any external parameter entities, even the external# DTD subset.# access: (parsing) read-only; (not parsing) read/writeall_features = [feature_namespaces,feature_namespace_prefixes,feature_string_interning,feature_validation,feature_external_ges,feature_external_pes]#============================================================================## CORE PROPERTIES##============================================================================property_lexical_handler = "http://xml.org/sax/properties/lexical-handler"# data type: xml.sax.sax2lib.LexicalHandler# description: An optional extension handler for lexical events like comments.# access: read/writeproperty_declaration_handler = "http://xml.org/sax/properties/declaration-handler"# data type: xml.sax.sax2lib.DeclHandler# description: An optional extension handler for DTD-related events other# than notations and unparsed entities.# access: read/writeproperty_dom_node = "http://xml.org/sax/properties/dom-node"# data type: org.w3c.dom.Node# description: When parsing, the current DOM node being visited if this is# a DOM iterator; when not parsing, the root DOM node for# iteration.# access: (parsing) read-only; (not parsing) read/writeproperty_xml_string = "http://xml.org/sax/properties/xml-string"# data type: String# description: The literal string of characters that was the source for# the current event.# access: read-onlyproperty_encoding = "http://www.python.org/sax/properties/encoding"# data type: String# description: The name of the encoding to assume for input data.# access: write: set the encoding, e.g. established by a higher-level# protocol. May change during parsing (e.g. after# processing a META tag)# read: return the current encoding (possibly established through# auto-detection.# initial value: UTF-8#property_interning_dict = "http://www.python.org/sax/properties/interning-dict"# data type: Dictionary# description: The dictionary used to intern common strings in the document# access: write: Request that the parser uses a specific dictionary, to# allow interning across different documents# read: return the current interning dictionary, or None#all_properties = [property_lexical_handler,property_dom_node,property_declaration_handler,property_xml_string,property_encoding,property_interning_dict]
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。