00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00045 class PHPTAL_Tag
00046 {
00050 var
$line = 0;
00051
00052
00053
00054 var
$_name =
"#cdata";
00055 var
$_attrs = array();
00056 var
$_children = array();
00057 var
$_content =
"";
00058 var
$_parent = null;
00059
00060
00061
00062 var
$_parser;
00063 var
$_replace_attributes = array();
00064 var
$_content_attributes = array();
00065 var
$_surround_attributes = array();
00066 var
$_head_foot_disabled =
false;
00067
00068
00078 function
PHPTAL_Tag(&$parser, $name, $attrs)
00079 {
00080 $this->_parser =& $parser;
00081 $this->_name = $name;
00082 $this->_attrs = $attrs;
00083 }
00084
00085 function
appendTemplateAttribute(&$hdlr)
00086 {
00087
switch ($hdlr->_phptal_type)
00088 {
00089
case _PHPTAL_REPLACE:
00090 $this->_replace_attributes[] =& $hdlr;
00091
break;
00092
case _PHPTAL_CONTENT:
00093 $this->_content_attributes[] =& $hdlr;
00094
break;
00095
case _PHPTAL_SURROUND:
00096 $this->_surround_attributes[] =& $hdlr;
00097
break;
00098
default:
00099
return PEAR::raiseError(
00100
"PHPTAL internal error : bad attribute type : "
00101 . get_class($hdlr));
00102 }
00103 }
00104
00105 function
name()
00106 {
00107
return $this->_name;
00108 }
00109
00110 function
isData()
00111 {
00112
return $this->_name ==
"#cdata";
00113 }
00114
00115 function
attributes()
00116 {
00117
return $this->_attrs;
00118 }
00119
00120 function
setParent(&$node)
00121 {
00122 $this->_parent =& $node;
00123 }
00124
00125 function &
getParent()
00126 {
00127
return $this->_parent;
00128 }
00129
00130 function
addChild(&$node)
00131 {
00132 $node->setParent($
this);
00133 $this->_children[] = &$node;
00134 }
00135
00136 function
setContent($str)
00137 {
00138 $this->_content = $str;
00139 }
00140
00141 function
hasContent()
00142 {
00143
return (count($this->_content_attributes) == 0
00144 || count($this->_children) == 0
00145 || strlen($this->_content) == 0);
00146 }
00147
00148 function
toString($tab=
"")
00149 {
00150 $buf =
new StringBuffer();
00151 $buf->appendln($tab, '+ node ', $this->name());
00152
for (
$i=0;
$i < count($this->_children);
$i++) {
00153 $child =& $this->_children[
$i];
00154 $buf->append($child->toString($tab .
" "));
00155 unset($child);
00156 }
00157
return $buf->toString();
00158 }
00159
00160 function
generateCode(&$g)
00161 {
00162
00163
if ($this->_name ==
"#cdata") {
00164 $g->doPrintString($this->_content);
00165
return;
00166 }
00167
00168
if ($this->_name ==
"#root") {
00169
return $this->
generateContent($g);
00170 }
00171
00172
00173
00174
if (count($this->_replace_attributes) > 0) {
00175 $err = $this->
surroundHead($g);
00176
if (PEAR::isError($err)) {
return $err; }
00177
00178
for (
$i=0;
$i<count($this->_replace_attributes);
$i++) {
00179 $h =& $this->_replace_attributes[
$i];
00180 $err = $h->activate($g, $
this);
00181
if (PEAR::isError($err)) {
return $err; }
00182 unset($h);
00183 }
00184
return $this->
surroundFoot($g);
00185 }
00186
00187 $err = $this->
surroundHead($g);
00188
if (PEAR::isError($err)) {
return $err; }
00189
00190 $this->
printHead($g);
00191
00192 $err = $this->
generateContent($g);
00193
if (PEAR::isError($err)) {
return $err; }
00194
00195 $this->
printFoot($g);
00196
00197 $err = $this->
surroundFoot($g);
00198
if (PEAR::isError($err)) {
return $err; }
00199 }
00200
00201 function
printHead(&$g)
00202 {
00203
if ($this->
headFootDisabled())
return;
00204
00205 $g->doPrintString(
'<', $this->_name);
00206
00207 $this->
printAttributes($g);
00208
00209
if ($this->
hasContent() && !$this->
_isXHTMLEmptyElement()) {
00210 $g->doPrintString(
'>');
00211 }
else {
00212 $g->doPrintString('/>');
00213 }
00214 }
00215
00216 function
printFoot(&$g)
00217 {
00218
if ($this->
headFootDisabled())
return;
00219
00220
if ($this->
hasContent() && !$this->
_isXHTMLEmptyElement()) {
00221 $g->doPrintString('</', $this->_name,
'>');
00222 }
00223 }
00224
00225 function
printAttributes(&$g)
00226 {
00227 global
$_phptal_xhtml_boolean_attributes;
00228 foreach ($this->_attrs as $key=>$value) {
00229
if ($this->_parser->_outputMode() == PHPTAL_XHTML
00230 && in_array(strtolower($key), $_phptal_xhtml_boolean_attributes)) {
00231 $g->doPrintString(
" $key");
00232 }
else {
00233 $g->doPrintString(
" $key=\"$value\"");
00234 }
00235 }
00236 }
00237
00238 function
surroundHead(&$g)
00239 {
00240
00241
00242
for (
$i=0;
$i<count($this->_surround_attributes);
$i++) {
00243 $h =& $this->_surround_attributes[
$i];
00244 $err = $h->start($g, $
this);
00245
if (PEAR::isError($err)) {
return $err; }
00246 unset($h);
00247 }
00248 }
00249
00250 function
surroundFoot(&$g)
00251 {
00252
00253
for (
$i = (count($this->_surround_attributes)-1);
$i >= 0;
$i--) {
00254 $err = $this->_surround_attributes[
$i]->end($g, $
this);
00255
if (PEAR::isError($err)) {
return $err; }
00256 }
00257 }
00258
00259 function
generateContent(&$g)
00260 {
00261
if (count($this->_content_attributes) > 0) {
00262
00263 foreach ($this->_content_attributes as $h) {
00264 $err = $h->activate($g, $
this);
00265
if (PEAR::isError($err)) {
return $err; }
00266 }
00267 }
else {
00268
00269 foreach ($this->_children as $child) {
00270 $err = $child->generateCode($g);
00271
if (PEAR::isError($err)) {
return $err; }
00272 }
00273 }
00274 }
00275
00276 function
disableHeadFoot()
00277 {
00278 $this->_head_foot_disabled =
true;
00279 }
00280
00281 function
headFootDisabled()
00282 {
00283
return $this->_head_foot_disabled;print_r($this->_root);
00284 }
00285
00286 function
_isXHTMLEmptyElement()
00287 {
00288 global
$_phptal_xhtml_empty_tags;
00289
if ($this->_parser->_outputMode() != PHPTAL_XHTML) {
00290
return false;
00291 }
00292
return in_array(strtoupper($this->name()),
$_phptal_xhtml_empty_tags);
00293 }
00294 }
00295
00296 ?>