Revision: 843
http://openautomation.svn.sourceforge.net/openautomation/?rev=843&view=rev
Author: mayerch
Date: 2012-05-28 13:39:44 +0000 (Mon, 28 May 2012)
Log Message:
-----------
Highly improved display of config file XML errors
Modified Paths:
--------------
CometVisu/trunk/visu/check_config.php
Modified: CometVisu/trunk/visu/check_config.php
===================================================================
--- CometVisu/trunk/visu/check_config.php 2012-05-28 07:13:31 UTC (rev 842)
+++ CometVisu/trunk/visu/check_config.php 2012-05-28 13:39:44 UTC (rev 843)
@@ -7,20 +7,104 @@
<body>
<?php
+$error_array = array();
+
+// this function was inspired by:
+// http://www.php.net/manual/en/function.highlight-string.php#84661
+function xml_highlight($s)
+{
+ $s = htmlspecialchars($s);
+ $s = preg_replace("#<([/]*?)(.*)([\s]*?)>#sU",
+ "<font color=\"#0000FF\"><\\1\\2\\3></font>",$s);
+ $s = preg_replace("#<([\?])(.*)([\?])>#sU",
+ "<font color=\"#800000\"><\\1\\2\\3></font>",$s);
+ $s = preg_replace("#<([^\s\?/=])(.*)([\[\s/]|>)#iU",
+ "<<font color=\"#808000\">\\1\\2</font>\\3",$s);
+ $s = preg_replace("#<([/])([^\s]*?)([\s\]]*?)>#iU",
+ "<\\1<font color=\"#808000\">\\2</font>\\3>",$s);
+ $s = preg_replace("#([^\s]*?)\=("|')(.*)("|')#isU",
+ "<font color=\"#800080\">\\1</font>=<font color=\"#FF00FF\">\\2\\3\\4</font>",$s);
+ $s = preg_replace("#<(.*)(\[)(.*)(\])>#isU",
+ "<\\1<font color=\"#800080\">\\2\\3\\4</font>>",$s);
+ return preg_replace( '#<br */>$#', '', nl2br($s) );
+}
+
+function libxml_display_error( $error )
+{
+ global $lines, $error_array;
+
+ $error_array[] = $error->line;
+
+ switch ($error->level)
+ {
+ case LIBXML_ERR_WARNING:
+ $return .= '<b>Warning ' . $error->code . '</b>: ';
+ break;
+ case LIBXML_ERR_ERROR:
+ $return .= '<b>Error ' . $error->code . '</b>: ';
+ break;
+ case LIBXML_ERR_FATAL:
+ $return .= '<b>Fatal Error ' . $error->code . '</b>: ';
+ break;
+ }
+
+ $return .= trim( $error->message );
+ $return .= ' on <a href="#' . ($error->line-1) . '">line <b>' . $error->line . '</b></a>';
+
+ $return .= '<pre>';
+ for( $i = max( 0, $error->line - 1 - 3); $i <= $error->line - 1 + 3; $i++ )
+ {
+ if( $i == $error->line - 1 ) $return .= '<b>';
+ $return .= sprintf( '%4d: ', $i+1 );
+ $return .= xml_highlight( $lines[ $i ] );
+ if( $i == $error->line - 1 ) $return .= '</b>';
+ }
+ $return .= '</pre>';
+
+ return $return;
+}
+
+// Enable user error handling
+libxml_use_internal_errors(true);
+
$dom = new DomDocument();
-$conffile = "visu_config";
+$conffile = 'visu_config';
if ($_GET['config']) {
- $conffile .= "_" . $_GET['config'];
+ $conffile .= "_" . $_GET['config'];
}
+$conffile .= '.xml';
-$dom->load($conffile . ".xml");
+$lines = file( $conffile );
+$dom->load( $conffile );
-if ($dom->schemaValidate("visu_config.xsd")) {
- print ("config <b>" . $conffile . " is valid </b> XML");
+if( $dom->schemaValidate( 'visu_config.xsd' ) )
+{
+ print ("config <b>" . $conffile . " is valid </b> XML");
} else {
- print ("config <b>" . $conffile . " is NOT </b> valid XML");
+ print ("config <b>" . $conffile . " is NOT </b> valid XML");
+
+ echo '<hr />';
+
+ $errors = libxml_get_errors();
+ foreach( $errors as $error )
+ {
+ echo libxml_display_error( $error );
+ }
+ libxml_clear_errors();
}
+echo '<hr />';
+
+echo '<pre>';
+foreach( $lines as $line_num => $line )
+{
+ $error_in_line = in_array( $line_num+1, $error_array );
+ if( $error_in_line ) echo '<b>';
+ printf( '<a name="%s">%4d</a>: ', $line_num, $line_num+1 );
+ echo xml_highlight( $line );
+ if( $error_in_line ) echo '</b>';
+}
+echo '</pre>';
?>
</body>
</html>
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|