Ejemplo n.º 1
0
 /**
  * $aObject 这是一个Node对象.它是模板引擎分析模板文件后的产品之一.Node对象包含了标签中的所有内容,包括Node的类型,内容,参数,等等,这些信息都是模板引擎分析模板得来.
  * 			比如这个if标签,你可以通过Node对象拿到它的源码,if的模板源码类似:
  * 			<if '(bool)$nTrue'>
  * 				<span>true</span>
  * 			</if>
  * 			也可以取得if标签的参数,if标签的参数就是上面源码中if后面的部分:
  * 			(bool)$nTrue
  * $aDev 输出设备,一般指网页
  * $aCompilerManager 编译管理器
  */
 public function compile(IObject $aObject, ObjectContainer $aObjectContainer, TargetCodeOutputStream $aDev, CompilerManager $aCompilerManager)
 {
     //确保传入的$aObject参数是node对象
     Type::check("org\\jecat\\framework\\ui\\xhtml\\Node", $aObject);
     //以下是编译过程
     //把<if>标签转换成php代码,也就是"if("
     //获得<if>标签中的条件语句,原封不动的放到if后面的括号中充当条件
     //但是这里并没有给代码块结尾,因为结尾在别的编译器中了,对于if标签来说,它的结尾工作放在</if>编译器那里了.是的,if标签是至少需要两个编译器才能完整编译
     $aDev->write('if(' . ExpressionCompiler::compileExpression($aObject->attributes()->anonymous()->source(), $aObjectContainer->variableDeclares()) . '){');
     /* 
      * 处理单行标签.单行格式是为了解决跨模板文件问题
      * if标签的多行格式是:
      * 			<if>
      * 			<else/>
      * 			</if>
      * 单行格式是
      * 			<if/>
      * 			<else/>
      * 			<if:end/>
      */
     if (!$aObject->headTag()->isSingle()) {
         $this->compileChildren($aObject, $aObjectContainer, $aDev, $aCompilerManager);
         $aDev->write("} ");
     }
 }
Ejemplo n.º 2
0
 public function compile(IObject $aObject, ObjectContainer $aObjectContainer, TargetCodeOutputStream $aDev, CompilerManager $aCompilerManager)
 {
     Type::check("org\\jecat\\framework\\ui\\xhtml\\Node", $aObject);
     $aDev->write('break ;');
     // 						 . ExpressionCompiler::compileExpression ( $aObject->attributes ()->source (), $aObjectContainer->variableDeclares() )
     // 						 . ';' );
 }
Ejemplo n.º 3
0
 public function compile(IObject $aObject, ObjectContainer $aObjectContainer, TargetCodeOutputStream $aDev, CompilerManager $aCompilerManager)
 {
     Type::check("org\\jecat\\framework\\ui\\xhtml\\Node", $aObject);
     $sLang = strtolower($aObject->attributes()->string('lang'));
     if (in_array($sLang, array('text/php', 'php')) and $aTailTag = $aObject->tailTag()) {
         // 编译头标签
         $this->compileTag($aObject->headTag(), $aObjectContainer, $aDev, $aCompilerManager);
         // 设置代码"上色器"
         $sVarName = parent::assignVariableName();
         $aDev->write("\r\n");
         $aDev->write("\${$sVarName} = new \\org\\jecat\\framework\\ui\\xhtml\\compiler\\node\\CodeColor() ;\r\n");
         $aDev->write("\\org\\jecat\\framework\\io\\StdOutputFilterMgr::singleton()->add(array(\${$sVarName},'outputFilter')) ;\r\n");
         $aDev->write("");
         // 编译 node body
         $this->compileChildren($aObject, $aObjectContainer, $aDev, $aCompilerManager);
         // 输出代码
         $aDev->write("\r\n");
         $aDev->write("\\org\\jecat\\framework\\io\\StdOutputFilterMgr::singleton()->remove( array(\${$sVarName},'outputFilter') ) ;\r\n");
         $aDev->write("\${$sVarName}->output(\$aDevice) ;");
         $aDev->write("");
         // 编译尾标签
         $this->compileTag($aTailTag, $aObjectContainer, $aDev, $aCompilerManager);
     } else {
         parent::compile($aObject, $aObjectContainer, $aDev, $aCompilerManager);
     }
 }
Ejemplo n.º 4
0
 public function compile(IObject $aObject, ObjectContainer $aObjectContainer, TargetCodeOutputStream $aDev, CompilerManager $aCompilerManager)
 {
     Type::check("org\\jecat\\framework\\ui\\xhtml\\Node", $aObject);
     $aAttributes = $aObject->attributes();
     if ($aAttributes->has("name")) {
         $sSubTemplateName = $aAttributes->string("name");
     } else {
         if (!($aNameVal = $aAttributes->anonymous())) {
             throw new Exception("subtemplate:define 节点缺少name属性(line:%d)", $aObject->line());
         }
         $sSubTemplateName = $aNameVal->source();
     }
     if (!is_callable($sSubTemplateName, true)) {
         throw new Exception("subtemplate:define 节点的name属性使用了无效的字符:%d", $sSubTemplateName);
     }
     $aDev->write("\r\n\r\n// -- subtemplate start ----------------------");
     $aDev->write("function __subtemplate_{$sSubTemplateName}(\$aVariables,\$aDevice){ ");
     // 准备 VariableDeclares
     $aOldVars = $aObjectContainer->variableDeclares();
     $aDeclareVariables = new VariableDeclares();
     $aObjectContainer->setVariableDeclares($aDeclareVariables);
     $aBuff = new OutputStreamBuffer();
     $aDev->write($aBuff);
     // 编译子对像
     $this->compileChildren($aObject, $aObjectContainer, $aDev, $aCompilerManager);
     // 声明用到的变量
     $aDeclareVariables->make($aBuff);
     $aObjectContainer->setVariableDeclares($aOldVars);
     $aDev->write("}// -- subtemplate end ----------------------\r\n\r\n");
 }
Ejemplo n.º 5
0
 public function compile(IObject $aObject, ObjectContainer $aObjectContainer, TargetCodeOutputStream $aDev, CompilerManager $aCompilerManager)
 {
     Type::check("org\\jecat\\framework\\ui\\xhtml\\Node", $aObject);
     $aAttributes = $aObject->attributes();
     if ($aAttributes->has("file")) {
         $sFileName = '"' . addslashes($aAttributes->string("file")) . '"';
     } else {
         if (!($aFileVal = $aAttributes->anonymous())) {
             throw new Exception("include 节点缺少file属性(line:%d)", $aObject->line());
         }
         $sFileName = '"' . addslashes($aFileVal->source()) . '"';
     }
     // 是否继承父模板中的变量
     $bExtendParentVars = $aAttributes->has("vars") ? $aAttributes->bool('vars') : true;
     // start
     $aDev->write("\r\n");
     // variables
     if (!$bExtendParentVars) {
         $aDev->write("\$__include_aVariables = new \\org\\jecat\\framework\\util\\DataSrc() ; \r\n");
         $aDev->write("\$__include_aVariables->addChild(\$aVariables) ;");
     } else {
         $aDev->write("\$__include_aVariables = \$aVariables ; \r\n");
     }
     // other variables
     foreach ($aAttributes as $sName => $aValue) {
         if (substr($sName, 0, 4) == 'var.' and $sVarName = substr($sName, 4)) {
             $sVarName = '"' . addslashes($sVarName) . '"';
             //$sValue = ExpressionCompiler::compileExpression($aValue->source(),$aObjectContainer->variableDeclares()) ;
             $sValue = $aAttributes->get($sName);
             $aDev->write("\$__include_aVariables->set({$sVarName},{$sValue}) ; \r\n");
         }
     }
     $aDev->write("\$this->display({$sFileName},\$__include_aVariables,\$aDevice) ; ");
 }
Ejemplo n.º 6
0
 public function compile(IObject $aObject, ObjectContainer $aObjectContainer, TargetCodeOutputStream $aDev, CompilerManager $aCompilerManager)
 {
     Type::check("org\\jecat\\framework\\ui\\xhtml\\Node", $aObject);
     $aDev->write(' }elseif(');
     $aDev->write(ExpressionCompiler::compileExpression($aObject->attributes()->anonymous()->source(), $aObjectContainer->variableDeclares()));
     $aDev->write("){ ");
     $this->compileChildren($aObject, $aObjectContainer, $aDev, $aCompilerManager);
 }
Ejemplo n.º 7
0
 public function remove($req)
 {
     if (is_string($req)) {
         parent::remove($req);
     } else {
         if ($req instanceof \org\jecat\framework\ui\xhtml\AttributeValue) {
             $this->removeByValue($req);
             // anonymous attribute
             $key = array_search($req, $this->arrAnonymous);
             if ($key !== false) {
                 unset($this->arrAnonymous[$key]);
             }
         } else {
             Type::check(array('string', __NAMESPACE__ . '\\AttributeValue'), $req);
         }
     }
 }
Ejemplo n.º 8
0
 public function compile(IObject $aObject, ObjectContainer $aObjectContainer, TargetCodeOutputStream $aDev, CompilerManager $aCompilerManager)
 {
     Type::check("org\\jecat\\framework\\ui\\xhtml\\Node", $aObject);
     if (!$aObjectContainer->variableDeclares()->hasDeclared('aStackForLoopIsEnableToRun')) {
         $aObjectContainer->variableDeclares()->declareVarible('aStackForLoopIsEnableToRun', 'new \\org\\jecat\\framework\\util\\Stack()');
     }
     $sIdxUserName = $aObject->attributes()->has('idx') ? $aObject->attributes()->get('idx') : '';
     $sIdxAutoName = NodeCompiler::assignVariableName('$__dowhile_idx_');
     if (!empty($sIdxUserName)) {
         $aDev->write(" {$sIdxAutoName} = -1; \$aStackForLoopIsEnableToRun->put(false);");
     }
     $aDev->write(" do{ \$bLoopIsEnableToRun = & \$aStackForLoopIsEnableToRun->getRef();\r\n\t\t\t\$bLoopIsEnableToRun = true;");
     if (!empty($sIdxUserName)) {
         $aDev->write(" {$sIdxAutoName}++; \r\n\t\t\t\t\t\t\t\$aVariables[{$sIdxUserName}]={$sIdxAutoName}; ");
     }
     $this->compileChildren($aObject, $aObjectContainer, $aDev, $aCompilerManager);
     $aDev->write(" }while(");
     $aDev->write(ExpressionCompiler::compileExpression($aObject->attributes()->anonymous()->source(), $aObjectContainer->variableDeclares()));
     $aDev->write(");");
 }
Ejemplo n.º 9
0
 public function compile(IObject $aObject, ObjectContainer $aObjectContainer, TargetCodeOutputStream $aDev, CompilerManager $aCompilerManager)
 {
     Type::check("org\\jecat\\framework\\ui\\xhtml\\Node", $aObject);
     $aAttributes = $aObject->attributes();
     if ($aAttributes->has("name")) {
         $sSubTemplateName = $aAttributes->string("name");
     } else {
         if (!($aSubTemplateNameVal = $aAttributes->anonymous())) {
             throw new Exception("subtemplate:define 节点缺少name属性(line:%d)", $aObject->line());
         }
         $sSubTemplateName = $aSubTemplateNameVal->source();
     }
     if (!is_callable($sSubTemplateName, true)) {
         throw new Exception("subtemplate:define 节点的name属性使用了无效的字符:%d", $sSubTemplateName);
     }
     $sSubTemplateFuncName = '__subtemplate_' . $sSubTemplateName;
     $aDev->write("\r\n// -- call subtemplate:{$sSubTemplateName} start---------------------");
     // 是否继承父模板中的变量
     $bExtendParentVars = $aAttributes->has("vars") ? $aAttributes->bool('vars') : false;
     // variables
     if (!$bExtendParentVars) {
         $aDev->write("\$__subtemplate_aVariables = new \\org\\jecat\\framework\\util\\DataSrc() ;");
         $aDev->write("\$__subtemplate_aVariables->addChild(\$aVariables) ;");
     } else {
         $aDev->write("\$__subtemplate_aVariables = \$aVariables ;");
     }
     // other variables
     foreach ($aAttributes as $sName => $aValue) {
         if (substr($sName, 0, 4) == 'var.' and $sVarName = substr($sName, 4)) {
             $sVarName = '"' . addslashes($sVarName) . '"';
             $sValue = ExpressionCompiler::compileExpression($aValue->source(), $aObjectContainer->variableDeclares());
             $aDev->write("\$__subtemplate_aVariables->set({$sVarName},{$sValue}) ;");
         }
     }
     $aDev->write("if( !function_exists('{$sSubTemplateFuncName}') ){");
     $aDev->write("\t\$aDevice->write(\"正在调用无效的子模板:{$sSubTemplateName}\") ;");
     $aDev->write("} else {");
     $aDev->write("\tcall_user_func_array('{$sSubTemplateFuncName}',array(\$__subtemplate_aVariables,\$aDevice)) ;");
     $aDev->write("}");
     $aDev->write("// -- call subtemplate:{$sSubTemplateName} end ---------------------\r\n\r\n");
 }
Ejemplo n.º 10
0
 public function compile(IObject $aObject, ObjectContainer $aObjectContainer, TargetCodeOutputStream $aDev, CompilerManager $aCompilerManager)
 {
     Type::check("org\\jecat\\framework\\ui\\xhtml\\Node", $aObject);
     $aAttrs = $aObject->attributes();
     $sType = strtolower($aAttrs->string('type'));
     if (in_array($sType, array('text/php', 'php'))) {
         foreach ($aObject->iterator() as $aChild) {
             if ($aChild instanceof AttributeValue) {
                 continue;
             }
             $aDev->write(ExpressionCompiler::compileExpression($aChild->source(), $aObjectContainer->variableDeclares(), false, true));
         }
     } else {
         if ($aAttrs->has('src') and !$aAttrs->bool('ignore')) {
             $sSrc = $aAttrs->get('src');
             $aDev->preprocessStream()->write("\\org\\jecat\\framework\\resrc\\HtmlResourcePool::singleton()->addRequire({$sSrc},\\org\\jecat\\framework\\resrc\\HtmlResourcePool::RESRC_JS) ;");
             // 清除后文中的空白字符
             ClearCompiler::clearAfterWhitespace($aObject);
         } else {
             parent::compile($aObject, $aObjectContainer, $aDev, $aCompilerManager);
         }
     }
 }
Ejemplo n.º 11
0
 public function compile(IObject $aObject, ObjectContainer $aObjectContainer, TargetCodeOutputStream $aDev, CompilerManager $aCompilerManager)
 {
     Type::check("org\\jecat\\framework\\ui\\xhtml\\Node", $aObject);
     $aDev->write("}");
 }
Ejemplo n.º 12
0
 public function setValue($data = null)
 {
     Type::check("org\\jecat\\framework\\fs\\File", $data);
     parent::setValue($data);
 }
Ejemplo n.º 13
0
 public function compile(IObject $aObject, ObjectContainer $aObjectContainer, TargetCodeOutputStream $aDev, CompilerManager $aCompilerManager)
 {
     if (!$aObjectContainer->variableDeclares()->hasDeclared('aStackForLoopIsEnableToRun')) {
         $aObjectContainer->variableDeclares()->declareVarible('aStackForLoopIsEnableToRun', 'new \\org\\jecat\\framework\\util\\Stack()');
     }
     Type::check("org\\jecat\\framework\\ui\\xhtml\\Node", $aObject);
     $aAttrs = $aObject->attributes();
     if ($aAttrs->has('for')) {
         $sForUserExp = $aAttrs->expression('for');
     } else {
         throw new Exception("foreach tag can not run without 'for' attribute");
     }
     $sKeyUserName = $aAttrs->has('key') ? $aAttrs->get('key') : '';
     $sItemUserName = $aAttrs->has('item') ? $aAttrs->get('item') : '';
     $bItemRef = $aAttrs->has('item.ref') ? $aAttrs->bool('item.ref') : false;
     $sIdxUserName = $aAttrs->has('idx') ? $aAttrs->get('idx') : '';
     $sForAutoName = NodeCompiler::assignVariableName('$__foreach_Arr_');
     $sItemAutoName = NodeCompiler::assignVariableName('$__foreach_item_');
     $sKeyAutoName = NodeCompiler::assignVariableName('$__foreach_key_');
     $sIdxAutoName = NodeCompiler::assignVariableName('$__foreach_idx_');
     $sItemRef = $bItemRef ? '&' : '';
     $aDev->write("\r\n// foreach start ");
     $aDev->write("{$sForAutoName} = {$sForUserExp};\r\n\$aStackForLoopIsEnableToRun->put(false);\r\n{$sIdxAutoName} = -1;\r\nforeach({$sForAutoName} as {$sKeyAutoName}=>{$sItemRef}{$sItemAutoName}){");
     $aDev->write("\$bLoopIsEnableToRun = & \$aStackForLoopIsEnableToRun->getRef();\r\n\t\$bLoopIsEnableToRun = true;\r\n\t{$sIdxAutoName}++;");
     if (!empty($sKeyUserName)) {
         $aDev->write("\t\t\$aVariables[{$sKeyUserName}]={$sKeyAutoName}; ");
     }
     if (!empty($sItemUserName)) {
         $aDev->write("\t\t\$aVariables[{$sItemUserName}]={$sItemAutoName}; ");
     }
     if (!empty($sIdxUserName)) {
         $aDev->write("\t\t\$aVariables[{$sIdxUserName}]={$sIdxAutoName}; ");
     }
     //是否是单行标签?
     if (!$aObject->headTag()->isSingle()) {
         //循环体,可能会包含foreach:else标签
         $this->compileChildren($aObject, $aObjectContainer, $aDev, $aCompilerManager);
         $aDev->write("}\r\n");
         // end if   (如果foreach的内容包含foreach:else标签,则此处为foreach:else的end)
     }
 }
Ejemplo n.º 14
0
 public function compile(IObject $aObject, ObjectContainer $aObjectContainer, TargetCodeOutputStream $aDev, CompilerManager $aCompilerManager)
 {
     Type::check("org\\jecat\\framework\\ui\\xhtml\\Node", $aObject);
     $aDev->write("\$aStackForLoopIsEnableToRun->out();\r\n\t\t\t\t\t\t}\r\n\t\t\t\t\t");
 }
Ejemplo n.º 15
0
 public function compile(IObject $aObject, ObjectContainer $aObjectContainer, TargetCodeOutputStream $aDev, CompilerManager $aCompilerManager)
 {
     Type::check("org\\jecat\\framework\\ui\\xhtml\\Node", $aObject);
     self::clearAfterWhitespace($aObject);
 }