<?
/**
* PTagParser Class
*
* @author Masahiro.Kobayashi < masa918@mb.infoweb.ne.jp >
* @version 0.1
*
*/
class PTagParser
{
/**
* array to manage tag prefix
* @var array
*/
var $tagPrefix = array();
/**
* array to manage instance name
* @var array
* <form:select ..../> => $instances['form']['select'] = $_form_select;
*/
var $instances = array();
/**
* array to manage class name
* @var array
*/
var $classes = array();
/**
* @access private
*/
var $tagStack = array();
/**
* body content
*/
var $bodyContent = "";
/**
*
* tmp dir
*/
var $tmpDir = "/tmp";
/**
*
* set tag library infomation
*
* @access public
* @param $prefix string
* @param $tag string
* @param $className string
*/
function setTaglib($prefix,$tag,$className)
{
if(!in_array($prefix,$this->tagPrefix)){
$this->tagPrefix[] = $prefix;
}
// set to variable name
$var_name = $this->makeVarName($prefix,$tag);
$this->instances[$prefix][$tag] = $var_name;
$this->classes[$var_name] = $className;
}
/**
*
* create taglib instance variable name
*
* @param $prefix string
* @param $tag string
* @return string
* @access private
*/
function makeVarName($prefix,$tag)
{
return '$_taglib_'.$prefix.'_'.$tag;
}
/**
*
* create xml parser
*
* @access private
*/
function makeParser($encode = "UTF-8")
{
$parser = xml_parser_create($encode);
xml_parser_set_option($parser,XML_OPTION_CASE_FOLDING,0);
xml_set_object($parser,$this);
xml_set_element_handler($parser,"_open_element","_close_element");
xml_set_character_data_handler($parser,"_cdata");
xml_set_default_handler($parser,"_default");
//xml_set_processing_instruction_handler($parser,"_exec");
return $parser;
}
/**
*
* parse xml file
* @access public
*/
function parse($input,$output = null)
{
ob_start();
//
// create instance
//
$this->out("<?php\n");
foreach($this->tagPrefix as $prefix){
$tags = $this->instances[$prefix];
foreach($tags as $tag => $var_name){
$this->out("$var_name = new ".$this->classes[$var_name]."();\n");
}
}
$this->out("?>\n");
//
// start parse
//
$parser = $this->makeParser();
$fp = fopen($input,"r");
while($line = fgets($fp,1024)){
$line =& $this->input($line);
$line =& str_replace("&","&",$line);
if(!xml_parse($parser,$line,feof($fp))){
ob_end_clean();
die(htmlspecialchars($line));
}
}
$content = ob_get_contents();
ob_end_clean();
$fout = fopen($output,"w");
fputs($fout,$content);
fclose($fout);
fclose($fp);
xml_parser_free($parser);
include_once($output);
}
/**
*
* open tag
* @access private
*/
function _open_element($parser,$name,$attrs)
{
$this->out($this->bodyContent);
$this->bodyContent = "";
$pos = strpos($name,":");
if($pos !== false){
$prefix = substr($name,0,$pos);
$tag = substr($name,$pos+1);
$varname = $this->makeVarName($prefix,$tag);
if(isset($this->classes[$varname])){
if(is_array($this->tagStack[$prefix])){
$curTag = current($this->tagStack[$prefix]);
}
//
// add stack
//
$this->tagStack[$prefix][] = $tag;
$this->out("<?php\n");
//
// set parent
//
if($curTag != ""){
$this->out($varname."->setParent(".$this->makeVarName($prefix,$curTag).");\n");
}
foreach($attrs as $key => $value){
$this->out($varname."->set(\"$key\",\"$value\");\n");
}
$this->out($varname."->doStartTag();\n");
$this->out("?>");
}
}
else{
$this->out("<?php\n");
$this->out("print(\"<".$name."\");\n");
foreach($attrs as $key => $val ){
$this->out('print(" '.$key.'=\"'.$val.'\"'."\");\n");
}
$this->out("print \">\";");
$this->out("\n?>\n");
}
}
/**
*
* close tag
*
* @access private
* @see makeParser()
*/
function _close_element($parser,$name)
{
$pos = strpos($name,":");
if($pos !== false){
$prefix = substr($name,0,$pos);
$tag = substr($name,$pos+1);
$varname = $this->makeVarName($prefix,$tag);
if(isset($this->classes[$varname])){
//
// pop stack
//
if(is_array($this->tagStack[$prefix])){
$curtag = array_pop($this->tagStack[$prefix]);
}
$this->out("<?php\n");
$body = trim($this->bodyContent);
$this->out($varname."->doBody(\"".$body."\");\n");
$this->bodyContent = "";
$this->out($varname."->doEndTag();\n");
$this->out($varname."->release();\n");
$this->out("?>");
}
}
else{
$this->out($this->bodyContent);
$this->bodyContent = "";
$name = strtolower($name);
switch($name){
case "br":
case "hr":
case "input":
break;
default:
$this->out("</$name>");
}
}
}
/**
*
* cdata section
*
* @access private
* @see makeParser
*/
function _cdata($parser,$data)
{
$this->bodyContent .= $this->output($data);
}
/**
* default section
*
* @access private
* @see makeParser
*/
function _default($parser,$data)
{
$data = str_replace("&","&",$data);
$this->out($this->output($data));
}
/**
* output handler
*
* @access private
*/
function out($str)
{
print $str;
}
/**
* file input filter
*/
function input(&$str)
{
return $str;
// change EUC-JP to UTF-8
//return mb_convert_encoding($str,"UTF-8","EUC-JP");
}
/**
* file output filter
*/
function output(&$str)
{
return $str;
// change UTF-8 to EUC-JP
//return mb_convert_encoding($str,"EUC-JP","UTF-8");
}
}
//
// create instance
//
$ptagParser =& new PTagParser();
function setPTaglib($prefix,$tag,$className)
{
global $ptagParser;
$ptagParser->setTaglib($prefix,$tag,$className);
}
function ptagParse($input,$output)
{
global $ptagParser;
$ptagParser->parse($input,$output);
}
?>
|