1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
|
<?php
include './settings.php';
class PageParser
{
var $buf; // CDATA buffer
var $out; // Output buffer
var $page; // Array containing filenames being used
function PageParser()
{
$this->buf = '';
$this->out = Array();
}
function openElement($p,$name,$attrs)
{
switch ($name)
{
case 'news':
$this->out[] = "<u>News</u>\n<br>\n" .
'<table border="0" style="padding: 4px" cellspacing="0">' .
"\n";
break;
case 'item':
$this->out[] = "\t<tr>\n";
break;
case 'date':
$this->out[] = "\t\t<td class=\"d-dark\">\n";
break;
case 'title':
$this->out[] = "\t\t<td class=\"d-cell\">\n";
break;
case 'content':
$this->out[] = "\t\t\t<br>\n";
break;
default:
// We're ignoring elements we don't recognize
}
}
function closeElement($p,$name)
{
if (strlen($this->buf))
{
switch ($name)
{
case 'date':
// TODO: A regexp to see if it's in YYYY-MM-DD format, and
// optionally rework it to Month Day, Year format
$this->out[] = "\t\t\t$this->buf\n";
break;
case 'title':
$this->out[] = "\t\t\t<b>$this->buf</b>\n";
break;
case 'content':
$this->out[] = $this->buf . "\n";
break;
default:
// We're ignoring CDATA that isn't
// in an element we care about
}
$this->buf = '';
}
switch ($name)
{
case 'news':
$this->out[] = "</table>\n";
break;
case 'item':
$this->out[] = "\t</tr>\n";
break;
case 'date':
$this->out[] = "\t\t</td>\n";
break;
case 'title':
break;
case 'content':
$this->out[] = "\t\t</td>\n";
break;
default:
// We're ignoring elements we don't recognize
}
}
function readCharacterData($p,$data)
{
$this->buf .= chop($data);
}
function readHeader()
{
if (isset($this->page['headerfile']) && is_file($this->page['headerfile']))
{
$arr = file($this->page['headerfile']);
// array_merge seems to be a suspect of rearranging array keys.
// Since I don't care enough to investigate now, we'll do it
// by hand. This way we know it's doing what we want.
foreach ($arr as $line)
$this->out[] = $line;
}
}
function readFooter()
{
if (isset($this->page['footerfile']) && is_file($this->page['footerfile']))
{
$arr = file($this->page['footerfile']);
foreach ($arr as $line)
$this->out[] = $line;
}
}
function setPage($page)
{
$this->page = $page;
}
};
$pobj = new PageParser;
$p = xml_parser_create();
xml_parser_set_option($p,XML_OPTION_CASE_FOLDING,0);
xml_set_object($p,&$pobj);
xml_set_character_data_handler($p, 'readCharacterData');
xml_set_element_handler($p,'openElement','closeElement');
foreach ($pages as $page)
{
$pobj->setPage($page);
$pobj->readHeader();
if (!$fp = @fopen($page['xmlfile'],'r'))
die("Could not open XML data file {$page['xmlfile']}. Exiting.");
if (!$outfp = @fopen($page['outputfile'],'w'))
die("Could not open output file {$page['outputfile']}. Exiting.");
while ($data = fread($fp, 4096))
{
if (!xml_parse($p, $data, feof($fp)))
die(sprintf("XML error: %s at line %d\n",
xml_error_string(xml_get_error_code($p)),
xml_get_current_line_number($p)));
}
$pobj->readFooter();
foreach ($pobj->out as $line)
fwrite($outfp,$line);
fclose($fp);
fclose($outfp);
}
xml_parser_free($p);
?>
|