View this PageEdit this Page (locked)Attachments to this PageHistory of this PageHomeRecent ChangesSearch the SwikiHelp Guide
Hotspots: Admin Pages | Turn-in Site |
Current Links: Cases Final Project Summer 2007

BwL M6

This milestone's requirements are simple: the guide has to complete 5 tasks, and it has to be able to read/write XML. The requirements page is here. The XML was completed prior to even m5, so that part was easy. The 5 tasks was a little trickier. We decided to have most of our code to handle the avatar moving in the tasks xml files. The parser files in this code in the xml files and then runs a "doIt:" command on it. This creates a lot of code for the xml task files, so we have some library code that can be re-used as needed.

YAXO is the standard XML parser for Squeak. It basically uses a tree form for storing the data. The hardest part of using it was converting the lists of data for the tasks into tree form. We chose to write separate methods to do this, included below.

XML Parser

         fileIn: aFileName
            "load instruction data in a line-by-line format from a file"

            | inputFile parsed steps | 

            [inputFile _ FileStream readOnlyFileNamed: aFileName.]
            on: FileDoesNotExistException
               do: [:exception |
                  Transcript show: '[ERROR] Unable to open file for reading! Attempted filename: ', aFileName; cr.
                  ^nil.
               ].
            (inputFile)
               ifNil: 
                  [Transcript show: ' Error opening file: invalid filename' ; cr] 
               ifNotNil: [ 

                  | tempDictionary stepElement |
		  parsed := XMLDOMParser parseDocumentFromFileNamed: aFileName.
		  taskName := (self findTag: 'Name' startElement: parsed) contents at: 1.
                  taskDescription _ (self findTag: 'Description' startElement: parsed) contents at: 1.
                  steps := (self findTag: 'Steps' startElement: parsed).
                  1 to: ((steps elements) size)-1 do: [ :index | 
                     tempDictionary := Dictionary new.
                     stepElement := self findTag: (index asString) startElement: steps.
                     tempDictionary at: 'Text' put: ((self findTag: 'Text' startElement: stepElement) contents at: 1).
                     tempDictionary at: 'Showme' put: ((self findTag: 'Showme' startElement: stepElement) contents at: 1).
                     tempDictionary at: 'Check' put: ((self findTag: 'Check' startElement: stepElement) contents at: 1).
                     taskStepList add: tempDictionary.].
                  steps := (self findTag: 'Keywords' startElement: parsed).
                  1 to: ((steps elements) size)-1 do: [ :index | 
                     taskKeywordList add: ((self findTag: (index asString) startElement: steps) contents at: 1).
                  ].
               inputFile close.
            ].

         fileOut: aFileName 
            "write the steps data out to a file"

            | outputFile |
            "Attempt to open a file for saving the data"
            outputFile _ FileStream forceNewFileNamed: aFileName.
            outputFile
               ifNil: [ "If the file is Nil (couldn't be opened)"
                  "Write an error message to the transcript window"
                  Transcript show: ' Error saving file: invalid filename' ; cr]

               ifNotNil: [ "If the file was opened successfully"
                  self createHeader.
                  xmlDocument addElement: (self xmlSteps).
                  xmlDocument addElement: (self xmlKeywords).
                  xmlDocument addElement: (self makeSubElement: 'Name' data: taskName).
                  xmlDocument addElement: (self makeSubElement: 'Description' data: taskDescription).
                  xmlDocument printCanonicalOn: outputFile.
               ].

            outputFile close.

         xmlSteps
            | anXMLElement subElement |
            anXMLElement_XMLElement named: 'Steps' attributes: Dictionary new.
            anXMLElement addElement: (self makeSubElement: 'Size' data: (taskStepList size)).
            1 to: (taskStepList size) do: [:index | 
               subElement_XMLElement named: (index asString) attributes: Dictionary new.
               subElement addElement: (self makeSubElement: 'Text' data: ((taskStepList at: index) at: 'Text')).
               subElement addElement: (self makeSubElement: 'Showme' data: ((taskStepList at: index) at: 'Showme')).
               subElement addElement: (self makeSubElement: 'Check' data: ((taskStepList at: index) at: 'Check')).
               anXMLElement addElement: subElement.
            ].
         ^anXMLElement


Link to this Page