CS 635 Advanced Object-Oriented Design & Programming Spring Semester, 2002 Assignment 3 |
||
---|---|---|
© 2002, All Rights Reserved, SDSU & Roger Whitney San Diego State University -- This page last updated 12-Feb-02 |
|
Percent
of Grade
|
Working
Code
|
15% |
Unit
Tests
|
15% |
Comments |
10% |
Quality
of Code
|
30% |
Proper
implementation of Patterns
|
30% |
CS635Document ::= <CS635Document>(CS635Document* | (header [text*] )* | text*)*</CS635Document> header ::= <header>char+</header> text ::= <text>char+</text>
<!DOCTYPE CS635Document [ <!ELEMENT CS635Document ( (CS635Document* | ( header , text*)* | text* )* )> <!ELEMENT header (#PCDATA)> <!ELEMENT text (#PCDATA)> ]>
<?xml version="1.0" ?> <CS635Document> <header>This is an example</header> <text>Not much here</text> <CS635Document> <text>Just text here</text> </CS635Document> </CS635Document>
<?xml version="1.0" ?> <!DOCTYPE CS635Document [ <!ELEMENT CS635Document ( (CS635Document* | ( header , text*)* | text* )* )> <!ELEMENT header (#PCDATA)> <!ELEMENT text (#PCDATA)> ]> <CS635Document> <header>This is an example</header> <text>Not much here</text> <CS635Document> <text>Just text here</text> </CS635Document> </CS635Document>
startDocument public void startDocument() virtual void startDocument ( )
endDocument public void endDocument () virtual void endDocument ()
startElement: namespaceURI localName: localName qName: name attributes: attributes public void startElement(java.lang.String uri, java.lang.String localName, java.lang.String qName, Attributes atts) throws SAXException virtual void startElement (const XMLCh *const uri,const XMLCh *const localname,const XMLCh *const qname,const Attributes &attrs)
public void endElement(java.lang.String uri, java.lang.String localName, java.lang.String qName) throws SAXException virtual void endElement (const XMLCh *const uri,const XMLCh *const localname,const XMLCh *const qname)
characters: aString public void characters(char[] ch, int start, int length) throws SAXException virtual void characters (const XMLCh *const chars, const unsigned int length)
<!DOCTYPE Sample [ <!ELEMENT Sample ( ( a* , b*)*)> <!ELEMENT a (#PCDATA)> <!ELEMENT b (#PCDATA)> ]>
<Sample> </Sample>
<Sample> <a>hi</a> <b>mom</b> <b> how are</b> <a> you </a> </Sample>
<Sample> <a>cat</a> </Sample>
Smalltalk.CS635 defineClass: #SAXDriverExample superclass: #{XML.SAXDriver} indexedType: #none private: false instanceVariableNames: 'currentNode root ' classInstanceVariableNames: '' imports: '' category: 'Assignment-3'!
CS635.SAXDriverExample comment: 'SAXDriverExample is an example of a SAX content handler in Smalltalk Instance Variables: currentNode <TextNode> represents the tag the parser is currently parsing. root <Collection of TextNode> Contains the elements or tags in the XML document. Does not contain the top level tag. '!
!CS635.SAXDriverExample methodsFor: 'content handler'!
characters: aString currentNode add: aString!
startDocument root := OrderedCollection new!
startElement: namespaceURI localName: localName qName: name attributes: attributes localName = 'a' | (localName = 'b') ifTrue: [currentNode := TextNode new. root add: currentNode]! !
Smalltalk.CS635 defineClass: #TextNode superclass: #{Core.Object} indexedType: #none private: false instanceVariableNames: 'text ' classInstanceVariableNames: '' imports: '' category: 'Assignment-3'!
CS635.TextNode comment: 'TextNode represents the text in a tag. In this example both tags a and b just contain text. This example does not need this class. SAXDriver could use a collection in instead. However, you will have to provide classes to represent each type of tag in the XML, so I use TextNode Instance Variables: text <String> text of the XML tag'!
!CS635.TextNode methodsFor: 'accessing'!
add: aString text isNil ifTrue:[text := String new]. text := text , aString!
printOn: aStream aStream nextPut: $:; nextPutAll: text; nextPut: $:! !
|page builder exampleDispatcher | page := '<?xml version="1.0" ?> <!DOCTYPE Sample [ <!ELEMENT Sample ( ( a* , b*)*)> <!ELEMENT a (#PCDATA)> <!ELEMENT b (#PCDATA)> ]> <Sample> <a>hi</a> <b>mom</b> <b>how are</b> <a>you</a> </Sample>'. builder := SAXDriverExample new. exampleDispatcher := SAXDispatcher new contentHandler: builder. XMLParser processDocumentString: page beforeScanDo: [:parser | parser saxDriver:(exampleDispatcher); validate: true]. builder inspect.
| builder exampleDispatcher |
builder := SAXDriverExample new. exampleDispatcher := SAXDispatcher new contentHandler: builder. XMLParser processDocumentInFilename: 'page' beforeScanDo: [:parser | parser saxDriver:(exampleDispatcher); validate: true]. builder inspect.
import org.xml.sax.helpers.DefaultHandler; import javax.xml.parsers.SAXParserFactory; import javax.xml.parsers.SAXParser; import java.util.Vector; import java.io.File; public class SAXDriverExample extends DefaultHandler { private Vector root; public static void main(String argv[]) { SAXDriverExample handler = new SAXDriverExample(); // Use the default (non-validating) parser SAXParserFactory factory = SAXParserFactory.newInstance(); try { SAXParser saxParser = factory.newSAXParser(); saxParser.parse( new File("sample"), handler ); } catch (Throwable t) { t.printStackTrace(); } System.out.println( handler.root()); } public void startDocument() { root = new Vector(); } public void characters(char[] ch, int start, int length) { root.addElement( new String( ch, start, length)); } public Vector root() { return root; } }The file sample contains:
<?xml version="1.0" ?> <Sample> <a>hi</a> <b>mom</b> <b>how are</b> <a>you</a> </Sample>