MIOLO20
Carregando...
Procurando...
Nenhuma entrada encontrada
class.phpwsdlformatter.php
Ir para a documentação deste ficheiro.
1<?php
2if(basename($_SERVER['SCRIPT_FILENAME'])==basename(__FILE__))
3 exit;
20{
26 protected $_parser = null;
27
33 protected $_input = null;
34
40 protected $_output = null;
41
47 protected $_offset = 0;
48
54 protected $_depth = 0;
55
61 protected $_empty = false;
62
68 protected $_buffer = "";
69
100 protected $_options = array(
101 "bufferSize" => 4096,
102 "paddingString" => " ",
103 "paddingMultiplier" => 4,
104 "formatCData" => true,
105 "multipleLineCData" => true,
106 "wordwrapCData" => 75,
107 "inputEOL" => "\n",
108 "outputEOL" => "\n"
109 );
110
118 public function __construct($input, $output, Array $options = array())
119 {
120 $this->_input = $input;
121 $this->_output = $output;
122 $this->_options = array_merge($this->_options, $options);
123
124 $this->_parser = xml_parser_create();
125
126 xml_set_object($this->_parser, $this);
127
128 xml_parser_set_option($this->_parser, XML_OPTION_CASE_FOLDING, false);
129 xml_parser_set_option($this->_parser, XML_OPTION_SKIP_WHITE, 0);
130
131 xml_set_element_handler($this->_parser, "_cbElementStart", "_cbElementEnd");
132 xml_set_character_data_handler($this->_parser, "_cbCharacterData");
133 }
134
141 protected function _getPaddingStr()
142 {
143 return str_repeat($this->_options["paddingString"], $this->_depth * $this->_options["paddingMultiplier"]);
144 }
145
154 protected function _cbElementStart($parser, $name, Array $attributes)
155 {
156 $idx = xml_get_current_byte_index($this->_parser);
157
158 $this->_empty = $this->_buffer[$idx - $this->_offset] == '/';
159
160 $attrs = "";
161 foreach ($attributes as $key => $val) {
162 $attrs .= " " . $key . "=\"" . $val . "\"";
163 }
164
165 fwrite($this->_output, $this->_getPaddingStr() . "<" . $name . $attrs . ($this->_empty ? ' />' : '>') . "\n");
166
167 if (!$this->_empty) ++$this->_depth;
168 }
169
177 protected function _cbElementEnd($parser, $name)
178 {
179 if (!$this->_empty) {
181
182 fwrite($this->_output, $this->_getPaddingStr() . "</" . $name . ">" . "\n");
183 } else {
184 $this->_empty = false;
185 }
186 }
187
195 protected function _cbCharacterData($parser, $data)
196 {
197 if (!$this->_options["formatCData"]) {
198 return;
199 }
200
201 $data = trim($data);
202
203 if (strlen($data)) {
204 $pad = $this->_getPaddingStr();
205
206 if ($this->_options["multipleLineCData"]) {
207
208 // remove all tabs
209 $data = str_replace("\t", "", $data);
210
211 // append each line with a padding string
212 $data = implode($this->_options["inputEOL"] . $pad, explode($this->_options["inputEOL"], $data));
213 }
214
215 if ($this->_options["wordwrapCData"]) {
216 $data = wordwrap($data, $this->_options["wordwrapCData"], $this->_options["outputEOL"] . $pad, false);
217 }
218
219 fwrite($this->_output, $pad . $data . "\n");
220 }
221 }
222
230 public function format()
231 {
232 while ($this->_buffer = fread($this->_input, $this->_options["bufferSize"])) {
233 if (!xml_parse($this->_parser, $this->_buffer, feof($this->_input))) {
234 throw new Exception(sprintf("XML error: %s at line %d",
235 xml_error_string(xml_get_error_code($this->_parser)),
236 xml_get_current_line_number($this->_parser)));
237 return;
238 }
239
240 $this->_offset += strlen($this->_buffer);
241 }
242
243 xml_parser_free($this->_parser);
244 }
245}
_cbElementStart($parser, $name, Array $attributes)
_cbCharacterData($parser, $data)
_cbElementEnd($parser, $name)
__construct($input, $output, Array $options=array())