PageRenderTime 41ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/libraries/domit/xml_domit_shared.php

https://bitbucket.org/asosso/joomla15
PHP | 267 lines | 119 code | 29 blank | 119 comment | 21 complexity | 86b5c891991d3c9866daaa423950e332 MD5 | raw file
Possible License(s): LGPL-2.1, Apache-2.0
  1. <?php
  2. /**
  3. * @package domit-xmlparser
  4. * @version 1.01
  5. * @copyright (C) 2004 John Heinstein. All rights reserved
  6. * @author John Heinstein <johnkarl@nbnet.nb.ca>
  7. * @link http://www.engageinteractive.com/domit/ DOMIT! Home Page
  8. * DOMIT! is Free Software
  9. **/
  10. if (!defined('DOMIT_INCLUDE_PATH')) {
  11. /* Path to DOMIT! files */
  12. define('DOMIT_INCLUDE_PATH', (dirname(__FILE__) . "/"));
  13. }
  14. //Nodes
  15. /** DOM Element nodeType */
  16. define('DOMIT_ELEMENT_NODE', 1);
  17. /** DOM Attr nodeType */
  18. define('DOMIT_ATTRIBUTE_NODE', 2);
  19. /** DOM Text nodeType */
  20. define('DOMIT_TEXT_NODE', 3);
  21. /** DOM CDATA Section nodeType */
  22. define('DOMIT_CDATA_SECTION_NODE', 4);
  23. /** DOM Entity Reference nodeType */
  24. define('DOMIT_ENTITY_REFERENCE_NODE', 5);
  25. /** DOM Entity nodeType */
  26. define('DOMIT_ENTITY_NODE', 6);
  27. /** DOM Processing Instruction nodeType */
  28. define('DOMIT_PROCESSING_INSTRUCTION_NODE', 7);
  29. /** DOM Comment nodeType */
  30. define('DOMIT_COMMENT_NODE', 8);
  31. /** DOM Document nodeType */
  32. define('DOMIT_DOCUMENT_NODE', 9);
  33. /** DOM DocType nodeType */
  34. define('DOMIT_DOCUMENT_TYPE_NODE', 10);
  35. /** DOM Document Fragment nodeType */
  36. define('DOMIT_DOCUMENT_FRAGMENT_NODE', 11);
  37. /** DOM Notation nodeType */
  38. define('DOMIT_NOTATION_NODE', 12);
  39. //DOM Level 1 Exceptions
  40. /** DOM error: array index out of bounds */
  41. define('DOMIT_INDEX_SIZE_ERR', 1);
  42. /** DOM error: text doesn't fit into a DOMString */
  43. define('DOMIT_DOMSTRING_SIZE_ERR', 2);
  44. /** DOM error: node can't be inserted at this location */
  45. define('DOMIT_HIERARCHY_REQUEST_ERR', 3);
  46. /** DOM error: node not a child of target document */
  47. define('DOMIT_WRONG_DOCUMENT_ERR', 4);
  48. /** DOM error: invalid character specified */
  49. define('DOMIT_INVALID_CHARACTER_ERR', 5);
  50. /** DOM error: data can't be added to current node */
  51. define('DOMIT_NO_DATA_ALLOWED_ERR', 6);
  52. /** DOM error: node is read-only */
  53. define('DOMIT_NO_MODIFICATION_ALLOWED_ERR', 7);
  54. /** DOM error: node can't be found in specified context */
  55. define('DOMIT_NOT_FOUND_ERR', 8);
  56. /** DOM error: operation not supported by current implementation */
  57. define('DOMIT_NOT_SUPPORTED_ERR', 9);
  58. /** DOM error: attribute currently in use elsewhere */
  59. define('DOMIT_INUSE_ATTRIBUTE_ERR', 10);
  60. //DOM Level 2 Exceptions
  61. /** DOM error: attempt made to use an object that is no longer usable */
  62. define('DOMIT_INVALID_STATE_ERR', 11);
  63. /** DOM error: invalid or illegal string specified */
  64. define('DOMIT_SYNTAX_ERR', 12);
  65. /** DOM error: can't modify underlying type of node */
  66. define('DOMIT_INVALID_MODIFICATION_ERR', 13);
  67. /** DOM error: attempt to change node in a way incompatible with namespaces */
  68. define('DOMIT_NAMESPACE_ERR', 14);
  69. /** DOM error: operation unsupported by underlying object */
  70. define('DOMIT_INVALID_ACCESS_ERR', 15);
  71. //DOMIT! Exceptions
  72. /** DOM error: attempt to instantiate abstract class */
  73. define('DOMIT_ABSTRACT_CLASS_INSTANTIATION_ERR', 100);
  74. /** DOM error: attempt to call abstract method */
  75. define('DOMIT_ABSTRACT_METHOD_INVOCATION_ERR', 101);
  76. /** DOM error: can't perform this action on or with Document Fragment */
  77. define('DOMIT_DOCUMENT_FRAGMENT_ERR', 102);
  78. //DOMIT! Error Modes
  79. /** continue on error */
  80. define('DOMIT_ONERROR_CONTINUE', 1);
  81. /** die on error */
  82. define('DOMIT_ONERROR_DIE', 2);
  83. /**
  84. *@global Object Instance of the UIDGenerator class
  85. */
  86. $GLOBALS['uidFactory'] = new UIDGenerator();
  87. require_once(DOMIT_INCLUDE_PATH . 'xml_domit_nodemaps.php');
  88. /**
  89. * Generates unique ids for each node
  90. *
  91. * @package domit-xmlparser
  92. * @author John Heinstein <johnkarl@nbnet.nb.ca>
  93. */
  94. class UIDGenerator {
  95. /** @var int A seed value for generating uids */
  96. var $seed;
  97. /** @var int A tally of the number of uids generated */
  98. var $counter = 0;
  99. /**
  100. * UIDGenerator constructor
  101. */
  102. function UIDGenerator() {
  103. $this->seed = 'node' . time();
  104. } //UIDGenerator
  105. /**
  106. * Generates a unique id
  107. * @return uid
  108. */
  109. function generateUID() {
  110. return ($this->seed . $this->counter++);
  111. } //generateUID
  112. } //UIDGenerator
  113. /**
  114. * @global object Reference to custom error handler for DOMException class
  115. */
  116. $GLOBALS['DOMIT_DOMException_errorHandler'] = null;
  117. /**
  118. * @global int Error mode; specifies whether to die on error or simply return
  119. */
  120. $GLOBALS['DOMIT_DOMException_mode'] = DOMIT_ONERROR_CONTINUE;
  121. /**
  122. * @global string Log file for errors
  123. */
  124. $GLOBALS['DOMIT_DOMException_log'] = null;
  125. /**
  126. * A DOMIT! exception handling class
  127. *
  128. * @package domit-xmlparser
  129. * @author John Heinstein <johnkarl@nbnet.nb.ca>
  130. */
  131. class DOMIT_DOMException {
  132. /**
  133. * Raises the specified exception
  134. * @param int The error number
  135. * @param string A string explanation of the error
  136. */
  137. function raiseException($errorNum, $errorString) {
  138. if ($GLOBALS['DOMIT_DOMException_errorHandler'] != null) {
  139. call_user_func($GLOBALS['DOMIT_DOMException_errorHandler'], $errorNum, $errorString);
  140. }
  141. else {
  142. $errorMessageText = $errorNum . ' ' . $errorString;
  143. $errorMessage = 'Error: ' . $errorMessageText;
  144. if ((!isset($GLOBALS['DOMIT_ERROR_FORMATTING_HTML'])) ||
  145. ($GLOBALS['DOMIT_ERROR_FORMATTING_HTML'] == true)) {
  146. $errorMessage = "<p><pre>" . $errorMessage . "</pre></p>";
  147. }
  148. //log error to file
  149. if ((isset($GLOBALS['DOMIT_DOMException_log'])) &&
  150. ($GLOBALS['DOMIT_DOMException_log'] != null)) {
  151. require_once(DOMIT_INCLUDE_PATH . 'php_file_utilities.php');
  152. $logItem = "\n" . date('Y-m-d H:i:s') . 'DOMIT! Error ' . $errorMessageText;
  153. php_file_utilities::putDataToFile($GLOBALS['DOMIT_DOMException_log'],
  154. $logItem, 'a');
  155. }
  156. switch ($GLOBALS['DOMIT_DOMException_mode']) {
  157. case DOMIT_ONERROR_CONTINUE:
  158. return;
  159. break;
  160. case DOMIT_ONERROR_DIE:
  161. die($errorMessage);
  162. break;
  163. }
  164. }
  165. } //raiseException
  166. /**
  167. * custom handler for DOM errors
  168. * @param object A reference to the custom error handler
  169. */
  170. function setErrorHandler($method) {
  171. $GLOBALS['DOMIT_DOMException_errorHandler'] =& $method;
  172. } //setErrorHandler
  173. /**
  174. * Set error mode
  175. * @param int The DOM error mode
  176. */
  177. function setErrorMode($mode) {
  178. $GLOBALS['DOMIT_DOMException_mode'] = $mode;
  179. } //setErrorMode
  180. /**
  181. * Set error mode
  182. * @param boolean True if errors should be logged
  183. * @param string Absolute or relative path to log file
  184. */
  185. function setErrorLog($doLogErrors, $logfile) {
  186. if ($doLogErrors) {
  187. $GLOBALS['DOMIT_DOMException_log'] = $logfile;
  188. }
  189. else {
  190. $GLOBALS['DOMIT_DOMException_log'] = null;
  191. }
  192. } //setErrorLog
  193. } //DOMIT_DOMException
  194. /**
  195. * A class representing the DOM Implementation node
  196. *
  197. * @package domit-xmlparser
  198. * @author John Heinstein <johnkarl@nbnet.nb.ca>
  199. */
  200. class DOMIT_DOMImplementation {
  201. function hasFeature($feature, $version = null) {
  202. if (strtoupper($feature) == 'XML') {
  203. if (($version == '1.0') || ($version == '2.0') || ($version == null)) {
  204. return true;
  205. }
  206. }
  207. return false;
  208. } //hasFeature
  209. /**
  210. * Creates a new DOMIT_Document node and appends a documentElement with the specified info
  211. * @param string The namespaceURI of the documentElement
  212. * @param string The $qualifiedName of the documentElement
  213. * @param Object A document type node
  214. * @return Object The new document fragment node
  215. */
  216. function &createDocument($namespaceURI, $qualifiedName, &$docType) {
  217. $xmldoc = new DOMIT_Document();
  218. $documentElement =& $xmldoc->createElementNS($namespaceURI, $qualifiedName);
  219. $xmldoc->setDocumentElement($documentElement);
  220. if ($docType != null) {
  221. $xmldoc->doctype =& $docType;
  222. }
  223. return $xmldoc;
  224. } //createDocument
  225. /**
  226. * Creates a new DOMIT_DocumentType node (not yet implemented!)
  227. * @param string The $qualifiedName
  228. * @param string The $publicID
  229. * @param string The $systemID
  230. * @return Object The new document type node
  231. */
  232. function &createDocumentType($qualifiedName, $publicID, $systemID) {
  233. //not yet implemented
  234. DOMIT_DOMException::raiseException(DOMIT_NOT_SUPPORTED_ERROR,
  235. ('Method createDocumentType is not yet implemented.'));
  236. } //createDocumentType
  237. } //DOMIT_DOMImplementation
  238. ?>