public class ContextParser extends org.xml.sax.helpers.DefaultHandler implements NamedContextFactory
com.nt.udc.util.xml
framework.
The class is responsible for handling the SAX events from the
parser and passing the necessary information to the appropriate
ElementContext
object.
There are to services it provides in doing this:
ElementContextFactory
objectsaddFactory
method. The ContextParser is typically extended to provide a single location
where all ElementContext objects can be registered.
public class ExportHandler extends ContextParser { public ExportHandler() { addFactory("export", ExportContext.getFactory()); addFactory("export", ExportContext.getFactory()); addFactory("systemModel", SystemModelContext.getFactory()); addFactory("nodeManager", NodeManagerContext.getFactory()); ... } }This sub-class can then be used with your favorite SAX2 parser. There are two ways in which this may be used.
startElement
event from the parser it creates the ElementContext using the
ElementContextFactory that has been registered for that element name.
If no factory has been registered then the
UnknowContext
is used. It is
left to the parent application to decide how to handle the UnknownContext.
Subsequent SAX events are used to populate the necessary information in
the ElementContext object. When the
endElement
event is called,
the ContextParser will add the current context to the parent. It is at this
point that the ElementContext becomes responsible for associating any created
Objects with the rest of the object model.
Constructor and Description |
---|
ContextParser() |
Modifier and Type | Method and Description |
---|---|
void |
addFactory(java.lang.String elementName,
ElementContextFactory context)
A registration method to bind an element name to an
ElementContextFactory . |
void |
characters(char[] ch,
int start,
int length)
For internal use only.
|
ElementContext |
createNewContext(java.lang.String name,
org.xml.sax.Attributes attrs)
For internal use only.
|
void |
endElement(java.lang.String uri,
java.lang.String localName,
java.lang.String qName)
For internal use only.
|
ElementContext |
getContext(java.lang.String name)
Used to retrieve the correct
ElementContext given an XML
element name. |
ElementContext |
getRoot()
Used to access the root
ElementContext for this
ContextParser. |
void |
setDocumentLocator(org.xml.sax.Locator locator) |
void |
startDocument()
For internal use only.
|
void |
startElement(java.lang.String uri,
java.lang.String localName,
java.lang.String qName,
org.xml.sax.Attributes attrs)
For internal use only.
|
void |
write(ElementContext ctx,
XMLWriter writer)
For internal use only.
|
public void addFactory(java.lang.String elementName, ElementContextFactory context)
ElementContextFactory
.
When a startElement
is
recieved from the SAX parser, the element name is used to look-up the
appropriate ElementContextFactory which is in turn used to create the
correct ElementContext
.
If no binding exists for that element name, then
the UnknowContext
will be used.elementName
- The name of the XML element that the
ElementContextFactory will be bound to. This string must conform to the
rules for XML element names.context
- The ElementContextFactory that creates ElementContext
objects for this XML element name.public void startDocument()
startDocument
in interface org.xml.sax.ContentHandler
startDocument
in class org.xml.sax.helpers.DefaultHandler
public void setDocumentLocator(org.xml.sax.Locator locator)
setDocumentLocator
in interface org.xml.sax.ContentHandler
setDocumentLocator
in class org.xml.sax.helpers.DefaultHandler
public void startElement(java.lang.String uri, java.lang.String localName, java.lang.String qName, org.xml.sax.Attributes attrs) throws org.xml.sax.SAXParseException
startElement
in interface org.xml.sax.ContentHandler
startElement
in class org.xml.sax.helpers.DefaultHandler
org.xml.sax.SAXParseException
public ElementContext getRoot()
ElementContext
for this
ContextParser. This will correspond to the ElementContext that is used
for the document root of the XML document.ContextParser
.public ElementContext getContext(java.lang.String name) throws org.xml.sax.SAXParseException
ElementContext
given an XML
element name.
Using the given name, this method will retrieve a new ElementContext using
the ElementContextFactory
bound against that name. If no binding exists then the
UnknownContext
will be
returned.getContext
in interface NamedContextFactory
name
- The XML element name which we are retrieving the
ElementContext for.org.xml.sax.SAXParseException
- if an execption is caused during the creation
of the ElementContext.public ElementContext createNewContext(java.lang.String name, org.xml.sax.Attributes attrs) throws org.xml.sax.SAXParseException
org.xml.sax.SAXParseException
public void endElement(java.lang.String uri, java.lang.String localName, java.lang.String qName)
endElement
in interface org.xml.sax.ContentHandler
endElement
in class org.xml.sax.helpers.DefaultHandler
public void characters(char[] ch, int start, int length)
characters
in interface org.xml.sax.ContentHandler
characters
in class org.xml.sax.helpers.DefaultHandler
public void write(ElementContext ctx, XMLWriter writer) throws org.xml.sax.SAXException
org.xml.sax.SAXException