tools - add missing comment blocks
[civicrm-core.git] / tools / extensions / org.civicrm.payment.googlecheckout / packages / Google / library / xml-processing / xmlbuilder.php
1 <?php
2 /*
3 Copyright (C) 2006 Google Inc.
4
5 This program is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public License
7 as published by the Free Software Foundation; either version 2
8 of the License, or (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 */
19
20
21
22 /*
23 * Class used to generate XML data
24 * Based on sample code available at http://simon.incutio.com/code/php/XmlWriter.class.php.txt
25 */
26
27 /**
28 * Class XmlBuilder
29 */
30 class XmlBuilder {
31 var $xml;
32 var $indent;
33 var $stack = array();
34
35 /**
36 * @param string $indent
37 */
38 function XmlBuilder($indent = ' ') {
39 $this->indent = $indent;
40 $this->xml = '<?xml version="1.0" encoding="utf-8"?>' . "\n";
41 }
42
43 function _indent() {
44 for ($i = 0, $j = count($this->stack); $i < $j; $i++) {
45 $this->xml .= $this->indent;
46 }
47 }
48
49 //Used when an element has sub-elements
50 // This function adds an open tag to the output
51 /**
52 * @param $element
53 * @param array $attributes
54 */
55 function Push($element, $attributes = array()) {
56 $this->_indent();
57 $this->xml .= '<' . $element;
58 foreach ($attributes as $key => $value) {
59 $this->xml .= ' ' . $key . '="' . htmlentities($value) . '"';
60 }
61 $this->xml .= ">\n";
62 $this->stack[] = $element;
63 }
64
65 //Used when an element has no subelements.
66 //Data within the open and close tags are provided with the
67 //contents variable
68 /**
69 * @param $element
70 * @param $content
71 * @param array $attributes
72 */
73 function Element($element, $content, $attributes = array()) {
74 $this->_indent();
75 $this->xml .= '<' . $element;
76 foreach ($attributes as $key => $value) {
77 $this->xml .= ' ' . $key . '="' . htmlentities($value) . '"';
78 }
79 $this->xml .= '>' . htmlentities($content) . '</' . $element . '>' . "\n";
80 }
81
82 /**
83 * @param $element
84 * @param array $attributes
85 */
86 function EmptyElement($element, $attributes = array()) {
87 $this->_indent();
88 $this->xml .= '<' . $element;
89 foreach ($attributes as $key => $value) {
90 $this->xml .= ' ' . $key . '="' . htmlentities($value) . '"';
91 }
92 $this->xml .= " />\n";
93 }
94
95 //Used to close an open tag
96 /**
97 * @param $pop_element
98 */
99 function Pop($pop_element) {
100 $element = array_pop($this->stack);
101 $this->_indent();
102 if ($element !== $pop_element)die('XML Error: Tag Mismatch when trying to close "' . $pop_element . '"');
103 else $this->xml .= "</$element>\n";
104 }
105
106 /**
107 * @return string
108 */
109 function GetXML() {
110 if (count($this->stack) != 0)die('XML Error: No matching closing tag found for " ' . array_pop($this->stack) { . '"'
111 );
112 }
113 else return $this->xml;
114 }
115 }
116