|
BEA Systems, Inc. | ||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object | +--com.beasys.commerce.util.dom.SAXDocumentBuilder
A SAX DocumentHandler that can generate a DOM Document for any SAX-compliant parser.
This will generate DOM implementation objects from the com.beasys.commerce.util.dom package, based off of the com.beasys.commerce.util.dom.DocumentImpl object.
Like all SAX handlers, this was not designed to be reusable, although it might accidently be (via the reset() method). It's also not intentionally thread-safe/reentrant; once you put this is on a parser and call parse(), don't do anything with it until that parse finishes.
Since SAX only deals with elements, attributes, processing instructions, and text, that's all that will be in the DOM Document generated. According to SAX, the parser must fully expanded entities, so there should be no EntityReferences. Since entities are expanded, there's no need in SAX for separating Text from CDATASections. Comments are not handed to SAX document handlers, so those are lost. A DocumentType for the Document will be generated when and if either a Notation, Entity, or Element is found; however, I haven't seen a SAX parser that passes Entities to the DTDHandler, so those might be lost. Plus, since the DOCTYPE isn't passed to the DocumentHandler, I can only assume that the DTD type of the DocType will be the tag name of the first element parsed.
To use this to construct a DOM Document tree, it would look like:
// get a SAX parser from somewhere Parser p = ...; // instantiate a document builder SAXDocumentBuilder builder = new SAXDocumentBuilder(); // install it on the parser builder.installOn(p); // parse the document p.parse(...); // get the Document tree Document doc = builder.getDocument();
DocumentImpl
,
Document
Field Summary | |
protected DocumentImpl |
doc
Our document object. |
protected org.xml.sax.Locator |
docLocator
The document-event locator object supplied by the parser. |
protected java.util.Stack |
docStack
Our document stack. |
protected DocumentTypeImpl |
docType
Our document type object for our document. |
protected boolean |
maintainWS
Do we maintain ignorable whitespace in the document. |
protected boolean |
strictParsing
Do we enforce strict parsing?. |
Constructor Summary | |
SAXDocumentBuilder()
Constructor that maintains whitespace and enforce strict parsing. |
|
SAXDocumentBuilder(boolean maintainWS,
boolean strictParsing)
Constructor. |
Method Summary | |
protected void |
appendToTOS(org.w3c.dom.Node node)
Add the given node to the TOS. |
protected void |
assignAttributeList(org.w3c.dom.Element e,
org.xml.sax.AttributeList list)
Assign the attributes in a SAX AttributeList to a DOM Element. |
void |
characters(char[] ch,
int start,
int length)
The parser found character data. |
protected org.xml.sax.SAXException |
createSAXException(java.lang.String message,
java.lang.Exception ex)
Generate a SAXException. |
void |
endDocument()
The end of a document parse. |
void |
endElement(java.lang.String name)
The end of an element. |
org.w3c.dom.Document |
getDocument()
Get the current Document object. |
protected DocumentImpl |
getDocumentImpl()
Get the document as a DocumentImpl. |
org.xml.sax.Locator |
getDocumentLocator()
Get the document-event locator we're currently using. |
protected DocumentTypeImpl |
getDocumentType()
Get the DocumentTypeImpl of the current Document. |
protected org.w3c.dom.Node |
getTOS()
Get the TOS as a Node. |
void |
ignorableWhitespace(char[] ch,
int start,
int length)
The parser found ignorable whitespace characters. |
void |
installOn(org.xml.sax.Parser p)
Install this builder on a SAX Parser. |
boolean |
maintainsWhitespace()
Tell if this maintains whitespace. |
void |
notationDecl(java.lang.String name,
java.lang.String publicId,
java.lang.String systemId)
The parser found a notation declaration. |
void |
processingInstruction(java.lang.String target,
java.lang.String data)
The parser found a processing instruction. |
void |
reset()
Reset the handler to a state where it can be used again. |
protected void |
resetDocumentStack()
Reset the document stack. |
void |
setDocumentLocator(org.xml.sax.Locator locator)
The parser will call this to set the document-event locator for the parse. |
void |
setMaintainsWhitespace(boolean maintainsWS)
Set if this should maintain whitespace. |
void |
setStrictParsing(boolean strictParsing)
Set if this should use strict parsing. |
void |
startDocument()
The start of a document parse. |
void |
startElement(java.lang.String name,
org.xml.sax.AttributeList attrs)
The start of an element. |
void |
unparsedEntityDecl(java.lang.String name,
java.lang.String publicId,
java.lang.String systemId,
java.lang.String notationName)
The parse found an unparsed entity declaration. |
boolean |
usesStrictParsing()
Tell if this enforces strict parsing. |
Methods inherited from class java.lang.Object |
clone,
equals,
finalize,
getClass,
hashCode,
notify,
notifyAll,
toString,
wait,
wait,
wait |
Field Detail |
protected org.xml.sax.Locator docLocator
protected java.util.Stack docStack
protected DocumentImpl doc
protected DocumentTypeImpl docType
protected boolean maintainWS
protected boolean strictParsing
This implies that:
Constructor Detail |
public SAXDocumentBuilder(boolean maintainWS, boolean strictParsing)
maintainWS
- true to maintain ignorable whitespace.strictParsing
- true to do strict parsing
.public SAXDocumentBuilder()
Method Detail |
public boolean maintainsWhitespace()
public void setMaintainsWhitespace(boolean maintainsWS)
public boolean usesStrictParsing()
public void setStrictParsing(boolean strictParsing)
public void reset()
This resets the document stack and clears the last generated document.
resetDocumentStack()
public void installOn(org.xml.sax.Parser p)
This is a convience method that sets the parser's document handler and dtd handler to this.
protected void resetDocumentStack()
This will also lazily initialize the document stack.
protected org.w3c.dom.Node getTOS() throws org.xml.sax.SAXException
protected void appendToTOS(org.w3c.dom.Node node) throws org.xml.sax.SAXException
protected org.xml.sax.SAXException createSAXException(java.lang.String message, java.lang.Exception ex)
If the locator was set, then this will generate a SAXParseException.
protected void assignAttributeList(org.w3c.dom.Element e, org.xml.sax.AttributeList list) throws org.xml.sax.SAXException
e
- the DOM Element.list
- the SAX AttributeList.public org.xml.sax.Locator getDocumentLocator()
public void setDocumentLocator(org.xml.sax.Locator locator)
public org.w3c.dom.Document getDocument()
protected DocumentImpl getDocumentImpl() throws org.xml.sax.SAXException
protected DocumentTypeImpl getDocumentType() throws org.xml.sax.SAXException
If the doucment type hasn't been created yet, it will be here.
getDocumentImpl()
public void startDocument() throws org.xml.sax.SAXException
This should reset everything, generate a Document for us to use, and push that Document onto the stack.
reset()
public void endDocument() throws org.xml.sax.SAXException
This should just clear the document stack.
public void characters(char[] ch, int start, int length) throws org.xml.sax.SAXException
This should generate a Text and add it to the current TOS.
getDocumentImpl()
,
appendToTOS(org.w3c.dom.Node)
public void ignorableWhitespace(char[] ch, int start, int length) throws org.xml.sax.SAXException
characters(char[], int, int)
public void processingInstruction(java.lang.String target, java.lang.String data) throws org.xml.sax.SAXException
This should generate a ProcessingInstruction and add it to the current TOS.
getDocumentImpl()
,
appendToTOS(org.w3c.dom.Node)
public void startElement(java.lang.String name, org.xml.sax.AttributeList attrs) throws org.xml.sax.SAXException
This should generate an Element, add it to the current TOS, the push it onto the stack. If this is the first element in the document (i.e. docStack.size() == 1), then also set the document's docType's DTD name to the tagName.
getDocumentImpl()
,
appendToTOS(org.w3c.dom.Node)
,
getDocumentType()
public void endElement(java.lang.String name) throws org.xml.sax.SAXException
This should pop the TOS.
public void notationDecl(java.lang.String name, java.lang.String publicId, java.lang.String systemId) throws org.xml.sax.SAXException
This should create a Notation and add it to the document type.
getDocumentType()
public void unparsedEntityDecl(java.lang.String name, java.lang.String publicId, java.lang.String systemId, java.lang.String notationName) throws org.xml.sax.SAXException
This should create an Entity and add it to the document's docType. However, I'm not sure where the children of the entity will come from.
getDocumentType()
|
BEA Systems, Inc. | ||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |