PageRenderTime 98ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/nwacko/classes/JavaHighlighter.php

http://interra.googlecode.com/
PHP | 268 lines | 151 code | 18 blank | 99 comment | 38 complexity | 91fb6ef9a8ed1547bd8a3d15db6ab7a7 MD5 | raw file
  1. <?php
  2. /**
  3. * Java Syntax Highlighting
  4. ******************************
  5. * Port to java highlighting (c) Mark Hissink Muller, 2004
  6. ******************************
  7. * D'apres le code originale de FEREY Damien et Dark Skull Software
  8. * publie sur http://www.phpcs.com/article.aspx?Val=649
  9. * Modifie par Eric Feldstein (mise sous forme de classe et adapte a WikiNi)
  10. ******************************
  11. * Peut facilement etre adapte pour d'autres langages (vb, c, c++...)
  12. * Il suffit de modifier le contenu des variables
  13. *
  14. * @version 1.0
  15. * @copyright FEREY Damien 23/06/2003
  16. * @copyright Dark Skull Software
  17. * http://www.darkskull.net
  18. *
  19. *
  20. **/
  21. class JavaHighlighter{
  22. var $code = ''; //the code to be hightlighed
  23. var $newcode = ''; //the generated code
  24. var $tok; // Le mot en train d'etre decoupe
  25. var $char; // Le caractere en cours
  26. var $i; // La position en cours dans le code
  27. var $codelength; // La longueur de la chaine de code
  28. /****************************************************************/
  29. /* Les variables qui definissent le comportement de l'analyseur */
  30. /****************************************************************/
  31. var $case_sensitive = true; // Langage sensible a la case ou pas
  32. var $tokdelimiters = " []()=+-/*:;,.\n\t\r "; // Les delimiteurs de mots
  33. /***************************************************/
  34. /* Les couleurs associees a chaque type de donnees */
  35. /***************************************************/
  36. var $colorkeyword = "#0000CC";
  37. var $colortext = "";
  38. var $colorstring = "#000000";
  39. var $colorcomment = "#006600";
  40. var $colorsymbol = "";
  41. var $colornumber = "#000080";
  42. var $colorpreproc = "#008000";
  43. /*************************************************/
  44. /* Les styles donnes pour chaque type de donnees */
  45. /*************************************************/
  46. var $stylekeyword = array("<b>", "</b>");
  47. var $styletext = array("", "");
  48. //var $stylestring = array("<span style=\"background-color:yellow\">", "</span>");
  49. var $stylestring = array("","");
  50. var $stylecomment = array("<i>", "</i>");
  51. var $stylesymbol = array("", "");
  52. var $stylenumber = array("", "");
  53. var $stylepreproc = array("<i>", "</i>");
  54. /*****************/
  55. /* Keywords */
  56. /*****************/
  57. var $keywords = array(
  58. 'abstract','double','double','strictfp','boolean','else',
  59. 'interface','super','break','extends','long','switch','byte','final','native',
  60. 'synchronized','case','finally','new','this','catch','float','package','throw','char','for',
  61. 'protected','public','published','record','packed','case','of','const','array',
  62. 'private','throws','class','goto','protected','transient','const','if','public','try',
  63. 'constructor','destructor','library','set','inherited','object','overload',
  64. 'continue','implements','return','void','default','import','short','volatile',
  65. 'do','instanceof','static','while');
  66. /***********************************/
  67. /* Delimiters for comment */
  68. /***********************************/
  69. var $commentdelimiters = array(
  70. array("//", "\n"),
  71. array("/*", "*/"),
  72. array("/**", "*/")
  73. );
  74. /********************************************/
  75. /* Delimiters for Strings */
  76. /********************************************/
  77. var $stringdelimiters = array(
  78. array("\"", "\"")
  79. );
  80. /********************************************************/
  81. /* Delimiters for pre-processor-instructions */
  82. /********************************************************/
  83. var $preprocdelimiters = array(
  84. array("(*\$", "*)"),
  85. array("{\$", "}")
  86. );
  87. /////////////////////////////////////////////////////////////////////////////////////////
  88. // Le code en lui-meme
  89. /////////////////////////////////////////////////////////////////////////////////////////
  90. /************************************************************************/
  91. /* Renvoie vrai si un caractere est visible et peut etre mis en couleur */
  92. /************************************************************************/
  93. function visiblechar($char) {
  94. $inviblechars = " \t\n\r ";
  95. return (!is_integer(strpos($inviblechars, $char)));
  96. }
  97. /************************************************************/
  98. /* Formatte un mot d'une maniere speciale (couleur + style) */
  99. /************************************************************/
  100. function formatspecialtok($tok, $color, $style)
  101. {
  102. if (empty($color)) return sprintf("%s$tok%s", $style[0], $style[1]);
  103. return sprintf("%s<font color=\"%s\">$tok</font>%s", $style[0], $color, $style[1]);
  104. }
  105. /*******************************************************************/
  106. /* Recherche un element dans un tableau sans se soucier de la case */
  107. /*******************************************************************/
  108. function array_search_case($needle, $array)
  109. {
  110. if (!is_array($array)) return FALSE;
  111. if (empty($array)) return FALSE;
  112. foreach($array as $index=>$string)
  113. if (strcasecmp($needle, $string) == 0) return intval($index);
  114. return FALSE;
  115. }
  116. /*****************************************************/
  117. /* Analyse un mot et le renvoie de maniere formattee */
  118. /*****************************************************/
  119. function analyseword($tok)
  120. {
  121. // Si c'est un nombre
  122. if (($tok[0] == '$') || ($tok[0] == '#') || ($tok == (string)intval($tok)))
  123. return $this->formatspecialtok($tok, $this->colornumber, $this->stylenumber);
  124. // Si c'est vide, on renvoie une chaine vide
  125. if (empty($tok)) return $tok;
  126. // Si c'est un mot cle
  127. if ((($this->case_sensitive) && (is_integer(array_search($tok, $this->keywords, FALSE)))) ||
  128. ((!$this->case_sensitive) && (is_integer($this->array_search_case($tok, $this->keywords)))))
  129. return $this->formatspecialtok($tok, $this->colorkeyword, $this->stylekeyword);
  130. // Sinon, on renvoie le mot sans formattage
  131. return $this->formatspecialtok($tok, $this->colortext, $this->styletext);
  132. }
  133. /***************************************************/
  134. /* On regarde si on ne tombe pas sur un delimiteur */
  135. /***************************************************/
  136. function parsearray($array, $color = "#000080", $style = array("<i>", "</i>"))
  137. {
  138. // On effectue quelques verifications
  139. if (!is_array($array)) return FALSE;
  140. if (!strlen($this->code)) return FALSE;
  141. if (!sizeof($array)) return FALSE;
  142. // On va essayer de comparer le caractere courrant avec le 1?
  143. // caractere de chaque premier delimiteur
  144. foreach($array as $delimiterarray) {
  145. $delimiter1 = $delimiterarray[0];
  146. // Si le 1? char correspond
  147. if ($this->char == $delimiter1[0]) {
  148. $match = TRUE;
  149. // On va tenter de comparer tous les autres caracteres
  150. // Pour verifier qu'on a bien le delimiteur complet
  151. for ($j = 1; ($j < strlen($delimiter1)) && $match; $j++) {
  152. $match = ($this->code[$this->i + $j] == $delimiter1[$j]);
  153. } // for
  154. // Si on l'a en entier
  155. if ($match) {
  156. $delimiter2 = $delimiterarray[1];
  157. // Alors on recherche le delimiteur de fin
  158. $delimiterend = strpos($this->code, $delimiter2, $this->i + strlen($delimiter1));
  159. // Si on ne trouve pas le delimiteur de fin, on prend tout le fichier
  160. if (!is_integer($delimiterend)) $delimiterend = strlen($this->code);
  161. // Maintenant qu'on a tout, on analyse le mot avant le delimiteur, s'il existe
  162. if (!empty($this->tok)) {
  163. $this->newcode .= $this->analyseword($this->tok);
  164. $this->tok = "";
  165. }
  166. // Ensuite, on place le texte contenu entre les delimiteurs
  167. $this->newcode .= $this->formatspecialtok(substr($this->code, $this->i, $delimiterend - $this->i + strlen($delimiter2)), $color, $style);
  168. // On replace l'indice au bon endroit
  169. $this->i = $delimiterend + strlen($delimiter2);
  170. // Enfin on recupere le caractere en cours
  171. if ($this->i > $this->codelength) $this->char = NULL;
  172. else $this->char = $this->code[$this->i];
  173. // On precise qu'on a trouve
  174. return TRUE;
  175. } //if
  176. } // if
  177. } // foreach
  178. return FALSE;
  179. }
  180. /******************************/
  181. /* On traite les cas speciaux */
  182. /******************************/
  183. function parsearrays()
  184. {
  185. $haschanged = TRUE;
  186. // A chaque changement, on redemarre la boucle entiere
  187. while($haschanged){
  188. // On regarde si on ne tombe pas sur un delimiteur de commentaire
  189. $haschanged = $this->parsearray($this->preprocdelimiters, $this->colorpreproc, $this->stylepreproc);
  190. if (!$haschanged) {
  191. // On regarde si on ne tombe pas sur un delimiteur de commentaire
  192. $haschanged = $this->parsearray($this->commentdelimiters, $this->colorcomment, $this->stylecomment);
  193. if (!$haschanged) {
  194. // Ou de chaine de caractere
  195. $haschanged = $this->parsearray($this->stringdelimiters, $this->colorstring, $this->stylestring);
  196. } // if
  197. } // if
  198. } // while
  199. } // parsearrays
  200. function dump($var,$name){
  201. // echo "<pre>$name = \n";
  202. // print_r($var);
  203. // echo "</pre><br />";
  204. }
  205. function trace($msg){
  206. error_log("$msg");
  207. }
  208. /***************************/
  209. /*Analyse the complete code */
  210. /***************************/
  211. function analysecode($text)
  212. {
  213. // Initialize variables
  214. $this->newcode = "";
  215. $this->tok = "";
  216. $this->char = NULL;
  217. $this->code = $text;
  218. $this->codelength = strlen($this->code);
  219. $this->trace("debut analysecode");
  220. $this->dump($this->codelength,"codelength");
  221. $this->dump($this->code,"code");
  222. for ($this->i = 0; $this->i < $this->codelength; $this->i++ ) {
  223. $this->dump($this->i,"i");
  224. $this->char = $this->code[$this->i];
  225. $this->dump($this->char,"char");
  226. // On regarde si on tombe sur un cas special
  227. $this->parsearrays();
  228. // On regarde si on est arrive au bout de la chaine
  229. if ($this->char == NULL) return $this->newcode;
  230. // On a fini d'analyser les commentaires, on regarde si on a un mot complet
  231. if (is_integer(strpos($this->tokdelimiters, $this->char))) {
  232. // On tombe sur un delimiteur, on coupe le mot
  233. $this->newcode .= $this->analyseword($this->tok);
  234. // On formatte le delimiteur
  235. if ($this->visiblechar($this->char)) $this->newcode .= $this->formatspecialtok($this->char, $this->colorsymbol, $this->stylesymbol);
  236. else $this->newcode .= $this->char;
  237. // On remet a 0 le mot en cours
  238. $this->tok = "";
  239. }
  240. else {// On n'a pas de mot complet, on complete le mot
  241. $this->tok .= $this->char;
  242. }
  243. } // for
  244. // On regarde si on arrive au bout du code
  245. if (!empty($this->tok)) $this->newcode .= $this->analyseword($this->tok);
  246. return $this->newcode;
  247. }
  248. }
  249. ?>