1 |
<?php |
2 |
|
3 |
/** |
4 |
* This contains the XMLDocumentClass |
5 |
* |
6 |
* $Id: XMLDocumentClass.inc,v 1.3 2003/10/02 23:38:16 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 |
//we should always render in |
139 |
$GLOBALS["HTML_RENDER_TYPE"] = XHTML_STRICT; |
140 |
} |
141 |
|
142 |
/** |
143 |
* function that will render the XML Document. |
144 |
* |
145 |
* @param int - the indentation level for |
146 |
* the container. |
147 |
* @param int - the output debug flag to |
148 |
* maintain compatibility w/ the API. |
149 |
* |
150 |
* @return string the raw html output. |
151 |
*/ |
152 |
function render( $indent_level = 0) { |
153 |
|
154 |
//dump out the content type |
155 |
$this->dump_http_header(); |
156 |
|
157 |
//set the encoding type |
158 |
if ($this->_show_char_encoding) { |
159 |
$this->_xml_obj->set_tag_attribute("encoding", $this->get_encoding() ); |
160 |
} |
161 |
|
162 |
//add the xml tag |
163 |
$output = $this->_xml_obj->render(0); |
164 |
|
165 |
//add any stylesheet links |
166 |
if ($this->_xml_stylesheet_container->count_content() > 0) { |
167 |
$output .= $this->_xml_stylesheet_container->render(0); |
168 |
} |
169 |
|
170 |
//build the doctype if needed. |
171 |
$this->_build_doctype(); |
172 |
|
173 |
if ($this->_doctype != NULL) { |
174 |
$output .= $this->_doctype->render(); |
175 |
} |
176 |
|
177 |
//set the root tag's attributes |
178 |
$this->_root_tag->set_tag_attributes( $this->get_root_attributes() ); |
179 |
|
180 |
//render the root tag's content |
181 |
$output .= $this->_root_tag->render($indent_level, 0); |
182 |
return $output; |
183 |
} |
184 |
|
185 |
/** |
186 |
* we override this class to make sure |
187 |
* we push all content inside the |
188 |
* local _xml_obj tag |
189 |
* |
190 |
* push content onto content stack |
191 |
* adds content to tag as a FIFO. |
192 |
* You can have n number of parameters. |
193 |
* each one will get added in succession to the content. |
194 |
* @param mixed $content - either string, or tag object. |
195 |
* @access public |
196 |
*/ |
197 |
function add() { |
198 |
if ($this->_root_tag == NULL) { |
199 |
$this->_build_root_tag(); |
200 |
} |
201 |
$args = func_get_args(); |
202 |
$num = func_num_args(); |
203 |
foreach( $args as $content) { |
204 |
$this->_root_tag->push( $content ); |
205 |
} |
206 |
} |
207 |
|
208 |
/** |
209 |
* Same as add(). |
210 |
* NOTE: only exists for 1.1.x compatibility |
211 |
* |
212 |
* @deprecated |
213 |
* @param mixed $content - either string, or tag object. |
214 |
* @access public |
215 |
*/ |
216 |
function push( ) { |
217 |
$args = func_get_args(); |
218 |
call_user_func_array( array(&$this, "add"), $args); |
219 |
} |
220 |
|
221 |
|
222 |
/** |
223 |
* we override this class to make sure |
224 |
* we push all content inside the |
225 |
* local _xml_obj tag |
226 |
* |
227 |
* push content onto content stack |
228 |
* adds content to tag as a FIFO |
229 |
* You can only add 1 element at a time, and |
230 |
* it will be added as a reference. So you can't do |
231 |
* push_reference("something");, since "something" is a |
232 |
* static. |
233 |
* @param mixed $content - either string, or tag object. |
234 |
* the tag object gets stored as a |
235 |
* reference to the original, so you |
236 |
* can push it, then modify it later. |
237 |
* @access public |
238 |
*/ |
239 |
function add_reference( &$content ) { |
240 |
if ($this->_root_tag == NULL) { |
241 |
$this->_build_root_tag(); |
242 |
} |
243 |
$this->_root_tag->add_reference( $content ); |
244 |
} |
245 |
|
246 |
/** |
247 |
* Same as add_reference |
248 |
* NOTE : only exists for compatibility with 1.1.x |
249 |
* |
250 |
* @deprecated |
251 |
* |
252 |
* @access public |
253 |
* @param mixed - a reference to some variable. |
254 |
*/ |
255 |
function push_reference( &$content ) { |
256 |
$this->add_reference( $content ); |
257 |
} |
258 |
|
259 |
/** |
260 |
* This function is used to add a new |
261 |
* xml-stylesheet tag to the top |
262 |
* portion of the document. |
263 |
* This will automatically get added |
264 |
* after the xml tag and before the |
265 |
* <!DOCTYPE > tag |
266 |
* |
267 |
* @param string - the href for the stylesheet link |
268 |
*/ |
269 |
function add_xml_stylesheet_link( $href ) { |
270 |
$this->_xml_stylesheet_container->add( xml_stylesheet($href) ); |
271 |
} |
272 |
|
273 |
/** |
274 |
* This function builds the _xml_stylesheet_container |
275 |
* |
276 |
*/ |
277 |
function _build_xml_stylesheet_container() { |
278 |
$this->_xml_stylesheet_container = container(); |
279 |
} |
280 |
|
281 |
|
282 |
/** |
283 |
* we override this class to make sure |
284 |
* we push all content inside the |
285 |
* local _xml_obj tag |
286 |
* |
287 |
* destroy existing content and start with new content. |
288 |
* @param mixed $content can be tag object, or raw (string). |
289 |
* @access public |
290 |
*/ |
291 |
function reset_content( ) { |
292 |
$this->_build_root_tag(); |
293 |
} |
294 |
|
295 |
|
296 |
/** |
297 |
* This function builds the local |
298 |
* xml container object that holds |
299 |
* all xml tags that are pushed into |
300 |
* this class |
301 |
* |
302 |
* @return XMLtag object |
303 |
*/ |
304 |
function _create_xml_obj() { |
305 |
$this->_xml_obj = &new XMLtag( array("version" => "1.0") ); |
306 |
} |
307 |
|
308 |
/** |
309 |
* This function builds the root xml |
310 |
* tag that will contain all of the Document's |
311 |
* xml tags |
312 |
* |
313 |
*/ |
314 |
function _build_root_tag() { |
315 |
$this->_root_tag = xml_tag($this->_root_name); |
316 |
} |
317 |
|
318 |
/** |
319 |
* set the character set |
320 |
* @param string $charset - the charset for the meta tag |
321 |
* |
322 |
*/ |
323 |
function set_encoding( $charset ) { |
324 |
$this->_charset = $charset; |
325 |
} |
326 |
|
327 |
/** |
328 |
* This function gets the current encoding type |
329 |
* |
330 |
* @return string - the current encoding type |
331 |
* for the xml |
332 |
*/ |
333 |
function get_encoding() { |
334 |
return $this->_charset; |
335 |
} |
336 |
|
337 |
/** |
338 |
* Set the document name |
339 |
* and the root tag's name. |
340 |
* @param string $doc_name |
341 |
* |
342 |
*/ |
343 |
function set_root_name($root_name) { |
344 |
$this->_root_name = $root_name; |
345 |
} |
346 |
|
347 |
/** |
348 |
* This function gets the document name |
349 |
* |
350 |
* @return string - the current document name |
351 |
* for the xml |
352 |
*/ |
353 |
function get_root_name() { |
354 |
return $this->_root_name; |
355 |
} |
356 |
|
357 |
/** |
358 |
* This function is used to set the |
359 |
* root xml tag's attributes |
360 |
* |
361 |
* @param array |
362 |
*/ |
363 |
function set_root_attributes( $attributes ) { |
364 |
$this->_root_attributes = $attributes; |
365 |
} |
366 |
|
367 |
/** |
368 |
* This sets 1 root tag attribute value pair |
369 |
* |
370 |
* @param string - the attribute name |
371 |
* @param string - the attribute value |
372 |
*/ |
373 |
function set_root_attribute( $name, $value ) { |
374 |
$this->_root_attributes[$name] = $value; |
375 |
} |
376 |
|
377 |
/** |
378 |
* This function is used to get the |
379 |
* root xml tag's attributes |
380 |
* |
381 |
* @return array |
382 |
*/ |
383 |
function get_root_attributes() { |
384 |
return $this->_root_attributes; |
385 |
} |
386 |
|
387 |
/** |
388 |
* This method is used to set the link attributes |
389 |
* for the DOCTYPE tag |
390 |
* |
391 |
*/ |
392 |
function set_doctype_link( $link ) { |
393 |
if ($link != NULL) { |
394 |
$this->_doctype_links[] = $link; |
395 |
} |
396 |
} |
397 |
|
398 |
/** |
399 |
* method to update the flag that lets us know |
400 |
* to show/render the character encoding string |
401 |
* in the xml tag or not |
402 |
* |
403 |
* @param boolean |
404 |
*/ |
405 |
function show_character_encoding( $flag=TRUE) { |
406 |
$this->_show_char_encoding = $flag; |
407 |
} |
408 |
|
409 |
/** |
410 |
* this function is used to set the flag |
411 |
* that tells this class to automatically |
412 |
* output the content type in the http header |
413 |
* |
414 |
* @param boolean |
415 |
*/ |
416 |
function show_http_header( $flag=TRUE ) { |
417 |
$this->_show_http_header = $flag; |
418 |
} |
419 |
|
420 |
/** |
421 |
* This function is used to set the |
422 |
* http header content-type for output to browsers |
423 |
* |
424 |
* @param string |
425 |
*/ |
426 |
function set_http_content_type( $type="application/xml") { |
427 |
$this->_http_content_type = $type; |
428 |
} |
429 |
|
430 |
/** |
431 |
* This function returns the current |
432 |
* value of the http header content type |
433 |
* |
434 |
* @return string |
435 |
*/ |
436 |
function get_http_content_type() { |
437 |
return $this->_http_content_type; |
438 |
} |
439 |
|
440 |
/** |
441 |
* This function is used to output the http |
442 |
* header for this document. |
443 |
* It makes a call to php's Header() to |
444 |
* set the Content-type |
445 |
* |
446 |
*/ |
447 |
function dump_http_header() { |
448 |
if ($this->_show_http_header) { |
449 |
Header("Content-type: ".$this->_http_content_type); |
450 |
} |
451 |
} |
452 |
|
453 |
/** |
454 |
* this function is used to change the |
455 |
* DOCTYPE tag's source attribute. By |
456 |
* default it is set to SYSTEM. |
457 |
* (ie <!DOCTYPE html SYSTEM > ) |
458 |
* ^^^^^^ |
459 |
* |
460 |
* NOTE: only 2 valid values 'SYSTEM', 'PUBLIC' |
461 |
* |
462 |
* @param string - the new source attribute |
463 |
*/ |
464 |
function set_doctype_source($source) { |
465 |
if (strcmp("PUBLIC", $source) != 0 && |
466 |
strcmp("SYSTEM", $source) != 0) { |
467 |
//user tried to set it to an invalid |
468 |
//value. |
469 |
trigger_error("XMLDocumentClass::set_doctype_source() ". |
470 |
"invalid source type of (".$source.") set. ", |
471 |
E_USER_WARNING); |
472 |
} else { |
473 |
$this->_doctype_source = $source; |
474 |
} |
475 |
} |
476 |
|
477 |
/** |
478 |
* This function returns the current |
479 |
* DOCTYPE tag's source attribute |
480 |
* |
481 |
* @return string |
482 |
*/ |
483 |
function get_doctype_source() { |
484 |
return $this->_doctype_source; |
485 |
} |
486 |
|
487 |
|
488 |
/** |
489 |
* This method builds the DOCTYPE tag for the document |
490 |
* |
491 |
*/ |
492 |
function _build_doctype() { |
493 |
if (count($this->_doctype_links) > 0) { |
494 |
$this->_doctype = xml_doctype($this->get_root_name(), |
495 |
$this->get_doctype_source(), |
496 |
$this->_doctype_links[0], |
497 |
count($this->_doctype_links) > 1 ? $this->_doctype_links[1] : NULL); |
498 |
} |
499 |
} |
500 |
} |
501 |
?> |