PageRenderTime 43ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/pear/MDB2/Schema/ParserDictionary.php

https://bitbucket.org/valmy/openx
PHP | 241 lines | 163 code | 18 blank | 60 comment | 35 complexity | 6c05f9a7e17045cb1779957f6705616e MD5 | raw file
  1. <?php
  2. // +----------------------------------------------------------------------+
  3. // | PHP versions 4 and 5 |
  4. // +----------------------------------------------------------------------+
  5. // | Copyright (c) 1998-2006 Manuel Lemos, Tomas V.V.Cox, |
  6. // | Stig. S. Bakken, Lukas Smith |
  7. // | All rights reserved. |
  8. // +----------------------------------------------------------------------+
  9. // | MDB2 is a merge of PEAR DB and Metabases that provides a unified DB |
  10. // | API as well as database abstraction for PHP applications. |
  11. // | This LICENSE is in the BSD license style. |
  12. // | |
  13. // | Redistribution and use in source and binary forms, with or without |
  14. // | modification, are permitted provided that the following conditions |
  15. // | are met: |
  16. // | |
  17. // | Redistributions of source code must retain the above copyright |
  18. // | notice, this list of conditions and the following disclaimer. |
  19. // | |
  20. // | Redistributions in binary form must reproduce the above copyright |
  21. // | notice, this list of conditions and the following disclaimer in the |
  22. // | documentation and/or other materials provided with the distribution. |
  23. // | |
  24. // | Neither the name of Manuel Lemos, Tomas V.V.Cox, Stig. S. Bakken, |
  25. // | Lukas Smith nor the names of his contributors may be used to endorse |
  26. // | or promote products derived from this software without specific prior|
  27. // | written permission. |
  28. // | |
  29. // | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
  30. // | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
  31. // | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
  32. // | FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
  33. // | REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
  34. // | INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
  35. // | BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS|
  36. // | OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED |
  37. // | AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
  38. // | LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY|
  39. // | WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
  40. // | POSSIBILITY OF SUCH DAMAGE. |
  41. // +----------------------------------------------------------------------+
  42. // | Author: Christian Dickmann <dickmann@php.net> |
  43. // +----------------------------------------------------------------------+
  44. //
  45. // $Id$
  46. //
  47. require_once 'XML/Parser.php';
  48. require_once 'MDB2/Schema/Validate.php';
  49. if (empty($GLOBALS['_MDB2_Schema_Reserved'])) {
  50. $GLOBALS['_MDB2_Schema_Reserved'] = array();
  51. }
  52. /**
  53. * Parses an XML schema file
  54. *
  55. * @package MDB2_Schema
  56. * @category Database
  57. * @access protected
  58. * @author Christian Dickmann <dickmann@php.net>
  59. */
  60. class MDB2_Dictionary_Parser extends XML_Parser
  61. {
  62. var $dictionary_definition = array();
  63. var $elements = array();
  64. var $element = '';
  65. var $count = 0;
  66. var $field = array();
  67. var $field_name = '';
  68. var $var_mode = false;
  69. var $variables = array();
  70. var $error;
  71. var $val;
  72. function __construct($variables, $fail_on_invalid_names = true, $structure = false, $valid_types = array(), $force_defaults = true)
  73. {
  74. // force ISO-8859-1 due to different defaults for PHP4 and PHP5
  75. // todo: this probably needs to be investigated some more andcleaned up
  76. parent::XML_Parser('ISO-8859-1');
  77. $this->variables = $variables;
  78. $this->structure = $structure;
  79. $this->val =& new MDB2_Schema_Validate($fail_on_invalid_names, $valid_types, $force_defaults);
  80. }
  81. function MDB2_Dictionary_Parser($variables, $fail_on_invalid_names = true, $structure = false, $valid_types = array(), $force_defaults = true)
  82. {
  83. $this->__construct($variables, $fail_on_invalid_names, $structure, $valid_types, $force_defaults);
  84. }
  85. function startHandler($xp, $element, $attribs)
  86. {
  87. if (strtolower($element) == 'variable') {
  88. $this->var_mode = true;
  89. return;
  90. }
  91. $this->elements[$this->count++] = strtolower($element);
  92. $this->element = implode('-', $this->elements);
  93. switch ($this->element) {
  94. case 'dictionary-field':
  95. $this->field_name = '';
  96. $this->field = array();
  97. break;
  98. }
  99. }
  100. function endHandler($xp, $element)
  101. {
  102. if (strtolower($element) == 'variable') {
  103. $this->var_mode = false;
  104. return;
  105. }
  106. switch ($this->element) {
  107. /* Field declaration */
  108. case 'dictionary-field':
  109. // $result = $this->val->validateField($this->table['fields'], $this->field, $this->field_name);
  110. if (PEAR::isError($result)) {
  111. $this->raiseError($result->getUserinfo(), 0, $xp, $result->getCode());
  112. } else {
  113. $this->dictionary_definition[$this->field_name] = $this->field;
  114. //$this->dictionary_definition['options'][] = "<option value=\"{$this->field_name}\">{$this->field_name}</option>";
  115. //$this->dictionary_definition['list'][] = "<li value=\"{$this->field_name}s\">{$this->field_name}</li>";
  116. }
  117. break;
  118. }
  119. unset($this->elements[--$this->count]);
  120. $this->element = implode('-', $this->elements);
  121. }
  122. function &raiseError($msg = null, $xmlecode = 0, $xp = null, $ecode = MDB2_SCHEMA_ERROR_PARSE)
  123. {
  124. if (is_null($this->error)) {
  125. $error = '';
  126. if (is_resource($msg)) {
  127. $error.= 'Parser error: '.xml_error_string(xml_get_error_code($msg));
  128. $xp = $msg;
  129. } else {
  130. $error.= 'Parser error: '.$msg;
  131. if (!is_resource($xp)) {
  132. $xp = $this->parser;
  133. }
  134. }
  135. if ($error_string = xml_error_string($xmlecode)) {
  136. $error.= ' - '.$error_string;
  137. }
  138. if (is_resource($xp)) {
  139. $byte = @xml_get_current_byte_index($xp);
  140. $line = @xml_get_current_line_number($xp);
  141. $column = @xml_get_current_column_number($xp);
  142. $error.= " - Byte: $byte; Line: $line; Col: $column";
  143. }
  144. $error.= "\n";
  145. $this->error =& MDB2_Schema::raiseError($ecode, null, null, $error);
  146. }
  147. return $this->error;
  148. }
  149. function cdataHandler($xp, $data)
  150. {
  151. if ($this->var_mode == true) {
  152. if (!isset($this->variables[$data])) {
  153. $this->raiseError('variable "'.$data.'" not found', null, $xp);
  154. return;
  155. }
  156. $data = $this->variables[$data];
  157. }
  158. switch ($this->element) {
  159. /* Field declaration */
  160. case 'dictionary-field-name':
  161. if (isset($this->field_name)) {
  162. $this->field_name.= $data;
  163. } else {
  164. $this->field_name = $data;
  165. }
  166. break;
  167. case 'dictionary-field-type':
  168. if (isset($this->field['type'])) {
  169. $this->field['type'].= $data;
  170. } else {
  171. $this->field['type'] = $data;
  172. }
  173. break;
  174. case 'dictionary-field-notnull':
  175. if (isset($this->field['notnull'])) {
  176. $this->field['notnull'].= $data;
  177. } else {
  178. $this->field['notnull'] = $data;
  179. }
  180. break;
  181. case 'dictionary-field-unsigned':
  182. if (isset($this->field['unsigned'])) {
  183. $this->field['unsigned'].= $data;
  184. } else {
  185. $this->field['unsigned'] = $data;
  186. }
  187. break;
  188. case 'dictionary-field-autoincrement':
  189. if (isset($this->field['autoincrement'])) {
  190. $this->field['autoincrement'].= $data;
  191. } else {
  192. $this->field['autoincrement'] = $data;
  193. }
  194. break;
  195. case 'dictionary-field-default':
  196. if (isset($this->field['default'])) {
  197. $this->field['default'].= $data;
  198. } else {
  199. $this->field['default'] = $data;
  200. }
  201. break;
  202. case 'dictionary-field-length':
  203. if (isset($this->field['length'])) {
  204. $this->field['length'].= $data;
  205. } else {
  206. $this->field['length'] = $data;
  207. }
  208. break;
  209. case 'dictionary-field-scale':
  210. if (isset($this->field['scale'])) {
  211. $this->field['scale'].= $data;
  212. } else {
  213. $this->field['scale'] = $data;
  214. }
  215. break;
  216. }
  217. }
  218. function setData(&$array, $key, $value)
  219. {
  220. $array[(count($array)-1)][$key] = $value;
  221. }
  222. }
  223. ?>