| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* This contains the XMLDocumentClass |
| 5 |
* |
| 6 |
* $Id: XMLDocumentClass.inc,v 1.12 2002/11/22 21:51:08 hemna Exp $ |
| 7 |
* |
| 8 |
* @author Walter A. Boring IV <waboring@buildabetterweb.com> |
| 9 |
* @package phpHtmlLib |
| 10 |
* |
| 11 |
*/ |
| 12 |
|
| 13 |
|
| 14 |
/** |
| 15 |
* |
| 16 |
* This class lets you build a complete |
| 17 |
* xml document and render it. |
| 18 |
* |
| 19 |
* It automatically creates the root xml tag |
| 20 |
* that will contain all of the content. |
| 21 |
* The name of the tag is the first parameter to |
| 22 |
* the constructor. You can add/change the attributes |
| 23 |
* to the root tag via the set_root_attributes() or |
| 24 |
* the set_root_attribute() methods. |
| 25 |
* |
| 26 |
* @author Walter A. Boring IV <waboring@buildabetterweb.com> |
| 27 |
* @package phpHtmlLib |
| 28 |
*/ |
| 29 |
class XMLDocumentClass extends Container { |
| 30 |
|
| 31 |
|
| 32 |
/** |
| 33 |
* holds the xml tag object that |
| 34 |
* all content will be wrapped in |
| 35 |
* |
| 36 |
*/ |
| 37 |
var $_xml_obj = NULL; |
| 38 |
|
| 39 |
/** |
| 40 |
* character set to be used in this |
| 41 |
* page. This gets automatically |
| 42 |
* placed in the <?xml encoding="" ?> |
| 43 |
* @var string |
| 44 |
* @access private |
| 45 |
*/ |
| 46 |
var $_charset = "iso-8859-1"; |
| 47 |
|
| 48 |
/** |
| 49 |
* This flag tells if we should |
| 50 |
* output the header content type. |
| 51 |
* this is usefull for folks using |
| 52 |
* this class for output not destined |
| 53 |
* for a browser. |
| 54 |
*/ |
| 55 |
var $_show_http_header = FALSE; |
| 56 |
|
| 57 |
/** |
| 58 |
* this is a hack to show the |
| 59 |
* character encoding or not. |
| 60 |
* DEFAULT: on |
| 61 |
* |
| 62 |
* @var boolean |
| 63 |
*/ |
| 64 |
var $_show_char_encoding = TRUE; |
| 65 |
|
| 66 |
/** |
| 67 |
* Holds the url of dtd |
| 68 |
*/ |
| 69 |
var $dtd = NULL; |
| 70 |
|
| 71 |
/** |
| 72 |
* Holds the name of the root xml |
| 73 |
* element as well as the DOCTYPE |
| 74 |
* document_element attribute |
| 75 |
*/ |
| 76 |
var $root_name = NULL; |
| 77 |
|
| 78 |
/** |
| 79 |
* The root xml tag for all data |
| 80 |
* |
| 81 |
* @var XMLTagClass object |
| 82 |
*/ |
| 83 |
var $_root_tag = NULL; |
| 84 |
|
| 85 |
/** |
| 86 |
* DOCTYPEag object that sets the document |
| 87 |
* type. This gets rendered prior to <html> |
| 88 |
* @var object |
| 89 |
* @access private |
| 90 |
*/ |
| 91 |
var $_doctype = NULL; |
| 92 |
|
| 93 |
/** |
| 94 |
* the DOCTYPE source attribute |
| 95 |
* |
| 96 |
* @var string |
| 97 |
*/ |
| 98 |
var $_doctype_source = "SYSTEM"; |
| 99 |
|
| 100 |
/** |
| 101 |
* This contains the doctype links |
| 102 |
* attributes. There can be a maximum |
| 103 |
* of 2 according to the XML spec |
| 104 |
*/ |
| 105 |
var $_doctype_links = array(); |
| 106 |
|
| 107 |
/** |
| 108 |
* The http content-type |
| 109 |
* |
| 110 |
*/ |
| 111 |
var $_http_content_type = "application/xml"; |
| 112 |
|
| 113 |
/** |
| 114 |
* The container for all the |
| 115 |
* xml-stylesheet tags |
| 116 |
* |
| 117 |
* @var object |
| 118 |
*/ |
| 119 |
var $_xml_stylesheet_container = NULL; |
| 120 |
|
| 121 |
|
| 122 |
/** |
| 123 |
* The constructor |
| 124 |
* |
| 125 |
* @param string - the root xml tag's name |
| 126 |
* @param string - the link path to the DTD (if any) |
| 127 |
* @param array - the root tag's attributes |
| 128 |
* |
| 129 |
*/ |
| 130 |
function XMLDocumentClass($root_name, $dtd = NULL, $root_attributes = array()) { |
| 131 |
$this->_create_xml_obj(); |
| 132 |
$this->set_root_name($root_name); |
| 133 |
$this->set_doctype_link( $dtd ); |
| 134 |
$this->set_root_attributes( $root_attributes ); |
| 135 |
$this->_build_root_tag(); |
| 136 |
$this->_build_xml_stylesheet_container(); |
| 137 |
} |
| 138 |
|
| 139 |
/** |
| 140 |
* function that will render the XML Document. |
| 141 |
* |
| 142 |
* @param int - the indentation level for |
| 143 |
* the container. |
| 144 |
* @param int - the output debug flag to |
| 145 |
* maintain compatibility w/ the API. |
| 146 |
* |
| 147 |
* @return string the raw html output. |
| 148 |
*/ |
| 149 |
function render( $indent_level = 0) { |
| 150 |
|
| 151 |
//dump out the content type |
| 152 |
$this->dump_http_header(); |
| 153 |
|
| 154 |
//set the encoding type |
| 155 |
if ($this->_show_char_encoding) { |
| 156 |
$this->_xml_obj->set_tag_attribute("encoding", $this->get_encoding() ); |
| 157 |
} |
| 158 |
|
| 159 |
//add the xml tag |
| 160 |
$output = $this->_xml_obj->render(0); |
| 161 |
|
| 162 |
//add any stylesheet links |
| 163 |
if ($this->_xml_stylesheet_container->count_content() > 0) { |
| 164 |
$output .= $this->_xml_stylesheet_container->render(0); |
| 165 |
} |
| 166 |
|
| 167 |
//build the doctype if needed. |
| 168 |
$this->_build_doctype(); |
| 169 |
|
| 170 |
if ($this->_doctype != NULL) { |
| 171 |
$output .= $this->_doctype->render(); |
| 172 |
} |
| 173 |
|
| 174 |
//set the root tag's attributes |
| 175 |
$this->_root_tag->set_tag_attributes( $this->get_root_attributes() ); |
| 176 |
|
| 177 |
//render the root tag's content |
| 178 |
$output .= $this->_root_tag->render($indent_level, 0); |
| 179 |
return $output; |
| 180 |
} |
| 181 |
|
| 182 |
/** |
| 183 |
* we override this class to make sure |
| 184 |
* we push all content inside the |
| 185 |
* local _xml_obj tag |
| 186 |
* |
| 187 |
* push content onto content stack |
| 188 |
* adds content to tag as a FIFO. |
| 189 |
* You can have n number of parameters. |
| 190 |
* each one will get added in succession to the content. |
| 191 |
* @param mixed $content - either string, or tag object. |
| 192 |
* @access public |
| 193 |
*/ |
| 194 |
function add() { |
| 195 |
if ($this->_root_tag == NULL) { |
| 196 |
$this->_build_root_tag(); |
| 197 |
} |
| 198 |
$args = func_get_args(); |
| 199 |
$num = func_num_args(); |
| 200 |
foreach( $args as $content) { |
| 201 |
$this->_root_tag->push( $content ); |
| 202 |
} |
| 203 |
} |
| 204 |
|
| 205 |
/** |
| 206 |
* Same as add(). |
| 207 |
* NOTE: only exists for 1.1.x compatibility |
| 208 |
* |
| 209 |
* @deprecated |
| 210 |
* @param mixed $content - either string, or tag object. |
| 211 |
* @access public |
| 212 |
*/ |
| 213 |
function push( ) { |
| 214 |
$args = func_get_args(); |
| 215 |
call_user_func_array( array(&$this, "add"), $args); |
| 216 |
} |
| 217 |
|
| 218 |
|
| 219 |
/** |
| 220 |
* we override this class to make sure |
| 221 |
* we push all content inside the |
| 222 |
* local _xml_obj tag |
| 223 |
* |
| 224 |
* push content onto content stack |
| 225 |
* adds content to tag as a FIFO |
| 226 |
* You can only add 1 element at a time, and |
| 227 |
* it will be added as a reference. So you can't do |
| 228 |
* push_reference("something");, since "something" is a |
| 229 |
* static. |
| 230 |
* @param mixed $content - either string, or tag object. |
| 231 |
* the tag object gets stored as a |
| 232 |
* reference to the original, so you |
| 233 |
* can push it, then modify it later. |
| 234 |
* @access public |
| 235 |
*/ |
| 236 |
function add_reference( &$content ) { |
| 237 |
if ($this->_root_tag == NULL) { |
| 238 |
$this->_build_root_tag(); |
| 239 |
} |
| 240 |
$this->_root_tag->add_reference( $content ); |
| 241 |
} |
| 242 |
|
| 243 |
/** |
| 244 |
* Same as add_reference |
| 245 |
* NOTE : only exists for compatibility with 1.1.x |
| 246 |
* |
| 247 |
* @deprecated |
| 248 |
* |
| 249 |
* @access public |
| 250 |
* @param mixed - a reference to some variable. |
| 251 |
*/ |
| 252 |
function push_reference( &$content ) { |
| 253 |
$this->add_reference( $content ); |
| 254 |
} |
| 255 |
|
| 256 |
/** |
| 257 |
* This function is used to add a new |
| 258 |
* xml-stylesheet tag to the top |
| 259 |
* portion of the document. |
| 260 |
* This will automatically get added |
| 261 |
* after the xml tag and before the |
| 262 |
* <!DOCTYPE > tag |
| 263 |
* |
| 264 |
* @param string - the href for the stylesheet link |
| 265 |
*/ |
| 266 |
function add_xml_stylesheet_link( $href ) { |
| 267 |
$this->_xml_stylesheet_container->add( xml_stylesheet($href) ); |
| 268 |
} |
| 269 |
|
| 270 |
/** |
| 271 |
* This function builds the _xml_stylesheet_container |
| 272 |
* |
| 273 |
*/ |
| 274 |
function _build_xml_stylesheet_container() { |
| 275 |
$this->_xml_stylesheet_container = container(); |
| 276 |
} |
| 277 |
|
| 278 |
|
| 279 |
/** |
| 280 |
* we override this class to make sure |
| 281 |
* we push all content inside the |
| 282 |
* local _xml_obj tag |
| 283 |
* |
| 284 |
* destroy existing content and start with new content. |
| 285 |
* @param mixed $content can be tag object, or raw (string). |
| 286 |
* @access public |
| 287 |
*/ |
| 288 |
function reset_content( ) { |
| 289 |
$this->_build_root_tag(); |
| 290 |
} |
| 291 |
|
| 292 |
|
| 293 |
/** |
| 294 |
* This function builds the local |
| 295 |
* xml container object that holds |
| 296 |
* all xml tags that are pushed into |
| 297 |
* this class |
| 298 |
* |
| 299 |
* @return XMLtag object |
| 300 |
*/ |
| 301 |
function _create_xml_obj() { |
| 302 |
$this->_xml_obj = &new XMLtag( array("version" => "1.0") ); |
| 303 |
} |
| 304 |
|
| 305 |
/** |
| 306 |
* This function builds the root xml |
| 307 |
* tag that will contain all of the Document's |
| 308 |
* xml tags |
| 309 |
* |
| 310 |
*/ |
| 311 |
function _build_root_tag() { |
| 312 |
$this->_root_tag = xml_tag($this->_root_name); |
| 313 |
} |
| 314 |
|
| 315 |
/** |
| 316 |
* set the character set |
| 317 |
* @param string $charset - the charset for the meta tag |
| 318 |
* |
| 319 |
*/ |
| 320 |
function set_encoding( $charset ) { |
| 321 |
$this->_charset = $charset; |
| 322 |
} |
| 323 |
|
| 324 |
/** |
| 325 |
* This function gets the current encoding type |
| 326 |
* |
| 327 |
* @return string - the current encoding type |
| 328 |
* for the xml |
| 329 |
*/ |
| 330 |
function get_encoding() { |
| 331 |
return $this->_charset; |
| 332 |
} |
| 333 |
|
| 334 |
/** |
| 335 |
* Set the document name |
| 336 |
* and the root tag's name. |
| 337 |
* @param string $doc_name |
| 338 |
* |
| 339 |
*/ |
| 340 |
function set_root_name($root_name) { |
| 341 |
$this->_root_name = $root_name; |
| 342 |
} |
| 343 |
|
| 344 |
/** |
| 345 |
* This function gets the document name |
| 346 |
* |
| 347 |
* @return string - the current document name |
| 348 |
* for the xml |
| 349 |
*/ |
| 350 |
function get_root_name() { |
| 351 |
return $this->_root_name; |
| 352 |
} |
| 353 |
|
| 354 |
/** |
| 355 |
* This function is used to set the |
| 356 |
* root xml tag's attributes |
| 357 |
* |
| 358 |
* @param array |
| 359 |
*/ |
| 360 |
function set_root_attributes( $attributes ) { |
| 361 |
$this->_root_attributes = $attributes; |
| 362 |
} |
| 363 |
|
| 364 |
/** |
| 365 |
* This sets 1 root tag attribute value pair |
| 366 |
* |
| 367 |
* @param string - the attribute name |
| 368 |
* @param string - the attribute value |
| 369 |
*/ |
| 370 |
function set_root_attribute( $name, $value ) { |
| 371 |
$this->_root_attributes[$name] = $value; |
| 372 |
} |
| 373 |
|
| 374 |
/** |
| 375 |
* This function is used to get the |
| 376 |
* root xml tag's attributes |
| 377 |
* |
| 378 |
* @return array |
| 379 |
*/ |
| 380 |
function get_root_attributes() { |
| 381 |
return $this->_root_attributes; |
| 382 |
} |
| 383 |
|
| 384 |
/** |
| 385 |
* This method is used to set the link attributes |
| 386 |
* for the DOCTYPE tag |
| 387 |
* |
| 388 |
*/ |
| 389 |
function set_doctype_link( $link ) { |
| 390 |
if ($link != NULL) { |
| 391 |
$this->_doctype_links[] = $link; |
| 392 |
} |
| 393 |
} |
| 394 |
|
| 395 |
/** |
| 396 |
* method to update the flag that lets us know |
| 397 |
* to show/render the character encoding string |
| 398 |
* in the xml tag or not |
| 399 |
* |
| 400 |
* @param boolean |
| 401 |
*/ |
| 402 |
function show_character_encoding( $flag=TRUE) { |
| 403 |
$this->_show_char_encoding = $flag; |
| 404 |
} |
| 405 |
|
| 406 |
/** |
| 407 |
* this function is used to set the flag |
| 408 |
* that tells this class to automatically |
| 409 |
* output the content type in the http header |
| 410 |
* |
| 411 |
* @param boolean |
| 412 |
*/ |
| 413 |
function show_http_header( $flag=TRUE ) { |
| 414 |
$this->_show_http_header = $flag; |
| 415 |
} |
| 416 |
|
| 417 |
/** |
| 418 |
* This function is used to set the |
| 419 |
* http header content-type for output to browsers |
| 420 |
* |
| 421 |
* @param string |
| 422 |
*/ |
| 423 |
function set_http_content_type( $type="application/xml") { |
| 424 |
$this->_http_content_type = $type; |
| 425 |
} |
| 426 |
|
| 427 |
/** |
| 428 |
* This function returns the current |
| 429 |
* value of the http header content type |
| 430 |
* |
| 431 |
* @return string |
| 432 |
*/ |
| 433 |
function get_http_content_type() { |
| 434 |
return $this->_http_content_type; |
| 435 |
} |
| 436 |
|
| 437 |
/** |
| 438 |
* This function is used to output the http |
| 439 |
* header for this document. |
| 440 |
* It makes a call to php's Header() to |
| 441 |
* set the Content-type |
| 442 |
* |
| 443 |
*/ |
| 444 |
function dump_http_header() { |
| 445 |
if ($this->_show_http_header) { |
| 446 |
Header("Content-type: ".$this->_http_content_type); |
| 447 |
} |
| 448 |
} |
| 449 |
|
| 450 |
/** |
| 451 |
* this function is used to change the |
| 452 |
* DOCTYPE tag's source attribute. By |
| 453 |
* default it is set to SYSTEM. |
| 454 |
* (ie <!DOCTYPE html SYSTEM > ) |
| 455 |
* ^^^^^^ |
| 456 |
* |
| 457 |
* NOTE: only 2 valid values 'SYSTEM', 'PUBLIC' |
| 458 |
* |
| 459 |
* @param string - the new source attribute |
| 460 |
*/ |
| 461 |
function set_doctype_source($source) { |
| 462 |
if (strcmp("PUBLIC", $source) != 0 && |
| 463 |
strcmp("SYSTEM", $source) != 0) { |
| 464 |
//user tried to set it to an invalid |
| 465 |
//value. |
| 466 |
trigger_error("XMLDocumentClass::set_doctype_source() ". |
| 467 |
"invalid source type of (".$source.") set. ", |
| 468 |
E_USER_WARNING); |
| 469 |
} else { |
| 470 |
$this->_doctype_source = $source; |
| 471 |
} |
| 472 |
} |
| 473 |
|
| 474 |
/** |
| 475 |
* This function returns the current |
| 476 |
* DOCTYPE tag's source attribute |
| 477 |
* |
| 478 |
* @return string |
| 479 |
*/ |
| 480 |
function get_doctype_source() { |
| 481 |
return $this->_doctype_source; |
| 482 |
} |
| 483 |
|
| 484 |
|
| 485 |
/** |
| 486 |
* This method builds the DOCTYPE tag for the document |
| 487 |
* |
| 488 |
*/ |
| 489 |
function _build_doctype() { |
| 490 |
if (count($this->_doctype_links) > 0) { |
| 491 |
$this->_doctype = xml_doctype($this->get_root_name(), |
| 492 |
$this->get_doctype_source(), |
| 493 |
$this->_doctype_links[0], |
| 494 |
$this->_doctype_links[1]); |
| 495 |
} |
| 496 |
} |
| 497 |
} |
| 498 |
?> |