[geeklog-cvs] Translator StringExtractor.class.php,1.2,1.3

tony at iowaoutdoors.org tony at iowaoutdoors.org
Tue Jul 20 01:44:20 EDT 2004


Update of /var/cvs/Translator
In directory www:/tmp/cvs-serv2549

Modified Files:
	StringExtractor.class.php 
Log Message:
Now uses libxml2 features in PHP5.  Code is much cleaner and should be faster.

Index: StringExtractor.class.php
===================================================================
RCS file: /var/cvs/Translator/StringExtractor.class.php,v
retrieving revision 1.2
retrieving revision 1.3
diff -C2 -d -r1.2 -r1.3
*** StringExtractor.class.php	19 Jul 2004 20:38:33 -0000	1.2
--- StringExtractor.class.php	20 Jul 2004 05:44:16 -0000	1.3
***************
*** 68,77 ****
      * @var array
      */
-     private $outputFp = null;
-     
-     /**
-     * @access private
-     * @var array
-     */
      private $appName = null;
      
--- 68,71 ----
***************
*** 107,110 ****
--- 101,111 ----
      
      /**
+     * Handle to XML Document Object Model handler
+     * @access private
+     * @var object
+     */
+     private $dom = null;
+     
+     /**
      * Constructor
      *
***************
*** 117,120 ****
--- 118,122 ----
      public function __construct($nativeLang)
      {
+         $this->dom = new DomDocument();
          $this->validExtensions = Array();
          $this->codeDirs = Array();
***************
*** 179,183 ****
                  $fileContents = file_get_contents($dirName . $curFile);
                  preg_match_all("/.->translate\((['\"](.*?)['\"])(,['\"](.*?)['\"])?\);/im", $fileContents, $results);
!                 $matches = $results[1];
                  $sections = $results[4];
                  if ($this->commandLineMode) {
--- 181,185 ----
                  $fileContents = file_get_contents($dirName . $curFile);
                  preg_match_all("/.->translate\((['\"](.*?)['\"])(,['\"](.*?)['\"])?\);/im", $fileContents, $results);
!                 $matches = $results[2];
                  $sections = $results[4];
                  if ($this->commandLineMode) {
***************
*** 186,190 ****
                  }
                  
!                 $this->addStrings($matches, $sections);
              }
          }
--- 188,203 ----
                  }
                  
!                 $numStrings = count($matches);
!                 
!                 for ($i = 0; $i < $numStrings; $i++) {
!                     // NOTE: following line will convert only &,< and > quotes are going to be OK
!                     // within XML tags
!                     $curString = htmlspecialchars($matches[$i], ENT_NOQUOTES);
!                     if (empty($sections[$i])) {
!                         $this->stringHolder['GLOBAL'][] = $matches[$i];
!                     } else {
!                         $this->stringHolder[strtoupper($sections[$i])][] = $matches[$i];
!                     }
!                 }
              }
          }
***************
*** 193,280 ****
      
      /**
!     * Writes a set of strings to the native XML file
      *
!     * This function is called as we find strings needing translation in the
!     * code files.  This method will actually add the appropriate XML for the
!     * string found.
      *
!     * @author Tony Bibbs <tony at geeklog.net>
      * @access private
-     * @param array $matches Array of strings
      *
      */
!     private function addStrings($matches, $sections)
!     {
!         $numStrings = count($matches);
!         for ($i = 0; $i < $numStrings; $i++) {
!             // NOTE: following line will convert only &,< and > quotes are going to be OK within
!             // XML tags
!             $curString = htmlspecialchars($matches[$i], ENT_NOQUOTES);
!             if (empty($sections[$i])) {
!                 $this->stringHolder['GLOBAL'][] = sprintf("            <ENTRY>\n
!                     <STRING_ID>%s</STRING_ID>\n                <STRING></STRING>\n
!                     </ENTRY>\n", $matches[$i]);
!             } else {
!                 $this->stringHolder[strtoupper($sections[$i])][] = sprintf("
!                     <ENTRY>\n                <STRING_ID>%s</STRING_ID>\n                <STRING>
!                     </STRING>\n            </ENTRY>\n", $matches[$i]);
!             }
!         }
!     }
!     
!     private function writeStrings()
      {
          reset($this->stringHolder);
          $numSections = count($this->stringHolder);
          for ($i = 0; $i < $numSections; $i++) {
              $curSection = key($this->stringHolder);
!             fwrite($this->outputFp, sprintf("        <SECTION name=\"%s\">\n", $curSection));
              foreach ($this->stringHolder[$curSection] as $curString) {
!                 fwrite($this->outputFp, $curString);
              }
!             fwrite($this->outputFp, "        </SECTION>\n");
              next($this->stringHolder);
          }
!     }
!     
!     /**
!     * Creates the header entries for XML file
!     *
!     * The XML schema adds the APPLICATION, TRANSLATOR and
!     * TRANSLATION XML tags along with their attributes
!     *
!     * @author Tony Bibbs <tony at geeklog.net>
!     * @access private
!     *
!     */
!     private function addXMLHeaders()
!     {
!         fwrite($this->outputFp,
!             sprintf("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n
!                 <APPLICATION name=\"%s\" version=\"%s\">\n
!                 <TRANSLATOR name=\"%s\" email=\"%s\" />\n    <TRANSLATION language=\"%s\">\n",
!             $this->appName,
!             $this->appVersion,
!             $this->authorName,
!             $this->authorEmail,
!             $this->nativeLang));
      }
  
      /**
-     * Adds XML footer entries to XML file
-     *
-     * This method simply closes the TRANSLATION and APPLICATION
-     * tags.
-     *
-     * @author Tony Bibbs <tony at geeklog.net>
-     *
-     */
-     private function addXMLFooters()
-     {
-         fwrite($this->outputFp,"    </TRANSLATION>\n</APPLICATION>");
-         fclose($this->outputFp);
-     }
-     
-     /**
      * When called from the command line this function will
      * take any arguments passed to the getstrings shell script
--- 206,268 ----
      
      /**
!     * Build XML and writes it to a file
      *
!     * Uses the new libxml2 support in PHP5 to build the XML output and writes it to a file
      *
!     * @author Tony Bibbs <tony at geeklog.net)
      * @access private
      *
      */
!     private function writeXML()
      {
+         // Create App Node
+         $appNode = $this->dom->createElement('APPLICATION');
+         
+         // Create translator node
+         $translatorNode = $this->dom->createElement('TRANSLATOR');
+         
+         // Add translator node to application node and set attributes
+         $tmpNode = $appNode->appendChild($translatorNode);
+         $tmpNode->setAttribute('name', $this->authorName);
+         $tmpNode->setAttribute('email', $this->authorEmail);
+         
+         // Create translation Node
+         $translationNode = $this->dom->createElement('TRANSLATION');
+         
          reset($this->stringHolder);
          $numSections = count($this->stringHolder);
          for ($i = 0; $i < $numSections; $i++) {
              $curSection = key($this->stringHolder);
!             $sectionNode = $this->dom->createElement('SECTION');
              foreach ($this->stringHolder[$curSection] as $curString) {
!                 $entryNode = $this->dom->createElement('ENTRY');
!                 $stringIDNode = $this->dom->createElement('STRING_ID');
!                 $stringIDText = $this->dom->createTextNode($curString);
!                 $stringIDNode->appendChild($stringIDText);
!                 $stringNode = $this->dom->createElement('STRING');
!                 $stringText = $this->dom->createTextNode('');
!                 $stringNode->appendChild($stringText);
!                 $entryNode->appendChild($stringIDNode);
!                 $entryNode->appendChild($stringNode);
!                 $sectionNode->appendChild($entryNode);
              }
!             $tmpNode = $translationNode->appendChild($sectionNode);
!             $tmpNode->setAttribute('name', $curSection);
              next($this->stringHolder);
          }
!         // Time to wrap things up.  First write translation node and attributes
!         $tmpNode = $appNode->appendChild($translationNode);
!         $tmpNode->setAttribute('language', $this->nativeLang);
!         
!         // Finally append Application Node with attributes
!         $tmpNode = $this->dom->appendChild($appNode);
!         $tmpNode->setAttribute('name', $this->appName);
!         $tmpNode->setAttribute('version', $this->appVersion);
!         
!         // Now write to a file
!         $this->dom->save($this->outputFile); exit;
      }
  
      /**
      * When called from the command line this function will
      * take any arguments passed to the getstrings shell script
***************
*** 441,444 ****
--- 429,435 ----
      {
          $this->codeDir = $path;
+         
+         // Now get all sub directories
+         $this->getDirs(); 
      }
      
***************
*** 464,468 ****
          $this->outputDir = $path;
          $this->outputFile = $path . $this->nativeLang . '.xml';
-         $this->outputFp = fopen($this->outputFile,'w');
      }
      
--- 455,458 ----
***************
*** 518,528 ****
          }
          
!         $this->getDirs();
!         $this->addXMLHeaders();
          foreach ($this->codeDirs as $nextDir) {
              $this->processFiles($nextDir);
          }
!         $this->writeStrings();
!         $this->addXMLFooters();
          
          if ($this->commandLineMode) {
--- 508,518 ----
          }
          
!         // Now process all files
          foreach ($this->codeDirs as $nextDir) {
              $this->processFiles($nextDir);
          }
!         
!         // Write XML
!         $this->writeXML();
          
          if ($this->commandLineMode) {




More information about the geeklog-cvs mailing list