oqlexception.class.inc.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. <?php
  2. // Copyright (C) 2010 Combodo SARL
  3. //
  4. // This program is free software; you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation; version 3 of the License.
  7. //
  8. // This program is distributed in the hope that it will be useful,
  9. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. // GNU General Public License for more details.
  12. //
  13. // You should have received a copy of the GNU General Public License
  14. // along with this program; if not, write to the Free Software
  15. // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  16. /**
  17. * Special handling for OQL syntax errors
  18. *
  19. * @author Erwan Taloc <erwan.taloc@combodo.com>
  20. * @author Romain Quetiez <romain.quetiez@combodo.com>
  21. * @author Denis Flaven <denis.flaven@combodo.com>
  22. * @license http://www.opensource.org/licenses/gpl-3.0.html LGPL
  23. */
  24. class OQLException extends CoreException
  25. {
  26. public function __construct($sIssue, $sInput, $iLine, $iCol, $sUnexpected, $aExpecting = null)
  27. {
  28. $this->m_MyIssue = $sIssue;
  29. $this->m_sInput = $sInput;
  30. $this->m_iLine = $iLine;
  31. $this->m_iCol = $iCol;
  32. $this->m_sUnexpected = $sUnexpected;
  33. $this->m_aExpecting = $aExpecting;
  34. if (is_null($this->m_aExpecting) || (count($this->m_aExpecting) == 0))
  35. {
  36. $sMessage = "$sIssue - found '{$this->m_sUnexpected}' at $iCol in '$sInput'";
  37. }
  38. else
  39. {
  40. $sExpectations = '{'.implode(', ', $this->m_aExpecting).'}';
  41. $sSuggest = self::FindClosestString($this->m_sUnexpected, $this->m_aExpecting);
  42. $sMessage = "$sIssue - found '{$this->m_sUnexpected}' at $iCol in '$sInput', expecting $sExpectations, I would suggest to use '$sSuggest'";
  43. }
  44. // make sure everything is assigned properly
  45. parent::__construct($sMessage, 0);
  46. }
  47. public function getHtmlDesc($sHighlightHtmlBegin = '<b>', $sHighlightHtmlEnd = '</b>')
  48. {
  49. $sRet = htmlentities($this->m_MyIssue.", found '".$this->m_sUnexpected."' in: ");
  50. $sRet .= htmlentities(substr($this->m_sInput, 0, $this->m_iCol));
  51. $sRet .= $sHighlightHtmlBegin.htmlentities(substr($this->m_sInput, $this->m_iCol, strlen($this->m_sUnexpected))).$sHighlightHtmlEnd;
  52. $sRet .= htmlentities(substr($this->m_sInput, $this->m_iCol + strlen($this->m_sUnexpected)));
  53. if (!is_null($this->m_aExpecting) && (count($this->m_aExpecting) > 0))
  54. {
  55. $sExpectations = '{'.implode(', ', $this->m_aExpecting).'}';
  56. $sRet .= ", expecting ".htmlentities($sExpectations);
  57. $sSuggest = self::FindClosestString($this->m_sUnexpected, $this->m_aExpecting);
  58. if (strlen($sSuggest) > 0)
  59. {
  60. $sRet .= ", I would suggest to use '$sHighlightHtmlBegin".htmlentities($sSuggest)."$sHighlightHtmlEnd'";
  61. }
  62. }
  63. return $sRet;
  64. }
  65. static protected function FindClosestString($sInput, $aDictionary)
  66. {
  67. // no shortest distance found, yet
  68. $fShortest = -1;
  69. $sRet = '';
  70. // loop through words to find the closest
  71. foreach ($aDictionary as $sSuggestion)
  72. {
  73. // calculate the distance between the input string and the suggested one
  74. $fDist = levenshtein($sInput, $sSuggestion);
  75. if ($fDist == 0)
  76. {
  77. // Exact match
  78. return $sSuggestion;
  79. }
  80. if ($fShortest < 0 || ($fDist < 4 && $fDist <= $fShortest))
  81. {
  82. // set the closest match, and shortest distance
  83. $sRet = $sSuggestion;
  84. $fShortest = $fDist;
  85. }
  86. }
  87. return $sRet;
  88. }
  89. }
  90. ?>