Cross-Platform C++

ot::sax
class ContentHandler  (abstract)

#include "ot/sax/ContentHandler.h"

ot::ManagedObject ot::sax::DefaultHandler Receive notification of the logical content of a document.

This is the main interface that most SAX applications implement. If the application needs to be informed of basic parsing events (such as element and character content), it implements this interface and registers an instance using XMLReader::setContentHandler().

The order of events in this interface is very important, and mirrors the order of information in the document itself. For example, all of an element's content (character data, processing instructions, and/or sub-elements) will appear, in order, between the startElement event and the corresponding endElement event.

All the methods of ContentHandler are pure virtual which means that you must provide an implementation for each one in your derived class. This policy has been adopted to prevent subtle differences in method signatures (that would cause a new method to be declared rather than overriding the desired virtual function) from going unnoticed. However, if you find this inconvenient, you may wish to derive from DefaultHandler, which has a default implementation for all methods.

Like all SAX event handlers, a ContentHandler must be registered with a XMLReader before it can receive and process events.

    RefPtr<XMLReader> rpReader = XMLReaderFactory::CreateXMLReader();
    rpReader->setContentHandler(new MyContentHandler);
    try {
        rpReader->parse(url);
    }
    catch(Exception& e) {
        Console::Out()->println(e.toString());
    }

See also:
XMLReader , DTDHandler , DefaultHandler , ErrorHandler



Method Summary
 virtual void characters(const CharType* pStart, size_t length)=0
         Receive notification of character data.
 virtual void endDocument()=0
         Receive notification of the end of a document.
 virtual void endElement(const String& namespaceURI, const String& localName, const String& qName)=0
         Receive notification of the end of an element.
 virtual void endPrefixMapping(const String& prefix)=0
         End the scope of a prefix-URI mapping.
 virtual void ignorableWhitespace(const CharType* pStart, size_t length)=0
         Receive notification of ignorable whitespace in element content.
 virtual void processingInstruction(const String& target, const String& data)=0
         Receive notification of a processing instruction.
 virtual void setDocumentLocator(Locator* pLocator)=0
         Receive an object for locating the origin of SAX document events.
 virtual void skippedEntity(const String& name)=0
         Receive notification of a skipped entity.
 virtual void startDocument()=0
         Receive notification of the beginning of a document.
 virtual void startElement(const String& namespaceURI, const String& localName, const String& qName, const Attributes& atts)=0
         Receive notification of the beginning of an element.
 virtual void startPrefixMapping(const String& prefix, const String& uri)=0
         Begin the scope of a prefix-URI Namespace mapping.

Methods inherited from class ot::ManagedObject
addRef, getRefCount, onFinalRelease, operator=, release

Method Detail

characters

virtual void characters(const CharType* pStart,
                        size_t length)=0
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.

The array of characters will form an integral number of Unicode characters in the internal OpenTop encoding.

The memory for the character array is managed by the SAX parser and the pStart pointer is only valid for the scope of this function. The application must not attempt to read from the array beyond the specified length.

Note that when the SAX parser has an element content model available (i.e. it has read a declaration for the current element from a DTD) it will report whitespace in element content using the ignorableWhitespace() event.

Parameters:
pStart - The start of an array of CharType characters from the XML document.
length - The number of CharType characters in the array.
Exceptions:
SAXException - The client may throw an exception during processing. If so, the SAX XML parser will stop parsing the current document.
See also:
ignorableWhitespace() , Locator

endDocument

virtual void endDocument()=0
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.

Exceptions:
SAXException - The client may throw an exception during processing. If so, the SAX XML parser will stop parsing the current document.
See also:
startDocument()

endElement

virtual void endElement(const String& namespaceURI,
                        const String& localName,
                        const String& qName)=0
Receive notification of the end of an element. The SAX parser will invoke this method at the end of every element in the XML document; there will be a corresponding startElement() event for every endElement event (even when the element is empty).

For information on the names, see startElement().

Parameters:
namespaceURI - The Namespace URI, or the empty string if the element has no Namespace URI or if Namespace processing is not being performed.
localName - The local name (without prefix), or the empty string if Namespace processing is not being performed.
qName - The qualified XML 1.0 name (with prefix), or the empty string if qualified names are not available.
Exceptions:
SAXException - The client may throw an exception during processing. If so, the SAX XML parser will stop parsing the current document.
See also:
startElement()

endPrefixMapping

virtual void endPrefixMapping(const String& prefix)=0
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.

Parameters:
prefix - The namespace prefix.
Exceptions:
SAXException - The client may throw an exception during processing. If so, the SAX XML parser will stop parsing the current document.
See also:
startPrefixMapping() , endElement()

ignorableWhitespace

virtual void ignorableWhitespace(const CharType* pStart,
                                 size_t length)=0
Receive notification of ignorable whitespace in element content. When the SAX Parser has a content model for the current element, it will use this method to report each chunk of whitespace in element content (see the W3C XML 1.0 recommendation, section 2.10).

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.

The array of characters will form an integral number of Unicode characters in the internal OpenTop encoding.

The memory for the character array is managed by the SAX parser and the pStart pointer is only valid for the scope of this function. The application must not attempt to read from the array beyond the specified length.

Note that, when validating, the SAX parser will report whitespace in element content using the ignorableWhitespace().

Parameters:
pStart - The start of an array of CharType characters from the XML document.
length - The number of CharType characters in the array.
Exceptions:
SAXException - The client may throw an exception during processing. If so, the SAX XML parser will stop parsing the current document.
See also:
characters()

processingInstruction

virtual void processingInstruction(const String& target,
                                   const String& data)=0
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.

The SAX parser will not report an XML declaration (XML 1.0, section 2.8) or a text declaration (XML 1.0, section 4.3.1) using this method.

Parameters:
target - The processing instruction target.
data - The processing instruction data, or the empty string if none was supplied. The data does not include any whitespace separating it from the target.
Exceptions:
SAXException - The client may throw an exception during processing. If so, the SAX XML parser will stop parsing the current document.

setDocumentLocator

virtual void setDocumentLocator(Locator* pLocator)=0
Receive an object for locating the origin of SAX document events. The SAX parser supplies the Locator to the application by invoking this method before invoking any of the other methods in the ContentHandler interface. The application should store pLocator in a RefPtr<Locator> if it wishes to use it in the future.

    void myHandler::setDocumentLocator(Locator* pLocator)
    {
        m_rpLocator = pLocator;
    }

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).

    void myHandler::ignorableWhitespace(const String& string)
    {
        Console::cout() << OT_T("received ignorable whitespace at [")
                        << m_rpLocator->getLineNumber()
                        << OT_T(",")
                        << m_rpLocator->getColumnNumber()
                        << OT_T("]") << endl;
    }

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.

Parameters:
pLocator - A pointer to an object that can return the location of any SAX document event.
See also:
Locator

skippedEntity

virtual void skippedEntity(const String& name)=0
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 features.

Parameters:
name - The name of the skipped entity. If it is a parameter entity, the name will begin with '%', and if it is the external DTD subset, it will be the string "[dtd]".
Exceptions:
SAXException - The client may throw an exception during processing. If so, the SAX XML parser will stop parsing the current document.

startDocument

virtual void startDocument()=0
Receive notification of the beginning of a document.

The SAX parser will invoke this method only once, before any other event callbacks (except for setDocumentLocator).

Exceptions:
SAXException - The client may throw an exception during processing. If so, the SAX XML parser will stop parsing the current document.
See also:
endDocument()

startElement

virtual void startElement(const String& namespaceURI,
                          const String& localName,
                          const String& qName,
                          const Attributes& atts)=0
Receive notification of the beginning of an element. The Parser will invoke this method at the beginning of every element in the XML document; there will be a corresponding endElement() event for every startElement event (even when the element is empty). All of the element's content will be reported, in order, before the corresponding endElement event.

This event allows up to three name components for each element:

  1. the Namespace URI;
  2. the local name; and
  3. the qualified (prefixed) name.

Any or all of these may be provided, depending on the values of the http://xml.org/sax/features/namespaces and the http://xml.org/sax/features/namespace-prefixes features:

Note that the attribute list provided will contain only attributes with explicit values (specified or defaulted): #IMPLIED attributes will be omitted. The attribute list will contain attributes used for Namespace declarations (xmlns* attributes) only if the http://xml.org/sax/features/namespace-prefixes feature is true (it is false by default).

Parameters:
namespaceURI - The Namespace URI, or the empty string if the element has no Namespace URI or if Namespace processing is not being performed.
localName - The local name (without prefix), or the empty string if Namespace processing is not being performed.
qName - The qualified name (with prefix).
atts - The attributes attached to the element. If there are no attributes, it shall be an empty Attributes object.
Exceptions:
SAXException - The client may throw an exception during processing. If so, the SAX XML parser will stop parsing the current document.
See also:
endElement() , Attributes

startPrefixMapping

virtual void startPrefixMapping(const String& prefix,
                                const String& uri)=0
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 otherwise guaranteed.

There will never be start/endPrefixMapping events for the 'xml' prefix, since it is predeclared and immutable.

Parameters:
prefix - The Namespace prefix being declared.
uri - The Namespace URI the prefix is mapped to.
Exceptions:
SAXException - The client may throw an exception during processing. If so, the SAX XML parser will stop parsing the current document.
See also:
endPrefixMapping , startElement


Cross-Platform C++

Found a bug or missing feature? Please email us at support@elcel.com

Copyright © 2000-2003 ElCel Technology   Trademark Acknowledgements