Helper classes

A parser developer may choose to write classes that implement the interfaces described above, and also add further useful methods. But this kind of support is optional. The standard includes some recommendations for helper classes, and specifies that they appear in the following package:

org.xml.sax.helpers

Attributes list implementation

The AttributesListImpl helper class has the following constructors and methods. It implements the AttributesList interface, and adds methods for adding and removing attributes. It could be used by application developers to persistently store and manage attributes.

Constructors:

AttributeListImpl();

AttributeListImpl(AttributeList atts);

Methods:

int     getLength();
String  getName(int i);
String  getType(int i);
String  getType(String name);
String  getValue(int i);
String  getValue(String name);

void    setAttributeList(AttributeList atts);
void    addAttribute(String name, String type,
                     String value);
void    removeAttribute(String name);
void    clear();

Note that SAX 2.0 deprecates this class and introduces a replacement called AttributesImpl.

Locator implementation

The LocatorImpl helper class implements the Locator interface, and adds methods for setting its properties, so would be most likely to be used by the parser rather than the application itself. It has the following constructor and methods.

Constructor:

LocatorImpl(Locator locator);

Methods:

int     getLineNumber();
int     getColumnNumber();
String  getSystemId();
String  getPublicId();

void    setColumnNumber(int columnNumber);
void    setLineNumber(int lineNumber);
void    setPublicId(String publicId);
void    setSystemId(String systemId);

Parser factory

The ParserFactory helper class has the following constructors. It is used to dynamically create a parser at run-time, so that application can be coded to cope with any compliant SAX parser without recompiling.

Constructors:

static Parser makeParser();
                  throws ClassNotFoundException,
                         IllegalAccessException,
                         InstantiationException,
                         NullPointerException,
                         ClassCastException;

static Parser makeParser(String className)
                  throws ClassNotFoundException,
                         IllegalAccessException,
                         InstantiationException,
                         ClassCastException;

There are no other methods.

The constructor with no parameter is used when the 'org.xml.sax.parser' system property holds the name of the parser.

Consider the following example:

import java.io.IOException;
import org.xml.sax.*;
import org.xml.sax.helpers.ParserFactory;
...
String pClass = "org.apache.xerces.parsers.SAXParser";

// CREATE PARSER

try
{
  Parser mySAXParser = ParserFactory.makeParser(pClass);
}
catch (SAXException err) { ... }
catch (NullPointerException err) { ... }
catch (ClassCastException err) { ... }

// REGISTER HANDLERS

mySAXParser.setDocumentHandler( this );
mySAXParser.setErrorHandler( this );
mySAXParser.setEntityResolver( this );

// PARSE DOCUMENT

try
{
  mySAX2Parser.parse("file:///TestFile.xml");
}
catch (SAXException err) { ... }

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.146.105.137