[Openfirst-cvscommit] base/includes xmlModule.php,1.3,1.4
Brought to you by:
xtimg
From: Jamie <ast...@us...> - 2005-08-23 18:15:57
|
Update of /cvsroot/openfirst/base/includes In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv9535/includes Modified Files: xmlModule.php Log Message: Partial parser ready Index: xmlModule.php =================================================================== RCS file: /cvsroot/openfirst/base/includes/xmlModule.php,v retrieving revision 1.3 retrieving revision 1.4 diff -C2 -d -r1.3 -r1.4 *** xmlModule.php 18 Jul 2005 22:34:33 -0000 1.3 --- xmlModule.php 23 Aug 2005 18:15:46 -0000 1.4 *************** *** 25,29 **** // and handles installation/removal/upgrading ! require_once('db_setup.php'); class xmlModule { --- 25,29 ---- // and handles installation/removal/upgrading ! require_once('dbase.php'); class xmlModule { *************** *** 33,39 **** // XML ! /*private*/ var $mxmlCurTag, $mRootElement; // Specific tags for later. ! /*private*/ var $mIncludesTag, $mDBTag; function xmlModule($File) { --- 33,41 ---- // XML ! /*private*/ var /*$mxmlCurTag, $mRootElement,*/ $mTagStack; // Specific tags for later. ! # /*private*/ var $mIncludesTag, $mDBTag; ! // States ! /*private*/ var $mxmlCurTable, $mxmlCurField, $mxmlCurKey; function xmlModule($File) { *************** *** 44,47 **** --- 46,55 ---- return; } + $this->mxmlCurTag = $this->mRootElement = false; + $this->mTagStack = array(); + $this->mxmlCurTable = false; + $this->mxmlCurField = false; + $this->mxmlCurKey = false; + $hXML = xml_parser_create(); xml_set_object($hXML, $this); *************** *** 49,56 **** xml_set_character_data_handler($hXML, 'characterData'); ! xml_parse($hXML, $this->getModuleDTD(), false); #Add predefined entities while ($data = fread($fp, 1024)) { if (!xml_parse($hXML, $data, feof($fp))) { xml_parser_free($hXML); unset($hXML); --- 57,71 ---- xml_set_character_data_handler($hXML, 'characterData'); ! // This doesn't work ! # xml_parse($hXML, $this->getModuleDTD(), false); //Add predefined entities while ($data = fread($fp, 1024)) { if (!xml_parse($hXML, $data, feof($fp))) { + $errno = xml_get_error_code($hXML); + $error = xml_error_string($errno); + $pos = xml_get_current_byte_index($hXML); + $line = xml_get_current_line_number($hXML); + $col = xml_get_current_column_number($hXML); + trigger_error("XML parse error: ($errno) $error in $File on line $line at column $col"); xml_parser_free($hXML); unset($hXML); *************** *** 61,75 **** xml_parser_free($hXML); unset($hXML); ! ! $this->mIncludes = array(); ! // Handle includes ! if (is_object($this->mIncludesTag)) { ! $IncludeTags = $this->mIncludesTag->Contents; ! foreach($IncludeTags as $tag) { ! if (is_object($tag) && is_a($tag, 'xmlElement')) { ! $this->mIncludes[] = $tag->Contents[0]; ! } ! } ! } if (is_null($this->mMaintainer)) $this->mMaintainer = $this->mAuthor; --- 76,86 ---- xml_parser_free($hXML); unset($hXML); ! // Need to free up space ! unset($this->mxmlCurTag); ! unset($this->mxmlCurTable); ! unset($this->mxmlCurField); ! unset($this->mxmlCurKey); ! unset($this->mRootElement); ! unset($this->mTagStack); if (is_null($this->mMaintainer)) $this->mMaintainer = $this->mAuthor; *************** *** 154,193 **** */ /*public*/ function prepareTables() { ! $mTables =& $this->mTables; ! $mDBTag =& $this->mDBTag; ! ! $mTables = array(); ! foreach($mDBTag->Contents as $tag) { ! if (!(is_object($tag) && is_a($tag, 'xmlElement'))) continue; ! if ($tag->Name != 'TABLE') continue; } } - /* - /*** PRIVATE FUNCTIONS ***/ ! /*private*/ function getModuleDTD() { ! /* global $BasePath, $StylePath; ! $xmlBasePath = str_replace(array('&', '<', '>', '"', '\'' ), ! array('&', '<', '>', '"e;', '''), ! $BasePath ! ); ! $xmlStylePath = str_replace(array('&', '<', '>', '"', '\'' ), ! array('&', '<', '>', '"e;', '''), ! $StylePath ! ); ! return "<!DOCTYPE module [ ! <!ENTITY BasePath \"$xmlBasePath\"> ! <!ENTITY StylePath \"$xmlStylePath\"> ! ]>";*/ return '<!DOCTYPE module [ <!ENTITY BasePath "$BasePath"> - <!ENTITY StylePath "$StylePath"> <!ENTITY fBasePath "$fBasePath"> <!ENTITY fStylePath "$fStylePath"> ]>'; } --- 165,299 ---- */ /*public*/ function prepareTables() { ! $this->mTables = array(); ! foreach($this->mDBTag->Contents as $table) { ! if (!( is_object($table) && is_a($table, 'xmlElement') )) continue; ! if ($table->Name != 'TABLE') continue; ! $name = $table->Attributes['name']; + $this->mTables[$name] = array( 'name' => $name, + 'fields' => array(), + 'keys' => array() + ); + + foreach($table->Contents as $tag) { + switch($tag->Name) { + case 'FIELDS': + $this->mTables[$name]['fields'] = $this->parseFields($tag); + break; + + case 'KEYS': + $this->mTables[$name]['keys'] = $this->parseKeys($tag); + break; + + case 'ROWS': + $this->mTables[$name]['data'] = $this->parseData($tag); + break; + } + } } + // Free up space + $this->mDBTag = false; } /*** PRIVATE FUNCTIONS ***/ ! /*private*/ function parseFields($fieldtag) { ! $rtn = array(); ! foreach($fieldtag->Contents as $tag) { ! if ($tag->Name != 'FIELD') continue; ! $field = array(); ! $field['name'] = $tag->Attributes['name']; ! if (isset($tag->Attributes['null'])) ! $field['null'] = ofConvert2Bool($tag->Attributes['null'], array('null')); ! if (isset($tag->Attributes['autoincrement'])) ! $field['auto'] = ofConvert2Bool($tag->Attributes['autoincrement'], array('autoincrement')); ! ! foreach($tag->Contents as $partstag) { ! switch($partstag->Name) { ! case 'TYPE': ! $field['type'] = $partstag->getTextContents(); ! break; ! ! case 'SET': ! case 'ENUM': ! $field['type'] = $partstag->Name; ! //Parse contents ! $values = array(); ! foreach($partstag->Children as $value) { ! $values[] = $value->getTextContents(); ! } ! $field['values'] = $values; ! break; ! ! case 'DEFAULT': ! $field['default'] = $partstag->getTextContents(); ! break; ! } ! } ! ! $rtn[$field['name']] = $field; ! } ! return $rtn; ! } ! ! /** ! */ ! /*private*/ function parseKeys($keytag) { ! $rtn = array(); ! foreach($keytag->Contents as $tag) { ! if ($tag->Name != 'KEY') continue; ! $key = array(); ! $key['type'] = 'index'; ! if (isset($tag->Attributes['type'])) $key['type'] = $tag->Attributes['type']; ! $key['name'] = $tag->Attributes['name']; ! if ( (strcasecmp($key['type'], 'primary') == 0) || (strcasecmp($key['name'], 'primary') == 0) ) { ! $key['type'] = 'primary'; ! $key['name'] = 'PRIMARY'; ! } else if ( $key['type'] && ! strcasecmp($key['type'], 'primary') && ! strcasecmp($key['type'], 'unique') && ! strcasecmp($key['type'], 'index') ) { ! // invalid key type. Skip this tag ! continue; ! } ! ! $key['cols'] = array(); ! foreach($tag->Contents as $coltag) { ! $key['cols'][] = $coltag->getTextContents(); ! } ! $rtn[$key['name']] = $key; ! } ! return $rtn; ! } ! ! /** ! */ ! /*private*/ function parseData($datatag) { ! $rtn = array(); ! foreach($datatag->Contents as $tag) { ! if ($tag->Name != 'ROW') continue; ! $row = array(); ! foreach($tag->Contents as $coltag) { ! $row[$coltag->Attributes['name']] = $coltag->getTextContents(); ! } ! $rtn[] = $row; ! } ! ! return $rtn; ! } ! ! /*private*/ function getModuleDTD() { return '<!DOCTYPE module [ <!ENTITY BasePath "$BasePath"> <!ENTITY fBasePath "$fBasePath"> + <!ENTITY StylePath "$StylePath"> <!ENTITY fStylePath "$fStylePath"> + <!ENTITY ModPath "$ModPath"> + <!ENTITY fModPath "$fModPath"> + <!ENTITY DirName "$DirName"> + + <!ENTITY null "NULL"> ]>'; } *************** *** 195,199 **** /*** XML PARSER HOOKS ***/ function startElement($parser, $name, $attrs) { ! if (isset($this->mxmlCurTag)) { $this->mxmlCurTag =& $this->mxmlCurTag->addElement($name, $attrs); } else { --- 301,306 ---- /*** XML PARSER HOOKS ***/ function startElement($parser, $name, $attrs) { ! array_push($this->mTagStack, $name); ! if (isset($this->mxmlCurTag) && is_object($this->mxmlCurTag)) { $this->mxmlCurTag =& $this->mxmlCurTag->addElement($name, $attrs); } else { *************** *** 204,211 **** if ($name == 'MODULE') { if (isset($attrs['ID'])) $this->mID = $attrs['ID']; ! } else if ($name == 'SCRIPT') { ! if (!isset($attrs['rel']) || !isset($attrs['src'])) break; ! $type = $attrs['rel']; ! $file = $attrs['src']; if ($type == 'setup') { $this->mSetupScript = $file; --- 311,319 ---- if ($name == 'MODULE') { if (isset($attrs['ID'])) $this->mID = $attrs['ID']; ! } ! else if ($name == 'SCRIPT') { ! if (!isset($attrs['REL']) || !isset($attrs['SRC'])) break; ! $type = $attrs['REL']; ! $file = $attrs['SRC']; if ($type == 'setup') { $this->mSetupScript = $file; *************** *** 216,241 **** } } } function endElement($parser, $name) { if ($name == 'NAME') { ! $this->mName = $this->mxmlCurTag->Contents[0]; } else if ($name == 'VERSION') { ! $this->mVersion = $this->mxmlCurTag->Contents[0]; } else if ($name == 'AUTHOR') { ! $this->mAuthor = $this->mxmlCurTag->Contents[0]; } else if ($name == 'MAINTAINER') { ! $this->mMaintainer = $this->mxmlCurTag->Contents[0]; } else if ($name == 'NAVBAR') { ! $this->mNavBar = $this->mxmlCurTag->Contents[0]; } else if ($name == 'ADMINBAR') { ! $this->mAdminBar = $this->mxmlCurTag->Contents[0]; ! } else if ($name == 'INCLUDES') { ! $this->mIncludesTag =& $this->mxmlCurTag; ! } else if ($name == 'DB') { ! $this->mDBTag =& $this->mxmlCurTag; } $this->mxmlCurTag =& $this->mxmlCurTag->getParent(); } --- 324,413 ---- } } + else if ($name == 'TABLE') { + if (!isset($attrs['NAME'])) { + print_r($attrs); + trigger_error('The table tag must always have a name attribute.', E_USER_ERROR); + } + $this->mxmlCurTable = $attrs['NAME']; + $this->mTables[$this->mxmlCurTable] = + array( 'name' => $this->mxmlCurTable, + 'fields' => array(), + 'keys' => array() + ); + } + else if ($name == 'FIELD' && in_array('TABLE', $this->mTagStack)) { + $field = array(); + $this->mxmlCurField = $field['name'] = $attrs['NAME']; + if (isset($attrs['null'])) + $field['null'] = ofConvert2Bool($attrs['NULL'], array('null')); + if (isset($attrs['autoincrement'])) + $field['auto'] = ofConvert2Bool($attrs['AUTOINCREMENT'], array('autoincrement')); + + $this->mTables[$this->mxmlCurTable]['fields'][$this->mxmlCurField] = $field; + } + else if ($name == 'KEY' && $this->mxmlCurTable) { + $key = array(); + $key['type'] = 'index'; + $key['name'] = $key['type'] = false; + if (isset($attrs['TYPE'])) $key['type'] = $attrs['TYPE']; + if (isset($attrs['NAME'])) $this->mxmlCurKey = $key['name'] = $attrs['NAME']; + if ( (strcasecmp($key['type'], 'primary') == 0) || (strcasecmp($key['name'], 'primary') == 0) ) { + $key['type'] = 'primary'; + $this->mxmlCurKey = $key['name'] = 'PRIMARY'; + } + + $key['cols'] = array(); + + $this->mTables[$this->mxmlCurTable]['keys'][$this->mxmlCurKey] = $key; + } + else if ($name == 'TYPE' && $this->mxmlCurField) { + if (isset($attrs['LENGTH'])) + $this->mTables[$this->mxmlCurTable]['fields'][$this->mxmlCurField]['length'] = $attrs['LENGTH']; + if (isset($attrs['UNSIGNED'])) + $this->mTables[$this->mxmlCurTable]['fields'][$this->mxmlCurField]['unsigned'] = ofConvert2Bool($attrs['UNSIGNED']); + } + else if (($name == 'SET' || $name == 'ENUM') && in_array('FIELD', $this->mTagStack)) { + $this->mTables[$this->mxmlCurTable]['fields'][$this->mxmlCurField]['type'] = $name; + $this->mTables[$this->mxmlCurTable]['fields'][$this->mxmlCurField]['values'] = array(); + + } } function endElement($parser, $name) { + $par_name = false; + if (count($this->mTagStack) > 1) + $par_name = $this->mTagStack[count($this->mTagStack)-2]; // One for the zero start, one for second from end if ($name == 'NAME') { ! $this->mName = $this->mxmlCurTag->getTextContents(); } else if ($name == 'VERSION') { ! $this->mVersion = $this->mxmlCurTag->getTextContents(); } else if ($name == 'AUTHOR') { ! $this->mAuthor = $this->mxmlCurTag->getTextContents(); } else if ($name == 'MAINTAINER') { ! $this->mMaintainer = $this->mxmlCurTag->getTextContents(); } else if ($name == 'NAVBAR') { ! $this->mNavBar = $this->mxmlCurTag->getTextContents(); } else if ($name == 'ADMINBAR') { ! $this->mAdminBar = $this->mxmlCurTag->getTextContents(); ! } else if ($name == 'INCLUDE') { ! $this->mIncludes[] = $file = $this->mxmlCurTag->getTextContents(); ! } else if ($name == 'TABLE') { ! $this->mxmlCurTable = false; ! } else if ($name == 'FIELD') { ! $this->mxmlCurField = false; ! } else if ($name == 'KEY') { ! $this->mxmlCurKey = false; ! } else if ($name == 'TYPE' && $this->mxmlCurField) { ! $this->mTables[$this->mxmlCurTable]['fields'][$this->mxmlCurField]['type'] = $this->mxmlCurTag->getTextContents(); ! } else if ($name == 'DEFAULT' && $this->mxmlCurField) { ! $this->mTables[$this->mxmlCurTable]['fields'][$this->mxmlCurField]['default'] = $this->mxmlCurTag->getTextContents(); ! } else if ($name == 'COL' && $this->mxmlCurKey) { ! $this->mTables[$this->mxmlCurTable]['keys'][$this->mxmlCurKey]['cols'][] = $this->mxmlCurTag->getTextContents(); ! } else if ($par_name == 'SET' || $par_name == 'ENUM') { ! $this->mTables[$this->mxmlCurTable]['fields'][$this->mxmlCurField]['values'][] = $this->mxmlCurTag->getTextContents(); } $this->mxmlCurTag =& $this->mxmlCurTag->getParent(); + array_pop($this->mTagStack); } *************** *** 245,248 **** --- 417,426 ---- } + /** + * Used internally when parsing the XML document as + * a cheap DOM substitute + * + * Should not be used elsewhere. + */ class xmlElement { var $Name, $Attributes, $Contents, $parent; *************** *** 256,259 **** --- 434,440 ---- /*public*/ function addCData($Text) { + // the DB XML doesn't care about whitespace. + $Text = trim($Text); + if ($Text == '') return; //Don't bother if it's nothing. if (count($this->Contents) == 0) { $this->Contents[] = $Text; *************** *** 277,280 **** --- 458,469 ---- return $this->parent; } + + /*public*/ function getTextContents() { + $rtn = ''; + foreach($this->Contents as $child) { + if (is_string($child)) $rtn .= $child; + } + return $rtn; + } } |