PageRenderTime 51ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/Tal/Parser/Xml/Exception.php

https://github.com/drslump/DrTal
PHP | 177 lines | 40 code | 9 blank | 128 comment | 0 complexity | 0f6db43ccb3f04c937b94f34cffa1f1c MD5 | raw file
  1. <?php
  2. namespace DrSlump\Tal\Parser\Xml;
  3. use DrSlump\Tal\Parser;
  4. class Exception extends Parser\Exception
  5. {
  6. protected $xml;
  7. protected $_errors = array();
  8. public function setXml( $xml )
  9. {
  10. $this->xml = $xml;
  11. }
  12. public function getXml()
  13. {
  14. return $this->xml;
  15. }
  16. public function addWarning( $ln, $col, $code, $message )
  17. {
  18. $this->_errors[] = array(
  19. 'level' => 'warning',
  20. 'ln' => $ln,
  21. 'col' => $col,
  22. 'code' => $code,
  23. 'msg' => $message
  24. );
  25. }
  26. public function addError( $ln, $col, $code, $message )
  27. {
  28. $this->_errors[] = array(
  29. 'level' => 'error',
  30. 'ln' => $ln,
  31. 'col' => $col,
  32. 'code' => $code,
  33. 'msg' => $message
  34. );
  35. }
  36. public function getErrors()
  37. {
  38. return $this->_errors;
  39. }
  40. }
  41. /*
  42. $levels = array(
  43. LIBXML_ERR_WARNING => 'Warning',
  44. LIBXML_ERR_ERROR => 'Error',
  45. LIBXML_ERR_FATAL => 'Fatal',
  46. );
  47. // First aggregate on line+column numbers
  48. $aggregated = array();
  49. foreach ($errors as $error) {
  50. $key = ($error->line-1) . '-' . $error->column;
  51. $err = array(
  52. 'level' => $error->level,
  53. 'code' => $error->code,
  54. 'msg' => $error->message,
  55. );
  56. if ( isset( $aggregated[$key] ) )
  57. $aggregated[$key][] = $err;
  58. else
  59. $aggregated[$key] = array($err);
  60. }
  61. // Convert the template to an array of lines
  62. $lines = preg_split( '/\r\n|\n|\r/', $this->tplString );
  63. $numLinesToShow = 5;
  64. foreach ( $aggregated as $key=>$errors ) {
  65. list( $ln, $col ) = explode('-', $key);
  66. $prevLn = max( 0, $ln - floor($numLinesToShow/2) );
  67. $postLn = min( count($lines), $ln + ceil($numLinesToShow/2) );
  68. echo '
  69. <style>
  70. thead td {
  71. font-family: sans-serif;
  72. font-size: 120%;
  73. background: black;
  74. color: white;
  75. }
  76. th{ text-align: right }
  77. td{
  78. font-family: Monospace;
  79. padding-left: .5em;
  80. }
  81. .current{
  82. background: #ecc;
  83. }
  84. .current th{background:#caa;}
  85. .current td {
  86. font-weight: bold;
  87. font-size: 120%;
  88. }
  89. .even{background:#eee}
  90. .even th{ background: #ccc }
  91. .odd{background:#ddd}
  92. .odd th { background: #bbb }
  93. .marker {
  94. background: #666;
  95. color: #FF3300;
  96. font-size: 120%;
  97. font-weight: bold;
  98. height: 10px;
  99. line-height: 10px;
  100. overflow: hidden;
  101. }
  102. .marker span {
  103. font-size: 80%;
  104. color: #ddd;
  105. }
  106. </style>';
  107. echo '
  108. <table width="100%" cellspacing="0" cellpadding="2">
  109. <thead>';
  110. foreach ( $errors as $err ) {
  111. echo '
  112. <tr>
  113. <td colspan="2">
  114. ' . "{$levels[$err['level']]} #{$err['code']}: <em>" . htmlentities($err['msg']) . '</em>
  115. </td>
  116. </tr>';
  117. }
  118. echo '
  119. </thead>
  120. <tbody>';
  121. for ( $i=$prevLn; $i<$postLn; $i++ ) {
  122. echo '<tr class="' . ($i==$ln ? 'current' : ($i%2 ? 'odd' : 'even')) . '">';
  123. echo '<th>' . $i . '</th>';
  124. echo '<td>' . htmlentities($lines[$i]) . '</td>';
  125. echo '</tr>';
  126. if ( $i == $ln ) {
  127. echo '<tr class="marker">';
  128. echo '<th>&nbsp;</th>';
  129. echo '<td>' . str_repeat('-', $col) . '^ <span>(Col: ' . $col . ')</span></td>';
  130. echo '</tr>';
  131. }
  132. }
  133. echo '
  134. </tbody>
  135. </table>';
  136. break;
  137. $line = $lines[$error->line-1];
  138. $trimmed = ltrim($line);
  139. $error->column = $error->column + ( strlen($line)-strlen($trimmed) );
  140. if ( $error->code == 68 ) {
  141. $error->column = strrpos( substr($line, 0, $error->column), '<' );
  142. }
  143. echo "<h3>" . $levels[$error->level] . " #{$error->code} at line {$error->line} column {$error->column}</h3>";
  144. echo "<pre>";
  145. echo htmlentities( rtrim($lines[$error->line-1]) . PHP_EOL );
  146. echo str_repeat( '-', $error->column ) . '^';
  147. echo "</pre>";
  148. echo "<p>Message: <em>" . htmlentities($error->message) . "</em></p>";
  149. }
  150. */