13require_once(
"XMLElement.php");
56 $this->namespaces = array();
57 $this->prefixes = array();
58 if ( $namespaces !=
null ) {
59 foreach( $namespaces AS $ns => $prefix ) {
60 $this->namespaces[$ns] = $prefix;
61 $this->prefixes[$prefix] = $prefix;
64 $this->next_prefix = 0;
74 if ( !isset($this->namespaces[$namespace]) ) {
75 if ( isset($prefix) && ($prefix ==
"" || isset($this->prefixes[$prefix])) ) $prefix =
null;
76 if ( $prefix ==
null ) {
78 if ( preg_match(
'/^(.*):([^:]+)$/', $namespace, $matches) ) {
79 $alpha = preg_replace(
'/[^a-z]/i',
'', $matches[2] );
80 $prefix = strtoupper(substr($alpha,0,1));
86 if ( isset($this->prefixes[$prefix]) ) {
87 for ( $i=1; $i<10 && isset($this->prefixes[
"$prefix$i"]); $i++ ) {
90 if ( isset($this->prefixes[
"$prefix$i"]) ) {
91 dbg_error_log(
"ERROR",
"Cannot find a free prefix for this namespace");
94 $prefix =
"$prefix$i";
95 dbg_error_log(
"XMLDocument",
"auto-assigning prefix of '%s' for ns of '%s'", $prefix, $namespace );
97 else if ( $prefix ==
"" || isset($this->prefixes[$prefix]) ) {
98 dbg_error_log(
"ERROR",
"Cannot assign the same prefix to two different namespaces");
102 $this->prefixes[$prefix] = $prefix;
103 $this->namespaces[$namespace] = $prefix;
106 if ( isset($this->namespaces[$namespace]) && $this->namespaces[$namespace] != $prefix ) {
107 dbg_error_log(
"ERROR",
"Cannot use the same namespace with two different prefixes");
110 $this->prefixes[$prefix] = $prefix;
111 $this->namespaces[$namespace] = $prefix;
119 foreach( $this->namespaces AS $k => $v ) {
134 foreach( $this->namespaces AS $n => $p ) {
135 if ( $p ==
"" ) $ns[
"xmlns"] = $n;
else $ns[
"xmlns:$p"] = $n;
151 function Tag( $in_tag, $namespace=
null, $prefix=
null ) {
153 if ( $namespace ==
null ) {
155 if ( preg_match(
'/^(.*):([^:]+)$/', $in_tag, $matches) ) {
156 $namespace = $matches[1];
168 if ( !isset($this->namespaces[$namespace]) ) {
171 $prefix = $this->namespaces[$namespace];
173 return $prefix . ($prefix ==
"" ?
"" :
":") . $tag;
176 static public $ns_dav =
'DAV:';
177 static public $ns_caldav =
'urn:ietf:params:xml:ns:caldav';
178 static public $ns_carddav =
'urn:ietf:params:xml:ns:carddav';
179 static public $ns_calendarserver =
'http://calendarserver.org/ns/';
191 function NSElement( &$element, $in_tag, $content=
false, $attributes=
false, $namespace=
null ) {
192 if ( $namespace ==
null && preg_match(
'/^(.*):([^:]+)$/', $in_tag, $matches) ) {
193 $namespace = $matches[1];
194 if ( preg_match(
'{^[A-Z][A-Z0-9]*$}', $namespace ) ) {
195 throw new Exception(
"Dodgy looking namespace from '".$in_tag.
"'!");
201 if ( isset($namespace) ) {
202 $tag = str_replace($namespace.
':',
'', $tag);
206 if ( isset($namespace) && !isset($this->namespaces[$namespace]) ) $this->
AddNamespace( $namespace );
207 return $element->NewElement( $tag, $content, $attributes, $namespace );
219 function DAVElement( &$element, $tag, $content=
false, $attributes=
false ) {
220 if ( !isset($this->namespaces[self::$ns_dav]) ) $this->
AddNamespace( self::$ns_dav,
'' );
221 return $this->
NSElement( $element, $tag, $content, $attributes, self::$ns_dav );
232 function CalDAVElement( &$element, $tag, $content=
false, $attributes=
false ) {
233 if ( !isset($this->namespaces[self::$ns_caldav]) ) $this->
AddNamespace( self::$ns_caldav,
'C' );
234 return $this->
NSElement( $element, $tag, $content, $attributes, self::$ns_caldav );
247 if ( !isset($this->namespaces[self::$ns_carddav]) ) $this->
AddNamespace( self::$ns_carddav,
'VC' );
248 return $this->
NSElement( $element, $tag, $content, $attributes, self::$ns_carddav );
261 if ( !isset($this->namespaces[self::$ns_calendarserver]) ) $this->
AddNamespace( self::$ns_calendarserver,
'A' );
262 return $this->
NSElement( $element, $tag, $content, $attributes, self::$ns_calendarserver );
272 function NewXMLElement( $in_tag, $content=
false, $attributes=
false, $xmlns=
null ) {
273 if ( $xmlns ==
null && preg_match(
'/^(.*):([^:]+)$/', $in_tag, $matches) ) {
274 $xmlns = $matches[1];
275 $tagname = $matches[2];
281 if ( isset($xmlns) && !isset($this->namespaces[$xmlns]) ) $this->
AddNamespace( $xmlns );
282 return new XMLElement($tagname, $content, $attributes, $xmlns );
295 function Render( $root, $content=
false, $attributes=
false, $xmlns=
null ) {
296 if ( is_object($root) ) {
302 $this->root = $this->
NewXMLElement( $root, $content, $attributes, $xmlns );
308 foreach( $this->namespaces AS $n => $p ) {
309 $this->root->SetAttribute(
'xmlns'.($p ==
'' ?
'' :
':') . $p, $n);
313 return $this->root->Render(0,
'<?xml version="1.0" encoding="utf-8" ?>');
323 if ( is_array($url) ) {
325 foreach( $url AS $href ) {
326 $set[] = $this->
href( $href );
330 if ( preg_match(
'[@+ ]',$url) ) {
331 trace_bug(
'URL "%s" was not encoded before call to XMLDocument::href()', $url );
332 $url = str_replace(
'%2F',
'/', rawurlencode($url));
NewXMLElement( $in_tag, $content=false, $attributes=false, $xmlns=null)
Render( $root, $content=false, $attributes=false, $xmlns=null)
CalendarserverElement(&$element, $tag, $content=false, $attributes=false)
__construct( $namespaces=null)
DAVElement(&$element, $tag, $content=false, $attributes=false)
Tag( $in_tag, $namespace=null, $prefix=null)
NSElement(&$element, $in_tag, $content=false, $attributes=false, $namespace=null)
CardDAVElement(&$element, $tag, $content=false, $attributes=false)
CalDAVElement(&$element, $tag, $content=false, $attributes=false)
AddNamespace( $namespace, $prefix=null)