[ Index ]
 

Code source de eZ Publish 3.9.0

Accédez au Source d'autres logiciels libresSoutenez Angelica Josefina !

title

Body

[fermer]

/lib/ezi18n/classes/ -> ezcodepagemapper.php (source)

   1  <?php
   2  //
   3  // Definition of eZCodePageMapper class
   4  //
   5  // Created on: <11-Jul-2002 15:39:41 amos>
   6  //
   7  // SOFTWARE NAME: eZ publish
   8  // SOFTWARE RELEASE: 3.9.0
   9  // BUILD VERSION: 17785
  10  // COPYRIGHT NOTICE: Copyright (C) 1999-2006 eZ systems AS
  11  // SOFTWARE LICENSE: GNU General Public License v2.0
  12  // NOTICE: >
  13  //   This program is free software; you can redistribute it and/or
  14  //   modify it under the terms of version 2.0  of the GNU General
  15  //   Public License as published by the Free Software Foundation.
  16  //
  17  //   This program is distributed in the hope that it will be useful,
  18  //   but WITHOUT ANY WARRANTY; without even the implied warranty of
  19  //   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  20  //   GNU General Public License for more details.
  21  //
  22  //   You should have received a copy of version 2.0 of the GNU General
  23  //   Public License along with this program; if not, write to the Free
  24  //   Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  25  //   MA 02110-1301, USA.
  26  //
  27  //
  28  
  29  /*! \file ezcodepagemapper.php
  30  */
  31  
  32  /*!
  33    \class eZCodePageMapper ezcodepagemapper.php
  34    \brief The class eZCodePageMapper does
  35  
  36  */
  37  
  38  include_once ( "lib/ezutils/classes/ezdebug.php" );
  39  include_once ( "lib/ezi18n/classes/ezcodepage.php" );
  40  
  41  define( "EZ_CODEPAGE_MAPPER_CACHE_CODE_DATE", 1026316422 );
  42  
  43  class eZCodePageMapper
  44  {
  45      /*!
  46       Constructor
  47      */
  48      function eZCodePageMapper( $input_charset_code, $output_charset_code, $use_cache = true )
  49      {
  50          $this->RequestedInputCharsetCode = $input_charset_code;
  51          $this->InputCharsetCode = eZCharsetInfo::realCharsetCode( $input_charset_code );
  52          $this->RequestedOutputCharsetCode = $output_charset_code;
  53          $this->OutputCharsetCode = eZCharsetInfo::realCharsetCode( $output_charset_code );
  54          $this->Valid = false;
  55          $this->load( $use_cache );
  56          $this->setSubstituteCharacter( 63 ); // ?
  57      }
  58  
  59      function isValid()
  60      {
  61          return $this->Valid;
  62      }
  63  
  64      function &mapInputCode( $in_code )
  65      {
  66          if ( isset( $this->InputOutputMap[$in_code] ) )
  67              return $this->InputOutputMap[$in_code];
  68          $retValue = null;
  69          return $retValue;
  70      }
  71  
  72      function &mapOutputCode( $out_code )
  73      {
  74          if ( isset( $this->OutputInputMap[$out_code] ) )
  75              return $this->OutputInputMap[$out_code];
  76          $retValue = null;
  77          return $retValue;
  78      }
  79  
  80      function mapInputChar( $in_char )
  81      {
  82          $in_code = ord( $in_char );
  83          if ( isset( $this->InputOutputMap[$in_code] ) )
  84              return chr( $this->InputOutputMap[$in_code] );
  85          return $this->SubstituteOutputChar;
  86      }
  87  
  88      function mapOutputChar( $out_char )
  89      {
  90          $out_code = ord( $out_char );
  91          if ( isset( $this->OutputInputMap[$out_code] ) )
  92              return chr( $this->OutputInputMap[$out_code] );
  93          return $this->SubstituteInputChar;
  94      }
  95  
  96      function substituteCharacterFor( $char )
  97      {
  98      }
  99  
 100      function substituteCharacter()
 101      {
 102          return $this->SubstituteCharValue;
 103      }
 104  
 105      function convertString( $str )
 106      {
 107          $out = "";
 108          $len = strlen( $str );
 109          for ( $i = 0; $i < $len; ++$i )
 110          {
 111              $char = $str[$i];
 112              $out .= $this->mapInputChar( $char );
 113          }
 114          return $out;
 115      }
 116  
 117      function strlen( $str )
 118      {
 119          return strlen( $str );
 120      }
 121  
 122      function strpos( $haystack, $needle, $offset = 0 )
 123      {
 124          return strpos( $haystack, $needle, $offset );
 125      }
 126  
 127      function strrpos( $haystack, $needle )
 128      {
 129          return strrpos( $haystack, $needle );
 130      }
 131  
 132      function substr( $str, $start, $length )
 133      {
 134          return substr( $str, $start, $length );
 135      }
 136  
 137      function setSubstituteCharacter( $char_code )
 138      {
 139          $this->SubstituteCharValue = $char_code;
 140          $input_codepage =& eZCodePage::instance( $this->InputCharsetCode );
 141          $output_codepage =& eZCodePage::instance( $this->OutputCharsetCode );
 142          if ( !$input_codepage->isValid() )
 143          {
 144              eZDebug::writeError( "Input codepage for " . $this->InputCharsetCode . " is not valid", "eZCodePageMapper" );
 145              return false;
 146          }
 147          if ( !$output_codepage->isValid() )
 148          {
 149              eZDebug::writeError( "Output codepage for " . $this->OutputCharsetCode . " is not valid", "eZCodePageMapper" );
 150              return false;
 151          }
 152          $this->SubstituteInputChar = chr( $input_codepage->unicodeToCode( $char_code ) );
 153          $this->SubstituteOutputChar = chr( $output_codepage->unicodeToCode( $char_code ) );
 154      }
 155  
 156      function load( $use_cache = true )
 157      {
 158          $cache_dir = "var/cache/codepages/";
 159          $cache_filename = md5( $this->InputCharsetCode . $this->OutputCharsetCode );
 160          $cache = $cache_dir . $cache_filename . ".php";
 161  
 162          if ( !eZCodePage::exists( $this->InputCharsetCode ) )
 163          {
 164              $input_file = eZCodePage::fileName( $this->InputCharsetCode );
 165              eZDebug::writeWarning( "Couldn't load input codepage file $input_file", "eZCodePageMapper" );
 166              return false;
 167          }
 168          if ( !eZCodePage::exists( $this->OutputCharsetCode ) )
 169          {
 170              $output_file = eZCodePage::fileName( $this->OutputCharsetCode );
 171              eZDebug::writeWarning( "Couldn't load output codepage file $output_file", "eZCodePageMapper" );
 172              return false;
 173          }
 174  
 175          $this->Valid = false;
 176          if ( file_exists( $cache ) and $use_cache )
 177          {
 178              $cache_m = filemtime( $cache );
 179              if ( eZCodePage::fileModification( $this->InputCharsetCode ) <= $cache_m and
 180                   eZCodePage::fileModification( $this->OutputCharsetCode ) <= $cache_m )
 181              {
 182                  unset( $eZCodePageMapperCacheCodeDate );
 183                  $in_out_map =& $this->InputOutputMap;
 184                  $out_in_map =& $this->OutputInputMap;
 185                  include( $cache );
 186                  if ( isset( $eZCodePageMapperCacheCodeDate ) or
 187                       $eZCodePageMapperCacheCodeDate == EZ_CODEPAGE_MAPPER_CACHE_CODE_DATE )
 188                  {
 189                      $this->Valid = true;
 190                      return;
 191                  }
 192              }
 193          }
 194  
 195          $this->InputOutputMap = array();
 196          $this->OutputInputMap = array();
 197  
 198          $input_codepage =& eZCodePage::instance( $this->InputCharsetCode );
 199          $output_codepage =& eZCodePage::instance( $this->OutputCharsetCode );
 200  
 201          if ( !$input_codepage->isValid() )
 202          {
 203              eZDebug::writeError( "Input codepage for " . $this->InputCharsetCode . " is not valid", "eZCodePageMapper" );
 204              return false;
 205          }
 206          if ( !$output_codepage->isValid() )
 207          {
 208              eZDebug::writeError( "Output codepage for " . $this->OutputCharsetCode . " is not valid", "eZCodePageMapper" );
 209              return false;
 210          }
 211  
 212          $min = max( $input_codepage->minCharValue(),
 213                      $output_codepage->minCharValue() );
 214          $max = min( $input_codepage->maxCharValue(),
 215                      $output_codepage->maxCharValue() );
 216  
 217          for ( $i = $min; $i <= $max; ++$i )
 218          {
 219              $code = $i;
 220              $unicode = $input_codepage->codeToUnicode( $code );
 221              if ( $unicode !== null )
 222              {
 223                  $output_code = $output_codepage->unicodeToCode( $unicode );
 224                  if ( $output_code !== null )
 225                  {
 226                      $this->InputOutputMap[$code] = $output_code;
 227                      $this->OutputInputMap[$output_code] = $code;
 228                  }
 229              }
 230          }
 231      }
 232  
 233      /*!
 234       Returns the only instance of the codepage mapper for $input_charset_code and $output_charset_code.
 235      */
 236      function &instance( $input_charset_code, $output_charset_code, $use_cache = true )
 237      {
 238          $cp =& $GLOBALS["eZCodePageMapper-$input_charset_code-$output_charset_code"];
 239          if ( get_class( $cp ) != "ezcodepagemapper" )
 240          {
 241              $cp = new eZCodePageMapper( $input_charset_code, $output_charset_code, $use_cache );
 242          }
 243          return $cp;
 244      }
 245  
 246  }
 247  
 248  ?>


Généré le : Sat Feb 24 10:30:04 2007 par Balluche grâce à PHPXref 0.7