12 require_once(
'AWLUtilities.php');
23 protected $attributes;
35 function __construct( $tagname, $content=
false, $attributes=
false, $xmlns=null ) {
36 $this->tagname=$tagname;
37 if ( gettype($content) ==
"object" ) {
39 $this->content = array(&$content);
43 $this->content = $content;
45 $this->attributes = $attributes;
46 if ( isset($xmlns) ) {
47 $this->xmlns = $xmlns;
50 if ( preg_match(
'{^(.*):([^:]*)$}', $tagname, $matches) ) {
51 $prefix = $matches[1];
53 if ( isset($this->attributes[
'xmlns:'.$prefix]) ) {
54 $this->xmlns = $this->attributes[
'xmlns:'.$prefix];
57 else if ( isset($this->attributes[
'xmlns']) ) {
58 $this->xmlns = $this->attributes[
'xmlns'];
69 if ( $this->content ===
false )
return 0;
70 if ( is_array($this->content) )
return count($this->content);
71 if ( $this->content ==
'' )
return 0;
82 if ( gettype($this->attributes) !=
"array" ) $this->attributes = array();
83 $this->attributes[$k] = $v;
84 if ( strtolower($k) ==
'xmlns' ) {
104 return $this->tagname;
113 return (empty($this->xmlns) ?
'' : $this->xmlns .
':') . $this->tagname;
122 if ( $attr ==
'xmlns' )
return $this->xmlns;
123 if ( isset($this->attributes[$attr]) )
return $this->attributes[$attr];
133 return $this->attributes;
142 return $this->content;
153 if ( gettype($this->content) ==
"array" ) {
154 foreach( $this->content AS $k => $v ) {
155 if ( empty($tag) || $v->GetNSTag() == $tag ) {
159 $elements = $elements + $v->GetElements($tag,
true);
163 else if ( empty($tag) || (isset($v->content->tagname) && $v->content->GetNSTag() == $tag) ) {
164 $elements[] = $this->content;
178 if ( !preg_match(
'#(/)?([^/]+)(/?.*)$#', $path, $matches ) )
return $elements;
180 if ( $matches[2] ==
'*' || $matches[2] == $this->
GetNSTag()) {
181 if ( $matches[3] ==
'' ) {
187 else if ( gettype($this->content) ==
"array" ) {
191 foreach( $this->content AS $k => $v ) {
192 $elements = array_merge( $elements, $v->GetPath($matches[3]) );
197 if ( $matches[1] !=
'/' && gettype($this->content) ==
"array" ) {
201 foreach( $this->content AS $k => $v ) {
202 $elements = array_merge( $elements, $v->GetPath($path) );
216 if ( gettype($this->content) !=
"array" ) $this->content = array();
217 $this->content[] =& $v;
218 return count($this->content);
230 function &
NewElement( $tagname, $content=
false, $attributes=
false, $xmlns=null ) {
231 if ( gettype($this->content) !=
"array" ) $this->content = array();
232 $element =
new XMLElement($tagname,$content,$attributes,$xmlns);
233 $this->content[] =& $element;
245 if ( is_array($this->content) ) {
250 foreach( $this->content AS $k => $v ) {
251 if ( is_object($v) ) {
252 $r .= $v->Render($indent+1,
"", $nslist, $force_xmlns);
255 $r .= substr(
" ",0,$indent);
262 if(strpos($this->content,
'<![CDATA[')===0 && strrpos($this->content,
']]>')===strlen($this->content)-3)
263 $r .=
'<![CDATA[' . str_replace(
']]>',
']]]]><![CDATA[>', substr($this->content, 9, -3)) .
']]>';
264 else if ( defined(
'ENT_XML1') && defined(
'ENT_DISALLOWED') )
266 $r .= htmlspecialchars($this->content, ENT_NOQUOTES | ENT_XML1 | ENT_DISALLOWED );
272 $r .= htmlspecialchars($this->content, ENT_NOQUOTES );
283 function Render($indent=0, $xmldef=
"", $nslist=null, $force_xmlns=
false) {
284 $r = ( $xmldef ==
"" ?
"" : $xmldef.
"\n");
287 $tagname = $this->tagname;
289 if ( gettype($this->attributes) ==
"array" ) {
293 foreach( $this->attributes AS $k => $v ) {
294 if ( preg_match(
'#^xmlns(:?(.+))?$#', $k, $matches ) ) {
296 if ( !isset($nslist) ) $nslist = array();
297 $prefix = (isset($matches[2]) ? $matches[2] :
'');
298 if ( isset($nslist[$v]) && $nslist[$v] == $prefix )
continue;
299 $nslist[$v] = $prefix;
300 if ( !isset($this->xmlns) ) $this->xmlns = $v;
303 $attr .= sprintf(
' %s="%s"', $k, htmlspecialchars($v) );
306 if ( isset($this->xmlns) && isset($nslist[$this->xmlns]) && $nslist[$this->xmlns] !=
'' ) {
308 $tagname = $nslist[$this->xmlns] .
':' . $tagname;
309 if ( $force_xmlns ) $attr .= sprintf(
' xmlns="%s"', $this->xmlns);
311 else if ( isset($this->xmlns) && !isset($nslist[$this->xmlns]) && gettype($this->attributes) ==
'array' && !isset($this->attributes[$this->xmlns]) ) {
313 $attr .= sprintf(
' xmlns="%s"', $this->xmlns);
315 else if ( $force_xmlns && isset($this->xmlns) && ! $xmlns_done ) {
317 $attr .= sprintf(
' xmlns="%s"', $this->xmlns);
320 $r .= substr(
" ",0,$indent) .
'<' . $tagname . $attr;
322 if ( (is_array($this->content) && count($this->content) > 0) || (!is_array($this->content) && strlen($this->content) > 0) ) {
325 $r .=
'</' . $tagname.
">\n";
334 function __tostring() {
348 function BuildXMLTree( $xmltags, &$start_from ) {
351 if ( !isset($start_from) ) $start_from = 0;
353 for( $i=0; $i < 50000 && isset($xmltags[$start_from]); $i++) {
354 $tagdata = $xmltags[$start_from++];
355 if ( !isset($tagdata) || !isset($tagdata[
'tag']) || !isset($tagdata[
'type']) )
break;
356 if ( $tagdata[
'type'] ==
"close" )
break;
358 $tag = $tagdata[
'tag'];
359 if ( preg_match(
'{^(.*):([^:]*)$}', $tag, $matches) ) {
360 $xmlns = $matches[1];
363 $attributes = ( isset($tagdata[
'attributes']) ? $tagdata[
'attributes'] : false );
364 if ( $tagdata[
'type'] ==
"open" ) {
365 $subtree = BuildXMLTree( $xmltags, $start_from );
366 $content[] =
new XMLElement($tag, $subtree, $attributes, $xmlns );
368 else if ( $tagdata[
'type'] ==
"complete" ) {
369 $value = ( isset($tagdata[
'value']) ? $tagdata[
'value'] : false );
370 $content[] =
new XMLElement($tag, $value, $attributes, $xmlns );
378 if ( count($content) == 1 ) {
RenderContent($indent=0, $nslist=null, $force_xmlns=false)
Render($indent=0, $xmldef="", $nslist=null, $force_xmlns=false)
& NewElement($tagname, $content=false, $attributes=false, $xmlns=null)
GetElements($tag=null, $recursive=false)
__construct($tagname, $content=false, $attributes=false, $xmlns=null)