00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00030 function validate_xml($xml_content)
00031 {
00032
00033 $tmpfile = write_tmp_file($xml_content);
00034
00035
00036 $fpi = '-
00037 $xmllint = "xmllint --noout --dtdvalidfpi '$fpi' --nonet --nowarning";
00038 $sed = 'sed -e \$d -e /warning:/d';
00039 $err_lines = shell("$xmllint $tmpfile 2>&1 | $sed");
00040
00041 unlink($tmpfile);
00042
00043
00044 $pattern = '/IDREF attribute linkend references an unknown ID "([^"]*)"/';
00045 if (preg_match_all($pattern, $err_lines, $matches))
00046 {
00047 //get an array of error section ID-s
00048 $xref_ids = $matches[1];
00049
00050 //get a list of all section id-s of the document
00051 $id_list = process_index_node('get_id_list');
00052 $doc_ids = explode("\n", $id_list);
00053
00054 //remove from error lines those lines
00055 //that have an id that exists in the document
00056 for ($i=0; $i < sizeof($xref_ids); $i++)
00057 {
00058 $id = $xref_ids[$i];
00059 if (in_array($id, $doc_ids))
00060 {
00061 $pattern = "/^.*IDREF attribute linkend references an unknown ID \"$id\"\$\n?/m";
00062 $err_lines = preg_replace($pattern, '', $err_lines);
00063 }
00064 }
00065 }
00066
00067
00068 if (trim($err_lines) != '')
00069 {
00070 display_error_messages($xml_content, $err_lines);
00071 return false;
00072 }
00073
00074 return true;
00075 }
00076
00078 function display_error_messages(&$xml_content, &$err_lines)
00079 {
00080 $arr_xml_lines = explode("\n", $xml_content);
00081 $arr_err_lines = explode("\n", $err_lines);
00082
00083 $message = "<ul>\n";
00084 for ($i=0; $i < sizeof($arr_err_lines); $i++)
00085 {
00086 $err_line = $arr_err_lines[$i];
00087 if (trim($err_line)=='') continue;
00088
00089
00090 $arr_fields = explode(':', $err_line);
00091 $line_nr = $arr_fields[1];
00092 $elem = $arr_fields[2];
00093 $elem = str_replace('element', '', $elem);
00094 $elem = trim($elem);
00095 $err_msg = $arr_fields[4];
00096 $err_msg = ereg_replace(', expecting.*', '', $err_msg);
00097
00098
00099 $xml_line = $arr_xml_lines[$line_nr - 1];
00100 $short_xml_line = get_short_xml_line($xml_line, $elem);
00101
00102
00103 $message .= "<li><span class='err_line'>$err_msg:</span> \n";
00104 $message .= "<span class='xml_line'>$short_xml_line</span></li>\n";
00105 }
00106 $message .= "</ul>\n";
00107
00108 popup_window("Validation Errors", $message);
00109 }
00110
00112 function get_short_xml_line($xml_line, $elem)
00113 {
00114 $pattern = "#</?$elem.*?>#";
00115
00116 preg_match_all($pattern, $xml_line, $arr_tags);
00117 $arr_chunks = preg_split($pattern, $xml_line);
00118
00119
00120 $tag0 = $arr_tags[0][0];
00121 $tag0 = '<strong>' . htmlspecialchars($tag0) . '</strong>';
00122
00123 $chunk0 = $arr_chunks[0];
00124 $chunk0 = substr($chunk0, -20);
00125 if (strlen($chunk0) < strlen($arr_chunks[0])) $chunk0 = '...' . $chunk0;
00126 $chunk0 = htmlspecialchars($chunk0);
00127
00128 $chunk1 = $arr_chunks[1];
00129 $chunk1 = substr($chunk1, -20);
00130 if (strlen($chunk1) < strlen($arr_chunks[1])) $chunk1 = $chunk1 . '...';
00131 $chunk1 = htmlspecialchars($chunk1);
00132
00133 $short_xml_line = $chunk0 . $tag0 . $chunk1;
00134
00135 if (sizeof($arr_tags[0]) > 1)
00136 {
00137 $tag1 = $arr_tags[0][1];
00138 $tag1 = '<strong>' . htmlspecialchars($tag1) . '</strong>';
00139
00140 $chunk2 = $arr_chunks[2];
00141 $chunk2 = substr($chunk2, -20);
00142 if (strlen($chunk2) < strlen($arr_chunks[2])) $chunk2 = $chunk2 . '...';
00143 $chunk2 = htmlspecialchars($chunk2);
00144
00145 $short_xml_line .= $tag1 . $chunk2;
00146 }
00147
00148 return $short_xml_line;
00149 }
00150 ?>