Package com.nt.udc.util.xml
Class ContextParser
java.lang.Object
org.xml.sax.helpers.DefaultHandler
com.nt.udc.util.xml.ContextParser
- All Implemented Interfaces:
NamedContextFactory
,ContentHandler
,DTDHandler
,EntityResolver
,ErrorHandler
- Direct Known Subclasses:
HierarchySchemaParser
,NARHandler
,TemplateSchemaParser
,TemplateSchemaWriter
A basic SAX event handler for parsing XML documents using the
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:
- A registration service for the
ElementContextFactory
objects - A SAX event handler to be used by the parser
addFactory
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.
- To build object models from XML documents
- To serialize object models as XML documents
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 Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoid
addFactory
(String elementName, ElementContextFactory context) A registration method to bind an element name to anElementContextFactory
.void
characters
(char[] ch, int start, int length) For internal use only.createNewContext
(String name, Attributes attrs) For internal use only.void
endElement
(String uri, String localName, String qName) For internal use only.getContext
(String name) Used to retrieve the correctElementContext
given an XML element name.getRoot()
Used to access the rootElementContext
for this ContextParser.void
setDocumentLocator
(Locator locator) void
For internal use only.void
startElement
(String uri, String localName, String qName, Attributes attrs) For internal use only.void
write
(ElementContext ctx, XMLWriter writer) For internal use only.Methods inherited from class org.xml.sax.helpers.DefaultHandler
endDocument, endPrefixMapping, error, fatalError, ignorableWhitespace, notationDecl, processingInstruction, resolveEntity, skippedEntity, startPrefixMapping, unparsedEntityDecl, warning
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface org.xml.sax.ContentHandler
declaration
-
Constructor Details
-
ContextParser
public ContextParser()
-
-
Method Details
-
addFactory
A registration method to bind an element name to anElementContextFactory
. When astartElement
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 correctElementContext
. If no binding exists for that element name, then theUnknowContext
will be used.- Parameters:
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.
-
startDocument
public void startDocument()For internal use only.- Specified by:
startDocument
in interfaceContentHandler
- Overrides:
startDocument
in classDefaultHandler
-
setDocumentLocator
- Specified by:
setDocumentLocator
in interfaceContentHandler
- Overrides:
setDocumentLocator
in classDefaultHandler
-
startElement
public void startElement(String uri, String localName, String qName, Attributes attrs) throws SAXParseException For internal use only.- Specified by:
startElement
in interfaceContentHandler
- Overrides:
startElement
in classDefaultHandler
- Throws:
SAXParseException
-
getRoot
Used to access the rootElementContext
for this ContextParser. This will correspond to the ElementContext that is used for the document root of the XML document.- Returns:
- the root ElementContext for this
ContextParser
.
-
getContext
Used to retrieve the correctElementContext
given an XML element name. Using the given name, this method will retrieve a new ElementContext using theElementContextFactory
bound against that name. If no binding exists then theUnknownContext
will be returned.- Specified by:
getContext
in interfaceNamedContextFactory
- Parameters:
name
- The XML element name which we are retrieving the ElementContext for.- Returns:
- The ElementContext bound to the given XML element name.
- Throws:
SAXParseException
- if an execption is caused during the creation of the ElementContext.
-
createNewContext
For internal use only.- Throws:
SAXParseException
-
endElement
For internal use only.- Specified by:
endElement
in interfaceContentHandler
- Overrides:
endElement
in classDefaultHandler
-
characters
public void characters(char[] ch, int start, int length) For internal use only.- Specified by:
characters
in interfaceContentHandler
- Overrides:
characters
in classDefaultHandler
-
write
For internal use only.- Throws:
SAXException
-