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
ElementContextFactoryobjects - 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 TypeMethodDescriptionvoidaddFactory(String elementName, ElementContextFactory context) A registration method to bind an element name to anElementContextFactory.voidcharacters(char[] ch, int start, int length) For internal use only.createNewContext(String name, Attributes attrs) For internal use only.voidendElement(String uri, String localName, String qName) For internal use only.getContext(String name) Used to retrieve the correctElementContextgiven an XML element name.getRoot()Used to access the rootElementContextfor this ContextParser.voidsetDocumentLocator(Locator locator) voidFor internal use only.voidstartElement(String uri, String localName, String qName, Attributes attrs) For internal use only.voidwrite(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, warningMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods 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 astartElementis 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 theUnknowContextwill 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:
startDocumentin interfaceContentHandler- Overrides:
startDocumentin classDefaultHandler
-
setDocumentLocator
- Specified by:
setDocumentLocatorin interfaceContentHandler- Overrides:
setDocumentLocatorin classDefaultHandler
-
startElement
public void startElement(String uri, String localName, String qName, Attributes attrs) throws SAXParseException For internal use only.- Specified by:
startElementin interfaceContentHandler- Overrides:
startElementin classDefaultHandler- Throws:
SAXParseException
-
getRoot
Used to access the rootElementContextfor 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 correctElementContextgiven an XML element name. Using the given name, this method will retrieve a new ElementContext using theElementContextFactorybound against that name. If no binding exists then theUnknownContextwill be returned.- Specified by:
getContextin 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:
endElementin interfaceContentHandler- Overrides:
endElementin classDefaultHandler
-
characters
public void characters(char[] ch, int start, int length) For internal use only.- Specified by:
charactersin interfaceContentHandler- Overrides:
charactersin classDefaultHandler
-
write
For internal use only.- Throws:
SAXException
-