oqlexception.class.inc.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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 = '<span style="font-weight: bolder">', $sHighlightHtmlEnd = '</span>')
  48. {
  49. $sRet = htmlentities($this->m_MyIssue.", found '".$this->m_sUnexpected."' in: ", ENT_QUOTES, 'UTF-8');
  50. $sRet .= htmlentities(substr($this->m_sInput, 0, $this->m_iCol), ENT_QUOTES, 'UTF-8');
  51. $sRet .= $sHighlightHtmlBegin.htmlentities(substr($this->m_sInput, $this->m_iCol, strlen($this->m_sUnexpected)), ENT_QUOTES, 'UTF-8').$sHighlightHtmlEnd;
  52. $sRet .= htmlentities(substr($this->m_sInput, $this->m_iCol + strlen($this->m_sUnexpected)), ENT_QUOTES, 'UTF-8');
  53. if (!is_null($this->m_aExpecting) && (count($this->m_aExpecting) > 0))
  54. {
  55. $sExpectations = '{'.implode(', ', $this->m_aExpecting).'}';
  56. $sRet .= ", expecting ".htmlentities($sExpectations, ENT_QUOTES, 'UTF-8');
  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, ENT_QUOTES, 'UTF-8')."$sHighlightHtmlEnd'";
  61. }
  62. }
  63. return $sRet;
  64. }
  65. public function GetIssue()
  66. {
  67. return $this->m_MyIssue;
  68. }
  69. public function GetSuggestions()
  70. {
  71. return $this->m_aExpecting;
  72. }
  73. public function GetWrongWord()
  74. {
  75. return $this->m_sUnexpected;
  76. }
  77. public function GetColumn()
  78. {
  79. return $this->m_iCol;
  80. }
  81. static public function FindClosestString($sInput, $aDictionary)
  82. {
  83. // no shortest distance found, yet
  84. $fShortest = -1;
  85. $sRet = '';
  86. // loop through words to find the closest
  87. foreach ($aDictionary as $sSuggestion)
  88. {
  89. // calculate the distance between the input string and the suggested one
  90. $fDist = levenshtein($sInput, $sSuggestion);
  91. if ($fDist == 0)
  92. {
  93. // Exact match
  94. return $sSuggestion;
  95. }
  96. if (($fDist <= 3) && ($fShortest < 0 || $fDist <= $fShortest))
  97. {
  98. // set the closest match, and shortest distance
  99. $sRet = $sSuggestion;
  100. $fShortest = $fDist;
  101. }
  102. }
  103. return $sRet;
  104. }
  105. }
  106. ?>