validate.php

Go to the documentation of this file.
00001 <?php
00002   /*
00003    This file is part of DocBookWiki.  DocBookWiki is a web application
00004    that displays and edits DocBook documents.
00005 
00006    Copyright (C) 2004, 2005, 2006, 2007
00007    Dashamir Hoxha, dashohoxha@users.sourceforge.net
00008 
00009    DocBookWiki is free software; you can redistribute it and/or modify
00010    it under the  terms of the GNU General  Public License as published
00011    by the Free  Software Foundation; either version 2  of the License,
00012    or (at your option) any later version.
00013 
00014    DocBookWiki is distributed in the  hope that it will be useful, but
00015    WITHOUT  ANY  WARRANTY;  without   even  the  implied  warranty  of
00016    MERCHANTABILITY or  FITNESS FOR A PARTICULAR PURPOSE.   See the GNU
00017    General Public License for more details.
00018 
00019    You should have  received a copy of the  GNU General Public License
00020    along  with  DocBookWiki;  if  not,  write  to  the  Free  Software
00021    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
00022    USA
00023   */
00024 
00030 function validate_xml($xml_content)
00031 {
00032   //write the docbook content to a temporary file
00033   $tmpfile = write_tmp_file($xml_content);
00034 
00035   //validate the temporary xml file
00036   $fpi = '-//OASIS//DTD DocBook XML V4.2//EN';
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   //remove any xref errors, if the section ID exists
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   //check for any errors
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       //extract the $line_nr etc. from the error line
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       //extract from the xml line a part around the error
00099       $xml_line = $arr_xml_lines[$line_nr - 1];
00100       $short_xml_line = get_short_xml_line($xml_line, $elem);
00101       
00102       //output the error line and the xml line
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 ?>

Generated on Wed Jan 9 08:27:32 2008 for DokBookWiki by  doxygen 1.5.2