13 require_once(
"XMLElement.php");
49 $this->namespaces = array();
50 $this->prefixes = array();
51 if ( $namespaces !=
null ) {
52 foreach( $namespaces AS $ns => $prefix ) {
53 $this->namespaces[$ns] = $prefix;
54 $this->prefixes[$prefix] = $prefix;
57 $this->next_prefix = 0;
67 if ( !isset($this->namespaces[$namespace]) ) {
68 if ( isset($prefix) && ($prefix ==
"" || isset($this->prefixes[$prefix])) ) $prefix =
null;
69 if ( $prefix ==
null ) {
71 if ( preg_match(
'/^(.*):([^:]+)$/', $namespace, $matches) ) {
72 $alpha = preg_replace(
'/[^a-z]/i',
'', $matches[2] );
73 $prefix = strtoupper(substr($alpha,0,1));
79 if ( isset($this->prefixes[$prefix]) ) {
80 for ( $i=1; $i<10 && isset($this->prefixes[
"$prefix$i"]); $i++ ) {
83 if ( isset($this->prefixes[
"$prefix$i"]) ) {
84 dbg_error_log(
"ERROR",
"Cannot find a free prefix for this namespace");
87 $prefix =
"$prefix$i";
88 dbg_error_log(
"XMLDocument",
"auto-assigning prefix of '%s' for ns of '%s'", $prefix, $namespace );
90 else if ( $prefix ==
"" || isset($this->prefixes[$prefix]) ) {
91 dbg_error_log(
"ERROR",
"Cannot assign the same prefix to two different namespaces");
95 $this->prefixes[$prefix] = $prefix;
96 $this->namespaces[$namespace] = $prefix;
99 if ( isset($this->namespaces[$namespace]) && $this->namespaces[$namespace] != $prefix ) {
100 dbg_error_log(
"ERROR",
"Cannot use the same namespace with two different prefixes");
103 $this->prefixes[$prefix] = $prefix;
104 $this->namespaces[$namespace] = $prefix;
112 foreach( $this->namespaces AS $k => $v ) {
127 foreach( $this->namespaces AS $n => $p ) {
128 if ( $p ==
"" ) $ns[
"xmlns"] = $n;
else $ns[
"xmlns:$p"] = $n;
144 function Tag( $in_tag, $namespace=
null, $prefix=
null ) {
146 if ( $namespace ==
null ) {
148 if ( preg_match(
'/^(.*):([^:]+)$/', $in_tag, $matches) ) {
149 $namespace = $matches[1];
161 if ( !isset($this->namespaces[$namespace]) ) {
164 $prefix = $this->namespaces[$namespace];
166 return $prefix . ($prefix ==
"" ?
"" :
":") . $tag;
169 static public $ns_dav =
'DAV:';
170 static public $ns_caldav =
'urn:ietf:params:xml:ns:caldav';
171 static public $ns_carddav =
'urn:ietf:params:xml:ns:carddav';
172 static public $ns_calendarserver =
'http://calendarserver.org/ns/';
184 function NSElement( &$element, $in_tag, $content=
false, $attributes=
false, $namespace=
null ) {
185 if ( $namespace ==
null && preg_match(
'/^(.*):([^:]+)$/', $in_tag, $matches) ) {
186 $namespace = $matches[1];
187 if ( preg_match(
'{^[A-Z][A-Z0-9]*$}', $namespace ) ) {
188 throw new Exception(
"Dodgy looking namespace from '".$in_tag.
"'!");
194 if ( isset($namespace) ) {
195 $tag = str_replace($namespace.
':',
'', $tag);
199 if ( isset($namespace) && !isset($this->namespaces[$namespace]) ) $this->
AddNamespace( $namespace );
200 return $element->NewElement( $tag, $content, $attributes, $namespace );
212 function DAVElement( &$element, $tag, $content=
false, $attributes=
false ) {
213 if ( !isset($this->namespaces[self::$ns_dav]) ) $this->
AddNamespace( self::$ns_dav,
'' );
214 return $this->
NSElement( $element, $tag, $content, $attributes, self::$ns_dav );
225 function CalDAVElement( &$element, $tag, $content=
false, $attributes=
false ) {
226 if ( !isset($this->namespaces[self::$ns_caldav]) ) $this->
AddNamespace( self::$ns_caldav,
'C' );
227 return $this->
NSElement( $element, $tag, $content, $attributes, self::$ns_caldav );
240 if ( !isset($this->namespaces[self::$ns_carddav]) ) $this->
AddNamespace( self::$ns_carddav,
'VC' );
241 return $this->
NSElement( $element, $tag, $content, $attributes, self::$ns_carddav );
254 if ( !isset($this->namespaces[self::$ns_calendarserver]) ) $this->
AddNamespace( self::$ns_calendarserver,
'A' );
255 return $this->
NSElement( $element, $tag, $content, $attributes, self::$ns_calendarserver );
265 function NewXMLElement( $in_tag, $content=
false, $attributes=
false, $xmlns=
null ) {
266 if ( $xmlns ==
null && preg_match(
'/^(.*):([^:]+)$/', $in_tag, $matches) ) {
267 $xmlns = $matches[1];
268 $tagname = $matches[2];
274 if ( isset($xmlns) && !isset($this->namespaces[$xmlns]) ) $this->
AddNamespace( $xmlns );
275 return new XMLElement($tagname, $content, $attributes, $xmlns );
288 function Render( $root, $content=
false, $attributes=
false, $xmlns=
null ) {
289 if ( is_object($root) ) {
295 $this->root = $this->
NewXMLElement( $root, $content, $attributes, $xmlns );
301 foreach( $this->namespaces AS $n => $p ) {
302 $this->root->SetAttribute(
'xmlns'.($p ==
'' ?
'' :
':') . $p, $n);
306 return $this->root->Render(0,
'<?xml version="1.0" encoding="utf-8" ?>');
316 if ( is_array($url) ) {
318 foreach( $url AS $href ) {
319 $set[] = $this->
href( $href );
323 if ( preg_match(
'[@+ ]',$url) ) {
324 trace_bug(
'URL "%s" was not encoded before call to XMLDocument::href()', $url );
325 $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)