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 ( $this->attributes ===
false ) $this->attributes = array();
47 if ( isset($xmlns) ) {
48 $this->xmlns = $xmlns;
51 if ( preg_match(
'{^(.*):([^:]*)$}', $tagname, $matches) ) {
52 $prefix = $matches[1];
54 if ( isset($this->attributes[
'xmlns:'.$prefix]) ) {
55 $this->xmlns = $this->attributes[
'xmlns:'.$prefix];
58 else if ( isset($this->attributes[
'xmlns']) ) {
59 $this->xmlns = $this->attributes[
'xmlns'];
70 if ( $this->content ===
false )
return 0;
71 if ( is_array($this->content) )
return count($this->content);
72 if ( $this->content ==
'' )
return 0;
83 if ( gettype($this->attributes) !=
"array" ) $this->attributes = array();
84 $this->attributes[$k] = $v;
85 if ( strtolower($k) ==
'xmlns' ) {
105 return $this->tagname;
114 return (empty($this->xmlns) ?
'' : $this->xmlns .
':') . $this->tagname;
123 if ( $attr ==
'xmlns' )
return $this->xmlns;
124 if ( isset($this->attributes[$attr]) )
return $this->attributes[$attr];
134 return $this->attributes;
143 return $this->content;
154 if ( gettype($this->content) ==
"array" ) {
155 foreach( $this->content AS $k => $v ) {
156 if ( empty($tag) || $v->GetNSTag() == $tag ) {
160 $elements = $elements + $v->GetElements($tag,
true);
164 else if ( empty($tag) || (isset($v->content->tagname) && $v->content->GetNSTag() == $tag) ) {
165 $elements[] = $this->content;
179 if ( !preg_match(
'#(/)?([^/]+)(/?.*)$#', $path, $matches ) )
return $elements;
181 if ( $matches[2] ==
'*' || $matches[2] == $this->
GetNSTag()) {
182 if ( $matches[3] ==
'' ) {
188 else if ( gettype($this->content) ==
"array" ) {
192 foreach( $this->content AS $k => $v ) {
193 $elements = array_merge( $elements, $v->GetPath($matches[3]) );
198 if ( $matches[1] !=
'/' && gettype($this->content) ==
"array" ) {
202 foreach( $this->content AS $k => $v ) {
203 $elements = array_merge( $elements, $v->GetPath($path) );
217 if ( gettype($this->content) !=
"array" ) $this->content = array();
218 $this->content[] =& $v;
219 return count($this->content);
231 function &
NewElement( $tagname, $content=
false, $attributes=
false, $xmlns=
null ) {
232 if ( gettype($this->content) !=
"array" ) $this->content = array();
233 $element =
new XMLElement($tagname,$content,$attributes,$xmlns);
234 $this->content[] =& $element;
246 if ( is_array($this->content) ) {
251 foreach( $this->content AS $k => $v ) {
252 if ( is_object($v) ) {
253 $r .= $v->Render($indent+1,
"", $nslist, $force_xmlns);
256 $r .= substr(
" ",0,$indent);
263 if(strpos($this->content,
'<![CDATA[')===0 && strrpos($this->content,
']]>')===strlen($this->content)-3)
264 $r .=
'<![CDATA[' . str_replace(
']]>',
']]]]><![CDATA[>', substr($this->content, 9, -3)) .
']]>';
265 else if ( defined(
'ENT_XML1') && defined(
'ENT_DISALLOWED') )
267 $r .= htmlspecialchars($this->content, ENT_NOQUOTES | ENT_XML1 | ENT_DISALLOWED );
273 $r .= htmlspecialchars($this->content, ENT_NOQUOTES );
284 function Render($indent=0, $xmldef=
"", $nslist=
null, $force_xmlns=
false) {
285 $r = ( $xmldef ==
"" ?
"" : $xmldef.
"\n");
288 $tagname = $this->tagname;
290 if ( gettype($this->attributes) ==
"array" ) {
294 foreach( $this->attributes AS $k => $v ) {
295 if ( preg_match(
'#^xmlns(:?(.+))?$#', $k, $matches ) ) {
297 if ( !isset($nslist) ) $nslist = array();
298 $prefix = (isset($matches[2]) ? $matches[2] :
'');
299 if ( isset($nslist[$v]) && $nslist[$v] == $prefix )
continue;
300 $nslist[$v] = $prefix;
301 if ( !isset($this->xmlns) ) $this->xmlns = $v;
304 $attr .= sprintf(
' %s="%s"', $k, htmlspecialchars($v) );
307 if ( isset($this->xmlns) && isset($nslist[$this->xmlns]) && $nslist[$this->xmlns] !=
'' ) {
309 $tagname = $nslist[$this->xmlns] .
':' . $tagname;
310 if ( $force_xmlns ) $attr .= sprintf(
' xmlns="%s"', $this->xmlns);
312 else if ( isset($this->xmlns) && !isset($nslist[$this->xmlns]) && gettype($this->attributes) ==
'array' && !isset($this->attributes[$this->xmlns]) ) {
314 $attr .= sprintf(
' xmlns="%s"', $this->xmlns);
316 else if ( $force_xmlns && isset($this->xmlns) && ! $xmlns_done ) {
318 $attr .= sprintf(
' xmlns="%s"', $this->xmlns);
321 $r .= substr(
" ",0,$indent) .
'<' . $tagname . $attr;
323 if ( (is_array($this->content) && count($this->content) > 0) || (!is_array($this->content) && strlen($this->content) > 0) ) {
326 $r .=
'</' . $tagname.
">\n";
335 function __tostring() {
349 function BuildXMLTree( $xmltags, &$start_from ) {
352 if ( !isset($start_from) ) $start_from = 0;
354 for( $i=0; $i < 50000 && isset($xmltags[$start_from]); $i++) {
355 $tagdata = $xmltags[$start_from++];
356 if ( !isset($tagdata) || !isset($tagdata[
'tag']) || !isset($tagdata[
'type']) )
break;
357 if ( $tagdata[
'type'] ==
"close" )
break;
359 $tag = $tagdata[
'tag'];
360 if ( preg_match(
'{^(.*):([^:]*)$}', $tag, $matches) ) {
361 $xmlns = $matches[1];
364 $attributes = ( isset($tagdata[
'attributes']) ? $tagdata[
'attributes'] : false );
365 if ( $tagdata[
'type'] ==
"open" ) {
366 $subtree = BuildXMLTree( $xmltags, $start_from );
367 $content[] =
new XMLElement($tag, $subtree, $attributes, $xmlns );
369 else if ( $tagdata[
'type'] ==
"complete" ) {
370 $value = ( isset($tagdata[
'value']) ? $tagdata[
'value'] : false );
371 $content[] =
new XMLElement($tag, $value, $attributes, $xmlns );
379 if ( count($content) == 1 ) {
& NewElement( $tagname, $content=false, $attributes=false, $xmlns=null)
__construct( $tagname, $content=false, $attributes=false, $xmlns=null)
Render($indent=0, $xmldef="", $nslist=null, $force_xmlns=false)
GetElements( $tag=null, $recursive=false)
RenderContent($indent=0, $nslist=null, $force_xmlns=false)