3 # FILE: SPT--XMLParser.php
8 # SomeMethod($SomeParameter, $AnotherParameter)
9 # - short description of method
11 # AUTHOR: Edward Almasy
13 # Part of the Scout Portal Toolkit
14 # Copyright 2005 Internet Scout Project
15 # http://scout.wisc.edu
20 # ---- PUBLIC INTERFACE --------------------------------------------------
25 # set default debug output level
26 $this->DebugLevel = 0;
28 # create XML parser and tell it about our methods
29 $this->Parser = xml_parser_create($Encoding);
30 xml_set_object($this->Parser, $this);
31 xml_set_element_handler($this->Parser,
"OpenTag",
"CloseTag");
32 xml_set_character_data_handler($this->Parser,
"ReceiveData");
34 # initialize tag storage arrays
35 $this->TagNames = array();
36 $this->TagAttribs = array();
37 $this->TagData = array();
38 $this->TagParents = array();
40 # initialize indexes for parsing and retrieving data
41 $this->CurrentParseIndex = -1;
42 $this->CurrentSeekIndex = -1;
43 $this->NameKeyCache = array();
46 # parse text stream and store result
49 # pass text to PHP XML parser
50 xml_parse($this->Parser, $Text, $LastTextToParse);
53 # move current tag pointer to specified item (returns NULL on failure)
54 function SeekTo()
# (args may be tag names or indexes)
56 # perform seek based on arguments passed by caller
57 $SeekResult = $this->
PerformSeek(func_get_args(), TRUE);
60 if ($SeekResult !== NULL)
62 # retrieve item count at seek location
63 $ItemCount = count($this->CurrentItemList);
67 # return null value to indicate that seek failed
71 # return count of tags found at requested location
72 if ($this->DebugLevel > 0)
74 print(
"XMLParser->SeekTo(");
77 foreach (func_get_args() as $Arg)
79 $DbugArgList .= $Sep.
"\"".$Arg.
"\"";
82 print($DbugArgList.
") returned ".intval($ItemCount).
" items starting at index ".$this->CurrentSeekIndex.
"\n");
87 # move seek pointer up one level (returns tag name or NULL if no parent)
90 # if we are not at the root of the tree
91 if ($this->CurrentSeekIndex >= 0)
93 # move up one level in tree
97 unset($this->CurrentItemList);
99 # return name of new tag to caller
104 # return NULL indicating that no parent was found
108 # return result to caller
109 if ($this->DebugLevel > 0) { print(
"XMLParser->SeekToParent() returned ".$Result.
"<br>\n"); }
113 # move seek pointer to first child of current tag (returns tag name or NULL if no children)
116 # look for tags with current tag as parent
117 $ChildTags = array_keys($this->TagParents, $this->CurrentSeekIndex);
119 # if child tag was found with requested index
120 if (isset($ChildTags[$ChildIndex]))
122 # set current seek index to child
123 $this->CurrentSeekIndex = $ChildTags[$ChildIndex];
125 # clear item list info
126 unset($this->CurrentItemList);
128 # return name of new tag to caller
133 # return NULL indicating that no children were found
137 # return result to caller
138 if ($this->DebugLevel > 0) { print(
"XMLParser->SeekToChild() returned ".$Result.
"<br>\n"); }
142 # move seek pointer to root of tree
145 $this->CurrentSeekIndex = -1;
148 # move to next tag at current level (returns tag name or NULL if no next)
151 # get list of tags with same parent as this tag
152 $LevelTags = array_keys($this->TagParents,
153 $this->TagParents[$this->CurrentSeekIndex]);
155 # find position of next tag in list
156 $NextTagPosition = array_search($this->CurrentSeekIndex, $LevelTags) + 1;
158 # if there is a next tag
159 if (count($LevelTags) > $NextTagPosition)
161 # move seek pointer to next tag at this level
162 $this->CurrentSeekIndex = $LevelTags[$NextTagPosition];
166 # return name of tag at new position to caller
171 # return NULL to caller to indicate no next tag
176 # move to next instance of current tag (returns index or NULL if no next)
179 # set up item list if necessary
182 # if there are items left to move to
183 if ($this->CurrentItemIndex < ($this->CurrentItemCount - 1))
185 # move item pointer to next item
186 $this->CurrentItemIndex++;
188 # set current seek pointer to next item
189 $this->CurrentSeekIndex =
192 # return new item index to caller
197 # return NULL value to caller to indicate failure
201 # return result to caller
205 # move to previous instance of current tag (returns index or NULL on fail)
208 # set up item list if necessary
211 # if we are not at the first item
212 if ($this->CurrentItemIndex > 0)
214 # move item pointer to previous item
215 $this->CurrentItemIndex--;
217 # set current seek pointer to next item
218 $this->CurrentSeekIndex =
221 # return new item index to caller
226 # return NULL value to caller to indicate failure
231 # retrieve tag name from current seek point
234 if (isset($this->TagNames[$this->CurrentSeekIndex]))
244 # retrieve data from current seek point
247 # assume that we will not be able to retrieve data
250 # if arguments were supplied
253 # retrieve index for specified point
254 $Index = $this->
PerformSeek(func_get_args(), FALSE);
256 # if valid index was found
259 # retrieve data at index to be returned to caller
260 $Data = $this->TagData[$Index];
265 # if current seek index points to valid tag
266 if ($this->CurrentSeekIndex >= 0)
268 # retrieve data to be returned to caller
273 # return data to caller
274 if ($this->DebugLevel > 0)
276 print(
"XMLParser->GetData(");
277 if (func_num_args()) { $ArgString =
"";
foreach (func_get_args() as $Arg) { $ArgString .=
"\"".$Arg.
"\", "; } $ArgString = substr($ArgString, 0, strlen($ArgString) - 2); print($ArgString); }
278 print(
") returned ".($Data ?
"\"".$Data.
"\"" :
"NULL").
"<br>\n");
283 # retrieve specified attribute(s) from current seek point or specified point below
284 # (first arg is attribute name and optional subsequent args tell where to seek to)
285 # (returns NULL if no such attribute for current or specified tag)
289 $Args = func_get_args();
292 # return requested attribute to caller
293 if ($this->DebugLevel > 0) { print(
"XMLParser->GetAttribute() returned ".$Attrib.
"<br>\n"); }
299 $Args = func_get_args();
302 # return requested attribute to caller
303 if ($this->DebugLevel > 0) { print(
"XMLParser->GetAttributes() returned ".count($Attribs).
" attributes<br>\n"); }
308 # ---- PRIVATE INTERFACE -------------------------------------------------
322 # set current debug output level (0-9)
325 $this->DebugLevel = $NewLevel;
328 # callback function for handling open tags
329 function OpenTag($Parser, $ElementName, $ElementAttribs)
331 # add new tag to list
332 $NewTagIndex = count($this->TagNames);
333 $this->TagNames[$NewTagIndex] = $ElementName;
334 $this->TagAttribs[$NewTagIndex] = $ElementAttribs;
336 $this->TagData[$NewTagIndex] = NULL;
338 # set current tag to new tag
339 $this->CurrentParseIndex = $NewTagIndex;
342 # callback function for receiving data between tags
345 # add data to currently open tag
349 # callback function for handling close tags
352 # if we have an open tag and closing tag matches currently open tag
353 if (($this->CurrentParseIndex >= 0)
354 && ($ElementName == $this->TagNames[$this->CurrentParseIndex]))
356 # set current tag to parent tag
361 # perform seek to point in tag tree and update seek pointer (if requested)
364 # for each tag name or index in argument list
366 foreach ($SeekArgs as $Arg)
368 # if argument is string
371 # look for tags with given name and current tag as parent
372 $Arg = strtoupper($Arg);
373 if (!isset($this->NameKeyCache[$Arg]))
375 $this->NameKeyCache[$Arg] = array_keys($this->TagNames, $Arg);
376 $TestArray = array_keys($this->TagNames, $Arg);
378 $ChildTags = array_keys($this->TagParents, $NewSeekIndex);
379 $NewItemList = array_values(
380 array_intersect($this->NameKeyCache[$Arg], $ChildTags));
381 $NewItemCount = count($NewItemList);
383 # if matching tag found
384 if ($NewItemCount > 0)
386 # update local seek index
387 $NewSeekIndex = $NewItemList[0];
389 # save new item index
394 # report seek failure to caller
400 # look for tags with same name and same parent as current tag
401 $NameTags = array_keys($this->TagNames, $this->TagNames[$NewSeekIndex]);
402 $ChildTags = array_keys($this->TagParents, $this->TagParents[$NewSeekIndex]);
403 $NewItemList = array_values(array_intersect($NameTags, $ChildTags));
404 $NewItemCount = count($NewItemList);
406 # if enough matching tags were found to contain requested index
407 if ($NewItemCount > $Arg)
409 # update local seek index
410 $NewSeekIndex = $NewItemList[$Arg];
412 # save new item index
413 $NewItemIndex = $Arg;
417 # report seek failure to caller
423 # if caller requested that seek pointer be moved to reflect seek
424 if ($MoveSeekPointer)
427 $this->CurrentSeekIndex = $NewSeekIndex;
429 # update item index and list
430 $this->CurrentItemIndex = $NewItemIndex;
431 $this->CurrentItemList = $NewItemList;
432 $this->CurrentItemCount = $NewItemCount;
435 # return index of found seek
436 return $NewSeekIndex;
441 # assume that we will not be able to retrieve attribute
444 # retrieve attribute name and (possibly) seek arguments
447 $AttribName = strtoupper(array_shift($Args));
450 # if arguments were supplied
453 # retrieve index for specified point
456 # if valid index was found
459 # if specified attribute exists
460 if (isset($this->TagAttribs[$Index][$AttribName]))
462 # retrieve attribute(s) at index to be returned to caller
465 $ReturnVal = $this->TagAttribs[$Index];
469 $ReturnVal = $this->TagAttribs[$Index][$AttribName];
476 # if current seek index points to valid tag
477 if ($this->CurrentSeekIndex >= 0)
479 # if specified attribute exists
480 if (isset($this->TagAttribs[$this->CurrentSeekIndex][$AttribName]))
482 # retrieve attribute(s) to be returned to caller
495 # return requested attribute to caller
499 # rebuild internal list of tags with the same tag name and same parent as current
502 # get list of tags with the same parent as current tag
503 $SameParentTags = array_keys($this->TagParents,
504 $this->TagParents[$this->CurrentSeekIndex]);
506 # get list of tags with the same name as current tag
507 $SameNameTags = array_keys($this->TagNames,
508 $this->TagNames[$this->CurrentSeekIndex]);
510 # intersect lists to get tags with both same name and same parent as current
511 $this->CurrentItemList = array_values(
512 array_intersect($SameNameTags, $SameParentTags));
514 # find and save index of current tag within item list
515 $this->CurrentItemIndex = array_search(
516 $this->CurrentSeekIndex, $this->CurrentItemList);
518 # save length of item list
519 $this->CurrentItemCount = count($this->CurrentItemList);
522 # internal method for debugging
525 foreach ($this->TagNames as $Index => $Name)
527 printf(
"[%03d] %-12.12s %03d %-30.30s \n", $Index, $Name, $this->TagParents[$Index], trim($this->TagData[$Index]));