Source for file parser.php

Documentation is available at parser.php

  1. <?php
  2. /**
  3.  * @package     Joomla.Administrator
  4.  * @subpackage  com_finder
  5.  *
  6.  * @copyright   Copyright (C) 2005 - 2013 Open Source Matters, Inc. All rights reserved.
  7.  * @license     GNU General Public License version 2 or later; see LICENSE
  8.  */
  9.  
  10. defined('_JEXEC'or die;
  11.  
  12. /**
  13.  * Parser base class for the Finder indexer package.
  14.  *
  15.  * @package     Joomla.Administrator
  16.  * @subpackage  com_finder
  17.  * @since       2.5
  18.  */
  19. abstract class FinderIndexerParser
  20. {
  21.     /**
  22.      * Method to get a parser, creating it if necessary.
  23.      *
  24.      * @param   string  $format  The type of parser to load.
  25.      *
  26.      * @return  FinderIndexerParser  A FinderIndexerParser instance.
  27.      *
  28.      * @since   2.5
  29.      * @throws  Exception on invalid parser.
  30.      */
  31.     public static function getInstance($format)
  32.     {
  33.         static $instances;
  34.  
  35.         // Only create one parser for each format.
  36.         if (isset($instances[$format]))
  37.         {
  38.             return $instances[$format];
  39.         }
  40.  
  41.         // Create an array of instances if necessary.
  42.         if (!is_array($instances))
  43.         {
  44.             $instances array();
  45.         }
  46.  
  47.         // Setup the adapter for the parser.
  48.         $format JFilterInput::getInstance()->clean($format'cmd');
  49.         $path = __DIR__ . '/parser/' $format '.php';
  50.         $class 'FinderIndexerParser' ucfirst($format);
  51.  
  52.         // Check if a parser exists for the format.
  53.         if (file_exists($path))
  54.         {
  55.             // Instantiate the parser.
  56.             include_once $path;
  57.             $instances[$formatnew $class;
  58.         }
  59.         else
  60.         {
  61.             // Throw invalid format exception.
  62.             throw new Exception(JText::sprintf('COM_FINDER_INDEXER_INVALID_PARSER'$format));
  63.         }
  64.  
  65.         return $instances[$format];
  66.     }
  67.  
  68.     /**
  69.      * Method to parse input and extract the plain text. Because this method is
  70.      * called from both inside and outside the indexer, it needs to be able to
  71.      * batch out its parsing functionality to deal with the inefficiencies of
  72.      * regular expressions. We will parse recursively in 2KB chunks.
  73.      *
  74.      * @param   string  $input  The input to parse.
  75.      *
  76.      * @return  string  The plain text input.
  77.      *
  78.      * @since   2.5
  79.      */
  80.     public function parse($input)
  81.     {
  82.         $return null;
  83.  
  84.         // Parse the input in batches if bigger than 2KB.
  85.         if (strlen($input2048)
  86.         {
  87.             $start 0;
  88.             $end strlen($input);
  89.             $chunk 2048;
  90.  
  91.             while ($start $end)
  92.             {
  93.                 // Setup the string.
  94.                 $string substr($input$start$chunk);
  95.  
  96.                 // Find the last space character if we aren't at the end.
  97.                 $ls (($start $chunk$end strrpos($string' 'false);
  98.  
  99.                 // Truncate to the last space character.
  100.                 if ($ls !== false)
  101.                 {
  102.                     $string substr($string0$ls);
  103.                 }
  104.  
  105.                 // Adjust the start position for the next iteration.
  106.                 $start += ($ls !== false ($ls $chunk$chunk $chunk);
  107.  
  108.                 // Parse the chunk.
  109.                 $return .= $this->process($string);
  110.             }
  111.         }
  112.         // The input is less than 2KB so we can parse it efficiently.
  113.         else
  114.         {
  115.             // Parse the chunk.
  116.             $return .= $this->process($input);
  117.         }
  118.  
  119.         return $return;
  120.     }
  121.  
  122.     /**
  123.      * Method to process input and extract the plain text.
  124.      *
  125.      * @param   string  $input  The input to process.
  126.      *
  127.      * @return  string  The plain text input.
  128.      *
  129.      * @since   2.5
  130.      */
  131.     abstract protected function process($input);
  132. }

Documentation generated on Tue, 19 Nov 2013 15:10:18 +0100 by phpDocumentor 1.4.3